Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SSL client with options |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
Mateusz Starzyk | 6c2e9b6 | 2021-05-19 17:54:54 +0200 | [diff] [blame] | 20 | #define MBEDTLS_ALLOW_PRIVATE_ACCESS |
| 21 | |
Gilles Peskine | ab7ce96 | 2021-01-05 21:27:53 +0100 | [diff] [blame] | 22 | #include "ssl_test_lib.h" |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 23 | |
Gilles Peskine | ab7ce96 | 2021-01-05 21:27:53 +0100 | [diff] [blame] | 24 | #if defined(MBEDTLS_SSL_TEST_IMPOSSIBLE) |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 25 | int main(void) |
Manuel Pégourié-Gonnard | e3c41ad | 2015-05-13 10:04:32 +0200 | [diff] [blame] | 26 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 27 | mbedtls_printf(MBEDTLS_SSL_TEST_IMPOSSIBLE); |
| 28 | mbedtls_exit(0); |
Gilles Peskine | ab7ce96 | 2021-01-05 21:27:53 +0100 | [diff] [blame] | 29 | } |
| 30 | #elif !defined(MBEDTLS_SSL_SRV_C) |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 31 | int main(void) |
Gilles Peskine | ab7ce96 | 2021-01-05 21:27:53 +0100 | [diff] [blame] | 32 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 33 | mbedtls_printf("MBEDTLS_SSL_SRV_C not defined.\n"); |
| 34 | mbedtls_exit(0); |
Manuel Pégourié-Gonnard | e3c41ad | 2015-05-13 10:04:32 +0200 | [diff] [blame] | 35 | } |
Gilles Peskine | b4df754 | 2021-01-08 21:20:09 +0100 | [diff] [blame] | 36 | #else /* !MBEDTLS_SSL_TEST_IMPOSSIBLE && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 37 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 38 | # include <stdint.h> |
Andres AG | 0b736db | 2017-02-08 14:05:57 +0000 | [diff] [blame] | 39 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 40 | # if !defined(_MSC_VER) |
| 41 | # include <inttypes.h> |
| 42 | # endif |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 43 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 44 | # if !defined(_WIN32) |
| 45 | # include <signal.h> |
| 46 | # endif |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 47 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 48 | # if defined(MBEDTLS_SSL_CACHE_C) |
| 49 | # include "mbedtls/ssl_cache.h" |
| 50 | # endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 51 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 52 | # if defined(MBEDTLS_SSL_TICKET_C) |
| 53 | # include "mbedtls/ssl_ticket.h" |
| 54 | # endif |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 55 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 56 | # if defined(MBEDTLS_SSL_COOKIE_C) |
| 57 | # include "mbedtls/ssl_cookie.h" |
| 58 | # endif |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 59 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 60 | # if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && defined(MBEDTLS_FS_IO) |
| 61 | # define SNI_OPTION |
| 62 | # endif |
Manuel Pégourié-Gonnard | e3c41ad | 2015-05-13 10:04:32 +0200 | [diff] [blame] | 63 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 64 | # if defined(_WIN32) |
| 65 | # include <windows.h> |
| 66 | # endif |
Manuel Pégourié-Gonnard | e3c41ad | 2015-05-13 10:04:32 +0200 | [diff] [blame] | 67 | |
Simon Butcher | cce68be | 2018-07-23 14:26:09 +0100 | [diff] [blame] | 68 | /* Size of memory to be allocated for the heap, when using the library's memory |
| 69 | * management and MBEDTLS_MEMORY_BUFFER_ALLOC_C is enabled. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 70 | # define MEMORY_HEAP_SIZE 120000 |
Simon Butcher | cce68be | 2018-07-23 14:26:09 +0100 | [diff] [blame] | 71 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 72 | # define DFL_SERVER_ADDR NULL |
| 73 | # define DFL_SERVER_PORT "4433" |
| 74 | # define DFL_RESPONSE_SIZE -1 |
| 75 | # define DFL_DEBUG_LEVEL 0 |
| 76 | # define DFL_NBIO 0 |
| 77 | # define DFL_EVENT 0 |
| 78 | # define DFL_READ_TIMEOUT 0 |
| 79 | # define DFL_CA_FILE "" |
| 80 | # define DFL_CA_PATH "" |
| 81 | # define DFL_CRT_FILE "" |
| 82 | # define DFL_KEY_FILE "" |
| 83 | # define DFL_KEY_PWD "" |
| 84 | # define DFL_CRT_FILE2 "" |
| 85 | # define DFL_KEY_FILE2 "" |
| 86 | # define DFL_KEY_PWD2 "" |
| 87 | # define DFL_ASYNC_OPERATIONS "-" |
| 88 | # define DFL_ASYNC_PRIVATE_DELAY1 (-1) |
| 89 | # define DFL_ASYNC_PRIVATE_DELAY2 (-1) |
| 90 | # define DFL_ASYNC_PRIVATE_ERROR (0) |
| 91 | # define DFL_PSK "" |
| 92 | # define DFL_PSK_OPAQUE 0 |
| 93 | # define DFL_PSK_LIST_OPAQUE 0 |
| 94 | # define DFL_PSK_IDENTITY "Client_identity" |
| 95 | # define DFL_ECJPAKE_PW NULL |
| 96 | # define DFL_PSK_LIST NULL |
| 97 | # define DFL_FORCE_CIPHER 0 |
| 98 | # define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED |
| 99 | # define DFL_ALLOW_LEGACY -2 |
| 100 | # define DFL_RENEGOTIATE 0 |
| 101 | # define DFL_RENEGO_DELAY -2 |
| 102 | # define DFL_RENEGO_PERIOD ((uint64_t)-1) |
| 103 | # define DFL_EXCHANGES 1 |
| 104 | # define DFL_MIN_VERSION -1 |
| 105 | # define DFL_MAX_VERSION -1 |
| 106 | # define DFL_SHA1 -1 |
| 107 | # define DFL_CID_ENABLED 0 |
| 108 | # define DFL_CID_VALUE "" |
| 109 | # define DFL_CID_ENABLED_RENEGO -1 |
| 110 | # define DFL_CID_VALUE_RENEGO NULL |
| 111 | # define DFL_AUTH_MODE -1 |
| 112 | # define DFL_CERT_REQ_CA_LIST MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED |
| 113 | # define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE |
| 114 | # define DFL_TRUNC_HMAC -1 |
| 115 | # define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED |
| 116 | # define DFL_TICKET_TIMEOUT 86400 |
| 117 | # define DFL_CACHE_MAX -1 |
| 118 | # define DFL_CACHE_TIMEOUT -1 |
| 119 | # define DFL_SNI NULL |
| 120 | # define DFL_ALPN_STRING NULL |
| 121 | # define DFL_CURVES NULL |
| 122 | # define DFL_DHM_FILE NULL |
| 123 | # define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM |
| 124 | # define DFL_COOKIES 1 |
| 125 | # define DFL_ANTI_REPLAY -1 |
| 126 | # define DFL_HS_TO_MIN 0 |
| 127 | # define DFL_HS_TO_MAX 0 |
| 128 | # define DFL_DTLS_MTU -1 |
| 129 | # define DFL_BADMAC_LIMIT -1 |
| 130 | # define DFL_DGRAM_PACKING 1 |
| 131 | # define DFL_EXTENDED_MS -1 |
| 132 | # define DFL_ETM -1 |
| 133 | # define DFL_SERIALIZE 0 |
| 134 | # define DFL_CONTEXT_FILE "" |
| 135 | # define DFL_EXTENDED_MS_ENFORCE -1 |
| 136 | # define DFL_CA_CALLBACK 0 |
| 137 | # define DFL_EAP_TLS 0 |
| 138 | # define DFL_REPRODUCIBLE 0 |
| 139 | # define DFL_NSS_KEYLOG 0 |
| 140 | # define DFL_NSS_KEYLOG_FILE NULL |
| 141 | # define DFL_QUERY_CONFIG_MODE 0 |
| 142 | # define DFL_USE_SRTP 0 |
| 143 | # define DFL_SRTP_FORCE_PROFILE 0 |
| 144 | # define DFL_SRTP_SUPPORT_MKI 0 |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 145 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 146 | # define LONG_RESPONSE \ |
| 147 | "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \ |
| 148 | "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \ |
| 149 | "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \ |
| 150 | "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \ |
| 151 | "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \ |
| 152 | "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \ |
| 153 | "07-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah</p>\r\n" |
Manuel Pégourié-Gonnard | bd7ce63 | 2013-07-17 15:34:17 +0200 | [diff] [blame] | 154 | |
Paul Bakker | 8e714d7 | 2013-07-18 11:05:13 +0200 | [diff] [blame] | 155 | /* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer |
| 156 | * packets (for fragmentation purposes) */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 157 | # define HTTP_RESPONSE \ |
| 158 | "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \ |
| 159 | "<h2>mbed TLS Test Server</h2>\r\n" \ |
| 160 | "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 161 | |
Manuel Pégourié-Gonnard | 95c0a63 | 2014-06-11 18:32:36 +0200 | [diff] [blame] | 162 | /* |
| 163 | * Size of the basic I/O buffer. Able to hold our default response. |
| 164 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 165 | * You will need to adapt the mbedtls_ssl_get_bytes_avail() test in ssl-opt.sh |
Manuel Pégourié-Gonnard | 95c0a63 | 2014-06-11 18:32:36 +0200 | [diff] [blame] | 166 | * if you change this value to something outside the range <= 100 or > 500 |
| 167 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 168 | # define DFL_IO_BUF_LEN 200 |
Manuel Pégourié-Gonnard | e7a3b10 | 2014-06-11 18:21:20 +0200 | [diff] [blame] | 169 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 170 | # if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 171 | # if defined(MBEDTLS_FS_IO) |
| 172 | # define USAGE_IO \ |
| 173 | " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \ |
| 174 | " default: \"\" (pre-loaded)\n" \ |
| 175 | " use \"none\" to skip loading any top-level CAs.\n" \ |
| 176 | " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \ |
| 177 | " default: \"\" (pre-loaded) (overrides ca_file)\n" \ |
| 178 | " use \"none\" to skip loading any top-level CAs.\n" \ |
| 179 | " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \ |
| 180 | " default: see note after key_file2\n" \ |
| 181 | " key_file=%%s default: see note after key_file2\n" \ |
| 182 | " key_pwd=%%s Password for key specified by key_file argument\n" \ |
| 183 | " default: none\n" \ |
| 184 | " crt_file2=%%s Your second cert and chain (in bottom to top order, top may be omitted)\n" \ |
| 185 | " default: see note after key_file2\n" \ |
| 186 | " key_file2=%%s default: see note below\n" \ |
| 187 | " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \ |
| 188 | " preloaded certificate(s) and key(s) are used if available\n" \ |
| 189 | " key_pwd2=%%s Password for key specified by key_file2 argument\n" \ |
| 190 | " default: none\n" \ |
| 191 | " dhm_file=%%s File containing Diffie-Hellman parameters\n" \ |
| 192 | " default: preloaded parameters\n" |
| 193 | # else |
| 194 | # define USAGE_IO \ |
| 195 | "\n" \ |
| 196 | " No file operations available (MBEDTLS_FS_IO not defined)\n" \ |
| 197 | "\n" |
| 198 | # endif /* MBEDTLS_FS_IO */ |
| 199 | # else |
| 200 | # define USAGE_IO "" |
| 201 | # endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 202 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 203 | # if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 204 | # define USAGE_SSL_ASYNC \ |
| 205 | " async_operations=%%c... d=decrypt, s=sign (default: -=off)\n" \ |
| 206 | " async_private_delay1=%%d Asynchronous delay for key_file or preloaded key\n" \ |
| 207 | " async_private_delay2=%%d Asynchronous delay for key_file2 and sni\n" \ |
| 208 | " default: -1 (not asynchronous)\n" \ |
| 209 | " async_private_error=%%d Async callback error injection (default=0=none,\n" \ |
| 210 | " 1=start, 2=cancel, 3=resume, negative=first time only)" |
| 211 | # else |
| 212 | # define USAGE_SSL_ASYNC "" |
| 213 | # endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 214 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 215 | # if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 216 | # define USAGE_CID \ |
| 217 | " cid=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension.\n" \ |
| 218 | " default: 0 (disabled)\n" \ |
| 219 | " cid_renego=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension during renegotiation.\n" \ |
| 220 | " default: same as 'cid' parameter\n" \ |
| 221 | " cid_val=%%s The CID to use for incoming messages (in hex, without 0x).\n" \ |
| 222 | " default: \"\"\n" \ |
| 223 | " cid_val_renego=%%s The CID to use for incoming messages (in hex, without 0x) after renegotiation.\n" \ |
| 224 | " default: same as 'cid_val' parameter\n" |
| 225 | # else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 226 | # define USAGE_CID "" |
| 227 | # endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | a7d2542 | 2019-04-09 17:28:10 +0100 | [diff] [blame] | 228 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 229 | # if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
| 230 | # define USAGE_PSK_RAW \ |
| 231 | " psk=%%s default: \"\" (disabled)\n" \ |
| 232 | " The PSK values are in hex, without 0x.\n" \ |
| 233 | " psk_list=%%s default: \"\"\n" \ |
| 234 | " A list of (PSK identity, PSK value) pairs.\n" \ |
| 235 | " The PSK values are in hex, without 0x.\n" \ |
| 236 | " id1,psk1[,id2,psk2[,...]]\n" \ |
| 237 | " psk_identity=%%s default: \"Client_identity\"\n" |
| 238 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 239 | # define USAGE_PSK_SLOT \ |
| 240 | " psk_opaque=%%d default: 0 (don't use opaque static PSK)\n" \ |
| 241 | " Enable this to store the PSK configured through command line\n" \ |
| 242 | " parameter `psk` in a PSA-based key slot.\n" \ |
| 243 | " Note: Currently only supported in conjunction with\n" \ |
| 244 | " the use of min_version to force TLS 1.2 and force_ciphersuite \n" \ |
| 245 | " to force a particular PSK-only ciphersuite.\n" \ |
| 246 | " Note: This is to test integration of PSA-based opaque PSKs with\n" \ |
| 247 | " Mbed TLS only. Production systems are likely to configure Mbed TLS\n" \ |
| 248 | " with prepopulated key slots instead of importing raw key material.\n" \ |
| 249 | " psk_list_opaque=%%d default: 0 (don't use opaque dynamic PSKs)\n" \ |
| 250 | " Enable this to store the list of dynamically chosen PSKs configured\n" \ |
| 251 | " through the command line parameter `psk_list` in PSA-based key slots.\n" \ |
| 252 | " Note: Currently only supported in conjunction with\n" \ |
| 253 | " the use of min_version to force TLS 1.2 and force_ciphersuite \n" \ |
| 254 | " to force a particular PSK-only ciphersuite.\n" \ |
| 255 | " Note: This is to test integration of PSA-based opaque PSKs with\n" \ |
| 256 | " Mbed TLS only. Production systems are likely to configure Mbed TLS\n" \ |
| 257 | " with prepopulated key slots instead of importing raw key material.\n" |
| 258 | # else |
| 259 | # define USAGE_PSK_SLOT "" |
| 260 | # endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 261 | # define USAGE_PSK USAGE_PSK_RAW USAGE_PSK_SLOT |
| 262 | # else |
| 263 | # define USAGE_PSK "" |
| 264 | # endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
| 265 | # if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 266 | # define USAGE_CA_CALLBACK \ |
| 267 | " ca_callback=%%d default: 0 (disabled)\n" \ |
| 268 | " Enable this to use the trusted certificate callback function\n" |
| 269 | # else |
| 270 | # define USAGE_CA_CALLBACK "" |
| 271 | # endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 272 | # if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 273 | # define USAGE_TICKETS \ |
| 274 | " tickets=%%d default: 1 (enabled)\n" \ |
| 275 | " ticket_timeout=%%d default: 86400 (one day)\n" |
| 276 | # else |
| 277 | # define USAGE_TICKETS "" |
| 278 | # endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 279 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 280 | # if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 281 | # define USAGE_EAP_TLS " eap_tls=%%d default: 0 (disabled)\n" |
| 282 | # define USAGE_NSS_KEYLOG \ |
| 283 | " nss_keylog=%%d default: 0 (disabled)\n" \ |
| 284 | " This cannot be used with eap_tls=1\n" |
| 285 | # define USAGE_NSS_KEYLOG_FILE " nss_keylog_file=%%s\n" |
| 286 | # if defined(MBEDTLS_SSL_DTLS_SRTP) |
| 287 | # define USAGE_SRTP \ |
| 288 | " use_srtp=%%d default: 0 (disabled)\n" \ |
| 289 | " srtp_force_profile=%%d default: 0 (all enabled)\n" \ |
| 290 | " available profiles:\n" \ |
| 291 | " 1 - SRTP_AES128_CM_HMAC_SHA1_80\n" \ |
| 292 | " 2 - SRTP_AES128_CM_HMAC_SHA1_32\n" \ |
| 293 | " 3 - SRTP_NULL_HMAC_SHA1_80\n" \ |
| 294 | " 4 - SRTP_NULL_HMAC_SHA1_32\n" \ |
| 295 | " support_mki=%%d default: 0 (not supported)\n" |
| 296 | # else /* MBEDTLS_SSL_DTLS_SRTP */ |
| 297 | # define USAGE_SRTP "" |
| 298 | # endif |
| 299 | # else /* MBEDTLS_SSL_EXPORT_KEYS */ |
| 300 | # define USAGE_EAP_TLS "" |
| 301 | # define USAGE_NSS_KEYLOG "" |
| 302 | # define USAGE_NSS_KEYLOG_FILE "" |
| 303 | # define USAGE_SRTP "" |
| 304 | # endif /* MBEDTLS_SSL_EXPORT_KEYS */ |
Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 305 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 306 | # if defined(MBEDTLS_SSL_CACHE_C) |
| 307 | # define USAGE_CACHE \ |
| 308 | " cache_max=%%d default: cache default (50)\n" \ |
| 309 | " cache_timeout=%%d default: cache default (1d)\n" |
| 310 | # else |
| 311 | # define USAGE_CACHE "" |
| 312 | # endif /* MBEDTLS_SSL_CACHE_C */ |
Manuel Pégourié-Gonnard | 4c88345 | 2014-02-20 21:32:41 +0100 | [diff] [blame] | 313 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 314 | # if defined(SNI_OPTION) |
| 315 | # if defined(MBEDTLS_X509_CRL_PARSE_C) |
| 316 | # define SNI_CRL ",crl" |
| 317 | # else |
| 318 | # define SNI_CRL "" |
| 319 | # endif |
Ron Eldor | 80d0419 | 2019-04-04 15:02:01 +0300 | [diff] [blame] | 320 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 321 | # define USAGE_SNI \ |
| 322 | " sni=%%s name1,cert1,key1,ca1" SNI_CRL \ |
| 323 | ",auth1[,...]\n" \ |
| 324 | " default: disabled\n" |
| 325 | # else |
| 326 | # define USAGE_SNI "" |
| 327 | # endif /* SNI_OPTION */ |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 328 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 329 | # if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 330 | # define USAGE_MAX_FRAG_LEN \ |
| 331 | " max_frag_len=%%d default: 16384 (tls default)\n" \ |
| 332 | " options: 512, 1024, 2048, 4096\n" |
| 333 | # else |
| 334 | # define USAGE_MAX_FRAG_LEN "" |
| 335 | # endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 336 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 337 | # if defined(MBEDTLS_SSL_ALPN) |
| 338 | # define USAGE_ALPN \ |
| 339 | " alpn=%%s default: \"\" (disabled)\n" \ |
| 340 | " example: spdy/1,http/1.1\n" |
| 341 | # else |
| 342 | # define USAGE_ALPN "" |
| 343 | # endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 344 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 345 | # if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) |
| 346 | # define USAGE_COOKIES \ |
| 347 | " cookies=0/1/-1 default: 1 (enabled)\n" \ |
| 348 | " 0: disabled, -1: library default (broken)\n" |
| 349 | # else |
| 350 | # define USAGE_COOKIES "" |
| 351 | # endif |
Manuel Pégourié-Gonnard | 26820e3 | 2014-07-23 19:34:59 +0200 | [diff] [blame] | 352 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 353 | # if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 354 | # define USAGE_ANTI_REPLAY \ |
| 355 | " anti_replay=0/1 default: (library default: enabled)\n" |
| 356 | # else |
| 357 | # define USAGE_ANTI_REPLAY "" |
| 358 | # endif |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 359 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 360 | # define USAGE_BADMAC_LIMIT \ |
| 361 | " badmac_limit=%%d default: (library default: disabled)\n" |
Manuel Pégourié-Gonnard | e698f59 | 2014-10-14 19:36:36 +0200 | [diff] [blame] | 362 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 363 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 364 | # define USAGE_DTLS \ |
| 365 | " dtls=%%d default: 0 (TLS)\n" \ |
| 366 | " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \ |
| 367 | " range of DTLS handshake timeouts in millisecs\n" \ |
| 368 | " mtu=%%d default: (library default: unlimited)\n" \ |
| 369 | " dgram_packing=%%d default: 1 (allowed)\n" \ |
| 370 | " allow or forbid packing of multiple\n" \ |
| 371 | " records within a single datgram.\n" |
| 372 | # else |
| 373 | # define USAGE_DTLS "" |
| 374 | # endif |
Manuel Pégourié-Gonnard | d823bd0 | 2014-10-01 14:40:56 +0200 | [diff] [blame] | 375 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 376 | # if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 377 | # define USAGE_EMS \ |
| 378 | " extended_ms=0/1 default: (library default: on)\n" |
| 379 | # else |
| 380 | # define USAGE_EMS "" |
| 381 | # endif |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 382 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 383 | # if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 384 | # define USAGE_ETM \ |
| 385 | " etm=0/1 default: (library default: on)\n" |
| 386 | # else |
| 387 | # define USAGE_ETM "" |
| 388 | # endif |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 389 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 390 | # define USAGE_REPRODUCIBLE \ |
| 391 | " reproducible=0/1 default: 0 (disabled)\n" |
Philippe Antoine | 738153a | 2019-06-18 20:16:43 +0200 | [diff] [blame] | 392 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 393 | # if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 394 | # define USAGE_RENEGO \ |
| 395 | " renegotiation=%%d default: 0 (disabled)\n" \ |
| 396 | " renegotiate=%%d default: 0 (disabled)\n" \ |
| 397 | " renego_delay=%%d default: -2 (library default)\n" \ |
| 398 | " renego_period=%%d default: (2^64 - 1 for TLS, 2^48 - 1 for DTLS)\n" |
| 399 | # else |
| 400 | # define USAGE_RENEGO "" |
| 401 | # endif |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 402 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 403 | # if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 404 | # define USAGE_ECJPAKE \ |
| 405 | " ecjpake_pw=%%s default: none (disabled)\n" |
| 406 | # else |
| 407 | # define USAGE_ECJPAKE "" |
| 408 | # endif |
Manuel Pégourié-Gonnard | 70905a7 | 2015-09-16 11:08:34 +0200 | [diff] [blame] | 409 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 410 | # if defined(MBEDTLS_ECP_C) |
| 411 | # define USAGE_CURVES \ |
| 412 | " curves=a,b,c,d default: \"default\" (library default)\n" \ |
| 413 | " example: \"secp521r1,brainpoolP512r1\"\n" \ |
| 414 | " - use \"none\" for empty list\n" \ |
| 415 | " - see mbedtls_ecp_curve_list()\n" \ |
| 416 | " for acceptable curve names\n" |
| 417 | # else |
| 418 | # define USAGE_CURVES "" |
| 419 | # endif |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 420 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 421 | # if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 422 | # define USAGE_SERIALIZATION \ |
| 423 | " serialize=%%d default: 0 (do not serialize/deserialize)\n" \ |
| 424 | " options: 1 (serialize)\n" \ |
| 425 | " 2 (serialize with re-initialization)\n" \ |
| 426 | " context_file=%%s The file path to write a serialized connection\n" \ |
| 427 | " in the form of base64 code (serialize option\n" \ |
| 428 | " must be set)\n" \ |
| 429 | " default: \"\" (do nothing)\n" \ |
| 430 | " option: a file path\n" |
| 431 | # else |
| 432 | # define USAGE_SERIALIZATION "" |
| 433 | # endif |
Jarno Lamsa | bbc7b41 | 2019-06-04 11:06:31 +0300 | [diff] [blame] | 434 | |
Gilles Peskine | b2971ff | 2020-04-14 19:41:01 +0200 | [diff] [blame] | 435 | /* USAGE is arbitrarily split to stay under the portable string literal |
| 436 | * length limit: 4095 bytes in C99. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 437 | # define USAGE1 \ |
| 438 | "\n usage: ssl_server2 param=<>...\n" \ |
| 439 | "\n acceptable parameters:\n" \ |
| 440 | " server_addr=%%s default: (all interfaces)\n" \ |
| 441 | " server_port=%%d default: 4433\n" \ |
| 442 | " debug_level=%%d default: 0 (disabled)\n" \ |
| 443 | " buffer_size=%%d default: 200 \n" \ |
| 444 | " (minimum: 1)\n" \ |
| 445 | " response_size=%%d default: about 152 (basic response)\n" \ |
| 446 | " (minimum: 0, max: 16384)\n" \ |
| 447 | " increases buffer_size if bigger\n" \ |
| 448 | " nbio=%%d default: 0 (blocking I/O)\n" \ |
| 449 | " options: 1 (non-blocking), 2 (added delays)\n" \ |
| 450 | " event=%%d default: 0 (loop)\n" \ |
| 451 | " options: 1 (level-triggered, implies nbio=1),\n" \ |
| 452 | " read_timeout=%%d default: 0 ms (no timeout)\n" \ |
| 453 | "\n" USAGE_DTLS USAGE_SRTP USAGE_COOKIES USAGE_ANTI_REPLAY \ |
| 454 | USAGE_BADMAC_LIMIT "\n" |
| 455 | # define USAGE2 \ |
| 456 | " auth_mode=%%s default: (library default: none)\n" \ |
| 457 | " options: none, optional, required\n" \ |
| 458 | " cert_req_ca_list=%%d default: 1 (send ca list)\n" \ |
| 459 | " options: 1 (send ca list), 0 (don't send)\n" USAGE_IO \ |
| 460 | "\n" USAGE_PSK USAGE_CA_CALLBACK USAGE_ECJPAKE "\n" |
| 461 | # define USAGE3 \ |
| 462 | " allow_legacy=%%d default: (library default: no)\n" USAGE_RENEGO \ |
| 463 | " exchanges=%%d default: 1\n" \ |
| 464 | "\n" USAGE_TICKETS USAGE_EAP_TLS USAGE_REPRODUCIBLE USAGE_NSS_KEYLOG \ |
| 465 | USAGE_NSS_KEYLOG_FILE USAGE_CACHE USAGE_MAX_FRAG_LEN USAGE_ALPN \ |
| 466 | USAGE_EMS USAGE_ETM USAGE_CURVES "\n" |
| 467 | # define USAGE4 \ |
| 468 | USAGE_SSL_ASYNC \ |
| 469 | USAGE_SNI \ |
| 470 | " allow_sha1=%%d default: 0\n" \ |
| 471 | " min_version=%%s default: (library default: tls1_2)\n" \ |
| 472 | " max_version=%%s default: (library default: tls1_2)\n" \ |
| 473 | " force_version=%%s default: \"\" (none)\n" \ |
| 474 | " options: tls1_2, dtls1_2\n" \ |
| 475 | "\n" \ |
| 476 | " force_ciphersuite=<name> default: all enabled\n" \ |
| 477 | " query_config=<name> return 0 if the specified\n" \ |
| 478 | " configuration macro is defined and 1\n" \ |
| 479 | " otherwise. The expansion of the macro\n" \ |
| 480 | " is printed if it is defined\n" USAGE_SERIALIZATION \ |
| 481 | " acceptable ciphersuite names:\n" |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 482 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 483 | # define ALPN_LIST_SIZE 10 |
| 484 | # define CURVE_LIST_SIZE 20 |
Hanno Becker | 8651a43 | 2017-06-09 16:13:22 +0100 | [diff] [blame] | 485 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 486 | # define PUT_UINT64_BE(out_be, in_le, i) \ |
| 487 | { \ |
| 488 | (out_be)[(i) + 0] = (unsigned char)(((in_le) >> 56) & 0xFF); \ |
| 489 | (out_be)[(i) + 1] = (unsigned char)(((in_le) >> 48) & 0xFF); \ |
| 490 | (out_be)[(i) + 2] = (unsigned char)(((in_le) >> 40) & 0xFF); \ |
| 491 | (out_be)[(i) + 3] = (unsigned char)(((in_le) >> 32) & 0xFF); \ |
| 492 | (out_be)[(i) + 4] = (unsigned char)(((in_le) >> 24) & 0xFF); \ |
| 493 | (out_be)[(i) + 5] = (unsigned char)(((in_le) >> 16) & 0xFF); \ |
| 494 | (out_be)[(i) + 6] = (unsigned char)(((in_le) >> 8) & 0xFF); \ |
| 495 | (out_be)[(i) + 7] = (unsigned char)(((in_le) >> 0) & 0xFF); \ |
| 496 | } |
Andres AG | 692ad84 | 2017-01-19 16:30:57 +0000 | [diff] [blame] | 497 | |
Manuel Pégourié-Gonnard | 6f19ce3 | 2021-06-16 12:08:34 +0200 | [diff] [blame] | 498 | /* This is global so it can be easily accessed by callback functions */ |
| 499 | rng_context_t rng; |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 500 | |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 501 | /* |
| 502 | * global options |
| 503 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 504 | struct options { |
| 505 | const char *server_addr; /* address on which the ssl service runs */ |
| 506 | const char *server_port; /* port on which the ssl service runs */ |
| 507 | int debug_level; /* level of debugging */ |
| 508 | int nbio; /* should I/O be blocking? */ |
| 509 | int event; /* loop or event-driven IO? level or edge triggered? */ |
| 510 | uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */ |
| 511 | int response_size; /* pad response with header to requested size */ |
| 512 | uint16_t buffer_size; /* IO buffer size */ |
| 513 | const char *ca_file; /* the file with the CA certificate(s) */ |
| 514 | const char *ca_path; /* the path with the CA certificate(s) reside */ |
| 515 | const char *crt_file; /* the file with the server certificate */ |
| 516 | const char *key_file; /* the file with the server key */ |
| 517 | const char *key_pwd; /* the password for the server key */ |
| 518 | const char *crt_file2; /* the file with the 2nd server certificate */ |
| 519 | const char *key_file2; /* the file with the 2nd server key */ |
| 520 | const char *key_pwd2; /* the password for the 2nd server key */ |
Gilles Peskine | fcca9d8 | 2018-01-12 13:47:48 +0100 | [diff] [blame] | 521 | const char *async_operations; /* supported SSL asynchronous operations */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 522 | int async_private_delay1; /* number of times f_async_resume needs to be |
| 523 | called for key 1, or -1 for no async */ |
| 524 | int async_private_delay2; /* number of times f_async_resume needs to be |
| 525 | called for key 2, or -1 for no async */ |
| 526 | int async_private_error; /* inject error in async private callback */ |
| 527 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | 1d911cd | 2018-11-15 13:06:09 +0000 | [diff] [blame] | 528 | int psk_opaque; |
| 529 | int psk_list_opaque; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 530 | # endif |
| 531 | # if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 532 | int ca_callback; /* Use callback for trusted certificate list */ |
| 533 | # endif |
| 534 | const char *psk; /* the pre-shared key */ |
| 535 | const char *psk_identity; /* the pre-shared key identity */ |
| 536 | char *psk_list; /* list of PSK id/key pairs for callback */ |
| 537 | const char *ecjpake_pw; /* the EC J-PAKE password */ |
| 538 | int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */ |
| 539 | int renegotiation; /* enable / disable renegotiation */ |
| 540 | int allow_legacy; /* allow legacy renegotiation */ |
| 541 | int renegotiate; /* attempt renegotiation? */ |
| 542 | int renego_delay; /* delay before enforcing renegotiation */ |
| 543 | uint64_t renego_period; /* period for automatic renegotiation */ |
| 544 | int exchanges; /* number of data exchanges */ |
| 545 | int min_version; /* minimum protocol version accepted */ |
| 546 | int max_version; /* maximum protocol version accepted */ |
| 547 | int allow_sha1; /* flag for SHA-1 support */ |
| 548 | int auth_mode; /* verify mode for connection */ |
| 549 | int cert_req_ca_list; /* should we send the CA list? */ |
| 550 | unsigned char mfl_code; /* code for maximum fragment length */ |
| 551 | int trunc_hmac; /* accept truncated hmac? */ |
| 552 | int tickets; /* enable / disable session tickets */ |
| 553 | int ticket_timeout; /* session ticket lifetime */ |
| 554 | int cache_max; /* max number of session cache entries */ |
| 555 | int cache_timeout; /* expiration delay of session cache entries */ |
| 556 | char *sni; /* string describing sni information */ |
| 557 | const char *curves; /* list of supported elliptic curves */ |
| 558 | const char *alpn_string; /* ALPN supported protocols */ |
| 559 | const char *dhm_file; /* the file with the DH parameters */ |
| 560 | int extended_ms; /* allow negotiation of extended MS? */ |
| 561 | int etm; /* allow negotiation of encrypt-then-MAC? */ |
| 562 | int transport; /* TLS or DTLS? */ |
| 563 | int cookies; /* Use cookies for DTLS? -1 to break them */ |
| 564 | int anti_replay; /* Use anti-replay for DTLS? -1 for default */ |
| 565 | uint32_t hs_to_min; /* Initial value of DTLS handshake timer */ |
| 566 | uint32_t hs_to_max; /* Max value of DTLS handshake timer */ |
| 567 | int dtls_mtu; /* UDP Maximum tranport unit for DTLS */ |
| 568 | int dgram_packing; /* allow/forbid datagram packing */ |
| 569 | int badmac_limit; /* Limit of records with bad MAC */ |
| 570 | int eap_tls; /* derive EAP-TLS keying material? */ |
| 571 | int nss_keylog; /* export NSS key log material */ |
Hanno Becker | 48f3a3d | 2019-08-29 06:31:22 +0100 | [diff] [blame] | 572 | const char *nss_keylog_file; /* NSS key log file */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 573 | int cid_enabled; /* whether to use the CID extension or not */ |
| 574 | int cid_enabled_renego; /* whether to use the CID extension or not |
| 575 | * during renegotiation */ |
| 576 | const char *cid_val; /* the CID to use for incoming messages */ |
| 577 | int serialize; /* serialize/deserialize connection */ |
| 578 | const char *context_file; /* the file to write a serialized connection |
| 579 | * in the form of base64 code (serialize |
| 580 | * option must be set) */ |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 581 | const char *cid_val_renego; /* the CID to use for incoming messages |
| 582 | * after renegotiation */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 583 | int reproducible; /* make communication reproducible */ |
| 584 | int query_config_mode; /* whether to read config */ |
| 585 | int use_srtp; /* Support SRTP */ |
| 586 | int force_srtp_profile; /* SRTP protection profile to use or all */ |
| 587 | int support_mki; /* The dtls mki mki support */ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 588 | } opt; |
| 589 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 590 | # include "ssl_test_common_source.c" |
Hanno Becker | dcc94e6 | 2019-07-03 17:05:43 +0100 | [diff] [blame] | 591 | |
Manuel Pégourié-Gonnard | fdee74b | 2014-06-10 15:15:06 +0200 | [diff] [blame] | 592 | /* |
Manuel Pégourié-Gonnard | 4d6f178 | 2015-06-19 14:40:39 +0200 | [diff] [blame] | 593 | * Return authmode from string, or -1 on error |
| 594 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 595 | static int get_auth_mode(const char *s) |
Manuel Pégourié-Gonnard | 4d6f178 | 2015-06-19 14:40:39 +0200 | [diff] [blame] | 596 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 597 | if (strcmp(s, "none") == 0) |
| 598 | return MBEDTLS_SSL_VERIFY_NONE; |
| 599 | if (strcmp(s, "optional") == 0) |
| 600 | return MBEDTLS_SSL_VERIFY_OPTIONAL; |
| 601 | if (strcmp(s, "required") == 0) |
| 602 | return MBEDTLS_SSL_VERIFY_REQUIRED; |
Manuel Pégourié-Gonnard | 4d6f178 | 2015-06-19 14:40:39 +0200 | [diff] [blame] | 603 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 604 | return -1; |
Manuel Pégourié-Gonnard | 4d6f178 | 2015-06-19 14:40:39 +0200 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | /* |
Manuel Pégourié-Gonnard | fdee74b | 2014-06-10 15:15:06 +0200 | [diff] [blame] | 608 | * Used by sni_parse and psk_parse to handle coma-separated lists |
| 609 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 610 | # define GET_ITEM(dst) \ |
| 611 | do { \ |
| 612 | (dst) = p; \ |
| 613 | while (*p != ',') \ |
| 614 | if (++p > end) \ |
| 615 | goto error; \ |
| 616 | *p++ = '\0'; \ |
| 617 | } while (0) |
Manuel Pégourié-Gonnard | fdee74b | 2014-06-10 15:15:06 +0200 | [diff] [blame] | 618 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 619 | # if defined(SNI_OPTION) |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 620 | typedef struct _sni_entry sni_entry; |
| 621 | |
| 622 | struct _sni_entry { |
| 623 | const char *name; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 624 | mbedtls_x509_crt *cert; |
| 625 | mbedtls_pk_context *key; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 626 | mbedtls_x509_crt *ca; |
| 627 | mbedtls_x509_crl *crl; |
Manuel Pégourié-Gonnard | 4d6f178 | 2015-06-19 14:40:39 +0200 | [diff] [blame] | 628 | int authmode; |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 629 | sni_entry *next; |
| 630 | }; |
| 631 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 632 | void sni_free(sni_entry *head) |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 633 | { |
| 634 | sni_entry *cur = head, *next; |
| 635 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 636 | while (cur != NULL) { |
| 637 | mbedtls_x509_crt_free(cur->cert); |
| 638 | mbedtls_free(cur->cert); |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 639 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 640 | mbedtls_pk_free(cur->key); |
| 641 | mbedtls_free(cur->key); |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 642 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 643 | mbedtls_x509_crt_free(cur->ca); |
| 644 | mbedtls_free(cur->ca); |
| 645 | # if defined(MBEDTLS_X509_CRL_PARSE_C) |
| 646 | mbedtls_x509_crl_free(cur->crl); |
| 647 | mbedtls_free(cur->crl); |
| 648 | # endif |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 649 | next = cur->next; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 650 | mbedtls_free(cur); |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 651 | cur = next; |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | /* |
Manuel Pégourié-Gonnard | 4d6f178 | 2015-06-19 14:40:39 +0200 | [diff] [blame] | 656 | * Parse a string of sextuples name1,crt1,key1,ca1,crl1,auth1[,...] |
| 657 | * into a usable sni_entry list. For ca1, crl1, auth1, the special value |
| 658 | * '-' means unset. If ca1 is unset, then crl1 is ignored too. |
Manuel Pégourié-Gonnard | 5c078e1 | 2015-02-14 13:56:39 +0000 | [diff] [blame] | 659 | * |
| 660 | * Modifies the input string! This is not production quality! |
| 661 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 662 | sni_entry *sni_parse(char *sni_string) |
Manuel Pégourié-Gonnard | 5c078e1 | 2015-02-14 13:56:39 +0000 | [diff] [blame] | 663 | { |
| 664 | sni_entry *cur = NULL, *new = NULL; |
| 665 | char *p = sni_string; |
| 666 | char *end = p; |
Ron Eldor | 80d0419 | 2019-04-04 15:02:01 +0300 | [diff] [blame] | 667 | char *crt_file, *key_file, *ca_file, *auth_str; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 668 | # if defined(MBEDTLS_X509_CRL_PARSE_C) |
Ron Eldor | 80d0419 | 2019-04-04 15:02:01 +0300 | [diff] [blame] | 669 | char *crl_file; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 670 | # endif |
Manuel Pégourié-Gonnard | 5c078e1 | 2015-02-14 13:56:39 +0000 | [diff] [blame] | 671 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 672 | while (*end != '\0') |
Manuel Pégourié-Gonnard | 5c078e1 | 2015-02-14 13:56:39 +0000 | [diff] [blame] | 673 | ++end; |
| 674 | *end = ','; |
| 675 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 676 | while (p <= end) { |
| 677 | if ((new = mbedtls_calloc(1, sizeof(sni_entry))) == NULL) { |
| 678 | sni_free(cur); |
| 679 | return NULL; |
Manuel Pégourié-Gonnard | 5c078e1 | 2015-02-14 13:56:39 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 682 | GET_ITEM(new->name); |
| 683 | GET_ITEM(crt_file); |
| 684 | GET_ITEM(key_file); |
| 685 | GET_ITEM(ca_file); |
| 686 | # if defined(MBEDTLS_X509_CRL_PARSE_C) |
| 687 | GET_ITEM(crl_file); |
| 688 | # endif |
| 689 | GET_ITEM(auth_str); |
Manuel Pégourié-Gonnard | 5c078e1 | 2015-02-14 13:56:39 +0000 | [diff] [blame] | 690 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 691 | if ((new->cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt))) == NULL || |
| 692 | (new->key = mbedtls_calloc(1, sizeof(mbedtls_pk_context))) == NULL) |
Manuel Pégourié-Gonnard | 4d6f178 | 2015-06-19 14:40:39 +0200 | [diff] [blame] | 693 | goto error; |
Manuel Pégourié-Gonnard | 5c078e1 | 2015-02-14 13:56:39 +0000 | [diff] [blame] | 694 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 695 | mbedtls_x509_crt_init(new->cert); |
| 696 | mbedtls_pk_init(new->key); |
Manuel Pégourié-Gonnard | 5c078e1 | 2015-02-14 13:56:39 +0000 | [diff] [blame] | 697 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 698 | if (mbedtls_x509_crt_parse_file(new->cert, crt_file) != 0 || |
| 699 | mbedtls_pk_parse_keyfile(new->key, key_file, "", rng_get, &rng) != |
| 700 | 0) |
Manuel Pégourié-Gonnard | 5c078e1 | 2015-02-14 13:56:39 +0000 | [diff] [blame] | 701 | goto error; |
Manuel Pégourié-Gonnard | 4d6f178 | 2015-06-19 14:40:39 +0200 | [diff] [blame] | 702 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 703 | if (strcmp(ca_file, "-") != 0) { |
| 704 | if ((new->ca = mbedtls_calloc(1, sizeof(mbedtls_x509_crt))) == NULL) |
Manuel Pégourié-Gonnard | 4d6f178 | 2015-06-19 14:40:39 +0200 | [diff] [blame] | 705 | goto error; |
| 706 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 707 | mbedtls_x509_crt_init(new->ca); |
Manuel Pégourié-Gonnard | 4d6f178 | 2015-06-19 14:40:39 +0200 | [diff] [blame] | 708 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 709 | if (mbedtls_x509_crt_parse_file(new->ca, ca_file) != 0) |
Manuel Pégourié-Gonnard | 4d6f178 | 2015-06-19 14:40:39 +0200 | [diff] [blame] | 710 | goto error; |
Manuel Pégourié-Gonnard | 5c078e1 | 2015-02-14 13:56:39 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 713 | # if defined(MBEDTLS_X509_CRL_PARSE_C) |
| 714 | if (strcmp(crl_file, "-") != 0) { |
| 715 | if ((new->crl = mbedtls_calloc(1, sizeof(mbedtls_x509_crl))) == |
| 716 | NULL) |
Manuel Pégourié-Gonnard | 4d6f178 | 2015-06-19 14:40:39 +0200 | [diff] [blame] | 717 | goto error; |
| 718 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 719 | mbedtls_x509_crl_init(new->crl); |
Manuel Pégourié-Gonnard | 4d6f178 | 2015-06-19 14:40:39 +0200 | [diff] [blame] | 720 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 721 | if (mbedtls_x509_crl_parse_file(new->crl, crl_file) != 0) |
Manuel Pégourié-Gonnard | 4d6f178 | 2015-06-19 14:40:39 +0200 | [diff] [blame] | 722 | goto error; |
| 723 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 724 | # endif |
Manuel Pégourié-Gonnard | 4d6f178 | 2015-06-19 14:40:39 +0200 | [diff] [blame] | 725 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 726 | if (strcmp(auth_str, "-") != 0) { |
| 727 | if ((new->authmode = get_auth_mode(auth_str)) < 0) |
Manuel Pégourié-Gonnard | 4d6f178 | 2015-06-19 14:40:39 +0200 | [diff] [blame] | 728 | goto error; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 729 | } else |
Manuel Pégourié-Gonnard | 4d6f178 | 2015-06-19 14:40:39 +0200 | [diff] [blame] | 730 | new->authmode = DFL_AUTH_MODE; |
| 731 | |
Manuel Pégourié-Gonnard | 5c078e1 | 2015-02-14 13:56:39 +0000 | [diff] [blame] | 732 | new->next = cur; |
| 733 | cur = new; |
| 734 | } |
| 735 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 736 | return cur; |
Manuel Pégourié-Gonnard | 5c078e1 | 2015-02-14 13:56:39 +0000 | [diff] [blame] | 737 | |
| 738 | error: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 739 | sni_free(new); |
| 740 | sni_free(cur); |
| 741 | return NULL; |
Manuel Pégourié-Gonnard | 5c078e1 | 2015-02-14 13:56:39 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | /* |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 745 | * SNI callback. |
| 746 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 747 | int sni_callback(void *p_info, |
| 748 | mbedtls_ssl_context *ssl, |
| 749 | const unsigned char *name, |
| 750 | size_t name_len) |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 751 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 752 | const sni_entry *cur = (const sni_entry *)p_info; |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 753 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 754 | while (cur != NULL) { |
| 755 | if (name_len == strlen(cur->name) && |
| 756 | memcmp(name, cur->name, name_len) == 0) { |
| 757 | if (cur->ca != NULL) |
| 758 | mbedtls_ssl_set_hs_ca_chain(ssl, cur->ca, cur->crl); |
Manuel Pégourié-Gonnard | 4d6f178 | 2015-06-19 14:40:39 +0200 | [diff] [blame] | 759 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 760 | if (cur->authmode != DFL_AUTH_MODE) |
| 761 | mbedtls_ssl_set_hs_authmode(ssl, cur->authmode); |
Manuel Pégourié-Gonnard | 4d6f178 | 2015-06-19 14:40:39 +0200 | [diff] [blame] | 762 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 763 | return mbedtls_ssl_set_hs_own_cert(ssl, cur->cert, cur->key); |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 764 | } |
| 765 | |
Mateusz Starzyk | 55768f2 | 2021-05-27 14:57:03 +0200 | [diff] [blame] | 766 | cur = cur->next; |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 767 | } |
| 768 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 769 | return -1; |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 770 | } |
| 771 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 772 | # endif /* SNI_OPTION */ |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 773 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 774 | # if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Hanno Becker | 554b6ea | 2019-04-30 14:18:06 +0100 | [diff] [blame] | 775 | |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 776 | typedef struct _psk_entry psk_entry; |
| 777 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 778 | struct _psk_entry { |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 779 | const char *name; |
| 780 | size_t key_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 781 | unsigned char key[MBEDTLS_PSK_MAX_LEN]; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 782 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | adc2ff2 | 2020-09-16 16:49:27 +0200 | [diff] [blame] | 783 | psa_key_id_t slot; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 784 | # endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 785 | psk_entry *next; |
| 786 | }; |
| 787 | |
| 788 | /* |
Manuel Pégourié-Gonnard | 5c078e1 | 2015-02-14 13:56:39 +0000 | [diff] [blame] | 789 | * Free a list of psk_entry's |
| 790 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 791 | int psk_free(psk_entry *head) |
Manuel Pégourié-Gonnard | 5c078e1 | 2015-02-14 13:56:39 +0000 | [diff] [blame] | 792 | { |
| 793 | psk_entry *next; |
| 794 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 795 | while (head != NULL) { |
| 796 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | c43b6ea | 2018-11-05 13:48:43 +0000 | [diff] [blame] | 797 | psa_status_t status; |
Ronald Cron | adc2ff2 | 2020-09-16 16:49:27 +0200 | [diff] [blame] | 798 | psa_key_id_t const slot = head->slot; |
Hanno Becker | c43b6ea | 2018-11-05 13:48:43 +0000 | [diff] [blame] | 799 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 800 | if (slot != 0) { |
| 801 | status = psa_destroy_key(slot); |
| 802 | if (status != PSA_SUCCESS) |
| 803 | return status; |
Hanno Becker | c43b6ea | 2018-11-05 13:48:43 +0000 | [diff] [blame] | 804 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 805 | # endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | c43b6ea | 2018-11-05 13:48:43 +0000 | [diff] [blame] | 806 | |
Manuel Pégourié-Gonnard | 5c078e1 | 2015-02-14 13:56:39 +0000 | [diff] [blame] | 807 | next = head->next; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 808 | mbedtls_free(head); |
Manuel Pégourié-Gonnard | 5c078e1 | 2015-02-14 13:56:39 +0000 | [diff] [blame] | 809 | head = next; |
| 810 | } |
Hanno Becker | c43b6ea | 2018-11-05 13:48:43 +0000 | [diff] [blame] | 811 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 812 | return 0; |
Manuel Pégourié-Gonnard | 5c078e1 | 2015-02-14 13:56:39 +0000 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | /* |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 816 | * Parse a string of pairs name1,key1[,name2,key2[,...]] |
| 817 | * into a usable psk_entry list. |
| 818 | * |
| 819 | * Modifies the input string! This is not production quality! |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 820 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 821 | psk_entry *psk_parse(char *psk_string) |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 822 | { |
| 823 | psk_entry *cur = NULL, *new = NULL; |
| 824 | char *p = psk_string; |
| 825 | char *end = p; |
| 826 | char *key_hex; |
| 827 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 828 | while (*end != '\0') |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 829 | ++end; |
| 830 | *end = ','; |
| 831 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 832 | while (p <= end) { |
| 833 | if ((new = mbedtls_calloc(1, sizeof(psk_entry))) == NULL) |
Manuel Pégourié-Gonnard | b199095 | 2015-02-18 09:32:06 +0000 | [diff] [blame] | 834 | goto error; |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 835 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 836 | memset(new, 0, sizeof(psk_entry)); |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 837 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 838 | GET_ITEM(new->name); |
| 839 | GET_ITEM(key_hex); |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 840 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 841 | if (mbedtls_test_unhexify(new->key, MBEDTLS_PSK_MAX_LEN, key_hex, |
| 842 | &new->key_len) != 0) |
Manuel Pégourié-Gonnard | 5c078e1 | 2015-02-14 13:56:39 +0000 | [diff] [blame] | 843 | goto error; |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 844 | |
| 845 | new->next = cur; |
| 846 | cur = new; |
| 847 | } |
| 848 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 849 | return cur; |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 850 | |
Manuel Pégourié-Gonnard | 5c078e1 | 2015-02-14 13:56:39 +0000 | [diff] [blame] | 851 | error: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 852 | psk_free(new); |
| 853 | psk_free(cur); |
| 854 | return 0; |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | /* |
| 858 | * PSK callback |
| 859 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 860 | int psk_callback(void *p_info, |
| 861 | mbedtls_ssl_context *ssl, |
| 862 | const unsigned char *name, |
| 863 | size_t name_len) |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 864 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 865 | psk_entry *cur = (psk_entry *)p_info; |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 866 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 867 | while (cur != NULL) { |
| 868 | if (name_len == strlen(cur->name) && |
| 869 | memcmp(name, cur->name, name_len) == 0) { |
| 870 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 871 | if (cur->slot != 0) |
| 872 | return mbedtls_ssl_set_hs_psk_opaque(ssl, cur->slot); |
Hanno Becker | b64ba5f | 2018-10-26 11:28:08 +0100 | [diff] [blame] | 873 | else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 874 | # endif |
| 875 | return mbedtls_ssl_set_hs_psk(ssl, cur->key, cur->key_len); |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 876 | } |
| 877 | |
Mateusz Starzyk | 55768f2 | 2021-05-27 14:57:03 +0200 | [diff] [blame] | 878 | cur = cur->next; |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 879 | } |
| 880 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 881 | return -1; |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 882 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 883 | # endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | 9e27163 | 2014-06-09 19:06:00 +0200 | [diff] [blame] | 884 | |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 885 | static mbedtls_net_context listen_fd, client_fd; |
Paul Bakker | bc3e54c | 2014-08-18 14:36:17 +0200 | [diff] [blame] | 886 | |
| 887 | /* Interruption handler to ensure clean exit (for valgrind testing) */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 888 | # if !defined(_WIN32) |
Manuel Pégourié-Gonnard | db49330 | 2014-08-14 15:36:12 +0200 | [diff] [blame] | 889 | static int received_sigterm = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 890 | void term_handler(int sig) |
Manuel Pégourié-Gonnard | db49330 | 2014-08-14 15:36:12 +0200 | [diff] [blame] | 891 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 892 | ((void)sig); |
Manuel Pégourié-Gonnard | db49330 | 2014-08-14 15:36:12 +0200 | [diff] [blame] | 893 | received_sigterm = 1; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 894 | mbedtls_net_free(&listen_fd); /* causes mbedtls_net_accept() to abort */ |
| 895 | mbedtls_net_free(&client_fd); /* causes net_read() to abort */ |
Manuel Pégourié-Gonnard | db49330 | 2014-08-14 15:36:12 +0200 | [diff] [blame] | 896 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 897 | # endif |
Manuel Pégourié-Gonnard | db49330 | 2014-08-14 15:36:12 +0200 | [diff] [blame] | 898 | |
Gilles Peskine | a36ac4f | 2018-04-26 08:05:02 +0200 | [diff] [blame] | 899 | /** Return true if \p ret is a status code indicating that there is an |
| 900 | * operation in progress on an SSL connection, and false if it indicates |
| 901 | * success or a fatal error. |
| 902 | * |
| 903 | * The possible operations in progress are: |
| 904 | * |
| 905 | * - A read, when the SSL input buffer does not contain a full message. |
| 906 | * - A write, when the SSL output buffer contains some data that has not |
| 907 | * been sent over the network yet. |
| 908 | * - An asynchronous callback that has not completed yet. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 909 | static int mbedtls_status_is_ssl_in_progress(int ret) |
Gilles Peskine | a36ac4f | 2018-04-26 08:05:02 +0200 | [diff] [blame] | 910 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 911 | return (ret == MBEDTLS_ERR_SSL_WANT_READ || |
Gilles Peskine | a36ac4f | 2018-04-26 08:05:02 +0200 | [diff] [blame] | 912 | ret == MBEDTLS_ERR_SSL_WANT_WRITE || |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 913 | ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS); |
Gilles Peskine | a36ac4f | 2018-04-26 08:05:02 +0200 | [diff] [blame] | 914 | } |
| 915 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 916 | # if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 917 | typedef struct { |
Gilles Peskine | 4481744 | 2018-06-13 18:09:28 +0200 | [diff] [blame] | 918 | mbedtls_x509_crt *cert; /*!< Certificate corresponding to the key */ |
| 919 | mbedtls_pk_context *pk; /*!< Private key */ |
| 920 | unsigned delay; /*!< Number of resume steps to go through */ |
| 921 | unsigned pk_owned : 1; /*!< Whether to free the pk object on exit */ |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 922 | } ssl_async_key_slot_t; |
| 923 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 924 | typedef enum |
| 925 | { |
Gilles Peskine | ad28bf0 | 2018-04-26 00:19:16 +0200 | [diff] [blame] | 926 | SSL_ASYNC_INJECT_ERROR_NONE = 0, /*!< Let the callbacks succeed */ |
| 927 | SSL_ASYNC_INJECT_ERROR_START, /*!< Inject error during start */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 928 | SSL_ASYNC_INJECT_ERROR_CANCEL, /*!< Close the connection after async start |
| 929 | */ |
Gilles Peskine | ad28bf0 | 2018-04-26 00:19:16 +0200 | [diff] [blame] | 930 | SSL_ASYNC_INJECT_ERROR_RESUME, /*!< Inject error during resume */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 931 | # define SSL_ASYNC_INJECT_ERROR_MAX SSL_ASYNC_INJECT_ERROR_RESUME |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 932 | } ssl_async_inject_error_t; |
| 933 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 934 | typedef struct { |
Gilles Peskine | e247989 | 2018-06-13 18:06:51 +0200 | [diff] [blame] | 935 | ssl_async_key_slot_t slots[4]; /* key, key2, sni1, sni2 */ |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 936 | size_t slots_used; |
| 937 | ssl_async_inject_error_t inject_error; |
| 938 | int (*f_rng)(void *, unsigned char *, size_t); |
| 939 | void *p_rng; |
| 940 | } ssl_async_key_context_t; |
| 941 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 942 | int ssl_async_set_key(ssl_async_key_context_t *ctx, |
| 943 | mbedtls_x509_crt *cert, |
| 944 | mbedtls_pk_context *pk, |
| 945 | int pk_take_ownership, |
| 946 | unsigned delay) |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 947 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 948 | if (ctx->slots_used >= sizeof(ctx->slots) / sizeof(*ctx->slots)) |
| 949 | return -1; |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 950 | ctx->slots[ctx->slots_used].cert = cert; |
| 951 | ctx->slots[ctx->slots_used].pk = pk; |
| 952 | ctx->slots[ctx->slots_used].delay = delay; |
Gilles Peskine | 4481744 | 2018-06-13 18:09:28 +0200 | [diff] [blame] | 953 | ctx->slots[ctx->slots_used].pk_owned = pk_take_ownership; |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 954 | ++ctx->slots_used; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 955 | return 0; |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 956 | } |
| 957 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 958 | # define SSL_ASYNC_INPUT_MAX_SIZE 512 |
Gilles Peskine | d326883 | 2018-04-26 06:23:59 +0200 | [diff] [blame] | 959 | |
| 960 | typedef enum |
| 961 | { |
| 962 | ASYNC_OP_SIGN, |
| 963 | ASYNC_OP_DECRYPT, |
| 964 | } ssl_async_operation_type_t; |
| 965 | /* Note that the enum above and the array below need to be kept in sync! |
| 966 | * `ssl_async_operation_names[op]` is the name of op for each value `op` |
| 967 | * of type `ssl_async_operation_type_t`. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 968 | static const char *const ssl_async_operation_names[] = { |
Gilles Peskine | d326883 | 2018-04-26 06:23:59 +0200 | [diff] [blame] | 969 | "sign", |
| 970 | "decrypt", |
| 971 | }; |
| 972 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 973 | typedef struct { |
Gilles Peskine | 6331d78 | 2018-04-26 13:27:43 +0200 | [diff] [blame] | 974 | unsigned slot; |
Gilles Peskine | d326883 | 2018-04-26 06:23:59 +0200 | [diff] [blame] | 975 | ssl_async_operation_type_t operation_type; |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 976 | mbedtls_md_type_t md_alg; |
Gilles Peskine | fcca9d8 | 2018-01-12 13:47:48 +0100 | [diff] [blame] | 977 | unsigned char input[SSL_ASYNC_INPUT_MAX_SIZE]; |
| 978 | size_t input_len; |
Gilles Peskine | ceb541b | 2018-04-26 06:30:45 +0200 | [diff] [blame] | 979 | unsigned remaining_delay; |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 980 | } ssl_async_operation_context_t; |
| 981 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 982 | static int ssl_async_start(mbedtls_ssl_context *ssl, |
| 983 | mbedtls_x509_crt *cert, |
| 984 | ssl_async_operation_type_t op_type, |
| 985 | mbedtls_md_type_t md_alg, |
| 986 | const unsigned char *input, |
| 987 | size_t input_len) |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 988 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 989 | ssl_async_key_context_t *config_data = |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 990 | mbedtls_ssl_conf_get_async_config_data(ssl->conf); |
Gilles Peskine | 6331d78 | 2018-04-26 13:27:43 +0200 | [diff] [blame] | 991 | unsigned slot; |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 992 | ssl_async_operation_context_t *ctx = NULL; |
Gilles Peskine | d326883 | 2018-04-26 06:23:59 +0200 | [diff] [blame] | 993 | const char *op_name = ssl_async_operation_names[op_type]; |
Gilles Peskine | f112725 | 2018-04-24 13:05:39 +0200 | [diff] [blame] | 994 | |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 995 | { |
| 996 | char dn[100]; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 997 | if (mbedtls_x509_dn_gets(dn, sizeof(dn), &cert->subject) > 0) |
| 998 | mbedtls_printf("Async %s callback: looking for DN=%s\n", op_name, |
| 999 | dn); |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 1000 | } |
Gilles Peskine | f112725 | 2018-04-24 13:05:39 +0200 | [diff] [blame] | 1001 | |
Gilles Peskine | 3dae1cf | 2018-04-30 12:07:56 +0200 | [diff] [blame] | 1002 | /* Look for a private key that matches the public key in cert. |
| 1003 | * Since this test code has the private key inside Mbed TLS, |
| 1004 | * we call mbedtls_pk_check_pair to match a private key with the |
| 1005 | * public key. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1006 | for (slot = 0; slot < config_data->slots_used; slot++) { |
| 1007 | if (mbedtls_pk_check_pair(&cert->pk, config_data->slots[slot].pk, |
| 1008 | rng_get, &rng) == 0) |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 1009 | break; |
| 1010 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1011 | if (slot == config_data->slots_used) { |
| 1012 | mbedtls_printf("Async %s callback: no key matches this certificate.\n", |
| 1013 | op_name); |
| 1014 | return MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH; |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 1015 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1016 | mbedtls_printf("Async %s callback: using key slot %u, delay=%u.\n", op_name, |
| 1017 | slot, config_data->slots[slot].delay); |
Gilles Peskine | f112725 | 2018-04-24 13:05:39 +0200 | [diff] [blame] | 1018 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1019 | if (config_data->inject_error == SSL_ASYNC_INJECT_ERROR_START) { |
| 1020 | mbedtls_printf("Async %s callback: injected error\n", op_name); |
| 1021 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 1022 | } |
Gilles Peskine | f112725 | 2018-04-24 13:05:39 +0200 | [diff] [blame] | 1023 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1024 | if (input_len > SSL_ASYNC_INPUT_MAX_SIZE) |
| 1025 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Gilles Peskine | f112725 | 2018-04-24 13:05:39 +0200 | [diff] [blame] | 1026 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1027 | ctx = mbedtls_calloc(1, sizeof(*ctx)); |
| 1028 | if (ctx == NULL) |
| 1029 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 1030 | ctx->slot = slot; |
Gilles Peskine | d326883 | 2018-04-26 06:23:59 +0200 | [diff] [blame] | 1031 | ctx->operation_type = op_type; |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 1032 | ctx->md_alg = md_alg; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1033 | memcpy(ctx->input, input, input_len); |
Gilles Peskine | fcca9d8 | 2018-01-12 13:47:48 +0100 | [diff] [blame] | 1034 | ctx->input_len = input_len; |
Gilles Peskine | ceb541b | 2018-04-26 06:30:45 +0200 | [diff] [blame] | 1035 | ctx->remaining_delay = config_data->slots[slot].delay; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1036 | mbedtls_ssl_set_async_operation_data(ssl, ctx); |
Gilles Peskine | f112725 | 2018-04-24 13:05:39 +0200 | [diff] [blame] | 1037 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1038 | if (ctx->remaining_delay == 0) |
| 1039 | return 0; |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 1040 | else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1041 | return MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS; |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 1042 | } |
| 1043 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1044 | static int ssl_async_sign(mbedtls_ssl_context *ssl, |
| 1045 | mbedtls_x509_crt *cert, |
| 1046 | mbedtls_md_type_t md_alg, |
| 1047 | const unsigned char *hash, |
| 1048 | size_t hash_len) |
Gilles Peskine | fcca9d8 | 2018-01-12 13:47:48 +0100 | [diff] [blame] | 1049 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1050 | return (ssl_async_start(ssl, cert, ASYNC_OP_SIGN, md_alg, hash, hash_len)); |
Gilles Peskine | fcca9d8 | 2018-01-12 13:47:48 +0100 | [diff] [blame] | 1051 | } |
| 1052 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1053 | static int ssl_async_decrypt(mbedtls_ssl_context *ssl, |
| 1054 | mbedtls_x509_crt *cert, |
| 1055 | const unsigned char *input, |
| 1056 | size_t input_len) |
Gilles Peskine | fcca9d8 | 2018-01-12 13:47:48 +0100 | [diff] [blame] | 1057 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1058 | return (ssl_async_start(ssl, cert, ASYNC_OP_DECRYPT, MBEDTLS_MD_NONE, input, |
| 1059 | input_len)); |
Gilles Peskine | fcca9d8 | 2018-01-12 13:47:48 +0100 | [diff] [blame] | 1060 | } |
| 1061 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1062 | static int ssl_async_resume(mbedtls_ssl_context *ssl, |
| 1063 | unsigned char *output, |
| 1064 | size_t *output_len, |
| 1065 | size_t output_size) |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 1066 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1067 | ssl_async_operation_context_t *ctx = |
| 1068 | mbedtls_ssl_get_async_operation_data(ssl); |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 1069 | ssl_async_key_context_t *config_data = |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1070 | mbedtls_ssl_conf_get_async_config_data(ssl->conf); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 1071 | ssl_async_key_slot_t *key_slot = &config_data->slots[ctx->slot]; |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 1072 | int ret; |
Gilles Peskine | f5a9996 | 2018-04-30 16:37:23 +0200 | [diff] [blame] | 1073 | const char *op_name; |
Gilles Peskine | f112725 | 2018-04-24 13:05:39 +0200 | [diff] [blame] | 1074 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1075 | if (ctx->remaining_delay > 0) { |
Gilles Peskine | ceb541b | 2018-04-26 06:30:45 +0200 | [diff] [blame] | 1076 | --ctx->remaining_delay; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1077 | mbedtls_printf("Async resume (slot %u): call %u more times.\n", |
| 1078 | ctx->slot, ctx->remaining_delay); |
| 1079 | return MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS; |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 1080 | } |
Gilles Peskine | f112725 | 2018-04-24 13:05:39 +0200 | [diff] [blame] | 1081 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1082 | switch (ctx->operation_type) { |
Gilles Peskine | d326883 | 2018-04-26 06:23:59 +0200 | [diff] [blame] | 1083 | case ASYNC_OP_DECRYPT: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1084 | ret = mbedtls_pk_decrypt(key_slot->pk, ctx->input, ctx->input_len, |
| 1085 | output, output_len, output_size, |
| 1086 | config_data->f_rng, config_data->p_rng); |
Gilles Peskine | d326883 | 2018-04-26 06:23:59 +0200 | [diff] [blame] | 1087 | break; |
| 1088 | case ASYNC_OP_SIGN: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1089 | ret = mbedtls_pk_sign(key_slot->pk, ctx->md_alg, ctx->input, |
| 1090 | ctx->input_len, output, output_size, |
| 1091 | output_len, config_data->f_rng, |
| 1092 | config_data->p_rng); |
Gilles Peskine | d326883 | 2018-04-26 06:23:59 +0200 | [diff] [blame] | 1093 | break; |
| 1094 | default: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1095 | mbedtls_printf( |
| 1096 | "Async resume (slot %u): unknown operation type %ld. This shouldn't happen.\n", |
| 1097 | ctx->slot, (long)ctx->operation_type); |
| 1098 | mbedtls_free(ctx); |
| 1099 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
Gilles Peskine | d326883 | 2018-04-26 06:23:59 +0200 | [diff] [blame] | 1100 | break; |
Gilles Peskine | fcca9d8 | 2018-01-12 13:47:48 +0100 | [diff] [blame] | 1101 | } |
Gilles Peskine | f112725 | 2018-04-24 13:05:39 +0200 | [diff] [blame] | 1102 | |
Gilles Peskine | f5a9996 | 2018-04-30 16:37:23 +0200 | [diff] [blame] | 1103 | op_name = ssl_async_operation_names[ctx->operation_type]; |
| 1104 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1105 | if (config_data->inject_error == SSL_ASYNC_INJECT_ERROR_RESUME) { |
| 1106 | mbedtls_printf("Async resume callback: %s done but injected error\n", |
| 1107 | op_name); |
| 1108 | mbedtls_free(ctx); |
| 1109 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 1110 | } |
Gilles Peskine | f112725 | 2018-04-24 13:05:39 +0200 | [diff] [blame] | 1111 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1112 | mbedtls_printf("Async resume (slot %u): %s done, status=%d.\n", ctx->slot, |
| 1113 | op_name, ret); |
| 1114 | mbedtls_free(ctx); |
| 1115 | return ret; |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 1116 | } |
| 1117 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1118 | static void ssl_async_cancel(mbedtls_ssl_context *ssl) |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 1119 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1120 | ssl_async_operation_context_t *ctx = |
| 1121 | mbedtls_ssl_get_async_operation_data(ssl); |
| 1122 | mbedtls_printf("Async cancel callback.\n"); |
| 1123 | mbedtls_free(ctx); |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 1124 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1125 | # endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 1126 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1127 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1128 | static psa_status_t psa_setup_psk_key_slot(psa_key_id_t *slot, |
| 1129 | psa_algorithm_t alg, |
| 1130 | unsigned char *psk, |
| 1131 | size_t psk_len) |
Hanno Becker | b64ba5f | 2018-10-26 11:28:08 +0100 | [diff] [blame] | 1132 | { |
| 1133 | psa_status_t status; |
Janos Follath | be4efc2 | 2019-08-08 11:38:18 +0100 | [diff] [blame] | 1134 | psa_key_attributes_t key_attributes; |
Hanno Becker | b64ba5f | 2018-10-26 11:28:08 +0100 | [diff] [blame] | 1135 | |
Janos Follath | be4efc2 | 2019-08-08 11:38:18 +0100 | [diff] [blame] | 1136 | key_attributes = psa_key_attributes_init(); |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1137 | psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE); |
| 1138 | psa_set_key_algorithm(&key_attributes, alg); |
| 1139 | psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE); |
Hanno Becker | b64ba5f | 2018-10-26 11:28:08 +0100 | [diff] [blame] | 1140 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1141 | status = psa_import_key(&key_attributes, psk, psk_len, slot); |
| 1142 | if (status != PSA_SUCCESS) { |
| 1143 | fprintf(stderr, "IMPORT\n"); |
| 1144 | return status; |
Hanno Becker | 1d911cd | 2018-11-15 13:06:09 +0000 | [diff] [blame] | 1145 | } |
Hanno Becker | b64ba5f | 2018-10-26 11:28:08 +0100 | [diff] [blame] | 1146 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1147 | return PSA_SUCCESS; |
Hanno Becker | b64ba5f | 2018-10-26 11:28:08 +0100 | [diff] [blame] | 1148 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1149 | # endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | b64ba5f | 2018-10-26 11:28:08 +0100 | [diff] [blame] | 1150 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1151 | # if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 1152 | int report_cid_usage(mbedtls_ssl_context *ssl, |
| 1153 | const char *additional_description) |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 1154 | { |
| 1155 | int ret; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1156 | unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX]; |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 1157 | size_t peer_cid_len; |
| 1158 | int cid_negotiated; |
| 1159 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1160 | if (opt.transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) |
| 1161 | return 0; |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 1162 | |
| 1163 | /* Check if the use of a CID has been negotiated */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1164 | ret = |
| 1165 | mbedtls_ssl_get_peer_cid(ssl, &cid_negotiated, peer_cid, &peer_cid_len); |
| 1166 | if (ret != 0) { |
| 1167 | mbedtls_printf( |
| 1168 | " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n", |
| 1169 | (unsigned int)-ret); |
| 1170 | return ret; |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 1171 | } |
| 1172 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1173 | if (cid_negotiated == MBEDTLS_SSL_CID_DISABLED) { |
| 1174 | if (opt.cid_enabled == MBEDTLS_SSL_CID_ENABLED) { |
| 1175 | mbedtls_printf( |
| 1176 | "(%s) Use of Connection ID was not offered by client.\n", |
| 1177 | additional_description); |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 1178 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1179 | } else { |
| 1180 | size_t idx = 0; |
| 1181 | mbedtls_printf("(%s) Use of Connection ID has been negotiated.\n", |
| 1182 | additional_description); |
| 1183 | mbedtls_printf("(%s) Peer CID (length %u Bytes): ", |
| 1184 | additional_description, (unsigned)peer_cid_len); |
| 1185 | while (idx < peer_cid_len) { |
| 1186 | mbedtls_printf("%02x ", peer_cid[idx]); |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 1187 | idx++; |
| 1188 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1189 | mbedtls_printf("\n"); |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 1190 | } |
| 1191 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1192 | return 0; |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 1193 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1194 | # endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 1195 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1196 | int main(int argc, char *argv[]) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1197 | { |
Manuel Pégourié-Gonnard | 6a0017b | 2015-01-22 10:33:29 +0000 | [diff] [blame] | 1198 | int ret = 0, len, written, frags, exchanges_left; |
gabor-mezei-arm | a9eecf1 | 2020-07-07 11:11:02 +0200 | [diff] [blame] | 1199 | int query_config_ret = 0; |
Hanno Becker | dcc94e6 | 2019-07-03 17:05:43 +0100 | [diff] [blame] | 1200 | io_ctx_t io_ctx; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1201 | unsigned char *buf = 0; |
| 1202 | # if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
| 1203 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | b64ba5f | 2018-10-26 11:28:08 +0100 | [diff] [blame] | 1204 | psa_algorithm_t alg = 0; |
Ronald Cron | adc2ff2 | 2020-09-16 16:49:27 +0200 | [diff] [blame] | 1205 | psa_key_id_t psk_slot = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1206 | # endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1207 | unsigned char psk[MBEDTLS_PSK_MAX_LEN]; |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 1208 | size_t psk_len = 0; |
Paul Bakker | 9b7fb6f | 2014-06-12 23:01:43 +0200 | [diff] [blame] | 1209 | psk_entry *psk_info = NULL; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1210 | # endif |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 1211 | const char *pers = "ssl_server2"; |
Manuel Pégourié-Gonnard | 336b824 | 2014-07-22 17:57:43 +0200 | [diff] [blame] | 1212 | unsigned char client_ip[16] = { 0 }; |
Manuel Pégourié-Gonnard | 0b104b0 | 2015-05-14 21:52:40 +0200 | [diff] [blame] | 1213 | size_t cliip_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1214 | # if defined(MBEDTLS_SSL_COOKIE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1215 | mbedtls_ssl_cookie_ctx cookie_ctx; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1216 | # endif |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1217 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1218 | # if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 1219 | mbedtls_x509_crt_profile crt_profile_for_test = |
| 1220 | mbedtls_x509_crt_profile_default; |
| 1221 | # endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1222 | mbedtls_ssl_context ssl; |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 1223 | mbedtls_ssl_config conf; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1224 | # if defined(MBEDTLS_TIMING_C) |
Manuel Pégourié-Gonnard | e3c41ad | 2015-05-13 10:04:32 +0200 | [diff] [blame] | 1225 | mbedtls_timing_delay_context timer; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1226 | # endif |
| 1227 | # if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 590f416 | 2014-11-05 14:23:03 +0100 | [diff] [blame] | 1228 | unsigned char renego_period[8] = { 0 }; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1229 | # endif |
| 1230 | # if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | db1cc76 | 2015-05-12 11:27:25 +0200 | [diff] [blame] | 1231 | uint32_t flags; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1232 | mbedtls_x509_crt cacert; |
| 1233 | mbedtls_x509_crt srvcert; |
| 1234 | mbedtls_pk_context pkey; |
| 1235 | mbedtls_x509_crt srvcert2; |
| 1236 | mbedtls_pk_context pkey2; |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 1237 | int key_cert_init = 0, key_cert_init2 = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1238 | # if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 1239 | ssl_async_key_context_t ssl_async_keys; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1240 | # endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 1241 | # endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 1242 | # if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1243 | mbedtls_dhm_context dhm; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1244 | # endif |
| 1245 | # if defined(MBEDTLS_SSL_CACHE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1246 | mbedtls_ssl_cache_context cache; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1247 | # endif |
| 1248 | # if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 1249 | mbedtls_ssl_ticket_context ticket_ctx; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1250 | # endif |
| 1251 | # if defined(SNI_OPTION) |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 1252 | sni_entry *sni_info = NULL; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1253 | # endif |
| 1254 | # if defined(MBEDTLS_ECP_C) |
Hanno Becker | 8651a43 | 2017-06-09 16:13:22 +0100 | [diff] [blame] | 1255 | mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE]; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1256 | const mbedtls_ecp_curve_info *curve_cur; |
| 1257 | # endif |
| 1258 | # if defined(MBEDTLS_SSL_ALPN) |
Hanno Becker | 8651a43 | 2017-06-09 16:13:22 +0100 | [diff] [blame] | 1259 | const char *alpn_list[ALPN_LIST_SIZE]; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1260 | # endif |
| 1261 | # if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
Simon Butcher | cce68be | 2018-07-23 14:26:09 +0100 | [diff] [blame] | 1262 | unsigned char alloc_buf[MEMORY_HEAP_SIZE]; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1263 | # endif |
| 1264 | # if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | a7d2542 | 2019-04-09 17:28:10 +0100 | [diff] [blame] | 1265 | unsigned char cid[MBEDTLS_SSL_CID_IN_LEN_MAX]; |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 1266 | unsigned char cid_renego[MBEDTLS_SSL_CID_IN_LEN_MAX]; |
Hanno Becker | a7d2542 | 2019-04-09 17:28:10 +0100 | [diff] [blame] | 1267 | size_t cid_len = 0; |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 1268 | size_t cid_renego_len = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1269 | # endif |
| 1270 | # if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Manuel Pégourié-Gonnard | 3309a67 | 2019-07-15 10:31:11 +0200 | [diff] [blame] | 1271 | unsigned char *context_buf = NULL; |
Gilles Peskine | 4e8b594 | 2019-09-20 19:12:31 +0200 | [diff] [blame] | 1272 | size_t context_buf_len = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1273 | # endif |
Hanno Becker | a7d2542 | 2019-04-09 17:28:10 +0100 | [diff] [blame] | 1274 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1275 | int i; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1276 | char *p, *q; |
| 1277 | const int *list; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1278 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | 5a9942e | 2018-11-12 17:47:48 +0000 | [diff] [blame] | 1279 | psa_status_t status; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1280 | # endif |
| 1281 | # if defined(MBEDTLS_SSL_EXPORT_KEYS) |
Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 1282 | unsigned char eap_tls_keymaterial[16]; |
| 1283 | unsigned char eap_tls_iv[8]; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1284 | const char *eap_tls_label = "client EAP encryption"; |
Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 1285 | eap_tls_keys eap_tls_keying; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1286 | # if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | 65b56ef | 2019-09-26 16:40:48 +0300 | [diff] [blame] | 1287 | /*! master keys and master salt for SRTP generated during handshake */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1288 | unsigned char |
| 1289 | dtls_srtp_key_material[MBEDTLS_TLS_SRTP_MAX_KEY_MATERIAL_LENGTH]; |
| 1290 | const char *dtls_srtp_label = "EXTRACTOR-dtls_srtp"; |
| 1291 | dtls_srtp_keys dtls_srtp_keying; |
| 1292 | const mbedtls_ssl_srtp_profile default_profiles[] = { |
| 1293 | MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_80, |
| 1294 | MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_32, |
| 1295 | MBEDTLS_TLS_SRTP_NULL_HMAC_SHA1_80, MBEDTLS_TLS_SRTP_NULL_HMAC_SHA1_32, |
| 1296 | MBEDTLS_TLS_SRTP_UNSET |
| 1297 | }; |
| 1298 | # endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 1299 | # endif /* MBEDTLS_SSL_EXPORT_KEYS */ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1300 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1301 | # if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 1302 | mbedtls_memory_buffer_alloc_init(alloc_buf, sizeof(alloc_buf)); |
| 1303 | # if defined(MBEDTLS_MEMORY_DEBUG) |
Piotr Nowicki | 0937ed2 | 2019-11-26 16:32:40 +0100 | [diff] [blame] | 1304 | size_t current_heap_memory, peak_heap_memory, heap_blocks; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1305 | # endif /* MBEDTLS_MEMORY_DEBUG */ |
| 1306 | # endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */ |
Paul Bakker | 82024bf | 2013-07-04 11:52:32 +0200 | [diff] [blame] | 1307 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1308 | # if defined(MBEDTLS_TEST_HOOKS) |
| 1309 | test_hooks_init(); |
| 1310 | # endif /* MBEDTLS_TEST_HOOKS */ |
Gilles Peskine | 53dea74 | 2021-02-02 22:55:06 +0100 | [diff] [blame] | 1311 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1312 | /* |
Manuel Pégourié-Gonnard | 3bd2aae | 2013-09-20 13:10:13 +0200 | [diff] [blame] | 1313 | * Make sure memory references are valid in case we exit early. |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1314 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1315 | mbedtls_net_init(&client_fd); |
| 1316 | mbedtls_net_init(&listen_fd); |
| 1317 | mbedtls_ssl_init(&ssl); |
| 1318 | mbedtls_ssl_config_init(&conf); |
| 1319 | rng_init(&rng); |
| 1320 | # if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 1321 | mbedtls_x509_crt_init(&cacert); |
| 1322 | mbedtls_x509_crt_init(&srvcert); |
| 1323 | mbedtls_pk_init(&pkey); |
| 1324 | mbedtls_x509_crt_init(&srvcert2); |
| 1325 | mbedtls_pk_init(&pkey2); |
| 1326 | # if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 1327 | memset(&ssl_async_keys, 0, sizeof(ssl_async_keys)); |
| 1328 | # endif |
| 1329 | # endif |
| 1330 | # if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO) |
| 1331 | mbedtls_dhm_init(&dhm); |
| 1332 | # endif |
| 1333 | # if defined(MBEDTLS_SSL_CACHE_C) |
| 1334 | mbedtls_ssl_cache_init(&cache); |
| 1335 | # endif |
| 1336 | # if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 1337 | mbedtls_ssl_ticket_init(&ticket_ctx); |
| 1338 | # endif |
| 1339 | # if defined(MBEDTLS_SSL_ALPN) |
| 1340 | memset((void *)alpn_list, 0, sizeof(alpn_list)); |
| 1341 | # endif |
| 1342 | # if defined(MBEDTLS_SSL_COOKIE_C) |
| 1343 | mbedtls_ssl_cookie_init(&cookie_ctx); |
| 1344 | # endif |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1345 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1346 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | 5a9942e | 2018-11-12 17:47:48 +0000 | [diff] [blame] | 1347 | status = psa_crypto_init(); |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1348 | if (status != PSA_SUCCESS) { |
| 1349 | mbedtls_fprintf(stderr, |
| 1350 | "Failed to initialize PSA Crypto implementation: %d\n", |
| 1351 | (int)status); |
Hanno Becker | 5a9942e | 2018-11-12 17:47:48 +0000 | [diff] [blame] | 1352 | ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 1353 | goto exit; |
| 1354 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1355 | # endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1356 | # if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 1357 | mbedtls_test_enable_insecure_external_rng(); |
| 1358 | # endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
Hanno Becker | 5a9942e | 2018-11-12 17:47:48 +0000 | [diff] [blame] | 1359 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1360 | # if !defined(_WIN32) |
Manuel Pégourié-Gonnard | 403a86f | 2014-11-17 12:46:49 +0100 | [diff] [blame] | 1361 | /* Abort cleanly on SIGTERM and SIGINT */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1362 | signal(SIGTERM, term_handler); |
| 1363 | signal(SIGINT, term_handler); |
| 1364 | # endif |
Manuel Pégourié-Gonnard | db49330 | 2014-08-14 15:36:12 +0200 | [diff] [blame] | 1365 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1366 | if (argc == 0) { |
| 1367 | usage: |
| 1368 | if (ret == 0) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1369 | ret = 1; |
| 1370 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1371 | mbedtls_printf(USAGE1); |
| 1372 | mbedtls_printf(USAGE2); |
| 1373 | mbedtls_printf(USAGE3); |
| 1374 | mbedtls_printf(USAGE4); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1375 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1376 | list = mbedtls_ssl_list_ciphersuites(); |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1377 | while (*list) { |
| 1378 | mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name(*list)); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 1379 | list++; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1380 | if (!*list) |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 1381 | break; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1382 | mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name(*list)); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1383 | list++; |
| 1384 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1385 | mbedtls_printf("\n"); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1386 | goto exit; |
| 1387 | } |
| 1388 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1389 | opt.buffer_size = DFL_IO_BUF_LEN; |
| 1390 | opt.server_addr = DFL_SERVER_ADDR; |
| 1391 | opt.server_port = DFL_SERVER_PORT; |
| 1392 | opt.debug_level = DFL_DEBUG_LEVEL; |
| 1393 | opt.event = DFL_EVENT; |
| 1394 | opt.response_size = DFL_RESPONSE_SIZE; |
| 1395 | opt.nbio = DFL_NBIO; |
| 1396 | opt.cid_enabled = DFL_CID_ENABLED; |
| 1397 | opt.cid_enabled_renego = DFL_CID_ENABLED_RENEGO; |
| 1398 | opt.cid_val = DFL_CID_VALUE; |
| 1399 | opt.cid_val_renego = DFL_CID_VALUE_RENEGO; |
| 1400 | opt.read_timeout = DFL_READ_TIMEOUT; |
| 1401 | opt.ca_file = DFL_CA_FILE; |
| 1402 | opt.ca_path = DFL_CA_PATH; |
| 1403 | opt.crt_file = DFL_CRT_FILE; |
| 1404 | opt.key_file = DFL_KEY_FILE; |
| 1405 | opt.key_pwd = DFL_KEY_PWD; |
| 1406 | opt.crt_file2 = DFL_CRT_FILE2; |
| 1407 | opt.key_file2 = DFL_KEY_FILE2; |
| 1408 | opt.key_pwd2 = DFL_KEY_PWD2; |
| 1409 | opt.async_operations = DFL_ASYNC_OPERATIONS; |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 1410 | opt.async_private_delay1 = DFL_ASYNC_PRIVATE_DELAY1; |
| 1411 | opt.async_private_delay2 = DFL_ASYNC_PRIVATE_DELAY2; |
| 1412 | opt.async_private_error = DFL_ASYNC_PRIVATE_ERROR; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1413 | opt.psk = DFL_PSK; |
| 1414 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1415 | opt.psk_opaque = DFL_PSK_OPAQUE; |
| 1416 | opt.psk_list_opaque = DFL_PSK_LIST_OPAQUE; |
| 1417 | # endif |
| 1418 | # if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1419 | opt.ca_callback = DFL_CA_CALLBACK; |
| 1420 | # endif |
| 1421 | opt.psk_identity = DFL_PSK_IDENTITY; |
| 1422 | opt.psk_list = DFL_PSK_LIST; |
| 1423 | opt.ecjpake_pw = DFL_ECJPAKE_PW; |
| 1424 | opt.force_ciphersuite[0] = DFL_FORCE_CIPHER; |
| 1425 | opt.renegotiation = DFL_RENEGOTIATION; |
| 1426 | opt.allow_legacy = DFL_ALLOW_LEGACY; |
| 1427 | opt.renegotiate = DFL_RENEGOTIATE; |
| 1428 | opt.renego_delay = DFL_RENEGO_DELAY; |
| 1429 | opt.renego_period = DFL_RENEGO_PERIOD; |
| 1430 | opt.exchanges = DFL_EXCHANGES; |
| 1431 | opt.min_version = DFL_MIN_VERSION; |
| 1432 | opt.max_version = DFL_MAX_VERSION; |
| 1433 | opt.allow_sha1 = DFL_SHA1; |
| 1434 | opt.auth_mode = DFL_AUTH_MODE; |
| 1435 | opt.cert_req_ca_list = DFL_CERT_REQ_CA_LIST; |
| 1436 | opt.mfl_code = DFL_MFL_CODE; |
| 1437 | opt.trunc_hmac = DFL_TRUNC_HMAC; |
| 1438 | opt.tickets = DFL_TICKETS; |
| 1439 | opt.ticket_timeout = DFL_TICKET_TIMEOUT; |
| 1440 | opt.cache_max = DFL_CACHE_MAX; |
| 1441 | opt.cache_timeout = DFL_CACHE_TIMEOUT; |
| 1442 | opt.sni = DFL_SNI; |
| 1443 | opt.alpn_string = DFL_ALPN_STRING; |
| 1444 | opt.curves = DFL_CURVES; |
| 1445 | opt.dhm_file = DFL_DHM_FILE; |
| 1446 | opt.transport = DFL_TRANSPORT; |
| 1447 | opt.cookies = DFL_COOKIES; |
| 1448 | opt.anti_replay = DFL_ANTI_REPLAY; |
| 1449 | opt.hs_to_min = DFL_HS_TO_MIN; |
| 1450 | opt.hs_to_max = DFL_HS_TO_MAX; |
| 1451 | opt.dtls_mtu = DFL_DTLS_MTU; |
| 1452 | opt.dgram_packing = DFL_DGRAM_PACKING; |
| 1453 | opt.badmac_limit = DFL_BADMAC_LIMIT; |
| 1454 | opt.extended_ms = DFL_EXTENDED_MS; |
| 1455 | opt.etm = DFL_ETM; |
| 1456 | opt.serialize = DFL_SERIALIZE; |
| 1457 | opt.context_file = DFL_CONTEXT_FILE; |
| 1458 | opt.eap_tls = DFL_EAP_TLS; |
| 1459 | opt.reproducible = DFL_REPRODUCIBLE; |
| 1460 | opt.nss_keylog = DFL_NSS_KEYLOG; |
| 1461 | opt.nss_keylog_file = DFL_NSS_KEYLOG_FILE; |
| 1462 | opt.query_config_mode = DFL_QUERY_CONFIG_MODE; |
| 1463 | opt.use_srtp = DFL_USE_SRTP; |
| 1464 | opt.force_srtp_profile = DFL_SRTP_FORCE_PROFILE; |
| 1465 | opt.support_mki = DFL_SRTP_SUPPORT_MKI; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1466 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1467 | for (i = 1; i < argc; i++) { |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1468 | p = argv[i]; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1469 | if ((q = strchr(p, '=')) == NULL) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1470 | goto usage; |
| 1471 | *q++ = '\0'; |
| 1472 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1473 | if (strcmp(p, "server_port") == 0) |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 1474 | opt.server_port = q; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1475 | else if (strcmp(p, "server_addr") == 0) |
Manuel Pégourié-Gonnard | 18d31f8 | 2013-12-13 16:21:41 +0100 | [diff] [blame] | 1476 | opt.server_addr = q; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1477 | else if (strcmp(p, "dtls") == 0) { |
| 1478 | int t = atoi(q); |
| 1479 | if (t == 0) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1480 | opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1481 | else if (t == 1) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1482 | opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM; |
Manuel Pégourié-Gonnard | e29fd4b | 2014-02-06 14:02:55 +0100 | [diff] [blame] | 1483 | else |
| 1484 | goto usage; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1485 | } else if (strcmp(p, "debug_level") == 0) { |
| 1486 | opt.debug_level = atoi(q); |
| 1487 | if (opt.debug_level < 0 || opt.debug_level > 65535) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1488 | goto usage; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1489 | } else if (strcmp(p, "nbio") == 0) { |
| 1490 | opt.nbio = atoi(q); |
| 1491 | if (opt.nbio < 0 || opt.nbio > 2) |
Manuel Pégourié-Gonnard | 5575316 | 2014-02-26 13:47:08 +0100 | [diff] [blame] | 1492 | goto usage; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1493 | } else if (strcmp(p, "event") == 0) { |
| 1494 | opt.event = atoi(q); |
| 1495 | if (opt.event < 0 || opt.event > 2) |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 1496 | goto usage; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1497 | } else if (strcmp(p, "read_timeout") == 0) |
| 1498 | opt.read_timeout = atoi(q); |
| 1499 | else if (strcmp(p, "buffer_size") == 0) { |
| 1500 | opt.buffer_size = atoi(q); |
| 1501 | if (opt.buffer_size < 1) |
Andrzej Kurek | 30e731d | 2017-10-12 13:50:29 +0200 | [diff] [blame] | 1502 | goto usage; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1503 | } else if (strcmp(p, "response_size") == 0) { |
| 1504 | opt.response_size = atoi(q); |
| 1505 | if (opt.response_size < 0 || |
| 1506 | opt.response_size > MBEDTLS_SSL_OUT_CONTENT_LEN) |
Andrzej Kurek | 30e731d | 2017-10-12 13:50:29 +0200 | [diff] [blame] | 1507 | goto usage; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1508 | if (opt.buffer_size < opt.response_size) |
Andrzej Kurek | 30e731d | 2017-10-12 13:50:29 +0200 | [diff] [blame] | 1509 | opt.buffer_size = opt.response_size; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1510 | } else if (strcmp(p, "ca_file") == 0) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1511 | opt.ca_file = q; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1512 | else if (strcmp(p, "ca_path") == 0) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1513 | opt.ca_path = q; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1514 | else if (strcmp(p, "crt_file") == 0) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1515 | opt.crt_file = q; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1516 | else if (strcmp(p, "key_file") == 0) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1517 | opt.key_file = q; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1518 | else if (strcmp(p, "key_pwd") == 0) |
Hanno Becker | e58a630 | 2018-11-07 16:20:16 +0000 | [diff] [blame] | 1519 | opt.key_pwd = q; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1520 | else if (strcmp(p, "crt_file2") == 0) |
Manuel Pégourié-Gonnard | 3ebb2cd | 2013-09-23 17:00:18 +0200 | [diff] [blame] | 1521 | opt.crt_file2 = q; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1522 | else if (strcmp(p, "key_file2") == 0) |
Manuel Pégourié-Gonnard | 3ebb2cd | 2013-09-23 17:00:18 +0200 | [diff] [blame] | 1523 | opt.key_file2 = q; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1524 | else if (strcmp(p, "key_pwd2") == 0) |
Hanno Becker | e58a630 | 2018-11-07 16:20:16 +0000 | [diff] [blame] | 1525 | opt.key_pwd2 = q; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1526 | else if (strcmp(p, "dhm_file") == 0) |
Manuel Pégourié-Gonnard | 736699c | 2014-06-09 11:29:50 +0200 | [diff] [blame] | 1527 | opt.dhm_file = q; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1528 | # if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 1529 | else if (strcmp(p, "async_operations") == 0) |
Gilles Peskine | fcca9d8 | 2018-01-12 13:47:48 +0100 | [diff] [blame] | 1530 | opt.async_operations = q; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1531 | else if (strcmp(p, "async_private_delay1") == 0) |
| 1532 | opt.async_private_delay1 = atoi(q); |
| 1533 | else if (strcmp(p, "async_private_delay2") == 0) |
| 1534 | opt.async_private_delay2 = atoi(q); |
| 1535 | else if (strcmp(p, "async_private_error") == 0) { |
| 1536 | int n = atoi(q); |
| 1537 | if (n < -SSL_ASYNC_INJECT_ERROR_MAX || |
| 1538 | n > SSL_ASYNC_INJECT_ERROR_MAX) { |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 1539 | ret = 2; |
| 1540 | goto usage; |
| 1541 | } |
| 1542 | opt.async_private_error = n; |
| 1543 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1544 | # endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 1545 | # if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 1546 | else if (strcmp(p, "cid") == 0) { |
| 1547 | opt.cid_enabled = atoi(q); |
| 1548 | if (opt.cid_enabled != 0 && opt.cid_enabled != 1) |
Hanno Becker | a7d2542 | 2019-04-09 17:28:10 +0100 | [diff] [blame] | 1549 | goto usage; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1550 | } else if (strcmp(p, "cid_renego") == 0) { |
| 1551 | opt.cid_enabled_renego = atoi(q); |
| 1552 | if (opt.cid_enabled_renego != 0 && opt.cid_enabled_renego != 1) |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 1553 | goto usage; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1554 | } else if (strcmp(p, "cid_val") == 0) { |
Hanno Becker | a7d2542 | 2019-04-09 17:28:10 +0100 | [diff] [blame] | 1555 | opt.cid_val = q; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1556 | } else if (strcmp(p, "cid_val_renego") == 0) { |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 1557 | opt.cid_val_renego = q; |
| 1558 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1559 | # endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 1560 | else if (strcmp(p, "psk") == 0) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 1561 | opt.psk = q; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1562 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1563 | else if (strcmp(p, "psk_opaque") == 0) |
| 1564 | opt.psk_opaque = atoi(q); |
| 1565 | else if (strcmp(p, "psk_list_opaque") == 0) |
| 1566 | opt.psk_list_opaque = atoi(q); |
| 1567 | # endif |
| 1568 | # if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1569 | else if (strcmp(p, "ca_callback") == 0) |
| 1570 | opt.ca_callback = atoi(q); |
| 1571 | # endif |
| 1572 | else if (strcmp(p, "psk_identity") == 0) |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 1573 | opt.psk_identity = q; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1574 | else if (strcmp(p, "psk_list") == 0) |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 1575 | opt.psk_list = q; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1576 | else if (strcmp(p, "ecjpake_pw") == 0) |
Manuel Pégourié-Gonnard | 70905a7 | 2015-09-16 11:08:34 +0200 | [diff] [blame] | 1577 | opt.ecjpake_pw = q; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1578 | else if (strcmp(p, "force_ciphersuite") == 0) { |
| 1579 | opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id(q); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1580 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1581 | if (opt.force_ciphersuite[0] == 0) { |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1582 | ret = 2; |
| 1583 | goto usage; |
| 1584 | } |
| 1585 | opt.force_ciphersuite[1] = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1586 | } else if (strcmp(p, "curves") == 0) |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 1587 | opt.curves = q; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1588 | else if (strcmp(p, "renegotiation") == 0) { |
| 1589 | opt.renegotiation = (atoi(q)) ? MBEDTLS_SSL_RENEGOTIATION_ENABLED : |
| 1590 | MBEDTLS_SSL_RENEGOTIATION_DISABLED; |
| 1591 | } else if (strcmp(p, "allow_legacy") == 0) { |
| 1592 | switch (atoi(q)) { |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 1593 | case -1: |
| 1594 | opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE; |
| 1595 | break; |
| 1596 | case 0: |
| 1597 | opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION; |
| 1598 | break; |
| 1599 | case 1: |
| 1600 | opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION; |
| 1601 | break; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1602 | default: |
| 1603 | goto usage; |
Manuel Pégourié-Gonnard | 85d915b | 2014-11-03 20:10:36 +0100 | [diff] [blame] | 1604 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1605 | } else if (strcmp(p, "renegotiate") == 0) { |
| 1606 | opt.renegotiate = atoi(q); |
| 1607 | if (opt.renegotiate < 0 || opt.renegotiate > 1) |
Manuel Pégourié-Gonnard | 780d671 | 2014-02-20 17:19:59 +0100 | [diff] [blame] | 1608 | goto usage; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1609 | } else if (strcmp(p, "renego_delay") == 0) { |
| 1610 | opt.renego_delay = atoi(q); |
| 1611 | } else if (strcmp(p, "renego_period") == 0) { |
| 1612 | # if defined(_MSC_VER) |
| 1613 | opt.renego_period = _strtoui64(q, NULL, 10); |
| 1614 | # else |
| 1615 | if (sscanf(q, "%" SCNu64, &opt.renego_period) != 1) |
Andres AG | 0b736db | 2017-02-08 14:05:57 +0000 | [diff] [blame] | 1616 | goto usage; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1617 | # endif /* _MSC_VER */ |
| 1618 | if (opt.renego_period < 2) |
Manuel Pégourié-Gonnard | 590f416 | 2014-11-05 14:23:03 +0100 | [diff] [blame] | 1619 | goto usage; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1620 | } else if (strcmp(p, "exchanges") == 0) { |
| 1621 | opt.exchanges = atoi(q); |
| 1622 | if (opt.exchanges < 0) |
Manuel Pégourié-Gonnard | 67686c4 | 2014-08-15 11:17:27 +0200 | [diff] [blame] | 1623 | goto usage; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1624 | } else if (strcmp(p, "min_version") == 0) { |
| 1625 | if (strcmp(q, "tls1_2") == 0 || strcmp(q, "dtls1_2") == 0) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1626 | opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 1627 | else |
| 1628 | goto usage; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1629 | } else if (strcmp(p, "max_version") == 0) { |
| 1630 | if (strcmp(q, "tls1_2") == 0 || strcmp(q, "dtls1_2") == 0) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1631 | opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3; |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 1632 | else |
| 1633 | goto usage; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1634 | } else if (strcmp(p, "allow_sha1") == 0) { |
| 1635 | switch (atoi(q)) { |
| 1636 | case 0: |
| 1637 | opt.allow_sha1 = 0; |
| 1638 | break; |
| 1639 | case 1: |
| 1640 | opt.allow_sha1 = 1; |
| 1641 | break; |
| 1642 | default: |
| 1643 | goto usage; |
Gilles Peskine | bc70a18 | 2017-05-09 15:59:24 +0200 | [diff] [blame] | 1644 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1645 | } else if (strcmp(p, "force_version") == 0) { |
| 1646 | if (strcmp(q, "tls1_2") == 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1647 | opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3; |
| 1648 | opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1649 | } else if (strcmp(q, "dtls1_2") == 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1650 | opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3; |
| 1651 | opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3; |
| 1652 | opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1653 | } else |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 1654 | goto usage; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1655 | } else if (strcmp(p, "auth_mode") == 0) { |
| 1656 | if ((opt.auth_mode = get_auth_mode(q)) < 0) |
Paul Bakker | 91ebfb5 | 2012-11-23 14:04:08 +0100 | [diff] [blame] | 1657 | goto usage; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1658 | } else if (strcmp(p, "cert_req_ca_list") == 0) { |
| 1659 | opt.cert_req_ca_list = atoi(q); |
| 1660 | if (opt.cert_req_ca_list < 0 || opt.cert_req_ca_list > 1) |
Janos Follath | 4817e27 | 2017-04-10 13:44:33 +0100 | [diff] [blame] | 1661 | goto usage; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1662 | } else if (strcmp(p, "max_frag_len") == 0) { |
| 1663 | if (strcmp(q, "512") == 0) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1664 | opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1665 | else if (strcmp(q, "1024") == 0) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1666 | opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1667 | else if (strcmp(q, "2048") == 0) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1668 | opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1669 | else if (strcmp(q, "4096") == 0) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1670 | opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096; |
Manuel Pégourié-Gonnard | 0c017a5 | 2013-07-18 14:07:36 +0200 | [diff] [blame] | 1671 | else |
| 1672 | goto usage; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1673 | } else if (strcmp(p, "alpn") == 0) { |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 1674 | opt.alpn_string = q; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1675 | } else if (strcmp(p, "trunc_hmac") == 0) { |
| 1676 | switch (atoi(q)) { |
| 1677 | case 0: |
| 1678 | opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; |
| 1679 | break; |
| 1680 | case 1: |
| 1681 | opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; |
| 1682 | break; |
| 1683 | default: |
| 1684 | goto usage; |
Manuel Pégourié-Gonnard | e117a8f | 2015-01-09 12:39:35 +0100 | [diff] [blame] | 1685 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1686 | } else if (strcmp(p, "extended_ms") == 0) { |
| 1687 | switch (atoi(q)) { |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 1688 | case 0: |
| 1689 | opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED; |
| 1690 | break; |
| 1691 | case 1: |
| 1692 | opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 1693 | break; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1694 | default: |
| 1695 | goto usage; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 1696 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1697 | } else if (strcmp(p, "etm") == 0) { |
| 1698 | switch (atoi(q)) { |
| 1699 | case 0: |
| 1700 | opt.etm = MBEDTLS_SSL_ETM_DISABLED; |
| 1701 | break; |
| 1702 | case 1: |
| 1703 | opt.etm = MBEDTLS_SSL_ETM_ENABLED; |
| 1704 | break; |
| 1705 | default: |
| 1706 | goto usage; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 1707 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1708 | } else if (strcmp(p, "tickets") == 0) { |
| 1709 | opt.tickets = atoi(q); |
| 1710 | if (opt.tickets < 0 || opt.tickets > 1) |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 1711 | goto usage; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1712 | } else if (strcmp(p, "ticket_timeout") == 0) { |
| 1713 | opt.ticket_timeout = atoi(q); |
| 1714 | if (opt.ticket_timeout < 0) |
Manuel Pégourié-Gonnard | dbe1ee1 | 2014-02-21 09:18:13 +0100 | [diff] [blame] | 1715 | goto usage; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1716 | } else if (strcmp(p, "cache_max") == 0) { |
| 1717 | opt.cache_max = atoi(q); |
| 1718 | if (opt.cache_max < 0) |
Manuel Pégourié-Gonnard | 4c88345 | 2014-02-20 21:32:41 +0100 | [diff] [blame] | 1719 | goto usage; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1720 | } else if (strcmp(p, "cache_timeout") == 0) { |
| 1721 | opt.cache_timeout = atoi(q); |
| 1722 | if (opt.cache_timeout < 0) |
Manuel Pégourié-Gonnard | c55a5b7 | 2014-02-20 22:50:56 +0100 | [diff] [blame] | 1723 | goto usage; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1724 | } else if (strcmp(p, "cookies") == 0) { |
| 1725 | opt.cookies = atoi(q); |
| 1726 | if (opt.cookies < -1 || opt.cookies > 1) |
Manuel Pégourié-Gonnard | 26820e3 | 2014-07-23 19:34:59 +0200 | [diff] [blame] | 1727 | goto usage; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1728 | } else if (strcmp(p, "anti_replay") == 0) { |
| 1729 | opt.anti_replay = atoi(q); |
| 1730 | if (opt.anti_replay < 0 || opt.anti_replay > 1) |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 1731 | goto usage; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1732 | } else if (strcmp(p, "badmac_limit") == 0) { |
| 1733 | opt.badmac_limit = atoi(q); |
| 1734 | if (opt.badmac_limit < 0) |
Manuel Pégourié-Gonnard | e698f59 | 2014-10-14 19:36:36 +0200 | [diff] [blame] | 1735 | goto usage; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1736 | } else if (strcmp(p, "hs_timeout") == 0) { |
| 1737 | if ((p = strchr(q, '-')) == NULL) |
Manuel Pégourié-Gonnard | d823bd0 | 2014-10-01 14:40:56 +0200 | [diff] [blame] | 1738 | goto usage; |
| 1739 | *p++ = '\0'; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1740 | opt.hs_to_min = atoi(q); |
| 1741 | opt.hs_to_max = atoi(p); |
| 1742 | if (opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min) |
Manuel Pégourié-Gonnard | d823bd0 | 2014-10-01 14:40:56 +0200 | [diff] [blame] | 1743 | goto usage; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1744 | } else if (strcmp(p, "mtu") == 0) { |
| 1745 | opt.dtls_mtu = atoi(q); |
| 1746 | if (opt.dtls_mtu < 0) |
Manuel Pégourié-Gonnard | b747c6c | 2018-08-12 13:28:53 +0200 | [diff] [blame] | 1747 | goto usage; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1748 | } else if (strcmp(p, "dgram_packing") == 0) { |
| 1749 | opt.dgram_packing = atoi(q); |
| 1750 | if (opt.dgram_packing != 0 && opt.dgram_packing != 1) { |
Hanno Becker | e7675d0 | 2018-08-14 13:28:56 +0100 | [diff] [blame] | 1751 | goto usage; |
| 1752 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1753 | } else if (strcmp(p, "sni") == 0) { |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 1754 | opt.sni = q; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1755 | } else if (strcmp(p, "query_config") == 0) { |
gabor-mezei-arm | a9eecf1 | 2020-07-07 11:11:02 +0200 | [diff] [blame] | 1756 | opt.query_config_mode = 1; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1757 | query_config_ret = query_config(q); |
gabor-mezei-arm | 7859585 | 2020-04-28 10:40:30 +0200 | [diff] [blame] | 1758 | goto exit; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1759 | } else if (strcmp(p, "serialize") == 0) { |
| 1760 | opt.serialize = atoi(q); |
| 1761 | if (opt.serialize < 0 || opt.serialize > 2) |
Jarno Lamsa | 9831c8a | 2019-05-29 13:33:32 +0300 | [diff] [blame] | 1762 | goto usage; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1763 | } else if (strcmp(p, "context_file") == 0) { |
Piotr Nowicki | 3de298f | 2020-04-16 14:35:19 +0200 | [diff] [blame] | 1764 | opt.context_file = q; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1765 | } else if (strcmp(p, "eap_tls") == 0) { |
| 1766 | opt.eap_tls = atoi(q); |
| 1767 | if (opt.eap_tls < 0 || opt.eap_tls > 1) |
Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 1768 | goto usage; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1769 | } else if (strcmp(p, "reproducible") == 0) { |
Philippe Antoine | aa4d152 | 2019-06-06 21:24:31 +0200 | [diff] [blame] | 1770 | opt.reproducible = 1; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1771 | } else if (strcmp(p, "nss_keylog") == 0) { |
| 1772 | opt.nss_keylog = atoi(q); |
| 1773 | if (opt.nss_keylog < 0 || opt.nss_keylog > 1) |
Hanno Becker | 48f3a3d | 2019-08-29 06:31:22 +0100 | [diff] [blame] | 1774 | goto usage; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1775 | } else if (strcmp(p, "nss_keylog_file") == 0) { |
Hanno Becker | 48f3a3d | 2019-08-29 06:31:22 +0100 | [diff] [blame] | 1776 | opt.nss_keylog_file = q; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1777 | } else if (strcmp(p, "use_srtp") == 0) { |
| 1778 | opt.use_srtp = atoi(q); |
| 1779 | } else if (strcmp(p, "srtp_force_profile") == 0) { |
| 1780 | opt.force_srtp_profile = atoi(q); |
| 1781 | } else if (strcmp(p, "support_mki") == 0) { |
| 1782 | opt.support_mki = atoi(q); |
| 1783 | } else |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1784 | goto usage; |
| 1785 | } |
| 1786 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1787 | if (opt.nss_keylog != 0 && opt.eap_tls != 0) { |
| 1788 | mbedtls_printf( |
| 1789 | "Error: eap_tls and nss_keylog options cannot be used together.\n"); |
Hanno Becker | bc5308c | 2019-09-09 11:38:51 +0100 | [diff] [blame] | 1790 | goto usage; |
| 1791 | } |
| 1792 | |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 1793 | /* Event-driven IO is incompatible with the above custom |
| 1794 | * receive and send functions, as the polling builds on |
| 1795 | * refers to the underlying net_context. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1796 | if (opt.event == 1 && opt.nbio != 1) { |
| 1797 | mbedtls_printf( |
| 1798 | "Warning: event-driven IO mandates nbio=1 - overwrite\n"); |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 1799 | opt.nbio = 1; |
| 1800 | } |
| 1801 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1802 | # if defined(MBEDTLS_DEBUG_C) |
| 1803 | mbedtls_debug_set_threshold(opt.debug_level); |
| 1804 | # endif |
| 1805 | buf = mbedtls_calloc(1, opt.buffer_size + 1); |
| 1806 | if (buf == NULL) { |
| 1807 | mbedtls_printf("Could not allocate %u bytes\n", opt.buffer_size); |
Andrzej Kurek | 30e731d | 2017-10-12 13:50:29 +0200 | [diff] [blame] | 1808 | ret = 3; |
| 1809 | goto exit; |
| 1810 | } |
Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 1811 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1812 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1813 | if (opt.psk_opaque != 0) { |
| 1814 | if (strlen(opt.psk) == 0) { |
| 1815 | mbedtls_printf( |
| 1816 | "psk_opaque set but no psk to be imported specified.\n"); |
Hanno Becker | b64ba5f | 2018-10-26 11:28:08 +0100 | [diff] [blame] | 1817 | ret = 2; |
| 1818 | goto usage; |
| 1819 | } |
| 1820 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1821 | if (opt.force_ciphersuite[0] <= 0) { |
| 1822 | mbedtls_printf( |
| 1823 | "opaque PSKs are only supported in conjunction with forcing TLS 1.2 and a PSK-only ciphersuite through the 'force_ciphersuite' option.\n"); |
Hanno Becker | b64ba5f | 2018-10-26 11:28:08 +0100 | [diff] [blame] | 1824 | ret = 2; |
| 1825 | goto usage; |
| 1826 | } |
| 1827 | } |
| 1828 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1829 | if (opt.psk_list_opaque != 0) { |
| 1830 | if (opt.psk_list == NULL) { |
| 1831 | mbedtls_printf( |
| 1832 | "psk_slot set but no psk to be imported specified.\n"); |
Hanno Becker | b64ba5f | 2018-10-26 11:28:08 +0100 | [diff] [blame] | 1833 | ret = 2; |
| 1834 | goto usage; |
| 1835 | } |
| 1836 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1837 | if (opt.force_ciphersuite[0] <= 0) { |
| 1838 | mbedtls_printf( |
| 1839 | "opaque PSKs are only supported in conjunction with forcing TLS 1.2 and a PSK-only ciphersuite through the 'force_ciphersuite' option.\n"); |
Hanno Becker | b64ba5f | 2018-10-26 11:28:08 +0100 | [diff] [blame] | 1840 | ret = 2; |
| 1841 | goto usage; |
| 1842 | } |
| 1843 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1844 | # endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | b64ba5f | 2018-10-26 11:28:08 +0100 | [diff] [blame] | 1845 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1846 | if (opt.force_ciphersuite[0] > 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1847 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Hanno Becker | 4cb1f4d | 2017-10-10 15:59:57 +0100 | [diff] [blame] | 1848 | ciphersuite_info = |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1849 | mbedtls_ssl_ciphersuite_from_id(opt.force_ciphersuite[0]); |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 1850 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1851 | if (opt.max_version != -1 && |
| 1852 | ciphersuite_info->min_minor_ver > opt.max_version) { |
| 1853 | mbedtls_printf( |
| 1854 | "forced ciphersuite not allowed with this protocol version\n"); |
Paul Bakker | 5b55b79 | 2013-07-19 13:43:43 +0200 | [diff] [blame] | 1855 | ret = 2; |
| 1856 | goto usage; |
| 1857 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1858 | if (opt.min_version != -1 && |
| 1859 | ciphersuite_info->max_minor_ver < opt.min_version) { |
| 1860 | mbedtls_printf( |
| 1861 | "forced ciphersuite not allowed with this protocol version\n"); |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 1862 | ret = 2; |
| 1863 | goto usage; |
| 1864 | } |
Manuel Pégourié-Gonnard | 798f15a | 2014-03-26 18:12:04 +0100 | [diff] [blame] | 1865 | |
| 1866 | /* If we select a version that's not supported by |
| 1867 | * this suite, then there will be no common ciphersuite... */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1868 | if (opt.max_version == -1 || |
| 1869 | opt.max_version > ciphersuite_info->max_minor_ver) { |
Paul Bakker | 5b55b79 | 2013-07-19 13:43:43 +0200 | [diff] [blame] | 1870 | opt.max_version = ciphersuite_info->max_minor_ver; |
Manuel Pégourié-Gonnard | 798f15a | 2014-03-26 18:12:04 +0100 | [diff] [blame] | 1871 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1872 | if (opt.min_version < ciphersuite_info->min_minor_ver) { |
Paul Bakker | 5b55b79 | 2013-07-19 13:43:43 +0200 | [diff] [blame] | 1873 | opt.min_version = ciphersuite_info->min_minor_ver; |
Manuel Pégourié-Gonnard | 798f15a | 2014-03-26 18:12:04 +0100 | [diff] [blame] | 1874 | } |
Manuel Pégourié-Gonnard | d42b7c8 | 2015-03-20 19:44:04 +0000 | [diff] [blame] | 1875 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1876 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1877 | if (opt.psk_opaque != 0 || opt.psk_list_opaque != 0) { |
Hanno Becker | b64ba5f | 2018-10-26 11:28:08 +0100 | [diff] [blame] | 1878 | /* Ensure that the chosen ciphersuite is PSK-only; we must know |
| 1879 | * the ciphersuite in advance to set the correct policy for the |
| 1880 | * PSK key slot. This limitation might go away in the future. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1881 | if (ciphersuite_info->key_exchange != MBEDTLS_KEY_EXCHANGE_PSK || |
| 1882 | opt.min_version != MBEDTLS_SSL_MINOR_VERSION_3) { |
| 1883 | mbedtls_printf( |
| 1884 | "opaque PSKs are only supported in conjunction with forcing TLS 1.2 and a PSK-only ciphersuite through the 'force_ciphersuite' option.\n"); |
Hanno Becker | b64ba5f | 2018-10-26 11:28:08 +0100 | [diff] [blame] | 1885 | ret = 2; |
| 1886 | goto usage; |
| 1887 | } |
| 1888 | |
| 1889 | /* Determine KDF algorithm the opaque PSK will be used in. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1890 | # if defined(MBEDTLS_SHA384_C) |
| 1891 | if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) |
Hanno Becker | b64ba5f | 2018-10-26 11:28:08 +0100 | [diff] [blame] | 1892 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
| 1893 | else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1894 | # endif /* MBEDTLS_SHA384_C */ |
Hanno Becker | b64ba5f | 2018-10-26 11:28:08 +0100 | [diff] [blame] | 1895 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 1896 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1897 | # endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 1898 | } |
| 1899 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1900 | # if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 1901 | if (mbedtls_test_unhexify(cid, sizeof(cid), opt.cid_val, &cid_len) != 0) { |
| 1902 | mbedtls_printf("CID not valid hex\n"); |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 1903 | goto exit; |
| 1904 | } |
| 1905 | |
| 1906 | /* Keep CID settings for renegotiation unless |
| 1907 | * specified otherwise. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1908 | if (opt.cid_enabled_renego == DFL_CID_ENABLED_RENEGO) |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 1909 | opt.cid_enabled_renego = opt.cid_enabled; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1910 | if (opt.cid_val_renego == DFL_CID_VALUE_RENEGO) |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 1911 | opt.cid_val_renego = opt.cid_val; |
| 1912 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1913 | if (mbedtls_test_unhexify(cid_renego, sizeof(cid_renego), |
| 1914 | opt.cid_val_renego, &cid_renego_len) != 0) { |
| 1915 | mbedtls_printf("CID not valid hex\n"); |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 1916 | goto exit; |
| 1917 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1918 | # endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | a7d2542 | 2019-04-09 17:28:10 +0100 | [diff] [blame] | 1919 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1920 | # if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1921 | /* |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 1922 | * Unhexify the pre-shared key and parse the list if any given |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 1923 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1924 | if (mbedtls_test_unhexify(psk, sizeof(psk), opt.psk, &psk_len) != 0) { |
| 1925 | mbedtls_printf("pre-shared key not valid hex\n"); |
Manuel Pégourié-Gonnard | 80c8553 | 2014-06-10 14:01:52 +0200 | [diff] [blame] | 1926 | goto exit; |
| 1927 | } |
| 1928 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1929 | if (opt.psk_list != NULL) { |
| 1930 | if ((psk_info = psk_parse(opt.psk_list)) == NULL) { |
| 1931 | mbedtls_printf("psk_list invalid"); |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 1932 | goto exit; |
| 1933 | } |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 1934 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1935 | # endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 1936 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1937 | # if defined(MBEDTLS_ECP_C) |
| 1938 | if (opt.curves != NULL) { |
| 1939 | p = (char *)opt.curves; |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 1940 | i = 0; |
| 1941 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1942 | if (strcmp(p, "none") == 0) { |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 1943 | curve_list[0] = MBEDTLS_ECP_DP_NONE; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1944 | } else if (strcmp(p, "default") != 0) { |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 1945 | /* Leave room for a final NULL in curve list */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1946 | while (i < CURVE_LIST_SIZE - 1 && *p != '\0') { |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 1947 | q = p; |
| 1948 | |
| 1949 | /* Terminate the current string */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1950 | while (*p != ',' && *p != '\0') |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 1951 | p++; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1952 | if (*p == ',') |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 1953 | *p++ = '\0'; |
| 1954 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1955 | if ((curve_cur = mbedtls_ecp_curve_info_from_name(q)) != NULL) { |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 1956 | curve_list[i++] = curve_cur->grp_id; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1957 | } else { |
| 1958 | mbedtls_printf("unknown curve %s\n", q); |
| 1959 | mbedtls_printf("supported curves: "); |
| 1960 | for (curve_cur = mbedtls_ecp_curve_list(); |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 1961 | curve_cur->grp_id != MBEDTLS_ECP_DP_NONE; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1962 | curve_cur++) { |
| 1963 | mbedtls_printf("%s ", curve_cur->name); |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 1964 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1965 | mbedtls_printf("\n"); |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 1966 | goto exit; |
| 1967 | } |
| 1968 | } |
| 1969 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1970 | mbedtls_printf("Number of curves: %d\n", i); |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 1971 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1972 | if (i == CURVE_LIST_SIZE - 1 && *p != '\0') { |
| 1973 | mbedtls_printf("curves list too long, maximum %d", |
| 1974 | CURVE_LIST_SIZE - 1); |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 1975 | goto exit; |
| 1976 | } |
| 1977 | |
| 1978 | curve_list[i] = MBEDTLS_ECP_DP_NONE; |
| 1979 | } |
| 1980 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1981 | # endif /* MBEDTLS_ECP_C */ |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 1982 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1983 | # if defined(MBEDTLS_SSL_ALPN) |
| 1984 | if (opt.alpn_string != NULL) { |
| 1985 | p = (char *)opt.alpn_string; |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 1986 | i = 0; |
| 1987 | |
| 1988 | /* Leave room for a final NULL in alpn_list */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1989 | while (i < ALPN_LIST_SIZE - 1 && *p != '\0') { |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 1990 | alpn_list[i++] = p; |
| 1991 | |
| 1992 | /* Terminate the current string and move on to next one */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1993 | while (*p != ',' && *p != '\0') |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 1994 | p++; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1995 | if (*p == ',') |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 1996 | *p++ = '\0'; |
| 1997 | } |
| 1998 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1999 | # endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 2000 | |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame] | 2001 | /* |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2002 | * 0. Initialize the RNG and the session data |
| 2003 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2004 | mbedtls_printf("\n . Seeding the random number generator..."); |
| 2005 | fflush(stdout); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2006 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2007 | ret = rng_seed(&rng, opt.reproducible, pers); |
| 2008 | if (ret != 0) |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 2009 | goto exit; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2010 | mbedtls_printf(" ok\n"); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2011 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2012 | # if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2013 | /* |
| 2014 | * 1.1. Load the trusted CA |
| 2015 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2016 | mbedtls_printf(" . Loading the CA root certificate ..."); |
| 2017 | fflush(stdout); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2018 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2019 | if (strcmp(opt.ca_path, "none") == 0 || strcmp(opt.ca_file, "none") == 0) { |
Hanno Becker | 8174bdf | 2019-03-05 16:22:07 +0000 | [diff] [blame] | 2020 | ret = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2021 | } else |
| 2022 | # if defined(MBEDTLS_FS_IO) |
| 2023 | if (strlen(opt.ca_path)) |
| 2024 | ret = mbedtls_x509_crt_parse_path(&cacert, opt.ca_path); |
| 2025 | else if (strlen(opt.ca_file)) |
| 2026 | ret = mbedtls_x509_crt_parse_file(&cacert, opt.ca_file); |
Hanno Becker | 8174bdf | 2019-03-05 16:22:07 +0000 | [diff] [blame] | 2027 | else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2028 | # endif |
Hanno Becker | 09b8cae | 2019-02-01 08:13:19 +0000 | [diff] [blame] | 2029 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2030 | # if defined(MBEDTLS_PEM_PARSE_C) |
| 2031 | for (i = 0; mbedtls_test_cas[i] != NULL; i++) { |
| 2032 | ret = mbedtls_x509_crt_parse( |
| 2033 | &cacert, (const unsigned char *)mbedtls_test_cas[i], |
| 2034 | mbedtls_test_cas_len[i]); |
| 2035 | if (ret != 0) |
Manuel Pégourié-Gonnard | 2f16506 | 2015-03-27 10:20:26 +0100 | [diff] [blame] | 2036 | break; |
| 2037 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2038 | if (ret == 0) |
| 2039 | # endif /* MBEDTLS_PEM_PARSE_C */ |
| 2040 | for (i = 0; mbedtls_test_cas_der[i] != NULL; i++) { |
| 2041 | ret = mbedtls_x509_crt_parse_der( |
| 2042 | &cacert, (const unsigned char *)mbedtls_test_cas_der[i], |
| 2043 | mbedtls_test_cas_der_len[i]); |
| 2044 | if (ret != 0) |
| 2045 | break; |
| 2046 | } |
Hanno Becker | 09b8cae | 2019-02-01 08:13:19 +0000 | [diff] [blame] | 2047 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2048 | if (ret < 0) { |
| 2049 | mbedtls_printf( |
| 2050 | " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", |
| 2051 | (unsigned int)-ret); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2052 | goto exit; |
| 2053 | } |
| 2054 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2055 | mbedtls_printf(" ok (%d skipped)\n", ret); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2056 | |
| 2057 | /* |
| 2058 | * 1.2. Load own certificate and private key |
| 2059 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2060 | mbedtls_printf(" . Loading the server cert. and key..."); |
| 2061 | fflush(stdout); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2062 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2063 | # if defined(MBEDTLS_FS_IO) |
| 2064 | if (strlen(opt.crt_file) && strcmp(opt.crt_file, "none") != 0) { |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 2065 | key_cert_init++; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2066 | if ((ret = mbedtls_x509_crt_parse_file(&srvcert, opt.crt_file)) != 0) { |
| 2067 | mbedtls_printf( |
| 2068 | " failed\n ! mbedtls_x509_crt_parse_file returned -0x%x\n\n", |
| 2069 | (unsigned int)-ret); |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 2070 | goto exit; |
| 2071 | } |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 2072 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2073 | if (strlen(opt.key_file) && strcmp(opt.key_file, "none") != 0) { |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 2074 | key_cert_init++; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2075 | if ((ret = mbedtls_pk_parse_keyfile(&pkey, opt.key_file, opt.key_pwd, |
| 2076 | rng_get, &rng)) != 0) { |
| 2077 | mbedtls_printf( |
| 2078 | " failed\n ! mbedtls_pk_parse_keyfile returned -0x%x\n\n", |
| 2079 | (unsigned int)-ret); |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 2080 | goto exit; |
| 2081 | } |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 2082 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2083 | if (key_cert_init == 1) { |
| 2084 | mbedtls_printf( |
| 2085 | " failed\n ! crt_file without key_file or vice-versa\n\n"); |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 2086 | goto exit; |
| 2087 | } |
| 2088 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2089 | if (strlen(opt.crt_file2) && strcmp(opt.crt_file2, "none") != 0) { |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 2090 | key_cert_init2++; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2091 | if ((ret = mbedtls_x509_crt_parse_file(&srvcert2, opt.crt_file2)) != |
| 2092 | 0) { |
| 2093 | mbedtls_printf( |
| 2094 | " failed\n ! mbedtls_x509_crt_parse_file(2) returned -0x%x\n\n", |
| 2095 | (unsigned int)-ret); |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 2096 | goto exit; |
| 2097 | } |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 2098 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2099 | if (strlen(opt.key_file2) && strcmp(opt.key_file2, "none") != 0) { |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 2100 | key_cert_init2++; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2101 | if ((ret = mbedtls_pk_parse_keyfile(&pkey2, opt.key_file2, opt.key_pwd2, |
| 2102 | rng_get, &rng)) != 0) { |
| 2103 | mbedtls_printf( |
| 2104 | " failed\n ! mbedtls_pk_parse_keyfile(2) returned -0x%x\n\n", |
| 2105 | (unsigned int)-ret); |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 2106 | goto exit; |
| 2107 | } |
Manuel Pégourié-Gonnard | b095a7b | 2013-09-24 21:14:51 +0200 | [diff] [blame] | 2108 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2109 | if (key_cert_init2 == 1) { |
| 2110 | mbedtls_printf( |
| 2111 | " failed\n ! crt_file2 without key_file2 or vice-versa\n\n"); |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 2112 | goto exit; |
| 2113 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2114 | # endif |
| 2115 | if (key_cert_init == 0 && strcmp(opt.crt_file, "none") != 0 && |
| 2116 | strcmp(opt.key_file, "none") != 0 && key_cert_init2 == 0 && |
| 2117 | strcmp(opt.crt_file2, "none") != 0 && |
| 2118 | strcmp(opt.key_file2, "none") != 0) { |
| 2119 | # if defined(MBEDTLS_RSA_C) |
| 2120 | if ((ret = mbedtls_x509_crt_parse( |
| 2121 | &srvcert, (const unsigned char *)mbedtls_test_srv_crt_rsa, |
| 2122 | mbedtls_test_srv_crt_rsa_len)) != 0) { |
| 2123 | mbedtls_printf( |
| 2124 | " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", |
| 2125 | (unsigned int)-ret); |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 2126 | goto exit; |
| 2127 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2128 | if ((ret = mbedtls_pk_parse_key( |
| 2129 | &pkey, (const unsigned char *)mbedtls_test_srv_key_rsa, |
| 2130 | mbedtls_test_srv_key_rsa_len, NULL, 0, rng_get, &rng)) != 0) { |
| 2131 | mbedtls_printf( |
| 2132 | " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n", |
| 2133 | (unsigned int)-ret); |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 2134 | goto exit; |
| 2135 | } |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 2136 | key_cert_init = 2; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2137 | # endif /* MBEDTLS_RSA_C */ |
| 2138 | # if defined(MBEDTLS_ECDSA_C) |
| 2139 | if ((ret = mbedtls_x509_crt_parse( |
| 2140 | &srvcert2, (const unsigned char *)mbedtls_test_srv_crt_ec, |
| 2141 | mbedtls_test_srv_crt_ec_len)) != 0) { |
| 2142 | mbedtls_printf(" failed\n ! x509_crt_parse2 returned -0x%x\n\n", |
| 2143 | (unsigned int)-ret); |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 2144 | goto exit; |
| 2145 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2146 | if ((ret = mbedtls_pk_parse_key( |
| 2147 | &pkey2, (const unsigned char *)mbedtls_test_srv_key_ec, |
| 2148 | mbedtls_test_srv_key_ec_len, NULL, 0, rng_get, &rng)) != 0) { |
| 2149 | mbedtls_printf(" failed\n ! pk_parse_key2 returned -0x%x\n\n", |
| 2150 | (unsigned int)-ret); |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 2151 | goto exit; |
| 2152 | } |
Manuel Pégourié-Gonnard | a0fdf8b | 2013-09-25 14:05:49 +0200 | [diff] [blame] | 2153 | key_cert_init2 = 2; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2154 | # endif /* MBEDTLS_ECDSA_C */ |
Manuel Pégourié-Gonnard | ac8474f | 2013-09-25 11:35:15 +0200 | [diff] [blame] | 2155 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2156 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2157 | mbedtls_printf(" ok\n"); |
| 2158 | # endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2159 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2160 | # if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO) |
| 2161 | if (opt.dhm_file != NULL) { |
| 2162 | mbedtls_printf(" . Loading DHM parameters..."); |
| 2163 | fflush(stdout); |
Manuel Pégourié-Gonnard | 736699c | 2014-06-09 11:29:50 +0200 | [diff] [blame] | 2164 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2165 | if ((ret = mbedtls_dhm_parse_dhmfile(&dhm, opt.dhm_file)) != 0) { |
| 2166 | mbedtls_printf( |
| 2167 | " failed\n ! mbedtls_dhm_parse_dhmfile returned -0x%04X\n\n", |
| 2168 | (unsigned int)-ret); |
Manuel Pégourié-Gonnard | 736699c | 2014-06-09 11:29:50 +0200 | [diff] [blame] | 2169 | goto exit; |
| 2170 | } |
| 2171 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2172 | mbedtls_printf(" ok\n"); |
Manuel Pégourié-Gonnard | 736699c | 2014-06-09 11:29:50 +0200 | [diff] [blame] | 2173 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2174 | # endif |
Manuel Pégourié-Gonnard | 736699c | 2014-06-09 11:29:50 +0200 | [diff] [blame] | 2175 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2176 | # if defined(SNI_OPTION) |
| 2177 | if (opt.sni != NULL) { |
| 2178 | mbedtls_printf(" . Setting up SNI information..."); |
| 2179 | fflush(stdout); |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 2180 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2181 | if ((sni_info = sni_parse(opt.sni)) == NULL) { |
| 2182 | mbedtls_printf(" failed\n"); |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 2183 | goto exit; |
| 2184 | } |
| 2185 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2186 | mbedtls_printf(" ok\n"); |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 2187 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2188 | # endif /* SNI_OPTION */ |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 2189 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2190 | /* |
| 2191 | * 2. Setup the listening TCP socket |
| 2192 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2193 | mbedtls_printf(" . Bind on %s://%s:%s/ ...", |
| 2194 | opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : |
| 2195 | "udp", |
| 2196 | opt.server_addr ? opt.server_addr : "*", opt.server_port); |
| 2197 | fflush(stdout); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2198 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2199 | if ((ret = mbedtls_net_bind(&listen_fd, opt.server_addr, opt.server_port, |
| 2200 | opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? |
| 2201 | MBEDTLS_NET_PROTO_TCP : |
| 2202 | MBEDTLS_NET_PROTO_UDP)) != 0) { |
| 2203 | mbedtls_printf(" failed\n ! mbedtls_net_bind returned -0x%x\n\n", |
| 2204 | (unsigned int)-ret); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2205 | goto exit; |
| 2206 | } |
| 2207 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2208 | mbedtls_printf(" ok\n"); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2209 | |
| 2210 | /* |
| 2211 | * 3. Setup stuff |
| 2212 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2213 | mbedtls_printf(" . Setting up the SSL/TLS structure..."); |
| 2214 | fflush(stdout); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2215 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2216 | if ((ret = mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_SERVER, |
| 2217 | opt.transport, |
| 2218 | MBEDTLS_SSL_PRESET_DEFAULT)) != 0) { |
| 2219 | mbedtls_printf( |
| 2220 | " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n", |
| 2221 | (unsigned int)-ret); |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 2222 | goto exit; |
| 2223 | } |
| 2224 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2225 | # if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | ef86ab2 | 2017-05-05 18:59:02 +0200 | [diff] [blame] | 2226 | /* The default algorithms profile disables SHA-1, but our tests still |
| 2227 | rely on it heavily. Hence we allow it here. A real-world server |
| 2228 | should use the default profile unless there is a good reason not to. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2229 | if (opt.allow_sha1 > 0) { |
| 2230 | crt_profile_for_test.allowed_mds |= |
| 2231 | MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA1); |
| 2232 | mbedtls_ssl_conf_cert_profile(&conf, &crt_profile_for_test); |
| 2233 | mbedtls_ssl_conf_sig_hashes(&conf, ssl_sig_hashes_for_test); |
Gilles Peskine | bc70a18 | 2017-05-09 15:59:24 +0200 | [diff] [blame] | 2234 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2235 | # endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Gilles Peskine | ef86ab2 | 2017-05-05 18:59:02 +0200 | [diff] [blame] | 2236 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2237 | if (opt.auth_mode != DFL_AUTH_MODE) |
| 2238 | mbedtls_ssl_conf_authmode(&conf, opt.auth_mode); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2239 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2240 | if (opt.cert_req_ca_list != DFL_CERT_REQ_CA_LIST) |
| 2241 | mbedtls_ssl_conf_cert_req_ca_list(&conf, opt.cert_req_ca_list); |
Janos Follath | 4817e27 | 2017-04-10 13:44:33 +0100 | [diff] [blame] | 2242 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2243 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2244 | if (opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX) |
| 2245 | mbedtls_ssl_conf_handshake_timeout(&conf, opt.hs_to_min, opt.hs_to_max); |
Manuel Pégourié-Gonnard | b747c6c | 2018-08-12 13:28:53 +0200 | [diff] [blame] | 2246 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2247 | if (opt.dgram_packing != DFL_DGRAM_PACKING) |
| 2248 | mbedtls_ssl_set_datagram_packing(&ssl, opt.dgram_packing); |
| 2249 | # endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | d823bd0 | 2014-10-01 14:40:56 +0200 | [diff] [blame] | 2250 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2251 | # if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 2252 | if ((ret = mbedtls_ssl_conf_max_frag_len(&conf, opt.mfl_code)) != 0) { |
| 2253 | mbedtls_printf( |
| 2254 | " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n", ret); |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 2255 | goto exit; |
Ron Eldor | 6ea6451 | 2018-07-02 10:08:07 +0300 | [diff] [blame] | 2256 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2257 | # endif |
Manuel Pégourié-Gonnard | 0c017a5 | 2013-07-18 14:07:36 +0200 | [diff] [blame] | 2258 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2259 | # if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 2260 | if (opt.cid_enabled == 1 || opt.cid_enabled_renego == 1) { |
| 2261 | if (opt.cid_enabled == 1 && opt.cid_enabled_renego == 1 && |
| 2262 | cid_len != cid_renego_len) { |
| 2263 | mbedtls_printf("CID length must not change during renegotiation\n"); |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 2264 | goto usage; |
| 2265 | } |
| 2266 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2267 | if (opt.cid_enabled == 1) |
| 2268 | ret = mbedtls_ssl_conf_cid(&conf, cid_len, |
| 2269 | MBEDTLS_SSL_UNEXPECTED_CID_IGNORE); |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 2270 | else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2271 | ret = mbedtls_ssl_conf_cid(&conf, cid_renego_len, |
| 2272 | MBEDTLS_SSL_UNEXPECTED_CID_IGNORE); |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 2273 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2274 | if (ret != 0) { |
| 2275 | mbedtls_printf( |
| 2276 | " failed\n ! mbedtls_ssl_conf_cid_len returned -%#04x\n\n", |
| 2277 | (unsigned int)-ret); |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 2278 | goto exit; |
| 2279 | } |
| 2280 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2281 | # endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 2282 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2283 | # if defined(MBEDTLS_SSL_DTLS_SRTP) |
| 2284 | const mbedtls_ssl_srtp_profile forced_profile[] = { |
| 2285 | opt.force_srtp_profile, MBEDTLS_TLS_SRTP_UNSET |
| 2286 | }; |
| 2287 | if (opt.use_srtp == 1) { |
| 2288 | if (opt.force_srtp_profile != 0) { |
| 2289 | ret = mbedtls_ssl_conf_dtls_srtp_protection_profiles( |
| 2290 | &conf, forced_profile); |
| 2291 | } else { |
| 2292 | ret = mbedtls_ssl_conf_dtls_srtp_protection_profiles( |
| 2293 | &conf, default_profiles); |
Ron Eldor | 6ea6451 | 2018-07-02 10:08:07 +0300 | [diff] [blame] | 2294 | } |
| 2295 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2296 | if (ret != 0) { |
| 2297 | mbedtls_printf( |
| 2298 | " failed\n ! mbedtls_ssl_conf_dtls_srtp_protection_profiles returned %d\n\n", |
| 2299 | ret); |
Ron Eldor | 6ea6451 | 2018-07-02 10:08:07 +0300 | [diff] [blame] | 2300 | goto exit; |
| 2301 | } |
| 2302 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2303 | mbedtls_ssl_conf_srtp_mki_value_supported( |
| 2304 | &conf, opt.support_mki ? MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED : |
| 2305 | MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED); |
Ron Eldor | 12c6ead | 2018-07-03 15:08:32 +0300 | [diff] [blame] | 2306 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2307 | } else if (opt.force_srtp_profile != 0) { |
| 2308 | mbedtls_printf( |
| 2309 | " failed\n ! must enable use_srtp to force srtp profile\n\n"); |
Ron Eldor | 6ea6451 | 2018-07-02 10:08:07 +0300 | [diff] [blame] | 2310 | goto exit; |
| 2311 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2312 | # endif /* MBEDTLS_SSL_DTLS_SRTP */ |
Ron Eldor | 6ea6451 | 2018-07-02 10:08:07 +0300 | [diff] [blame] | 2313 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2314 | # if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 2315 | if (opt.extended_ms != DFL_EXTENDED_MS) |
| 2316 | mbedtls_ssl_conf_extended_master_secret(&conf, opt.extended_ms); |
| 2317 | # endif |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2318 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2319 | # if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 2320 | if (opt.etm != DFL_ETM) |
| 2321 | mbedtls_ssl_conf_encrypt_then_mac(&conf, opt.etm); |
| 2322 | # endif |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2323 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2324 | # if defined(MBEDTLS_SSL_ALPN) |
| 2325 | if (opt.alpn_string != NULL) |
| 2326 | if ((ret = mbedtls_ssl_conf_alpn_protocols(&conf, alpn_list)) != 0) { |
| 2327 | mbedtls_printf( |
| 2328 | " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n", |
| 2329 | ret); |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 2330 | goto exit; |
| 2331 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2332 | # endif |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 2333 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2334 | if (opt.reproducible) { |
| 2335 | # if defined(MBEDTLS_HAVE_TIME) |
| 2336 | # if defined(MBEDTLS_PLATFORM_TIME_ALT) |
| 2337 | mbedtls_platform_set_time(dummy_constant_time); |
| 2338 | # else |
| 2339 | fprintf(stderr, |
| 2340 | "Warning: reproducible option used without constant time\n"); |
| 2341 | # endif |
| 2342 | # endif /* MBEDTLS_HAVE_TIME */ |
Philippe Antoine | 986b6f2 | 2019-06-07 15:04:32 +0200 | [diff] [blame] | 2343 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2344 | mbedtls_ssl_conf_rng(&conf, rng_get, &rng); |
| 2345 | mbedtls_ssl_conf_dbg(&conf, my_debug, stdout); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2346 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2347 | # if defined(MBEDTLS_SSL_CACHE_C) |
| 2348 | if (opt.cache_max != -1) |
| 2349 | mbedtls_ssl_cache_set_max_entries(&cache, opt.cache_max); |
Manuel Pégourié-Gonnard | 4c88345 | 2014-02-20 21:32:41 +0100 | [diff] [blame] | 2350 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2351 | if (opt.cache_timeout != -1) |
| 2352 | mbedtls_ssl_cache_set_timeout(&cache, opt.cache_timeout); |
Manuel Pégourié-Gonnard | c55a5b7 | 2014-02-20 22:50:56 +0100 | [diff] [blame] | 2353 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2354 | mbedtls_ssl_conf_session_cache(&conf, &cache, mbedtls_ssl_cache_get, |
| 2355 | mbedtls_ssl_cache_set); |
| 2356 | # endif |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2357 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2358 | # if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 2359 | if (opt.tickets == MBEDTLS_SSL_SESSION_TICKETS_ENABLED) { |
| 2360 | if ((ret = mbedtls_ssl_ticket_setup(&ticket_ctx, rng_get, &rng, |
| 2361 | MBEDTLS_CIPHER_AES_256_GCM, |
| 2362 | opt.ticket_timeout)) != 0) { |
| 2363 | mbedtls_printf( |
| 2364 | " failed\n ! mbedtls_ssl_ticket_setup returned %d\n\n", ret); |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 2365 | goto exit; |
| 2366 | } |
Manuel Pégourié-Gonnard | dbe1ee1 | 2014-02-21 09:18:13 +0100 | [diff] [blame] | 2367 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2368 | mbedtls_ssl_conf_session_tickets_cb(&conf, mbedtls_ssl_ticket_write, |
| 2369 | mbedtls_ssl_ticket_parse, |
| 2370 | &ticket_ctx); |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 2371 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2372 | # endif |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 2373 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2374 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2375 | if (opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 2376 | # if defined(MBEDTLS_SSL_COOKIE_C) |
| 2377 | if (opt.cookies > 0) { |
| 2378 | if ((ret = mbedtls_ssl_cookie_setup(&cookie_ctx, rng_get, &rng)) != |
| 2379 | 0) { |
| 2380 | mbedtls_printf( |
| 2381 | " failed\n ! mbedtls_ssl_cookie_setup returned %d\n\n", |
| 2382 | ret); |
Manuel Pégourié-Gonnard | 26820e3 | 2014-07-23 19:34:59 +0200 | [diff] [blame] | 2383 | goto exit; |
| 2384 | } |
Manuel Pégourié-Gonnard | d485d19 | 2014-07-23 14:56:15 +0200 | [diff] [blame] | 2385 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2386 | mbedtls_ssl_conf_dtls_cookies(&conf, mbedtls_ssl_cookie_write, |
| 2387 | mbedtls_ssl_cookie_check, |
| 2388 | &cookie_ctx); |
| 2389 | } else |
| 2390 | # endif /* MBEDTLS_SSL_COOKIE_C */ |
| 2391 | # if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) |
| 2392 | if (opt.cookies == 0) { |
| 2393 | mbedtls_ssl_conf_dtls_cookies(&conf, NULL, NULL, NULL); |
| 2394 | } else |
| 2395 | # endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */ |
Manuel Pégourié-Gonnard | 26820e3 | 2014-07-23 19:34:59 +0200 | [diff] [blame] | 2396 | { |
| 2397 | ; /* Nothing to do */ |
| 2398 | } |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 2399 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2400 | # if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 2401 | if (opt.anti_replay != DFL_ANTI_REPLAY) |
| 2402 | mbedtls_ssl_conf_dtls_anti_replay(&conf, opt.anti_replay); |
| 2403 | # endif |
Manuel Pégourié-Gonnard | e698f59 | 2014-10-14 19:36:36 +0200 | [diff] [blame] | 2404 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2405 | if (opt.badmac_limit != DFL_BADMAC_LIMIT) |
| 2406 | mbedtls_ssl_conf_dtls_badmac_limit(&conf, opt.badmac_limit); |
Manuel Pégourié-Gonnard | 98545f1 | 2014-07-22 22:10:43 +0200 | [diff] [blame] | 2407 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2408 | # endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 98545f1 | 2014-07-22 22:10:43 +0200 | [diff] [blame] | 2409 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2410 | if (opt.force_ciphersuite[0] != DFL_FORCE_CIPHER) |
| 2411 | mbedtls_ssl_conf_ciphersuites(&conf, opt.force_ciphersuite); |
Manuel Pégourié-Gonnard | d42b7c8 | 2015-03-20 19:44:04 +0000 | [diff] [blame] | 2412 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2413 | if (opt.allow_legacy != DFL_ALLOW_LEGACY) |
| 2414 | mbedtls_ssl_conf_legacy_renegotiation(&conf, opt.allow_legacy); |
| 2415 | # if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2416 | mbedtls_ssl_conf_renegotiation(&conf, opt.renegotiation); |
Manuel Pégourié-Gonnard | 590f416 | 2014-11-05 14:23:03 +0100 | [diff] [blame] | 2417 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2418 | if (opt.renego_delay != DFL_RENEGO_DELAY) |
| 2419 | mbedtls_ssl_conf_renegotiation_enforced(&conf, opt.renego_delay); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2420 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2421 | if (opt.renego_period != DFL_RENEGO_PERIOD) { |
| 2422 | PUT_UINT64_BE(renego_period, opt.renego_period, 0); |
| 2423 | mbedtls_ssl_conf_renegotiation_period(&conf, renego_period); |
Manuel Pégourié-Gonnard | 590f416 | 2014-11-05 14:23:03 +0100 | [diff] [blame] | 2424 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2425 | # endif |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2426 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2427 | # if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 2428 | if (strcmp(opt.ca_path, "none") != 0 && strcmp(opt.ca_file, "none") != 0) { |
| 2429 | # if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 2430 | if (opt.ca_callback != 0) |
| 2431 | mbedtls_ssl_conf_ca_cb(&conf, ca_callback, &cacert); |
Jarno Lamsa | 1b4a2ba | 2019-03-27 17:55:27 +0200 | [diff] [blame] | 2432 | else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2433 | # endif |
| 2434 | mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL); |
Manuel Pégourié-Gonnard | 3e1b178 | 2014-02-27 13:35:00 +0100 | [diff] [blame] | 2435 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2436 | if (key_cert_init) { |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 2437 | mbedtls_pk_context *pk = &pkey; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2438 | # if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 2439 | if (opt.async_private_delay1 >= 0) { |
| 2440 | ret = ssl_async_set_key(&ssl_async_keys, &srvcert, pk, 0, |
| 2441 | opt.async_private_delay1); |
| 2442 | if (ret < 0) { |
| 2443 | mbedtls_printf(" Test error: ssl_async_set_key failed (%d)\n", |
| 2444 | ret); |
Gilles Peskine | d6fbfde | 2018-04-30 10:23:56 +0200 | [diff] [blame] | 2445 | goto exit; |
| 2446 | } |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 2447 | pk = NULL; |
| 2448 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2449 | # endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 2450 | if ((ret = mbedtls_ssl_conf_own_cert(&conf, &srvcert, pk)) != 0) { |
| 2451 | mbedtls_printf( |
| 2452 | " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret); |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 2453 | goto exit; |
| 2454 | } |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 2455 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2456 | if (key_cert_init2) { |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 2457 | mbedtls_pk_context *pk = &pkey2; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2458 | # if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 2459 | if (opt.async_private_delay2 >= 0) { |
| 2460 | ret = ssl_async_set_key(&ssl_async_keys, &srvcert2, pk, 0, |
| 2461 | opt.async_private_delay2); |
| 2462 | if (ret < 0) { |
| 2463 | mbedtls_printf(" Test error: ssl_async_set_key failed (%d)\n", |
| 2464 | ret); |
Gilles Peskine | d6fbfde | 2018-04-30 10:23:56 +0200 | [diff] [blame] | 2465 | goto exit; |
| 2466 | } |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 2467 | pk = NULL; |
| 2468 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2469 | # endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 2470 | if ((ret = mbedtls_ssl_conf_own_cert(&conf, &srvcert2, pk)) != 0) { |
| 2471 | mbedtls_printf( |
| 2472 | " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret); |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 2473 | goto exit; |
| 2474 | } |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 2475 | } |
| 2476 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2477 | # if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 2478 | if (opt.async_operations[0] != '-') { |
Gilles Peskine | fcca9d8 | 2018-01-12 13:47:48 +0100 | [diff] [blame] | 2479 | mbedtls_ssl_async_sign_t *sign = NULL; |
| 2480 | mbedtls_ssl_async_decrypt_t *decrypt = NULL; |
Gilles Peskine | 12ab5d4 | 2018-04-24 12:32:04 +0200 | [diff] [blame] | 2481 | const char *r; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2482 | for (r = opt.async_operations; *r; r++) { |
| 2483 | switch (*r) { |
| 2484 | case 'd': |
| 2485 | decrypt = ssl_async_decrypt; |
| 2486 | break; |
| 2487 | case 's': |
| 2488 | sign = ssl_async_sign; |
| 2489 | break; |
Gilles Peskine | fcca9d8 | 2018-01-12 13:47:48 +0100 | [diff] [blame] | 2490 | } |
| 2491 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2492 | ssl_async_keys.inject_error = (opt.async_private_error < 0 ? |
| 2493 | -opt.async_private_error : |
| 2494 | opt.async_private_error); |
Gilles Peskine | 535fb37 | 2021-01-13 18:59:46 +0100 | [diff] [blame] | 2495 | ssl_async_keys.f_rng = rng_get; |
| 2496 | ssl_async_keys.p_rng = &rng; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2497 | mbedtls_ssl_conf_async_private_cb(&conf, sign, decrypt, |
| 2498 | ssl_async_resume, ssl_async_cancel, |
| 2499 | &ssl_async_keys); |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 2500 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2501 | # endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 2502 | # endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 2503 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2504 | # if defined(SNI_OPTION) |
| 2505 | if (opt.sni != NULL) { |
| 2506 | mbedtls_ssl_conf_sni(&conf, sni_callback, sni_info); |
| 2507 | # if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 2508 | if (opt.async_private_delay2 >= 0) { |
Gilles Peskine | e247989 | 2018-06-13 18:06:51 +0200 | [diff] [blame] | 2509 | sni_entry *cur; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2510 | for (cur = sni_info; cur != NULL; cur = cur->next) { |
| 2511 | ret = ssl_async_set_key(&ssl_async_keys, cur->cert, cur->key, 1, |
| 2512 | opt.async_private_delay2); |
| 2513 | if (ret < 0) { |
| 2514 | mbedtls_printf( |
| 2515 | " Test error: ssl_async_set_key failed (%d)\n", ret); |
Gilles Peskine | e247989 | 2018-06-13 18:06:51 +0200 | [diff] [blame] | 2516 | goto exit; |
| 2517 | } |
| 2518 | cur->key = NULL; |
Gilles Peskine | 166ce74 | 2018-04-30 10:30:49 +0200 | [diff] [blame] | 2519 | } |
Gilles Peskine | 166ce74 | 2018-04-30 10:30:49 +0200 | [diff] [blame] | 2520 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2521 | # endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 166ce74 | 2018-04-30 10:30:49 +0200 | [diff] [blame] | 2522 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2523 | # endif |
Manuel Pégourié-Gonnard | 5d917ff | 2014-02-21 16:52:06 +0100 | [diff] [blame] | 2524 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2525 | # if defined(MBEDTLS_ECP_C) |
| 2526 | if (opt.curves != NULL && strcmp(opt.curves, "default") != 0) { |
| 2527 | mbedtls_ssl_conf_curves(&conf, curve_list); |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 2528 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2529 | # endif |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 2530 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2531 | # if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Hanno Becker | b64ba5f | 2018-10-26 11:28:08 +0100 | [diff] [blame] | 2532 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2533 | if (strlen(opt.psk) != 0 && strlen(opt.psk_identity) != 0) { |
| 2534 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2535 | if (opt.psk_opaque != 0) { |
Hanno Becker | 1d911cd | 2018-11-15 13:06:09 +0000 | [diff] [blame] | 2536 | /* The algorithm has already been determined earlier. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2537 | status = psa_setup_psk_key_slot(&psk_slot, alg, psk, psk_len); |
| 2538 | if (status != PSA_SUCCESS) { |
| 2539 | fprintf(stderr, "SETUP FAIL\n"); |
Hanno Becker | 1d911cd | 2018-11-15 13:06:09 +0000 | [diff] [blame] | 2540 | ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 2541 | goto exit; |
| 2542 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2543 | if ((ret = mbedtls_ssl_conf_psk_opaque( |
| 2544 | &conf, psk_slot, (const unsigned char *)opt.psk_identity, |
| 2545 | strlen(opt.psk_identity))) != 0) { |
| 2546 | mbedtls_printf( |
| 2547 | " failed\n ! mbedtls_ssl_conf_psk_opaque returned %d\n\n", |
| 2548 | ret); |
Hanno Becker | b64ba5f | 2018-10-26 11:28:08 +0100 | [diff] [blame] | 2549 | goto exit; |
| 2550 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2551 | } else |
| 2552 | # endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2553 | if (psk_len > 0) { |
| 2554 | ret = mbedtls_ssl_conf_psk(&conf, psk, psk_len, |
| 2555 | (const unsigned char *)opt.psk_identity, |
| 2556 | strlen(opt.psk_identity)); |
| 2557 | if (ret != 0) { |
| 2558 | mbedtls_printf( |
| 2559 | " failed\n mbedtls_ssl_conf_psk returned -0x%04X\n\n", |
| 2560 | (unsigned int)-ret); |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 2561 | goto exit; |
| 2562 | } |
Manuel Pégourié-Gonnard | dc019b9 | 2014-06-10 15:24:51 +0200 | [diff] [blame] | 2563 | } |
| 2564 | } |
| 2565 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2566 | if (opt.psk_list != NULL) { |
| 2567 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2568 | if (opt.psk_list_opaque != 0) { |
Hanno Becker | b64ba5f | 2018-10-26 11:28:08 +0100 | [diff] [blame] | 2569 | psk_entry *cur_psk; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2570 | for (cur_psk = psk_info; cur_psk != NULL; cur_psk = cur_psk->next) { |
| 2571 | status = psa_setup_psk_key_slot(&cur_psk->slot, alg, |
| 2572 | cur_psk->key, cur_psk->key_len); |
| 2573 | if (status != PSA_SUCCESS) { |
Hanno Becker | b64ba5f | 2018-10-26 11:28:08 +0100 | [diff] [blame] | 2574 | ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 2575 | goto exit; |
| 2576 | } |
| 2577 | } |
| 2578 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2579 | # endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | 1d911cd | 2018-11-15 13:06:09 +0000 | [diff] [blame] | 2580 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2581 | mbedtls_ssl_conf_psk_cb(&conf, psk_callback, psk_info); |
Hanno Becker | b64ba5f | 2018-10-26 11:28:08 +0100 | [diff] [blame] | 2582 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2583 | # endif |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2584 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2585 | # if defined(MBEDTLS_DHM_C) |
Paul Bakker | 5d19f86 | 2012-09-28 07:33:00 +0000 | [diff] [blame] | 2586 | /* |
| 2587 | * Use different group than default DHM group |
| 2588 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2589 | # if defined(MBEDTLS_FS_IO) |
| 2590 | if (opt.dhm_file != NULL) |
| 2591 | ret = mbedtls_ssl_conf_dh_param_ctx(&conf, &dhm); |
| 2592 | # endif |
| 2593 | if (ret != 0) { |
| 2594 | mbedtls_printf( |
| 2595 | " failed\n mbedtls_ssl_conf_dh_param returned -0x%04X\n\n", |
| 2596 | (unsigned int)-ret); |
Manuel Pégourié-Gonnard | 736699c | 2014-06-09 11:29:50 +0200 | [diff] [blame] | 2597 | goto exit; |
| 2598 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2599 | # endif |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2600 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2601 | if (opt.min_version != DFL_MIN_VERSION) |
| 2602 | mbedtls_ssl_conf_min_version(&conf, MBEDTLS_SSL_MAJOR_VERSION_3, |
| 2603 | opt.min_version); |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2604 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2605 | if (opt.max_version != DFL_MIN_VERSION) |
| 2606 | mbedtls_ssl_conf_max_version(&conf, MBEDTLS_SSL_MAJOR_VERSION_3, |
| 2607 | opt.max_version); |
Paul Bakker | c1516be | 2013-06-29 16:01:32 +0200 | [diff] [blame] | 2608 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2609 | if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) { |
| 2610 | mbedtls_printf(" failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", |
| 2611 | (unsigned int)-ret); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 2612 | goto exit; |
| 2613 | } |
| 2614 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2615 | # if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 2616 | if (opt.eap_tls != 0) { |
| 2617 | mbedtls_ssl_set_export_keys_cb(&ssl, eap_tls_key_derivation, |
| 2618 | &eap_tls_keying); |
| 2619 | } else if (opt.nss_keylog != 0) { |
| 2620 | mbedtls_ssl_set_export_keys_cb(&ssl, nss_keylog_export, NULL); |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 2621 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2622 | # if defined(MBEDTLS_SSL_DTLS_SRTP) |
| 2623 | else if (opt.use_srtp != 0) { |
| 2624 | mbedtls_ssl_set_export_keys_cb(&ssl, dtls_srtp_key_derivation, |
| 2625 | &dtls_srtp_keying); |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 2626 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2627 | # endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 2628 | # endif /* MBEDTLS_SSL_EXPORT_KEYS */ |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 2629 | |
Hanno Becker | dcc94e6 | 2019-07-03 17:05:43 +0100 | [diff] [blame] | 2630 | io_ctx.ssl = &ssl; |
| 2631 | io_ctx.net = &client_fd; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2632 | mbedtls_ssl_set_bio(&ssl, &io_ctx, send_cb, recv_cb, |
| 2633 | opt.nbio == 0 ? recv_timeout_cb : NULL); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 2634 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2635 | # if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 2636 | if (opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 2637 | if ((ret = mbedtls_ssl_set_cid(&ssl, opt.cid_enabled, cid, cid_len)) != |
| 2638 | 0) { |
| 2639 | mbedtls_printf(" failed\n ! mbedtls_ssl_set_cid returned %d\n\n", |
| 2640 | ret); |
Hanno Becker | a7d2542 | 2019-04-09 17:28:10 +0100 | [diff] [blame] | 2641 | goto exit; |
| 2642 | } |
| 2643 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2644 | # endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | a7d2542 | 2019-04-09 17:28:10 +0100 | [diff] [blame] | 2645 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2646 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2647 | if (opt.dtls_mtu != DFL_DTLS_MTU) |
| 2648 | mbedtls_ssl_set_mtu(&ssl, opt.dtls_mtu); |
| 2649 | # endif |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 2650 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2651 | # if defined(MBEDTLS_TIMING_C) |
| 2652 | mbedtls_ssl_set_timer_cb(&ssl, &timer, mbedtls_timing_set_delay, |
| 2653 | mbedtls_timing_get_delay); |
| 2654 | # endif |
Manuel Pégourié-Gonnard | e3c41ad | 2015-05-13 10:04:32 +0200 | [diff] [blame] | 2655 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2656 | mbedtls_printf(" ok\n"); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2657 | |
| 2658 | reset: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2659 | # if !defined(_WIN32) |
| 2660 | if (received_sigterm) { |
| 2661 | mbedtls_printf(" interrupted by SIGTERM (not in net_accept())\n"); |
| 2662 | if (ret == MBEDTLS_ERR_NET_INVALID_CONTEXT) |
Manuel Pégourié-Gonnard | 4fa619f | 2018-01-22 10:55:10 +0100 | [diff] [blame] | 2663 | ret = 0; |
| 2664 | |
Manuel Pégourié-Gonnard | b6440a4 | 2014-09-20 12:03:00 +0200 | [diff] [blame] | 2665 | goto exit; |
| 2666 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2667 | # endif |
Manuel Pégourié-Gonnard | b6440a4 | 2014-09-20 12:03:00 +0200 | [diff] [blame] | 2668 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2669 | if (ret == MBEDTLS_ERR_SSL_CLIENT_RECONNECT) { |
| 2670 | mbedtls_printf(" ! Client initiated reconnection from same port\n"); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 2671 | goto handshake; |
| 2672 | } |
| 2673 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2674 | # ifdef MBEDTLS_ERROR_C |
| 2675 | if (ret != 0) { |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2676 | char error_buf[100]; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2677 | mbedtls_strerror(ret, error_buf, 100); |
| 2678 | mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2679 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2680 | # endif |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2681 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2682 | mbedtls_net_free(&client_fd); |
Manuel Pégourié-Gonnard | 8a06d9c | 2014-03-23 18:23:41 +0100 | [diff] [blame] | 2683 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2684 | mbedtls_ssl_session_reset(&ssl); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2685 | |
| 2686 | /* |
| 2687 | * 3. Wait until a client connects |
| 2688 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2689 | mbedtls_printf(" . Waiting for a remote connection ..."); |
| 2690 | fflush(stdout); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2691 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2692 | if ((ret = mbedtls_net_accept(&listen_fd, &client_fd, client_ip, |
| 2693 | sizeof(client_ip), &cliip_len)) != 0) { |
| 2694 | # if !defined(_WIN32) |
| 2695 | if (received_sigterm) { |
| 2696 | mbedtls_printf(" interrupted by SIGTERM (in net_accept())\n"); |
| 2697 | if (ret == MBEDTLS_ERR_NET_ACCEPT_FAILED) |
Manuel Pégourié-Gonnard | 4fa619f | 2018-01-22 10:55:10 +0100 | [diff] [blame] | 2698 | ret = 0; |
| 2699 | |
Manuel Pégourié-Gonnard | db49330 | 2014-08-14 15:36:12 +0200 | [diff] [blame] | 2700 | goto exit; |
| 2701 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2702 | # endif |
Manuel Pégourié-Gonnard | db49330 | 2014-08-14 15:36:12 +0200 | [diff] [blame] | 2703 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2704 | mbedtls_printf(" failed\n ! mbedtls_net_accept returned -0x%x\n\n", |
| 2705 | (unsigned int)-ret); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2706 | goto exit; |
| 2707 | } |
| 2708 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2709 | if (opt.nbio > 0) |
| 2710 | ret = mbedtls_net_set_nonblock(&client_fd); |
Manuel Pégourié-Gonnard | 5575316 | 2014-02-26 13:47:08 +0100 | [diff] [blame] | 2711 | else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2712 | ret = mbedtls_net_set_block(&client_fd); |
| 2713 | if (ret != 0) { |
| 2714 | mbedtls_printf(" failed\n ! net_set_(non)block() returned -0x%x\n\n", |
| 2715 | (unsigned int)-ret); |
Manuel Pégourié-Gonnard | 5575316 | 2014-02-26 13:47:08 +0100 | [diff] [blame] | 2716 | goto exit; |
| 2717 | } |
| 2718 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2719 | mbedtls_ssl_conf_read_timeout(&conf, opt.read_timeout); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2720 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2721 | # if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) |
| 2722 | if (opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 2723 | if ((ret = mbedtls_ssl_set_client_transport_id(&ssl, client_ip, |
| 2724 | cliip_len)) != 0) { |
| 2725 | mbedtls_printf( |
| 2726 | " failed\n ! mbedtls_ssl_set_client_transport_id() returned -0x%x\n\n", |
| 2727 | (unsigned int)-ret); |
Manuel Pégourié-Gonnard | 336b824 | 2014-07-22 17:57:43 +0200 | [diff] [blame] | 2728 | goto exit; |
| 2729 | } |
| 2730 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2731 | # endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */ |
Manuel Pégourié-Gonnard | 336b824 | 2014-07-22 17:57:43 +0200 | [diff] [blame] | 2732 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2733 | # if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 2734 | if (opt.ecjpake_pw != DFL_ECJPAKE_PW) { |
| 2735 | if ((ret = mbedtls_ssl_set_hs_ecjpake_password( |
| 2736 | &ssl, (const unsigned char *)opt.ecjpake_pw, |
| 2737 | strlen(opt.ecjpake_pw))) != 0) { |
| 2738 | mbedtls_printf( |
| 2739 | " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n", |
| 2740 | ret); |
Manuel Pégourié-Gonnard | 70905a7 | 2015-09-16 11:08:34 +0200 | [diff] [blame] | 2741 | goto exit; |
| 2742 | } |
| 2743 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2744 | # endif |
Manuel Pégourié-Gonnard | 70905a7 | 2015-09-16 11:08:34 +0200 | [diff] [blame] | 2745 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2746 | mbedtls_printf(" ok\n"); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2747 | |
| 2748 | /* |
| 2749 | * 4. Handshake |
| 2750 | */ |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 2751 | handshake: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2752 | mbedtls_printf(" . Performing the SSL/TLS handshake..."); |
| 2753 | fflush(stdout); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2754 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2755 | while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) { |
| 2756 | # if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 2757 | if (ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS && |
| 2758 | ssl_async_keys.inject_error == SSL_ASYNC_INJECT_ERROR_CANCEL) { |
| 2759 | mbedtls_printf(" cancelling on injected error\n"); |
Gilles Peskine | 60ee4ca | 2018-01-08 11:28:05 +0100 | [diff] [blame] | 2760 | break; |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 2761 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2762 | # endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | b44692f | 2018-04-24 12:18:19 +0200 | [diff] [blame] | 2763 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2764 | if (!mbedtls_status_is_ssl_in_progress(ret)) |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2765 | break; |
| 2766 | |
| 2767 | /* For event-driven IO, wait for socket to become available */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2768 | if (opt.event == 1 /* level triggered IO */) { |
| 2769 | # if defined(MBEDTLS_TIMING_C) |
| 2770 | ret = idle(&client_fd, &timer, ret); |
| 2771 | # else |
| 2772 | ret = idle(&client_fd, ret); |
| 2773 | # endif |
| 2774 | if (ret != 0) |
Hanno Becker | adfa64f | 2018-03-15 11:35:07 +0000 | [diff] [blame] | 2775 | goto reset; |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 2776 | } |
Gilles Peskine | 9eb5e9a | 2018-01-05 21:15:57 +0100 | [diff] [blame] | 2777 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2778 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2779 | if (ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED) { |
| 2780 | mbedtls_printf(" hello verification requested\n"); |
Manuel Pégourié-Gonnard | caecdae | 2014-10-13 19:04:37 +0200 | [diff] [blame] | 2781 | ret = 0; |
| 2782 | goto reset; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2783 | } else if (ret != 0) { |
| 2784 | mbedtls_printf(" failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", |
| 2785 | (unsigned int)-ret); |
Manuel Pégourié-Gonnard | 6ea831d | 2015-06-22 16:50:52 +0200 | [diff] [blame] | 2786 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2787 | # if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 2788 | if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) { |
Manuel Pégourié-Gonnard | 6ea831d | 2015-06-22 16:50:52 +0200 | [diff] [blame] | 2789 | char vrfy_buf[512]; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2790 | flags = mbedtls_ssl_get_verify_result(&ssl); |
Manuel Pégourié-Gonnard | 6ea831d | 2015-06-22 16:50:52 +0200 | [diff] [blame] | 2791 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2792 | x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), " ! ", flags); |
Manuel Pégourié-Gonnard | 6ea831d | 2015-06-22 16:50:52 +0200 | [diff] [blame] | 2793 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2794 | mbedtls_printf("%s\n", vrfy_buf); |
Manuel Pégourié-Gonnard | 6ea831d | 2015-06-22 16:50:52 +0200 | [diff] [blame] | 2795 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2796 | # endif |
Manuel Pégourié-Gonnard | 6ea831d | 2015-06-22 16:50:52 +0200 | [diff] [blame] | 2797 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2798 | # if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 2799 | if (opt.async_private_error < 0) |
Gilles Peskine | 60ee4ca | 2018-01-08 11:28:05 +0100 | [diff] [blame] | 2800 | /* Injected error only the first time round, to test reset */ |
| 2801 | ssl_async_keys.inject_error = SSL_ASYNC_INJECT_ERROR_NONE; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2802 | # endif |
Manuel Pégourié-Gonnard | caecdae | 2014-10-13 19:04:37 +0200 | [diff] [blame] | 2803 | goto reset; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2804 | } else /* ret == 0 */ |
Manuel Pégourié-Gonnard | caecdae | 2014-10-13 19:04:37 +0200 | [diff] [blame] | 2805 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2806 | mbedtls_printf( |
| 2807 | " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n", |
| 2808 | mbedtls_ssl_get_version(&ssl), mbedtls_ssl_get_ciphersuite(&ssl)); |
Manuel Pégourié-Gonnard | caecdae | 2014-10-13 19:04:37 +0200 | [diff] [blame] | 2809 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2810 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2811 | if ((ret = mbedtls_ssl_get_record_expansion(&ssl)) >= 0) |
| 2812 | mbedtls_printf(" [ Record expansion is %d ]\n", ret); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 2813 | else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2814 | mbedtls_printf(" [ Record expansion is unknown ]\n"); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 2815 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2816 | # if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 2817 | mbedtls_printf(" [ Maximum incoming record payload length is %u ]\n", |
| 2818 | (unsigned int)mbedtls_ssl_get_max_in_record_payload(&ssl)); |
| 2819 | mbedtls_printf(" [ Maximum outgoing record payload length is %u ]\n", |
| 2820 | (unsigned int)mbedtls_ssl_get_max_out_record_payload(&ssl)); |
| 2821 | # endif |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2822 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2823 | # if defined(MBEDTLS_SSL_ALPN) |
| 2824 | if (opt.alpn_string != NULL) { |
| 2825 | const char *alp = mbedtls_ssl_get_alpn_protocol(&ssl); |
| 2826 | mbedtls_printf(" [ Application Layer Protocol is %s ]\n", |
| 2827 | alp ? alp : "(none)"); |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 2828 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2829 | # endif |
Manuel Pégourié-Gonnard | 1bd2281 | 2014-04-05 14:34:07 +0200 | [diff] [blame] | 2830 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2831 | # if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2832 | /* |
Ron Eldor | 2a47be5 | 2017-06-20 15:23:23 +0300 | [diff] [blame] | 2833 | * 5. Verify the client certificate |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2834 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2835 | mbedtls_printf(" . Verifying peer X.509 certificate..."); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2836 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2837 | if ((flags = mbedtls_ssl_get_verify_result(&ssl)) != 0) { |
Manuel Pégourié-Gonnard | 89addc4 | 2015-04-20 10:56:18 +0100 | [diff] [blame] | 2838 | char vrfy_buf[512]; |
| 2839 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2840 | mbedtls_printf(" failed\n"); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2841 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2842 | x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), " ! ", flags); |
| 2843 | mbedtls_printf("%s\n", vrfy_buf); |
| 2844 | } else |
| 2845 | mbedtls_printf(" ok\n"); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2846 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2847 | # if !defined(MBEDTLS_X509_REMOVE_INFO) |
| 2848 | if (mbedtls_ssl_get_peer_cert(&ssl) != NULL) { |
Manuel Pégourié-Gonnard | 1c5b9fc | 2015-06-27 14:38:51 +0200 | [diff] [blame] | 2849 | char crt_buf[512]; |
| 2850 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2851 | mbedtls_printf(" . Peer certificate information ...\n"); |
| 2852 | mbedtls_x509_crt_info(crt_buf, sizeof(crt_buf), " ", |
| 2853 | mbedtls_ssl_get_peer_cert(&ssl)); |
| 2854 | mbedtls_printf("%s\n", crt_buf); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2855 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2856 | # endif /* MBEDTLS_X509_REMOVE_INFO */ |
| 2857 | # endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2858 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2859 | # if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 2860 | if (opt.eap_tls != 0) { |
Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 2861 | size_t j = 0; |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 2862 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2863 | if ((ret = mbedtls_ssl_tls_prf( |
| 2864 | eap_tls_keying.tls_prf_type, eap_tls_keying.master_secret, |
| 2865 | sizeof(eap_tls_keying.master_secret), eap_tls_label, |
| 2866 | eap_tls_keying.randbytes, sizeof(eap_tls_keying.randbytes), |
| 2867 | eap_tls_keymaterial, sizeof(eap_tls_keymaterial))) != 0) { |
| 2868 | mbedtls_printf( |
| 2869 | " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n", |
| 2870 | (unsigned int)-ret); |
Ron Eldor | 65d8c26 | 2019-06-04 13:05:36 +0300 | [diff] [blame] | 2871 | goto reset; |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 2872 | } |
| 2873 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2874 | mbedtls_printf(" EAP-TLS key material is:"); |
| 2875 | for (j = 0; j < sizeof(eap_tls_keymaterial); j++) { |
| 2876 | if (j % 8 == 0) |
Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 2877 | mbedtls_printf("\n "); |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2878 | mbedtls_printf("%02x ", eap_tls_keymaterial[j]); |
Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 2879 | } |
| 2880 | mbedtls_printf("\n"); |
| 2881 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2882 | if ((ret = mbedtls_ssl_tls_prf(eap_tls_keying.tls_prf_type, NULL, 0, |
| 2883 | eap_tls_label, eap_tls_keying.randbytes, |
| 2884 | sizeof(eap_tls_keying.randbytes), |
| 2885 | eap_tls_iv, sizeof(eap_tls_iv))) != 0) { |
| 2886 | mbedtls_printf( |
| 2887 | " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n", |
| 2888 | (unsigned int)-ret); |
| 2889 | goto reset; |
| 2890 | } |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 2891 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2892 | mbedtls_printf(" EAP-TLS IV is:"); |
| 2893 | for (j = 0; j < sizeof(eap_tls_iv); j++) { |
| 2894 | if (j % 8 == 0) |
Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 2895 | mbedtls_printf("\n "); |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2896 | mbedtls_printf("%02x ", eap_tls_iv[j]); |
Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 2897 | } |
| 2898 | mbedtls_printf("\n"); |
| 2899 | } |
Ron Eldor | 65b56ef | 2019-09-26 16:40:48 +0300 | [diff] [blame] | 2900 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2901 | # if defined(MBEDTLS_SSL_DTLS_SRTP) |
| 2902 | else if (opt.use_srtp != 0) { |
Ron Eldor | 65b56ef | 2019-09-26 16:40:48 +0300 | [diff] [blame] | 2903 | size_t j = 0; |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2904 | mbedtls_dtls_srtp_info dtls_srtp_negotiation_result; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2905 | mbedtls_ssl_get_dtls_srtp_negotiation_result( |
| 2906 | &ssl, &dtls_srtp_negotiation_result); |
Ron Eldor | 65b56ef | 2019-09-26 16:40:48 +0300 | [diff] [blame] | 2907 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2908 | if (dtls_srtp_negotiation_result.chosen_dtls_srtp_profile == |
| 2909 | MBEDTLS_TLS_SRTP_UNSET) { |
| 2910 | mbedtls_printf(" Unable to negotiate " |
| 2911 | "the use of DTLS-SRTP\n"); |
| 2912 | } else { |
| 2913 | if ((ret = mbedtls_ssl_tls_prf( |
| 2914 | dtls_srtp_keying.tls_prf_type, |
| 2915 | dtls_srtp_keying.master_secret, |
| 2916 | sizeof(dtls_srtp_keying.master_secret), dtls_srtp_label, |
| 2917 | dtls_srtp_keying.randbytes, |
| 2918 | sizeof(dtls_srtp_keying.randbytes), dtls_srtp_key_material, |
| 2919 | sizeof(dtls_srtp_key_material))) != 0) { |
| 2920 | mbedtls_printf( |
| 2921 | " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n", |
| 2922 | (unsigned int)-ret); |
Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 2923 | goto exit; |
| 2924 | } |
Ron Eldor | 65b56ef | 2019-09-26 16:40:48 +0300 | [diff] [blame] | 2925 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2926 | mbedtls_printf(" DTLS-SRTP key material is:"); |
| 2927 | for (j = 0; j < sizeof(dtls_srtp_key_material); j++) { |
| 2928 | if (j % 8 == 0) |
| 2929 | mbedtls_printf("\n "); |
| 2930 | mbedtls_printf("%02x ", dtls_srtp_key_material[j]); |
Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 2931 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2932 | mbedtls_printf("\n"); |
Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 2933 | |
Johan Pascal | 9bc50b0 | 2020-09-24 12:01:13 +0200 | [diff] [blame] | 2934 | /* produce a less readable output used to perform automatic checks |
| 2935 | * - compare client and server output |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2936 | * - interop test with openssl which client produces this kind of |
| 2937 | * output |
Johan Pascal | 9bc50b0 | 2020-09-24 12:01:13 +0200 | [diff] [blame] | 2938 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2939 | mbedtls_printf(" Keying material: "); |
| 2940 | for (j = 0; j < sizeof(dtls_srtp_key_material); j++) { |
| 2941 | mbedtls_printf("%02X", dtls_srtp_key_material[j]); |
Johan Pascal | 9bc50b0 | 2020-09-24 12:01:13 +0200 | [diff] [blame] | 2942 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2943 | mbedtls_printf("\n"); |
Johan Pascal | 20c7db3 | 2020-10-26 22:45:58 +0100 | [diff] [blame] | 2944 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2945 | if (dtls_srtp_negotiation_result.mki_len > 0) { |
| 2946 | mbedtls_printf(" DTLS-SRTP mki value: "); |
| 2947 | for (j = 0; j < dtls_srtp_negotiation_result.mki_len; j++) { |
| 2948 | mbedtls_printf("%02X", |
| 2949 | dtls_srtp_negotiation_result.mki_value[j]); |
Johan Pascal | 20c7db3 | 2020-10-26 22:45:58 +0100 | [diff] [blame] | 2950 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2951 | } else { |
| 2952 | mbedtls_printf(" DTLS-SRTP no mki value negotiated"); |
Johan Pascal | 20c7db3 | 2020-10-26 22:45:58 +0100 | [diff] [blame] | 2953 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2954 | mbedtls_printf("\n"); |
Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 2955 | } |
Ron Eldor | 65b56ef | 2019-09-26 16:40:48 +0300 | [diff] [blame] | 2956 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2957 | # endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 2958 | # endif /* MBEDTLS_SSL_EXPORT_KEYS */ |
Hanno Becker | a7d2542 | 2019-04-09 17:28:10 +0100 | [diff] [blame] | 2959 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2960 | # if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 2961 | ret = report_cid_usage(&ssl, "initial handshake"); |
| 2962 | if (ret != 0) |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 2963 | goto exit; |
| 2964 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2965 | if (opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 2966 | if ((ret = mbedtls_ssl_set_cid(&ssl, opt.cid_enabled_renego, cid_renego, |
| 2967 | cid_renego_len)) != 0) { |
| 2968 | mbedtls_printf(" failed\n ! mbedtls_ssl_set_cid returned %d\n\n", |
| 2969 | ret); |
Hanno Becker | a7d2542 | 2019-04-09 17:28:10 +0100 | [diff] [blame] | 2970 | goto exit; |
| 2971 | } |
Hanno Becker | a7d2542 | 2019-04-09 17:28:10 +0100 | [diff] [blame] | 2972 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2973 | # endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | a7d2542 | 2019-04-09 17:28:10 +0100 | [diff] [blame] | 2974 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2975 | # if defined(MBEDTLS_MEMORY_DEBUG) |
| 2976 | mbedtls_memory_buffer_alloc_cur_get(¤t_heap_memory, &heap_blocks); |
| 2977 | mbedtls_memory_buffer_alloc_max_get(&peak_heap_memory, &heap_blocks); |
| 2978 | mbedtls_printf( |
| 2979 | "Heap memory usage after handshake: %lu bytes. Peak memory usage was %lu\n", |
| 2980 | (unsigned long)current_heap_memory, (unsigned long)peak_heap_memory); |
| 2981 | # endif /* MBEDTLS_MEMORY_DEBUG */ |
Piotr Nowicki | 0937ed2 | 2019-11-26 16:32:40 +0100 | [diff] [blame] | 2982 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2983 | if (opt.exchanges == 0) |
Manuel Pégourié-Gonnard | 6a2bc23 | 2014-10-09 15:33:13 +0200 | [diff] [blame] | 2984 | goto close_notify; |
| 2985 | |
Manuel Pégourié-Gonnard | 6a0017b | 2015-01-22 10:33:29 +0000 | [diff] [blame] | 2986 | exchanges_left = opt.exchanges; |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 2987 | data_exchange: |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2988 | /* |
| 2989 | * 6. Read the HTTP Request |
| 2990 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2991 | mbedtls_printf(" < Read from client:"); |
| 2992 | fflush(stdout); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 2993 | |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 2994 | /* |
| 2995 | * TLS and DTLS need different reading styles (stream vs datagram) |
| 2996 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2997 | if (opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM) { |
| 2998 | do { |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 2999 | int terminated = 0; |
Andrzej Kurek | 30e731d | 2017-10-12 13:50:29 +0200 | [diff] [blame] | 3000 | len = opt.buffer_size - 1; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3001 | memset(buf, 0, opt.buffer_size); |
| 3002 | ret = mbedtls_ssl_read(&ssl, buf, len); |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 3003 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3004 | if (mbedtls_status_is_ssl_in_progress(ret)) { |
| 3005 | if (opt.event == 1 /* level triggered IO */) { |
| 3006 | # if defined(MBEDTLS_TIMING_C) |
| 3007 | idle(&client_fd, &timer, ret); |
| 3008 | # else |
| 3009 | idle(&client_fd, ret); |
| 3010 | # endif |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 3011 | } |
| 3012 | |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 3013 | continue; |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 3014 | } |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 3015 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3016 | if (ret <= 0) { |
| 3017 | switch (ret) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3018 | case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3019 | mbedtls_printf(" connection was closed gracefully\n"); |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 3020 | goto close_notify; |
| 3021 | |
| 3022 | case 0: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3023 | case MBEDTLS_ERR_NET_CONN_RESET: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3024 | mbedtls_printf(" connection was reset by peer\n"); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3025 | ret = MBEDTLS_ERR_NET_CONN_RESET; |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 3026 | goto reset; |
| 3027 | |
| 3028 | default: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3029 | mbedtls_printf(" mbedtls_ssl_read returned -0x%x\n", |
| 3030 | (unsigned int)-ret); |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 3031 | goto reset; |
| 3032 | } |
| 3033 | } |
| 3034 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3035 | if (mbedtls_ssl_get_bytes_avail(&ssl) == 0) { |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 3036 | len = ret; |
| 3037 | buf[len] = '\0'; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3038 | mbedtls_printf(" %d bytes read\n\n%s\n", len, (char *)buf); |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 3039 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3040 | /* End of message should be detected according to the syntax of |
| 3041 | * the application protocol (eg HTTP), just use a dummy test |
| 3042 | * here. */ |
| 3043 | if (buf[len - 1] == '\n') |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 3044 | terminated = 1; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3045 | } else { |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 3046 | int extra_len, ori_len; |
| 3047 | unsigned char *larger_buf; |
| 3048 | |
| 3049 | ori_len = ret; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3050 | extra_len = (int)mbedtls_ssl_get_bytes_avail(&ssl); |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 3051 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3052 | larger_buf = mbedtls_calloc(1, ori_len + extra_len + 1); |
| 3053 | if (larger_buf == NULL) { |
| 3054 | mbedtls_printf(" ! memory allocation failed\n"); |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 3055 | ret = 1; |
| 3056 | goto reset; |
| 3057 | } |
| 3058 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3059 | memset(larger_buf, 0, ori_len + extra_len); |
| 3060 | memcpy(larger_buf, buf, ori_len); |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 3061 | |
| 3062 | /* This read should never fail and get the whole cached data */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3063 | ret = mbedtls_ssl_read(&ssl, larger_buf + ori_len, extra_len); |
| 3064 | if (ret != extra_len || |
| 3065 | mbedtls_ssl_get_bytes_avail(&ssl) != 0) { |
| 3066 | mbedtls_printf( |
| 3067 | " ! mbedtls_ssl_read failed on cached data\n"); |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 3068 | ret = 1; |
| 3069 | goto reset; |
| 3070 | } |
| 3071 | |
| 3072 | larger_buf[ori_len + extra_len] = '\0'; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3073 | mbedtls_printf(" %d bytes read (%d + %d)\n\n%s\n", |
| 3074 | ori_len + extra_len, ori_len, extra_len, |
| 3075 | (char *)larger_buf); |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 3076 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3077 | /* End of message should be detected according to the syntax of |
| 3078 | * the application protocol (eg HTTP), just use a dummy test |
| 3079 | * here. */ |
| 3080 | if (larger_buf[ori_len + extra_len - 1] == '\n') |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 3081 | terminated = 1; |
| 3082 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3083 | mbedtls_free(larger_buf); |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 3084 | } |
| 3085 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3086 | if (terminated) { |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 3087 | ret = 0; |
| 3088 | break; |
| 3089 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3090 | } while (1); |
| 3091 | } else /* Not stream, so datagram */ |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 3092 | { |
Andrzej Kurek | 30e731d | 2017-10-12 13:50:29 +0200 | [diff] [blame] | 3093 | len = opt.buffer_size - 1; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3094 | memset(buf, 0, opt.buffer_size); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 3095 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3096 | do { |
Hanno Becker | ddc3ebb | 2018-03-13 11:39:09 +0000 | [diff] [blame] | 3097 | /* Without the call to `mbedtls_ssl_check_pending`, it might |
| 3098 | * happen that the client sends application data in the same |
| 3099 | * datagram as the Finished message concluding the handshake. |
| 3100 | * In this case, the application data would be ready to be |
| 3101 | * processed while the underlying transport wouldn't signal |
| 3102 | * any further incoming data. |
| 3103 | * |
| 3104 | * See the test 'Event-driven I/O: session-id resume, UDP packing' |
| 3105 | * in tests/ssl-opt.sh. |
| 3106 | */ |
| 3107 | |
| 3108 | /* For event-driven IO, wait for socket to become available */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3109 | if (mbedtls_ssl_check_pending(&ssl) == 0 && |
| 3110 | opt.event == 1 /* level triggered IO */) { |
| 3111 | # if defined(MBEDTLS_TIMING_C) |
| 3112 | idle(&client_fd, &timer, MBEDTLS_ERR_SSL_WANT_READ); |
| 3113 | # else |
| 3114 | idle(&client_fd, MBEDTLS_ERR_SSL_WANT_READ); |
| 3115 | # endif |
Hanno Becker | ddc3ebb | 2018-03-13 11:39:09 +0000 | [diff] [blame] | 3116 | } |
| 3117 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3118 | ret = mbedtls_ssl_read(&ssl, buf, len); |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 3119 | |
Hanno Becker | ddc3ebb | 2018-03-13 11:39:09 +0000 | [diff] [blame] | 3120 | /* Note that even if `mbedtls_ssl_check_pending` returns true, |
| 3121 | * it can happen that the subsequent call to `mbedtls_ssl_read` |
| 3122 | * returns `MBEDTLS_ERR_SSL_WANT_READ`, because the pending messages |
| 3123 | * might be discarded (e.g. because they are retransmissions). */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3124 | } while (mbedtls_status_is_ssl_in_progress(ret)); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 3125 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3126 | if (ret <= 0) { |
| 3127 | switch (ret) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3128 | case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3129 | mbedtls_printf(" connection was closed gracefully\n"); |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 3130 | ret = 0; |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 3131 | goto close_notify; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 3132 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 3133 | default: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3134 | mbedtls_printf(" mbedtls_ssl_read returned -0x%x\n", |
| 3135 | (unsigned int)-ret); |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 3136 | goto reset; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 3137 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 3138 | } |
| 3139 | |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 3140 | len = ret; |
| 3141 | buf[len] = '\0'; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3142 | mbedtls_printf(" %d bytes read\n\n%s", len, (char *)buf); |
Manuel Pégourié-Gonnard | cce220d | 2014-10-06 18:11:43 +0200 | [diff] [blame] | 3143 | ret = 0; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 3144 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 3145 | |
| 3146 | /* |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 3147 | * 7a. Request renegotiation while client is waiting for input from us. |
Manuel Pégourié-Gonnard | a6ace04 | 2014-10-15 12:44:41 +0200 | [diff] [blame] | 3148 | * (only on the first exchange, to be able to test retransmission) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 3149 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3150 | # if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3151 | if (opt.renegotiate && exchanges_left == opt.exchanges) { |
| 3152 | mbedtls_printf(" . Requestion renegotiation..."); |
| 3153 | fflush(stdout); |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 3154 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3155 | while ((ret = mbedtls_ssl_renegotiate(&ssl)) != 0) { |
| 3156 | if (!mbedtls_status_is_ssl_in_progress(ret)) { |
| 3157 | mbedtls_printf( |
| 3158 | " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n", |
| 3159 | ret); |
Manuel Pégourié-Gonnard | 296e3b1 | 2014-08-19 12:59:03 +0200 | [diff] [blame] | 3160 | goto reset; |
| 3161 | } |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 3162 | |
| 3163 | /* For event-driven IO, wait for socket to become available */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3164 | if (opt.event == 1 /* level triggered IO */) { |
| 3165 | # if defined(MBEDTLS_TIMING_C) |
| 3166 | idle(&client_fd, &timer, ret); |
| 3167 | # else |
| 3168 | idle(&client_fd, ret); |
| 3169 | # endif |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 3170 | } |
Manuel Pégourié-Gonnard | 296e3b1 | 2014-08-19 12:59:03 +0200 | [diff] [blame] | 3171 | } |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 3172 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3173 | mbedtls_printf(" ok\n"); |
Manuel Pégourié-Gonnard | 296e3b1 | 2014-08-19 12:59:03 +0200 | [diff] [blame] | 3174 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3175 | # endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 296e3b1 | 2014-08-19 12:59:03 +0200 | [diff] [blame] | 3176 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3177 | # if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3178 | ret = report_cid_usage(&ssl, "after renegotiation"); |
| 3179 | if (ret != 0) |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 3180 | goto exit; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3181 | # endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | b42ec0d | 2019-05-03 17:30:59 +0100 | [diff] [blame] | 3182 | |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 3183 | /* |
| 3184 | * 7. Write the 200 Response |
| 3185 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3186 | mbedtls_printf(" > Write to client:"); |
| 3187 | fflush(stdout); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 3188 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3189 | len = |
| 3190 | sprintf((char *)buf, HTTP_RESPONSE, mbedtls_ssl_get_ciphersuite(&ssl)); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 3191 | |
Andrzej Kurek | 30e731d | 2017-10-12 13:50:29 +0200 | [diff] [blame] | 3192 | /* Add padding to the response to reach opt.response_size in length */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3193 | if (opt.response_size != DFL_RESPONSE_SIZE && len < opt.response_size) { |
| 3194 | memset(buf + len, 'B', opt.response_size - len); |
Andrzej Kurek | 30e731d | 2017-10-12 13:50:29 +0200 | [diff] [blame] | 3195 | len += opt.response_size - len; |
| 3196 | } |
| 3197 | |
| 3198 | /* Truncate if response size is smaller than the "natural" size */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3199 | if (opt.response_size != DFL_RESPONSE_SIZE && len > opt.response_size) { |
Andrzej Kurek | 30e731d | 2017-10-12 13:50:29 +0200 | [diff] [blame] | 3200 | len = opt.response_size; |
| 3201 | |
| 3202 | /* Still end with \r\n unless that's really not possible */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3203 | if (len >= 2) |
| 3204 | buf[len - 2] = '\r'; |
| 3205 | if (len >= 1) |
| 3206 | buf[len - 1] = '\n'; |
Andrzej Kurek | 30e731d | 2017-10-12 13:50:29 +0200 | [diff] [blame] | 3207 | } |
| 3208 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3209 | if (opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM) { |
| 3210 | for (written = 0, frags = 0; written < len; written += ret, frags++) { |
| 3211 | while ((ret = mbedtls_ssl_write(&ssl, buf + written, |
| 3212 | len - written)) <= 0) { |
| 3213 | if (ret == MBEDTLS_ERR_NET_CONN_RESET) { |
| 3214 | mbedtls_printf( |
| 3215 | " failed\n ! peer closed the connection\n\n"); |
Manuel Pégourié-Gonnard | 2d87e41 | 2014-10-13 18:38:36 +0200 | [diff] [blame] | 3216 | goto reset; |
| 3217 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 3218 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3219 | if (!mbedtls_status_is_ssl_in_progress(ret)) { |
| 3220 | mbedtls_printf( |
| 3221 | " failed\n ! mbedtls_ssl_write returned %d\n\n", ret); |
Manuel Pégourié-Gonnard | 2d87e41 | 2014-10-13 18:38:36 +0200 | [diff] [blame] | 3222 | goto reset; |
| 3223 | } |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 3224 | |
| 3225 | /* For event-driven IO, wait for socket to become available */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3226 | if (opt.event == 1 /* level triggered IO */) { |
| 3227 | # if defined(MBEDTLS_TIMING_C) |
| 3228 | idle(&client_fd, &timer, ret); |
| 3229 | # else |
| 3230 | idle(&client_fd, ret); |
| 3231 | # endif |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 3232 | } |
Manuel Pégourié-Gonnard | bd7ce63 | 2013-07-17 15:34:17 +0200 | [diff] [blame] | 3233 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 3234 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3235 | } else /* Not stream, so datagram */ |
Manuel Pégourié-Gonnard | 2d87e41 | 2014-10-13 18:38:36 +0200 | [diff] [blame] | 3236 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3237 | while (1) { |
| 3238 | ret = mbedtls_ssl_write(&ssl, buf, len); |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 3239 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3240 | if (!mbedtls_status_is_ssl_in_progress(ret)) |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 3241 | break; |
| 3242 | |
| 3243 | /* For event-driven IO, wait for socket to become available */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3244 | if (opt.event == 1 /* level triggered IO */) { |
| 3245 | # if defined(MBEDTLS_TIMING_C) |
| 3246 | idle(&client_fd, &timer, ret); |
| 3247 | # else |
| 3248 | idle(&client_fd, ret); |
| 3249 | # endif |
Hanno Becker | 16970d2 | 2017-10-10 15:56:37 +0100 | [diff] [blame] | 3250 | } |
| 3251 | } |
Manuel Pégourié-Gonnard | 2d87e41 | 2014-10-13 18:38:36 +0200 | [diff] [blame] | 3252 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3253 | if (ret < 0) { |
| 3254 | mbedtls_printf(" failed\n ! mbedtls_ssl_write returned %d\n\n", |
| 3255 | ret); |
Manuel Pégourié-Gonnard | 2d87e41 | 2014-10-13 18:38:36 +0200 | [diff] [blame] | 3256 | goto reset; |
| 3257 | } |
| 3258 | |
| 3259 | frags = 1; |
| 3260 | written = ret; |
| 3261 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 3262 | |
Manuel Pégourié-Gonnard | bd7ce63 | 2013-07-17 15:34:17 +0200 | [diff] [blame] | 3263 | buf[written] = '\0'; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3264 | mbedtls_printf(" %d bytes written in %d fragments\n\n%s\n", written, frags, |
| 3265 | (char *)buf); |
Manuel Pégourié-Gonnard | a92ed48 | 2015-01-14 10:46:08 +0100 | [diff] [blame] | 3266 | ret = 0; |
Manuel Pégourié-Gonnard | f3dc2f6 | 2013-10-29 18:17:41 +0100 | [diff] [blame] | 3267 | |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 3268 | /* |
Jarno Lamsa | 5a3a16c | 2019-05-29 15:41:21 +0300 | [diff] [blame] | 3269 | * 7b. Simulate serialize/deserialize and go back to data exchange |
| 3270 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3271 | # if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 3272 | if (opt.serialize != 0) { |
Jarno Lamsa | 15b3a7a | 2019-06-06 15:10:07 +0300 | [diff] [blame] | 3273 | size_t buf_len; |
Jarno Lamsa | 5a3a16c | 2019-05-29 15:41:21 +0300 | [diff] [blame] | 3274 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3275 | mbedtls_printf(" . Serializing live connection..."); |
Jarno Lamsa | 5a3a16c | 2019-05-29 15:41:21 +0300 | [diff] [blame] | 3276 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3277 | ret = mbedtls_ssl_context_save(&ssl, NULL, 0, &buf_len); |
| 3278 | if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) { |
| 3279 | mbedtls_printf(" failed\n ! mbedtls_ssl_context_save returned " |
| 3280 | "-0x%x\n\n", |
| 3281 | (unsigned int)-ret); |
Jarno Lamsa | 5a3a16c | 2019-05-29 15:41:21 +0300 | [diff] [blame] | 3282 | |
| 3283 | goto exit; |
| 3284 | } |
| 3285 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3286 | if ((context_buf = mbedtls_calloc(1, buf_len)) == NULL) { |
| 3287 | mbedtls_printf(" failed\n ! Couldn't allocate buffer for " |
| 3288 | "serialized context"); |
Jarno Lamsa | 5a3a16c | 2019-05-29 15:41:21 +0300 | [diff] [blame] | 3289 | |
| 3290 | goto exit; |
| 3291 | } |
Manuel Pégourié-Gonnard | 3309a67 | 2019-07-15 10:31:11 +0200 | [diff] [blame] | 3292 | context_buf_len = buf_len; |
Jarno Lamsa | 5a3a16c | 2019-05-29 15:41:21 +0300 | [diff] [blame] | 3293 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3294 | if ((ret = mbedtls_ssl_context_save(&ssl, context_buf, buf_len, |
| 3295 | &buf_len)) != 0) { |
| 3296 | mbedtls_printf(" failed\n ! mbedtls_ssl_context_save returned " |
| 3297 | "-0x%x\n\n", |
| 3298 | (unsigned int)-ret); |
Jarno Lamsa | 5a3a16c | 2019-05-29 15:41:21 +0300 | [diff] [blame] | 3299 | |
| 3300 | goto exit; |
| 3301 | } |
| 3302 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3303 | mbedtls_printf(" ok\n"); |
Manuel Pégourié-Gonnard | a88399c | 2019-07-12 10:41:55 +0200 | [diff] [blame] | 3304 | |
Piotr Nowicki | 3de298f | 2020-04-16 14:35:19 +0200 | [diff] [blame] | 3305 | /* Save serialized context to the 'opt.context_file' as a base64 code */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3306 | if (0 < strlen(opt.context_file)) { |
Piotr Nowicki | 3de298f | 2020-04-16 14:35:19 +0200 | [diff] [blame] | 3307 | FILE *b64_file; |
| 3308 | uint8_t *b64_buf; |
| 3309 | size_t b64_len; |
| 3310 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3311 | mbedtls_printf(" . Save serialized context to a file... "); |
Piotr Nowicki | 3de298f | 2020-04-16 14:35:19 +0200 | [diff] [blame] | 3312 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3313 | mbedtls_base64_encode(NULL, 0, &b64_len, context_buf, buf_len); |
Piotr Nowicki | 3de298f | 2020-04-16 14:35:19 +0200 | [diff] [blame] | 3314 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3315 | if ((b64_buf = mbedtls_calloc(1, b64_len)) == NULL) { |
| 3316 | mbedtls_printf("failed\n ! Couldn't allocate buffer for " |
| 3317 | "the base64 code\n"); |
Piotr Nowicki | 3de298f | 2020-04-16 14:35:19 +0200 | [diff] [blame] | 3318 | goto exit; |
| 3319 | } |
| 3320 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3321 | if ((ret = mbedtls_base64_encode(b64_buf, b64_len, &b64_len, |
| 3322 | context_buf, buf_len)) != 0) { |
| 3323 | mbedtls_printf("failed\n ! mbedtls_base64_encode returned " |
| 3324 | "-0x%x\n", |
| 3325 | (unsigned int)-ret); |
| 3326 | mbedtls_free(b64_buf); |
Piotr Nowicki | 3de298f | 2020-04-16 14:35:19 +0200 | [diff] [blame] | 3327 | goto exit; |
| 3328 | } |
| 3329 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3330 | if ((b64_file = fopen(opt.context_file, "w")) == NULL) { |
| 3331 | mbedtls_printf("failed\n ! Cannot open '%s' for writing.\n", |
| 3332 | opt.context_file); |
| 3333 | mbedtls_free(b64_buf); |
Piotr Nowicki | 3de298f | 2020-04-16 14:35:19 +0200 | [diff] [blame] | 3334 | goto exit; |
| 3335 | } |
| 3336 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3337 | if (b64_len != fwrite(b64_buf, 1, b64_len, b64_file)) { |
| 3338 | mbedtls_printf("failed\n ! fwrite(%ld bytes) failed\n", |
| 3339 | (long)b64_len); |
| 3340 | mbedtls_free(b64_buf); |
| 3341 | fclose(b64_file); |
Piotr Nowicki | 3de298f | 2020-04-16 14:35:19 +0200 | [diff] [blame] | 3342 | goto exit; |
| 3343 | } |
| 3344 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3345 | mbedtls_free(b64_buf); |
| 3346 | fclose(b64_file); |
Piotr Nowicki | 3de298f | 2020-04-16 14:35:19 +0200 | [diff] [blame] | 3347 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3348 | mbedtls_printf("ok\n"); |
Piotr Nowicki | 3de298f | 2020-04-16 14:35:19 +0200 | [diff] [blame] | 3349 | } |
| 3350 | |
Manuel Pégourié-Gonnard | a88399c | 2019-07-12 10:41:55 +0200 | [diff] [blame] | 3351 | /* |
| 3352 | * This simulates a workflow where you have a long-lived server |
| 3353 | * instance, potentially with a pool of ssl_context objects, and you |
| 3354 | * just want to re-use one while the connection is inactive: in that |
| 3355 | * case you can just reset() it, and then it's ready to receive |
| 3356 | * serialized data from another connection (or the same here). |
| 3357 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3358 | if (opt.serialize == 1) { |
Manuel Pégourié-Gonnard | 9df5a82 | 2019-07-23 14:51:09 +0200 | [diff] [blame] | 3359 | /* nothing to do here, done by context_save() already */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3360 | mbedtls_printf(" . Context has been reset... ok\n"); |
Manuel Pégourié-Gonnard | a88399c | 2019-07-12 10:41:55 +0200 | [diff] [blame] | 3361 | } |
| 3362 | |
| 3363 | /* |
| 3364 | * This simulates a workflow where you have one server instance per |
| 3365 | * connection, and want to release it entire when the connection is |
| 3366 | * inactive, and spawn it again when needed again - this would happen |
| 3367 | * between ssl_free() and ssl_init() below, together with any other |
| 3368 | * teardown/startup code needed - for example, preparing the |
| 3369 | * ssl_config again (see section 3 "setup stuff" in this file). |
| 3370 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3371 | if (opt.serialize == 2) { |
| 3372 | mbedtls_printf(" . Freeing and reinitializing context..."); |
Manuel Pégourié-Gonnard | a88399c | 2019-07-12 10:41:55 +0200 | [diff] [blame] | 3373 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3374 | mbedtls_ssl_free(&ssl); |
Jarno Lamsa | 304d61c | 2019-06-06 10:40:52 +0300 | [diff] [blame] | 3375 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3376 | mbedtls_ssl_init(&ssl); |
Jarno Lamsa | 304d61c | 2019-06-06 10:40:52 +0300 | [diff] [blame] | 3377 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3378 | if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) { |
| 3379 | mbedtls_printf(" failed\n ! mbedtls_ssl_setup returned " |
| 3380 | "-0x%x\n\n", |
| 3381 | (unsigned int)-ret); |
Jarno Lamsa | 304d61c | 2019-06-06 10:40:52 +0300 | [diff] [blame] | 3382 | goto exit; |
| 3383 | } |
| 3384 | |
Manuel Pégourié-Gonnard | a88399c | 2019-07-12 10:41:55 +0200 | [diff] [blame] | 3385 | /* |
| 3386 | * This illustrates the minimum amount of things you need to set |
| 3387 | * up, however you could set up much more if desired, for example |
| 3388 | * if you want to share your set up code between the case of |
| 3389 | * establishing a new connection and this case. |
| 3390 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3391 | if (opt.nbio == 2) |
| 3392 | mbedtls_ssl_set_bio(&ssl, &client_fd, delayed_send, |
| 3393 | delayed_recv, NULL); |
Jarno Lamsa | 304d61c | 2019-06-06 10:40:52 +0300 | [diff] [blame] | 3394 | else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3395 | mbedtls_ssl_set_bio( |
| 3396 | &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, |
| 3397 | opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL); |
Jarno Lamsa | 304d61c | 2019-06-06 10:40:52 +0300 | [diff] [blame] | 3398 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3399 | # if defined(MBEDTLS_TIMING_C) |
| 3400 | mbedtls_ssl_set_timer_cb(&ssl, &timer, mbedtls_timing_set_delay, |
| 3401 | mbedtls_timing_get_delay); |
| 3402 | # endif /* MBEDTLS_TIMING_C */ |
Manuel Pégourié-Gonnard | a88399c | 2019-07-12 10:41:55 +0200 | [diff] [blame] | 3403 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3404 | mbedtls_printf(" ok\n"); |
Jarno Lamsa | 304d61c | 2019-06-06 10:40:52 +0300 | [diff] [blame] | 3405 | } |
| 3406 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3407 | mbedtls_printf(" . Deserializing connection..."); |
Jarno Lamsa | 5a3a16c | 2019-05-29 15:41:21 +0300 | [diff] [blame] | 3408 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3409 | if ((ret = mbedtls_ssl_context_load(&ssl, context_buf, buf_len)) != 0) { |
| 3410 | mbedtls_printf("failed\n ! mbedtls_ssl_context_load returned " |
| 3411 | "-0x%x\n\n", |
| 3412 | (unsigned int)-ret); |
Jarno Lamsa | 5a3a16c | 2019-05-29 15:41:21 +0300 | [diff] [blame] | 3413 | |
| 3414 | goto exit; |
| 3415 | } |
Manuel Pégourié-Gonnard | a88399c | 2019-07-12 10:41:55 +0200 | [diff] [blame] | 3416 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3417 | mbedtls_free(context_buf); |
Manuel Pégourié-Gonnard | 3309a67 | 2019-07-15 10:31:11 +0200 | [diff] [blame] | 3418 | context_buf = NULL; |
| 3419 | context_buf_len = 0; |
| 3420 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3421 | mbedtls_printf(" ok\n"); |
Jarno Lamsa | 5a3a16c | 2019-05-29 15:41:21 +0300 | [diff] [blame] | 3422 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3423 | # endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Jarno Lamsa | 5a3a16c | 2019-05-29 15:41:21 +0300 | [diff] [blame] | 3424 | |
| 3425 | /* |
| 3426 | * 7c. Continue doing data exchanges? |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 3427 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3428 | if (--exchanges_left > 0) |
Manuel Pégourié-Gonnard | a8c0a0d | 2014-08-15 12:07:38 +0200 | [diff] [blame] | 3429 | goto data_exchange; |
| 3430 | |
| 3431 | /* |
| 3432 | * 8. Done, cleanly close the connection |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 3433 | */ |
| 3434 | close_notify: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3435 | mbedtls_printf(" . Closing the connection..."); |
Manuel Pégourié-Gonnard | 6b0d268 | 2014-03-25 11:24:43 +0100 | [diff] [blame] | 3436 | |
Manuel Pégourié-Gonnard | 994f8b5 | 2014-10-09 19:56:44 +0200 | [diff] [blame] | 3437 | /* No error checking, the connection might be closed already */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3438 | do |
| 3439 | ret = mbedtls_ssl_close_notify(&ssl); |
| 3440 | while (ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
Manuel Pégourié-Gonnard | 994f8b5 | 2014-10-09 19:56:44 +0200 | [diff] [blame] | 3441 | ret = 0; |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 3442 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3443 | mbedtls_printf(" done\n"); |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 3444 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 3445 | goto reset; |
| 3446 | |
Manuel Pégourié-Gonnard | e08660e | 2014-08-16 11:28:40 +0200 | [diff] [blame] | 3447 | /* |
| 3448 | * Cleanup and exit |
| 3449 | */ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 3450 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3451 | # ifdef MBEDTLS_ERROR_C |
| 3452 | if (ret != 0) { |
gabor-mezei-arm | de47217 | 2020-09-04 14:44:25 +0200 | [diff] [blame] | 3453 | char error_buf[100]; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3454 | mbedtls_strerror(ret, error_buf, 100); |
| 3455 | mbedtls_printf("Last error was: -0x%X - %s\n\n", (unsigned int)-ret, |
| 3456 | error_buf); |
gabor-mezei-arm | de47217 | 2020-09-04 14:44:25 +0200 | [diff] [blame] | 3457 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3458 | # endif |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 3459 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3460 | if (opt.query_config_mode == DFL_QUERY_CONFIG_MODE) { |
| 3461 | mbedtls_printf(" . Cleaning up..."); |
| 3462 | fflush(stdout); |
gabor-mezei-arm | a9eecf1 | 2020-07-07 11:11:02 +0200 | [diff] [blame] | 3463 | } |
Manuel Pégourié-Gonnard | f29e5de | 2014-11-21 11:54:41 +0100 | [diff] [blame] | 3464 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3465 | mbedtls_net_free(&client_fd); |
| 3466 | mbedtls_net_free(&listen_fd); |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 3467 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3468 | # if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO) |
| 3469 | mbedtls_dhm_free(&dhm); |
| 3470 | # endif |
| 3471 | # if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3472 | mbedtls_x509_crt_free(&cacert); |
| 3473 | mbedtls_x509_crt_free(&srvcert); |
| 3474 | mbedtls_pk_free(&pkey); |
| 3475 | mbedtls_x509_crt_free(&srvcert2); |
| 3476 | mbedtls_pk_free(&pkey2); |
| 3477 | # endif |
| 3478 | # if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 3479 | for (i = 0; (size_t)i < ssl_async_keys.slots_used; i++) { |
| 3480 | if (ssl_async_keys.slots[i].pk_owned) { |
| 3481 | mbedtls_pk_free(ssl_async_keys.slots[i].pk); |
| 3482 | mbedtls_free(ssl_async_keys.slots[i].pk); |
Gilles Peskine | 4481744 | 2018-06-13 18:09:28 +0200 | [diff] [blame] | 3483 | ssl_async_keys.slots[i].pk = NULL; |
| 3484 | } |
| 3485 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3486 | # endif |
| 3487 | # if defined(SNI_OPTION) |
| 3488 | sni_free(sni_info); |
| 3489 | # endif |
| 3490 | # if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
| 3491 | ret = psk_free(psk_info); |
| 3492 | if ((ret != 0) && (opt.query_config_mode == DFL_QUERY_CONFIG_MODE)) |
| 3493 | mbedtls_printf("Failed to list of opaque PSKs - error was %d\n", ret); |
| 3494 | # endif |
| 3495 | # if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO) |
| 3496 | mbedtls_dhm_free(&dhm); |
| 3497 | # endif |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3498 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3499 | # if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) && \ |
| 3500 | defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3501 | if (opt.psk_opaque != 0) { |
Hanno Becker | c43b6ea | 2018-11-05 13:48:43 +0000 | [diff] [blame] | 3502 | /* This is ok even if the slot hasn't been |
| 3503 | * initialized (we might have jumed here |
| 3504 | * immediately because of bad cmd line params, |
| 3505 | * for example). */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3506 | status = psa_destroy_key(psk_slot); |
| 3507 | if ((status != PSA_SUCCESS) && |
| 3508 | (opt.query_config_mode == DFL_QUERY_CONFIG_MODE)) { |
| 3509 | mbedtls_printf("Failed to destroy key slot %u - error was %d", |
| 3510 | (unsigned)psk_slot, (int)status); |
Hanno Becker | c43b6ea | 2018-11-05 13:48:43 +0000 | [diff] [blame] | 3511 | } |
| 3512 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3513 | # endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED && \ |
| 3514 | MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | c43b6ea | 2018-11-05 13:48:43 +0000 | [diff] [blame] | 3515 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3516 | mbedtls_ssl_free(&ssl); |
| 3517 | mbedtls_ssl_config_free(&conf); |
| 3518 | rng_free(&rng); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 3519 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3520 | # if defined(MBEDTLS_SSL_CACHE_C) |
| 3521 | mbedtls_ssl_cache_free(&cache); |
| 3522 | # endif |
| 3523 | # if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 3524 | mbedtls_ssl_ticket_free(&ticket_ctx); |
| 3525 | # endif |
| 3526 | # if defined(MBEDTLS_SSL_COOKIE_C) |
| 3527 | mbedtls_ssl_cookie_free(&cookie_ctx); |
| 3528 | # endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3529 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3530 | mbedtls_free(buf); |
Hanno Becker | 095d9cf | 2018-10-09 12:39:13 +0100 | [diff] [blame] | 3531 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3532 | # if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 3533 | if (context_buf != NULL) |
| 3534 | mbedtls_platform_zeroize(context_buf, context_buf_len); |
| 3535 | mbedtls_free(context_buf); |
| 3536 | # endif |
Manuel Pégourié-Gonnard | 3309a67 | 2019-07-15 10:31:11 +0200 | [diff] [blame] | 3537 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3538 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3539 | mbedtls_psa_crypto_free(); |
| 3540 | # endif |
Gilles Peskine | 414e717 | 2021-02-03 00:04:08 +0100 | [diff] [blame] | 3541 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3542 | # if defined(MBEDTLS_TEST_HOOKS) |
Gilles Peskine | d0a46e5 | 2021-02-03 00:03:03 +0100 | [diff] [blame] | 3543 | /* Let test hooks detect errors such as resource leaks. |
| 3544 | * Don't do it in query_config mode, because some test code prints |
| 3545 | * information to stdout and this gets mixed with the regular output. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3546 | if (opt.query_config_mode == DFL_QUERY_CONFIG_MODE) { |
| 3547 | if (test_hooks_failure_detected()) { |
| 3548 | if (ret == 0) |
Gilles Peskine | d0a46e5 | 2021-02-03 00:03:03 +0100 | [diff] [blame] | 3549 | ret = 1; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3550 | mbedtls_printf("Test hooks detected errors.\n"); |
Gilles Peskine | d0a46e5 | 2021-02-03 00:03:03 +0100 | [diff] [blame] | 3551 | } |
Gilles Peskine | 53dea74 | 2021-02-02 22:55:06 +0100 | [diff] [blame] | 3552 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3553 | test_hooks_free(); |
| 3554 | # endif /* MBEDTLS_TEST_HOOKS */ |
Gilles Peskine | 53dea74 | 2021-02-02 22:55:06 +0100 | [diff] [blame] | 3555 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3556 | # if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 3557 | # if defined(MBEDTLS_MEMORY_DEBUG) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3558 | mbedtls_memory_buffer_alloc_status(); |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3559 | # endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3560 | mbedtls_memory_buffer_alloc_free(); |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3561 | # endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */ |
Paul Bakker | 82024bf | 2013-07-04 11:52:32 +0200 | [diff] [blame] | 3562 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3563 | if (opt.query_config_mode == DFL_QUERY_CONFIG_MODE) { |
| 3564 | mbedtls_printf(" done.\n"); |
Manuel Pégourié-Gonnard | f29e5de | 2014-11-21 11:54:41 +0100 | [diff] [blame] | 3565 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3566 | # if defined(_WIN32) |
| 3567 | mbedtls_printf(" + Press Enter to exit this program.\n"); |
| 3568 | fflush(stdout); |
| 3569 | getchar(); |
| 3570 | # endif |
gabor-mezei-arm | a9eecf1 | 2020-07-07 11:11:02 +0200 | [diff] [blame] | 3571 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 3572 | |
Paul Bakker | dbd79ca | 2013-07-24 16:28:35 +0200 | [diff] [blame] | 3573 | // Shell can not handle large exit numbers -> 1 for errors |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3574 | if (ret < 0) |
Paul Bakker | dbd79ca | 2013-07-24 16:28:35 +0200 | [diff] [blame] | 3575 | ret = 1; |
| 3576 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3577 | if (opt.query_config_mode == DFL_QUERY_CONFIG_MODE) |
| 3578 | mbedtls_exit(ret); |
gabor-mezei-arm | a9eecf1 | 2020-07-07 11:11:02 +0200 | [diff] [blame] | 3579 | else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3580 | mbedtls_exit(query_config_ret); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 3581 | } |
Gilles Peskine | b4df754 | 2021-01-08 21:20:09 +0100 | [diff] [blame] | 3582 | #endif /* !MBEDTLS_SSL_TEST_IMPOSSIBLE && MBEDTLS_SSL_SRV_C */ |