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