| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* | 
| Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 2 | *  TLS shared functions | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3 | * | 
| Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | *  Copyright The Mbed TLS Contributors | 
| Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 5 | *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6 | */ | 
|  | 7 | /* | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8 | *  http://www.ietf.org/rfc/rfc2246.txt | 
|  | 9 | *  http://www.ietf.org/rfc/rfc4346.txt | 
|  | 10 | */ | 
|  | 11 |  | 
| Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 12 | #include "common.h" | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 13 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 14 | #if defined(MBEDTLS_SSL_TLS_C) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 15 |  | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 16 | #include "mbedtls/platform.h" | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 17 |  | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 18 | #include "mbedtls/ssl.h" | 
| Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 19 | #include "ssl_client.h" | 
| Ronald Cron | 27c85e7 | 2022-03-08 11:37:55 +0100 | [diff] [blame] | 20 | #include "ssl_debug_helpers.h" | 
| Chris Jones | 84a773f | 2021-03-05 18:38:47 +0000 | [diff] [blame] | 21 | #include "ssl_misc.h" | 
| Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 22 |  | 
| Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 23 | #include "mbedtls/debug.h" | 
|  | 24 | #include "mbedtls/error.h" | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 25 | #include "mbedtls/platform_util.h" | 
| Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 26 | #include "mbedtls/version.h" | 
| Gabor Mezei | 765862c | 2021-10-19 12:22:25 +0200 | [diff] [blame] | 27 | #include "mbedtls/constant_time.h" | 
| Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 28 |  | 
| Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 29 | #include <string.h> | 
|  | 30 |  | 
| Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 31 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Manuel Pégourié-Gonnard | 02b10d8 | 2023-03-28 12:33:20 +0200 | [diff] [blame] | 32 | #include "md_psa.h" | 
| Manuel Pégourié-Gonnard | 2be8c63 | 2023-06-07 13:07:21 +0200 | [diff] [blame] | 33 | #include "psa_util_internal.h" | 
| Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 34 | #include "psa/crypto.h" | 
|  | 35 | #endif | 
|  | 36 |  | 
| Janos Follath | 23bdca0 | 2016-10-07 14:47:14 +0100 | [diff] [blame] | 37 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 38 | #include "mbedtls/oid.h" | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 39 | #endif | 
|  | 40 |  | 
| Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 41 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Andrzej Kurek | 0064484 | 2023-05-30 05:45:00 -0400 | [diff] [blame] | 42 | /* Define local translating functions to save code size by not using too many | 
|  | 43 | * arguments in each translating place. */ | 
|  | 44 | static int local_err_translation(psa_status_t status) | 
|  | 45 | { | 
|  | 46 | return psa_status_to_mbedtls(status, psa_to_ssl_errors, | 
| Andrzej Kurek | 1e4a030 | 2023-05-30 09:45:17 -0400 | [diff] [blame] | 47 | ARRAY_LENGTH(psa_to_ssl_errors), | 
| Andrzej Kurek | 0064484 | 2023-05-30 05:45:00 -0400 | [diff] [blame] | 48 | psa_generic_status_to_mbedtls); | 
|  | 49 | } | 
|  | 50 | #define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status) | 
| Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 51 | #endif | 
|  | 52 |  | 
| Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 53 | #if defined(MBEDTLS_TEST_HOOKS) | 
|  | 54 | static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args; | 
|  | 55 |  | 
|  | 56 | void mbedtls_ssl_set_chk_buf_ptr_fail_args( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 57 | const uint8_t *cur, const uint8_t *end, size_t need) | 
| Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 58 | { | 
|  | 59 | chk_buf_ptr_fail_args.cur = cur; | 
|  | 60 | chk_buf_ptr_fail_args.end = end; | 
|  | 61 | chk_buf_ptr_fail_args.need = need; | 
|  | 62 | } | 
|  | 63 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 64 | void mbedtls_ssl_reset_chk_buf_ptr_fail_args(void) | 
| Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 65 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 66 | memset(&chk_buf_ptr_fail_args, 0, sizeof(chk_buf_ptr_fail_args)); | 
| Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 67 | } | 
|  | 68 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 69 | int mbedtls_ssl_cmp_chk_buf_ptr_fail_args(mbedtls_ssl_chk_buf_ptr_args *args) | 
| Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 70 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 71 | return (chk_buf_ptr_fail_args.cur  != args->cur) || | 
|  | 72 | (chk_buf_ptr_fail_args.end  != args->end) || | 
|  | 73 | (chk_buf_ptr_fail_args.need != args->need); | 
| Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 74 | } | 
|  | 75 | #endif /* MBEDTLS_TEST_HOOKS */ | 
|  | 76 |  | 
| Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 77 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 78 |  | 
| Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 79 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
| Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 80 | /* Top-level Connection ID API */ | 
|  | 81 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 82 | int mbedtls_ssl_conf_cid(mbedtls_ssl_config *conf, | 
|  | 83 | size_t len, | 
|  | 84 | int ignore_other_cid) | 
| Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 85 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 86 | if (len > MBEDTLS_SSL_CID_IN_LEN_MAX) { | 
|  | 87 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 88 | } | 
| Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 89 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 90 | if (ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL && | 
|  | 91 | ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) { | 
|  | 92 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
| Hanno Becker | 611ac77 | 2019-05-14 11:45:26 +0100 | [diff] [blame] | 93 | } | 
|  | 94 |  | 
|  | 95 | conf->ignore_unexpected_cid = ignore_other_cid; | 
| Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 96 | conf->cid_len = len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 97 | return 0; | 
| Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 98 | } | 
|  | 99 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 100 | int mbedtls_ssl_set_cid(mbedtls_ssl_context *ssl, | 
|  | 101 | int enable, | 
|  | 102 | unsigned char const *own_cid, | 
|  | 103 | size_t own_cid_len) | 
| Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 104 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 105 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) { | 
|  | 106 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 107 | } | 
| Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 108 |  | 
| Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 109 | ssl->negotiate_cid = enable; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 110 | if (enable == MBEDTLS_SSL_CID_DISABLED) { | 
|  | 111 | MBEDTLS_SSL_DEBUG_MSG(3, ("Disable use of CID extension.")); | 
|  | 112 | return 0; | 
| Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 113 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 114 | MBEDTLS_SSL_DEBUG_MSG(3, ("Enable use of CID extension.")); | 
|  | 115 | MBEDTLS_SSL_DEBUG_BUF(3, "Own CID", own_cid, own_cid_len); | 
| Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 116 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 117 | if (own_cid_len != ssl->conf->cid_len) { | 
|  | 118 | MBEDTLS_SSL_DEBUG_MSG(3, ("CID length %u does not match CID length %u in config", | 
|  | 119 | (unsigned) own_cid_len, | 
|  | 120 | (unsigned) ssl->conf->cid_len)); | 
|  | 121 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
| Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 122 | } | 
|  | 123 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 124 | memcpy(ssl->own_cid, own_cid, own_cid_len); | 
| Hanno Becker | b7ee0cf | 2019-04-30 14:07:31 +0100 | [diff] [blame] | 125 | /* Truncation is not an issue here because | 
|  | 126 | * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */ | 
|  | 127 | ssl->own_cid_len = (uint8_t) own_cid_len; | 
| Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 128 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 129 | return 0; | 
| Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 130 | } | 
|  | 131 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 132 | int mbedtls_ssl_get_own_cid(mbedtls_ssl_context *ssl, | 
|  | 133 | int *enabled, | 
|  | 134 | unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX], | 
|  | 135 | size_t *own_cid_len) | 
| Paul Elliott | 0113cf1 | 2022-03-11 20:26:47 +0000 | [diff] [blame] | 136 | { | 
|  | 137 | *enabled = MBEDTLS_SSL_CID_DISABLED; | 
|  | 138 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 139 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) { | 
|  | 140 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 141 | } | 
| Paul Elliott | 0113cf1 | 2022-03-11 20:26:47 +0000 | [diff] [blame] | 142 |  | 
|  | 143 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is | 
|  | 144 | * zero as this is indistinguishable from not requesting to use | 
|  | 145 | * the CID extension. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 146 | if (ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) { | 
|  | 147 | return 0; | 
|  | 148 | } | 
| Paul Elliott | 0113cf1 | 2022-03-11 20:26:47 +0000 | [diff] [blame] | 149 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 150 | if (own_cid_len != NULL) { | 
| Paul Elliott | 0113cf1 | 2022-03-11 20:26:47 +0000 | [diff] [blame] | 151 | *own_cid_len = ssl->own_cid_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 152 | if (own_cid != NULL) { | 
|  | 153 | memcpy(own_cid, ssl->own_cid, ssl->own_cid_len); | 
|  | 154 | } | 
| Paul Elliott | 0113cf1 | 2022-03-11 20:26:47 +0000 | [diff] [blame] | 155 | } | 
|  | 156 |  | 
|  | 157 | *enabled = MBEDTLS_SSL_CID_ENABLED; | 
|  | 158 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 159 | return 0; | 
| Paul Elliott | 0113cf1 | 2022-03-11 20:26:47 +0000 | [diff] [blame] | 160 | } | 
|  | 161 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 162 | int mbedtls_ssl_get_peer_cid(mbedtls_ssl_context *ssl, | 
|  | 163 | int *enabled, | 
|  | 164 | unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX], | 
|  | 165 | size_t *peer_cid_len) | 
| Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 166 | { | 
| Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 167 | *enabled = MBEDTLS_SSL_CID_DISABLED; | 
| Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 168 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 169 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || | 
|  | 170 | mbedtls_ssl_is_handshake_over(ssl) == 0) { | 
|  | 171 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
| Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 172 | } | 
| Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 173 |  | 
| Hanno Becker | c5f2422 | 2019-05-03 12:54:52 +0100 | [diff] [blame] | 174 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions | 
|  | 175 | * were used, but client and server requested the empty CID. | 
|  | 176 | * This is indistinguishable from not using the CID extension | 
|  | 177 | * in the first place. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 178 | if (ssl->transform_in->in_cid_len  == 0 && | 
|  | 179 | ssl->transform_in->out_cid_len == 0) { | 
|  | 180 | return 0; | 
| Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 181 | } | 
|  | 182 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 183 | if (peer_cid_len != NULL) { | 
| Hanno Becker | 615ef17 | 2019-05-22 16:50:35 +0100 | [diff] [blame] | 184 | *peer_cid_len = ssl->transform_in->out_cid_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 185 | if (peer_cid != NULL) { | 
|  | 186 | memcpy(peer_cid, ssl->transform_in->out_cid, | 
|  | 187 | ssl->transform_in->out_cid_len); | 
| Hanno Becker | 615ef17 | 2019-05-22 16:50:35 +0100 | [diff] [blame] | 188 | } | 
|  | 189 | } | 
| Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 190 |  | 
|  | 191 | *enabled = MBEDTLS_SSL_CID_ENABLED; | 
|  | 192 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 193 | return 0; | 
| Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 194 | } | 
| Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 195 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
| Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 196 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 197 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 198 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 199 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
| Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 200 | /* | 
|  | 201 | * Convert max_fragment_length codes to length. | 
|  | 202 | * RFC 6066 says: | 
|  | 203 | *    enum{ | 
|  | 204 | *        2^9(1), 2^10(2), 2^11(3), 2^12(4), (255) | 
|  | 205 | *    } MaxFragmentLength; | 
|  | 206 | * and we add 0 -> extension unused | 
|  | 207 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 208 | static unsigned int ssl_mfl_code_to_length(int mfl) | 
| Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 209 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 210 | switch (mfl) { | 
|  | 211 | case MBEDTLS_SSL_MAX_FRAG_LEN_NONE: | 
|  | 212 | return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN; | 
|  | 213 | case MBEDTLS_SSL_MAX_FRAG_LEN_512: | 
|  | 214 | return 512; | 
|  | 215 | case MBEDTLS_SSL_MAX_FRAG_LEN_1024: | 
|  | 216 | return 1024; | 
|  | 217 | case MBEDTLS_SSL_MAX_FRAG_LEN_2048: | 
|  | 218 | return 2048; | 
|  | 219 | case MBEDTLS_SSL_MAX_FRAG_LEN_4096: | 
|  | 220 | return 4096; | 
|  | 221 | default: | 
|  | 222 | return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN; | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 223 | } | 
|  | 224 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 225 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ | 
| Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 226 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 227 | int mbedtls_ssl_session_copy(mbedtls_ssl_session *dst, | 
|  | 228 | const mbedtls_ssl_session *src) | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 229 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 230 | mbedtls_ssl_session_free(dst); | 
|  | 231 | memcpy(dst, src, sizeof(mbedtls_ssl_session)); | 
| 吴敬辉 | 0b71611 | 2021-11-29 10:46:35 +0800 | [diff] [blame] | 232 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 233 | dst->ticket = NULL; | 
| Xiaokang Qian | 8730644 | 2022-10-12 09:47:38 +0000 | [diff] [blame] | 234 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ | 
|  | 235 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 236 | dst->hostname = NULL; | 
| 吴敬辉 | 0b71611 | 2021-11-29 10:46:35 +0800 | [diff] [blame] | 237 | #endif | 
| Xiaokang Qian | 8730644 | 2022-10-12 09:47:38 +0000 | [diff] [blame] | 238 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ | 
| 吴敬辉 | 0b71611 | 2021-11-29 10:46:35 +0800 | [diff] [blame] | 239 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 240 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 241 |  | 
|  | 242 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 243 | if (src->peer_cert != NULL) { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 244 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 245 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 246 | dst->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt)); | 
|  | 247 | if (dst->peer_cert == NULL) { | 
|  | 248 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
|  | 249 | } | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 250 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 251 | mbedtls_x509_crt_init(dst->peer_cert); | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 252 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 253 | if ((ret = mbedtls_x509_crt_parse_der(dst->peer_cert, src->peer_cert->raw.p, | 
|  | 254 | src->peer_cert->raw.len)) != 0) { | 
|  | 255 | mbedtls_free(dst->peer_cert); | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 256 | dst->peer_cert = NULL; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 257 | return ret; | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 258 | } | 
|  | 259 | } | 
| Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 260 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 261 | if (src->peer_cert_digest != NULL) { | 
| Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 262 | dst->peer_cert_digest = | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 263 | mbedtls_calloc(1, src->peer_cert_digest_len); | 
|  | 264 | if (dst->peer_cert_digest == NULL) { | 
|  | 265 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
|  | 266 | } | 
| Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 267 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 268 | memcpy(dst->peer_cert_digest, src->peer_cert_digest, | 
|  | 269 | src->peer_cert_digest_len); | 
| Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 270 | dst->peer_cert_digest_type = src->peer_cert_digest_type; | 
| Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 271 | dst->peer_cert_digest_len = src->peer_cert_digest_len; | 
| Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 272 | } | 
|  | 273 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 274 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 275 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 276 |  | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 277 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 278 | if (src->ticket != NULL) { | 
|  | 279 | dst->ticket = mbedtls_calloc(1, src->ticket_len); | 
|  | 280 | if (dst->ticket == NULL) { | 
|  | 281 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
|  | 282 | } | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 283 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 284 | memcpy(dst->ticket, src->ticket, src->ticket_len); | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 285 | } | 
| Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 286 |  | 
|  | 287 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ | 
|  | 288 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 289 | if (src->endpoint == MBEDTLS_SSL_IS_CLIENT) { | 
| Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 290 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 291 | ret = mbedtls_ssl_session_set_hostname(dst, src->hostname); | 
|  | 292 | if (ret != 0) { | 
|  | 293 | return ret; | 
|  | 294 | } | 
| Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 295 | } | 
|  | 296 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 && | 
|  | 297 | MBEDTLS_SSL_SERVER_NAME_INDICATION */ | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 298 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 299 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 300 | return 0; | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 301 | } | 
|  | 302 |  | 
| Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 303 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 304 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 305 | static int resize_buffer(unsigned char **buffer, size_t len_new, size_t *len_old) | 
| Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 306 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 307 | unsigned char *resized_buffer = mbedtls_calloc(1, len_new); | 
|  | 308 | if (resized_buffer == NULL) { | 
| Yanray Wang | 365ee3e | 2023-11-22 10:28:28 +0800 | [diff] [blame] | 309 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 310 | } | 
| Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 311 |  | 
|  | 312 | /* We want to copy len_new bytes when downsizing the buffer, and | 
|  | 313 | * len_old bytes when upsizing, so we choose the smaller of two sizes, | 
|  | 314 | * to fit one buffer into another. Size checks, ensuring that no data is | 
|  | 315 | * lost, are done outside of this function. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 316 | memcpy(resized_buffer, *buffer, | 
|  | 317 | (len_new < *len_old) ? len_new : *len_old); | 
| Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 318 | mbedtls_zeroize_and_free(*buffer, *len_old); | 
| Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 319 |  | 
|  | 320 | *buffer = resized_buffer; | 
|  | 321 | *len_old = len_new; | 
|  | 322 |  | 
|  | 323 | return 0; | 
|  | 324 | } | 
| Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 325 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 326 | static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing, | 
|  | 327 | size_t in_buf_new_len, | 
|  | 328 | size_t out_buf_new_len) | 
| Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 329 | { | 
|  | 330 | int modified = 0; | 
|  | 331 | size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0; | 
|  | 332 | size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 333 | if (ssl->in_buf != NULL) { | 
| Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 334 | written_in = ssl->in_msg - ssl->in_buf; | 
|  | 335 | iv_offset_in = ssl->in_iv - ssl->in_buf; | 
|  | 336 | len_offset_in = ssl->in_len - ssl->in_buf; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 337 | if (downsizing ? | 
| Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 338 | ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len : | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 339 | ssl->in_buf_len < in_buf_new_len) { | 
|  | 340 | if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != 0) { | 
|  | 341 | MBEDTLS_SSL_DEBUG_MSG(1, ("input buffer resizing failed - out of memory")); | 
|  | 342 | } else { | 
|  | 343 | MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET, | 
|  | 344 | in_buf_new_len)); | 
| Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 345 | modified = 1; | 
|  | 346 | } | 
|  | 347 | } | 
|  | 348 | } | 
|  | 349 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 350 | if (ssl->out_buf != NULL) { | 
| Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 351 | written_out = ssl->out_msg - ssl->out_buf; | 
|  | 352 | iv_offset_out = ssl->out_iv - ssl->out_buf; | 
|  | 353 | len_offset_out = ssl->out_len - ssl->out_buf; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 354 | if (downsizing ? | 
| Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 355 | ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len : | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 356 | ssl->out_buf_len < out_buf_new_len) { | 
|  | 357 | if (resize_buffer(&ssl->out_buf, out_buf_new_len, &ssl->out_buf_len) != 0) { | 
|  | 358 | MBEDTLS_SSL_DEBUG_MSG(1, ("output buffer resizing failed - out of memory")); | 
|  | 359 | } else { | 
|  | 360 | MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET, | 
|  | 361 | out_buf_new_len)); | 
| Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 362 | modified = 1; | 
|  | 363 | } | 
|  | 364 | } | 
|  | 365 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 366 | if (modified) { | 
| Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 367 | /* Update pointers here to avoid doing it twice. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 368 | mbedtls_ssl_reset_in_out_pointers(ssl); | 
| Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 369 | /* Fields below might not be properly updated with record | 
|  | 370 | * splitting or with CID, so they are manually updated here. */ | 
|  | 371 | ssl->out_msg = ssl->out_buf + written_out; | 
|  | 372 | ssl->out_len = ssl->out_buf + len_offset_out; | 
|  | 373 | ssl->out_iv = ssl->out_buf + iv_offset_out; | 
|  | 374 |  | 
|  | 375 | ssl->in_msg = ssl->in_buf + written_in; | 
|  | 376 | ssl->in_len = ssl->in_buf + len_offset_in; | 
|  | 377 | ssl->in_iv = ssl->in_buf + iv_offset_in; | 
|  | 378 | } | 
|  | 379 | } | 
| Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 380 | #endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */ | 
|  | 381 |  | 
| Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 382 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 383 |  | 
|  | 384 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 385 | typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen, | 
|  | 386 | const char *label, | 
|  | 387 | const unsigned char *random, size_t rlen, | 
|  | 388 | unsigned char *dstbuf, size_t dlen); | 
| Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 389 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 390 | static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id); | 
| Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 391 |  | 
|  | 392 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ | 
|  | 393 |  | 
|  | 394 | /* Type for the TLS PRF */ | 
|  | 395 | typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *, | 
|  | 396 | const unsigned char *, size_t, | 
|  | 397 | unsigned char *, size_t); | 
|  | 398 |  | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 399 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 400 | static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform, | 
|  | 401 | int ciphersuite, | 
|  | 402 | const unsigned char master[48], | 
| Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 403 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 404 | int encrypt_then_mac, | 
| Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 405 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 406 | ssl_tls_prf_t tls_prf, | 
|  | 407 | const unsigned char randbytes[64], | 
|  | 408 | mbedtls_ssl_protocol_version tls_version, | 
|  | 409 | unsigned endpoint, | 
|  | 410 | const mbedtls_ssl_context *ssl); | 
| Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 411 |  | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 412 | #if defined(MBEDTLS_MD_CAN_SHA256) | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 413 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 414 | static int tls_prf_sha256(const unsigned char *secret, size_t slen, | 
| Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 415 | const char *label, | 
|  | 416 | const unsigned char *random, size_t rlen, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 417 | unsigned char *dstbuf, size_t dlen); | 
| Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 418 | static int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *); | 
|  | 419 | static int ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 420 |  | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 421 | #endif /* MBEDTLS_MD_CAN_SHA256*/ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 422 |  | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 423 | #if defined(MBEDTLS_MD_CAN_SHA384) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 424 | MBEDTLS_CHECK_RETURN_CRITICAL | 
|  | 425 | static int tls_prf_sha384(const unsigned char *secret, size_t slen, | 
|  | 426 | const char *label, | 
|  | 427 | const unsigned char *random, size_t rlen, | 
|  | 428 | unsigned char *dstbuf, size_t dlen); | 
|  | 429 |  | 
| Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 430 | static int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *); | 
|  | 431 | static int ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int); | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 432 | #endif /* MBEDTLS_MD_CAN_SHA384*/ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 433 |  | 
|  | 434 | static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session, | 
|  | 435 | unsigned char *buf, | 
|  | 436 | size_t buf_len); | 
|  | 437 |  | 
|  | 438 | MBEDTLS_CHECK_RETURN_CRITICAL | 
|  | 439 | static int ssl_tls12_session_load(mbedtls_ssl_session *session, | 
|  | 440 | const unsigned char *buf, | 
|  | 441 | size_t len); | 
|  | 442 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 443 |  | 
| Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 444 | static int ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 445 |  | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 446 | #if defined(MBEDTLS_MD_CAN_SHA256) | 
| Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 447 | static int ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t); | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 448 | #endif /* MBEDTLS_MD_CAN_SHA256*/ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 449 |  | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 450 | #if defined(MBEDTLS_MD_CAN_SHA384) | 
| Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 451 | static int ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t); | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 452 | #endif /* MBEDTLS_MD_CAN_SHA384*/ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 453 |  | 
|  | 454 | int  mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf, | 
|  | 455 | const unsigned char *secret, size_t slen, | 
|  | 456 | const char *label, | 
|  | 457 | const unsigned char *random, size_t rlen, | 
|  | 458 | unsigned char *dstbuf, size_t dlen) | 
| Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 459 | { | 
|  | 460 | mbedtls_ssl_tls_prf_cb *tls_prf = NULL; | 
|  | 461 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 462 | switch (prf) { | 
| Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 463 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 464 | #if defined(MBEDTLS_MD_CAN_SHA384) | 
| Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 465 | case MBEDTLS_SSL_TLS_PRF_SHA384: | 
|  | 466 | tls_prf = tls_prf_sha384; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 467 | break; | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 468 | #endif /* MBEDTLS_MD_CAN_SHA384*/ | 
|  | 469 | #if defined(MBEDTLS_MD_CAN_SHA256) | 
| Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 470 | case MBEDTLS_SSL_TLS_PRF_SHA256: | 
|  | 471 | tls_prf = tls_prf_sha256; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 472 | break; | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 473 | #endif /* MBEDTLS_MD_CAN_SHA256*/ | 
| Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 474 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 475 | default: | 
|  | 476 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 477 | } | 
|  | 478 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 479 | return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen); | 
| Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 480 | } | 
|  | 481 |  | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 482 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 483 | static void ssl_clear_peer_cert(mbedtls_ssl_session *session) | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 484 | { | 
|  | 485 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 486 | if (session->peer_cert != NULL) { | 
|  | 487 | mbedtls_x509_crt_free(session->peer_cert); | 
|  | 488 | mbedtls_free(session->peer_cert); | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 489 | session->peer_cert = NULL; | 
|  | 490 | } | 
|  | 491 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 492 | if (session->peer_cert_digest != NULL) { | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 493 | /* Zeroization is not necessary. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 494 | mbedtls_free(session->peer_cert_digest); | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 495 | session->peer_cert_digest      = NULL; | 
|  | 496 | session->peer_cert_digest_type = MBEDTLS_MD_NONE; | 
|  | 497 | session->peer_cert_digest_len  = 0; | 
|  | 498 | } | 
|  | 499 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 500 | } | 
|  | 501 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
|  | 502 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 503 | uint32_t mbedtls_ssl_get_extension_id(unsigned int extension_type) | 
| Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 504 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 505 | switch (extension_type) { | 
| Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 506 | case MBEDTLS_TLS_EXT_SERVERNAME: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 507 | return MBEDTLS_SSL_EXT_ID_SERVERNAME; | 
| Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 508 |  | 
|  | 509 | case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 510 | return MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH; | 
| Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 511 |  | 
|  | 512 | case MBEDTLS_TLS_EXT_STATUS_REQUEST: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 513 | return MBEDTLS_SSL_EXT_ID_STATUS_REQUEST; | 
| Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 514 |  | 
|  | 515 | case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 516 | return MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS; | 
| Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 517 |  | 
|  | 518 | case MBEDTLS_TLS_EXT_SIG_ALG: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 519 | return MBEDTLS_SSL_EXT_ID_SIG_ALG; | 
| Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 520 |  | 
|  | 521 | case MBEDTLS_TLS_EXT_USE_SRTP: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 522 | return MBEDTLS_SSL_EXT_ID_USE_SRTP; | 
| Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 523 |  | 
|  | 524 | case MBEDTLS_TLS_EXT_HEARTBEAT: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 525 | return MBEDTLS_SSL_EXT_ID_HEARTBEAT; | 
| Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 526 |  | 
|  | 527 | case MBEDTLS_TLS_EXT_ALPN: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 528 | return MBEDTLS_SSL_EXT_ID_ALPN; | 
| Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 529 |  | 
|  | 530 | case MBEDTLS_TLS_EXT_SCT: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 531 | return MBEDTLS_SSL_EXT_ID_SCT; | 
| Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 532 |  | 
|  | 533 | case MBEDTLS_TLS_EXT_CLI_CERT_TYPE: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 534 | return MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE; | 
| Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 535 |  | 
|  | 536 | case MBEDTLS_TLS_EXT_SERV_CERT_TYPE: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 537 | return MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE; | 
| Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 538 |  | 
|  | 539 | case MBEDTLS_TLS_EXT_PADDING: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 540 | return MBEDTLS_SSL_EXT_ID_PADDING; | 
| Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 541 |  | 
|  | 542 | case MBEDTLS_TLS_EXT_PRE_SHARED_KEY: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 543 | return MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY; | 
| Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 544 |  | 
|  | 545 | case MBEDTLS_TLS_EXT_EARLY_DATA: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 546 | return MBEDTLS_SSL_EXT_ID_EARLY_DATA; | 
| Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 547 |  | 
|  | 548 | case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 549 | return MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS; | 
| Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 550 |  | 
|  | 551 | case MBEDTLS_TLS_EXT_COOKIE: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 552 | return MBEDTLS_SSL_EXT_ID_COOKIE; | 
| Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 553 |  | 
|  | 554 | case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 555 | return MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES; | 
| Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 556 |  | 
|  | 557 | case MBEDTLS_TLS_EXT_CERT_AUTH: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 558 | return MBEDTLS_SSL_EXT_ID_CERT_AUTH; | 
| Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 559 |  | 
|  | 560 | case MBEDTLS_TLS_EXT_OID_FILTERS: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 561 | return MBEDTLS_SSL_EXT_ID_OID_FILTERS; | 
| Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 562 |  | 
|  | 563 | case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 564 | return MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH; | 
| Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 565 |  | 
|  | 566 | case MBEDTLS_TLS_EXT_SIG_ALG_CERT: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 567 | return MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT; | 
| Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 568 |  | 
|  | 569 | case MBEDTLS_TLS_EXT_KEY_SHARE: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 570 | return MBEDTLS_SSL_EXT_ID_KEY_SHARE; | 
| Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 571 |  | 
|  | 572 | case MBEDTLS_TLS_EXT_TRUNCATED_HMAC: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 573 | return MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC; | 
| Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 574 |  | 
|  | 575 | case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 576 | return MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS; | 
| Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 577 |  | 
|  | 578 | case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 579 | return MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC; | 
| Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 580 |  | 
|  | 581 | case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 582 | return MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET; | 
| Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 583 |  | 
| Jan Bruckner | 151f642 | 2023-02-10 12:45:19 +0100 | [diff] [blame] | 584 | case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT: | 
|  | 585 | return MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT; | 
|  | 586 |  | 
| Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 587 | case MBEDTLS_TLS_EXT_SESSION_TICKET: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 588 | return MBEDTLS_SSL_EXT_ID_SESSION_TICKET; | 
| Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 589 |  | 
|  | 590 | } | 
|  | 591 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 592 | return MBEDTLS_SSL_EXT_ID_UNRECOGNIZED; | 
| Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 593 | } | 
|  | 594 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 595 | uint32_t mbedtls_ssl_get_extension_mask(unsigned int extension_type) | 
| Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 596 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 597 | return 1 << mbedtls_ssl_get_extension_id(extension_type); | 
| Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 598 | } | 
|  | 599 |  | 
| Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 600 | #if defined(MBEDTLS_DEBUG_C) | 
|  | 601 | static const char *extension_name_table[] = { | 
| Jerry Yu | ea52ed9 | 2022-11-08 21:01:17 +0800 | [diff] [blame] | 602 | [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized", | 
| Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 603 | [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name", | 
|  | 604 | [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length", | 
|  | 605 | [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request", | 
|  | 606 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups", | 
|  | 607 | [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms", | 
|  | 608 | [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp", | 
|  | 609 | [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat", | 
|  | 610 | [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation", | 
|  | 611 | [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp", | 
|  | 612 | [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type", | 
|  | 613 | [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type", | 
|  | 614 | [MBEDTLS_SSL_EXT_ID_PADDING] = "padding", | 
|  | 615 | [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key", | 
|  | 616 | [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data", | 
|  | 617 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions", | 
|  | 618 | [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie", | 
|  | 619 | [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes", | 
|  | 620 | [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities", | 
|  | 621 | [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters", | 
|  | 622 | [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth", | 
|  | 623 | [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert", | 
|  | 624 | [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share", | 
|  | 625 | [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac", | 
|  | 626 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats", | 
|  | 627 | [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac", | 
|  | 628 | [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret", | 
| Jan Bruckner | 151f642 | 2023-02-10 12:45:19 +0100 | [diff] [blame] | 629 | [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket", | 
|  | 630 | [MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT] = "record_size_limit" | 
| Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 631 | }; | 
|  | 632 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 633 | static unsigned int extension_type_table[] = { | 
| Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 634 | [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff, | 
|  | 635 | [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME, | 
|  | 636 | [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, | 
|  | 637 | [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST, | 
|  | 638 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, | 
|  | 639 | [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG, | 
|  | 640 | [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP, | 
|  | 641 | [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT, | 
|  | 642 | [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN, | 
|  | 643 | [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT, | 
|  | 644 | [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE, | 
|  | 645 | [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE, | 
|  | 646 | [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING, | 
|  | 647 | [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY, | 
|  | 648 | [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA, | 
|  | 649 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, | 
|  | 650 | [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE, | 
|  | 651 | [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES, | 
|  | 652 | [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH, | 
|  | 653 | [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS, | 
|  | 654 | [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH, | 
|  | 655 | [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT, | 
|  | 656 | [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE, | 
|  | 657 | [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC, | 
|  | 658 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, | 
|  | 659 | [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, | 
|  | 660 | [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, | 
| Jan Bruckner | 151f642 | 2023-02-10 12:45:19 +0100 | [diff] [blame] | 661 | [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET, | 
|  | 662 | [MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT] = MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT | 
| Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 663 | }; | 
|  | 664 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 665 | const char *mbedtls_ssl_get_extension_name(unsigned int extension_type) | 
| Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 666 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 667 | return extension_name_table[ | 
|  | 668 | mbedtls_ssl_get_extension_id(extension_type)]; | 
| Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 669 | } | 
|  | 670 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 671 | static const char *ssl_tls13_get_hs_msg_name(int hs_msg_type) | 
| Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 672 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 673 | switch (hs_msg_type) { | 
| Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 674 | case MBEDTLS_SSL_HS_CLIENT_HELLO: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 675 | return "ClientHello"; | 
| Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 676 | case MBEDTLS_SSL_HS_SERVER_HELLO: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 677 | return "ServerHello"; | 
| Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 678 | case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 679 | return "HelloRetryRequest"; | 
| Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 680 | case MBEDTLS_SSL_HS_NEW_SESSION_TICKET: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 681 | return "NewSessionTicket"; | 
| Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 682 | case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 683 | return "EncryptedExtensions"; | 
| Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 684 | case MBEDTLS_SSL_HS_CERTIFICATE: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 685 | return "Certificate"; | 
| Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 686 | case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 687 | return "CertificateRequest"; | 
| Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 688 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 689 | return "Unknown"; | 
| Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 690 | } | 
|  | 691 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 692 | void mbedtls_ssl_print_extension(const mbedtls_ssl_context *ssl, | 
|  | 693 | int level, const char *file, int line, | 
|  | 694 | int hs_msg_type, unsigned int extension_type, | 
|  | 695 | const char *extra_msg0, const char *extra_msg1) | 
| Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 696 | { | 
|  | 697 | const char *extra_msg; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 698 | if (extra_msg0 && extra_msg1) { | 
| Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 699 | mbedtls_debug_print_msg( | 
|  | 700 | ssl, level, file, line, | 
|  | 701 | "%s: %s(%u) extension %s %s.", | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 702 | ssl_tls13_get_hs_msg_name(hs_msg_type), | 
|  | 703 | mbedtls_ssl_get_extension_name(extension_type), | 
| Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 704 | extension_type, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 705 | extra_msg0, extra_msg1); | 
| Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 706 | return; | 
|  | 707 | } | 
|  | 708 |  | 
|  | 709 | extra_msg = extra_msg0 ? extra_msg0 : extra_msg1; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 710 | if (extra_msg) { | 
| Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 711 | mbedtls_debug_print_msg( | 
|  | 712 | ssl, level, file, line, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 713 | "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name(hs_msg_type), | 
|  | 714 | mbedtls_ssl_get_extension_name(extension_type), extension_type, | 
|  | 715 | extra_msg); | 
| Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 716 | return; | 
|  | 717 | } | 
|  | 718 |  | 
|  | 719 | mbedtls_debug_print_msg( | 
|  | 720 | ssl, level, file, line, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 721 | "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name(hs_msg_type), | 
|  | 722 | mbedtls_ssl_get_extension_name(extension_type), extension_type); | 
| Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 723 | } | 
|  | 724 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 725 | void mbedtls_ssl_print_extensions(const mbedtls_ssl_context *ssl, | 
|  | 726 | int level, const char *file, int line, | 
|  | 727 | int hs_msg_type, uint32_t extensions_mask, | 
|  | 728 | const char *extra) | 
| Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 729 | { | 
|  | 730 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 731 | for (unsigned i = 0; | 
|  | 732 | i < sizeof(extension_name_table) / sizeof(extension_name_table[0]); | 
|  | 733 | i++) { | 
| Jerry Yu | 79aa721 | 2022-11-08 21:30:21 +0800 | [diff] [blame] | 734 | mbedtls_ssl_print_extension( | 
| Jerry Yu | ea52ed9 | 2022-11-08 21:01:17 +0800 | [diff] [blame] | 735 | ssl, level, file, line, hs_msg_type, extension_type_table[i], | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 736 | extensions_mask & (1 << i) ? "exists" : "does not exist", extra); | 
| Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 737 | } | 
|  | 738 | } | 
|  | 739 |  | 
| Pengyu Lv | ee455c0 | 2023-01-12 14:37:24 +0800 | [diff] [blame] | 740 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) | 
| Pengyu Lv | ee455c0 | 2023-01-12 14:37:24 +0800 | [diff] [blame] | 741 | static const char *ticket_flag_name_table[] = | 
|  | 742 | { | 
|  | 743 | [0] = "ALLOW_PSK_RESUMPTION", | 
|  | 744 | [2] = "ALLOW_PSK_EPHEMERAL_RESUMPTION", | 
|  | 745 | [3] = "ALLOW_EARLY_DATA", | 
|  | 746 | }; | 
|  | 747 |  | 
| Pengyu Lv | 4938a56 | 2023-01-16 11:28:49 +0800 | [diff] [blame] | 748 | void mbedtls_ssl_print_ticket_flags(const mbedtls_ssl_context *ssl, | 
|  | 749 | int level, const char *file, int line, | 
|  | 750 | unsigned int flags) | 
| Pengyu Lv | ee455c0 | 2023-01-12 14:37:24 +0800 | [diff] [blame] | 751 | { | 
|  | 752 | size_t i; | 
|  | 753 |  | 
|  | 754 | mbedtls_debug_print_msg(ssl, level, file, line, | 
| Pengyu Lv | 4938a56 | 2023-01-16 11:28:49 +0800 | [diff] [blame] | 755 | "print ticket_flags (0x%02x)", flags); | 
|  | 756 |  | 
|  | 757 | flags = flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK; | 
| Pengyu Lv | ee455c0 | 2023-01-12 14:37:24 +0800 | [diff] [blame] | 758 |  | 
|  | 759 | for (i = 0; i < ARRAY_LENGTH(ticket_flag_name_table); i++) { | 
| Pengyu Lv | 4938a56 | 2023-01-16 11:28:49 +0800 | [diff] [blame] | 760 | if ((flags & (1 << i))) { | 
| Pengyu Lv | ee455c0 | 2023-01-12 14:37:24 +0800 | [diff] [blame] | 761 | mbedtls_debug_print_msg(ssl, level, file, line, "- %s is set.", | 
|  | 762 | ticket_flag_name_table[i]); | 
|  | 763 | } | 
|  | 764 | } | 
|  | 765 | } | 
|  | 766 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */ | 
|  | 767 |  | 
| Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 768 | #endif /* MBEDTLS_DEBUG_C */ | 
|  | 769 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 770 | void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl, | 
|  | 771 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info) | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 772 | { | 
|  | 773 | ((void) ciphersuite_info); | 
|  | 774 |  | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 775 | #if defined(MBEDTLS_MD_CAN_SHA384) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 776 | if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) { | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 777 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 778 | } else | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 779 | #endif | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 780 | #if defined(MBEDTLS_MD_CAN_SHA256) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 781 | if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) { | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 782 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 783 | } else | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 784 | #endif | 
|  | 785 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 786 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 787 | return; | 
|  | 788 | } | 
|  | 789 | } | 
|  | 790 |  | 
| Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 791 | int mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 43cc127 | 2023-02-06 11:48:19 +0100 | [diff] [blame] | 792 | unsigned hs_type, | 
|  | 793 | size_t total_hs_len) | 
| Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 794 | { | 
|  | 795 | unsigned char hs_hdr[4]; | 
|  | 796 |  | 
|  | 797 | /* Build HS header for checksum update. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 798 | hs_hdr[0] = MBEDTLS_BYTE_0(hs_type); | 
|  | 799 | hs_hdr[1] = MBEDTLS_BYTE_2(total_hs_len); | 
|  | 800 | hs_hdr[2] = MBEDTLS_BYTE_1(total_hs_len); | 
|  | 801 | hs_hdr[3] = MBEDTLS_BYTE_0(total_hs_len); | 
| Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 802 |  | 
| Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 803 | return ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr)); | 
| Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 804 | } | 
|  | 805 |  | 
| Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 806 | int mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 43cc127 | 2023-02-06 11:48:19 +0100 | [diff] [blame] | 807 | unsigned hs_type, | 
|  | 808 | unsigned char const *msg, | 
|  | 809 | size_t msg_len) | 
| Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 810 | { | 
| Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 811 | int ret; | 
|  | 812 | ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl, hs_type, msg_len); | 
| Manuel Pégourié-Gonnard | 43cc127 | 2023-02-06 11:48:19 +0100 | [diff] [blame] | 813 | if (ret != 0) { | 
| Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 814 | return ret; | 
| Manuel Pégourié-Gonnard | 43cc127 | 2023-02-06 11:48:19 +0100 | [diff] [blame] | 815 | } | 
| Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 816 | return ssl->handshake->update_checksum(ssl, msg, msg_len); | 
| Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 817 | } | 
|  | 818 |  | 
| Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 819 | int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl) | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 820 | { | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 821 | #if defined(MBEDTLS_MD_CAN_SHA256) || \ | 
|  | 822 | defined(MBEDTLS_MD_CAN_SHA384) | 
| Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 823 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 824 | psa_status_t status; | 
|  | 825 | #else | 
|  | 826 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 827 | #endif | 
| Manuel Pégourié-Gonnard | 626aaed | 2023-02-06 22:03:06 +0100 | [diff] [blame] | 828 | #else /* SHA-256 or SHA-384 */ | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 829 | ((void) ssl); | 
| Manuel Pégourié-Gonnard | 626aaed | 2023-02-06 22:03:06 +0100 | [diff] [blame] | 830 | #endif /* SHA-256 or SHA-384 */ | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 831 | #if defined(MBEDTLS_MD_CAN_SHA256) | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 832 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 833 | status = psa_hash_abort(&ssl->handshake->fin_sha256_psa); | 
|  | 834 | if (status != PSA_SUCCESS) { | 
| Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 835 | return mbedtls_md_error_from_psa(status); | 
| Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 836 | } | 
|  | 837 | status = psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256); | 
|  | 838 | if (status != PSA_SUCCESS) { | 
| Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 839 | return mbedtls_md_error_from_psa(status); | 
| Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 840 | } | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 841 | #else | 
| Manuel Pégourié-Gonnard | 947cee1 | 2023-03-06 11:59:59 +0100 | [diff] [blame] | 842 | mbedtls_md_free(&ssl->handshake->fin_sha256); | 
|  | 843 | mbedtls_md_init(&ssl->handshake->fin_sha256); | 
| Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 844 | ret = mbedtls_md_setup(&ssl->handshake->fin_sha256, | 
|  | 845 | mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), | 
|  | 846 | 0); | 
|  | 847 | if (ret != 0) { | 
|  | 848 | return ret; | 
|  | 849 | } | 
|  | 850 | ret = mbedtls_md_starts(&ssl->handshake->fin_sha256); | 
| Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 851 | if (ret != 0) { | 
|  | 852 | return ret; | 
|  | 853 | } | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 854 | #endif | 
|  | 855 | #endif | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 856 | #if defined(MBEDTLS_MD_CAN_SHA384) | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 857 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 858 | status = psa_hash_abort(&ssl->handshake->fin_sha384_psa); | 
|  | 859 | if (status != PSA_SUCCESS) { | 
| Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 860 | return mbedtls_md_error_from_psa(status); | 
| Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 861 | } | 
|  | 862 | status = psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384); | 
|  | 863 | if (status != PSA_SUCCESS) { | 
| Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 864 | return mbedtls_md_error_from_psa(status); | 
| Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 865 | } | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 866 | #else | 
| Manuel Pégourié-Gonnard | 947cee1 | 2023-03-06 11:59:59 +0100 | [diff] [blame] | 867 | mbedtls_md_free(&ssl->handshake->fin_sha384); | 
|  | 868 | mbedtls_md_init(&ssl->handshake->fin_sha384); | 
| Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 869 | ret = mbedtls_md_setup(&ssl->handshake->fin_sha384, | 
|  | 870 | mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0); | 
|  | 871 | if (ret != 0) { | 
|  | 872 | return ret; | 
|  | 873 | } | 
|  | 874 | ret = mbedtls_md_starts(&ssl->handshake->fin_sha384); | 
| Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 875 | if (ret != 0) { | 
|  | 876 | return ret; | 
|  | 877 | } | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 878 | #endif | 
|  | 879 | #endif | 
| Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 880 | return 0; | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 881 | } | 
|  | 882 |  | 
| Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 883 | static int ssl_update_checksum_start(mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 43cc127 | 2023-02-06 11:48:19 +0100 | [diff] [blame] | 884 | const unsigned char *buf, size_t len) | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 885 | { | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 886 | #if defined(MBEDTLS_MD_CAN_SHA256) || \ | 
|  | 887 | defined(MBEDTLS_MD_CAN_SHA384) | 
| Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 888 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 889 | psa_status_t status; | 
|  | 890 | #else | 
|  | 891 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 892 | #endif | 
| Manuel Pégourié-Gonnard | 626aaed | 2023-02-06 22:03:06 +0100 | [diff] [blame] | 893 | #else /* SHA-256 or SHA-384 */ | 
|  | 894 | ((void) ssl); | 
|  | 895 | (void) buf; | 
|  | 896 | (void) len; | 
|  | 897 | #endif /* SHA-256 or SHA-384 */ | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 898 | #if defined(MBEDTLS_MD_CAN_SHA256) | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 899 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 900 | status = psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len); | 
|  | 901 | if (status != PSA_SUCCESS) { | 
| Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 902 | return mbedtls_md_error_from_psa(status); | 
| Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 903 | } | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 904 | #else | 
| Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 905 | ret = mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len); | 
| Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 906 | if (ret != 0) { | 
|  | 907 | return ret; | 
|  | 908 | } | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 909 | #endif | 
|  | 910 | #endif | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 911 | #if defined(MBEDTLS_MD_CAN_SHA384) | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 912 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 913 | status = psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len); | 
|  | 914 | if (status != PSA_SUCCESS) { | 
| Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 915 | return mbedtls_md_error_from_psa(status); | 
| Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 916 | } | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 917 | #else | 
| Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 918 | ret = mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len); | 
| Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 919 | if (ret != 0) { | 
|  | 920 | return ret; | 
|  | 921 | } | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 922 | #endif | 
|  | 923 | #endif | 
| Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 924 | return 0; | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 925 | } | 
|  | 926 |  | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 927 | #if defined(MBEDTLS_MD_CAN_SHA256) | 
| Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 928 | static int ssl_update_checksum_sha256(mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 43cc127 | 2023-02-06 11:48:19 +0100 | [diff] [blame] | 929 | const unsigned char *buf, size_t len) | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 930 | { | 
|  | 931 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 932 | return mbedtls_md_error_from_psa(psa_hash_update( | 
|  | 933 | &ssl->handshake->fin_sha256_psa, buf, len)); | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 934 | #else | 
| Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 935 | return mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len); | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 936 | #endif | 
|  | 937 | } | 
|  | 938 | #endif | 
|  | 939 |  | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 940 | #if defined(MBEDTLS_MD_CAN_SHA384) | 
| Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 941 | static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 43cc127 | 2023-02-06 11:48:19 +0100 | [diff] [blame] | 942 | const unsigned char *buf, size_t len) | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 943 | { | 
|  | 944 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 945 | return mbedtls_md_error_from_psa(psa_hash_update( | 
|  | 946 | &ssl->handshake->fin_sha384_psa, buf, len)); | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 947 | #else | 
| Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 948 | return mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len); | 
| Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 949 | #endif | 
|  | 950 | } | 
|  | 951 | #endif | 
|  | 952 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 953 | static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake) | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 954 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 955 | memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params)); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 956 |  | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 957 | #if defined(MBEDTLS_MD_CAN_SHA256) | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 958 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 959 | handshake->fin_sha256_psa = psa_hash_operation_init(); | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 960 | #else | 
| Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 961 | mbedtls_md_init(&handshake->fin_sha256); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 962 | #endif | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 963 | #endif | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 964 | #if defined(MBEDTLS_MD_CAN_SHA384) | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 965 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 966 | handshake->fin_sha384_psa = psa_hash_operation_init(); | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 967 | #else | 
| Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 968 | mbedtls_md_init(&handshake->fin_sha384); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 969 | #endif | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 970 | #endif | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 971 |  | 
|  | 972 | handshake->update_checksum = ssl_update_checksum_start; | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 973 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 974 | #if defined(MBEDTLS_DHM_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 975 | mbedtls_dhm_init(&handshake->dhm_ctx); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 976 | #endif | 
| Valerio Setti | 7aeec54 | 2023-07-05 18:57:21 +0200 | [diff] [blame] | 977 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ | 
| Valerio Setti | 6eb0054 | 2023-07-07 17:04:24 +0200 | [diff] [blame] | 978 | defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 979 | mbedtls_ecdh_init(&handshake->ecdh_ctx); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 980 | #endif | 
| Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 981 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 982 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 983 | handshake->psa_pake_ctx = psa_pake_operation_init(); | 
|  | 984 | handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 985 | #else | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 986 | mbedtls_ecjpake_init(&handshake->ecjpake_ctx); | 
| Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 987 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 988 | #if defined(MBEDTLS_SSL_CLI_C) | 
|  | 989 | handshake->ecjpake_cache = NULL; | 
|  | 990 | handshake->ecjpake_cache_len = 0; | 
|  | 991 | #endif | 
| Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 992 | #endif | 
| Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 993 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 994 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 995 | mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx); | 
| Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 996 | #endif | 
|  | 997 |  | 
| Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 998 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
|  | 999 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; | 
|  | 1000 | #endif | 
| Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 1001 |  | 
|  | 1002 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ | 
|  | 1003 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1004 | mbedtls_pk_init(&handshake->peer_pubkey); | 
| Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 1005 | #endif | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1006 | } | 
|  | 1007 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1008 | void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform) | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1009 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1010 | memset(transform, 0, sizeof(mbedtls_ssl_transform)); | 
| Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 1011 |  | 
| Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 1012 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 1013 | transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 1014 | transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT; | 
| Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 1015 | #else | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1016 | mbedtls_cipher_init(&transform->cipher_ctx_enc); | 
|  | 1017 | mbedtls_cipher_init(&transform->cipher_ctx_dec); | 
| Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 1018 | #endif | 
|  | 1019 |  | 
| Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 1020 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) | 
| Neil Armstrong | 39b8e7d | 2022-02-23 09:24:45 +0100 | [diff] [blame] | 1021 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 1022 | transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 1023 | transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT; | 
| Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 1024 | #else | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1025 | mbedtls_md_init(&transform->md_ctx_enc); | 
|  | 1026 | mbedtls_md_init(&transform->md_ctx_dec); | 
| Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1027 | #endif | 
| Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 1028 | #endif | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1029 | } | 
|  | 1030 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1031 | void mbedtls_ssl_session_init(mbedtls_ssl_session *session) | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1032 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1033 | memset(session, 0, sizeof(mbedtls_ssl_session)); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1034 | } | 
|  | 1035 |  | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1036 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1037 | static int ssl_handshake_init(mbedtls_ssl_context *ssl) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1038 | { | 
| Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 1039 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 1040 |  | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1041 | /* Clear old handshake information if present */ | 
| Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1042 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1043 | if (ssl->transform_negotiate) { | 
|  | 1044 | mbedtls_ssl_transform_free(ssl->transform_negotiate); | 
|  | 1045 | } | 
| Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1046 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1047 | if (ssl->session_negotiate) { | 
|  | 1048 | mbedtls_ssl_session_free(ssl->session_negotiate); | 
|  | 1049 | } | 
|  | 1050 | if (ssl->handshake) { | 
|  | 1051 | mbedtls_ssl_handshake_free(ssl); | 
|  | 1052 | } | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1053 |  | 
| Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1054 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1055 | /* | 
|  | 1056 | * Either the pointers are now NULL or cleared properly and can be freed. | 
|  | 1057 | * Now allocate missing structures. | 
|  | 1058 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1059 | if (ssl->transform_negotiate == NULL) { | 
|  | 1060 | ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform)); | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 1061 | } | 
| Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1062 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1063 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1064 | if (ssl->session_negotiate == NULL) { | 
|  | 1065 | ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session)); | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 1066 | } | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1067 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1068 | if (ssl->handshake == NULL) { | 
|  | 1069 | ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params)); | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 1070 | } | 
| Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1071 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
|  | 1072 | /* If the buffers are too small - reallocate */ | 
| Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 1073 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1074 | handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN, | 
|  | 1075 | MBEDTLS_SSL_OUT_BUFFER_LEN); | 
| Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1076 | #endif | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1077 |  | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1078 | /* All pointers should exist and can be directly freed without issue */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1079 | if (ssl->handshake           == NULL || | 
| Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1080 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1081 | ssl->transform_negotiate == NULL || | 
| Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1082 | #endif | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1083 | ssl->session_negotiate   == NULL) { | 
|  | 1084 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed")); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1085 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1086 | mbedtls_free(ssl->handshake); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1087 | ssl->handshake = NULL; | 
| Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1088 |  | 
|  | 1089 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1090 | mbedtls_free(ssl->transform_negotiate); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1091 | ssl->transform_negotiate = NULL; | 
| Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1092 | #endif | 
|  | 1093 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1094 | mbedtls_free(ssl->session_negotiate); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1095 | ssl->session_negotiate = NULL; | 
|  | 1096 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1097 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1098 | } | 
|  | 1099 |  | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1100 | /* Initialize structures */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1101 | mbedtls_ssl_session_init(ssl->session_negotiate); | 
|  | 1102 | ssl_handshake_params_init(ssl->handshake); | 
| Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 1103 |  | 
| Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1104 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1105 | mbedtls_ssl_transform_init(ssl->transform_negotiate); | 
| Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1106 | #endif | 
|  | 1107 |  | 
| Manuel Pégourié-Gonnard | 537f231 | 2023-02-05 10:17:45 +0100 | [diff] [blame] | 1108 | /* Setup handshake checksums */ | 
| Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 1109 | ret = mbedtls_ssl_reset_checksum(ssl); | 
|  | 1110 | if (ret != 0) { | 
|  | 1111 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret); | 
|  | 1112 | return ret; | 
|  | 1113 | } | 
| Manuel Pégourié-Gonnard | 537f231 | 2023-02-05 10:17:45 +0100 | [diff] [blame] | 1114 |  | 
| Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 1115 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ | 
|  | 1116 | defined(MBEDTLS_SSL_SRV_C) && \ | 
|  | 1117 | defined(MBEDTLS_SSL_SESSION_TICKETS) | 
|  | 1118 | ssl->handshake->new_session_tickets_count = | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1119 | ssl->conf->new_session_tickets_count; | 
| Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 1120 | #endif | 
|  | 1121 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1122 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1123 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { | 
| Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1124 | ssl->handshake->alt_transform_out = ssl->transform_out; | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1125 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1126 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { | 
| Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1127 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1128 | } else { | 
| Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1129 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1130 | } | 
| Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1131 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1132 | mbedtls_ssl_set_timer(ssl, 0); | 
| Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1133 | } | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1134 | #endif | 
|  | 1135 |  | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1136 | /* | 
|  | 1137 | * curve_list is translated to IANA TLS group identifiers here because | 
|  | 1138 | * mbedtls_ssl_conf_curves returns void and so can't return | 
|  | 1139 | * any error codes. | 
|  | 1140 | */ | 
| Manuel Pégourié-Gonnard | f07ce3b | 2023-09-22 11:53:41 +0200 | [diff] [blame] | 1141 | #if defined(MBEDTLS_ECP_C) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1142 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
|  | 1143 | /* Heap allocate and translate curve_list from internal to IANA group ids */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1144 | if (ssl->conf->curve_list != NULL) { | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1145 | size_t length; | 
|  | 1146 | const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list; | 
|  | 1147 |  | 
| Thomas Daubney | 850a079 | 2023-05-22 12:05:03 +0100 | [diff] [blame] | 1148 | for (length = 0;  (curve_list[length] != MBEDTLS_ECP_DP_NONE); length++) { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1149 | } | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1150 |  | 
|  | 1151 | /* Leave room for zero termination */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1152 | uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t)); | 
|  | 1153 | if (group_list == NULL) { | 
|  | 1154 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
|  | 1155 | } | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1156 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1157 | for (size_t i = 0; i < length; i++) { | 
| Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 1158 | uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1159 | curve_list[i]); | 
|  | 1160 | if (tls_id == 0) { | 
|  | 1161 | mbedtls_free(group_list); | 
|  | 1162 | return MBEDTLS_ERR_SSL_BAD_CONFIG; | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1163 | } | 
| Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 1164 | group_list[i] = tls_id; | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1165 | } | 
|  | 1166 |  | 
|  | 1167 | group_list[length] = 0; | 
|  | 1168 |  | 
|  | 1169 | ssl->handshake->group_list = group_list; | 
|  | 1170 | ssl->handshake->group_list_heap_allocated = 1; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1171 | } else { | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1172 | ssl->handshake->group_list = ssl->conf->group_list; | 
|  | 1173 | ssl->handshake->group_list_heap_allocated = 0; | 
|  | 1174 | } | 
|  | 1175 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ | 
| Manuel Pégourié-Gonnard | f07ce3b | 2023-09-22 11:53:41 +0200 | [diff] [blame] | 1176 | #endif /* MBEDTLS_ECP_C */ | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1177 |  | 
| Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1178 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1179 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
| Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 1180 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 1181 | /* Heap allocate and translate sig_hashes from internal hash identifiers to | 
|  | 1182 | signature algorithms IANA identifiers.  */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1183 | if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) && | 
|  | 1184 | ssl->conf->sig_hashes != NULL) { | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1185 | const int *md; | 
|  | 1186 | const int *sig_hashes = ssl->conf->sig_hashes; | 
| Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1187 | size_t sig_algs_len = 0; | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1188 | uint16_t *p; | 
|  | 1189 |  | 
| Tom Cosgrove | 6ef9bb3 | 2023-03-08 14:19:51 +0000 | [diff] [blame] | 1190 | MBEDTLS_STATIC_ASSERT(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN | 
|  | 1191 | <= (SIZE_MAX - (2 * sizeof(uint16_t))), | 
|  | 1192 | "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big"); | 
| Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 1193 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1194 | for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) { | 
|  | 1195 | if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) { | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1196 | continue; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1197 | } | 
| Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 1198 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1199 | sig_algs_len += sizeof(uint16_t); | 
| Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1200 | #endif | 
| Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 1201 |  | 
| Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1202 | #if defined(MBEDTLS_RSA_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1203 | sig_algs_len += sizeof(uint16_t); | 
| Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1204 | #endif | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1205 | if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) { | 
|  | 1206 | return MBEDTLS_ERR_SSL_BAD_CONFIG; | 
|  | 1207 | } | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1208 | } | 
|  | 1209 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1210 | if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) { | 
|  | 1211 | return MBEDTLS_ERR_SSL_BAD_CONFIG; | 
|  | 1212 | } | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1213 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1214 | ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len + | 
|  | 1215 | sizeof(uint16_t)); | 
|  | 1216 | if (ssl->handshake->sig_algs == NULL) { | 
|  | 1217 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
|  | 1218 | } | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1219 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1220 | p = (uint16_t *) ssl->handshake->sig_algs; | 
|  | 1221 | for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) { | 
|  | 1222 | unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md); | 
|  | 1223 | if (hash == MBEDTLS_SSL_HASH_NONE) { | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1224 | continue; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1225 | } | 
| Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 1226 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1227 | *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA); | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1228 | p++; | 
| Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 1229 | #endif | 
|  | 1230 | #if defined(MBEDTLS_RSA_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1231 | *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA); | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1232 | p++; | 
| Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 1233 | #endif | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1234 | } | 
| Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 1235 | *p = MBEDTLS_TLS_SIG_NONE; | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1236 | ssl->handshake->sig_algs_heap_allocated = 1; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1237 | } else | 
| Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 1238 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1239 | { | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1240 | ssl->handshake->sig_algs_heap_allocated = 0; | 
|  | 1241 | } | 
| Jerry Yu | cc53910 | 2022-06-27 16:27:35 +0800 | [diff] [blame] | 1242 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ | 
| Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1243 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1244 | return 0; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1245 | } | 
|  | 1246 |  | 
| Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1247 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1248 | /* Dummy cookie callbacks for defaults */ | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1249 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1250 | static int ssl_cookie_write_dummy(void *ctx, | 
|  | 1251 | unsigned char **p, unsigned char *end, | 
|  | 1252 | const unsigned char *cli_id, size_t cli_id_len) | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1253 | { | 
|  | 1254 | ((void) ctx); | 
|  | 1255 | ((void) p); | 
|  | 1256 | ((void) end); | 
|  | 1257 | ((void) cli_id); | 
|  | 1258 | ((void) cli_id_len); | 
|  | 1259 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1260 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1261 | } | 
|  | 1262 |  | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1263 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1264 | static int ssl_cookie_check_dummy(void *ctx, | 
|  | 1265 | const unsigned char *cookie, size_t cookie_len, | 
|  | 1266 | const unsigned char *cli_id, size_t cli_id_len) | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1267 | { | 
|  | 1268 | ((void) ctx); | 
|  | 1269 | ((void) cookie); | 
|  | 1270 | ((void) cookie_len); | 
|  | 1271 | ((void) cli_id); | 
|  | 1272 | ((void) cli_id_len); | 
|  | 1273 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1274 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1275 | } | 
| Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1276 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1277 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1278 | /* | 
|  | 1279 | * Initialize an SSL context | 
|  | 1280 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1281 | void mbedtls_ssl_init(mbedtls_ssl_context *ssl) | 
| Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1282 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1283 | memset(ssl, 0, sizeof(mbedtls_ssl_context)); | 
| Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1284 | } | 
|  | 1285 |  | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1286 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1287 | static int ssl_conf_version_check(const mbedtls_ssl_context *ssl) | 
| Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1288 | { | 
| Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 1289 | const mbedtls_ssl_config *conf = ssl->conf; | 
|  | 1290 |  | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1291 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1292 | if (mbedtls_ssl_conf_is_tls13_only(conf)) { | 
|  | 1293 | if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { | 
|  | 1294 | MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported.")); | 
|  | 1295 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 1296 | } | 
|  | 1297 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1298 | MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only.")); | 
|  | 1299 | return 0; | 
| Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1300 | } | 
|  | 1301 | #endif | 
|  | 1302 |  | 
|  | 1303 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1304 | if (mbedtls_ssl_conf_is_tls12_only(conf)) { | 
|  | 1305 | MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only.")); | 
|  | 1306 | return 0; | 
| Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1307 | } | 
|  | 1308 | #endif | 
|  | 1309 |  | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1310 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1311 | if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) { | 
|  | 1312 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { | 
|  | 1313 | MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2")); | 
|  | 1314 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 1315 | } | 
|  | 1316 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1317 | MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2.")); | 
|  | 1318 | return 0; | 
| Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1319 | } | 
|  | 1320 | #endif | 
|  | 1321 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1322 | MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid.")); | 
|  | 1323 | return MBEDTLS_ERR_SSL_BAD_CONFIG; | 
| Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1324 | } | 
|  | 1325 |  | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1326 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1327 | static int ssl_conf_check(const mbedtls_ssl_context *ssl) | 
|  | 1328 | { | 
|  | 1329 | int ret; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1330 | ret = ssl_conf_version_check(ssl); | 
|  | 1331 | if (ret != 0) { | 
|  | 1332 | return ret; | 
|  | 1333 | } | 
| Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1334 |  | 
| Jerry Yu | def7ae4 | 2022-10-30 14:13:19 +0800 | [diff] [blame] | 1335 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
|  | 1336 | /* RFC 8446 section 4.4.3 | 
|  | 1337 | * | 
|  | 1338 | * If the verification fails, the receiver MUST terminate the handshake with | 
|  | 1339 | * a "decrypt_error" alert. | 
|  | 1340 | * | 
|  | 1341 | * If the client is configured as TLS 1.3 only with optional verify, return | 
|  | 1342 | * bad config. | 
|  | 1343 | * | 
|  | 1344 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1345 | if (mbedtls_ssl_conf_tls13_ephemeral_enabled( | 
|  | 1346 | (mbedtls_ssl_context *) ssl)                            && | 
| Jerry Yu | def7ae4 | 2022-10-30 14:13:19 +0800 | [diff] [blame] | 1347 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT                && | 
|  | 1348 | ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3    && | 
|  | 1349 | ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3    && | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1350 | ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) { | 
| Jerry Yu | def7ae4 | 2022-10-30 14:13:19 +0800 | [diff] [blame] | 1351 | MBEDTLS_SSL_DEBUG_MSG( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1352 | 1, ("Optional verify auth mode " | 
|  | 1353 | "is not available for TLS 1.3 client")); | 
|  | 1354 | return MBEDTLS_ERR_SSL_BAD_CONFIG; | 
| Jerry Yu | def7ae4 | 2022-10-30 14:13:19 +0800 | [diff] [blame] | 1355 | } | 
|  | 1356 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
|  | 1357 |  | 
| Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1358 | /* Space for further checks */ | 
|  | 1359 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1360 | return 0; | 
| Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1361 | } | 
|  | 1362 |  | 
| Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1363 | /* | 
|  | 1364 | * Setup an SSL context | 
|  | 1365 | */ | 
| Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 1366 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1367 | int mbedtls_ssl_setup(mbedtls_ssl_context *ssl, | 
|  | 1368 | const mbedtls_ssl_config *conf) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1369 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1370 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1371 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; | 
|  | 1372 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1373 |  | 
| Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 1374 | ssl->conf = conf; | 
| Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1375 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1376 | if ((ret = ssl_conf_check(ssl)) != 0) { | 
|  | 1377 | return ret; | 
|  | 1378 | } | 
| Ronald Cron | 8a12aee | 2023-03-08 15:30:43 +0100 | [diff] [blame] | 1379 | ssl->tls_version = ssl->conf->max_tls_version; | 
| Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1380 |  | 
| Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1381 | /* | 
| Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 1382 | * Prepare base structures | 
| Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1383 | */ | 
| k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 1384 |  | 
|  | 1385 | /* Set to NULL in case of an error condition */ | 
|  | 1386 | ssl->out_buf = NULL; | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1387 |  | 
| Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1388 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
|  | 1389 | ssl->in_buf_len = in_buf_len; | 
|  | 1390 | #endif | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1391 | ssl->in_buf = mbedtls_calloc(1, in_buf_len); | 
|  | 1392 | if (ssl->in_buf == NULL) { | 
|  | 1393 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len)); | 
| k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 1394 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1395 | goto error; | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1396 | } | 
|  | 1397 |  | 
| Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1398 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
|  | 1399 | ssl->out_buf_len = out_buf_len; | 
|  | 1400 | #endif | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1401 | ssl->out_buf = mbedtls_calloc(1, out_buf_len); | 
|  | 1402 | if (ssl->out_buf == NULL) { | 
|  | 1403 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len)); | 
| k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 1404 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1405 | goto error; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1406 | } | 
|  | 1407 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1408 | mbedtls_ssl_reset_in_out_pointers(ssl); | 
| Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 1409 |  | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1410 | #if defined(MBEDTLS_SSL_DTLS_SRTP) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1411 | memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info)); | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1412 | #endif | 
|  | 1413 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1414 | if ((ret = ssl_handshake_init(ssl)) != 0) { | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1415 | goto error; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1416 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1417 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1418 | return 0; | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1419 |  | 
|  | 1420 | error: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1421 | mbedtls_free(ssl->in_buf); | 
|  | 1422 | mbedtls_free(ssl->out_buf); | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1423 |  | 
|  | 1424 | ssl->conf = NULL; | 
|  | 1425 |  | 
| Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1426 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
|  | 1427 | ssl->in_buf_len = 0; | 
|  | 1428 | ssl->out_buf_len = 0; | 
|  | 1429 | #endif | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1430 | ssl->in_buf = NULL; | 
|  | 1431 | ssl->out_buf = NULL; | 
|  | 1432 |  | 
|  | 1433 | ssl->in_hdr = NULL; | 
|  | 1434 | ssl->in_ctr = NULL; | 
|  | 1435 | ssl->in_len = NULL; | 
|  | 1436 | ssl->in_iv = NULL; | 
|  | 1437 | ssl->in_msg = NULL; | 
|  | 1438 |  | 
|  | 1439 | ssl->out_hdr = NULL; | 
|  | 1440 | ssl->out_ctr = NULL; | 
|  | 1441 | ssl->out_len = NULL; | 
|  | 1442 | ssl->out_iv = NULL; | 
|  | 1443 | ssl->out_msg = NULL; | 
|  | 1444 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1445 | return ret; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1446 | } | 
|  | 1447 |  | 
|  | 1448 | /* | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1449 | * Reset an initialized and used SSL context for re-use while retaining | 
|  | 1450 | * all application-set variables, function pointers and data. | 
| Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1451 | * | 
|  | 1452 | * If partial is non-zero, keep data in the input buffer and client ID. | 
|  | 1453 | * (Use when a DTLS client reconnects from the same port.) | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1454 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1455 | void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl, | 
|  | 1456 | int partial) | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1457 | { | 
| Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1458 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
|  | 1459 | size_t in_buf_len = ssl->in_buf_len; | 
|  | 1460 | size_t out_buf_len = ssl->out_buf_len; | 
|  | 1461 | #else | 
|  | 1462 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; | 
|  | 1463 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; | 
|  | 1464 | #endif | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1465 |  | 
| Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1466 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C) | 
|  | 1467 | partial = 0; | 
| Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 1468 | #endif | 
|  | 1469 |  | 
| Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1470 | /* Cancel any possibly running timer */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1471 | mbedtls_ssl_set_timer(ssl, 0); | 
| Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1472 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1473 | mbedtls_ssl_reset_in_out_pointers(ssl); | 
| Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1474 |  | 
|  | 1475 | /* Reset incoming message parsing */ | 
|  | 1476 | ssl->in_offt    = NULL; | 
|  | 1477 | ssl->nb_zero    = 0; | 
|  | 1478 | ssl->in_msgtype = 0; | 
|  | 1479 | ssl->in_msglen  = 0; | 
|  | 1480 | ssl->in_hslen   = 0; | 
|  | 1481 | ssl->keep_current_message = 0; | 
|  | 1482 | ssl->transform_in  = NULL; | 
|  | 1483 |  | 
|  | 1484 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 1485 | ssl->next_record_offset = 0; | 
|  | 1486 | ssl->in_epoch = 0; | 
|  | 1487 | #endif | 
|  | 1488 |  | 
|  | 1489 | /* Keep current datagram if partial == 1 */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1490 | if (partial == 0) { | 
| Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1491 | ssl->in_left = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1492 | memset(ssl->in_buf, 0, in_buf_len); | 
| Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1493 | } | 
|  | 1494 |  | 
| Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 1495 | ssl->send_alert = 0; | 
|  | 1496 |  | 
| Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1497 | /* Reset outgoing message writing */ | 
|  | 1498 | ssl->out_msgtype = 0; | 
|  | 1499 | ssl->out_msglen  = 0; | 
|  | 1500 | ssl->out_left    = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1501 | memset(ssl->out_buf, 0, out_buf_len); | 
|  | 1502 | memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr)); | 
| Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1503 | ssl->transform_out = NULL; | 
|  | 1504 |  | 
|  | 1505 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1506 | mbedtls_ssl_dtls_replay_reset(ssl); | 
| Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1507 | #endif | 
|  | 1508 |  | 
| XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1509 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1510 | if (ssl->transform) { | 
|  | 1511 | mbedtls_ssl_transform_free(ssl->transform); | 
|  | 1512 | mbedtls_free(ssl->transform); | 
| Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1513 | ssl->transform = NULL; | 
|  | 1514 | } | 
| XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1515 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 1516 |  | 
| XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1517 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1518 | mbedtls_ssl_transform_free(ssl->transform_application); | 
|  | 1519 | mbedtls_free(ssl->transform_application); | 
| XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1520 | ssl->transform_application = NULL; | 
|  | 1521 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1522 | if (ssl->handshake != NULL) { | 
| Jerry Yu | 3d9b590 | 2022-11-04 14:07:25 +0800 | [diff] [blame] | 1523 | #if defined(MBEDTLS_SSL_EARLY_DATA) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1524 | mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata); | 
|  | 1525 | mbedtls_free(ssl->handshake->transform_earlydata); | 
| XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1526 | ssl->handshake->transform_earlydata = NULL; | 
| Jerry Yu | 3d9b590 | 2022-11-04 14:07:25 +0800 | [diff] [blame] | 1527 | #endif | 
| XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1528 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1529 | mbedtls_ssl_transform_free(ssl->handshake->transform_handshake); | 
|  | 1530 | mbedtls_free(ssl->handshake->transform_handshake); | 
| XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1531 | ssl->handshake->transform_handshake = NULL; | 
|  | 1532 | } | 
|  | 1533 |  | 
| XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1534 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
| Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1535 | } | 
|  | 1536 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1537 | int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial) | 
| Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1538 | { | 
|  | 1539 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 1540 |  | 
|  | 1541 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; | 
|  | 1542 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1543 | mbedtls_ssl_session_reset_msg_layer(ssl, partial); | 
| Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1544 |  | 
|  | 1545 | /* Reset renegotiation state */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1546 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 1547 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1548 | ssl->renego_records_seen = 0; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1549 |  | 
|  | 1550 | ssl->verify_data_len = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1551 | memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN); | 
|  | 1552 | memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN); | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1553 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1554 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1555 |  | 
| Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1556 | ssl->session_in  = NULL; | 
| Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 1557 | ssl->session_out = NULL; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1558 | if (ssl->session) { | 
|  | 1559 | mbedtls_ssl_session_free(ssl->session); | 
|  | 1560 | mbedtls_free(ssl->session); | 
| Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1561 | ssl->session = NULL; | 
|  | 1562 | } | 
|  | 1563 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1564 | #if defined(MBEDTLS_SSL_ALPN) | 
| Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 1565 | ssl->alpn_chosen = NULL; | 
|  | 1566 | #endif | 
|  | 1567 |  | 
| Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1568 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) | 
| David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 1569 | int free_cli_id = 1; | 
| Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1570 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1571 | free_cli_id = (partial == 0); | 
| Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1572 | #endif | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1573 | if (free_cli_id) { | 
|  | 1574 | mbedtls_free(ssl->cli_id); | 
| Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1575 | ssl->cli_id = NULL; | 
|  | 1576 | ssl->cli_id_len = 0; | 
|  | 1577 | } | 
| Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 1578 | #endif | 
|  | 1579 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1580 | if ((ret = ssl_handshake_init(ssl)) != 0) { | 
|  | 1581 | return ret; | 
|  | 1582 | } | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1583 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1584 | return 0; | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1585 | } | 
|  | 1586 |  | 
| Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 1587 | /* | 
| Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1588 | * Reset an initialized and used SSL context for re-use while retaining | 
|  | 1589 | * all application-set variables, function pointers and data. | 
|  | 1590 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1591 | int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl) | 
| Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1592 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1593 | return mbedtls_ssl_session_reset_int(ssl, 0); | 
| Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1594 | } | 
|  | 1595 |  | 
|  | 1596 | /* | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1597 | * SSL set accessors | 
|  | 1598 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1599 | void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1600 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1601 | conf->endpoint   = endpoint; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1602 | } | 
|  | 1603 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1604 | void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport) | 
| Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 1605 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1606 | conf->transport = transport; | 
| Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 1607 | } | 
|  | 1608 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1609 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1610 | void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode) | 
| Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 1611 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1612 | conf->anti_replay = mode; | 
| Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 1613 | } | 
|  | 1614 | #endif | 
|  | 1615 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1616 | void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit) | 
| Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1617 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1618 | conf->badmac_limit = limit; | 
| Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1619 | } | 
| Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1620 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1621 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1622 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1623 | void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl, | 
|  | 1624 | unsigned allow_packing) | 
| Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1625 | { | 
|  | 1626 | ssl->disable_datagram_packing = !allow_packing; | 
|  | 1627 | } | 
|  | 1628 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1629 | void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf, | 
|  | 1630 | uint32_t min, uint32_t max) | 
| Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1631 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1632 | conf->hs_timeout_min = min; | 
|  | 1633 | conf->hs_timeout_max = max; | 
| Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1634 | } | 
|  | 1635 | #endif | 
|  | 1636 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1637 | void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1638 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1639 | conf->authmode   = authmode; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1640 | } | 
|  | 1641 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1642 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1643 | void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf, | 
|  | 1644 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), | 
|  | 1645 | void *p_vrfy) | 
| Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1646 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1647 | conf->f_vrfy      = f_vrfy; | 
|  | 1648 | conf->p_vrfy      = p_vrfy; | 
| Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1649 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1650 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1651 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1652 | void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf, | 
|  | 1653 | int (*f_rng)(void *, unsigned char *, size_t), | 
|  | 1654 | void *p_rng) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1655 | { | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1656 | conf->f_rng      = f_rng; | 
|  | 1657 | conf->p_rng      = p_rng; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1658 | } | 
|  | 1659 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1660 | void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf, | 
|  | 1661 | void (*f_dbg)(void *, int, const char *, int, const char *), | 
|  | 1662 | void  *p_dbg) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1663 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1664 | conf->f_dbg      = f_dbg; | 
|  | 1665 | conf->p_dbg      = p_dbg; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1666 | } | 
|  | 1667 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1668 | void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl, | 
|  | 1669 | void *p_bio, | 
|  | 1670 | mbedtls_ssl_send_t *f_send, | 
|  | 1671 | mbedtls_ssl_recv_t *f_recv, | 
|  | 1672 | mbedtls_ssl_recv_timeout_t *f_recv_timeout) | 
| Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1673 | { | 
|  | 1674 | ssl->p_bio          = p_bio; | 
|  | 1675 | ssl->f_send         = f_send; | 
|  | 1676 | ssl->f_recv         = f_recv; | 
|  | 1677 | ssl->f_recv_timeout = f_recv_timeout; | 
| Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 1678 | } | 
|  | 1679 |  | 
| Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 1680 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1681 | void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu) | 
| Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 1682 | { | 
|  | 1683 | ssl->mtu = mtu; | 
|  | 1684 | } | 
|  | 1685 | #endif | 
|  | 1686 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1687 | void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout) | 
| Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 1688 | { | 
|  | 1689 | conf->read_timeout   = timeout; | 
| Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1690 | } | 
|  | 1691 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1692 | void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl, | 
|  | 1693 | void *p_timer, | 
|  | 1694 | mbedtls_ssl_set_timer_t *f_set_timer, | 
|  | 1695 | mbedtls_ssl_get_timer_t *f_get_timer) | 
| Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1696 | { | 
|  | 1697 | ssl->p_timer        = p_timer; | 
|  | 1698 | ssl->f_set_timer    = f_set_timer; | 
|  | 1699 | ssl->f_get_timer    = f_get_timer; | 
| Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1700 |  | 
|  | 1701 | /* Make sure we start with no timer running */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1702 | mbedtls_ssl_set_timer(ssl, 0); | 
| Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1703 | } | 
|  | 1704 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1705 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1706 | void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf, | 
|  | 1707 | void *p_cache, | 
|  | 1708 | mbedtls_ssl_cache_get_t *f_get_cache, | 
|  | 1709 | mbedtls_ssl_cache_set_t *f_set_cache) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1710 | { | 
| Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 1711 | conf->p_cache = p_cache; | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1712 | conf->f_get_cache = f_get_cache; | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1713 | conf->f_set_cache = f_set_cache; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1714 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1715 | #endif /* MBEDTLS_SSL_SRV_C */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1716 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1717 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1718 | int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1719 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1720 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1721 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1722 | if (ssl == NULL || | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1723 | session == NULL || | 
|  | 1724 | ssl->session_negotiate == NULL || | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1725 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) { | 
|  | 1726 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1727 | } | 
|  | 1728 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1729 | if (ssl->handshake->resume == 1) { | 
|  | 1730 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
|  | 1731 | } | 
| Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 1732 |  | 
| Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1733 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1734 | if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { | 
| Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1735 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1736 | mbedtls_ssl_ciphersuite_from_id(session->ciphersuite); | 
| Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1737 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1738 | if (mbedtls_ssl_validate_ciphersuite( | 
| Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1739 | ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1740 | MBEDTLS_SSL_VERSION_TLS1_3) != 0) { | 
|  | 1741 | MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.", | 
|  | 1742 | session->ciphersuite)); | 
|  | 1743 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
| Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1744 | } | 
| Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1745 | } | 
| Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1746 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
| Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1747 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1748 | if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate, | 
|  | 1749 | session)) != 0) { | 
|  | 1750 | return ret; | 
|  | 1751 | } | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1752 |  | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1753 | ssl->handshake->resume = 1; | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1754 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1755 | return 0; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1756 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1757 | #endif /* MBEDTLS_SSL_CLI_C */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1758 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1759 | void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf, | 
|  | 1760 | const int *ciphersuites) | 
| Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1761 | { | 
| Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 1762 | conf->ciphersuite_list = ciphersuites; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1763 | } | 
|  | 1764 |  | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1765 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1766 | void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf, | 
|  | 1767 | const int kex_modes) | 
| Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1768 | { | 
| Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1769 | conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL; | 
| Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1770 | } | 
| Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame] | 1771 |  | 
|  | 1772 | #if defined(MBEDTLS_SSL_EARLY_DATA) | 
| Yanray Wang | d5ed36f | 2023-11-07 11:40:43 +0800 | [diff] [blame] | 1773 | void mbedtls_ssl_conf_early_data(mbedtls_ssl_config *conf, | 
|  | 1774 | int early_data_enabled) | 
| Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame] | 1775 | { | 
|  | 1776 | conf->early_data_enabled = early_data_enabled; | 
|  | 1777 | } | 
| Jerry Yu | cc4e007 | 2022-11-22 17:22:22 +0800 | [diff] [blame] | 1778 |  | 
|  | 1779 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Yanray Wang | 0751761 | 2023-11-07 11:47:36 +0800 | [diff] [blame] | 1780 | void mbedtls_ssl_conf_max_early_data_size( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1781 | mbedtls_ssl_config *conf, uint32_t max_early_data_size) | 
| Jerry Yu | cc4e007 | 2022-11-22 17:22:22 +0800 | [diff] [blame] | 1782 | { | 
| Jerry Yu | 39da985 | 2022-12-06 16:58:36 +0800 | [diff] [blame] | 1783 | conf->max_early_data_size = max_early_data_size; | 
| Jerry Yu | cc4e007 | 2022-11-22 17:22:22 +0800 | [diff] [blame] | 1784 | } | 
|  | 1785 | #endif /* MBEDTLS_SSL_SRV_C */ | 
|  | 1786 |  | 
| Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame] | 1787 | #endif /* MBEDTLS_SSL_EARLY_DATA */ | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1788 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
| Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1789 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1790 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1791 | void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf, | 
|  | 1792 | const mbedtls_x509_crt_profile *profile) | 
| Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1793 | { | 
|  | 1794 | conf->cert_profile = profile; | 
|  | 1795 | } | 
|  | 1796 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1797 | static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert) | 
| Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1798 | { | 
|  | 1799 | mbedtls_ssl_key_cert *cur = key_cert, *next; | 
|  | 1800 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1801 | while (cur != NULL) { | 
| Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1802 | next = cur->next; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1803 | mbedtls_free(cur); | 
| Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1804 | cur = next; | 
|  | 1805 | } | 
|  | 1806 | } | 
|  | 1807 |  | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1808 | /* Append a new keycert entry to a (possibly empty) list */ | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1809 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1810 | static int ssl_append_key_cert(mbedtls_ssl_key_cert **head, | 
|  | 1811 | mbedtls_x509_crt *cert, | 
|  | 1812 | mbedtls_pk_context *key) | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1813 | { | 
| niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1814 | mbedtls_ssl_key_cert *new_cert; | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1815 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1816 | if (cert == NULL) { | 
| Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1817 | /* Free list if cert is null */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1818 | ssl_key_cert_free(*head); | 
| Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1819 | *head = NULL; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1820 | return 0; | 
| Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1821 | } | 
|  | 1822 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1823 | new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert)); | 
|  | 1824 | if (new_cert == NULL) { | 
|  | 1825 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
|  | 1826 | } | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1827 |  | 
| niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1828 | new_cert->cert = cert; | 
|  | 1829 | new_cert->key  = key; | 
|  | 1830 | new_cert->next = NULL; | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1831 |  | 
| Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1832 | /* Update head if the list was null, else add to the end */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1833 | if (*head == NULL) { | 
| niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1834 | *head = new_cert; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1835 | } else { | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1836 | mbedtls_ssl_key_cert *cur = *head; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1837 | while (cur->next != NULL) { | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1838 | cur = cur->next; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1839 | } | 
| niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1840 | cur->next = new_cert; | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1841 | } | 
|  | 1842 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1843 | return 0; | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1844 | } | 
|  | 1845 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1846 | int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1847 | mbedtls_x509_crt *own_cert, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1848 | mbedtls_pk_context *pk_key) | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1849 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1850 | return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key); | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1851 | } | 
|  | 1852 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1853 | void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1854 | mbedtls_x509_crt *ca_chain, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1855 | mbedtls_x509_crl *ca_crl) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1856 | { | 
| Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1857 | conf->ca_chain   = ca_chain; | 
|  | 1858 | conf->ca_crl     = ca_crl; | 
| Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1859 |  | 
|  | 1860 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) | 
|  | 1861 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() | 
|  | 1862 | * cannot be used together. */ | 
|  | 1863 | conf->f_ca_cb = NULL; | 
|  | 1864 | conf->p_ca_cb = NULL; | 
|  | 1865 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1866 | } | 
| Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1867 |  | 
|  | 1868 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1869 | void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf, | 
|  | 1870 | mbedtls_x509_crt_ca_cb_t f_ca_cb, | 
|  | 1871 | void *p_ca_cb) | 
| Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1872 | { | 
|  | 1873 | conf->f_ca_cb = f_ca_cb; | 
|  | 1874 | conf->p_ca_cb = p_ca_cb; | 
|  | 1875 |  | 
|  | 1876 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() | 
|  | 1877 | * cannot be used together. */ | 
|  | 1878 | conf->ca_chain   = NULL; | 
|  | 1879 | conf->ca_crl     = NULL; | 
|  | 1880 | } | 
|  | 1881 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1882 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 1883 |  | 
| Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1884 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1885 | const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl, | 
|  | 1886 | size_t *name_len) | 
| Glenn Strauss | 6989407 | 2022-01-24 12:58:00 -0500 | [diff] [blame] | 1887 | { | 
|  | 1888 | *name_len = ssl->handshake->sni_name_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1889 | return ssl->handshake->sni_name; | 
| Glenn Strauss | 6989407 | 2022-01-24 12:58:00 -0500 | [diff] [blame] | 1890 | } | 
|  | 1891 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1892 | int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl, | 
|  | 1893 | mbedtls_x509_crt *own_cert, | 
|  | 1894 | mbedtls_pk_context *pk_key) | 
| Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1895 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1896 | return ssl_append_key_cert(&ssl->handshake->sni_key_cert, | 
|  | 1897 | own_cert, pk_key); | 
| Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1898 | } | 
| Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 1899 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1900 | void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl, | 
|  | 1901 | mbedtls_x509_crt *ca_chain, | 
|  | 1902 | mbedtls_x509_crl *ca_crl) | 
| Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 1903 | { | 
|  | 1904 | ssl->handshake->sni_ca_chain   = ca_chain; | 
|  | 1905 | ssl->handshake->sni_ca_crl     = ca_crl; | 
|  | 1906 | } | 
| Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1907 |  | 
| Glenn Strauss | 999ef70 | 2022-03-11 01:37:23 -0500 | [diff] [blame] | 1908 | #if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1909 | void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl, | 
|  | 1910 | const mbedtls_x509_crt *crt) | 
| Glenn Strauss | 999ef70 | 2022-03-11 01:37:23 -0500 | [diff] [blame] | 1911 | { | 
|  | 1912 | ssl->handshake->dn_hints = crt; | 
|  | 1913 | } | 
|  | 1914 | #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ | 
|  | 1915 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1916 | void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl, | 
|  | 1917 | int authmode) | 
| Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1918 | { | 
|  | 1919 | ssl->handshake->sni_authmode = authmode; | 
|  | 1920 | } | 
| Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1921 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ | 
|  | 1922 |  | 
| Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1923 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1924 | void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl, | 
|  | 1925 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), | 
|  | 1926 | void *p_vrfy) | 
| Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1927 | { | 
|  | 1928 | ssl->f_vrfy = f_vrfy; | 
|  | 1929 | ssl->p_vrfy = p_vrfy; | 
|  | 1930 | } | 
|  | 1931 | #endif | 
|  | 1932 |  | 
| Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1933 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1934 |  | 
| Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1935 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Przemek Stekiel | fde1128 | 2023-03-13 16:06:09 +0100 | [diff] [blame] | 1936 | static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' }; | 
|  | 1937 | static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' }; | 
|  | 1938 |  | 
| Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1939 | static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1940 | mbedtls_ssl_context *ssl, | 
|  | 1941 | mbedtls_svc_key_id_t pwd) | 
| Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1942 | { | 
|  | 1943 | psa_status_t status; | 
| Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1944 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); | 
| Przemek Stekiel | fde1128 | 2023-03-13 16:06:09 +0100 | [diff] [blame] | 1945 | const uint8_t *user = NULL; | 
| Przemek Stekiel | 4cd2031 | 2023-03-01 11:11:28 +0100 | [diff] [blame] | 1946 | size_t user_len = 0; | 
| Przemek Stekiel | fde1128 | 2023-03-13 16:06:09 +0100 | [diff] [blame] | 1947 | const uint8_t *peer = NULL; | 
| Przemek Stekiel | 4cd2031 | 2023-03-01 11:11:28 +0100 | [diff] [blame] | 1948 | size_t peer_len = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1949 | psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE); | 
|  | 1950 | psa_pake_cs_set_primitive(&cipher_suite, | 
|  | 1951 | PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, | 
|  | 1952 | PSA_ECC_FAMILY_SECP_R1, | 
|  | 1953 | 256)); | 
|  | 1954 | psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256); | 
| Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1955 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1956 | status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite); | 
|  | 1957 | if (status != PSA_SUCCESS) { | 
| Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1958 | return status; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1959 | } | 
| Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1960 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1961 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { | 
| Przemek Stekiel | fde1128 | 2023-03-13 16:06:09 +0100 | [diff] [blame] | 1962 | user = jpake_server_id; | 
|  | 1963 | user_len = sizeof(jpake_server_id); | 
|  | 1964 | peer = jpake_client_id; | 
|  | 1965 | peer_len = sizeof(jpake_client_id); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1966 | } else { | 
| Przemek Stekiel | fde1128 | 2023-03-13 16:06:09 +0100 | [diff] [blame] | 1967 | user = jpake_client_id; | 
|  | 1968 | user_len = sizeof(jpake_client_id); | 
|  | 1969 | peer = jpake_server_id; | 
|  | 1970 | peer_len = sizeof(jpake_server_id); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1971 | } | 
| Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1972 |  | 
| Przemek Stekiel | 4cd2031 | 2023-03-01 11:11:28 +0100 | [diff] [blame] | 1973 | status = psa_pake_set_user(&ssl->handshake->psa_pake_ctx, user, user_len); | 
|  | 1974 | if (status != PSA_SUCCESS) { | 
|  | 1975 | return status; | 
|  | 1976 | } | 
|  | 1977 |  | 
|  | 1978 | status = psa_pake_set_peer(&ssl->handshake->psa_pake_ctx, peer, peer_len); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1979 | if (status != PSA_SUCCESS) { | 
| Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1980 | return status; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1981 | } | 
| Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1982 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1983 | status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd); | 
|  | 1984 | if (status != PSA_SUCCESS) { | 
| Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1985 | return status; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1986 | } | 
| Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1987 |  | 
|  | 1988 | ssl->handshake->psa_pake_ctx_is_ok = 1; | 
|  | 1989 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1990 | return PSA_SUCCESS; | 
| Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1991 | } | 
|  | 1992 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1993 | int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl, | 
|  | 1994 | const unsigned char *pw, | 
|  | 1995 | size_t pw_len) | 
| Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1996 | { | 
|  | 1997 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 1998 | psa_status_t status; | 
|  | 1999 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2000 | if (ssl->handshake == NULL || ssl->conf == NULL) { | 
|  | 2001 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
| Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 2002 | } | 
|  | 2003 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2004 | /* Empty password is not valid  */ | 
|  | 2005 | if ((pw == NULL) || (pw_len == 0)) { | 
|  | 2006 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 2007 | } | 
|  | 2008 |  | 
|  | 2009 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); | 
|  | 2010 | psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE); | 
|  | 2011 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); | 
|  | 2012 |  | 
|  | 2013 | status = psa_import_key(&attributes, pw, pw_len, | 
|  | 2014 | &ssl->handshake->psa_pake_password); | 
|  | 2015 | if (status != PSA_SUCCESS) { | 
|  | 2016 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; | 
|  | 2017 | } | 
|  | 2018 |  | 
|  | 2019 | status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, | 
|  | 2020 | ssl->handshake->psa_pake_password); | 
|  | 2021 | if (status != PSA_SUCCESS) { | 
|  | 2022 | psa_destroy_key(ssl->handshake->psa_pake_password); | 
|  | 2023 | psa_pake_abort(&ssl->handshake->psa_pake_ctx); | 
|  | 2024 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; | 
|  | 2025 | } | 
|  | 2026 |  | 
|  | 2027 | return 0; | 
| Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 2028 | } | 
| Valerio Setti | a9a97dc | 2022-11-28 18:26:16 +0100 | [diff] [blame] | 2029 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2030 | int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl, | 
|  | 2031 | mbedtls_svc_key_id_t pwd) | 
| Valerio Setti | a9a97dc | 2022-11-28 18:26:16 +0100 | [diff] [blame] | 2032 | { | 
| Valerio Setti | a9a97dc | 2022-11-28 18:26:16 +0100 | [diff] [blame] | 2033 | psa_status_t status; | 
|  | 2034 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2035 | if (ssl->handshake == NULL || ssl->conf == NULL) { | 
|  | 2036 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
| Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 2037 | } | 
|  | 2038 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2039 | if (mbedtls_svc_key_id_is_null(pwd)) { | 
|  | 2040 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 2041 | } | 
|  | 2042 |  | 
|  | 2043 | status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd); | 
|  | 2044 | if (status != PSA_SUCCESS) { | 
|  | 2045 | psa_pake_abort(&ssl->handshake->psa_pake_ctx); | 
|  | 2046 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; | 
|  | 2047 | } | 
|  | 2048 |  | 
|  | 2049 | return 0; | 
| Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 2050 | } | 
|  | 2051 | #else /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2052 | int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl, | 
|  | 2053 | const unsigned char *pw, | 
|  | 2054 | size_t pw_len) | 
| Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2055 | { | 
|  | 2056 | mbedtls_ecjpake_role role; | 
|  | 2057 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2058 | if (ssl->handshake == NULL || ssl->conf == NULL) { | 
|  | 2059 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 2060 | } | 
| Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2061 |  | 
| Valerio Setti | c689ed8 | 2022-12-07 14:40:38 +0100 | [diff] [blame] | 2062 | /* Empty password is not valid  */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2063 | if ((pw == NULL) || (pw_len == 0)) { | 
|  | 2064 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 2065 | } | 
| Valerio Setti | c689ed8 | 2022-12-07 14:40:38 +0100 | [diff] [blame] | 2066 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2067 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { | 
| Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2068 | role = MBEDTLS_ECJPAKE_SERVER; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2069 | } else { | 
| Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2070 | role = MBEDTLS_ECJPAKE_CLIENT; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2071 | } | 
| Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2072 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2073 | return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx, | 
|  | 2074 | role, | 
|  | 2075 | MBEDTLS_MD_SHA256, | 
|  | 2076 | MBEDTLS_ECP_DP_SECP256R1, | 
|  | 2077 | pw, pw_len); | 
| Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2078 | } | 
| Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 2079 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 2080 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
| Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2081 |  | 
| Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 2082 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2083 | int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf) | 
| Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2084 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2085 | if (conf->psk_identity     == NULL || | 
|  | 2086 | conf->psk_identity_len == 0) { | 
|  | 2087 | return 0; | 
| Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 2088 | } | 
|  | 2089 |  | 
| Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2090 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2091 | if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) { | 
|  | 2092 | return 1; | 
|  | 2093 | } | 
| Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2094 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 2095 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2096 | if (conf->psk != NULL && conf->psk_len != 0) { | 
|  | 2097 | return 1; | 
|  | 2098 | } | 
| Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2099 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2100 | return 0; | 
| Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2101 | } | 
|  | 2102 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2103 | static void ssl_conf_remove_psk(mbedtls_ssl_config *conf) | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2104 | { | 
|  | 2105 | /* Remove reference to existing PSK, if any. */ | 
|  | 2106 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2107 | if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) { | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2108 | /* The maintenance of the PSK key slot is the | 
|  | 2109 | * user's responsibility. */ | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2110 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2111 | } | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2112 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2113 | if (conf->psk != NULL) { | 
| Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 2114 | mbedtls_zeroize_and_free(conf->psk, conf->psk_len); | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2115 | conf->psk = NULL; | 
|  | 2116 | conf->psk_len = 0; | 
|  | 2117 | } | 
|  | 2118 |  | 
|  | 2119 | /* Remove reference to PSK identity, if any. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2120 | if (conf->psk_identity != NULL) { | 
|  | 2121 | mbedtls_free(conf->psk_identity); | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2122 | conf->psk_identity = NULL; | 
|  | 2123 | conf->psk_identity_len = 0; | 
|  | 2124 | } | 
|  | 2125 | } | 
|  | 2126 |  | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2127 | /* This function assumes that PSK identity in the SSL config is unset. | 
|  | 2128 | * It checks that the provided identity is well-formed and attempts | 
|  | 2129 | * to make a copy of it in the SSL config. | 
|  | 2130 | * On failure, the PSK identity in the config remains unset. */ | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 2131 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2132 | static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf, | 
|  | 2133 | unsigned char const *psk_identity, | 
|  | 2134 | size_t psk_identity_len) | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2135 | { | 
| Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 2136 | /* Identity len will be encoded on two bytes */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2137 | if (psk_identity               == NULL || | 
| Ronald Cron | 2a87e9b | 2022-10-19 10:55:26 +0200 | [diff] [blame] | 2138 | psk_identity_len           == 0    || | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2139 | (psk_identity_len >> 16) != 0    || | 
|  | 2140 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) { | 
|  | 2141 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
| Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 2142 | } | 
|  | 2143 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2144 | conf->psk_identity = mbedtls_calloc(1, psk_identity_len); | 
|  | 2145 | if (conf->psk_identity == NULL) { | 
|  | 2146 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
|  | 2147 | } | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 2148 |  | 
| Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 2149 | conf->psk_identity_len = psk_identity_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2150 | memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len); | 
| Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 2151 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2152 | return 0; | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 2153 | } | 
|  | 2154 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2155 | int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf, | 
|  | 2156 | const unsigned char *psk, size_t psk_len, | 
|  | 2157 | const unsigned char *psk_identity, size_t psk_identity_len) | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2158 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2159 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2160 |  | 
|  | 2161 | /* We currently only support one PSK, raw or opaque. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2162 | if (mbedtls_ssl_conf_has_static_psk(conf)) { | 
|  | 2163 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
|  | 2164 | } | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2165 |  | 
|  | 2166 | /* Check and set raw PSK */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2167 | if (psk == NULL) { | 
|  | 2168 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 2169 | } | 
|  | 2170 | if (psk_len == 0) { | 
|  | 2171 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 2172 | } | 
|  | 2173 | if (psk_len > MBEDTLS_PSK_MAX_LEN) { | 
|  | 2174 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 2175 | } | 
| Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 2176 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2177 | if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) { | 
|  | 2178 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
|  | 2179 | } | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2180 | conf->psk_len = psk_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2181 | memcpy(conf->psk, psk, conf->psk_len); | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2182 |  | 
|  | 2183 | /* Check and set PSK Identity */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2184 | ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len); | 
|  | 2185 | if (ret != 0) { | 
|  | 2186 | ssl_conf_remove_psk(conf); | 
|  | 2187 | } | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2188 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2189 | return ret; | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2190 | } | 
|  | 2191 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2192 | static void ssl_remove_psk(mbedtls_ssl_context *ssl) | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2193 | { | 
|  | 2194 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2195 | if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) { | 
| Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2196 | /* The maintenance of the external PSK key slot is the | 
|  | 2197 | * user's responsibility. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2198 | if (ssl->handshake->psk_opaque_is_internal) { | 
|  | 2199 | psa_destroy_key(ssl->handshake->psk_opaque); | 
| Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2200 | ssl->handshake->psk_opaque_is_internal = 0; | 
|  | 2201 | } | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2202 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2203 | } | 
| Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 2204 | #else | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2205 | if (ssl->handshake->psk != NULL) { | 
| Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 2206 | mbedtls_zeroize_and_free(ssl->handshake->psk, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2207 | ssl->handshake->psk_len); | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2208 | ssl->handshake->psk_len = 0; | 
|  | 2209 | } | 
| Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 2210 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2211 | } | 
|  | 2212 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2213 | int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl, | 
|  | 2214 | const unsigned char *psk, size_t psk_len) | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2215 | { | 
| Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2216 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 2217 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); | 
| Jerry Yu | 5d01c05 | 2022-08-17 10:18:10 +0800 | [diff] [blame] | 2218 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Jerry Yu | 24b8c81 | 2022-08-20 19:06:56 +0800 | [diff] [blame] | 2219 | psa_algorithm_t alg = PSA_ALG_NONE; | 
| Jerry Yu | 5d01c05 | 2022-08-17 10:18:10 +0800 | [diff] [blame] | 2220 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; | 
| Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2221 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 2222 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2223 | if (psk == NULL || ssl->handshake == NULL) { | 
|  | 2224 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 2225 | } | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2226 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2227 | if (psk_len > MBEDTLS_PSK_MAX_LEN) { | 
|  | 2228 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 2229 | } | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2230 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2231 | ssl_remove_psk(ssl); | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2232 |  | 
| Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2233 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 2234 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2235 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) { | 
|  | 2236 | if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) { | 
|  | 2237 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); | 
|  | 2238 | } else { | 
|  | 2239 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); | 
|  | 2240 | } | 
|  | 2241 | psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE); | 
| Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 2242 | } | 
|  | 2243 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2244 |  | 
| Jerry Yu | 568ec25 | 2022-07-22 21:27:34 +0800 | [diff] [blame] | 2245 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2246 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { | 
|  | 2247 | alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH); | 
|  | 2248 | psa_set_key_usage_flags(&key_attributes, | 
|  | 2249 | PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT); | 
| Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 2250 | } | 
|  | 2251 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
|  | 2252 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2253 | psa_set_key_algorithm(&key_attributes, alg); | 
|  | 2254 | psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE); | 
| Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2255 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2256 | status = psa_import_key(&key_attributes, psk, psk_len, &key); | 
|  | 2257 | if (status != PSA_SUCCESS) { | 
|  | 2258 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; | 
|  | 2259 | } | 
| Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2260 |  | 
|  | 2261 | /* Allow calling psa_destroy_key() on psk remove */ | 
|  | 2262 | ssl->handshake->psk_opaque_is_internal = 1; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2263 | return mbedtls_ssl_set_hs_psk_opaque(ssl, key); | 
| Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2264 | #else | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2265 | if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) { | 
|  | 2266 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
|  | 2267 | } | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2268 |  | 
|  | 2269 | ssl->handshake->psk_len = psk_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2270 | memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len); | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2271 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2272 | return 0; | 
| Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2273 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2274 | } | 
|  | 2275 |  | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2276 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2277 | int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf, | 
|  | 2278 | mbedtls_svc_key_id_t psk, | 
|  | 2279 | const unsigned char *psk_identity, | 
|  | 2280 | size_t psk_identity_len) | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2281 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2282 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2283 |  | 
|  | 2284 | /* We currently only support one PSK, raw or opaque. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2285 | if (mbedtls_ssl_conf_has_static_psk(conf)) { | 
|  | 2286 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
|  | 2287 | } | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2288 |  | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2289 | /* Check and set opaque PSK */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2290 | if (mbedtls_svc_key_id_is_null(psk)) { | 
|  | 2291 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 2292 | } | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2293 | conf->psk_opaque = psk; | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2294 |  | 
|  | 2295 | /* Check and set PSK Identity */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2296 | ret = ssl_conf_set_psk_identity(conf, psk_identity, | 
|  | 2297 | psk_identity_len); | 
|  | 2298 | if (ret != 0) { | 
|  | 2299 | ssl_conf_remove_psk(conf); | 
|  | 2300 | } | 
| Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2301 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2302 | return ret; | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2303 | } | 
|  | 2304 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2305 | int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl, | 
|  | 2306 | mbedtls_svc_key_id_t psk) | 
| Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2307 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2308 | if ((mbedtls_svc_key_id_is_null(psk)) || | 
|  | 2309 | (ssl->handshake == NULL)) { | 
|  | 2310 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 2311 | } | 
| Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2312 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2313 | ssl_remove_psk(ssl); | 
| Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2314 | ssl->handshake->psk_opaque = psk; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2315 | return 0; | 
| Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2316 | } | 
|  | 2317 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 2318 |  | 
| Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 2319 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2320 | void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf, | 
|  | 2321 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, | 
|  | 2322 | size_t), | 
|  | 2323 | void *p_psk) | 
| Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2324 | { | 
|  | 2325 | conf->f_psk = f_psk; | 
|  | 2326 | conf->p_psk = p_psk; | 
|  | 2327 | } | 
| Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 2328 | #endif /* MBEDTLS_SSL_SRV_C */ | 
|  | 2329 |  | 
| Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 2330 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ | 
| Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2331 |  | 
|  | 2332 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2333 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2334 | psa_algorithm_t alg) | 
| Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2335 | { | 
| Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2336 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2337 | if (alg == PSA_ALG_CBC_NO_PADDING) { | 
|  | 2338 | return MBEDTLS_SSL_MODE_CBC; | 
|  | 2339 | } | 
| Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2340 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2341 | if (PSA_ALG_IS_AEAD(alg)) { | 
|  | 2342 | return MBEDTLS_SSL_MODE_AEAD; | 
|  | 2343 | } | 
|  | 2344 | return MBEDTLS_SSL_MODE_STREAM; | 
| Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2345 | } | 
|  | 2346 |  | 
|  | 2347 | #else /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 2348 |  | 
|  | 2349 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2350 | mbedtls_cipher_mode_t mode) | 
| Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2351 | { | 
|  | 2352 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2353 | if (mode == MBEDTLS_MODE_CBC) { | 
|  | 2354 | return MBEDTLS_SSL_MODE_CBC; | 
|  | 2355 | } | 
| Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2356 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ | 
|  | 2357 |  | 
| Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2358 | #if defined(MBEDTLS_GCM_C) || \ | 
|  | 2359 | defined(MBEDTLS_CCM_C) || \ | 
|  | 2360 | defined(MBEDTLS_CHACHAPOLY_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2361 | if (mode == MBEDTLS_MODE_GCM || | 
| Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2362 | mode == MBEDTLS_MODE_CCM || | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2363 | mode == MBEDTLS_MODE_CHACHAPOLY) { | 
|  | 2364 | return MBEDTLS_SSL_MODE_AEAD; | 
|  | 2365 | } | 
| Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2366 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ | 
| Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2367 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2368 | return MBEDTLS_SSL_MODE_STREAM; | 
| Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2369 | } | 
| Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2370 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2371 |  | 
| Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2372 | static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode( | 
|  | 2373 | mbedtls_ssl_mode_t base_mode, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2374 | int encrypt_then_mac) | 
| Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2375 | { | 
|  | 2376 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2377 | if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED && | 
|  | 2378 | base_mode == MBEDTLS_SSL_MODE_CBC) { | 
|  | 2379 | return MBEDTLS_SSL_MODE_CBC_ETM; | 
| Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2380 | } | 
|  | 2381 | #else | 
|  | 2382 | (void) encrypt_then_mac; | 
|  | 2383 | #endif | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2384 | return base_mode; | 
| Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2385 | } | 
|  | 2386 |  | 
| Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 2387 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2388 | const mbedtls_ssl_transform *transform) | 
| Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2389 | { | 
| Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2390 | mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode( | 
| Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2391 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2392 | transform->psa_alg | 
| Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2393 | #else | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2394 | mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc) | 
| Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2395 | #endif | 
|  | 2396 | ); | 
| Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2397 |  | 
| Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2398 | int encrypt_then_mac = 0; | 
| Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2399 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) | 
| Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2400 | encrypt_then_mac = transform->encrypt_then_mac; | 
|  | 2401 | #endif | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2402 | return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac); | 
| Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2403 | } | 
|  | 2404 |  | 
| Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 2405 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite( | 
| Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2406 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2407 | int encrypt_then_mac, | 
| Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2408 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2409 | const mbedtls_ssl_ciphersuite_t *suite) | 
| Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2410 | { | 
| Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2411 | mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM; | 
|  | 2412 |  | 
| Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2413 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 2414 | psa_status_t status; | 
|  | 2415 | psa_algorithm_t alg; | 
|  | 2416 | psa_key_type_t type; | 
|  | 2417 | size_t size; | 
| Dave Rodgman | 2eab462 | 2023-10-05 13:30:37 +0100 | [diff] [blame] | 2418 | status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) suite->cipher, | 
|  | 2419 | 0, &alg, &type, &size); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2420 | if (status == PSA_SUCCESS) { | 
|  | 2421 | base_mode = mbedtls_ssl_get_base_mode(alg); | 
|  | 2422 | } | 
| Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2423 | #else | 
|  | 2424 | const mbedtls_cipher_info_t *cipher = | 
| Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 2425 | mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) suite->cipher); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2426 | if (cipher != NULL) { | 
| Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2427 | base_mode = | 
|  | 2428 | mbedtls_ssl_get_base_mode( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2429 | mbedtls_cipher_info_get_mode(cipher)); | 
| Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2430 | } | 
| Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2431 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 2432 |  | 
| Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2433 | #if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) | 
|  | 2434 | int encrypt_then_mac = 0; | 
|  | 2435 | #endif | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2436 | return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac); | 
| Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2437 | } | 
|  | 2438 |  | 
| Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 2439 | #if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2440 |  | 
|  | 2441 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2442 | /* Serialization of TLS 1.3 sessions: | 
|  | 2443 | * | 
|  | 2444 | *     struct { | 
| Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 2445 | *       opaque hostname<0..2^16-1>; | 
| Jerry Yu | 342a555 | 2023-11-10 14:23:39 +0800 | [diff] [blame] | 2446 | *       uint64 ticket_reception_time; | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2447 | *       uint32 ticket_lifetime; | 
| Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2448 | *       opaque ticket<1..2^16-1>; | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2449 | *     } ClientOnlyData; | 
|  | 2450 | * | 
|  | 2451 | *     struct { | 
|  | 2452 | *       uint8 endpoint; | 
|  | 2453 | *       uint8 ciphersuite[2]; | 
|  | 2454 | *       uint32 ticket_age_add; | 
|  | 2455 | *       uint8 ticket_flags; | 
|  | 2456 | *       opaque resumption_key<0..255>; | 
| Jerry Yu | 02e3a07 | 2022-12-12 15:13:20 +0800 | [diff] [blame] | 2457 | *       uint32 max_early_data_size; | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2458 | *       select ( endpoint ) { | 
|  | 2459 | *            case client: ClientOnlyData; | 
| Jerry Yu | cf91351 | 2023-11-14 11:06:52 +0800 | [diff] [blame] | 2460 | *            case server: uint64 ticket_creation_time; | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2461 | *        }; | 
|  | 2462 | *     } serialized_session_tls13; | 
|  | 2463 | * | 
|  | 2464 | */ | 
|  | 2465 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
| Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2466 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2467 | static int ssl_tls13_session_save(const mbedtls_ssl_session *session, | 
|  | 2468 | unsigned char *buf, | 
|  | 2469 | size_t buf_len, | 
|  | 2470 | size_t *olen) | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2471 | { | 
|  | 2472 | unsigned char *p = buf; | 
| Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 2473 | #if defined(MBEDTLS_SSL_CLI_C) && \ | 
|  | 2474 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2475 | size_t hostname_len = (session->hostname == NULL) ? | 
|  | 2476 | 0 : strlen(session->hostname) + 1; | 
| Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 2477 | #endif | 
| Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2478 | size_t needed =   1                             /* endpoint */ | 
|  | 2479 | + 2                             /* ciphersuite */ | 
|  | 2480 | + 4                             /* ticket_age_add */ | 
| Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2481 | + 1                             /* ticket_flags */ | 
|  | 2482 | + 1;                            /* resumption_key length */ | 
| Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2483 | *olen = 0; | 
| Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2484 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2485 | if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) { | 
|  | 2486 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 2487 | } | 
| Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2488 | needed += session->resumption_key_len;  /* resumption_key */ | 
|  | 2489 |  | 
| Jerry Yu | 02e3a07 | 2022-12-12 15:13:20 +0800 | [diff] [blame] | 2490 | #if defined(MBEDTLS_SSL_EARLY_DATA) | 
|  | 2491 | needed += 4;                            /* max_early_data_size */ | 
|  | 2492 | #endif | 
|  | 2493 |  | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2494 | #if defined(MBEDTLS_HAVE_TIME) | 
| Jerry Yu | cf91351 | 2023-11-14 11:06:52 +0800 | [diff] [blame] | 2495 | needed += 8; /* ticket_creation_time or ticket_reception_time */ | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2496 | #endif | 
|  | 2497 |  | 
|  | 2498 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2499 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { | 
| Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 2500 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| Xiaokang Qian | bc663a0 | 2022-10-09 11:14:39 +0000 | [diff] [blame] | 2501 | needed +=  2                        /* hostname_len */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2502 | + hostname_len;           /* hostname */ | 
| Xiaokang Qian | 281fd1b | 2022-09-20 11:35:41 +0000 | [diff] [blame] | 2503 | #endif | 
|  | 2504 |  | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2505 | needed +=   4                       /* ticket_lifetime */ | 
| Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2506 | + 2;                      /* ticket_len */ | 
| Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2507 |  | 
|  | 2508 | /* Check size_t overflow */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2509 | if (session->ticket_len > SIZE_MAX - needed) { | 
|  | 2510 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 2511 | } | 
| Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2512 |  | 
| Jerry Yu | e28d974 | 2022-08-18 15:44:03 +0800 | [diff] [blame] | 2513 | needed += session->ticket_len;      /* ticket */ | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2514 | } | 
|  | 2515 | #endif /* MBEDTLS_SSL_CLI_C */ | 
|  | 2516 |  | 
| Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2517 | *olen = needed; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2518 | if (needed > buf_len) { | 
|  | 2519 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; | 
|  | 2520 | } | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2521 |  | 
|  | 2522 | p[0] = session->endpoint; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2523 | MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 1); | 
|  | 2524 | MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 3); | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2525 | p[7] = session->ticket_flags; | 
|  | 2526 |  | 
|  | 2527 | /* save resumption_key */ | 
| Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2528 | p[8] = session->resumption_key_len; | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2529 | p += 9; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2530 | memcpy(p, session->resumption_key, session->resumption_key_len); | 
| Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2531 | p += session->resumption_key_len; | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2532 |  | 
| Jerry Yu | 02e3a07 | 2022-12-12 15:13:20 +0800 | [diff] [blame] | 2533 | #if defined(MBEDTLS_SSL_EARLY_DATA) | 
|  | 2534 | MBEDTLS_PUT_UINT32_BE(session->max_early_data_size, p, 0); | 
|  | 2535 | p += 4; | 
|  | 2536 | #endif | 
|  | 2537 |  | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2538 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2539 | if (session->endpoint == MBEDTLS_SSL_IS_SERVER) { | 
| Jerry Yu | 25ba4d4 | 2023-11-10 14:12:20 +0800 | [diff] [blame] | 2540 | MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_creation_time, p, 0); | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2541 | p += 8; | 
|  | 2542 | } | 
|  | 2543 | #endif /* MBEDTLS_HAVE_TIME */ | 
|  | 2544 |  | 
|  | 2545 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2546 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { | 
| Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2547 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2548 | MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0); | 
| Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2549 | p += 2; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2550 | if (hostname_len > 0) { | 
| Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2551 | /* save host name */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2552 | memcpy(p, session->hostname, hostname_len); | 
| Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2553 | p += hostname_len; | 
|  | 2554 | } | 
|  | 2555 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ | 
|  | 2556 |  | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2557 | #if defined(MBEDTLS_HAVE_TIME) | 
| Jerry Yu | 342a555 | 2023-11-10 14:23:39 +0800 | [diff] [blame] | 2558 | MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_reception_time, p, 0); | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2559 | p += 8; | 
|  | 2560 | #endif | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2561 | MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0); | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2562 | p += 4; | 
|  | 2563 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2564 | MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0); | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2565 | p += 2; | 
| Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2566 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2567 | if (session->ticket != NULL && session->ticket_len > 0) { | 
|  | 2568 | memcpy(p, session->ticket, session->ticket_len); | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2569 | p += session->ticket_len; | 
|  | 2570 | } | 
|  | 2571 | } | 
|  | 2572 | #endif /* MBEDTLS_SSL_CLI_C */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2573 | return 0; | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2574 | } | 
|  | 2575 |  | 
|  | 2576 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2577 | static int ssl_tls13_session_load(mbedtls_ssl_session *session, | 
|  | 2578 | const unsigned char *buf, | 
|  | 2579 | size_t len) | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2580 | { | 
|  | 2581 | const unsigned char *p = buf; | 
|  | 2582 | const unsigned char *end = buf + len; | 
|  | 2583 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2584 | if (end - p < 9) { | 
|  | 2585 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 2586 | } | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2587 | session->endpoint = p[0]; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2588 | session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 1); | 
|  | 2589 | session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 3); | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2590 | session->ticket_flags = p[7]; | 
|  | 2591 |  | 
|  | 2592 | /* load resumption_key */ | 
| Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2593 | session->resumption_key_len = p[8]; | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2594 | p += 9; | 
|  | 2595 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2596 | if (end - p < session->resumption_key_len) { | 
|  | 2597 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 2598 | } | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2599 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2600 | if (sizeof(session->resumption_key) < session->resumption_key_len) { | 
|  | 2601 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 2602 | } | 
|  | 2603 | memcpy(session->resumption_key, p, session->resumption_key_len); | 
| Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2604 | p += session->resumption_key_len; | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2605 |  | 
| Jerry Yu | 02e3a07 | 2022-12-12 15:13:20 +0800 | [diff] [blame] | 2606 | #if defined(MBEDTLS_SSL_EARLY_DATA) | 
|  | 2607 | if (end - p < 4) { | 
|  | 2608 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 2609 | } | 
|  | 2610 | session->max_early_data_size = MBEDTLS_GET_UINT32_BE(p, 0); | 
|  | 2611 | p += 4; | 
|  | 2612 | #endif | 
|  | 2613 |  | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2614 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2615 | if (session->endpoint == MBEDTLS_SSL_IS_SERVER) { | 
|  | 2616 | if (end - p < 8) { | 
|  | 2617 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 2618 | } | 
| Jerry Yu | 25ba4d4 | 2023-11-10 14:12:20 +0800 | [diff] [blame] | 2619 | session->ticket_creation_time = MBEDTLS_GET_UINT64_BE(p, 0); | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2620 | p += 8; | 
|  | 2621 | } | 
|  | 2622 | #endif /* MBEDTLS_HAVE_TIME */ | 
|  | 2623 |  | 
|  | 2624 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2625 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { | 
| Yanray Wang | fd25654 | 2023-11-22 10:32:03 +0800 | [diff] [blame] | 2626 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2627 | size_t hostname_len; | 
|  | 2628 | /* load host name */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2629 | if (end - p < 2) { | 
|  | 2630 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 2631 | } | 
|  | 2632 | hostname_len = MBEDTLS_GET_UINT16_BE(p, 0); | 
| Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2633 | p += 2; | 
|  | 2634 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2635 | if (end - p < (long int) hostname_len) { | 
|  | 2636 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 2637 | } | 
|  | 2638 | if (hostname_len > 0) { | 
|  | 2639 | session->hostname = mbedtls_calloc(1, hostname_len); | 
|  | 2640 | if (session->hostname == NULL) { | 
|  | 2641 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
|  | 2642 | } | 
|  | 2643 | memcpy(session->hostname, p, hostname_len); | 
| Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2644 | p += hostname_len; | 
|  | 2645 | } | 
| Yanray Wang | fd25654 | 2023-11-22 10:32:03 +0800 | [diff] [blame] | 2646 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ | 
| Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2647 |  | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2648 | #if defined(MBEDTLS_HAVE_TIME) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2649 | if (end - p < 8) { | 
|  | 2650 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 2651 | } | 
| Jerry Yu | 342a555 | 2023-11-10 14:23:39 +0800 | [diff] [blame] | 2652 | session->ticket_reception_time = MBEDTLS_GET_UINT64_BE(p, 0); | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2653 | p += 8; | 
|  | 2654 | #endif | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2655 | if (end - p < 4) { | 
|  | 2656 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 2657 | } | 
|  | 2658 | session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0); | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2659 | p += 4; | 
|  | 2660 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2661 | if (end - p <  2) { | 
|  | 2662 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 2663 | } | 
|  | 2664 | session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0); | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2665 | p += 2; | 
|  | 2666 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2667 | if (end - p < (long int) session->ticket_len) { | 
|  | 2668 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 2669 | } | 
|  | 2670 | if (session->ticket_len > 0) { | 
|  | 2671 | session->ticket = mbedtls_calloc(1, session->ticket_len); | 
|  | 2672 | if (session->ticket == NULL) { | 
|  | 2673 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
|  | 2674 | } | 
|  | 2675 | memcpy(session->ticket, p, session->ticket_len); | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2676 | p += session->ticket_len; | 
|  | 2677 | } | 
|  | 2678 | } | 
|  | 2679 | #endif /* MBEDTLS_SSL_CLI_C */ | 
|  | 2680 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2681 | return 0; | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2682 |  | 
|  | 2683 | } | 
|  | 2684 | #else /* MBEDTLS_SSL_SESSION_TICKETS */ | 
| Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2685 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2686 | static int ssl_tls13_session_save(const mbedtls_ssl_session *session, | 
|  | 2687 | unsigned char *buf, | 
|  | 2688 | size_t buf_len, | 
|  | 2689 | size_t *olen) | 
| Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2690 | { | 
|  | 2691 | ((void) session); | 
|  | 2692 | ((void) buf); | 
|  | 2693 | ((void) buf_len); | 
| Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2694 | *olen = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2695 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2696 | } | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2697 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2698 | static int ssl_tls13_session_load(const mbedtls_ssl_session *session, | 
|  | 2699 | unsigned char *buf, | 
|  | 2700 | size_t buf_len) | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2701 | { | 
|  | 2702 | ((void) session); | 
|  | 2703 | ((void) buf); | 
|  | 2704 | ((void) buf_len); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2705 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2706 | } | 
|  | 2707 | #endif /* !MBEDTLS_SSL_SESSION_TICKETS */ | 
| Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2708 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
|  | 2709 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2710 | psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type, | 
|  | 2711 | size_t taglen, | 
|  | 2712 | psa_algorithm_t *alg, | 
|  | 2713 | psa_key_type_t *key_type, | 
|  | 2714 | size_t *key_size) | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2715 | { | 
| Yanray Wang | 19e4dc8 | 2023-11-16 18:02:05 +0800 | [diff] [blame] | 2716 | #if !defined(MBEDTLS_SSL_HAVE_CCM) | 
|  | 2717 | (void) taglen; | 
|  | 2718 | #endif | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2719 | switch (mbedtls_cipher_type) { | 
| Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2720 | #if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CBC) | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2721 | case MBEDTLS_CIPHER_AES_128_CBC: | 
|  | 2722 | *alg = PSA_ALG_CBC_NO_PADDING; | 
|  | 2723 | *key_type = PSA_KEY_TYPE_AES; | 
|  | 2724 | *key_size = 128; | 
|  | 2725 | break; | 
| Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2726 | #endif | 
|  | 2727 | #if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CCM) | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2728 | case MBEDTLS_CIPHER_AES_128_CCM: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2729 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM; | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2730 | *key_type = PSA_KEY_TYPE_AES; | 
|  | 2731 | *key_size = 128; | 
|  | 2732 | break; | 
| Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2733 | #endif | 
|  | 2734 | #if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_GCM) | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2735 | case MBEDTLS_CIPHER_AES_128_GCM: | 
|  | 2736 | *alg = PSA_ALG_GCM; | 
|  | 2737 | *key_type = PSA_KEY_TYPE_AES; | 
|  | 2738 | *key_size = 128; | 
|  | 2739 | break; | 
| Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2740 | #endif | 
|  | 2741 | #if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CCM) | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2742 | case MBEDTLS_CIPHER_AES_192_CCM: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2743 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM; | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2744 | *key_type = PSA_KEY_TYPE_AES; | 
|  | 2745 | *key_size = 192; | 
|  | 2746 | break; | 
| Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2747 | #endif | 
|  | 2748 | #if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_GCM) | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2749 | case MBEDTLS_CIPHER_AES_192_GCM: | 
|  | 2750 | *alg = PSA_ALG_GCM; | 
|  | 2751 | *key_type = PSA_KEY_TYPE_AES; | 
|  | 2752 | *key_size = 192; | 
|  | 2753 | break; | 
| Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2754 | #endif | 
|  | 2755 | #if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CBC) | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2756 | case MBEDTLS_CIPHER_AES_256_CBC: | 
|  | 2757 | *alg = PSA_ALG_CBC_NO_PADDING; | 
|  | 2758 | *key_type = PSA_KEY_TYPE_AES; | 
|  | 2759 | *key_size = 256; | 
|  | 2760 | break; | 
| Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2761 | #endif | 
|  | 2762 | #if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CCM) | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2763 | case MBEDTLS_CIPHER_AES_256_CCM: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2764 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM; | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2765 | *key_type = PSA_KEY_TYPE_AES; | 
|  | 2766 | *key_size = 256; | 
|  | 2767 | break; | 
| Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2768 | #endif | 
|  | 2769 | #if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_GCM) | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2770 | case MBEDTLS_CIPHER_AES_256_GCM: | 
|  | 2771 | *alg = PSA_ALG_GCM; | 
|  | 2772 | *key_type = PSA_KEY_TYPE_AES; | 
|  | 2773 | *key_size = 256; | 
|  | 2774 | break; | 
| Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2775 | #endif | 
|  | 2776 | #if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CBC) | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2777 | case MBEDTLS_CIPHER_ARIA_128_CBC: | 
|  | 2778 | *alg = PSA_ALG_CBC_NO_PADDING; | 
|  | 2779 | *key_type = PSA_KEY_TYPE_ARIA; | 
|  | 2780 | *key_size = 128; | 
|  | 2781 | break; | 
| Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2782 | #endif | 
|  | 2783 | #if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CCM) | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2784 | case MBEDTLS_CIPHER_ARIA_128_CCM: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2785 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM; | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2786 | *key_type = PSA_KEY_TYPE_ARIA; | 
|  | 2787 | *key_size = 128; | 
|  | 2788 | break; | 
| Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2789 | #endif | 
|  | 2790 | #if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_GCM) | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2791 | case MBEDTLS_CIPHER_ARIA_128_GCM: | 
|  | 2792 | *alg = PSA_ALG_GCM; | 
|  | 2793 | *key_type = PSA_KEY_TYPE_ARIA; | 
|  | 2794 | *key_size = 128; | 
|  | 2795 | break; | 
| Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2796 | #endif | 
|  | 2797 | #if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CCM) | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2798 | case MBEDTLS_CIPHER_ARIA_192_CCM: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2799 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM; | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2800 | *key_type = PSA_KEY_TYPE_ARIA; | 
|  | 2801 | *key_size = 192; | 
|  | 2802 | break; | 
| Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2803 | #endif | 
|  | 2804 | #if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_GCM) | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2805 | case MBEDTLS_CIPHER_ARIA_192_GCM: | 
|  | 2806 | *alg = PSA_ALG_GCM; | 
|  | 2807 | *key_type = PSA_KEY_TYPE_ARIA; | 
|  | 2808 | *key_size = 192; | 
|  | 2809 | break; | 
| Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2810 | #endif | 
|  | 2811 | #if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CBC) | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2812 | case MBEDTLS_CIPHER_ARIA_256_CBC: | 
|  | 2813 | *alg = PSA_ALG_CBC_NO_PADDING; | 
|  | 2814 | *key_type = PSA_KEY_TYPE_ARIA; | 
|  | 2815 | *key_size = 256; | 
|  | 2816 | break; | 
| Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2817 | #endif | 
|  | 2818 | #if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CCM) | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2819 | case MBEDTLS_CIPHER_ARIA_256_CCM: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2820 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM; | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2821 | *key_type = PSA_KEY_TYPE_ARIA; | 
|  | 2822 | *key_size = 256; | 
|  | 2823 | break; | 
| Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2824 | #endif | 
|  | 2825 | #if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_GCM) | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2826 | case MBEDTLS_CIPHER_ARIA_256_GCM: | 
|  | 2827 | *alg = PSA_ALG_GCM; | 
|  | 2828 | *key_type = PSA_KEY_TYPE_ARIA; | 
|  | 2829 | *key_size = 256; | 
|  | 2830 | break; | 
| Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2831 | #endif | 
|  | 2832 | #if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CBC) | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2833 | case MBEDTLS_CIPHER_CAMELLIA_128_CBC: | 
|  | 2834 | *alg = PSA_ALG_CBC_NO_PADDING; | 
|  | 2835 | *key_type = PSA_KEY_TYPE_CAMELLIA; | 
|  | 2836 | *key_size = 128; | 
|  | 2837 | break; | 
| Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2838 | #endif | 
|  | 2839 | #if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CCM) | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2840 | case MBEDTLS_CIPHER_CAMELLIA_128_CCM: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2841 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM; | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2842 | *key_type = PSA_KEY_TYPE_CAMELLIA; | 
|  | 2843 | *key_size = 128; | 
|  | 2844 | break; | 
| Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2845 | #endif | 
|  | 2846 | #if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_GCM) | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2847 | case MBEDTLS_CIPHER_CAMELLIA_128_GCM: | 
|  | 2848 | *alg = PSA_ALG_GCM; | 
|  | 2849 | *key_type = PSA_KEY_TYPE_CAMELLIA; | 
|  | 2850 | *key_size = 128; | 
|  | 2851 | break; | 
| Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2852 | #endif | 
|  | 2853 | #if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CCM) | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2854 | case MBEDTLS_CIPHER_CAMELLIA_192_CCM: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2855 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM; | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2856 | *key_type = PSA_KEY_TYPE_CAMELLIA; | 
|  | 2857 | *key_size = 192; | 
|  | 2858 | break; | 
| Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2859 | #endif | 
|  | 2860 | #if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_GCM) | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2861 | case MBEDTLS_CIPHER_CAMELLIA_192_GCM: | 
|  | 2862 | *alg = PSA_ALG_GCM; | 
|  | 2863 | *key_type = PSA_KEY_TYPE_CAMELLIA; | 
|  | 2864 | *key_size = 192; | 
|  | 2865 | break; | 
| Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2866 | #endif | 
|  | 2867 | #if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CBC) | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2868 | case MBEDTLS_CIPHER_CAMELLIA_256_CBC: | 
|  | 2869 | *alg = PSA_ALG_CBC_NO_PADDING; | 
|  | 2870 | *key_type = PSA_KEY_TYPE_CAMELLIA; | 
|  | 2871 | *key_size = 256; | 
|  | 2872 | break; | 
| Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2873 | #endif | 
|  | 2874 | #if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CCM) | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2875 | case MBEDTLS_CIPHER_CAMELLIA_256_CCM: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2876 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM; | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2877 | *key_type = PSA_KEY_TYPE_CAMELLIA; | 
|  | 2878 | *key_size = 256; | 
|  | 2879 | break; | 
| Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2880 | #endif | 
|  | 2881 | #if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_GCM) | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2882 | case MBEDTLS_CIPHER_CAMELLIA_256_GCM: | 
|  | 2883 | *alg = PSA_ALG_GCM; | 
|  | 2884 | *key_type = PSA_KEY_TYPE_CAMELLIA; | 
|  | 2885 | *key_size = 256; | 
|  | 2886 | break; | 
| Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2887 | #endif | 
|  | 2888 | #if defined(MBEDTLS_SSL_HAVE_CHACHAPOLY) | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2889 | case MBEDTLS_CIPHER_CHACHA20_POLY1305: | 
|  | 2890 | *alg = PSA_ALG_CHACHA20_POLY1305; | 
|  | 2891 | *key_type = PSA_KEY_TYPE_CHACHA20; | 
|  | 2892 | *key_size = 256; | 
|  | 2893 | break; | 
| Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2894 | #endif | 
| Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2895 | case MBEDTLS_CIPHER_NULL: | 
|  | 2896 | *alg = MBEDTLS_SSL_NULL_CIPHER; | 
|  | 2897 | *key_type = 0; | 
|  | 2898 | *key_size = 0; | 
|  | 2899 | break; | 
|  | 2900 | default: | 
|  | 2901 | return PSA_ERROR_NOT_SUPPORTED; | 
|  | 2902 | } | 
|  | 2903 |  | 
|  | 2904 | return PSA_SUCCESS; | 
|  | 2905 | } | 
| Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 2906 | #endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */ | 
| Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2907 |  | 
| Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2908 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2909 | int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf, | 
|  | 2910 | const unsigned char *dhm_P, size_t P_len, | 
|  | 2911 | const unsigned char *dhm_G, size_t G_len) | 
| Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2912 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2913 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2914 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2915 | mbedtls_mpi_free(&conf->dhm_P); | 
|  | 2916 | mbedtls_mpi_free(&conf->dhm_G); | 
| Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2917 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2918 | if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 || | 
|  | 2919 | (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) { | 
|  | 2920 | mbedtls_mpi_free(&conf->dhm_P); | 
|  | 2921 | mbedtls_mpi_free(&conf->dhm_G); | 
|  | 2922 | return ret; | 
| Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2923 | } | 
|  | 2924 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2925 | return 0; | 
| Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2926 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2927 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2928 | int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx) | 
| Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2929 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2930 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2931 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2932 | mbedtls_mpi_free(&conf->dhm_P); | 
|  | 2933 | mbedtls_mpi_free(&conf->dhm_G); | 
| Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2934 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2935 | if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P, | 
|  | 2936 | &conf->dhm_P)) != 0 || | 
|  | 2937 | (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G, | 
|  | 2938 | &conf->dhm_G)) != 0) { | 
|  | 2939 | mbedtls_mpi_free(&conf->dhm_P); | 
|  | 2940 | mbedtls_mpi_free(&conf->dhm_G); | 
|  | 2941 | return ret; | 
| Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 2942 | } | 
| Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2943 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2944 | return 0; | 
| Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2945 | } | 
| Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2946 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ | 
| Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2947 |  | 
| Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 2948 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 2949 | /* | 
|  | 2950 | * Set the minimum length for Diffie-Hellman parameters | 
|  | 2951 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2952 | void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf, | 
|  | 2953 | unsigned int bitlen) | 
| Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 2954 | { | 
|  | 2955 | conf->dhm_min_bitlen = bitlen; | 
|  | 2956 | } | 
|  | 2957 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ | 
|  | 2958 |  | 
| Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2959 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) | 
| Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2960 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 2961 | /* | 
|  | 2962 | * Set allowed/preferred hashes for handshake signatures | 
|  | 2963 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2964 | void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf, | 
|  | 2965 | const int *hashes) | 
| Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 2966 | { | 
|  | 2967 | conf->sig_hashes = hashes; | 
|  | 2968 | } | 
| Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2969 | #endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2970 |  | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2971 | /* Configure allowed signature algorithms for handshake */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2972 | void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf, | 
|  | 2973 | const uint16_t *sig_algs) | 
| Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2974 | { | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2975 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
|  | 2976 | conf->sig_hashes = NULL; | 
|  | 2977 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ | 
|  | 2978 | conf->sig_algs = sig_algs; | 
| Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2979 | } | 
| Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2980 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ | 
| Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 2981 |  | 
| Manuel Pégourié-Gonnard | f07ce3b | 2023-09-22 11:53:41 +0200 | [diff] [blame] | 2982 | #if defined(MBEDTLS_ECP_C) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2983 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
| Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2984 | /* | 
|  | 2985 | * Set the allowed elliptic curves | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2986 | * | 
|  | 2987 | * mbedtls_ssl_setup() takes the provided list | 
|  | 2988 | * and translates it to a list of IANA TLS group identifiers, | 
|  | 2989 | * stored in ssl->handshake->group_list. | 
|  | 2990 | * | 
| Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2991 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2992 | void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf, | 
|  | 2993 | const mbedtls_ecp_group_id *curve_list) | 
| Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2994 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2995 | conf->curve_list = curve_list; | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2996 | conf->group_list = NULL; | 
| Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2997 | } | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2998 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ | 
| Manuel Pégourié-Gonnard | f07ce3b | 2023-09-22 11:53:41 +0200 | [diff] [blame] | 2999 | #endif /* MBEDTLS_ECP_C */ | 
| Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 3000 |  | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3001 | /* | 
|  | 3002 | * Set the allowed groups | 
|  | 3003 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3004 | void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf, | 
|  | 3005 | const uint16_t *group_list) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3006 | { | 
| Manuel Pégourié-Gonnard | f07ce3b | 2023-09-22 11:53:41 +0200 | [diff] [blame] | 3007 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3008 | conf->curve_list = NULL; | 
|  | 3009 | #endif | 
|  | 3010 | conf->group_list = group_list; | 
|  | 3011 | } | 
|  | 3012 |  | 
| Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 3013 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3014 | int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3015 | { | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 3016 | /* Initialize to suppress unnecessary compiler warning */ | 
|  | 3017 | size_t hostname_len = 0; | 
|  | 3018 |  | 
|  | 3019 | /* Check if new hostname is valid before | 
|  | 3020 | * making any change to current one */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3021 | if (hostname != NULL) { | 
|  | 3022 | hostname_len = strlen(hostname); | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 3023 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3024 | if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) { | 
|  | 3025 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 3026 | } | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 3027 | } | 
|  | 3028 |  | 
|  | 3029 | /* Now it's clear that we will overwrite the old hostname, | 
|  | 3030 | * so we can free it safely */ | 
|  | 3031 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3032 | if (ssl->hostname != NULL) { | 
| Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 3033 | mbedtls_zeroize_and_free(ssl->hostname, strlen(ssl->hostname)); | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 3034 | } | 
|  | 3035 |  | 
|  | 3036 | /* Passing NULL as hostname shall clear the old one */ | 
| Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 3037 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3038 | if (hostname == NULL) { | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 3039 | ssl->hostname = NULL; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3040 | } else { | 
|  | 3041 | ssl->hostname = mbedtls_calloc(1, hostname_len + 1); | 
|  | 3042 | if (ssl->hostname == NULL) { | 
|  | 3043 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
|  | 3044 | } | 
| Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 3045 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3046 | memcpy(ssl->hostname, hostname, hostname_len); | 
| Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 3047 |  | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 3048 | ssl->hostname[hostname_len] = '\0'; | 
|  | 3049 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3050 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3051 | return 0; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3052 | } | 
| Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 3053 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3054 |  | 
| Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 3055 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3056 | void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf, | 
|  | 3057 | int (*f_sni)(void *, mbedtls_ssl_context *, | 
|  | 3058 | const unsigned char *, size_t), | 
|  | 3059 | void *p_sni) | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 3060 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3061 | conf->f_sni = f_sni; | 
|  | 3062 | conf->p_sni = p_sni; | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 3063 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3064 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 3065 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3066 | #if defined(MBEDTLS_SSL_ALPN) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3067 | int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos) | 
| Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 3068 | { | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 3069 | size_t cur_len, tot_len; | 
|  | 3070 | const char **p; | 
|  | 3071 |  | 
|  | 3072 | /* | 
| Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 3073 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings | 
|  | 3074 | * MUST NOT be truncated." | 
|  | 3075 | * We check lengths now rather than later. | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 3076 | */ | 
|  | 3077 | tot_len = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3078 | for (p = protos; *p != NULL; p++) { | 
|  | 3079 | cur_len = strlen(*p); | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 3080 | tot_len += cur_len; | 
|  | 3081 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3082 | if ((cur_len == 0) || | 
|  | 3083 | (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) || | 
|  | 3084 | (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) { | 
|  | 3085 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 3086 | } | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 3087 | } | 
|  | 3088 |  | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3089 | conf->alpn_list = protos; | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 3090 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3091 | return 0; | 
| Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 3092 | } | 
|  | 3093 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3094 | const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl) | 
| Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 3095 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3096 | return ssl->alpn_chosen; | 
| Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 3097 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3098 | #endif /* MBEDTLS_SSL_ALPN */ | 
| Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 3099 |  | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3100 | #if defined(MBEDTLS_SSL_DTLS_SRTP) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3101 | void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf, | 
|  | 3102 | int support_mki_value) | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 3103 | { | 
|  | 3104 | conf->dtls_srtp_mki_support = support_mki_value; | 
|  | 3105 | } | 
|  | 3106 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3107 | int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl, | 
|  | 3108 | unsigned char *mki_value, | 
|  | 3109 | uint16_t mki_len) | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 3110 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3111 | if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) { | 
|  | 3112 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
| Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 3113 | } | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 3114 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3115 | if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) { | 
|  | 3116 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 3117 | } | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 3118 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3119 | memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len); | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 3120 | ssl->dtls_srtp_info.mki_len = mki_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3121 | return 0; | 
| Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 3122 | } | 
|  | 3123 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3124 | int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf, | 
|  | 3125 | const mbedtls_ssl_srtp_profile *profiles) | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3126 | { | 
| Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 3127 | const mbedtls_ssl_srtp_profile *p; | 
|  | 3128 | size_t list_size = 0; | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3129 |  | 
| Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 3130 | /* check the profiles list: all entry must be valid, | 
|  | 3131 | * its size cannot be more than the total number of supported profiles, currently 4 */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3132 | for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET && | 
|  | 3133 | list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH; | 
|  | 3134 | p++) { | 
|  | 3135 | if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) { | 
| Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 3136 | list_size++; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3137 | } else { | 
| Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 3138 | /* unsupported value, stop parsing and set the size to an error value */ | 
|  | 3139 | list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1; | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3140 | } | 
|  | 3141 | } | 
|  | 3142 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3143 | if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) { | 
|  | 3144 | conf->dtls_srtp_profile_list = NULL; | 
|  | 3145 | conf->dtls_srtp_profile_list_len = 0; | 
|  | 3146 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
| Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 3147 | } | 
|  | 3148 |  | 
| Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 3149 | conf->dtls_srtp_profile_list = profiles; | 
| Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 3150 | conf->dtls_srtp_profile_list_len = list_size; | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3151 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3152 | return 0; | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3153 | } | 
|  | 3154 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3155 | void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl, | 
|  | 3156 | mbedtls_dtls_srtp_info *dtls_srtp_info) | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3157 | { | 
| Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 3158 | dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile; | 
|  | 3159 | /* do not copy the mki value if there is no chosen profile */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3160 | if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) { | 
| Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 3161 | dtls_srtp_info->mki_len = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3162 | } else { | 
| Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 3163 | dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3164 | memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value, | 
|  | 3165 | ssl->dtls_srtp_info.mki_len); | 
| Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 3166 | } | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3167 | } | 
| Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3168 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ | 
|  | 3169 |  | 
| Aditya Patwardhan | 3096f33 | 2022-07-26 14:31:46 +0530 | [diff] [blame] | 3170 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3171 | void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor) | 
| Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 3172 | { | 
| Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 3173 | conf->max_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor); | 
| Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 3174 | } | 
|  | 3175 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3176 | void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor) | 
| Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 3177 | { | 
| Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 3178 | conf->min_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor); | 
| Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 3179 | } | 
| Aditya Patwardhan | 3096f33 | 2022-07-26 14:31:46 +0530 | [diff] [blame] | 3180 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ | 
| Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 3181 |  | 
| Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3182 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3183 | void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf, | 
|  | 3184 | char cert_req_ca_list) | 
| Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3185 | { | 
|  | 3186 | conf->cert_req_ca_list = cert_req_ca_list; | 
|  | 3187 | } | 
|  | 3188 | #endif | 
|  | 3189 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3190 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3191 | void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm) | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 3192 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3193 | conf->encrypt_then_mac = etm; | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 3194 | } | 
|  | 3195 | #endif | 
|  | 3196 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3197 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3198 | void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems) | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 3199 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3200 | conf->extended_ms = ems; | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 3201 | } | 
|  | 3202 | #endif | 
|  | 3203 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3204 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3205 | int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code) | 
| Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 3206 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3207 | if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || | 
|  | 3208 | ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) { | 
|  | 3209 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
| Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 3210 | } | 
|  | 3211 |  | 
| Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 3212 | conf->mfl_code = mfl_code; | 
| Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 3213 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3214 | return 0; | 
| Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 3215 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3216 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ | 
| Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 3217 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3218 | void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3219 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3220 | conf->allow_legacy_renegotiation = allow_legacy; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3221 | } | 
|  | 3222 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3223 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3224 | void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation) | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 3225 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3226 | conf->disable_renegotiation = renegotiation; | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 3227 | } | 
|  | 3228 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3229 | void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records) | 
| Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 3230 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3231 | conf->renego_max_records = max_records; | 
| Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 3232 | } | 
|  | 3233 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3234 | void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf, | 
|  | 3235 | const unsigned char period[8]) | 
| Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 3236 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3237 | memcpy(conf->renego_period, period, 8); | 
| Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 3238 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3239 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3240 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3241 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3242 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3243 | void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets) | 
| Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 3244 | { | 
| Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 3245 | conf->session_tickets = use_tickets; | 
| Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 3246 | } | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3247 | #endif | 
| Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 3248 |  | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3249 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 3250 |  | 
| Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 3251 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3252 | void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf, | 
|  | 3253 | uint16_t num_tickets) | 
| Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 3254 | { | 
| Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 3255 | conf->new_session_tickets_count = num_tickets; | 
| Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 3256 | } | 
|  | 3257 | #endif | 
|  | 3258 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3259 | void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf, | 
|  | 3260 | mbedtls_ssl_ticket_write_t *f_ticket_write, | 
|  | 3261 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, | 
|  | 3262 | void *p_ticket) | 
| Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 3263 | { | 
| Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 3264 | conf->f_ticket_write = f_ticket_write; | 
|  | 3265 | conf->f_ticket_parse = f_ticket_parse; | 
|  | 3266 | conf->p_ticket       = p_ticket; | 
| Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 3267 | } | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3268 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3269 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ | 
| Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 3270 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3271 | void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl, | 
|  | 3272 | mbedtls_ssl_export_keys_t *f_export_keys, | 
|  | 3273 | void *p_export_keys) | 
| Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 3274 | { | 
| Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 3275 | ssl->f_export_keys = f_export_keys; | 
|  | 3276 | ssl->p_export_keys = p_export_keys; | 
| Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 3277 | } | 
| Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 3278 |  | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3279 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) | 
| Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3280 | void mbedtls_ssl_conf_async_private_cb( | 
|  | 3281 | mbedtls_ssl_config *conf, | 
|  | 3282 | mbedtls_ssl_async_sign_t *f_async_sign, | 
|  | 3283 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, | 
|  | 3284 | mbedtls_ssl_async_resume_t *f_async_resume, | 
|  | 3285 | mbedtls_ssl_async_cancel_t *f_async_cancel, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3286 | void *async_config_data) | 
| Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3287 | { | 
|  | 3288 | conf->f_async_sign_start = f_async_sign; | 
|  | 3289 | conf->f_async_decrypt_start = f_async_decrypt; | 
|  | 3290 | conf->f_async_resume = f_async_resume; | 
|  | 3291 | conf->f_async_cancel = f_async_cancel; | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3292 | conf->p_async_config_data = async_config_data; | 
|  | 3293 | } | 
|  | 3294 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3295 | void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf) | 
| Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3296 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3297 | return conf->p_async_config_data; | 
| Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3298 | } | 
|  | 3299 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3300 | void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl) | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3301 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3302 | if (ssl->handshake == NULL) { | 
|  | 3303 | return NULL; | 
|  | 3304 | } else { | 
|  | 3305 | return ssl->handshake->user_async_ctx; | 
|  | 3306 | } | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3307 | } | 
|  | 3308 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3309 | void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl, | 
|  | 3310 | void *ctx) | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3311 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3312 | if (ssl->handshake != NULL) { | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3313 | ssl->handshake->user_async_ctx = ctx; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3314 | } | 
| Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3315 | } | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3316 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ | 
| Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3317 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3318 | /* | 
|  | 3319 | * SSL get accessors | 
|  | 3320 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3321 | uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3322 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3323 | if (ssl->session != NULL) { | 
|  | 3324 | return ssl->session->verify_result; | 
|  | 3325 | } | 
| Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 3326 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3327 | if (ssl->session_negotiate != NULL) { | 
|  | 3328 | return ssl->session_negotiate->verify_result; | 
|  | 3329 | } | 
| Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 3330 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3331 | return 0xFFFFFFFF; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3332 | } | 
|  | 3333 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3334 | int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl) | 
| Glenn Strauss | 8f52690 | 2022-01-13 00:04:49 -0500 | [diff] [blame] | 3335 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3336 | if (ssl == NULL || ssl->session == NULL) { | 
|  | 3337 | return 0; | 
|  | 3338 | } | 
| Glenn Strauss | 8f52690 | 2022-01-13 00:04:49 -0500 | [diff] [blame] | 3339 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3340 | return ssl->session->ciphersuite; | 
| Glenn Strauss | 8f52690 | 2022-01-13 00:04:49 -0500 | [diff] [blame] | 3341 | } | 
|  | 3342 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3343 | const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl) | 
| Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 3344 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3345 | if (ssl == NULL || ssl->session == NULL) { | 
|  | 3346 | return NULL; | 
|  | 3347 | } | 
| Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 3348 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3349 | return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite); | 
| Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 3350 | } | 
|  | 3351 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3352 | const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl) | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3353 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3354 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3355 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { | 
|  | 3356 | switch (ssl->tls_version) { | 
| Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3357 | case MBEDTLS_SSL_VERSION_TLS1_2: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3358 | return "DTLSv1.2"; | 
| Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 3359 | default: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3360 | return "unknown (DTLS)"; | 
| Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 3361 | } | 
|  | 3362 | } | 
|  | 3363 | #endif | 
|  | 3364 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3365 | switch (ssl->tls_version) { | 
| Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3366 | case MBEDTLS_SSL_VERSION_TLS1_2: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3367 | return "TLSv1.2"; | 
| Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3368 | case MBEDTLS_SSL_VERSION_TLS1_3: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3369 | return "TLSv1.3"; | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3370 | default: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3371 | return "unknown"; | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3372 | } | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3373 | } | 
|  | 3374 |  | 
| Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 3375 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3376 | size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl) | 
| Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3377 | { | 
| David Horstmann | 95d516f | 2021-05-04 18:36:56 +0100 | [diff] [blame] | 3378 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; | 
| Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3379 | size_t read_mfl; | 
|  | 3380 |  | 
| Jerry Yu | ddda050 | 2022-12-01 19:43:12 +0800 | [diff] [blame] | 3381 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3382 | /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3383 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && | 
|  | 3384 | ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) { | 
|  | 3385 | return ssl_mfl_code_to_length(ssl->conf->mfl_code); | 
| Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3386 | } | 
| Jerry Yu | ddda050 | 2022-12-01 19:43:12 +0800 | [diff] [blame] | 3387 | #endif | 
| Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3388 |  | 
|  | 3389 | /* Check if a smaller max length was negotiated */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3390 | if (ssl->session_out != NULL) { | 
|  | 3391 | read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code); | 
|  | 3392 | if (read_mfl < max_len) { | 
| Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3393 | max_len = read_mfl; | 
|  | 3394 | } | 
|  | 3395 | } | 
|  | 3396 |  | 
| Jerry Yu | ddda050 | 2022-12-01 19:43:12 +0800 | [diff] [blame] | 3397 | /* During a handshake, use the value being negotiated */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3398 | if (ssl->session_negotiate != NULL) { | 
|  | 3399 | read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code); | 
|  | 3400 | if (read_mfl < max_len) { | 
| Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3401 | max_len = read_mfl; | 
|  | 3402 | } | 
|  | 3403 | } | 
|  | 3404 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3405 | return max_len; | 
| Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3406 | } | 
|  | 3407 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3408 | size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl) | 
| Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 3409 | { | 
|  | 3410 | size_t max_len; | 
|  | 3411 |  | 
|  | 3412 | /* | 
|  | 3413 | * Assume mfl_code is correct since it was checked when set | 
|  | 3414 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3415 | max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code); | 
| Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 3416 |  | 
| Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 3417 | /* Check if a smaller max length was negotiated */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3418 | if (ssl->session_out != NULL && | 
|  | 3419 | ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) { | 
|  | 3420 | max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code); | 
| Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 3421 | } | 
|  | 3422 |  | 
| Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 3423 | /* During a handshake, use the value being negotiated */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3424 | if (ssl->session_negotiate != NULL && | 
|  | 3425 | ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) { | 
|  | 3426 | max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code); | 
| Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 3427 | } | 
|  | 3428 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3429 | return max_len; | 
| Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 3430 | } | 
|  | 3431 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ | 
|  | 3432 |  | 
| Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3433 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3434 | size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl) | 
| Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3435 | { | 
| Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 3436 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3437 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && | 
|  | 3438 | (ssl->state == MBEDTLS_SSL_CLIENT_HELLO || | 
|  | 3439 | ssl->state == MBEDTLS_SSL_SERVER_HELLO)) { | 
|  | 3440 | return 0; | 
|  | 3441 | } | 
| Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 3442 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3443 | if (ssl->handshake == NULL || ssl->handshake->mtu == 0) { | 
|  | 3444 | return ssl->mtu; | 
|  | 3445 | } | 
| Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3446 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3447 | if (ssl->mtu == 0) { | 
|  | 3448 | return ssl->handshake->mtu; | 
|  | 3449 | } | 
| Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3450 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3451 | return ssl->mtu < ssl->handshake->mtu ? | 
|  | 3452 | ssl->mtu : ssl->handshake->mtu; | 
| Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3453 | } | 
|  | 3454 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 3455 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3456 | int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl) | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3457 | { | 
|  | 3458 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; | 
|  | 3459 |  | 
| Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 3460 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ | 
|  | 3461 | !defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 3462 | (void) ssl; | 
|  | 3463 | #endif | 
|  | 3464 |  | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3465 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3466 | const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl); | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3467 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3468 | if (max_len > mfl) { | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3469 | max_len = mfl; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3470 | } | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3471 | #endif | 
|  | 3472 |  | 
|  | 3473 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3474 | if (mbedtls_ssl_get_current_mtu(ssl) != 0) { | 
|  | 3475 | const size_t mtu = mbedtls_ssl_get_current_mtu(ssl); | 
|  | 3476 | const int ret = mbedtls_ssl_get_record_expansion(ssl); | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3477 | const size_t overhead = (size_t) ret; | 
|  | 3478 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3479 | if (ret < 0) { | 
|  | 3480 | return ret; | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3481 | } | 
|  | 3482 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3483 | if (mtu <= overhead) { | 
|  | 3484 | MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion")); | 
|  | 3485 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
|  | 3486 | } | 
|  | 3487 |  | 
|  | 3488 | if (max_len > mtu - overhead) { | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3489 | max_len = mtu - overhead; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3490 | } | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3491 | } | 
| Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3492 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3493 |  | 
| Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 3494 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) &&        \ | 
|  | 3495 | !defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 3496 | ((void) ssl); | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3497 | #endif | 
|  | 3498 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3499 | return (int) max_len; | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3500 | } | 
|  | 3501 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3502 | int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl) | 
| Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 3503 | { | 
|  | 3504 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; | 
|  | 3505 |  | 
|  | 3506 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
|  | 3507 | (void) ssl; | 
|  | 3508 | #endif | 
|  | 3509 |  | 
|  | 3510 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3511 | const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl); | 
| Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 3512 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3513 | if (max_len > mfl) { | 
| Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 3514 | max_len = mfl; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3515 | } | 
| Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 3516 | #endif | 
|  | 3517 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3518 | return (int) max_len; | 
| Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 3519 | } | 
|  | 3520 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3521 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3522 | const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl) | 
| Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3523 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3524 | if (ssl == NULL || ssl->session == NULL) { | 
|  | 3525 | return NULL; | 
|  | 3526 | } | 
| Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3527 |  | 
| Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 3528 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3529 | return ssl->session->peer_cert; | 
| Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 3530 | #else | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3531 | return NULL; | 
| Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 3532 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
| Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3533 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3534 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3535 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3536 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3537 | int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl, | 
|  | 3538 | mbedtls_ssl_session *dst) | 
| Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3539 | { | 
| Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3540 | int ret; | 
|  | 3541 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3542 | if (ssl == NULL || | 
| Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3543 | dst == NULL || | 
|  | 3544 | ssl->session == NULL || | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3545 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) { | 
|  | 3546 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
| Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3547 | } | 
|  | 3548 |  | 
| Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3549 | /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer | 
|  | 3550 | * idempotent: Each session can only be exported once. | 
|  | 3551 | * | 
|  | 3552 | * (This is in preparation for TLS 1.3 support where we will | 
|  | 3553 | * need the ability to export multiple sessions (aka tickets), | 
|  | 3554 | * which will be achieved by calling mbedtls_ssl_get_session() | 
|  | 3555 | * multiple times until it fails.) | 
|  | 3556 | * | 
|  | 3557 | * Check whether we have already exported the current session, | 
|  | 3558 | * and fail if so. | 
|  | 3559 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3560 | if (ssl->session->exported == 1) { | 
|  | 3561 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
|  | 3562 | } | 
| Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3563 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3564 | ret = mbedtls_ssl_session_copy(dst, ssl->session); | 
|  | 3565 | if (ret != 0) { | 
|  | 3566 | return ret; | 
|  | 3567 | } | 
| Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3568 |  | 
|  | 3569 | /* Remember that we've exported the session. */ | 
|  | 3570 | ssl->session->exported = 1; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3571 | return 0; | 
| Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3572 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3573 | #endif /* MBEDTLS_SSL_CLI_C */ | 
| Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3574 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3575 | /* | 
| Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 3576 | * Define ticket header determining Mbed TLS version | 
|  | 3577 | * and structure of the ticket. | 
|  | 3578 | */ | 
|  | 3579 |  | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3580 | /* | 
| Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3581 | * Define bitflag determining compile-time settings influencing | 
|  | 3582 | * structure of serialized SSL sessions. | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3583 | */ | 
|  | 3584 |  | 
| Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3585 | #if defined(MBEDTLS_HAVE_TIME) | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3586 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 | 
| Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3587 | #else | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3588 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3589 | #endif /* MBEDTLS_HAVE_TIME */ | 
|  | 3590 |  | 
|  | 3591 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3592 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3593 | #else | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3594 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3595 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
|  | 3596 |  | 
|  | 3597 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3598 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3599 | #else | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3600 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3601 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ | 
|  | 3602 |  | 
|  | 3603 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3604 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3605 | #else | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3606 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3607 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ | 
|  | 3608 |  | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3609 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3610 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3611 | #else | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3612 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3613 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ | 
|  | 3614 |  | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3615 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
|  | 3616 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 | 
|  | 3617 | #else | 
|  | 3618 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 | 
|  | 3619 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ | 
|  | 3620 |  | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3621 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT          0 | 
|  | 3622 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT           1 | 
|  | 3623 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 | 
|  | 3624 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT           3 | 
| Hanno Becker | 37bdbe6 | 2021-08-01 05:38:58 +0100 | [diff] [blame] | 3625 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT           4 | 
|  | 3626 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT        5 | 
| Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3627 |  | 
| Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3628 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG                           \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3629 | ((uint16_t) (                                                      \ | 
|  | 3630 | (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \ | 
|  | 3631 | (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \ | 
|  | 3632 | (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \ | 
|  | 3633 | SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \ | 
|  | 3634 | (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \ | 
|  | 3635 | (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \ | 
|  | 3636 | (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT))) | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3637 |  | 
| Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 3638 | static unsigned char ssl_serialized_session_header[] = { | 
| Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3639 | MBEDTLS_VERSION_MAJOR, | 
|  | 3640 | MBEDTLS_VERSION_MINOR, | 
|  | 3641 | MBEDTLS_VERSION_PATCH, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3642 | MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG), | 
|  | 3643 | MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG), | 
| Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 3644 | }; | 
| Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 3645 |  | 
|  | 3646 | /* | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3647 | * Serialize a session in the following format: | 
| Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 3648 | * (in the presentation language of TLS, RFC 8446 section 3) | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3649 | * | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3650 | *  struct { | 
| Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 3651 | * | 
| Hanno Becker | dce5097 | 2021-08-01 05:39:23 +0100 | [diff] [blame] | 3652 | *    opaque mbedtls_version[3];   // library version: major, minor, patch | 
|  | 3653 | *    opaque session_format[2];    // library-version specific 16-bit field | 
|  | 3654 | *                                 // determining the format of the remaining | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3655 | *                                 // serialized data. | 
| Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 3656 | * | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3657 | *          Note: When updating the format, remember to keep | 
|  | 3658 | *          these version+format bytes. | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3659 | * | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3660 | *                                 // In this version, `session_format` determines | 
|  | 3661 | *                                 // the setting of those compile-time | 
|  | 3662 | *                                 // configuration options which influence | 
|  | 3663 | *                                 // the structure of mbedtls_ssl_session. | 
|  | 3664 | * | 
| Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3665 | *    uint8_t minor_ver;           // Protocol minor version. Possible values: | 
| Jerry Yu | 0c2a738 | 2022-12-06 13:27:25 +0800 | [diff] [blame] | 3666 | *                                 // - TLS 1.2 (0x0303) | 
|  | 3667 | *                                 // - TLS 1.3 (0x0304) | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3668 | * | 
| Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3669 | *    select (serialized_session.tls_version) { | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3670 | * | 
| Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3671 | *      case MBEDTLS_SSL_VERSION_TLS1_2: | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3672 | *        serialized_session_tls12 data; | 
| Jerry Yu | 0c2a738 | 2022-12-06 13:27:25 +0800 | [diff] [blame] | 3673 | *      case MBEDTLS_SSL_VERSION_TLS1_3: | 
| Jerry Yu | ddda050 | 2022-12-01 19:43:12 +0800 | [diff] [blame] | 3674 | *        serialized_session_tls13 data; | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3675 | * | 
|  | 3676 | *   }; | 
|  | 3677 | * | 
|  | 3678 | * } serialized_session; | 
|  | 3679 | * | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3680 | */ | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3681 |  | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3682 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3683 | static int ssl_session_save(const mbedtls_ssl_session *session, | 
|  | 3684 | unsigned char omit_header, | 
|  | 3685 | unsigned char *buf, | 
|  | 3686 | size_t buf_len, | 
|  | 3687 | size_t *olen) | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3688 | { | 
|  | 3689 | unsigned char *p = buf; | 
|  | 3690 | size_t used = 0; | 
| Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3691 | size_t remaining_len; | 
| Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3692 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
|  | 3693 | size_t out_len; | 
|  | 3694 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 3695 | #endif | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3696 | if (session == NULL) { | 
|  | 3697 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 
|  | 3698 | } | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3699 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3700 | if (!omit_header) { | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3701 | /* | 
|  | 3702 | * Add Mbed TLS version identifier | 
|  | 3703 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3704 | used += sizeof(ssl_serialized_session_header); | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3705 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3706 | if (used <= buf_len) { | 
|  | 3707 | memcpy(p, ssl_serialized_session_header, | 
|  | 3708 | sizeof(ssl_serialized_session_header)); | 
|  | 3709 | p += sizeof(ssl_serialized_session_header); | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3710 | } | 
|  | 3711 | } | 
|  | 3712 |  | 
|  | 3713 | /* | 
|  | 3714 | * TLS version identifier | 
|  | 3715 | */ | 
|  | 3716 | used += 1; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3717 | if (used <= buf_len) { | 
|  | 3718 | *p++ = MBEDTLS_BYTE_0(session->tls_version); | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3719 | } | 
|  | 3720 |  | 
|  | 3721 | /* Forward to version-specific serialization routine. */ | 
| Jerry Yu | fca4d57 | 2022-07-21 10:37:48 +0800 | [diff] [blame] | 3722 | remaining_len = (buf_len >= used) ? buf_len - used : 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3723 | switch (session->tls_version) { | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3724 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3725 | case MBEDTLS_SSL_VERSION_TLS1_2: | 
|  | 3726 | used += ssl_tls12_session_save(session, p, remaining_len); | 
|  | 3727 | break; | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3728 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 3729 |  | 
| Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3730 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3731 | case MBEDTLS_SSL_VERSION_TLS1_3: | 
|  | 3732 | ret = ssl_tls13_session_save(session, p, remaining_len, &out_len); | 
|  | 3733 | if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) { | 
|  | 3734 | return ret; | 
|  | 3735 | } | 
|  | 3736 | used += out_len; | 
|  | 3737 | break; | 
| Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3738 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
|  | 3739 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3740 | default: | 
|  | 3741 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3742 | } | 
|  | 3743 |  | 
|  | 3744 | *olen = used; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3745 | if (used > buf_len) { | 
|  | 3746 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; | 
|  | 3747 | } | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3748 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3749 | return 0; | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3750 | } | 
|  | 3751 |  | 
|  | 3752 | /* | 
| Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3753 | * Public wrapper for ssl_session_save() | 
|  | 3754 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3755 | int mbedtls_ssl_session_save(const mbedtls_ssl_session *session, | 
|  | 3756 | unsigned char *buf, | 
|  | 3757 | size_t buf_len, | 
|  | 3758 | size_t *olen) | 
| Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3759 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3760 | return ssl_session_save(session, 0, buf, buf_len, olen); | 
| Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3761 | } | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3762 |  | 
| Jerry Yu | f1b23ca | 2022-02-18 11:48:47 +0800 | [diff] [blame] | 3763 | /* | 
|  | 3764 | * Deserialize session, see mbedtls_ssl_session_save() for format. | 
|  | 3765 | * | 
|  | 3766 | * This internal version is wrapped by a public function that cleans up in | 
|  | 3767 | * case of error, and has an extra option omit_header. | 
|  | 3768 | */ | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3769 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3770 | static int ssl_session_load(mbedtls_ssl_session *session, | 
|  | 3771 | unsigned char omit_header, | 
|  | 3772 | const unsigned char *buf, | 
|  | 3773 | size_t len) | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3774 | { | 
|  | 3775 | const unsigned char *p = buf; | 
|  | 3776 | const unsigned char * const end = buf + len; | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3777 | size_t remaining_len; | 
|  | 3778 |  | 
|  | 3779 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3780 | if (session == NULL) { | 
|  | 3781 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 
|  | 3782 | } | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3783 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3784 | if (!omit_header) { | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3785 | /* | 
|  | 3786 | * Check Mbed TLS version identifier | 
|  | 3787 | */ | 
|  | 3788 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3789 | if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) { | 
|  | 3790 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3791 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3792 |  | 
|  | 3793 | if (memcmp(p, ssl_serialized_session_header, | 
|  | 3794 | sizeof(ssl_serialized_session_header)) != 0) { | 
|  | 3795 | return MBEDTLS_ERR_SSL_VERSION_MISMATCH; | 
|  | 3796 | } | 
|  | 3797 | p += sizeof(ssl_serialized_session_header); | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3798 | } | 
|  | 3799 |  | 
|  | 3800 | /* | 
|  | 3801 | * TLS version identifier | 
|  | 3802 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3803 | if (1 > (size_t) (end - p)) { | 
|  | 3804 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 3805 | } | 
| Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 3806 | session->tls_version = (mbedtls_ssl_protocol_version) (0x0300 | *p++); | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3807 |  | 
|  | 3808 | /* Dispatch according to TLS version. */ | 
| Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 3809 | remaining_len = (size_t) (end - p); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3810 | switch (session->tls_version) { | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3811 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3812 | case MBEDTLS_SSL_VERSION_TLS1_2: | 
|  | 3813 | return ssl_tls12_session_load(session, p, remaining_len); | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3814 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 3815 |  | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3816 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3817 | case MBEDTLS_SSL_VERSION_TLS1_3: | 
|  | 3818 | return ssl_tls13_session_load(session, p, remaining_len); | 
| Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3819 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
|  | 3820 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3821 | default: | 
|  | 3822 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
| Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3823 | } | 
|  | 3824 | } | 
|  | 3825 |  | 
| Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3826 | /* | 
| Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 3827 | * Deserialize session: public wrapper for error cleaning | 
| Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3828 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3829 | int mbedtls_ssl_session_load(mbedtls_ssl_session *session, | 
|  | 3830 | const unsigned char *buf, | 
|  | 3831 | size_t len) | 
| Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3832 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3833 | int ret = ssl_session_load(session, 0, buf, len); | 
| Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3834 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3835 | if (ret != 0) { | 
|  | 3836 | mbedtls_ssl_session_free(session); | 
|  | 3837 | } | 
| Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3838 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3839 | return ret; | 
| Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3840 | } | 
|  | 3841 |  | 
|  | 3842 | /* | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3843 | * Perform a single step of the SSL handshake | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3844 | */ | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3845 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3846 | static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl) | 
| Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3847 | { | 
|  | 3848 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 3849 |  | 
| Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3850 | /* | 
|  | 3851 | * We may have not been able to send to the peer all the handshake data | 
| Ronald Cron | 3f20b77 | 2022-03-08 16:00:02 +0100 | [diff] [blame] | 3852 | * that were written into the output buffer by the previous handshake step, | 
|  | 3853 | * if the write to the network callback returned with the | 
| Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3854 | * #MBEDTLS_ERR_SSL_WANT_WRITE error code. | 
|  | 3855 | * We proceed to the next handshake step only when all data from the | 
|  | 3856 | * previous one have been sent to the peer, thus we make sure that this is | 
|  | 3857 | * the case here by calling `mbedtls_ssl_flush_output()`. The function may | 
|  | 3858 | * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case | 
|  | 3859 | * we have to wait before to go ahead. | 
|  | 3860 | * In the case of TLS 1.3, handshake step handlers do not send data to the | 
|  | 3861 | * peer. Data are only sent here and through | 
|  | 3862 | * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an | 
| Andrzej Kurek | 5c65c57 | 2022-04-13 14:28:52 -0400 | [diff] [blame] | 3863 | * alert occurred. | 
| Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3864 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3865 | if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) { | 
|  | 3866 | return ret; | 
|  | 3867 | } | 
| Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3868 |  | 
|  | 3869 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3870 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
|  | 3871 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) { | 
|  | 3872 | if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) { | 
|  | 3873 | return ret; | 
|  | 3874 | } | 
| Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3875 | } | 
|  | 3876 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 3877 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3878 | return ret; | 
| Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3879 | } | 
|  | 3880 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3881 | int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3882 | { | 
| Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3883 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3884 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3885 | if (ssl            == NULL                       || | 
| Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3886 | ssl->conf      == NULL                       || | 
|  | 3887 | ssl->handshake == NULL                       || | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3888 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) { | 
|  | 3889 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
| Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3890 | } | 
|  | 3891 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3892 | ret = ssl_prepare_handshake_step(ssl); | 
|  | 3893 | if (ret != 0) { | 
|  | 3894 | return ret; | 
|  | 3895 | } | 
| Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3896 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3897 | ret = mbedtls_ssl_handle_pending_alert(ssl); | 
|  | 3898 | if (ret != 0) { | 
| Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3899 | goto cleanup; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3900 | } | 
| Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3901 |  | 
| Tom Cosgrove | 2fdc7b3 | 2022-09-21 12:33:17 +0100 | [diff] [blame] | 3902 | /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or | 
|  | 3903 | * MBEDTLS_SSL_IS_SERVER, this is the return code we give */ | 
|  | 3904 | ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 3905 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3906 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3907 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { | 
|  | 3908 | MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s", | 
| Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 3909 | mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state))); | 
| Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3910 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3911 | switch (ssl->state) { | 
| Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3912 | case MBEDTLS_SSL_HELLO_REQUEST: | 
|  | 3913 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; | 
| Tom Cosgrove | 87d9c6c | 2022-09-22 09:27:56 +0100 | [diff] [blame] | 3914 | ret = 0; | 
| Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3915 | break; | 
| Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3916 |  | 
| Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3917 | case MBEDTLS_SSL_CLIENT_HELLO: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3918 | ret = mbedtls_ssl_write_client_hello(ssl); | 
| Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3919 | break; | 
|  | 3920 |  | 
|  | 3921 | default: | 
|  | 3922 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3923 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { | 
|  | 3924 | ret = mbedtls_ssl_tls13_handshake_client_step(ssl); | 
|  | 3925 | } else { | 
|  | 3926 | ret = mbedtls_ssl_handshake_client_step(ssl); | 
|  | 3927 | } | 
| Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3928 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3929 | ret = mbedtls_ssl_handshake_client_step(ssl); | 
| Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3930 | #else | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3931 | ret = mbedtls_ssl_tls13_handshake_client_step(ssl); | 
| Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3932 | #endif | 
|  | 3933 | } | 
| Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3934 | } | 
| Ronald Cron | 6291b23 | 2023-03-08 15:51:25 +0100 | [diff] [blame] | 3935 | #endif /* MBEDTLS_SSL_CLI_C */ | 
|  | 3936 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3937 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3938 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { | 
| Ronald Cron | 6291b23 | 2023-03-08 15:51:25 +0100 | [diff] [blame] | 3939 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
|  | 3940 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3941 | ret = mbedtls_ssl_tls13_handshake_server_step(ssl); | 
| Ronald Cron | 6291b23 | 2023-03-08 15:51:25 +0100 | [diff] [blame] | 3942 | } else { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3943 | ret = mbedtls_ssl_handshake_server_step(ssl); | 
|  | 3944 | } | 
| Ronald Cron | 6291b23 | 2023-03-08 15:51:25 +0100 | [diff] [blame] | 3945 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 3946 | ret = mbedtls_ssl_handshake_server_step(ssl); | 
|  | 3947 | #else | 
|  | 3948 | ret = mbedtls_ssl_tls13_handshake_server_step(ssl); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3949 | #endif | 
| Ronald Cron | 6291b23 | 2023-03-08 15:51:25 +0100 | [diff] [blame] | 3950 | } | 
|  | 3951 | #endif /* MBEDTLS_SSL_SRV_C */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3952 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3953 | if (ret != 0) { | 
| Jerry Yu | bbd5a3f | 2021-09-18 20:50:22 +0800 | [diff] [blame] | 3954 | /* handshake_step return error. And it is same | 
|  | 3955 | * with alert_reason. | 
|  | 3956 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3957 | if (ssl->send_alert) { | 
|  | 3958 | ret = mbedtls_ssl_handle_pending_alert(ssl); | 
| Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3959 | goto cleanup; | 
|  | 3960 | } | 
|  | 3961 | } | 
|  | 3962 |  | 
|  | 3963 | cleanup: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3964 | return ret; | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3965 | } | 
|  | 3966 |  | 
|  | 3967 | /* | 
|  | 3968 | * Perform the SSL handshake | 
|  | 3969 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3970 | int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl) | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3971 | { | 
|  | 3972 | int ret = 0; | 
|  | 3973 |  | 
| Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3974 | /* Sanity checks */ | 
|  | 3975 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3976 | if (ssl == NULL || ssl->conf == NULL) { | 
|  | 3977 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 3978 | } | 
| Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3979 |  | 
| Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3980 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3981 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
|  | 3982 | (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) { | 
|  | 3983 | MBEDTLS_SSL_DEBUG_MSG(1, ("You must use " | 
|  | 3984 | "mbedtls_ssl_set_timer_cb() for DTLS")); | 
|  | 3985 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
| Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3986 | } | 
|  | 3987 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 3988 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3989 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake")); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3990 |  | 
| Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3991 | /* Main handshake loop */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3992 | while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) { | 
|  | 3993 | ret = mbedtls_ssl_handshake_step(ssl); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3994 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3995 | if (ret != 0) { | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3996 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3997 | } | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3998 | } | 
|  | 3999 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4000 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake")); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4001 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4002 | return ret; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4003 | } | 
|  | 4004 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4005 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 4006 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4007 | /* | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4008 | * Write HelloRequest to request renegotiation on server | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4009 | */ | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4010 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4011 | static int ssl_write_hello_request(mbedtls_ssl_context *ssl) | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4012 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4013 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4014 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4015 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request")); | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4016 |  | 
|  | 4017 | ssl->out_msglen  = 4; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4018 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 4019 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_HELLO_REQUEST; | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4020 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4021 | if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { | 
|  | 4022 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret); | 
|  | 4023 | return ret; | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4024 | } | 
|  | 4025 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4026 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request")); | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4027 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4028 | return 0; | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4029 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4030 | #endif /* MBEDTLS_SSL_SRV_C */ | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4031 |  | 
|  | 4032 | /* | 
|  | 4033 | * Actually renegotiate current connection, triggered by either: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4034 | * - any side: calling mbedtls_ssl_renegotiate(), | 
|  | 4035 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), | 
|  | 4036 | * - server: receiving any handshake message on server during mbedtls_ssl_read() after | 
| Manuel Pégourié-Gonnard | 55e4ff2 | 2014-08-19 11:16:35 +0200 | [diff] [blame] | 4037 | *   the initial handshake is completed. | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4038 | * If the handshake doesn't complete due to waiting for I/O, it will continue | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4039 | * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively. | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4040 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4041 | int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4042 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4043 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4044 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4045 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate")); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4046 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4047 | if ((ret = ssl_handshake_init(ssl)) != 0) { | 
|  | 4048 | return ret; | 
|  | 4049 | } | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4050 |  | 
| Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 4051 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and | 
|  | 4052 | * the ServerHello will have message_seq = 1" */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4053 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4054 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
|  | 4055 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) { | 
|  | 4056 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { | 
| Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4057 | ssl->handshake->out_msg_seq = 1; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4058 | } else { | 
| Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4059 | ssl->handshake->in_msg_seq = 1; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4060 | } | 
| Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 4061 | } | 
|  | 4062 | #endif | 
|  | 4063 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4064 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; | 
|  | 4065 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4066 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4067 | if ((ret = mbedtls_ssl_handshake(ssl)) != 0) { | 
|  | 4068 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret); | 
|  | 4069 | return ret; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4070 | } | 
|  | 4071 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4072 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate")); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4073 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4074 | return 0; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4075 | } | 
|  | 4076 |  | 
|  | 4077 | /* | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4078 | * Renegotiate current connection on client, | 
|  | 4079 | * or request renegotiation on server | 
|  | 4080 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4081 | int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl) | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4082 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4083 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4084 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4085 | if (ssl == NULL || ssl->conf == NULL) { | 
|  | 4086 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 4087 | } | 
| Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 4088 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4089 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4090 | /* On server, just send the request */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4091 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { | 
|  | 4092 | if (mbedtls_ssl_is_handshake_over(ssl) == 0) { | 
|  | 4093 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 4094 | } | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4095 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4096 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; | 
| Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 4097 |  | 
|  | 4098 | /* Did we already try/start sending HelloRequest? */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4099 | if (ssl->out_left != 0) { | 
|  | 4100 | return mbedtls_ssl_flush_output(ssl); | 
|  | 4101 | } | 
| Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 4102 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4103 | return ssl_write_hello_request(ssl); | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4104 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4105 | #endif /* MBEDTLS_SSL_SRV_C */ | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4106 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4107 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4108 | /* | 
|  | 4109 | * On client, either start the renegotiation process or, | 
|  | 4110 | * if already in progress, continue the handshake | 
|  | 4111 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4112 | if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) { | 
|  | 4113 | if (mbedtls_ssl_is_handshake_over(ssl) == 0) { | 
|  | 4114 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4115 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4116 |  | 
|  | 4117 | if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) { | 
|  | 4118 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret); | 
|  | 4119 | return ret; | 
|  | 4120 | } | 
|  | 4121 | } else { | 
|  | 4122 | if ((ret = mbedtls_ssl_handshake(ssl)) != 0) { | 
|  | 4123 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret); | 
|  | 4124 | return ret; | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4125 | } | 
|  | 4126 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4127 | #endif /* MBEDTLS_SSL_CLI_C */ | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4128 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4129 | return ret; | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4130 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4131 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4132 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4133 | void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4134 | { | 
| Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 4135 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; | 
|  | 4136 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4137 | if (handshake == NULL) { | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4138 | return; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4139 | } | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4140 |  | 
| Valerio Setti | 6f0441d | 2023-07-03 12:11:36 +0200 | [diff] [blame] | 4141 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4142 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4143 | if (ssl->handshake->group_list_heap_allocated) { | 
|  | 4144 | mbedtls_free((void *) handshake->group_list); | 
|  | 4145 | } | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4146 | handshake->group_list = NULL; | 
|  | 4147 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ | 
| Valerio Setti | 6f0441d | 2023-07-03 12:11:36 +0200 | [diff] [blame] | 4148 | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4149 |  | 
| Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4150 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4151 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4152 | if (ssl->handshake->sig_algs_heap_allocated) { | 
|  | 4153 | mbedtls_free((void *) handshake->sig_algs); | 
|  | 4154 | } | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4155 | handshake->sig_algs = NULL; | 
|  | 4156 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ | 
| Xiaofei Bai | c234ecf | 2022-02-08 09:59:23 +0000 | [diff] [blame] | 4157 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4158 | if (ssl->handshake->certificate_request_context) { | 
|  | 4159 | mbedtls_free((void *) handshake->certificate_request_context); | 
| Xiaofei Bai | c234ecf | 2022-02-08 09:59:23 +0000 | [diff] [blame] | 4160 | } | 
|  | 4161 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
| Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4162 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ | 
| Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4163 |  | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4164 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4165 | if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) { | 
|  | 4166 | ssl->conf->f_async_cancel(ssl); | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4167 | handshake->async_in_progress = 0; | 
|  | 4168 | } | 
|  | 4169 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ | 
|  | 4170 |  | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 4171 | #if defined(MBEDTLS_MD_CAN_SHA256) | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4172 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4173 | psa_hash_abort(&handshake->fin_sha256_psa); | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4174 | #else | 
| Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 4175 | mbedtls_md_free(&handshake->fin_sha256); | 
| Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 4176 | #endif | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4177 | #endif | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 4178 | #if defined(MBEDTLS_MD_CAN_SHA384) | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4179 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4180 | psa_hash_abort(&handshake->fin_sha384_psa); | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4181 | #else | 
| Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 4182 | mbedtls_md_free(&handshake->fin_sha384); | 
| Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 4183 | #endif | 
| Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4184 | #endif | 
| Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 4185 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4186 | #if defined(MBEDTLS_DHM_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4187 | mbedtls_dhm_free(&handshake->dhm_ctx); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4188 | #endif | 
| Valerio Setti | 7aeec54 | 2023-07-05 18:57:21 +0200 | [diff] [blame] | 4189 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ | 
| Valerio Setti | 6eb0054 | 2023-07-07 17:04:24 +0200 | [diff] [blame] | 4190 | defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4191 | mbedtls_ecdh_free(&handshake->ecdh_ctx); | 
| Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 4192 | #endif | 
| Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 4193 |  | 
| Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 4194 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 4195 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4196 | psa_pake_abort(&handshake->psa_pake_ctx); | 
| Valerio Setti | eb3f788 | 2022-12-08 18:42:58 +0100 | [diff] [blame] | 4197 | /* | 
|  | 4198 | * Opaque keys are not stored in the handshake's data and it's the user | 
|  | 4199 | * responsibility to destroy them. Clear ones, instead, are created by | 
|  | 4200 | * the TLS library and should be destroyed at the same level | 
|  | 4201 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4202 | if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) { | 
|  | 4203 | psa_destroy_key(handshake->psa_pake_password); | 
| Valerio Setti | eb3f788 | 2022-12-08 18:42:58 +0100 | [diff] [blame] | 4204 | } | 
| Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 4205 | handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 4206 | #else | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4207 | mbedtls_ecjpake_free(&handshake->ecjpake_ctx); | 
| Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 4208 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 4209 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4210 | mbedtls_free(handshake->ecjpake_cache); | 
| Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 4211 | handshake->ecjpake_cache = NULL; | 
|  | 4212 | handshake->ecjpake_cache_len = 0; | 
|  | 4213 | #endif | 
| Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 4214 | #endif | 
| Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 4215 |  | 
| Valerio Setti | 7aeec54 | 2023-07-05 18:57:21 +0200 | [diff] [blame] | 4216 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_ANY_ENABLED) || \ | 
| Valerio Setti | 45d56f3 | 2023-07-13 17:23:20 +0200 | [diff] [blame] | 4217 | defined(MBEDTLS_KEY_EXCHANGE_WITH_ECDSA_ANY_ENABLED) || \ | 
| Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 4218 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 4219 | /* explicit void pointer cast for buggy MS compiler */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4220 | mbedtls_free((void *) handshake->curves_tls_id); | 
| Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 4221 | #endif | 
|  | 4222 |  | 
| Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 4223 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) | 
| Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4224 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4225 | if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) { | 
| Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4226 | /* The maintenance of the external PSK key slot is the | 
|  | 4227 | * user's responsibility. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4228 | if (ssl->handshake->psk_opaque_is_internal) { | 
|  | 4229 | psa_destroy_key(ssl->handshake->psk_opaque); | 
| Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4230 | ssl->handshake->psk_opaque_is_internal = 0; | 
|  | 4231 | } | 
|  | 4232 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 4233 | } | 
| Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 4234 | #else | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4235 | if (handshake->psk != NULL) { | 
| Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 4236 | mbedtls_zeroize_and_free(handshake->psk, handshake->psk_len); | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 4237 | } | 
| Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 4238 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 4239 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 4240 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4241 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ | 
|  | 4242 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 4243 | /* | 
|  | 4244 | * Free only the linked list wrapper, not the keys themselves | 
|  | 4245 | * since the belong to the SNI callback | 
|  | 4246 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4247 | ssl_key_cert_free(handshake->sni_key_cert); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4248 | #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */ | 
| Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 4249 |  | 
| Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4250 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4251 | mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx); | 
|  | 4252 | if (handshake->ecrs_peer_cert != NULL) { | 
|  | 4253 | mbedtls_x509_crt_free(handshake->ecrs_peer_cert); | 
|  | 4254 | mbedtls_free(handshake->ecrs_peer_cert); | 
| Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 4255 | } | 
| Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 4256 | #endif | 
|  | 4257 |  | 
| Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 4258 | #if defined(MBEDTLS_X509_CRT_PARSE_C) &&        \ | 
|  | 4259 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4260 | mbedtls_pk_free(&handshake->peer_pubkey); | 
| Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 4261 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 4262 |  | 
| XiaokangQian | 9b93c0d | 2022-02-09 06:02:25 +0000 | [diff] [blame] | 4263 | #if defined(MBEDTLS_SSL_CLI_C) && \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4264 | (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3)) | 
|  | 4265 | mbedtls_free(handshake->cookie); | 
| XiaokangQian | 9b93c0d | 2022-02-09 06:02:25 +0000 | [diff] [blame] | 4266 | #endif /* MBEDTLS_SSL_CLI_C && | 
|  | 4267 | ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */ | 
| XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 4268 |  | 
|  | 4269 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4270 | mbedtls_ssl_flight_free(handshake->flight); | 
|  | 4271 | mbedtls_ssl_buffering_free(ssl); | 
| XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 4272 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 4273 |  | 
| Valerio Setti | ea59c43 | 2023-07-25 11:14:03 +0200 | [diff] [blame] | 4274 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED) | 
| Przemek Stekiel | 7ac93be | 2023-07-04 10:02:38 +0200 | [diff] [blame] | 4275 | if (handshake->xxdh_psa_privkey_is_external == 0) { | 
|  | 4276 | psa_destroy_key(handshake->xxdh_psa_privkey); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4277 | } | 
| Valerio Setti | ea59c43 | 2023-07-25 11:14:03 +0200 | [diff] [blame] | 4278 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED */ | 
| Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 4279 |  | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4280 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4281 | mbedtls_ssl_transform_free(handshake->transform_handshake); | 
|  | 4282 | mbedtls_free(handshake->transform_handshake); | 
| Jerry Yu | 3d9b590 | 2022-11-04 14:07:25 +0800 | [diff] [blame] | 4283 | #if defined(MBEDTLS_SSL_EARLY_DATA) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4284 | mbedtls_ssl_transform_free(handshake->transform_earlydata); | 
|  | 4285 | mbedtls_free(handshake->transform_earlydata); | 
| Jerry Yu | 3d9b590 | 2022-11-04 14:07:25 +0800 | [diff] [blame] | 4286 | #endif | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4287 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
| Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 4288 |  | 
| Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4289 |  | 
|  | 4290 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
|  | 4291 | /* If the buffers are too big - reallocate. Because of the way Mbed TLS | 
|  | 4292 | * processes datagrams and the fact that a datagram is allowed to have | 
|  | 4293 | * several records in it, it is possible that the I/O buffers are not | 
|  | 4294 | * empty at this stage */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4295 | handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl), | 
|  | 4296 | mbedtls_ssl_get_output_buflen(ssl)); | 
| Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4297 | #endif | 
| Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 4298 |  | 
| Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 4299 | /* mbedtls_platform_zeroize MUST be last one in this function */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4300 | mbedtls_platform_zeroize(handshake, | 
|  | 4301 | sizeof(mbedtls_ssl_handshake_params)); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4302 | } | 
|  | 4303 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4304 | void mbedtls_ssl_session_free(mbedtls_ssl_session *session) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4305 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4306 | if (session == NULL) { | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4307 | return; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4308 | } | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4309 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4310 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4311 | ssl_clear_peer_cert(session); | 
| Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4312 | #endif | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 4313 |  | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4314 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) | 
| Xiaokang Qian | bc663a0 | 2022-10-09 11:14:39 +0000 | [diff] [blame] | 4315 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ | 
|  | 4316 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4317 | mbedtls_free(session->hostname); | 
| Xiaokang Qian | 281fd1b | 2022-09-20 11:35:41 +0000 | [diff] [blame] | 4318 | #endif | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4319 | mbedtls_free(session->ticket); | 
| Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 4320 | #endif | 
| Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 4321 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4322 | mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session)); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4323 | } | 
|  | 4324 |  | 
| Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 4325 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) | 
| Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4326 |  | 
|  | 4327 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
|  | 4328 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u | 
|  | 4329 | #else | 
|  | 4330 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u | 
|  | 4331 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
|  | 4332 |  | 
| Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4333 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u | 
| Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4334 |  | 
|  | 4335 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
|  | 4336 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u | 
|  | 4337 | #else | 
|  | 4338 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u | 
|  | 4339 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ | 
|  | 4340 |  | 
|  | 4341 | #if defined(MBEDTLS_SSL_ALPN) | 
|  | 4342 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u | 
|  | 4343 | #else | 
|  | 4344 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u | 
|  | 4345 | #endif /* MBEDTLS_SSL_ALPN */ | 
|  | 4346 |  | 
|  | 4347 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT    0 | 
|  | 4348 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT     1 | 
|  | 4349 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT      2 | 
|  | 4350 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT                  3 | 
|  | 4351 |  | 
|  | 4352 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG   \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4353 | ((uint32_t) (                              \ | 
|  | 4354 | (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \ | 
|  | 4355 | SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \ | 
|  | 4356 | (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \ | 
|  | 4357 | SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \ | 
|  | 4358 | (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \ | 
|  | 4359 | SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \ | 
|  | 4360 | (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \ | 
|  | 4361 | 0u)) | 
| Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4362 |  | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4363 | static unsigned char ssl_serialized_context_header[] = { | 
|  | 4364 | MBEDTLS_VERSION_MAJOR, | 
|  | 4365 | MBEDTLS_VERSION_MINOR, | 
|  | 4366 | MBEDTLS_VERSION_PATCH, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4367 | MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG), | 
|  | 4368 | MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG), | 
|  | 4369 | MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG), | 
|  | 4370 | MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG), | 
|  | 4371 | MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG), | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4372 | }; | 
|  | 4373 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4374 | /* | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4375 | * Serialize a full SSL context | 
| Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 4376 | * | 
|  | 4377 | * The format of the serialized data is: | 
|  | 4378 | * (in the presentation language of TLS, RFC 8446 section 3) | 
|  | 4379 | * | 
|  | 4380 | *  // header | 
|  | 4381 | *  opaque mbedtls_version[3];   // major, minor, patch | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4382 | *  opaque context_format[5];    // version-specific field determining | 
| Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 4383 | *                               // the format of the remaining | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4384 | *                               // serialized data. | 
| Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4385 | *  Note: When updating the format, remember to keep these | 
|  | 4386 | *        version+format bytes. (We may make their size part of the API.) | 
| Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 4387 | * | 
|  | 4388 | *  // session sub-structure | 
|  | 4389 | *  opaque session<1..2^32-1>;  // see mbedtls_ssl_session_save() | 
|  | 4390 | *  // transform sub-structure | 
|  | 4391 | *  uint8 random[64];           // ServerHello.random+ClientHello.random | 
|  | 4392 | *  uint8 in_cid<0..2^8-1>      // Connection ID: expected incoming value | 
|  | 4393 | *  uint8 out_cid<0..2^8-1>     // Connection ID: outgoing value to use | 
|  | 4394 | *  // fields from ssl_context | 
|  | 4395 | *  uint32 badmac_seen;         // DTLS: number of records with failing MAC | 
|  | 4396 | *  uint64 in_window_top;       // DTLS: last validated record seq_num | 
|  | 4397 | *  uint64 in_window;           // DTLS: bitmask for replay protection | 
|  | 4398 | *  uint8 disable_datagram_packing; // DTLS: only one record per datagram | 
|  | 4399 | *  uint64 cur_out_ctr;         // Record layer: outgoing sequence number | 
|  | 4400 | *  uint16 mtu;                 // DTLS: path mtu (max outgoing fragment size) | 
|  | 4401 | *  uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol | 
|  | 4402 | * | 
|  | 4403 | * Note that many fields of the ssl_context or sub-structures are not | 
|  | 4404 | * serialized, as they fall in one of the following categories: | 
|  | 4405 | * | 
|  | 4406 | *  1. forced value (eg in_left must be 0) | 
|  | 4407 | *  2. pointer to dynamically-allocated memory (eg session, transform) | 
|  | 4408 | *  3. value can be re-derived from other data (eg session keys from MS) | 
|  | 4409 | *  4. value was temporary (eg content of input buffer) | 
|  | 4410 | *  5. value will be provided by the user again (eg I/O callbacks and context) | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4411 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4412 | int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl, | 
|  | 4413 | unsigned char *buf, | 
|  | 4414 | size_t buf_len, | 
|  | 4415 | size_t *olen) | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4416 | { | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4417 | unsigned char *p = buf; | 
|  | 4418 | size_t used = 0; | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4419 | size_t session_len; | 
|  | 4420 | int ret = 0; | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4421 |  | 
| Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 4422 | /* | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4423 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in | 
|  | 4424 | * this function's documentation. | 
|  | 4425 | * | 
|  | 4426 | * These are due to assumptions/limitations in the implementation. Some of | 
|  | 4427 | * them are likely to stay (no handshake in progress) some might go away | 
|  | 4428 | * (only DTLS) but are currently used to simplify the implementation. | 
| Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 4429 | */ | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4430 | /* The initial handshake must be over */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4431 | if (mbedtls_ssl_is_handshake_over(ssl) == 0) { | 
|  | 4432 | MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over")); | 
|  | 4433 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4434 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4435 | if (ssl->handshake != NULL) { | 
|  | 4436 | MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed")); | 
|  | 4437 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4438 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4439 | /* Double-check that sub-structures are indeed ready */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4440 | if (ssl->transform == NULL || ssl->session == NULL) { | 
|  | 4441 | MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready")); | 
|  | 4442 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4443 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4444 | /* There must be no pending incoming or outgoing data */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4445 | if (mbedtls_ssl_check_pending(ssl) != 0) { | 
|  | 4446 | MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data")); | 
|  | 4447 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4448 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4449 | if (ssl->out_left != 0) { | 
|  | 4450 | MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data")); | 
|  | 4451 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4452 | } | 
| Dave Rodgman | 556e8a3 | 2022-12-06 16:31:25 +0000 | [diff] [blame] | 4453 | /* Protocol must be DTLS, not TLS */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4454 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) { | 
|  | 4455 | MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported")); | 
|  | 4456 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4457 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4458 | /* Version must be 1.2 */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4459 | if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) { | 
|  | 4460 | MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported")); | 
|  | 4461 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4462 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4463 | /* We must be using an AEAD ciphersuite */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4464 | if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) { | 
|  | 4465 | MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported")); | 
|  | 4466 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4467 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4468 | /* Renegotiation must not be enabled */ | 
|  | 4469 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4470 | if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) { | 
|  | 4471 | MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled")); | 
|  | 4472 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
| Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4473 | } | 
| Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4474 | #endif | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4475 |  | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4476 | /* | 
|  | 4477 | * Version and format identifier | 
|  | 4478 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4479 | used += sizeof(ssl_serialized_context_header); | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4480 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4481 | if (used <= buf_len) { | 
|  | 4482 | memcpy(p, ssl_serialized_context_header, | 
|  | 4483 | sizeof(ssl_serialized_context_header)); | 
|  | 4484 | p += sizeof(ssl_serialized_context_header); | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4485 | } | 
|  | 4486 |  | 
|  | 4487 | /* | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4488 | * Session (length + data) | 
|  | 4489 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4490 | ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len); | 
|  | 4491 | if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) { | 
|  | 4492 | return ret; | 
|  | 4493 | } | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4494 |  | 
|  | 4495 | used += 4 + session_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4496 | if (used <= buf_len) { | 
|  | 4497 | MBEDTLS_PUT_UINT32_BE(session_len, p, 0); | 
| Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4498 | p += 4; | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4499 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4500 | ret = ssl_session_save(ssl->session, 1, | 
|  | 4501 | p, session_len, &session_len); | 
|  | 4502 | if (ret != 0) { | 
|  | 4503 | return ret; | 
|  | 4504 | } | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4505 |  | 
|  | 4506 | p += session_len; | 
|  | 4507 | } | 
|  | 4508 |  | 
|  | 4509 | /* | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4510 | * Transform | 
|  | 4511 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4512 | used += sizeof(ssl->transform->randbytes); | 
|  | 4513 | if (used <= buf_len) { | 
|  | 4514 | memcpy(p, ssl->transform->randbytes, | 
|  | 4515 | sizeof(ssl->transform->randbytes)); | 
|  | 4516 | p += sizeof(ssl->transform->randbytes); | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4517 | } | 
|  | 4518 |  | 
|  | 4519 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
| Dave Rodgman | c37ad44 | 2023-11-03 23:36:06 +0000 | [diff] [blame] | 4520 | used += 2U + ssl->transform->in_cid_len + ssl->transform->out_cid_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4521 | if (used <= buf_len) { | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4522 | *p++ = ssl->transform->in_cid_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4523 | memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len); | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4524 | p += ssl->transform->in_cid_len; | 
|  | 4525 |  | 
|  | 4526 | *p++ = ssl->transform->out_cid_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4527 | memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len); | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4528 | p += ssl->transform->out_cid_len; | 
|  | 4529 | } | 
|  | 4530 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
|  | 4531 |  | 
|  | 4532 | /* | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4533 | * Saved fields from top-level ssl_context structure | 
|  | 4534 | */ | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4535 | used += 4; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4536 | if (used <= buf_len) { | 
|  | 4537 | MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0); | 
| Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4538 | p += 4; | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4539 | } | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4540 |  | 
|  | 4541 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
|  | 4542 | used += 16; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4543 | if (used <= buf_len) { | 
|  | 4544 | MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0); | 
| Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4545 | p += 8; | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4546 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4547 | MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0); | 
| Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4548 | p += 8; | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4549 | } | 
|  | 4550 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ | 
|  | 4551 |  | 
|  | 4552 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 4553 | used += 1; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4554 | if (used <= buf_len) { | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4555 | *p++ = ssl->disable_datagram_packing; | 
|  | 4556 | } | 
|  | 4557 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 4558 |  | 
| Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 4559 | used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4560 | if (used <= buf_len) { | 
|  | 4561 | memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN); | 
| Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 4562 | p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4563 | } | 
|  | 4564 |  | 
|  | 4565 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 4566 | used += 2; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4567 | if (used <= buf_len) { | 
|  | 4568 | MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0); | 
| Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4569 | p += 2; | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4570 | } | 
|  | 4571 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 4572 |  | 
|  | 4573 | #if defined(MBEDTLS_SSL_ALPN) | 
|  | 4574 | { | 
|  | 4575 | const uint8_t alpn_len = ssl->alpn_chosen | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4576 | ? (uint8_t) strlen(ssl->alpn_chosen) | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4577 | : 0; | 
|  | 4578 |  | 
|  | 4579 | used += 1 + alpn_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4580 | if (used <= buf_len) { | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4581 | *p++ = alpn_len; | 
|  | 4582 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4583 | if (ssl->alpn_chosen != NULL) { | 
|  | 4584 | memcpy(p, ssl->alpn_chosen, alpn_len); | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4585 | p += alpn_len; | 
|  | 4586 | } | 
|  | 4587 | } | 
|  | 4588 | } | 
|  | 4589 | #endif /* MBEDTLS_SSL_ALPN */ | 
|  | 4590 |  | 
|  | 4591 | /* | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4592 | * Done | 
|  | 4593 | */ | 
|  | 4594 | *olen = used; | 
|  | 4595 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4596 | if (used > buf_len) { | 
|  | 4597 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; | 
|  | 4598 | } | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4599 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4600 | MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used); | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4601 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4602 | return mbedtls_ssl_session_reset_int(ssl, 0); | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4603 | } | 
|  | 4604 |  | 
|  | 4605 | /* | 
| Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 4606 | * Deserialize context, see mbedtls_ssl_context_save() for format. | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4607 | * | 
|  | 4608 | * This internal version is wrapped by a public function that cleans up in | 
|  | 4609 | * case of error. | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4610 | */ | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4611 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4612 | static int ssl_context_load(mbedtls_ssl_context *ssl, | 
|  | 4613 | const unsigned char *buf, | 
|  | 4614 | size_t len) | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4615 | { | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4616 | const unsigned char *p = buf; | 
|  | 4617 | const unsigned char * const end = buf + len; | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4618 | size_t session_len; | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4619 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4620 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 4621 | tls_prf_fn prf_func = NULL; | 
| Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4622 | #endif | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4623 |  | 
| Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4624 | /* | 
|  | 4625 | * The context should have been freshly setup or reset. | 
|  | 4626 | * Give the user an error in case of obvious misuse. | 
| Manuel Pégourié-Gonnard | 4ca930f | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 4627 | * (Checking session is useful because it won't be NULL if we're | 
| Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4628 | * renegotiating, or if the user mistakenly loaded a session first.) | 
|  | 4629 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4630 | if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST || | 
|  | 4631 | ssl->session != NULL) { | 
|  | 4632 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
| Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4633 | } | 
|  | 4634 |  | 
|  | 4635 | /* | 
|  | 4636 | * We can't check that the config matches the initial one, but we can at | 
|  | 4637 | * least check it matches the requirements for serializing. | 
|  | 4638 | */ | 
| Dave Rodgman | e99b24d | 2023-09-14 15:45:03 +0100 | [diff] [blame] | 4639 | if ( | 
| Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4640 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 4641 | ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED || | 
| Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4642 | #endif | 
| Dave Rodgman | e99b24d | 2023-09-14 15:45:03 +0100 | [diff] [blame] | 4643 | ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || | 
|  | 4644 | ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 || | 
|  | 4645 | ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 | 
|  | 4646 | ) { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4647 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
| Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4648 | } | 
|  | 4649 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4650 | MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len); | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4651 |  | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4652 | /* | 
|  | 4653 | * Check version identifier | 
|  | 4654 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4655 | if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) { | 
|  | 4656 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4657 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4658 |  | 
|  | 4659 | if (memcmp(p, ssl_serialized_context_header, | 
|  | 4660 | sizeof(ssl_serialized_context_header)) != 0) { | 
|  | 4661 | return MBEDTLS_ERR_SSL_VERSION_MISMATCH; | 
|  | 4662 | } | 
|  | 4663 | p += sizeof(ssl_serialized_context_header); | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4664 |  | 
|  | 4665 | /* | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4666 | * Session | 
|  | 4667 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4668 | if ((size_t) (end - p) < 4) { | 
|  | 4669 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 4670 | } | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4671 |  | 
| Thomas Daubney | f9f0ba8 | 2023-05-23 17:34:33 +0100 | [diff] [blame] | 4672 | session_len = MBEDTLS_GET_UINT32_BE(p, 0); | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4673 | p += 4; | 
|  | 4674 |  | 
| Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4675 | /* This has been allocated by ssl_handshake_init(), called by | 
| Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4676 | * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */ | 
| Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4677 | ssl->session = ssl->session_negotiate; | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4678 | ssl->session_in = ssl->session; | 
|  | 4679 | ssl->session_out = ssl->session; | 
| Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4680 | ssl->session_negotiate = NULL; | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4681 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4682 | if ((size_t) (end - p) < session_len) { | 
|  | 4683 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 4684 | } | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4685 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4686 | ret = ssl_session_load(ssl->session, 1, p, session_len); | 
|  | 4687 | if (ret != 0) { | 
|  | 4688 | mbedtls_ssl_session_free(ssl->session); | 
|  | 4689 | return ret; | 
| Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4690 | } | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4691 |  | 
|  | 4692 | p += session_len; | 
|  | 4693 |  | 
|  | 4694 | /* | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4695 | * Transform | 
|  | 4696 | */ | 
|  | 4697 |  | 
| Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4698 | /* This has been allocated by ssl_handshake_init(), called by | 
| Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4699 | * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */ | 
| Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 4700 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4701 | ssl->transform = ssl->transform_negotiate; | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4702 | ssl->transform_in = ssl->transform; | 
|  | 4703 | ssl->transform_out = ssl->transform; | 
| Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4704 | ssl->transform_negotiate = NULL; | 
| Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 4705 | #endif | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4706 |  | 
| Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4707 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4708 | prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite); | 
|  | 4709 | if (prf_func == NULL) { | 
|  | 4710 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 4711 | } | 
| Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 4712 |  | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4713 | /* Read random bytes and populate structure */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4714 | if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) { | 
|  | 4715 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 4716 | } | 
| Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4717 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4718 | ret = ssl_tls12_populate_transform(ssl->transform, | 
|  | 4719 | ssl->session->ciphersuite, | 
|  | 4720 | ssl->session->master, | 
| Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 4721 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4722 | ssl->session->encrypt_then_mac, | 
| Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 4723 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4724 | prf_func, | 
|  | 4725 | p, /* currently pointing to randbytes */ | 
|  | 4726 | MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */ | 
|  | 4727 | ssl->conf->endpoint, | 
|  | 4728 | ssl); | 
|  | 4729 | if (ret != 0) { | 
|  | 4730 | return ret; | 
|  | 4731 | } | 
| Jerry Yu | 840fbb2 | 2022-02-17 14:59:29 +0800 | [diff] [blame] | 4732 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4733 | p += sizeof(ssl->transform->randbytes); | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4734 |  | 
|  | 4735 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
|  | 4736 | /* Read connection IDs and store them */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4737 | if ((size_t) (end - p) < 1) { | 
|  | 4738 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 4739 | } | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4740 |  | 
|  | 4741 | ssl->transform->in_cid_len = *p++; | 
|  | 4742 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4743 | if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) { | 
|  | 4744 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 4745 | } | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4746 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4747 | memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len); | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4748 | p += ssl->transform->in_cid_len; | 
|  | 4749 |  | 
|  | 4750 | ssl->transform->out_cid_len = *p++; | 
|  | 4751 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4752 | if ((size_t) (end - p) < ssl->transform->out_cid_len) { | 
|  | 4753 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 4754 | } | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4755 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4756 | memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len); | 
| Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4757 | p += ssl->transform->out_cid_len; | 
|  | 4758 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
|  | 4759 |  | 
|  | 4760 | /* | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4761 | * Saved fields from top-level ssl_context structure | 
|  | 4762 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4763 | if ((size_t) (end - p) < 4) { | 
|  | 4764 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 4765 | } | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4766 |  | 
| Thomas Daubney | f9f0ba8 | 2023-05-23 17:34:33 +0100 | [diff] [blame] | 4767 | ssl->badmac_seen = MBEDTLS_GET_UINT32_BE(p, 0); | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4768 | p += 4; | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4769 |  | 
|  | 4770 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4771 | if ((size_t) (end - p) < 16) { | 
|  | 4772 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 4773 | } | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4774 |  | 
| Thomas Daubney | f9f0ba8 | 2023-05-23 17:34:33 +0100 | [diff] [blame] | 4775 | ssl->in_window_top = MBEDTLS_GET_UINT64_BE(p, 0); | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4776 | p += 8; | 
|  | 4777 |  | 
| Thomas Daubney | f9f0ba8 | 2023-05-23 17:34:33 +0100 | [diff] [blame] | 4778 | ssl->in_window = MBEDTLS_GET_UINT64_BE(p, 0); | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4779 | p += 8; | 
|  | 4780 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ | 
|  | 4781 |  | 
|  | 4782 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4783 | if ((size_t) (end - p) < 1) { | 
|  | 4784 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 4785 | } | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4786 |  | 
|  | 4787 | ssl->disable_datagram_packing = *p++; | 
|  | 4788 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 4789 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4790 | if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) { | 
|  | 4791 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 4792 | } | 
|  | 4793 | memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr)); | 
|  | 4794 | p += sizeof(ssl->cur_out_ctr); | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4795 |  | 
|  | 4796 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4797 | if ((size_t) (end - p) < 2) { | 
|  | 4798 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 4799 | } | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4800 |  | 
| Dave Rodgman | a3d0f61 | 2023-11-03 23:34:02 +0000 | [diff] [blame] | 4801 | ssl->mtu = MBEDTLS_GET_UINT16_BE(p, 0); | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4802 | p += 2; | 
|  | 4803 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 4804 |  | 
|  | 4805 | #if defined(MBEDTLS_SSL_ALPN) | 
|  | 4806 | { | 
|  | 4807 | uint8_t alpn_len; | 
|  | 4808 | const char **cur; | 
|  | 4809 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4810 | if ((size_t) (end - p) < 1) { | 
|  | 4811 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 4812 | } | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4813 |  | 
|  | 4814 | alpn_len = *p++; | 
|  | 4815 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4816 | if (alpn_len != 0 && ssl->conf->alpn_list != NULL) { | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4817 | /* alpn_chosen should point to an item in the configured list */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4818 | for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) { | 
|  | 4819 | if (strlen(*cur) == alpn_len && | 
|  | 4820 | memcmp(p, cur, alpn_len) == 0) { | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4821 | ssl->alpn_chosen = *cur; | 
|  | 4822 | break; | 
|  | 4823 | } | 
|  | 4824 | } | 
|  | 4825 | } | 
|  | 4826 |  | 
|  | 4827 | /* can only happen on conf mismatch */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4828 | if (alpn_len != 0 && ssl->alpn_chosen == NULL) { | 
|  | 4829 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 4830 | } | 
| Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4831 |  | 
|  | 4832 | p += alpn_len; | 
|  | 4833 | } | 
|  | 4834 | #endif /* MBEDTLS_SSL_ALPN */ | 
|  | 4835 |  | 
|  | 4836 | /* | 
| Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4837 | * Forced fields from top-level ssl_context structure | 
|  | 4838 | * | 
|  | 4839 | * Most of them already set to the correct value by mbedtls_ssl_init() and | 
|  | 4840 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. | 
|  | 4841 | */ | 
|  | 4842 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; | 
| Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 4843 | ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2; | 
| Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4844 |  | 
| Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 4845 | /* Adjust pointers for header fields of outgoing records to | 
|  | 4846 | * the given transform, accounting for explicit IV and CID. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4847 | mbedtls_ssl_update_out_pointers(ssl, ssl->transform); | 
| Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 4848 |  | 
| Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4849 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 4850 | ssl->in_epoch = 1; | 
|  | 4851 | #endif | 
|  | 4852 |  | 
|  | 4853 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, | 
|  | 4854 | * which we don't want - otherwise we'd end up freeing the wrong transform | 
| Hanno Becker | ce5f5fd | 2020-02-05 10:47:44 +0000 | [diff] [blame] | 4855 | * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform() | 
|  | 4856 | * inappropriately. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4857 | if (ssl->handshake != NULL) { | 
|  | 4858 | mbedtls_ssl_handshake_free(ssl); | 
|  | 4859 | mbedtls_free(ssl->handshake); | 
| Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4860 | ssl->handshake = NULL; | 
|  | 4861 | } | 
|  | 4862 |  | 
|  | 4863 | /* | 
| Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4864 | * Done - should have consumed entire buffer | 
|  | 4865 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4866 | if (p != end) { | 
|  | 4867 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 4868 | } | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4869 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4870 | return 0; | 
| Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4871 | } | 
|  | 4872 |  | 
|  | 4873 | /* | 
| Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 4874 | * Deserialize context: public wrapper for error cleaning | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4875 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4876 | int mbedtls_ssl_context_load(mbedtls_ssl_context *context, | 
|  | 4877 | const unsigned char *buf, | 
|  | 4878 | size_t len) | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4879 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4880 | int ret = ssl_context_load(context, buf, len); | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4881 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4882 | if (ret != 0) { | 
|  | 4883 | mbedtls_ssl_free(context); | 
|  | 4884 | } | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4885 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4886 | return ret; | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4887 | } | 
| Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 4888 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ | 
| Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4889 |  | 
|  | 4890 | /* | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4891 | * Free an SSL context | 
|  | 4892 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4893 | void mbedtls_ssl_free(mbedtls_ssl_context *ssl) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4894 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4895 | if (ssl == NULL) { | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4896 | return; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4897 | } | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4898 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4899 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> free")); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4900 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4901 | if (ssl->out_buf != NULL) { | 
| sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 4902 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
|  | 4903 | size_t out_buf_len = ssl->out_buf_len; | 
|  | 4904 | #else | 
|  | 4905 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; | 
|  | 4906 | #endif | 
|  | 4907 |  | 
| Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 4908 | mbedtls_zeroize_and_free(ssl->out_buf, out_buf_len); | 
| Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4909 | ssl->out_buf = NULL; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4910 | } | 
|  | 4911 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4912 | if (ssl->in_buf != NULL) { | 
| sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 4913 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) | 
|  | 4914 | size_t in_buf_len = ssl->in_buf_len; | 
|  | 4915 | #else | 
|  | 4916 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; | 
|  | 4917 | #endif | 
|  | 4918 |  | 
| Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 4919 | mbedtls_zeroize_and_free(ssl->in_buf, in_buf_len); | 
| Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4920 | ssl->in_buf = NULL; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4921 | } | 
|  | 4922 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4923 | if (ssl->transform) { | 
|  | 4924 | mbedtls_ssl_transform_free(ssl->transform); | 
|  | 4925 | mbedtls_free(ssl->transform); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4926 | } | 
|  | 4927 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4928 | if (ssl->handshake) { | 
|  | 4929 | mbedtls_ssl_handshake_free(ssl); | 
|  | 4930 | mbedtls_free(ssl->handshake); | 
| Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 4931 |  | 
|  | 4932 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4933 | mbedtls_ssl_transform_free(ssl->transform_negotiate); | 
|  | 4934 | mbedtls_free(ssl->transform_negotiate); | 
| Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 4935 | #endif | 
|  | 4936 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4937 | mbedtls_ssl_session_free(ssl->session_negotiate); | 
|  | 4938 | mbedtls_free(ssl->session_negotiate); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4939 | } | 
|  | 4940 |  | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4941 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4942 | mbedtls_ssl_transform_free(ssl->transform_application); | 
|  | 4943 | mbedtls_free(ssl->transform_application); | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4944 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
| Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 4945 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4946 | if (ssl->session) { | 
|  | 4947 | mbedtls_ssl_session_free(ssl->session); | 
|  | 4948 | mbedtls_free(ssl->session); | 
| Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 4949 | } | 
|  | 4950 |  | 
| Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 4951 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4952 | if (ssl->hostname != NULL) { | 
| Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 4953 | mbedtls_zeroize_and_free(ssl->hostname, strlen(ssl->hostname)); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4954 | } | 
| Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 4955 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4956 |  | 
| Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 4957 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4958 | mbedtls_free(ssl->cli_id); | 
| Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 4959 | #endif | 
|  | 4960 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4961 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= free")); | 
| Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 4962 |  | 
| Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 4963 | /* Actually clear after last debug message */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4964 | mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context)); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4965 | } | 
|  | 4966 |  | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4967 | /* | 
| Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 4968 | * Initialize mbedtls_ssl_config | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4969 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4970 | void mbedtls_ssl_config_init(mbedtls_ssl_config *conf) | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4971 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4972 | memset(conf, 0, sizeof(mbedtls_ssl_config)); | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4973 | } | 
|  | 4974 |  | 
| Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4975 | /* The selection should be the same as mbedtls_x509_crt_profile_default in | 
|  | 4976 | * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters: | 
| Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4977 | * curves with a lower resource usage come first. | 
| Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4978 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise | 
| Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4979 | * about this list. | 
|  | 4980 | */ | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4981 | static uint16_t ssl_preset_default_groups[] = { | 
| Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 4982 | #if defined(MBEDTLS_ECP_HAVE_CURVE25519) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4983 | MBEDTLS_SSL_IANA_TLS_GROUP_X25519, | 
| Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4984 | #endif | 
| Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 4985 | #if defined(MBEDTLS_ECP_HAVE_SECP256R1) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4986 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, | 
| Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4987 | #endif | 
| Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 4988 | #if defined(MBEDTLS_ECP_HAVE_SECP384R1) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4989 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, | 
| Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4990 | #endif | 
| Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 4991 | #if defined(MBEDTLS_ECP_HAVE_CURVE448) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4992 | MBEDTLS_SSL_IANA_TLS_GROUP_X448, | 
| Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4993 | #endif | 
| Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 4994 | #if defined(MBEDTLS_ECP_HAVE_SECP521R1) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4995 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, | 
| Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4996 | #endif | 
| Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 4997 | #if defined(MBEDTLS_ECP_HAVE_BP256R1) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4998 | MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, | 
| Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4999 | #endif | 
| Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 5000 | #if defined(MBEDTLS_ECP_HAVE_BP384R1) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5001 | MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, | 
| Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 5002 | #endif | 
| Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 5003 | #if defined(MBEDTLS_ECP_HAVE_BP512R1) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5004 | MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, | 
| Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 5005 | #endif | 
| Przemek Stekiel | 316c19e | 2023-05-31 15:25:11 +0200 | [diff] [blame] | 5006 | #if defined(PSA_WANT_ALG_FFDH) | 
| Przemek Stekiel | 383f471 | 2022-12-12 14:48:57 +0100 | [diff] [blame] | 5007 | MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048, | 
|  | 5008 | MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072, | 
|  | 5009 | MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096, | 
|  | 5010 | MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144, | 
|  | 5011 | MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192, | 
|  | 5012 | #endif | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5013 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE | 
| Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 5014 | }; | 
| Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 5015 |  | 
| Alexey Tsvetkov | 2ca3437 | 2022-06-07 10:21:05 +0300 | [diff] [blame] | 5016 | static const int ssl_preset_suiteb_ciphersuites[] = { | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5017 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, | 
|  | 5018 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, | 
|  | 5019 | 0 | 
|  | 5020 | }; | 
|  | 5021 |  | 
| Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5022 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) | 
| Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 5023 |  | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5024 | /* NOTICE: | 
| Jerry Yu | 0b994b8 | 2022-01-25 17:22:12 +0800 | [diff] [blame] | 5025 | *   For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following | 
| Jerry Yu | 370e146 | 2022-01-25 10:36:53 +0800 | [diff] [blame] | 5026 | *   rules SHOULD be upheld. | 
|  | 5027 | *   - No duplicate entries. | 
|  | 5028 | *   - But if there is a good reason, do not change the order of the algorithms. | 
| Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 5029 | *   - ssl_tls12_preset* is for TLS 1.2 use only. | 
| Jerry Yu | 370e146 | 2022-01-25 10:36:53 +0800 | [diff] [blame] | 5030 | *   - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes. | 
| Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5031 | */ | 
| Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 5032 | static uint16_t ssl_preset_default_sig_algs[] = { | 
| Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5033 |  | 
| Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5034 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \ | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5035 | defined(MBEDTLS_MD_CAN_SHA256) && \ | 
| Valerio Setti | cf29c5d | 2023-09-01 09:03:41 +0200 | [diff] [blame] | 5036 | defined(PSA_WANT_ECC_SECP_R1_256) | 
| Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 5037 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, | 
| Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5038 | // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256) | 
|  | 5039 | #endif | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5040 |  | 
| Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5041 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \ | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5042 | defined(MBEDTLS_MD_CAN_SHA384) && \ | 
| Valerio Setti | cf29c5d | 2023-09-01 09:03:41 +0200 | [diff] [blame] | 5043 | defined(PSA_WANT_ECC_SECP_R1_384) | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5044 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, | 
| Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5045 | // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384) | 
|  | 5046 | #endif | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5047 |  | 
| Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5048 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \ | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5049 | defined(MBEDTLS_MD_CAN_SHA512) && \ | 
| Valerio Setti | cf29c5d | 2023-09-01 09:03:41 +0200 | [diff] [blame] | 5050 | defined(PSA_WANT_ECC_SECP_R1_521) | 
| Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 5051 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512, | 
| Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5052 | // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512) | 
|  | 5053 | #endif | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5054 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5055 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \ | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5056 | defined(MBEDTLS_MD_CAN_SHA512) | 
| Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 5057 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5058 | #endif \ | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5059 | /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_MD_CAN_SHA512 */ | 
| Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 5060 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5061 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \ | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5062 | defined(MBEDTLS_MD_CAN_SHA384) | 
| Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 5063 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5064 | #endif \ | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5065 | /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_MD_CAN_SHA384 */ | 
| Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 5066 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5067 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \ | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5068 | defined(MBEDTLS_MD_CAN_SHA256) | 
| Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 5069 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5070 | #endif \ | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5071 | /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_MD_CAN_SHA256 */ | 
| Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 5072 |  | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5073 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA512) | 
| Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 5074 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512, | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5075 | #endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA512 */ | 
| Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 5076 |  | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5077 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA384) | 
| Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 5078 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384, | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5079 | #endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA384 */ | 
| Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 5080 |  | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5081 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA256) | 
| Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 5082 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5083 | #endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA256 */ | 
| Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 5084 |  | 
| Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5085 | MBEDTLS_TLS_SIG_NONE | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5086 | }; | 
|  | 5087 |  | 
|  | 5088 | /* NOTICE: see above */ | 
|  | 5089 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 5090 | static uint16_t ssl_tls12_preset_default_sig_algs[] = { | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5091 | #if defined(MBEDTLS_MD_CAN_SHA512) | 
| Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 5092 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5093 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512), | 
| Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 5094 | #endif | 
| Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 5095 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) | 
|  | 5096 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, | 
|  | 5097 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ | 
| Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5098 | #if defined(MBEDTLS_RSA_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5099 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512), | 
| Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5100 | #endif | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5101 | #endif /* MBEDTLS_MD_CAN_SHA512*/ | 
|  | 5102 | #if defined(MBEDTLS_MD_CAN_SHA384) | 
| Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 5103 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5104 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384), | 
| Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 5105 | #endif | 
| Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 5106 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) | 
|  | 5107 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, | 
|  | 5108 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ | 
| Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5109 | #if defined(MBEDTLS_RSA_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5110 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384), | 
| Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5111 | #endif | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5112 | #endif /* MBEDTLS_MD_CAN_SHA384*/ | 
|  | 5113 | #if defined(MBEDTLS_MD_CAN_SHA256) | 
| Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 5114 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5115 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256), | 
| Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 5116 | #endif | 
| Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 5117 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) | 
|  | 5118 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, | 
|  | 5119 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ | 
| Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5120 | #if defined(MBEDTLS_RSA_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5121 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256), | 
| Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5122 | #endif | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5123 | #endif /* MBEDTLS_MD_CAN_SHA256*/ | 
| Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5124 | MBEDTLS_TLS_SIG_NONE | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5125 | }; | 
|  | 5126 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 5127 | /* NOTICE: see above */ | 
|  | 5128 | static uint16_t ssl_preset_suiteb_sig_algs[] = { | 
|  | 5129 |  | 
| Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5130 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \ | 
| Valerio Setti | 45d56f3 | 2023-07-13 17:23:20 +0200 | [diff] [blame] | 5131 | defined(MBEDTLS_MD_CAN_SHA256) && \ | 
| Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 5132 | defined(MBEDTLS_ECP_HAVE_SECP256R1) | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5133 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, | 
| Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5134 | // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256) | 
|  | 5135 | #endif | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5136 |  | 
| Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5137 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \ | 
| Valerio Setti | 45d56f3 | 2023-07-13 17:23:20 +0200 | [diff] [blame] | 5138 | defined(MBEDTLS_MD_CAN_SHA384) && \ | 
| Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 5139 | defined(MBEDTLS_ECP_HAVE_SECP384R1) | 
| Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 5140 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, | 
| Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5141 | // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384) | 
|  | 5142 | #endif | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5143 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5144 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \ | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5145 | defined(MBEDTLS_MD_CAN_SHA256) | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5146 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5147 | #endif \ | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5148 | /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_MD_CAN_SHA256*/ | 
| Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5149 |  | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5150 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA256) | 
| Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 5151 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5152 | #endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA256*/ | 
| Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 5153 |  | 
| Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5154 | MBEDTLS_TLS_SIG_NONE | 
| Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 5155 | }; | 
| Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 5156 |  | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5157 | /* NOTICE: see above */ | 
|  | 5158 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 5159 | static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = { | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5160 | #if defined(MBEDTLS_MD_CAN_SHA256) | 
| Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 5161 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5162 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256), | 
| Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 5163 | #endif | 
| Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5164 | #if defined(MBEDTLS_RSA_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5165 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256), | 
| Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5166 | #endif | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5167 | #endif /* MBEDTLS_MD_CAN_SHA256*/ | 
|  | 5168 | #if defined(MBEDTLS_MD_CAN_SHA384) | 
| Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 5169 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5170 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384), | 
| Jerry Yu | 18c833e | 2022-01-25 10:55:47 +0800 | [diff] [blame] | 5171 | #endif | 
| Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5172 | #if defined(MBEDTLS_RSA_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5173 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384), | 
| Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5174 | #endif | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5175 | #endif /* MBEDTLS_MD_CAN_SHA256*/ | 
| Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5176 | MBEDTLS_TLS_SIG_NONE | 
| Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 5177 | }; | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5178 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 5179 |  | 
| Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5180 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5181 |  | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5182 | static uint16_t ssl_preset_suiteb_groups[] = { | 
| Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 5183 | #if defined(MBEDTLS_ECP_HAVE_SECP256R1) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5184 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, | 
| Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 5185 | #endif | 
| Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 5186 | #if defined(MBEDTLS_ECP_HAVE_SECP384R1) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5187 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, | 
| Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 5188 | #endif | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5189 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5190 | }; | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5191 |  | 
| Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5192 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5193 | /* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs` | 
| Jerry Yu | 370e146 | 2022-01-25 10:36:53 +0800 | [diff] [blame] | 5194 | * to make sure there are no duplicated signature algorithm entries. */ | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5195 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5196 | static int ssl_check_no_sig_alg_duplication(uint16_t *sig_algs) | 
| Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5197 | { | 
|  | 5198 | size_t i, j; | 
|  | 5199 | int ret = 0; | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5200 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5201 | for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) { | 
|  | 5202 | for (j = 0; j < i; j++) { | 
|  | 5203 | if (sig_algs[i] != sig_algs[j]) { | 
| Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 5204 | continue; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5205 | } | 
|  | 5206 | mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET | 
|  | 5207 | ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n", | 
|  | 5208 | sig_algs[i], j, i); | 
| Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 5209 | ret = -1; | 
| Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5210 | } | 
|  | 5211 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5212 | return ret; | 
| Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5213 | } | 
|  | 5214 |  | 
| Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5215 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ | 
| Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5216 |  | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5217 | /* | 
| Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 5218 | * Load default in mbedtls_ssl_config | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5219 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5220 | int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf, | 
|  | 5221 | int endpoint, int transport, int preset) | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5222 | { | 
| Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 5223 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5224 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 5225 | #endif | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5226 |  | 
| Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5227 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5228 | if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) { | 
|  | 5229 | mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n"); | 
|  | 5230 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5231 | } | 
|  | 5232 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5233 | if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) { | 
|  | 5234 | mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n"); | 
|  | 5235 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5236 | } | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5237 |  | 
|  | 5238 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5239 | if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) { | 
|  | 5240 | mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n"); | 
|  | 5241 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5242 | } | 
|  | 5243 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5244 | if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) { | 
|  | 5245 | mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n"); | 
|  | 5246 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5247 | } | 
|  | 5248 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5249 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ | 
| Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5250 |  | 
| Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 5251 | /* Use the functions here so that they are covered in tests, | 
|  | 5252 | * but otherwise access member directly for efficiency */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5253 | mbedtls_ssl_conf_endpoint(conf, endpoint); | 
|  | 5254 | mbedtls_ssl_conf_transport(conf, transport); | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5255 |  | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5256 | /* | 
|  | 5257 | * Things that are common to all presets | 
|  | 5258 | */ | 
| Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 5259 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5260 | if (endpoint == MBEDTLS_SSL_IS_CLIENT) { | 
| Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 5261 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; | 
|  | 5262 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
|  | 5263 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; | 
|  | 5264 | #endif | 
|  | 5265 | } | 
|  | 5266 | #endif | 
|  | 5267 |  | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5268 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 5269 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; | 
|  | 5270 | #endif | 
|  | 5271 |  | 
|  | 5272 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
|  | 5273 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; | 
|  | 5274 | #endif | 
|  | 5275 |  | 
| Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 5276 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5277 | conf->f_cookie_write = ssl_cookie_write_dummy; | 
|  | 5278 | conf->f_cookie_check = ssl_cookie_check_dummy; | 
|  | 5279 | #endif | 
|  | 5280 |  | 
|  | 5281 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
|  | 5282 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; | 
|  | 5283 | #endif | 
|  | 5284 |  | 
| Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 5285 | #if defined(MBEDTLS_SSL_SRV_C) | 
|  | 5286 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; | 
| TRodziewicz | 3946f79 | 2021-06-14 12:11:18 +0200 | [diff] [blame] | 5287 | conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER; | 
| Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 5288 | #endif | 
|  | 5289 |  | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5290 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 5291 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; | 
|  | 5292 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; | 
|  | 5293 | #endif | 
|  | 5294 |  | 
|  | 5295 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 5296 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5297 | memset(conf->renego_period,     0x00, 2); | 
|  | 5298 | memset(conf->renego_period + 2, 0xFF, 6); | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5299 | #endif | 
|  | 5300 |  | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5301 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5302 | if (endpoint == MBEDTLS_SSL_IS_SERVER) { | 
| Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 5303 | const unsigned char dhm_p[] = | 
|  | 5304 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; | 
|  | 5305 | const unsigned char dhm_g[] = | 
|  | 5306 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; | 
| Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 5307 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5308 | if ((ret = mbedtls_ssl_conf_dh_param_bin(conf, | 
|  | 5309 | dhm_p, sizeof(dhm_p), | 
|  | 5310 | dhm_g, sizeof(dhm_g))) != 0) { | 
|  | 5311 | return ret; | 
| Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 5312 | } | 
|  | 5313 | } | 
| Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 5314 | #endif | 
|  | 5315 |  | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5316 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Jerry Yu | 6ee56aa | 2022-12-06 17:47:22 +0800 | [diff] [blame] | 5317 |  | 
|  | 5318 | #if defined(MBEDTLS_SSL_EARLY_DATA) | 
| Yanray Wang | d5ed36f | 2023-11-07 11:40:43 +0800 | [diff] [blame] | 5319 | mbedtls_ssl_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED); | 
| Jerry Yu | 6ee56aa | 2022-12-06 17:47:22 +0800 | [diff] [blame] | 5320 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Yanray Wang | 0751761 | 2023-11-07 11:47:36 +0800 | [diff] [blame] | 5321 | mbedtls_ssl_conf_max_early_data_size(conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE); | 
| Jerry Yu | 6ee56aa | 2022-12-06 17:47:22 +0800 | [diff] [blame] | 5322 | #endif | 
|  | 5323 | #endif /* MBEDTLS_SSL_EARLY_DATA */ | 
|  | 5324 |  | 
| Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 5325 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) | 
| Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 5326 | mbedtls_ssl_conf_new_session_tickets( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5327 | conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS); | 
| Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 5328 | #endif | 
| Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 5329 | /* | 
|  | 5330 | * Allow all TLS 1.3 key exchange modes by default. | 
|  | 5331 | */ | 
| Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 5332 | conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL; | 
| Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5333 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
| Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 5334 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5335 | if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { | 
| Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 5336 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5337 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; | 
| XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 5338 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; | 
| XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5339 | #else | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5340 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 5341 | #endif | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5342 | } else { | 
| XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5343 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Ronald Cron | 097ba14 | 2023-03-08 16:18:00 +0100 | [diff] [blame] | 5344 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; | 
|  | 5345 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; | 
| XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5346 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
|  | 5347 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; | 
|  | 5348 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; | 
|  | 5349 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 5350 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; | 
|  | 5351 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; | 
|  | 5352 | #else | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5353 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5354 | #endif | 
|  | 5355 | } | 
| Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 5356 |  | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5357 | /* | 
|  | 5358 | * Preset-specific defaults | 
|  | 5359 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5360 | switch (preset) { | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5361 | /* | 
|  | 5362 | * NSA Suite B | 
|  | 5363 | */ | 
|  | 5364 | case MBEDTLS_SSL_PRESET_SUITEB: | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5365 |  | 
| Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 5366 | conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites; | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5367 |  | 
|  | 5368 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 5369 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5370 | #endif | 
|  | 5371 |  | 
| Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5372 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5373 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5374 | if (mbedtls_ssl_conf_is_tls12_only(conf)) { | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5375 | conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5376 | } else | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5377 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5378 | conf->sig_algs = ssl_preset_suiteb_sig_algs; | 
| Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5379 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5380 |  | 
| Manuel Pégourié-Gonnard | f07ce3b | 2023-09-22 11:53:41 +0200 | [diff] [blame] | 5381 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5382 | conf->curve_list = NULL; | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5383 | #endif | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5384 | conf->group_list = ssl_preset_suiteb_groups; | 
| Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 5385 | break; | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5386 |  | 
|  | 5387 | /* | 
|  | 5388 | * Default | 
|  | 5389 | */ | 
|  | 5390 | default: | 
| Ronald Cron | f660655 | 2022-03-15 11:23:25 +0100 | [diff] [blame] | 5391 |  | 
| Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 5392 | conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites(); | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5393 |  | 
|  | 5394 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 5395 | conf->cert_profile = &mbedtls_x509_crt_profile_default; | 
|  | 5396 | #endif | 
|  | 5397 |  | 
| Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5398 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5399 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5400 | if (mbedtls_ssl_conf_is_tls12_only(conf)) { | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5401 | conf->sig_algs = ssl_tls12_preset_default_sig_algs; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5402 | } else | 
| Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5403 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5404 | conf->sig_algs = ssl_preset_default_sig_algs; | 
| Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5405 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5406 |  | 
| Manuel Pégourié-Gonnard | f07ce3b | 2023-09-22 11:53:41 +0200 | [diff] [blame] | 5407 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5408 | conf->curve_list = NULL; | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5409 | #endif | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5410 | conf->group_list = ssl_preset_default_groups; | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5411 |  | 
|  | 5412 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 5413 | conf->dhm_min_bitlen = 1024; | 
|  | 5414 | #endif | 
|  | 5415 | } | 
|  | 5416 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5417 | return 0; | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5418 | } | 
|  | 5419 |  | 
|  | 5420 | /* | 
|  | 5421 | * Free mbedtls_ssl_config | 
|  | 5422 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5423 | void mbedtls_ssl_config_free(mbedtls_ssl_config *conf) | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5424 | { | 
|  | 5425 | #if defined(MBEDTLS_DHM_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5426 | mbedtls_mpi_free(&conf->dhm_P); | 
|  | 5427 | mbedtls_mpi_free(&conf->dhm_G); | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5428 | #endif | 
|  | 5429 |  | 
| Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 5430 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) | 
| Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 5431 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5432 | if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) { | 
| Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 5433 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 5434 | } | 
| Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5435 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5436 | if (conf->psk != NULL) { | 
| Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 5437 | mbedtls_zeroize_and_free(conf->psk, conf->psk_len); | 
| Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 5438 | conf->psk = NULL; | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5439 | conf->psk_len = 0; | 
| junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 5440 | } | 
|  | 5441 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5442 | if (conf->psk_identity != NULL) { | 
| Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 5443 | mbedtls_zeroize_and_free(conf->psk_identity, conf->psk_identity_len); | 
| Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 5444 | conf->psk_identity = NULL; | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5445 | conf->psk_identity_len = 0; | 
|  | 5446 | } | 
| Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 5447 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5448 |  | 
|  | 5449 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5450 | ssl_key_cert_free(conf->key_cert); | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5451 | #endif | 
|  | 5452 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5453 | mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config)); | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5454 | } | 
|  | 5455 |  | 
| Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 5456 | #if defined(MBEDTLS_PK_C) && \ | 
| Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 5457 | (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED)) | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5458 | /* | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5459 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5460 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5461 | unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk) | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5462 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5463 | #if defined(MBEDTLS_RSA_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5464 | if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) { | 
|  | 5465 | return MBEDTLS_SSL_SIG_RSA; | 
|  | 5466 | } | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5467 | #endif | 
| Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 5468 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5469 | if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) { | 
|  | 5470 | return MBEDTLS_SSL_SIG_ECDSA; | 
|  | 5471 | } | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5472 | #endif | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5473 | return MBEDTLS_SSL_SIG_ANON; | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5474 | } | 
|  | 5475 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5476 | unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type) | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 5477 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5478 | switch (type) { | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 5479 | case MBEDTLS_PK_RSA: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5480 | return MBEDTLS_SSL_SIG_RSA; | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 5481 | case MBEDTLS_PK_ECDSA: | 
|  | 5482 | case MBEDTLS_PK_ECKEY: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5483 | return MBEDTLS_SSL_SIG_ECDSA; | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 5484 | default: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5485 | return MBEDTLS_SSL_SIG_ANON; | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 5486 | } | 
|  | 5487 | } | 
|  | 5488 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5489 | mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig) | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5490 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5491 | switch (sig) { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5492 | #if defined(MBEDTLS_RSA_C) | 
|  | 5493 | case MBEDTLS_SSL_SIG_RSA: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5494 | return MBEDTLS_PK_RSA; | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5495 | #endif | 
| Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 5496 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5497 | case MBEDTLS_SSL_SIG_ECDSA: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5498 | return MBEDTLS_PK_ECDSA; | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5499 | #endif | 
|  | 5500 | default: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5501 | return MBEDTLS_PK_NONE; | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5502 | } | 
|  | 5503 | } | 
| Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 5504 | #endif /* MBEDTLS_PK_C && | 
|  | 5505 | ( MBEDTLS_RSA_C || MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED ) */ | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5506 |  | 
| Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 5507 | /* | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5508 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX | 
| Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 5509 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5510 | mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash) | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5511 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5512 | switch (hash) { | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5513 | #if defined(MBEDTLS_MD_CAN_MD5) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5514 | case MBEDTLS_SSL_HASH_MD5: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5515 | return MBEDTLS_MD_MD5; | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5516 | #endif | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5517 | #if defined(MBEDTLS_MD_CAN_SHA1) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5518 | case MBEDTLS_SSL_HASH_SHA1: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5519 | return MBEDTLS_MD_SHA1; | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5520 | #endif | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5521 | #if defined(MBEDTLS_MD_CAN_SHA224) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5522 | case MBEDTLS_SSL_HASH_SHA224: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5523 | return MBEDTLS_MD_SHA224; | 
| Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 5524 | #endif | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5525 | #if defined(MBEDTLS_MD_CAN_SHA256) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5526 | case MBEDTLS_SSL_HASH_SHA256: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5527 | return MBEDTLS_MD_SHA256; | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5528 | #endif | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5529 | #if defined(MBEDTLS_MD_CAN_SHA384) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5530 | case MBEDTLS_SSL_HASH_SHA384: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5531 | return MBEDTLS_MD_SHA384; | 
| Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 5532 | #endif | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5533 | #if defined(MBEDTLS_MD_CAN_SHA512) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5534 | case MBEDTLS_SSL_HASH_SHA512: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5535 | return MBEDTLS_MD_SHA512; | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5536 | #endif | 
|  | 5537 | default: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5538 | return MBEDTLS_MD_NONE; | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5539 | } | 
|  | 5540 | } | 
|  | 5541 |  | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5542 | /* | 
|  | 5543 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX | 
|  | 5544 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5545 | unsigned char mbedtls_ssl_hash_from_md_alg(int md) | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5546 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5547 | switch (md) { | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5548 | #if defined(MBEDTLS_MD_CAN_MD5) | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5549 | case MBEDTLS_MD_MD5: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5550 | return MBEDTLS_SSL_HASH_MD5; | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5551 | #endif | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5552 | #if defined(MBEDTLS_MD_CAN_SHA1) | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5553 | case MBEDTLS_MD_SHA1: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5554 | return MBEDTLS_SSL_HASH_SHA1; | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5555 | #endif | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5556 | #if defined(MBEDTLS_MD_CAN_SHA224) | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5557 | case MBEDTLS_MD_SHA224: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5558 | return MBEDTLS_SSL_HASH_SHA224; | 
| Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 5559 | #endif | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5560 | #if defined(MBEDTLS_MD_CAN_SHA256) | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5561 | case MBEDTLS_MD_SHA256: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5562 | return MBEDTLS_SSL_HASH_SHA256; | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5563 | #endif | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5564 | #if defined(MBEDTLS_MD_CAN_SHA384) | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5565 | case MBEDTLS_MD_SHA384: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5566 | return MBEDTLS_SSL_HASH_SHA384; | 
| Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 5567 | #endif | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5568 | #if defined(MBEDTLS_MD_CAN_SHA512) | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5569 | case MBEDTLS_MD_SHA512: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5570 | return MBEDTLS_SSL_HASH_SHA512; | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5571 | #endif | 
|  | 5572 | default: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5573 | return MBEDTLS_SSL_HASH_NONE; | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5574 | } | 
|  | 5575 | } | 
|  | 5576 |  | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5577 | /* | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5578 | * Check if a curve proposed by the peer is in our list. | 
| Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 5579 | * Return 0 if we're willing to use it, -1 otherwise. | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5580 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5581 | int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id) | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5582 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5583 | const uint16_t *group_list = mbedtls_ssl_get_groups(ssl); | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5584 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5585 | if (group_list == NULL) { | 
|  | 5586 | return -1; | 
| Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5587 | } | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5588 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5589 | for (; *group_list != 0; group_list++) { | 
|  | 5590 | if (*group_list == tls_id) { | 
|  | 5591 | return 0; | 
|  | 5592 | } | 
|  | 5593 | } | 
|  | 5594 |  | 
|  | 5595 | return -1; | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5596 | } | 
| Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 5597 |  | 
| Valerio Setti | 49e6907 | 2023-06-27 17:27:51 +0200 | [diff] [blame] | 5598 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) | 
| Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 5599 | /* | 
|  | 5600 | * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id. | 
|  | 5601 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5602 | int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id) | 
| Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 5603 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5604 | uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id); | 
| Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 5605 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5606 | if (tls_id == 0) { | 
| Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 5607 | return -1; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5608 | } | 
| Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 5609 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5610 | return mbedtls_ssl_check_curve_tls_id(ssl, tls_id); | 
| Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 5611 | } | 
| Valerio Setti | 49e6907 | 2023-06-27 17:27:51 +0200 | [diff] [blame] | 5612 | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ | 
| Valerio Setti | 67419f0 | 2023-01-04 16:12:42 +0100 | [diff] [blame] | 5613 |  | 
| Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5614 | static const struct { | 
|  | 5615 | uint16_t tls_id; | 
|  | 5616 | mbedtls_ecp_group_id ecp_group_id; | 
|  | 5617 | psa_ecc_family_t psa_family; | 
|  | 5618 | uint16_t bits; | 
| Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5619 | } tls_id_match_table[] = | 
|  | 5620 | { | 
| Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 5621 | #if defined(MBEDTLS_ECP_HAVE_SECP521R1) | 
| Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 5622 | { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521 }, | 
| Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5623 | #endif | 
| Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 5624 | #if defined(MBEDTLS_ECP_HAVE_BP512R1) | 
| Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 5625 | { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512 }, | 
| Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5626 | #endif | 
| Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 5627 | #if defined(MBEDTLS_ECP_HAVE_SECP384R1) | 
| Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 5628 | { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384 }, | 
| Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5629 | #endif | 
| Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 5630 | #if defined(MBEDTLS_ECP_HAVE_BP384R1) | 
| Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 5631 | { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384 }, | 
| Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5632 | #endif | 
| Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 5633 | #if defined(MBEDTLS_ECP_HAVE_SECP256R1) | 
| Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 5634 | { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256 }, | 
| Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5635 | #endif | 
| Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 5636 | #if defined(MBEDTLS_ECP_HAVE_SECP256K1) | 
| Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 5637 | { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256 }, | 
| Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5638 | #endif | 
| Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 5639 | #if defined(MBEDTLS_ECP_HAVE_BP256R1) | 
| Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 5640 | { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256 }, | 
| Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5641 | #endif | 
| Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 5642 | #if defined(MBEDTLS_ECP_HAVE_SECP224R1) | 
| Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 5643 | { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224 }, | 
| Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5644 | #endif | 
| Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 5645 | #if defined(MBEDTLS_ECP_HAVE_SECP224K1) | 
| Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 5646 | { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224 }, | 
| Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5647 | #endif | 
| Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 5648 | #if defined(MBEDTLS_ECP_HAVE_SECP192R1) | 
| Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 5649 | { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192 }, | 
| Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5650 | #endif | 
| Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 5651 | #if defined(MBEDTLS_ECP_HAVE_SECP192K1) | 
| Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 5652 | { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192 }, | 
| Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5653 | #endif | 
| Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 5654 | #if defined(MBEDTLS_ECP_HAVE_CURVE25519) | 
| Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 5655 | { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255 }, | 
| Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5656 | #endif | 
| Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 5657 | #if defined(MBEDTLS_ECP_HAVE_CURVE448) | 
| Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 5658 | { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448 }, | 
| Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5659 | #endif | 
| Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 5660 | { 0, MBEDTLS_ECP_DP_NONE, 0, 0 }, | 
| Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5661 | }; | 
|  | 5662 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5663 | int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id, | 
| Przemek Stekiel | da4fba6 | 2023-06-02 14:52:28 +0200 | [diff] [blame] | 5664 | psa_key_type_t *type, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5665 | size_t *bits) | 
| Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5666 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5667 | for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) { | 
|  | 5668 | if (tls_id_match_table[i].tls_id == tls_id) { | 
| Przemek Stekiel | da4fba6 | 2023-06-02 14:52:28 +0200 | [diff] [blame] | 5669 | if (type != NULL) { | 
|  | 5670 | *type = PSA_KEY_TYPE_ECC_KEY_PAIR(tls_id_match_table[i].psa_family); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5671 | } | 
|  | 5672 | if (bits != NULL) { | 
| Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5673 | *bits = tls_id_match_table[i].bits; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5674 | } | 
| Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5675 | return PSA_SUCCESS; | 
|  | 5676 | } | 
|  | 5677 | } | 
|  | 5678 |  | 
|  | 5679 | return PSA_ERROR_NOT_SUPPORTED; | 
|  | 5680 | } | 
|  | 5681 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5682 | mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id) | 
| Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5683 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5684 | for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) { | 
|  | 5685 | if (tls_id_match_table[i].tls_id == tls_id) { | 
| Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5686 | return tls_id_match_table[i].ecp_group_id; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5687 | } | 
| Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5688 | } | 
|  | 5689 |  | 
|  | 5690 | return MBEDTLS_ECP_DP_NONE; | 
|  | 5691 | } | 
|  | 5692 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5693 | uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id) | 
| Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5694 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5695 | for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE; | 
|  | 5696 | i++) { | 
|  | 5697 | if (tls_id_match_table[i].ecp_group_id == grp_id) { | 
| Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5698 | return tls_id_match_table[i].tls_id; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5699 | } | 
| Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5700 | } | 
|  | 5701 |  | 
|  | 5702 | return 0; | 
|  | 5703 | } | 
|  | 5704 |  | 
| Valerio Setti | 67419f0 | 2023-01-04 16:12:42 +0100 | [diff] [blame] | 5705 | #if defined(MBEDTLS_DEBUG_C) | 
| Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 5706 | static const struct { | 
|  | 5707 | uint16_t tls_id; | 
|  | 5708 | const char *name; | 
|  | 5709 | } tls_id_curve_name_table[] = | 
|  | 5710 | { | 
| Valerio Setti | 54e2379 | 2023-07-07 10:49:27 +0200 | [diff] [blame] | 5711 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, "secp521r1" }, | 
|  | 5712 | { MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, "brainpoolP512r1" }, | 
|  | 5713 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, "secp384r1" }, | 
|  | 5714 | { MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, "brainpoolP384r1" }, | 
|  | 5715 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, "secp256r1" }, | 
|  | 5716 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256K1, "secp256k1" }, | 
|  | 5717 | { MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, "brainpoolP256r1" }, | 
|  | 5718 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, "secp224r1" }, | 
|  | 5719 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224K1, "secp224k1" }, | 
|  | 5720 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, "secp192r1" }, | 
|  | 5721 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192K1, "secp192k1" }, | 
|  | 5722 | { MBEDTLS_SSL_IANA_TLS_GROUP_X25519, "x25519" }, | 
|  | 5723 | { MBEDTLS_SSL_IANA_TLS_GROUP_X448, "x448" }, | 
| Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 5724 | { 0, NULL }, | 
|  | 5725 | }; | 
|  | 5726 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5727 | const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id) | 
| Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5728 | { | 
| Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 5729 | for (int i = 0; tls_id_curve_name_table[i].tls_id != 0; i++) { | 
|  | 5730 | if (tls_id_curve_name_table[i].tls_id == tls_id) { | 
|  | 5731 | return tls_id_curve_name_table[i].name; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5732 | } | 
| Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5733 | } | 
|  | 5734 |  | 
|  | 5735 | return NULL; | 
|  | 5736 | } | 
| Valerio Setti | 67419f0 | 2023-01-04 16:12:42 +0100 | [diff] [blame] | 5737 | #endif | 
| Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5738 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5739 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5740 | int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert, | 
|  | 5741 | const mbedtls_ssl_ciphersuite_t *ciphersuite, | 
|  | 5742 | int cert_endpoint, | 
|  | 5743 | uint32_t *flags) | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5744 | { | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5745 | int ret = 0; | 
| Dave Rodgman | c37ad44 | 2023-11-03 23:36:06 +0000 | [diff] [blame] | 5746 | unsigned int usage = 0; | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5747 | const char *ext_oid; | 
|  | 5748 | size_t ext_len; | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5749 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5750 | if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) { | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5751 | /* Server part of the key exchange */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5752 | switch (ciphersuite->key_exchange) { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5753 | case MBEDTLS_KEY_EXCHANGE_RSA: | 
|  | 5754 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: | 
| Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5755 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5756 | break; | 
|  | 5757 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5758 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: | 
|  | 5759 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: | 
|  | 5760 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: | 
|  | 5761 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5762 | break; | 
|  | 5763 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5764 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: | 
|  | 5765 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: | 
| Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5766 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5767 | break; | 
|  | 5768 |  | 
|  | 5769 | /* Don't use default: we want warnings when adding new values */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5770 | case MBEDTLS_KEY_EXCHANGE_NONE: | 
|  | 5771 | case MBEDTLS_KEY_EXCHANGE_PSK: | 
|  | 5772 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: | 
|  | 5773 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: | 
| Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 5774 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5775 | usage = 0; | 
|  | 5776 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5777 | } else { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5778 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ | 
|  | 5779 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5780 | } | 
|  | 5781 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5782 | if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) { | 
| Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5783 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5784 | ret = -1; | 
|  | 5785 | } | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5786 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5787 | if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5788 | ext_oid = MBEDTLS_OID_SERVER_AUTH; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5789 | ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH); | 
|  | 5790 | } else { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5791 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5792 | ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH); | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5793 | } | 
|  | 5794 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5795 | if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) { | 
| Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5796 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5797 | ret = -1; | 
|  | 5798 | } | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5799 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5800 | return ret; | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5801 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5802 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 5803 |  | 
| Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5804 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5805 | int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl, | 
|  | 5806 | const mbedtls_md_type_t md, | 
|  | 5807 | unsigned char *dst, | 
|  | 5808 | size_t dst_len, | 
|  | 5809 | size_t *olen) | 
| Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5810 | { | 
| Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5811 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 5812 | psa_hash_operation_t *hash_operation_to_clone; | 
|  | 5813 | psa_hash_operation_t hash_operation = psa_hash_operation_init(); | 
|  | 5814 |  | 
| Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5815 | *olen = 0; | 
| Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5816 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5817 | switch (md) { | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5818 | #if defined(MBEDTLS_MD_CAN_SHA384) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5819 | case MBEDTLS_MD_SHA384: | 
|  | 5820 | hash_operation_to_clone = &ssl->handshake->fin_sha384_psa; | 
|  | 5821 | break; | 
| Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5822 | #endif | 
|  | 5823 |  | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5824 | #if defined(MBEDTLS_MD_CAN_SHA256) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5825 | case MBEDTLS_MD_SHA256: | 
|  | 5826 | hash_operation_to_clone = &ssl->handshake->fin_sha256_psa; | 
|  | 5827 | break; | 
| Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5828 | #endif | 
|  | 5829 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5830 | default: | 
|  | 5831 | goto exit; | 
|  | 5832 | } | 
|  | 5833 |  | 
|  | 5834 | status = psa_hash_clone(hash_operation_to_clone, &hash_operation); | 
|  | 5835 | if (status != PSA_SUCCESS) { | 
| Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5836 | goto exit; | 
|  | 5837 | } | 
|  | 5838 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5839 | status = psa_hash_finish(&hash_operation, dst, dst_len, olen); | 
|  | 5840 | if (status != PSA_SUCCESS) { | 
| Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5841 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5842 | } | 
| Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5843 |  | 
|  | 5844 | exit: | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5845 | #if !defined(MBEDTLS_MD_CAN_SHA384) && \ | 
|  | 5846 | !defined(MBEDTLS_MD_CAN_SHA256) | 
| Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 5847 | (void) ssl; | 
|  | 5848 | #endif | 
| Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 5849 | return PSA_TO_MBEDTLS_ERR(status); | 
| Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5850 | } | 
|  | 5851 | #else /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 5852 |  | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5853 | #if defined(MBEDTLS_MD_CAN_SHA384) | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5854 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5855 | static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl, | 
|  | 5856 | unsigned char *dst, | 
|  | 5857 | size_t dst_len, | 
|  | 5858 | size_t *olen) | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5859 | { | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5860 | int ret; | 
| Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame] | 5861 | mbedtls_md_context_t sha384; | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5862 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5863 | if (dst_len < 48) { | 
|  | 5864 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 
|  | 5865 | } | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5866 |  | 
| Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame] | 5867 | mbedtls_md_init(&sha384); | 
|  | 5868 | ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0); | 
| Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 5869 | if (ret != 0) { | 
|  | 5870 | goto exit; | 
|  | 5871 | } | 
| Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame] | 5872 | ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384); | 
| Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 5873 | if (ret != 0) { | 
|  | 5874 | goto exit; | 
|  | 5875 | } | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5876 |  | 
| Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame] | 5877 | if ((ret = mbedtls_md_finish(&sha384, dst)) != 0) { | 
| Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 5878 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret); | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5879 | goto exit; | 
|  | 5880 | } | 
|  | 5881 |  | 
|  | 5882 | *olen = 48; | 
|  | 5883 |  | 
|  | 5884 | exit: | 
|  | 5885 |  | 
| Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame] | 5886 | mbedtls_md_free(&sha384); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5887 | return ret; | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5888 | } | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5889 | #endif /* MBEDTLS_MD_CAN_SHA384 */ | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5890 |  | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5891 | #if defined(MBEDTLS_MD_CAN_SHA256) | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5892 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5893 | static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl, | 
|  | 5894 | unsigned char *dst, | 
|  | 5895 | size_t dst_len, | 
|  | 5896 | size_t *olen) | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5897 | { | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5898 | int ret; | 
| Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 5899 | mbedtls_md_context_t sha256; | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5900 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5901 | if (dst_len < 32) { | 
|  | 5902 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 
|  | 5903 | } | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5904 |  | 
| Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 5905 | mbedtls_md_init(&sha256); | 
|  | 5906 | ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0); | 
|  | 5907 | if (ret != 0) { | 
|  | 5908 | goto exit; | 
|  | 5909 | } | 
|  | 5910 | ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256); | 
|  | 5911 | if (ret != 0) { | 
|  | 5912 | goto exit; | 
|  | 5913 | } | 
| Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5914 |  | 
| Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 5915 | if ((ret = mbedtls_md_finish(&sha256, dst)) != 0) { | 
|  | 5916 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret); | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5917 | goto exit; | 
|  | 5918 | } | 
|  | 5919 |  | 
|  | 5920 | *olen = 32; | 
|  | 5921 |  | 
|  | 5922 | exit: | 
|  | 5923 |  | 
| Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 5924 | mbedtls_md_free(&sha256); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5925 | return ret; | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5926 | } | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5927 | #endif /* MBEDTLS_MD_CAN_SHA256 */ | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5928 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5929 | int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl, | 
|  | 5930 | const mbedtls_md_type_t md, | 
|  | 5931 | unsigned char *dst, | 
|  | 5932 | size_t dst_len, | 
|  | 5933 | size_t *olen) | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5934 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5935 | switch (md) { | 
| Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5936 |  | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5937 | #if defined(MBEDTLS_MD_CAN_SHA384) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5938 | case MBEDTLS_MD_SHA384: | 
|  | 5939 | return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen); | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5940 | #endif /* MBEDTLS_MD_CAN_SHA384*/ | 
| Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5941 |  | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5942 | #if defined(MBEDTLS_MD_CAN_SHA256) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5943 | case MBEDTLS_MD_SHA256: | 
|  | 5944 | return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen); | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5945 | #endif /* MBEDTLS_MD_CAN_SHA256*/ | 
| Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5946 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5947 | default: | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5948 | #if !defined(MBEDTLS_MD_CAN_SHA384) && \ | 
|  | 5949 | !defined(MBEDTLS_MD_CAN_SHA256) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5950 | (void) ssl; | 
|  | 5951 | (void) dst; | 
|  | 5952 | (void) dst_len; | 
|  | 5953 | (void) olen; | 
| Andrzej Kurek | 409248a | 2022-10-24 10:33:21 -0400 | [diff] [blame] | 5954 | #endif | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5955 | break; | 
| Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5956 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5957 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5958 | } | 
| XiaokangQian | 647719a | 2021-12-07 09:16:29 +0000 | [diff] [blame] | 5959 |  | 
| Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5960 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ | 
| Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5961 |  | 
| Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5962 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) | 
| Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5963 | /* mbedtls_ssl_parse_sig_alg_ext() | 
|  | 5964 | * | 
|  | 5965 | * The `extension_data` field of signature algorithm contains  a `SignatureSchemeList` | 
|  | 5966 | * value (TLS 1.3 RFC8446): | 
|  | 5967 | *      enum { | 
|  | 5968 | *         .... | 
|  | 5969 | *        ecdsa_secp256r1_sha256( 0x0403 ), | 
|  | 5970 | *        ecdsa_secp384r1_sha384( 0x0503 ), | 
|  | 5971 | *        ecdsa_secp521r1_sha512( 0x0603 ), | 
|  | 5972 | *         .... | 
|  | 5973 | *      } SignatureScheme; | 
|  | 5974 | * | 
|  | 5975 | *      struct { | 
|  | 5976 | *         SignatureScheme supported_signature_algorithms<2..2^16-2>; | 
|  | 5977 | *      } SignatureSchemeList; | 
|  | 5978 | * | 
|  | 5979 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` | 
|  | 5980 | * value (TLS 1.2 RFC5246): | 
|  | 5981 | *      enum { | 
|  | 5982 | *          none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), | 
|  | 5983 | *          sha512(6), (255) | 
|  | 5984 | *      } HashAlgorithm; | 
|  | 5985 | * | 
|  | 5986 | *      enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } | 
|  | 5987 | *        SignatureAlgorithm; | 
|  | 5988 | * | 
|  | 5989 | *      struct { | 
|  | 5990 | *          HashAlgorithm hash; | 
|  | 5991 | *          SignatureAlgorithm signature; | 
|  | 5992 | *      } SignatureAndHashAlgorithm; | 
|  | 5993 | * | 
|  | 5994 | *      SignatureAndHashAlgorithm | 
|  | 5995 | *        supported_signature_algorithms<2..2^16-2>; | 
|  | 5996 | * | 
|  | 5997 | * The TLS 1.3 signature algorithm extension was defined to be a compatible | 
|  | 5998 | * generalization of the TLS 1.2 signature algorithm extension. | 
|  | 5999 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by | 
|  | 6000 | * `SignatureScheme` field of TLS 1.3 | 
|  | 6001 | * | 
|  | 6002 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6003 | int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl, | 
|  | 6004 | const unsigned char *buf, | 
|  | 6005 | const unsigned char *end) | 
| Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6006 | { | 
|  | 6007 | const unsigned char *p = buf; | 
|  | 6008 | size_t supported_sig_algs_len = 0; | 
|  | 6009 | const unsigned char *supported_sig_algs_end; | 
|  | 6010 | uint16_t sig_alg; | 
|  | 6011 | uint32_t common_idx = 0; | 
|  | 6012 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6013 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2); | 
|  | 6014 | supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0); | 
| Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6015 | p += 2; | 
|  | 6016 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6017 | memset(ssl->handshake->received_sig_algs, 0, | 
|  | 6018 | sizeof(ssl->handshake->received_sig_algs)); | 
| Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6019 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6020 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len); | 
| Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6021 | supported_sig_algs_end = p + supported_sig_algs_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6022 | while (p < supported_sig_algs_end) { | 
|  | 6023 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2); | 
|  | 6024 | sig_alg = MBEDTLS_GET_UINT16_BE(p, 0); | 
| Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6025 | p += 2; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6026 | MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s", | 
|  | 6027 | sig_alg, | 
|  | 6028 | mbedtls_ssl_sig_alg_to_str(sig_alg))); | 
| Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 6029 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6030 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 && | 
|  | 6031 | (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) && | 
|  | 6032 | mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) { | 
| Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6033 | continue; | 
| Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 6034 | } | 
|  | 6035 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6036 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6037 | MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s", | 
|  | 6038 | mbedtls_ssl_sig_alg_to_str(sig_alg))); | 
| Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6039 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6040 | if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) { | 
| Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6041 | ssl->handshake->received_sig_algs[common_idx] = sig_alg; | 
|  | 6042 | common_idx += 1; | 
|  | 6043 | } | 
|  | 6044 | } | 
|  | 6045 | /* Check that we consumed all the message. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6046 | if (p != end) { | 
|  | 6047 | MBEDTLS_SSL_DEBUG_MSG(1, | 
|  | 6048 | ("Signature algorithms extension length misaligned")); | 
|  | 6049 | MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, | 
|  | 6050 | MBEDTLS_ERR_SSL_DECODE_ERROR); | 
|  | 6051 | return MBEDTLS_ERR_SSL_DECODE_ERROR; | 
| Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6052 | } | 
|  | 6053 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6054 | if (common_idx == 0) { | 
|  | 6055 | MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common")); | 
|  | 6056 | MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE, | 
|  | 6057 | MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE); | 
|  | 6058 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; | 
| Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6059 | } | 
|  | 6060 |  | 
| Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 6061 | ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6062 | return 0; | 
| Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6063 | } | 
|  | 6064 |  | 
| Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 6065 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ | 
| Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6066 |  | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6067 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 6068 |  | 
|  | 6069 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 6070 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6071 | static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation, | 
|  | 6072 | mbedtls_svc_key_id_t key, | 
|  | 6073 | psa_algorithm_t alg, | 
|  | 6074 | const unsigned char *raw_psk, size_t raw_psk_length, | 
|  | 6075 | const unsigned char *seed, size_t seed_length, | 
|  | 6076 | const unsigned char *label, size_t label_length, | 
|  | 6077 | const unsigned char *other_secret, | 
|  | 6078 | size_t other_secret_length, | 
|  | 6079 | size_t capacity) | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6080 | { | 
|  | 6081 | psa_status_t status; | 
|  | 6082 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6083 | status = psa_key_derivation_setup(derivation, alg); | 
|  | 6084 | if (status != PSA_SUCCESS) { | 
|  | 6085 | return status; | 
|  | 6086 | } | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6087 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6088 | if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { | 
|  | 6089 | status = psa_key_derivation_input_bytes(derivation, | 
|  | 6090 | PSA_KEY_DERIVATION_INPUT_SEED, | 
|  | 6091 | seed, seed_length); | 
|  | 6092 | if (status != PSA_SUCCESS) { | 
|  | 6093 | return status; | 
| Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 6094 | } | 
|  | 6095 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6096 | if (other_secret != NULL) { | 
|  | 6097 | status = psa_key_derivation_input_bytes(derivation, | 
|  | 6098 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, | 
|  | 6099 | other_secret, other_secret_length); | 
|  | 6100 | if (status != PSA_SUCCESS) { | 
|  | 6101 | return status; | 
|  | 6102 | } | 
|  | 6103 | } | 
|  | 6104 |  | 
|  | 6105 | if (mbedtls_svc_key_id_is_null(key)) { | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6106 | status = psa_key_derivation_input_bytes( | 
|  | 6107 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6108 | raw_psk, raw_psk_length); | 
|  | 6109 | } else { | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6110 | status = psa_key_derivation_input_key( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6111 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key); | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6112 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6113 | if (status != PSA_SUCCESS) { | 
|  | 6114 | return status; | 
|  | 6115 | } | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6116 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6117 | status = psa_key_derivation_input_bytes(derivation, | 
|  | 6118 | PSA_KEY_DERIVATION_INPUT_LABEL, | 
|  | 6119 | label, label_length); | 
|  | 6120 | if (status != PSA_SUCCESS) { | 
|  | 6121 | return status; | 
|  | 6122 | } | 
|  | 6123 | } else { | 
|  | 6124 | return PSA_ERROR_NOT_SUPPORTED; | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6125 | } | 
|  | 6126 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6127 | status = psa_key_derivation_set_capacity(derivation, capacity); | 
|  | 6128 | if (status != PSA_SUCCESS) { | 
|  | 6129 | return status; | 
|  | 6130 | } | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6131 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6132 | return PSA_SUCCESS; | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6133 | } | 
|  | 6134 |  | 
| Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 6135 | #if defined(PSA_WANT_ALG_SHA_384) || \ | 
|  | 6136 | defined(PSA_WANT_ALG_SHA_256) | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6137 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6138 | static int tls_prf_generic(mbedtls_md_type_t md_type, | 
|  | 6139 | const unsigned char *secret, size_t slen, | 
|  | 6140 | const char *label, | 
|  | 6141 | const unsigned char *random, size_t rlen, | 
|  | 6142 | unsigned char *dstbuf, size_t dlen) | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6143 | { | 
|  | 6144 | psa_status_t status; | 
|  | 6145 | psa_algorithm_t alg; | 
|  | 6146 | mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT; | 
|  | 6147 | psa_key_derivation_operation_t derivation = | 
|  | 6148 | PSA_KEY_DERIVATION_OPERATION_INIT; | 
|  | 6149 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6150 | if (md_type == MBEDTLS_MD_SHA384) { | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6151 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6152 | } else { | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6153 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6154 | } | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6155 |  | 
|  | 6156 | /* Normally a "secret" should be long enough to be impossible to | 
|  | 6157 | * find by brute force, and in particular should not be empty. But | 
|  | 6158 | * this PRF is also used to derive an IV, in particular in EAP-TLS, | 
|  | 6159 | * and for this use case it makes sense to have a 0-length "secret". | 
|  | 6160 | * Since the key API doesn't allow importing a key of length 0, | 
|  | 6161 | * keep master_key=0, which setup_psa_key_derivation() understands | 
|  | 6162 | * to mean a 0-length "secret" input. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6163 | if (slen != 0) { | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6164 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6165 | psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE); | 
|  | 6166 | psa_set_key_algorithm(&key_attributes, alg); | 
|  | 6167 | psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE); | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6168 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6169 | status = psa_import_key(&key_attributes, secret, slen, &master_key); | 
|  | 6170 | if (status != PSA_SUCCESS) { | 
|  | 6171 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; | 
|  | 6172 | } | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6173 | } | 
|  | 6174 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6175 | status = setup_psa_key_derivation(&derivation, | 
|  | 6176 | master_key, alg, | 
|  | 6177 | NULL, 0, | 
|  | 6178 | random, rlen, | 
|  | 6179 | (unsigned char const *) label, | 
|  | 6180 | (size_t) strlen(label), | 
|  | 6181 | NULL, 0, | 
|  | 6182 | dlen); | 
|  | 6183 | if (status != PSA_SUCCESS) { | 
|  | 6184 | psa_key_derivation_abort(&derivation); | 
|  | 6185 | psa_destroy_key(master_key); | 
|  | 6186 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6187 | } | 
|  | 6188 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6189 | status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen); | 
|  | 6190 | if (status != PSA_SUCCESS) { | 
|  | 6191 | psa_key_derivation_abort(&derivation); | 
|  | 6192 | psa_destroy_key(master_key); | 
|  | 6193 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6194 | } | 
|  | 6195 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6196 | status = psa_key_derivation_abort(&derivation); | 
|  | 6197 | if (status != PSA_SUCCESS) { | 
|  | 6198 | psa_destroy_key(master_key); | 
|  | 6199 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6200 | } | 
|  | 6201 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6202 | if (!mbedtls_svc_key_id_is_null(master_key)) { | 
|  | 6203 | status = psa_destroy_key(master_key); | 
|  | 6204 | } | 
|  | 6205 | if (status != PSA_SUCCESS) { | 
|  | 6206 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; | 
|  | 6207 | } | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6208 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6209 | return 0; | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6210 | } | 
| Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 6211 | #endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */ | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6212 | #else /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 6213 |  | 
| Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 6214 | #if defined(MBEDTLS_MD_C) &&       \ | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6215 | (defined(MBEDTLS_MD_CAN_SHA256) || \ | 
|  | 6216 | defined(MBEDTLS_MD_CAN_SHA384)) | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6217 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6218 | static int tls_prf_generic(mbedtls_md_type_t md_type, | 
|  | 6219 | const unsigned char *secret, size_t slen, | 
|  | 6220 | const char *label, | 
|  | 6221 | const unsigned char *random, size_t rlen, | 
|  | 6222 | unsigned char *dstbuf, size_t dlen) | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6223 | { | 
|  | 6224 | size_t nb; | 
|  | 6225 | size_t i, j, k, md_len; | 
|  | 6226 | unsigned char *tmp; | 
|  | 6227 | size_t tmp_len = 0; | 
|  | 6228 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; | 
|  | 6229 | const mbedtls_md_info_t *md_info; | 
|  | 6230 | mbedtls_md_context_t md_ctx; | 
|  | 6231 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 6232 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6233 | mbedtls_md_init(&md_ctx); | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6234 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6235 | if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) { | 
|  | 6236 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 
|  | 6237 | } | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6238 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6239 | md_len = mbedtls_md_get_size(md_info); | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6240 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6241 | tmp_len = md_len + strlen(label) + rlen; | 
|  | 6242 | tmp = mbedtls_calloc(1, tmp_len); | 
|  | 6243 | if (tmp == NULL) { | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6244 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
|  | 6245 | goto exit; | 
|  | 6246 | } | 
|  | 6247 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6248 | nb = strlen(label); | 
|  | 6249 | memcpy(tmp + md_len, label, nb); | 
|  | 6250 | memcpy(tmp + md_len + nb, random, rlen); | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6251 | nb += rlen; | 
|  | 6252 |  | 
|  | 6253 | /* | 
|  | 6254 | * Compute P_<hash>(secret, label + random)[0..dlen] | 
|  | 6255 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6256 | if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) { | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6257 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6258 | } | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6259 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6260 | ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen); | 
|  | 6261 | if (ret != 0) { | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6262 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6263 | } | 
|  | 6264 | ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb); | 
|  | 6265 | if (ret != 0) { | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6266 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6267 | } | 
|  | 6268 | ret = mbedtls_md_hmac_finish(&md_ctx, tmp); | 
|  | 6269 | if (ret != 0) { | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6270 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6271 | } | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6272 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6273 | for (i = 0; i < dlen; i += md_len) { | 
|  | 6274 | ret = mbedtls_md_hmac_reset(&md_ctx); | 
|  | 6275 | if (ret != 0) { | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6276 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6277 | } | 
|  | 6278 | ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb); | 
|  | 6279 | if (ret != 0) { | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6280 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6281 | } | 
|  | 6282 | ret = mbedtls_md_hmac_finish(&md_ctx, h_i); | 
|  | 6283 | if (ret != 0) { | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6284 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6285 | } | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6286 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6287 | ret = mbedtls_md_hmac_reset(&md_ctx); | 
|  | 6288 | if (ret != 0) { | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6289 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6290 | } | 
|  | 6291 | ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len); | 
|  | 6292 | if (ret != 0) { | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6293 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6294 | } | 
|  | 6295 | ret = mbedtls_md_hmac_finish(&md_ctx, tmp); | 
|  | 6296 | if (ret != 0) { | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6297 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6298 | } | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6299 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6300 | k = (i + md_len > dlen) ? dlen % md_len : md_len; | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6301 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6302 | for (j = 0; j < k; j++) { | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6303 | dstbuf[i + j]  = h_i[j]; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6304 | } | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6305 | } | 
|  | 6306 |  | 
|  | 6307 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6308 | mbedtls_md_free(&md_ctx); | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6309 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6310 | if (tmp != NULL) { | 
|  | 6311 | mbedtls_platform_zeroize(tmp, tmp_len); | 
|  | 6312 | } | 
| Dave Rodgman | 29b9b2b | 2022-11-01 16:08:14 +0000 | [diff] [blame] | 6313 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6314 | mbedtls_platform_zeroize(h_i, sizeof(h_i)); | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6315 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6316 | mbedtls_free(tmp); | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6317 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6318 | return ret; | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6319 | } | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6320 | #endif /* MBEDTLS_MD_C && ( MBEDTLS_MD_CAN_SHA256 || MBEDTLS_MD_CAN_SHA384 ) */ | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6321 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 6322 |  | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6323 | #if defined(MBEDTLS_MD_CAN_SHA256) | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6324 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6325 | static int tls_prf_sha256(const unsigned char *secret, size_t slen, | 
|  | 6326 | const char *label, | 
|  | 6327 | const unsigned char *random, size_t rlen, | 
|  | 6328 | unsigned char *dstbuf, size_t dlen) | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6329 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6330 | return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen, | 
|  | 6331 | label, random, rlen, dstbuf, dlen); | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6332 | } | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6333 | #endif /* MBEDTLS_MD_CAN_SHA256*/ | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6334 |  | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6335 | #if defined(MBEDTLS_MD_CAN_SHA384) | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6336 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6337 | static int tls_prf_sha384(const unsigned char *secret, size_t slen, | 
|  | 6338 | const char *label, | 
|  | 6339 | const unsigned char *random, size_t rlen, | 
|  | 6340 | unsigned char *dstbuf, size_t dlen) | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6341 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6342 | return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen, | 
|  | 6343 | label, random, rlen, dstbuf, dlen); | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6344 | } | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6345 | #endif /* MBEDTLS_MD_CAN_SHA384*/ | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6346 |  | 
| Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6347 | /* | 
|  | 6348 | * Set appropriate PRF function and other SSL / TLS1.2 functions | 
|  | 6349 | * | 
|  | 6350 | * Inputs: | 
| Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6351 | * - hash associated with the ciphersuite (only used by TLS 1.2) | 
|  | 6352 | * | 
|  | 6353 | * Outputs: | 
|  | 6354 | * - the tls_prf, calc_verify and calc_finished members of handshake structure | 
|  | 6355 | */ | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6356 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6357 | static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake, | 
|  | 6358 | mbedtls_md_type_t hash) | 
| Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6359 | { | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6360 | #if defined(MBEDTLS_MD_CAN_SHA384) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6361 | if (hash == MBEDTLS_MD_SHA384) { | 
| Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6362 | handshake->tls_prf = tls_prf_sha384; | 
|  | 6363 | handshake->calc_verify = ssl_calc_verify_tls_sha384; | 
|  | 6364 | handshake->calc_finished = ssl_calc_finished_tls_sha384; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6365 | } else | 
| Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6366 | #endif | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6367 | #if defined(MBEDTLS_MD_CAN_SHA256) | 
| Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6368 | { | 
| Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 6369 | (void) hash; | 
| Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6370 | handshake->tls_prf = tls_prf_sha256; | 
|  | 6371 | handshake->calc_verify = ssl_calc_verify_tls_sha256; | 
|  | 6372 | handshake->calc_finished = ssl_calc_finished_tls_sha256; | 
|  | 6373 | } | 
| Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 6374 | #else | 
| Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6375 | { | 
| Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6376 | (void) handshake; | 
| Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 6377 | (void) hash; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6378 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 
| Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6379 | } | 
| Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 6380 | #endif | 
| Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6381 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6382 | return 0; | 
| Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6383 | } | 
| Jerry Yu | d6ab235 | 2022-02-17 14:03:43 +0800 | [diff] [blame] | 6384 |  | 
| Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6385 | /* | 
|  | 6386 | * Compute master secret if needed | 
|  | 6387 | * | 
|  | 6388 | * Parameters: | 
|  | 6389 | * [in/out] handshake | 
|  | 6390 | *          [in] resume, premaster, extended_ms, calc_verify, tls_prf | 
|  | 6391 | *               (PSA-PSK) ciphersuite_info, psk_opaque | 
|  | 6392 | *          [out] premaster (cleared) | 
|  | 6393 | * [out] master | 
|  | 6394 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK | 
|  | 6395 | *      debug: conf->f_dbg, conf->p_dbg | 
|  | 6396 | *      EMS: passed to calc_verify (debug + session_negotiate) | 
| Ronald Cron | a25cf58 | 2022-03-07 11:10:36 +0100 | [diff] [blame] | 6397 | *      PSA-PSA: conf | 
| Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6398 | */ | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6399 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6400 | static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake, | 
|  | 6401 | unsigned char *master, | 
|  | 6402 | const mbedtls_ssl_context *ssl) | 
| Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6403 | { | 
|  | 6404 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 6405 |  | 
|  | 6406 | /* cf. RFC 5246, Section 8.1: | 
|  | 6407 | * "The master secret is always exactly 48 bytes in length." */ | 
|  | 6408 | size_t const master_secret_len = 48; | 
|  | 6409 |  | 
|  | 6410 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
|  | 6411 | unsigned char session_hash[48]; | 
|  | 6412 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ | 
|  | 6413 |  | 
|  | 6414 | /* The label for the KDF used for key expansion. | 
|  | 6415 | * This is either "master secret" or "extended master secret" | 
|  | 6416 | * depending on whether the Extended Master Secret extension | 
|  | 6417 | * is used. */ | 
|  | 6418 | char const *lbl = "master secret"; | 
|  | 6419 |  | 
| Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 6420 | /* The seed for the KDF used for key expansion. | 
| Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6421 | * - If the Extended Master Secret extension is not used, | 
|  | 6422 | *   this is ClientHello.Random + ServerHello.Random | 
|  | 6423 | *   (see Sect. 8.1 in RFC 5246). | 
|  | 6424 | * - If the Extended Master Secret extension is used, | 
|  | 6425 | *   this is the transcript of the handshake so far. | 
|  | 6426 | *   (see Sect. 4 in RFC 7627). */ | 
| Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 6427 | unsigned char const *seed = handshake->randbytes; | 
|  | 6428 | size_t seed_len = 64; | 
| Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6429 |  | 
|  | 6430 | #if !defined(MBEDTLS_DEBUG_C) &&                    \ | 
|  | 6431 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ | 
|  | 6432 | !(defined(MBEDTLS_USE_PSA_CRYPTO) &&            \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6433 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) | 
| Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6434 | ssl = NULL; /* make sure we don't use it except for those cases */ | 
|  | 6435 | (void) ssl; | 
|  | 6436 | #endif | 
|  | 6437 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6438 | if (handshake->resume != 0) { | 
|  | 6439 | MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)")); | 
|  | 6440 | return 0; | 
| Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6441 | } | 
|  | 6442 |  | 
|  | 6443 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6444 | if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) { | 
| Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6445 | lbl  = "extended master secret"; | 
| Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 6446 | seed = session_hash; | 
| Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 6447 | ret = handshake->calc_verify(ssl, session_hash, &seed_len); | 
|  | 6448 | if (ret != 0) { | 
|  | 6449 | MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret); | 
|  | 6450 | } | 
| Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6451 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6452 | MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret", | 
|  | 6453 | session_hash, seed_len); | 
| Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6454 | } | 
| Przemek Stekiel | 169bf0b | 2022-04-29 07:53:29 +0200 | [diff] [blame] | 6455 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ | 
| Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6456 |  | 
| Przemek Stekiel | 99114f3 | 2022-04-22 11:20:09 +0200 | [diff] [blame] | 6457 | #if defined(MBEDTLS_USE_PSA_CRYPTO) &&                   \ | 
| Przemek Stekiel | 8a4b7fd | 2022-04-28 09:22:22 +0200 | [diff] [blame] | 6458 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6459 | if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) { | 
| Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6460 | /* Perform PSK-to-MS expansion in a single step. */ | 
|  | 6461 | psa_status_t status; | 
|  | 6462 | psa_algorithm_t alg; | 
|  | 6463 | mbedtls_svc_key_id_t psk; | 
|  | 6464 | psa_key_derivation_operation_t derivation = | 
|  | 6465 | PSA_KEY_DERIVATION_OPERATION_INIT; | 
| Dave Rodgman | 2eab462 | 2023-10-05 13:30:37 +0100 | [diff] [blame] | 6466 | mbedtls_md_type_t hash_alg = (mbedtls_md_type_t) handshake->ciphersuite_info->mac; | 
| Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6467 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6468 | MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion")); | 
| Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6469 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6470 | psk = mbedtls_ssl_get_opaque_psk(ssl); | 
| Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6471 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6472 | if (hash_alg == MBEDTLS_MD_SHA384) { | 
| Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6473 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6474 | } else { | 
| Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6475 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6476 | } | 
| Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6477 |  | 
| Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 6478 | size_t other_secret_len = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6479 | unsigned char *other_secret = NULL; | 
| Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 6480 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6481 | switch (handshake->ciphersuite_info->key_exchange) { | 
| Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6482 | /* Provide other secret. | 
| Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 6483 | * Other secret is stored in premaster, where first 2 bytes hold the | 
| Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6484 | * length of the other key. | 
| Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 6485 | */ | 
| Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6486 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: | 
| Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 6487 | /* For RSA-PSK other key length is always 48 bytes. */ | 
| Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6488 | other_secret_len = 48; | 
|  | 6489 | other_secret = handshake->premaster + 2; | 
|  | 6490 | break; | 
|  | 6491 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: | 
| Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6492 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: | 
| Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6493 | other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0); | 
|  | 6494 | other_secret = handshake->premaster + 2; | 
|  | 6495 | break; | 
|  | 6496 | default: | 
|  | 6497 | break; | 
| Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 6498 | } | 
|  | 6499 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6500 | status = setup_psa_key_derivation(&derivation, psk, alg, | 
|  | 6501 | ssl->conf->psk, ssl->conf->psk_len, | 
|  | 6502 | seed, seed_len, | 
|  | 6503 | (unsigned char const *) lbl, | 
|  | 6504 | (size_t) strlen(lbl), | 
|  | 6505 | other_secret, other_secret_len, | 
|  | 6506 | master_secret_len); | 
|  | 6507 | if (status != PSA_SUCCESS) { | 
|  | 6508 | psa_key_derivation_abort(&derivation); | 
|  | 6509 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; | 
| Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6510 | } | 
|  | 6511 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6512 | status = psa_key_derivation_output_bytes(&derivation, | 
|  | 6513 | master, | 
|  | 6514 | master_secret_len); | 
|  | 6515 | if (status != PSA_SUCCESS) { | 
|  | 6516 | psa_key_derivation_abort(&derivation); | 
|  | 6517 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; | 
| Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6518 | } | 
|  | 6519 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6520 | status = psa_key_derivation_abort(&derivation); | 
|  | 6521 | if (status != PSA_SUCCESS) { | 
|  | 6522 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; | 
|  | 6523 | } | 
|  | 6524 | } else | 
| Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6525 | #endif | 
|  | 6526 | { | 
| Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 6527 | #if defined(MBEDTLS_USE_PSA_CRYPTO) &&                              \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6528 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
|  | 6529 | if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) { | 
| Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 6530 | psa_status_t status; | 
|  | 6531 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; | 
|  | 6532 | psa_key_derivation_operation_t derivation = | 
|  | 6533 | PSA_KEY_DERIVATION_OPERATION_INIT; | 
|  | 6534 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6535 | MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE")); | 
| Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 6536 |  | 
|  | 6537 | handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE; | 
|  | 6538 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6539 | status = psa_key_derivation_setup(&derivation, alg); | 
|  | 6540 | if (status != PSA_SUCCESS) { | 
|  | 6541 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; | 
| Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 6542 | } | 
|  | 6543 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6544 | status = psa_key_derivation_set_capacity(&derivation, | 
|  | 6545 | PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE); | 
|  | 6546 | if (status != PSA_SUCCESS) { | 
|  | 6547 | psa_key_derivation_abort(&derivation); | 
|  | 6548 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; | 
| Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 6549 | } | 
|  | 6550 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6551 | status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx, | 
|  | 6552 | &derivation); | 
|  | 6553 | if (status != PSA_SUCCESS) { | 
|  | 6554 | psa_key_derivation_abort(&derivation); | 
|  | 6555 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; | 
| Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 6556 | } | 
|  | 6557 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6558 | status = psa_key_derivation_output_bytes(&derivation, | 
|  | 6559 | handshake->premaster, | 
|  | 6560 | handshake->pmslen); | 
|  | 6561 | if (status != PSA_SUCCESS) { | 
|  | 6562 | psa_key_derivation_abort(&derivation); | 
|  | 6563 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; | 
|  | 6564 | } | 
|  | 6565 |  | 
|  | 6566 | status = psa_key_derivation_abort(&derivation); | 
|  | 6567 | if (status != PSA_SUCCESS) { | 
|  | 6568 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; | 
| Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 6569 | } | 
|  | 6570 | } | 
|  | 6571 | #endif | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6572 | ret = handshake->tls_prf(handshake->premaster, handshake->pmslen, | 
|  | 6573 | lbl, seed, seed_len, | 
|  | 6574 | master, | 
|  | 6575 | master_secret_len); | 
|  | 6576 | if (ret != 0) { | 
|  | 6577 | MBEDTLS_SSL_DEBUG_RET(1, "prf", ret); | 
|  | 6578 | return ret; | 
| Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6579 | } | 
|  | 6580 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6581 | MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret", | 
|  | 6582 | handshake->premaster, | 
|  | 6583 | handshake->pmslen); | 
| Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6584 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6585 | mbedtls_platform_zeroize(handshake->premaster, | 
|  | 6586 | sizeof(handshake->premaster)); | 
| Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6587 | } | 
|  | 6588 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6589 | return 0; | 
| Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6590 | } | 
|  | 6591 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6592 | int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl) | 
| Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6593 | { | 
|  | 6594 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 6595 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = | 
|  | 6596 | ssl->handshake->ciphersuite_info; | 
|  | 6597 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6598 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys")); | 
| Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6599 |  | 
|  | 6600 | /* Set PRF, calc_verify and calc_finished function pointers */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6601 | ret = ssl_set_handshake_prfs(ssl->handshake, | 
| Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 6602 | (mbedtls_md_type_t) ciphersuite_info->mac); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6603 | if (ret != 0) { | 
|  | 6604 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret); | 
|  | 6605 | return ret; | 
| Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6606 | } | 
|  | 6607 |  | 
|  | 6608 | /* Compute master secret if needed */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6609 | ret = ssl_compute_master(ssl->handshake, | 
|  | 6610 | ssl->session_negotiate->master, | 
|  | 6611 | ssl); | 
|  | 6612 | if (ret != 0) { | 
|  | 6613 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret); | 
|  | 6614 | return ret; | 
| Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6615 | } | 
|  | 6616 |  | 
|  | 6617 | /* Swap the client and server random values: | 
|  | 6618 | * - MS derivation wanted client+server (RFC 5246 8.1) | 
|  | 6619 | * - key derivation wants server+client (RFC 5246 6.3) */ | 
|  | 6620 | { | 
|  | 6621 | unsigned char tmp[64]; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6622 | memcpy(tmp, ssl->handshake->randbytes, 64); | 
|  | 6623 | memcpy(ssl->handshake->randbytes, tmp + 32, 32); | 
|  | 6624 | memcpy(ssl->handshake->randbytes + 32, tmp, 32); | 
|  | 6625 | mbedtls_platform_zeroize(tmp, sizeof(tmp)); | 
| Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6626 | } | 
|  | 6627 |  | 
|  | 6628 | /* Populate transform structure */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6629 | ret = ssl_tls12_populate_transform(ssl->transform_negotiate, | 
|  | 6630 | ssl->session_negotiate->ciphersuite, | 
|  | 6631 | ssl->session_negotiate->master, | 
| Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 6632 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6633 | ssl->session_negotiate->encrypt_then_mac, | 
| Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 6634 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6635 | ssl->handshake->tls_prf, | 
|  | 6636 | ssl->handshake->randbytes, | 
|  | 6637 | ssl->tls_version, | 
|  | 6638 | ssl->conf->endpoint, | 
|  | 6639 | ssl); | 
|  | 6640 | if (ret != 0) { | 
|  | 6641 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret); | 
|  | 6642 | return ret; | 
| Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6643 | } | 
|  | 6644 |  | 
|  | 6645 | /* We no longer need Server/ClientHello.random values */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6646 | mbedtls_platform_zeroize(ssl->handshake->randbytes, | 
|  | 6647 | sizeof(ssl->handshake->randbytes)); | 
| Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6648 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6649 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys")); | 
| Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6650 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6651 | return 0; | 
| Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6652 | } | 
| Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6653 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6654 | int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md) | 
| Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 6655 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6656 | switch (md) { | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6657 | #if defined(MBEDTLS_MD_CAN_SHA384) | 
| Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 6658 | case MBEDTLS_SSL_HASH_SHA384: | 
|  | 6659 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; | 
|  | 6660 | break; | 
|  | 6661 | #endif | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6662 | #if defined(MBEDTLS_MD_CAN_SHA256) | 
| Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 6663 | case MBEDTLS_SSL_HASH_SHA256: | 
|  | 6664 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; | 
|  | 6665 | break; | 
|  | 6666 | #endif | 
|  | 6667 | default: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6668 | return -1; | 
| Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 6669 | } | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6670 | #if !defined(MBEDTLS_MD_CAN_SHA384) && \ | 
|  | 6671 | !defined(MBEDTLS_MD_CAN_SHA256) | 
| Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 6672 | (void) ssl; | 
|  | 6673 | #endif | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6674 | return 0; | 
| Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 6675 | } | 
|  | 6676 |  | 
| Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 6677 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 6678 | static int ssl_calc_verify_tls_psa(const mbedtls_ssl_context *ssl, | 
|  | 6679 | const psa_hash_operation_t *hs_op, | 
|  | 6680 | size_t buffer_size, | 
|  | 6681 | unsigned char *hash, | 
|  | 6682 | size_t *hlen) | 
|  | 6683 | { | 
|  | 6684 | psa_status_t status; | 
| Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 6685 | psa_hash_operation_t cloned_op = psa_hash_operation_init(); | 
| Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 6686 |  | 
|  | 6687 | #if !defined(MBEDTLS_DEBUG_C) | 
|  | 6688 | (void) ssl; | 
|  | 6689 | #endif | 
|  | 6690 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify")); | 
| Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 6691 | status = psa_hash_clone(hs_op, &cloned_op); | 
| Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 6692 | if (status != PSA_SUCCESS) { | 
|  | 6693 | goto exit; | 
|  | 6694 | } | 
|  | 6695 |  | 
| Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 6696 | status = psa_hash_finish(&cloned_op, hash, buffer_size, hlen); | 
| Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 6697 | if (status != PSA_SUCCESS) { | 
|  | 6698 | goto exit; | 
|  | 6699 | } | 
|  | 6700 |  | 
|  | 6701 | MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen); | 
|  | 6702 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify")); | 
|  | 6703 |  | 
|  | 6704 | exit: | 
| Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 6705 | psa_hash_abort(&cloned_op); | 
| Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 6706 | return mbedtls_md_error_from_psa(status); | 
|  | 6707 | } | 
|  | 6708 | #else | 
|  | 6709 | static int ssl_calc_verify_tls_legacy(const mbedtls_ssl_context *ssl, | 
|  | 6710 | const mbedtls_md_context_t *hs_ctx, | 
|  | 6711 | unsigned char *hash, | 
|  | 6712 | size_t *hlen) | 
|  | 6713 | { | 
|  | 6714 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 6715 | mbedtls_md_context_t cloned_ctx; | 
| Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 6716 |  | 
| Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 6717 | mbedtls_md_init(&cloned_ctx); | 
| Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 6718 |  | 
|  | 6719 | #if !defined(MBEDTLS_DEBUG_C) | 
|  | 6720 | (void) ssl; | 
|  | 6721 | #endif | 
|  | 6722 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify")); | 
|  | 6723 |  | 
| Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 6724 | ret = mbedtls_md_setup(&cloned_ctx, mbedtls_md_info_from_ctx(hs_ctx), 0); | 
| Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 6725 | if (ret != 0) { | 
|  | 6726 | goto exit; | 
|  | 6727 | } | 
| Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 6728 | ret = mbedtls_md_clone(&cloned_ctx, hs_ctx); | 
| Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 6729 | if (ret != 0) { | 
|  | 6730 | goto exit; | 
|  | 6731 | } | 
|  | 6732 |  | 
| Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 6733 | ret = mbedtls_md_finish(&cloned_ctx, hash); | 
| Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 6734 | if (ret != 0) { | 
|  | 6735 | goto exit; | 
|  | 6736 | } | 
|  | 6737 |  | 
|  | 6738 | *hlen = mbedtls_md_get_size(mbedtls_md_info_from_ctx(hs_ctx)); | 
|  | 6739 |  | 
|  | 6740 | MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen); | 
|  | 6741 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify")); | 
|  | 6742 |  | 
|  | 6743 | exit: | 
| Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 6744 | mbedtls_md_free(&cloned_ctx); | 
| Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 6745 | return ret; | 
|  | 6746 | } | 
|  | 6747 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 6748 |  | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6749 | #if defined(MBEDTLS_MD_CAN_SHA256) | 
| Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 6750 | int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl, | 
|  | 6751 | unsigned char *hash, | 
|  | 6752 | size_t *hlen) | 
| Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6753 | { | 
|  | 6754 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 6755 | return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha256_psa, 32, | 
|  | 6756 | hash, hlen); | 
| Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6757 | #else | 
| Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 6758 | return ssl_calc_verify_tls_legacy(ssl, &ssl->handshake->fin_sha256, | 
|  | 6759 | hash, hlen); | 
| Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6760 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6761 | } | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6762 | #endif /* MBEDTLS_MD_CAN_SHA256 */ | 
| Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6763 |  | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6764 | #if defined(MBEDTLS_MD_CAN_SHA384) | 
| Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 6765 | int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl, | 
|  | 6766 | unsigned char *hash, | 
|  | 6767 | size_t *hlen) | 
| Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6768 | { | 
|  | 6769 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 6770 | return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha384_psa, 48, | 
|  | 6771 | hash, hlen); | 
| Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6772 | #else | 
| Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 6773 | return ssl_calc_verify_tls_legacy(ssl, &ssl->handshake->fin_sha384, | 
|  | 6774 | hash, hlen); | 
| Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6775 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6776 | } | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6777 | #endif /* MBEDTLS_MD_CAN_SHA384 */ | 
| Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6778 |  | 
| Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6779 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) &&                      \ | 
|  | 6780 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6781 | int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex) | 
| Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6782 | { | 
|  | 6783 | unsigned char *p = ssl->handshake->premaster; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6784 | unsigned char *end = p + sizeof(ssl->handshake->premaster); | 
| Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6785 | const unsigned char *psk = NULL; | 
|  | 6786 | size_t psk_len = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6787 | int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len); | 
| Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6788 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6789 | if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) { | 
| Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6790 | /* | 
|  | 6791 | * This should never happen because the existence of a PSK is always | 
| Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6792 | * checked before calling this function. | 
|  | 6793 | * | 
|  | 6794 | * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with | 
| Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 6795 | * the shared secret without PSK. | 
| Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6796 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6797 | if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) { | 
|  | 6798 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); | 
|  | 6799 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 
| Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6800 | } | 
| Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6801 | } | 
|  | 6802 |  | 
|  | 6803 | /* | 
|  | 6804 | * PMS = struct { | 
|  | 6805 | *     opaque other_secret<0..2^16-1>; | 
|  | 6806 | *     opaque psk<0..2^16-1>; | 
|  | 6807 | * }; | 
|  | 6808 | * with "other_secret" depending on the particular key exchange | 
|  | 6809 | */ | 
|  | 6810 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6811 | if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) { | 
|  | 6812 | if (end - p < 2) { | 
|  | 6813 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 6814 | } | 
| Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6815 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6816 | MBEDTLS_PUT_UINT16_BE(psk_len, p, 0); | 
| Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6817 | p += 2; | 
|  | 6818 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6819 | if (end < p || (size_t) (end - p) < psk_len) { | 
|  | 6820 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 6821 | } | 
| Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6822 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6823 | memset(p, 0, psk_len); | 
| Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6824 | p += psk_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6825 | } else | 
| Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6826 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ | 
|  | 6827 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6828 | if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) { | 
| Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6829 | /* | 
|  | 6830 | * other_secret already set by the ClientKeyExchange message, | 
|  | 6831 | * and is 48 bytes long | 
|  | 6832 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6833 | if (end - p < 2) { | 
|  | 6834 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 6835 | } | 
| Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6836 |  | 
|  | 6837 | *p++ = 0; | 
|  | 6838 | *p++ = 48; | 
|  | 6839 | p += 48; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6840 | } else | 
| Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6841 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ | 
|  | 6842 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6843 | if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) { | 
| Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6844 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 6845 | size_t len; | 
|  | 6846 |  | 
|  | 6847 | /* Write length only when we know the actual value */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6848 | if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx, | 
| Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 6849 | p + 2, (size_t) (end - (p + 2)), &len, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6850 | ssl->conf->f_rng, ssl->conf->p_rng)) != 0) { | 
|  | 6851 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret); | 
|  | 6852 | return ret; | 
| Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6853 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6854 | MBEDTLS_PUT_UINT16_BE(len, p, 0); | 
| Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6855 | p += 2 + len; | 
|  | 6856 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6857 | MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K); | 
|  | 6858 | } else | 
| Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6859 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ | 
| Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6860 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6861 | if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) { | 
| Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6862 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 6863 | size_t zlen; | 
|  | 6864 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6865 | if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen, | 
| Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 6866 | p + 2, (size_t) (end - (p + 2)), | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6867 | ssl->conf->f_rng, ssl->conf->p_rng)) != 0) { | 
|  | 6868 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret); | 
|  | 6869 | return ret; | 
| Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6870 | } | 
|  | 6871 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6872 | MBEDTLS_PUT_UINT16_BE(zlen, p, 0); | 
| Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6873 | p += 2 + zlen; | 
|  | 6874 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6875 | MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx, | 
|  | 6876 | MBEDTLS_DEBUG_ECDH_Z); | 
|  | 6877 | } else | 
| Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6878 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ | 
| Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6879 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6880 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); | 
|  | 6881 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 
| Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6882 | } | 
|  | 6883 |  | 
|  | 6884 | /* opaque psk<0..2^16-1>; */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6885 | if (end - p < 2) { | 
|  | 6886 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 6887 | } | 
| Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6888 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6889 | MBEDTLS_PUT_UINT16_BE(psk_len, p, 0); | 
| Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6890 | p += 2; | 
|  | 6891 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6892 | if (end < p || (size_t) (end - p) < psk_len) { | 
|  | 6893 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 6894 | } | 
| Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6895 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6896 | memcpy(p, psk, psk_len); | 
| Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6897 | p += psk_len; | 
|  | 6898 |  | 
| Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 6899 | ssl->handshake->pmslen = (size_t) (p - ssl->handshake->premaster); | 
| Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6900 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6901 | return 0; | 
| Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6902 | } | 
| Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6903 | #endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ | 
| Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6904 |  | 
|  | 6905 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6906 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6907 | static int ssl_write_hello_request(mbedtls_ssl_context *ssl); | 
| Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6908 |  | 
|  | 6909 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6910 | int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl) | 
| Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6911 | { | 
|  | 6912 | /* If renegotiation is not enforced, retransmit until we would reach max | 
|  | 6913 | * timeout if we were using the usual handshake doubling scheme */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6914 | if (ssl->conf->renego_max_records < 0) { | 
| Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6915 | uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1; | 
|  | 6916 | unsigned char doublings = 1; | 
|  | 6917 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6918 | while (ratio != 0) { | 
| Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6919 | ++doublings; | 
|  | 6920 | ratio >>= 1; | 
|  | 6921 | } | 
|  | 6922 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6923 | if (++ssl->renego_records_seen > doublings) { | 
|  | 6924 | MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request")); | 
|  | 6925 | return 0; | 
| Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6926 | } | 
|  | 6927 | } | 
|  | 6928 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6929 | return ssl_write_hello_request(ssl); | 
| Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6930 | } | 
|  | 6931 | #endif | 
|  | 6932 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6933 |  | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6934 | /* | 
|  | 6935 | * Handshake functions | 
|  | 6936 | */ | 
|  | 6937 | #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
|  | 6938 | /* No certificate support -> dummy functions */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6939 | int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl) | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6940 | { | 
|  | 6941 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
|  | 6942 | ssl->handshake->ciphersuite_info; | 
|  | 6943 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6944 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate")); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6945 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6946 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { | 
|  | 6947 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate")); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6948 | ssl->state++; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6949 | return 0; | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6950 | } | 
|  | 6951 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6952 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); | 
|  | 6953 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6954 | } | 
|  | 6955 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6956 | int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl) | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6957 | { | 
|  | 6958 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
|  | 6959 | ssl->handshake->ciphersuite_info; | 
|  | 6960 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6961 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate")); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6962 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6963 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { | 
|  | 6964 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate")); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6965 | ssl->state++; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6966 | return 0; | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6967 | } | 
|  | 6968 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6969 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); | 
|  | 6970 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6971 | } | 
|  | 6972 |  | 
|  | 6973 | #else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
|  | 6974 | /* Some certificate support -> implement write and parse */ | 
|  | 6975 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6976 | int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl) | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6977 | { | 
|  | 6978 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
|  | 6979 | size_t i, n; | 
|  | 6980 | const mbedtls_x509_crt *crt; | 
|  | 6981 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
|  | 6982 | ssl->handshake->ciphersuite_info; | 
|  | 6983 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6984 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate")); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6985 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6986 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { | 
|  | 6987 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate")); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6988 | ssl->state++; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6989 | return 0; | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6990 | } | 
|  | 6991 |  | 
|  | 6992 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6993 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { | 
|  | 6994 | if (ssl->handshake->client_auth == 0) { | 
|  | 6995 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate")); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6996 | ssl->state++; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6997 | return 0; | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6998 | } | 
|  | 6999 | } | 
|  | 7000 | #endif /* MBEDTLS_SSL_CLI_C */ | 
|  | 7001 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7002 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { | 
|  | 7003 | if (mbedtls_ssl_own_cert(ssl) == NULL) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7004 | /* Should never happen because we shouldn't have picked the | 
|  | 7005 | * ciphersuite if we don't have a certificate. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7006 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7007 | } | 
|  | 7008 | } | 
|  | 7009 | #endif | 
|  | 7010 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7011 | MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl)); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7012 |  | 
|  | 7013 | /* | 
|  | 7014 | *     0  .  0    handshake type | 
|  | 7015 | *     1  .  3    handshake length | 
|  | 7016 | *     4  .  6    length of all certs | 
|  | 7017 | *     7  .  9    length of cert. 1 | 
|  | 7018 | *    10  . n-1   peer certificate | 
|  | 7019 | *     n  . n+2   length of cert. 2 | 
|  | 7020 | *    n+3 . ...   upper level cert, etc. | 
|  | 7021 | */ | 
|  | 7022 | i = 7; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7023 | crt = mbedtls_ssl_own_cert(ssl); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7024 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7025 | while (crt != NULL) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7026 | n = crt->raw.len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7027 | if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) { | 
|  | 7028 | MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET | 
|  | 7029 | " > %" MBEDTLS_PRINTF_SIZET, | 
|  | 7030 | i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN)); | 
|  | 7031 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7032 | } | 
|  | 7033 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7034 | ssl->out_msg[i] = MBEDTLS_BYTE_2(n); | 
|  | 7035 | ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n); | 
|  | 7036 | ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7037 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7038 | i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7039 | i += n; crt = crt->next; | 
|  | 7040 | } | 
|  | 7041 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7042 | ssl->out_msg[4]  = MBEDTLS_BYTE_2(i - 7); | 
|  | 7043 | ssl->out_msg[5]  = MBEDTLS_BYTE_1(i - 7); | 
|  | 7044 | ssl->out_msg[6]  = MBEDTLS_BYTE_0(i - 7); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7045 |  | 
|  | 7046 | ssl->out_msglen  = i; | 
|  | 7047 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 7048 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_CERTIFICATE; | 
|  | 7049 |  | 
|  | 7050 | ssl->state++; | 
|  | 7051 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7052 | if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { | 
|  | 7053 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret); | 
|  | 7054 | return ret; | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7055 | } | 
|  | 7056 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7057 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate")); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7058 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7059 | return ret; | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7060 | } | 
|  | 7061 |  | 
|  | 7062 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 7063 |  | 
|  | 7064 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7065 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7066 | static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl, | 
|  | 7067 | unsigned char *crt_buf, | 
|  | 7068 | size_t crt_buf_len) | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7069 | { | 
|  | 7070 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; | 
|  | 7071 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7072 | if (peer_crt == NULL) { | 
|  | 7073 | return -1; | 
|  | 7074 | } | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7075 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7076 | if (peer_crt->raw.len != crt_buf_len) { | 
|  | 7077 | return -1; | 
|  | 7078 | } | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7079 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7080 | return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7081 | } | 
|  | 7082 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7083 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7084 | static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl, | 
|  | 7085 | unsigned char *crt_buf, | 
|  | 7086 | size_t crt_buf_len) | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7087 | { | 
|  | 7088 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 7089 | unsigned char const * const peer_cert_digest = | 
|  | 7090 | ssl->session->peer_cert_digest; | 
|  | 7091 | mbedtls_md_type_t const peer_cert_digest_type = | 
|  | 7092 | ssl->session->peer_cert_digest_type; | 
|  | 7093 | mbedtls_md_info_t const * const digest_info = | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7094 | mbedtls_md_info_from_type(peer_cert_digest_type); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7095 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; | 
|  | 7096 | size_t digest_len; | 
|  | 7097 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7098 | if (peer_cert_digest == NULL || digest_info == NULL) { | 
|  | 7099 | return -1; | 
|  | 7100 | } | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7101 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7102 | digest_len = mbedtls_md_get_size(digest_info); | 
|  | 7103 | if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) { | 
|  | 7104 | return -1; | 
|  | 7105 | } | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7106 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7107 | ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest); | 
|  | 7108 | if (ret != 0) { | 
|  | 7109 | return -1; | 
|  | 7110 | } | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7111 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7112 | return memcmp(tmp_digest, peer_cert_digest, digest_len); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7113 | } | 
|  | 7114 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 7115 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ | 
|  | 7116 |  | 
|  | 7117 | /* | 
|  | 7118 | * Once the certificate message is read, parse it into a cert chain and | 
|  | 7119 | * perform basic checks, but leave actual verification to the caller | 
|  | 7120 | */ | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7121 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7122 | static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl, | 
|  | 7123 | mbedtls_x509_crt *chain) | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7124 | { | 
|  | 7125 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 7126 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7127 | int crt_cnt = 0; | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7128 | #endif | 
|  | 7129 | size_t i, n; | 
|  | 7130 | uint8_t alert; | 
|  | 7131 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7132 | if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) { | 
|  | 7133 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); | 
|  | 7134 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 7135 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); | 
|  | 7136 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7137 | } | 
|  | 7138 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7139 | if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) { | 
|  | 7140 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 7141 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); | 
|  | 7142 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7143 | } | 
|  | 7144 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7145 | if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) { | 
|  | 7146 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); | 
|  | 7147 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 7148 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); | 
|  | 7149 | return MBEDTLS_ERR_SSL_DECODE_ERROR; | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7150 | } | 
|  | 7151 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7152 | i = mbedtls_ssl_hs_hdr_len(ssl); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7153 |  | 
|  | 7154 | /* | 
|  | 7155 | * Same message structure as in mbedtls_ssl_write_certificate() | 
|  | 7156 | */ | 
| Dave Rodgman | a3d0f61 | 2023-11-03 23:34:02 +0000 | [diff] [blame] | 7157 | n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7158 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7159 | if (ssl->in_msg[i] != 0 || | 
|  | 7160 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) { | 
|  | 7161 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); | 
|  | 7162 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 7163 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); | 
|  | 7164 | return MBEDTLS_ERR_SSL_DECODE_ERROR; | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7165 | } | 
|  | 7166 |  | 
|  | 7167 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ | 
|  | 7168 | i += 3; | 
|  | 7169 |  | 
|  | 7170 | /* Iterate through and parse the CRTs in the provided chain. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7171 | while (i < ssl->in_hslen) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7172 | /* Check that there's room for the next CRT's length fields. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7173 | if (i + 3 > ssl->in_hslen) { | 
|  | 7174 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); | 
|  | 7175 | mbedtls_ssl_send_alert_message(ssl, | 
|  | 7176 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 7177 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); | 
|  | 7178 | return MBEDTLS_ERR_SSL_DECODE_ERROR; | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7179 | } | 
|  | 7180 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support | 
|  | 7181 | * anything beyond 2**16 ~ 64K. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7182 | if (ssl->in_msg[i] != 0) { | 
|  | 7183 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); | 
|  | 7184 | mbedtls_ssl_send_alert_message(ssl, | 
|  | 7185 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 7186 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT); | 
|  | 7187 | return MBEDTLS_ERR_SSL_BAD_CERTIFICATE; | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7188 | } | 
|  | 7189 |  | 
|  | 7190 | /* Read length of the next CRT in the chain. */ | 
| Dave Rodgman | a3d0f61 | 2023-11-03 23:34:02 +0000 | [diff] [blame] | 7191 | n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7192 | i += 3; | 
|  | 7193 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7194 | if (n < 128 || i + n > ssl->in_hslen) { | 
|  | 7195 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); | 
|  | 7196 | mbedtls_ssl_send_alert_message(ssl, | 
|  | 7197 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 7198 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); | 
|  | 7199 | return MBEDTLS_ERR_SSL_DECODE_ERROR; | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7200 | } | 
|  | 7201 |  | 
|  | 7202 | /* Check if we're handling the first CRT in the chain. */ | 
|  | 7203 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7204 | if (crt_cnt++ == 0 && | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7205 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7206 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7207 | /* During client-side renegotiation, check that the server's | 
|  | 7208 | * end-CRTs hasn't changed compared to the initial handshake, | 
|  | 7209 | * mitigating the triple handshake attack. On success, reuse | 
|  | 7210 | * the original end-CRT instead of parsing it again. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7211 | MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation")); | 
|  | 7212 | if (ssl_check_peer_crt_unchanged(ssl, | 
|  | 7213 | &ssl->in_msg[i], | 
|  | 7214 | n) != 0) { | 
|  | 7215 | MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation")); | 
|  | 7216 | mbedtls_ssl_send_alert_message(ssl, | 
|  | 7217 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 7218 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED); | 
|  | 7219 | return MBEDTLS_ERR_SSL_BAD_CERTIFICATE; | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7220 | } | 
|  | 7221 |  | 
|  | 7222 | /* Now we can safely free the original chain. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7223 | ssl_clear_peer_cert(ssl->session); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7224 | } | 
|  | 7225 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ | 
|  | 7226 |  | 
|  | 7227 | /* Parse the next certificate in the chain. */ | 
|  | 7228 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7229 | ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7230 | #else | 
|  | 7231 | /* If we don't need to store the CRT chain permanently, parse | 
|  | 7232 | * it in-place from the input buffer instead of making a copy. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7233 | ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7234 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7235 | switch (ret) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7236 | case 0: /*ok*/ | 
|  | 7237 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: | 
|  | 7238 | /* Ignore certificate with an unknown algorithm: maybe a | 
|  | 7239 | prior certificate was already trusted. */ | 
|  | 7240 | break; | 
|  | 7241 |  | 
|  | 7242 | case MBEDTLS_ERR_X509_ALLOC_FAILED: | 
|  | 7243 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; | 
|  | 7244 | goto crt_parse_der_failed; | 
|  | 7245 |  | 
|  | 7246 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: | 
|  | 7247 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
|  | 7248 | goto crt_parse_der_failed; | 
|  | 7249 |  | 
|  | 7250 | default: | 
|  | 7251 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7252 | crt_parse_der_failed: | 
|  | 7253 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert); | 
|  | 7254 | MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret); | 
|  | 7255 | return ret; | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7256 | } | 
|  | 7257 |  | 
|  | 7258 | i += n; | 
|  | 7259 | } | 
|  | 7260 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7261 | MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain); | 
|  | 7262 | return 0; | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7263 | } | 
|  | 7264 |  | 
|  | 7265 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7266 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7267 | static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl) | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7268 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7269 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { | 
|  | 7270 | return -1; | 
|  | 7271 | } | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7272 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7273 | if (ssl->in_hslen   == 3 + mbedtls_ssl_hs_hdr_len(ssl) && | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7274 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE    && | 
|  | 7275 | ssl->in_msg[0]  == MBEDTLS_SSL_HS_CERTIFICATE   && | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7276 | memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) { | 
|  | 7277 | MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate")); | 
|  | 7278 | return 0; | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7279 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7280 | return -1; | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7281 | } | 
|  | 7282 | #endif /* MBEDTLS_SSL_SRV_C */ | 
|  | 7283 |  | 
|  | 7284 | /* Check if a certificate message is expected. | 
|  | 7285 | * Return either | 
|  | 7286 | * - SSL_CERTIFICATE_EXPECTED, or | 
|  | 7287 | * - SSL_CERTIFICATE_SKIP | 
|  | 7288 | * indicating whether a Certificate message is expected or not. | 
|  | 7289 | */ | 
|  | 7290 | #define SSL_CERTIFICATE_EXPECTED 0 | 
|  | 7291 | #define SSL_CERTIFICATE_SKIP     1 | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7292 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7293 | static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl, | 
|  | 7294 | int authmode) | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7295 | { | 
|  | 7296 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
|  | 7297 | ssl->handshake->ciphersuite_info; | 
|  | 7298 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7299 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { | 
|  | 7300 | return SSL_CERTIFICATE_SKIP; | 
|  | 7301 | } | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7302 |  | 
|  | 7303 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7304 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { | 
|  | 7305 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) { | 
|  | 7306 | return SSL_CERTIFICATE_SKIP; | 
|  | 7307 | } | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7308 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7309 | if (authmode == MBEDTLS_SSL_VERIFY_NONE) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7310 | ssl->session_negotiate->verify_result = | 
|  | 7311 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7312 | return SSL_CERTIFICATE_SKIP; | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7313 | } | 
|  | 7314 | } | 
|  | 7315 | #else | 
|  | 7316 | ((void) authmode); | 
|  | 7317 | #endif /* MBEDTLS_SSL_SRV_C */ | 
|  | 7318 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7319 | return SSL_CERTIFICATE_EXPECTED; | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7320 | } | 
|  | 7321 |  | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7322 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7323 | static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl, | 
|  | 7324 | int authmode, | 
|  | 7325 | mbedtls_x509_crt *chain, | 
|  | 7326 | void *rs_ctx) | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7327 | { | 
|  | 7328 | int ret = 0; | 
|  | 7329 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = | 
|  | 7330 | ssl->handshake->ciphersuite_info; | 
|  | 7331 | int have_ca_chain = 0; | 
|  | 7332 |  | 
|  | 7333 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); | 
|  | 7334 | void *p_vrfy; | 
|  | 7335 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7336 | if (authmode == MBEDTLS_SSL_VERIFY_NONE) { | 
|  | 7337 | return 0; | 
|  | 7338 | } | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7339 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7340 | if (ssl->f_vrfy != NULL) { | 
|  | 7341 | MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback")); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7342 | f_vrfy = ssl->f_vrfy; | 
|  | 7343 | p_vrfy = ssl->p_vrfy; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7344 | } else { | 
|  | 7345 | MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback")); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7346 | f_vrfy = ssl->conf->f_vrfy; | 
|  | 7347 | p_vrfy = ssl->conf->p_vrfy; | 
|  | 7348 | } | 
|  | 7349 |  | 
|  | 7350 | /* | 
|  | 7351 | * Main check: verify certificate | 
|  | 7352 | */ | 
|  | 7353 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7354 | if (ssl->conf->f_ca_cb != NULL) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7355 | ((void) rs_ctx); | 
|  | 7356 | have_ca_chain = 1; | 
|  | 7357 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7358 | MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification")); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7359 | ret = mbedtls_x509_crt_verify_with_ca_cb( | 
|  | 7360 | chain, | 
|  | 7361 | ssl->conf->f_ca_cb, | 
|  | 7362 | ssl->conf->p_ca_cb, | 
|  | 7363 | ssl->conf->cert_profile, | 
|  | 7364 | ssl->hostname, | 
|  | 7365 | &ssl->session_negotiate->verify_result, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7366 | f_vrfy, p_vrfy); | 
|  | 7367 | } else | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7368 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ | 
|  | 7369 | { | 
|  | 7370 | mbedtls_x509_crt *ca_chain; | 
|  | 7371 | mbedtls_x509_crl *ca_crl; | 
|  | 7372 |  | 
|  | 7373 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7374 | if (ssl->handshake->sni_ca_chain != NULL) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7375 | ca_chain = ssl->handshake->sni_ca_chain; | 
|  | 7376 | ca_crl   = ssl->handshake->sni_ca_crl; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7377 | } else | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7378 | #endif | 
|  | 7379 | { | 
|  | 7380 | ca_chain = ssl->conf->ca_chain; | 
|  | 7381 | ca_crl   = ssl->conf->ca_crl; | 
|  | 7382 | } | 
|  | 7383 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7384 | if (ca_chain != NULL) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7385 | have_ca_chain = 1; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7386 | } | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7387 |  | 
|  | 7388 | ret = mbedtls_x509_crt_verify_restartable( | 
|  | 7389 | chain, | 
|  | 7390 | ca_chain, ca_crl, | 
|  | 7391 | ssl->conf->cert_profile, | 
|  | 7392 | ssl->hostname, | 
|  | 7393 | &ssl->session_negotiate->verify_result, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7394 | f_vrfy, p_vrfy, rs_ctx); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7395 | } | 
|  | 7396 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7397 | if (ret != 0) { | 
|  | 7398 | MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7399 | } | 
|  | 7400 |  | 
|  | 7401 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7402 | if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) { | 
|  | 7403 | return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; | 
|  | 7404 | } | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7405 | #endif | 
|  | 7406 |  | 
|  | 7407 | /* | 
|  | 7408 | * Secondary checks: always done, but change 'ret' only if it was 0 | 
|  | 7409 | */ | 
|  | 7410 |  | 
| Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 7411 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7412 | { | 
|  | 7413 | const mbedtls_pk_context *pk = &chain->pk; | 
|  | 7414 |  | 
| Manuel Pégourié-Gonnard | 66b0d61 | 2022-06-17 10:49:29 +0200 | [diff] [blame] | 7415 | /* If certificate uses an EC key, make sure the curve is OK. | 
|  | 7416 | * This is a public key, so it can't be opaque, so can_do() is a good | 
|  | 7417 | * enough check to ensure pk_ec() is safe to use here. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7418 | if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY)) { | 
| Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 7419 | /* and in the unlikely case the above assumption no longer holds | 
|  | 7420 | * we are making sure that pk_ec() here does not return a NULL | 
|  | 7421 | */ | 
| Valerio Setti | d040509 | 2023-05-24 13:16:40 +0200 | [diff] [blame] | 7422 | mbedtls_ecp_group_id grp_id = mbedtls_pk_get_group_id(pk); | 
|  | 7423 | if (grp_id == MBEDTLS_ECP_DP_NONE) { | 
|  | 7424 | MBEDTLS_SSL_DEBUG_MSG(1, ("invalid group ID")); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7425 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 
| Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 7426 | } | 
| Valerio Setti | 9720778 | 2023-05-18 18:59:06 +0200 | [diff] [blame] | 7427 | if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) { | 
| Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 7428 | ssl->session_negotiate->verify_result |= | 
|  | 7429 | MBEDTLS_X509_BADCERT_BAD_KEY; | 
|  | 7430 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7431 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)")); | 
|  | 7432 | if (ret == 0) { | 
| Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 7433 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7434 | } | 
| Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 7435 | } | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7436 | } | 
|  | 7437 | } | 
| Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 7438 | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7439 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7440 | if (mbedtls_ssl_check_cert_usage(chain, | 
|  | 7441 | ciphersuite_info, | 
|  | 7442 | !ssl->conf->endpoint, | 
|  | 7443 | &ssl->session_negotiate->verify_result) != 0) { | 
|  | 7444 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)")); | 
|  | 7445 | if (ret == 0) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7446 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7447 | } | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7448 | } | 
|  | 7449 |  | 
|  | 7450 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a | 
|  | 7451 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, | 
|  | 7452 | * with details encoded in the verification flags. All other kinds | 
|  | 7453 | * of error codes, including those from the user provided f_vrfy | 
|  | 7454 | * functions, are treated as fatal and lead to a failure of | 
|  | 7455 | * ssl_parse_certificate even if verification was optional. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7456 | if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && | 
|  | 7457 | (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || | 
|  | 7458 | ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7459 | ret = 0; | 
|  | 7460 | } | 
|  | 7461 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7462 | if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) { | 
|  | 7463 | MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain")); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7464 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; | 
|  | 7465 | } | 
|  | 7466 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7467 | if (ret != 0) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7468 | uint8_t alert; | 
|  | 7469 |  | 
|  | 7470 | /* The certificate may have been rejected for several reasons. | 
|  | 7471 | Pick one and send the corresponding alert. Which alert to send | 
|  | 7472 | may be a subject of debate in some cases. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7473 | if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7474 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7475 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7476 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7477 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7478 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7479 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7480 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7481 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7482 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7483 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7484 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7485 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7486 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7487 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7488 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7489 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7490 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7491 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7492 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7493 | } else { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7494 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7495 | } | 
|  | 7496 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 7497 | alert); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7498 | } | 
|  | 7499 |  | 
|  | 7500 | #if defined(MBEDTLS_DEBUG_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7501 | if (ssl->session_negotiate->verify_result != 0) { | 
|  | 7502 | MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x", | 
|  | 7503 | (unsigned int) ssl->session_negotiate->verify_result)); | 
|  | 7504 | } else { | 
|  | 7505 | MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear")); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7506 | } | 
|  | 7507 | #endif /* MBEDTLS_DEBUG_C */ | 
|  | 7508 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7509 | return ret; | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7510 | } | 
|  | 7511 |  | 
|  | 7512 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7513 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7514 | static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl, | 
|  | 7515 | unsigned char *start, size_t len) | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7516 | { | 
|  | 7517 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 7518 | /* Remember digest of the peer's end-CRT. */ | 
|  | 7519 | ssl->session_negotiate->peer_cert_digest = | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7520 | mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN); | 
|  | 7521 | if (ssl->session_negotiate->peer_cert_digest == NULL) { | 
|  | 7522 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed", | 
|  | 7523 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN)); | 
|  | 7524 | mbedtls_ssl_send_alert_message(ssl, | 
|  | 7525 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 7526 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7527 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7528 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7529 | } | 
|  | 7530 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7531 | ret = mbedtls_md(mbedtls_md_info_from_type( | 
|  | 7532 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE), | 
|  | 7533 | start, len, | 
|  | 7534 | ssl->session_negotiate->peer_cert_digest); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7535 |  | 
|  | 7536 | ssl->session_negotiate->peer_cert_digest_type = | 
|  | 7537 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; | 
|  | 7538 | ssl->session_negotiate->peer_cert_digest_len = | 
|  | 7539 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; | 
|  | 7540 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7541 | return ret; | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7542 | } | 
|  | 7543 |  | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7544 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7545 | static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl, | 
|  | 7546 | unsigned char *start, size_t len) | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7547 | { | 
|  | 7548 | unsigned char *end = start + len; | 
|  | 7549 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 7550 |  | 
|  | 7551 | /* Make a copy of the peer's raw public key. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7552 | mbedtls_pk_init(&ssl->handshake->peer_pubkey); | 
|  | 7553 | ret = mbedtls_pk_parse_subpubkey(&start, end, | 
|  | 7554 | &ssl->handshake->peer_pubkey); | 
|  | 7555 | if (ret != 0) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7556 | /* We should have parsed the public key before. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7557 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7558 | } | 
|  | 7559 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7560 | return 0; | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7561 | } | 
|  | 7562 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 7563 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7564 | int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl) | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7565 | { | 
|  | 7566 | int ret = 0; | 
|  | 7567 | int crt_expected; | 
|  | 7568 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
|  | 7569 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET | 
|  | 7570 | ? ssl->handshake->sni_authmode | 
|  | 7571 | : ssl->conf->authmode; | 
|  | 7572 | #else | 
|  | 7573 | const int authmode = ssl->conf->authmode; | 
|  | 7574 | #endif | 
|  | 7575 | void *rs_ctx = NULL; | 
|  | 7576 | mbedtls_x509_crt *chain = NULL; | 
|  | 7577 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7578 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate")); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7579 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7580 | crt_expected = ssl_parse_certificate_coordinate(ssl, authmode); | 
|  | 7581 | if (crt_expected == SSL_CERTIFICATE_SKIP) { | 
|  | 7582 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate")); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7583 | goto exit; | 
|  | 7584 | } | 
|  | 7585 |  | 
|  | 7586 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7587 | if (ssl->handshake->ecrs_enabled && | 
|  | 7588 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7589 | chain = ssl->handshake->ecrs_peer_cert; | 
|  | 7590 | ssl->handshake->ecrs_peer_cert = NULL; | 
|  | 7591 | goto crt_verify; | 
|  | 7592 | } | 
|  | 7593 | #endif | 
|  | 7594 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7595 | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7596 | /* mbedtls_ssl_read_record may have sent an alert already. We | 
|  | 7597 | let it decide whether to alert. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7598 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7599 | goto exit; | 
|  | 7600 | } | 
|  | 7601 |  | 
|  | 7602 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7603 | if (ssl_srv_check_client_no_crt_notification(ssl) == 0) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7604 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; | 
|  | 7605 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7606 | if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7607 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7608 | } | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7609 |  | 
|  | 7610 | goto exit; | 
|  | 7611 | } | 
|  | 7612 | #endif /* MBEDTLS_SSL_SRV_C */ | 
|  | 7613 |  | 
|  | 7614 | /* Clear existing peer CRT structure in case we tried to | 
|  | 7615 | * reuse a session but it failed, and allocate a new one. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7616 | ssl_clear_peer_cert(ssl->session_negotiate); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7617 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7618 | chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt)); | 
|  | 7619 | if (chain == NULL) { | 
|  | 7620 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", | 
|  | 7621 | sizeof(mbedtls_x509_crt))); | 
|  | 7622 | mbedtls_ssl_send_alert_message(ssl, | 
|  | 7623 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 7624 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7625 |  | 
|  | 7626 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
|  | 7627 | goto exit; | 
|  | 7628 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7629 | mbedtls_x509_crt_init(chain); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7630 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7631 | ret = ssl_parse_certificate_chain(ssl, chain); | 
|  | 7632 | if (ret != 0) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7633 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7634 | } | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7635 |  | 
|  | 7636 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7637 | if (ssl->handshake->ecrs_enabled) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7638 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7639 | } | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7640 |  | 
|  | 7641 | crt_verify: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7642 | if (ssl->handshake->ecrs_enabled) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7643 | rs_ctx = &ssl->handshake->ecrs_ctx; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7644 | } | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7645 | #endif | 
|  | 7646 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7647 | ret = ssl_parse_certificate_verify(ssl, authmode, | 
|  | 7648 | chain, rs_ctx); | 
|  | 7649 | if (ret != 0) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7650 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7651 | } | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7652 |  | 
|  | 7653 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 7654 | { | 
|  | 7655 | unsigned char *crt_start, *pk_start; | 
|  | 7656 | size_t crt_len, pk_len; | 
|  | 7657 |  | 
|  | 7658 | /* We parse the CRT chain without copying, so | 
|  | 7659 | * these pointers point into the input buffer, | 
|  | 7660 | * and are hence still valid after freeing the | 
|  | 7661 | * CRT chain. */ | 
|  | 7662 |  | 
|  | 7663 | crt_start = chain->raw.p; | 
|  | 7664 | crt_len   = chain->raw.len; | 
|  | 7665 |  | 
|  | 7666 | pk_start = chain->pk_raw.p; | 
|  | 7667 | pk_len   = chain->pk_raw.len; | 
|  | 7668 |  | 
|  | 7669 | /* Free the CRT structures before computing | 
|  | 7670 | * digest and copying the peer's public key. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7671 | mbedtls_x509_crt_free(chain); | 
|  | 7672 | mbedtls_free(chain); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7673 | chain = NULL; | 
|  | 7674 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7675 | ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len); | 
|  | 7676 | if (ret != 0) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7677 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7678 | } | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7679 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7680 | ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len); | 
|  | 7681 | if (ret != 0) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7682 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7683 | } | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7684 | } | 
|  | 7685 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 7686 | /* Pass ownership to session structure. */ | 
|  | 7687 | ssl->session_negotiate->peer_cert = chain; | 
|  | 7688 | chain = NULL; | 
|  | 7689 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 7690 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7691 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate")); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7692 |  | 
|  | 7693 | exit: | 
|  | 7694 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7695 | if (ret == 0) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7696 | ssl->state++; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7697 | } | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7698 |  | 
|  | 7699 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7700 | if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) { | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7701 | ssl->handshake->ecrs_peer_cert = chain; | 
|  | 7702 | chain = NULL; | 
|  | 7703 | } | 
|  | 7704 | #endif | 
|  | 7705 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7706 | if (chain != NULL) { | 
|  | 7707 | mbedtls_x509_crt_free(chain); | 
|  | 7708 | mbedtls_free(chain); | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7709 | } | 
|  | 7710 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7711 | return ret; | 
| Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7712 | } | 
|  | 7713 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
|  | 7714 |  | 
| Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 7715 | static int ssl_calc_finished_tls_generic(mbedtls_ssl_context *ssl, void *ctx, | 
|  | 7716 | unsigned char *padbuf, size_t hlen, | 
|  | 7717 | unsigned char *buf, int from) | 
| Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7718 | { | 
| Dave Rodgman | c37ad44 | 2023-11-03 23:36:06 +0000 | [diff] [blame] | 7719 | unsigned int len = 12; | 
| Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7720 | const char *sender; | 
| Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7721 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7722 | psa_status_t status; | 
| Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 7723 | psa_hash_operation_t *hs_op = ctx; | 
| Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7724 | psa_hash_operation_t cloned_op = PSA_HASH_OPERATION_INIT; | 
| Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 7725 | size_t hash_size; | 
| Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7726 | #else | 
| Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 7727 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 7728 | mbedtls_md_context_t *hs_ctx = ctx; | 
| Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7729 | mbedtls_md_context_t cloned_ctx; | 
|  | 7730 | mbedtls_md_init(&cloned_ctx); | 
| Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7731 | #endif | 
|  | 7732 |  | 
|  | 7733 | mbedtls_ssl_session *session = ssl->session_negotiate; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7734 | if (!session) { | 
| Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7735 | session = ssl->session; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7736 | } | 
| Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7737 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7738 | sender = (from == MBEDTLS_SSL_IS_CLIENT) | 
| Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7739 | ? "client finished" | 
|  | 7740 | : "server finished"; | 
|  | 7741 |  | 
|  | 7742 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 7743 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls")); | 
| Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7744 |  | 
| Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7745 | status = psa_hash_clone(hs_op, &cloned_op); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7746 | if (status != PSA_SUCCESS) { | 
| Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 7747 | goto exit; | 
| Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7748 | } | 
|  | 7749 |  | 
| Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7750 | status = psa_hash_finish(&cloned_op, padbuf, hlen, &hash_size); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7751 | if (status != PSA_SUCCESS) { | 
| Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 7752 | goto exit; | 
| Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7753 | } | 
| Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 7754 | MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, hlen); | 
| Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7755 | #else | 
| Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 7756 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls")); | 
| Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7757 |  | 
| Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7758 | ret = mbedtls_md_setup(&cloned_ctx, mbedtls_md_info_from_ctx(hs_ctx), 0); | 
| Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 7759 | if (ret != 0) { | 
|  | 7760 | goto exit; | 
|  | 7761 | } | 
| Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7762 | ret = mbedtls_md_clone(&cloned_ctx, hs_ctx); | 
| Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 7763 | if (ret != 0) { | 
|  | 7764 | goto exit; | 
|  | 7765 | } | 
| Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7766 |  | 
| Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7767 | ret = mbedtls_md_finish(&cloned_ctx, padbuf); | 
| Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 7768 | if (ret != 0) { | 
|  | 7769 | goto exit; | 
|  | 7770 | } | 
|  | 7771 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 7772 |  | 
|  | 7773 | MBEDTLS_SSL_DEBUG_BUF(4, "finished output", padbuf, hlen); | 
|  | 7774 |  | 
| Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7775 | /* | 
|  | 7776 | * TLSv1.2: | 
|  | 7777 | *   hash = PRF( master, finished_label, | 
|  | 7778 | *               Hash( handshake ) )[0.11] | 
|  | 7779 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7780 | ssl->handshake->tls_prf(session->master, 48, sender, | 
| Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 7781 | padbuf, hlen, buf, len); | 
| Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7782 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7783 | MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len); | 
| Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7784 |  | 
| Paul Elliott | ecb95be | 2023-08-11 16:41:04 +0100 | [diff] [blame] | 7785 | mbedtls_platform_zeroize(padbuf, hlen); | 
| Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7786 |  | 
| Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 7787 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished")); | 
| Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 7788 |  | 
|  | 7789 | exit: | 
|  | 7790 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7791 | psa_hash_abort(&cloned_op); | 
| Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 7792 | return mbedtls_md_error_from_psa(status); | 
| Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 7793 | #else | 
| Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7794 | mbedtls_md_free(&cloned_ctx); | 
| Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 7795 | return ret; | 
|  | 7796 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7797 | } | 
| Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 7798 |  | 
|  | 7799 | #if defined(MBEDTLS_MD_CAN_SHA256) | 
|  | 7800 | static int ssl_calc_finished_tls_sha256( | 
|  | 7801 | mbedtls_ssl_context *ssl, unsigned char *buf, int from) | 
|  | 7802 | { | 
|  | 7803 | unsigned char padbuf[32]; | 
|  | 7804 | return ssl_calc_finished_tls_generic(ssl, | 
|  | 7805 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 7806 | &ssl->handshake->fin_sha256_psa, | 
|  | 7807 | #else | 
|  | 7808 | &ssl->handshake->fin_sha256, | 
|  | 7809 | #endif | 
|  | 7810 | padbuf, sizeof(padbuf), | 
|  | 7811 | buf, from); | 
|  | 7812 | } | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 7813 | #endif /* MBEDTLS_MD_CAN_SHA256*/ | 
| Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7814 |  | 
| Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7815 |  | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 7816 | #if defined(MBEDTLS_MD_CAN_SHA384) | 
| Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 7817 | static int ssl_calc_finished_tls_sha384( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7818 | mbedtls_ssl_context *ssl, unsigned char *buf, int from) | 
| Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7819 | { | 
| Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7820 | unsigned char padbuf[48]; | 
| Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 7821 | return ssl_calc_finished_tls_generic(ssl, | 
| Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7822 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 7823 | &ssl->handshake->fin_sha384_psa, | 
| Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7824 | #else | 
| Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 7825 | &ssl->handshake->fin_sha384, | 
| Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7826 | #endif | 
| Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 7827 | padbuf, sizeof(padbuf), | 
|  | 7828 | buf, from); | 
| Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7829 | } | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 7830 | #endif /* MBEDTLS_MD_CAN_SHA384*/ | 
| Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7831 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7832 | void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl) | 
| Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7833 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7834 | MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free")); | 
| Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7835 |  | 
|  | 7836 | /* | 
|  | 7837 | * Free our handshake params | 
|  | 7838 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7839 | mbedtls_ssl_handshake_free(ssl); | 
|  | 7840 | mbedtls_free(ssl->handshake); | 
| Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7841 | ssl->handshake = NULL; | 
|  | 7842 |  | 
|  | 7843 | /* | 
| Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 7844 | * Free the previous transform and switch in the current one | 
| Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7845 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7846 | if (ssl->transform) { | 
|  | 7847 | mbedtls_ssl_transform_free(ssl->transform); | 
|  | 7848 | mbedtls_free(ssl->transform); | 
| Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7849 | } | 
|  | 7850 | ssl->transform = ssl->transform_negotiate; | 
|  | 7851 | ssl->transform_negotiate = NULL; | 
|  | 7852 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7853 | MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free")); | 
| Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7854 | } | 
|  | 7855 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7856 | void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl) | 
| Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7857 | { | 
|  | 7858 | int resume = ssl->handshake->resume; | 
|  | 7859 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7860 | MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup")); | 
| Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7861 |  | 
|  | 7862 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7863 | if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) { | 
| Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7864 | ssl->renego_status =  MBEDTLS_SSL_RENEGOTIATION_DONE; | 
|  | 7865 | ssl->renego_records_seen = 0; | 
|  | 7866 | } | 
|  | 7867 | #endif | 
|  | 7868 |  | 
|  | 7869 | /* | 
|  | 7870 | * Free the previous session and switch in the current one | 
|  | 7871 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7872 | if (ssl->session) { | 
| Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7873 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 7874 | /* RFC 7366 3.1: keep the EtM state */ | 
|  | 7875 | ssl->session_negotiate->encrypt_then_mac = | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7876 | ssl->session->encrypt_then_mac; | 
| Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7877 | #endif | 
|  | 7878 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7879 | mbedtls_ssl_session_free(ssl->session); | 
|  | 7880 | mbedtls_free(ssl->session); | 
| Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7881 | } | 
|  | 7882 | ssl->session = ssl->session_negotiate; | 
|  | 7883 | ssl->session_negotiate = NULL; | 
|  | 7884 |  | 
|  | 7885 | /* | 
|  | 7886 | * Add cache entry | 
|  | 7887 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7888 | if (ssl->conf->f_set_cache != NULL && | 
| Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7889 | ssl->session->id_len != 0 && | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7890 | resume == 0) { | 
|  | 7891 | if (ssl->conf->f_set_cache(ssl->conf->p_cache, | 
|  | 7892 | ssl->session->id, | 
|  | 7893 | ssl->session->id_len, | 
|  | 7894 | ssl->session) != 0) { | 
|  | 7895 | MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session")); | 
|  | 7896 | } | 
| Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7897 | } | 
|  | 7898 |  | 
|  | 7899 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7900 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
|  | 7901 | ssl->handshake->flight != NULL) { | 
| Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7902 | /* Cancel handshake timer */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7903 | mbedtls_ssl_set_timer(ssl, 0); | 
| Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7904 |  | 
|  | 7905 | /* Keep last flight around in case we need to resend it: | 
|  | 7906 | * we need the handshake and transform structures for that */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7907 | MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform")); | 
|  | 7908 | } else | 
| Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7909 | #endif | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7910 | mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl); | 
| Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7911 |  | 
| Jerry Yu | 5ed73ff | 2022-10-27 13:08:42 +0800 | [diff] [blame] | 7912 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; | 
| Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7913 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7914 | MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup")); | 
| Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7915 | } | 
|  | 7916 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7917 | int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl) | 
| Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7918 | { | 
| Dave Rodgman | c37ad44 | 2023-11-03 23:36:06 +0000 | [diff] [blame] | 7919 | int ret; | 
|  | 7920 | unsigned int hash_len; | 
| Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7921 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7922 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished")); | 
| Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7923 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7924 | mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate); | 
| Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7925 |  | 
| Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 7926 | ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint); | 
|  | 7927 | if (ret != 0) { | 
|  | 7928 | MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret); | 
|  | 7929 | } | 
| Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7930 |  | 
|  | 7931 | /* | 
|  | 7932 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites | 
|  | 7933 | * may define some other value. Currently (early 2016), no defined | 
|  | 7934 | * ciphersuite does this (and this is unlikely to change as activity has | 
|  | 7935 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. | 
|  | 7936 | */ | 
|  | 7937 | hash_len = 12; | 
|  | 7938 |  | 
|  | 7939 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 7940 | ssl->verify_data_len = hash_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7941 | memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len); | 
| Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7942 | #endif | 
|  | 7943 |  | 
|  | 7944 | ssl->out_msglen  = 4 + hash_len; | 
|  | 7945 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 7946 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_FINISHED; | 
|  | 7947 |  | 
|  | 7948 | /* | 
|  | 7949 | * In case of session resuming, invert the client and server | 
|  | 7950 | * ChangeCipherSpec messages order. | 
|  | 7951 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7952 | if (ssl->handshake->resume != 0) { | 
| Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7953 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7954 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { | 
| Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7955 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7956 | } | 
| Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7957 | #endif | 
|  | 7958 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7959 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { | 
| Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7960 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7961 | } | 
| Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7962 | #endif | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7963 | } else { | 
| Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7964 | ssl->state++; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7965 | } | 
| Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7966 |  | 
|  | 7967 | /* | 
|  | 7968 | * Switch to our negotiated transform and session parameters for outbound | 
|  | 7969 | * data. | 
|  | 7970 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7971 | MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data")); | 
| Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7972 |  | 
|  | 7973 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7974 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { | 
| Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7975 | unsigned char i; | 
|  | 7976 |  | 
|  | 7977 | /* Remember current epoch settings for resending */ | 
|  | 7978 | ssl->handshake->alt_transform_out = ssl->transform_out; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7979 | memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, | 
|  | 7980 | sizeof(ssl->handshake->alt_out_ctr)); | 
| Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7981 |  | 
|  | 7982 | /* Set sequence_number to zero */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7983 | memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2); | 
| Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7984 |  | 
|  | 7985 |  | 
|  | 7986 | /* Increment epoch */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7987 | for (i = 2; i > 0; i--) { | 
|  | 7988 | if (++ssl->cur_out_ctr[i - 1] != 0) { | 
| Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7989 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7990 | } | 
|  | 7991 | } | 
| Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7992 |  | 
|  | 7993 | /* The loop goes to its end iff the counter is wrapping */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7994 | if (i == 0) { | 
|  | 7995 | MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap")); | 
|  | 7996 | return MBEDTLS_ERR_SSL_COUNTER_WRAPPING; | 
| Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7997 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7998 | } else | 
| Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7999 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8000 | memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr)); | 
| Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8001 |  | 
|  | 8002 | ssl->transform_out = ssl->transform_negotiate; | 
|  | 8003 | ssl->session_out = ssl->session_negotiate; | 
|  | 8004 |  | 
|  | 8005 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8006 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { | 
|  | 8007 | mbedtls_ssl_send_flight_completed(ssl); | 
|  | 8008 | } | 
| Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8009 | #endif | 
|  | 8010 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8011 | if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { | 
|  | 8012 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret); | 
|  | 8013 | return ret; | 
| Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8014 | } | 
|  | 8015 |  | 
|  | 8016 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8017 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
|  | 8018 | (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) { | 
|  | 8019 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret); | 
|  | 8020 | return ret; | 
| Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8021 | } | 
|  | 8022 | #endif | 
|  | 8023 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8024 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished")); | 
| Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8025 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8026 | return 0; | 
| Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8027 | } | 
|  | 8028 |  | 
| Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8029 | #define SSL_MAX_HASH_LEN 12 | 
|  | 8030 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8031 | int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl) | 
| Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8032 | { | 
|  | 8033 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 8034 | unsigned int hash_len = 12; | 
|  | 8035 | unsigned char buf[SSL_MAX_HASH_LEN]; | 
|  | 8036 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8037 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished")); | 
| Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8038 |  | 
| Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 8039 | ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1); | 
|  | 8040 | if (ret != 0) { | 
|  | 8041 | MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret); | 
|  | 8042 | } | 
| Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8043 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8044 | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { | 
|  | 8045 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); | 
| Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8046 | goto exit; | 
|  | 8047 | } | 
|  | 8048 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8049 | if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) { | 
|  | 8050 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message")); | 
|  | 8051 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 8052 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); | 
| Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8053 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; | 
|  | 8054 | goto exit; | 
|  | 8055 | } | 
|  | 8056 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8057 | if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) { | 
|  | 8058 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 8059 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); | 
| Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8060 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; | 
|  | 8061 | goto exit; | 
|  | 8062 | } | 
|  | 8063 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8064 | if (ssl->in_hslen  != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) { | 
|  | 8065 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message")); | 
|  | 8066 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 8067 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); | 
| Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8068 | ret = MBEDTLS_ERR_SSL_DECODE_ERROR; | 
|  | 8069 | goto exit; | 
|  | 8070 | } | 
|  | 8071 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8072 | if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), | 
|  | 8073 | buf, hash_len) != 0) { | 
|  | 8074 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message")); | 
|  | 8075 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 8076 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR); | 
| Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8077 | ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; | 
|  | 8078 | goto exit; | 
|  | 8079 | } | 
|  | 8080 |  | 
|  | 8081 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 8082 | ssl->verify_data_len = hash_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8083 | memcpy(ssl->peer_verify_data, buf, hash_len); | 
| Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8084 | #endif | 
|  | 8085 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8086 | if (ssl->handshake->resume != 0) { | 
| Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8087 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8088 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { | 
| Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8089 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8090 | } | 
| Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8091 | #endif | 
|  | 8092 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8093 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { | 
| Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8094 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8095 | } | 
| Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8096 | #endif | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8097 | } else { | 
| Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8098 | ssl->state++; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8099 | } | 
| Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8100 |  | 
|  | 8101 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8102 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { | 
|  | 8103 | mbedtls_ssl_recv_flight_completed(ssl); | 
|  | 8104 | } | 
| Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8105 | #endif | 
|  | 8106 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8107 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished")); | 
| Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8108 |  | 
|  | 8109 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8110 | mbedtls_platform_zeroize(buf, hash_len); | 
|  | 8111 | return ret; | 
| Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8112 | } | 
|  | 8113 |  | 
| Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 8114 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) | 
|  | 8115 | /* | 
|  | 8116 | * Helper to get TLS 1.2 PRF from ciphersuite | 
|  | 8117 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) | 
|  | 8118 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8119 | static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id) | 
| Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 8120 | { | 
| Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 8121 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8122 | mbedtls_ssl_ciphersuite_from_id(ciphersuite_id); | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 8123 | #if defined(MBEDTLS_MD_CAN_SHA384) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8124 | if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) { | 
|  | 8125 | return tls_prf_sha384; | 
|  | 8126 | } else | 
| Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 8127 | #endif | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 8128 | #if defined(MBEDTLS_MD_CAN_SHA256) | 
| Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 8129 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8130 | if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) { | 
|  | 8131 | return tls_prf_sha256; | 
|  | 8132 | } | 
| Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 8133 | } | 
|  | 8134 | #endif | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 8135 | #if !defined(MBEDTLS_MD_CAN_SHA384) && \ | 
|  | 8136 | !defined(MBEDTLS_MD_CAN_SHA256) | 
| Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 8137 | (void) ciphersuite_info; | 
|  | 8138 | #endif | 
| Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 8139 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8140 | return NULL; | 
| Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 8141 | } | 
|  | 8142 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ | 
| Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 8143 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8144 | static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf) | 
| Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 8145 | { | 
|  | 8146 | ((void) tls_prf); | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 8147 | #if defined(MBEDTLS_MD_CAN_SHA384) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8148 | if (tls_prf == tls_prf_sha384) { | 
|  | 8149 | return MBEDTLS_SSL_TLS_PRF_SHA384; | 
|  | 8150 | } else | 
| Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 8151 | #endif | 
| Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 8152 | #if defined(MBEDTLS_MD_CAN_SHA256) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8153 | if (tls_prf == tls_prf_sha256) { | 
|  | 8154 | return MBEDTLS_SSL_TLS_PRF_SHA256; | 
|  | 8155 | } else | 
| Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 8156 | #endif | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8157 | return MBEDTLS_SSL_TLS_PRF_NONE; | 
| Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 8158 | } | 
|  | 8159 |  | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8160 | /* | 
|  | 8161 | * Populate a transform structure with session keys and all the other | 
|  | 8162 | * necessary information. | 
|  | 8163 | * | 
|  | 8164 | * Parameters: | 
|  | 8165 | * - [in/out]: transform: structure to populate | 
|  | 8166 | *      [in] must be just initialised with mbedtls_ssl_transform_init() | 
|  | 8167 | *      [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf() | 
|  | 8168 | * - [in] ciphersuite | 
|  | 8169 | * - [in] master | 
|  | 8170 | * - [in] encrypt_then_mac | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8171 | * - [in] tls_prf: pointer to PRF to use for key derivation | 
|  | 8172 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random | 
| Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 8173 | * - [in] tls_version: TLS version | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8174 | * - [in] endpoint: client or server | 
|  | 8175 | * - [in] ssl: used for: | 
|  | 8176 | *        - ssl->conf->{f,p}_export_keys | 
|  | 8177 | *      [in] optionally used for: | 
|  | 8178 | *        - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg | 
|  | 8179 | */ | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 8180 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8181 | static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform, | 
|  | 8182 | int ciphersuite, | 
|  | 8183 | const unsigned char master[48], | 
| Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8184 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8185 | int encrypt_then_mac, | 
| Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8186 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8187 | ssl_tls_prf_t tls_prf, | 
|  | 8188 | const unsigned char randbytes[64], | 
|  | 8189 | mbedtls_ssl_protocol_version tls_version, | 
|  | 8190 | unsigned endpoint, | 
|  | 8191 | const mbedtls_ssl_context *ssl) | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8192 | { | 
|  | 8193 | int ret = 0; | 
|  | 8194 | unsigned char keyblk[256]; | 
|  | 8195 | unsigned char *key1; | 
|  | 8196 | unsigned char *key2; | 
|  | 8197 | unsigned char *mac_enc; | 
|  | 8198 | unsigned char *mac_dec; | 
|  | 8199 | size_t mac_key_len = 0; | 
|  | 8200 | size_t iv_copy_len; | 
|  | 8201 | size_t keylen; | 
|  | 8202 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; | 
| Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8203 | mbedtls_ssl_mode_t ssl_mode; | 
| Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8204 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8205 | const mbedtls_cipher_info_t *cipher_info; | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8206 | const mbedtls_md_info_t *md_info; | 
| Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8207 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8208 |  | 
|  | 8209 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 8210 | psa_key_type_t key_type; | 
|  | 8211 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
|  | 8212 | psa_algorithm_t alg; | 
| Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8213 | psa_algorithm_t mac_alg = 0; | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8214 | size_t key_bits; | 
|  | 8215 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 8216 | #endif | 
|  | 8217 |  | 
|  | 8218 | #if !defined(MBEDTLS_DEBUG_C) && \ | 
|  | 8219 | !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8220 | if (ssl->f_export_keys == NULL) { | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8221 | ssl = NULL; /* make sure we don't use it except for these cases */ | 
|  | 8222 | (void) ssl; | 
|  | 8223 | } | 
|  | 8224 | #endif | 
|  | 8225 |  | 
|  | 8226 | /* | 
|  | 8227 | * Some data just needs copying into the structure | 
|  | 8228 | */ | 
| Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8229 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8230 | transform->encrypt_then_mac = encrypt_then_mac; | 
| Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8231 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ | 
| Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 8232 | transform->tls_version = tls_version; | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8233 |  | 
|  | 8234 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8235 | memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes)); | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8236 | #endif | 
|  | 8237 |  | 
|  | 8238 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8239 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8240 | /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform | 
|  | 8241 | * generation separate. This should never happen. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8242 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8243 | } | 
|  | 8244 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
|  | 8245 |  | 
|  | 8246 | /* | 
|  | 8247 | * Get various info structures | 
|  | 8248 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8249 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite); | 
|  | 8250 | if (ciphersuite_info == NULL) { | 
|  | 8251 | MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found", | 
|  | 8252 | ciphersuite)); | 
|  | 8253 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8254 | } | 
|  | 8255 |  | 
| Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 8256 | ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite( | 
| Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8257 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8258 | encrypt_then_mac, | 
| Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8259 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8260 | ciphersuite_info); | 
| Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8261 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8262 | if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) { | 
| Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8263 | transform->taglen = | 
|  | 8264 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8265 | } | 
| Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8266 |  | 
|  | 8267 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Dave Rodgman | 2eab462 | 2023-10-05 13:30:37 +0100 | [diff] [blame] | 8268 | if ((status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) ciphersuite_info->cipher, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8269 | transform->taglen, | 
|  | 8270 | &alg, | 
|  | 8271 | &key_type, | 
|  | 8272 | &key_bits)) != PSA_SUCCESS) { | 
| Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 8273 | ret = PSA_TO_MBEDTLS_ERR(status); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8274 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret); | 
| Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8275 | goto end; | 
|  | 8276 | } | 
|  | 8277 | #else | 
| Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 8278 | cipher_info = mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) ciphersuite_info->cipher); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8279 | if (cipher_info == NULL) { | 
|  | 8280 | MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found", | 
|  | 8281 | ciphersuite_info->cipher)); | 
|  | 8282 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8283 | } | 
| Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8284 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8285 |  | 
| Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8286 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Dave Rodgman | 2eab462 | 2023-10-05 13:30:37 +0100 | [diff] [blame] | 8287 | mac_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8288 | if (mac_alg == 0) { | 
| Manuel Pégourié-Gonnard | 2d6d993 | 2023-03-28 11:38:08 +0200 | [diff] [blame] | 8289 | MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md_psa_alg_from_type for %u not found", | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8290 | (unsigned) ciphersuite_info->mac)); | 
|  | 8291 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
| Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8292 | } | 
|  | 8293 | #else | 
| Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 8294 | md_info = mbedtls_md_info_from_type((mbedtls_md_type_t) ciphersuite_info->mac); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8295 | if (md_info == NULL) { | 
|  | 8296 | MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found", | 
|  | 8297 | (unsigned) ciphersuite_info->mac)); | 
|  | 8298 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8299 | } | 
| Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8300 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8301 |  | 
|  | 8302 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
|  | 8303 | /* Copy own and peer's CID if the use of the CID | 
|  | 8304 | * extension has been negotiated. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8305 | if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) { | 
|  | 8306 | MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform")); | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8307 |  | 
|  | 8308 | transform->in_cid_len = ssl->own_cid_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8309 | memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len); | 
|  | 8310 | MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid, | 
|  | 8311 | transform->in_cid_len); | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8312 |  | 
|  | 8313 | transform->out_cid_len = ssl->handshake->peer_cid_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8314 | memcpy(transform->out_cid, ssl->handshake->peer_cid, | 
|  | 8315 | ssl->handshake->peer_cid_len); | 
|  | 8316 | MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid, | 
|  | 8317 | transform->out_cid_len); | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8318 | } | 
|  | 8319 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
|  | 8320 |  | 
|  | 8321 | /* | 
|  | 8322 | * Compute key block using the PRF | 
|  | 8323 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8324 | ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256); | 
|  | 8325 | if (ret != 0) { | 
|  | 8326 | MBEDTLS_SSL_DEBUG_RET(1, "prf", ret); | 
|  | 8327 | return ret; | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8328 | } | 
|  | 8329 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8330 | MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s", | 
|  | 8331 | mbedtls_ssl_get_ciphersuite_name(ciphersuite))); | 
|  | 8332 | MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48); | 
|  | 8333 | MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64); | 
|  | 8334 | MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256); | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8335 |  | 
|  | 8336 | /* | 
|  | 8337 | * Determine the appropriate key, IV and MAC length. | 
|  | 8338 | */ | 
|  | 8339 |  | 
| Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8340 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 8341 | keylen = PSA_BITS_TO_BYTES(key_bits); | 
|  | 8342 | #else | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8343 | keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8; | 
| Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8344 | #endif | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8345 |  | 
| Valerio Setti | e570704 | 2023-10-11 11:54:42 +0200 | [diff] [blame] | 8346 | #if defined(MBEDTLS_SSL_HAVE_AEAD) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8347 | if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) { | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8348 | size_t explicit_ivlen; | 
|  | 8349 |  | 
|  | 8350 | transform->maclen = 0; | 
|  | 8351 | mac_key_len = 0; | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8352 |  | 
|  | 8353 | /* All modes haves 96-bit IVs, but the length of the static parts vary | 
|  | 8354 | * with mode and version: | 
|  | 8355 | * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes | 
|  | 8356 | *   (to be concatenated with a dynamically chosen IV of 8 Bytes) | 
|  | 8357 | * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's | 
|  | 8358 | *   a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record | 
|  | 8359 | *   sequence number). | 
|  | 8360 | */ | 
|  | 8361 | transform->ivlen = 12; | 
| David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 8362 |  | 
|  | 8363 | int is_chachapoly = 0; | 
| Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8364 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8365 | is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20); | 
| Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8366 | #else | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8367 | is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info) | 
|  | 8368 | == MBEDTLS_MODE_CHACHAPOLY); | 
| Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8369 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 8370 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8371 | if (is_chachapoly) { | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8372 | transform->fixed_ivlen = 12; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8373 | } else { | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8374 | transform->fixed_ivlen = 4; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8375 | } | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8376 |  | 
|  | 8377 | /* Minimum length of encrypted record */ | 
|  | 8378 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; | 
|  | 8379 | transform->minlen = explicit_ivlen + transform->taglen; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8380 | } else | 
| Valerio Setti | e570704 | 2023-10-11 11:54:42 +0200 | [diff] [blame] | 8381 | #endif /* MBEDTLS_SSL_HAVE_AEAD */ | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8382 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8383 | if (ssl_mode == MBEDTLS_SSL_MODE_STREAM || | 
| Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8384 | ssl_mode == MBEDTLS_SSL_MODE_CBC || | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8385 | ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) { | 
| Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8386 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8387 | size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type); | 
| Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8388 | #else | 
| Dave Rodgman | 85a8813 | 2023-06-24 11:41:50 +0100 | [diff] [blame] | 8389 | size_t block_size = mbedtls_cipher_info_get_block_size(cipher_info); | 
| Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8390 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 8391 |  | 
|  | 8392 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8393 | /* Get MAC length */ | 
|  | 8394 | mac_key_len = PSA_HASH_LENGTH(mac_alg); | 
|  | 8395 | #else | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8396 | /* Initialize HMAC contexts */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8397 | if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 || | 
|  | 8398 | (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) { | 
|  | 8399 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret); | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8400 | goto end; | 
|  | 8401 | } | 
|  | 8402 |  | 
|  | 8403 | /* Get MAC length */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8404 | mac_key_len = mbedtls_md_get_size(md_info); | 
| Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8405 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8406 | transform->maclen = mac_key_len; | 
|  | 8407 |  | 
|  | 8408 | /* IV length */ | 
| Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8409 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8410 | transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg); | 
| Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8411 | #else | 
| Dave Rodgman | bb521fd | 2023-06-24 11:21:25 +0100 | [diff] [blame] | 8412 | transform->ivlen = mbedtls_cipher_info_get_iv_size(cipher_info); | 
| Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8413 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8414 |  | 
|  | 8415 | /* Minimum length */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8416 | if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) { | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8417 | transform->minlen = transform->maclen; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8418 | } else { | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8419 | /* | 
|  | 8420 | * GenericBlockCipher: | 
|  | 8421 | * 1. if EtM is in use: one block plus MAC | 
|  | 8422 | *    otherwise: * first multiple of blocklen greater than maclen | 
|  | 8423 | * 2. IV | 
|  | 8424 | */ | 
|  | 8425 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8426 | if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) { | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8427 | transform->minlen = transform->maclen | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8428 | + block_size; | 
|  | 8429 | } else | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8430 | #endif | 
|  | 8431 | { | 
|  | 8432 | transform->minlen = transform->maclen | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8433 | + block_size | 
|  | 8434 | - transform->maclen % block_size; | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8435 | } | 
|  | 8436 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8437 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) { | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8438 | transform->minlen += transform->ivlen; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8439 | } else { | 
|  | 8440 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8441 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 
|  | 8442 | goto end; | 
|  | 8443 | } | 
|  | 8444 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8445 | } else | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8446 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ | 
|  | 8447 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8448 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); | 
|  | 8449 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8450 | } | 
|  | 8451 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8452 | MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u", | 
|  | 8453 | (unsigned) keylen, | 
|  | 8454 | (unsigned) transform->minlen, | 
|  | 8455 | (unsigned) transform->ivlen, | 
|  | 8456 | (unsigned) transform->maclen)); | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8457 |  | 
|  | 8458 | /* | 
|  | 8459 | * Finally setup the cipher contexts, IVs and MAC secrets. | 
|  | 8460 | */ | 
|  | 8461 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8462 | if (endpoint == MBEDTLS_SSL_IS_CLIENT) { | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8463 | key1 = keyblk + mac_key_len * 2; | 
|  | 8464 | key2 = keyblk + mac_key_len * 2 + keylen; | 
|  | 8465 |  | 
|  | 8466 | mac_enc = keyblk; | 
|  | 8467 | mac_dec = keyblk + mac_key_len; | 
|  | 8468 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8469 | iv_copy_len = (transform->fixed_ivlen) ? | 
|  | 8470 | transform->fixed_ivlen : transform->ivlen; | 
|  | 8471 | memcpy(transform->iv_enc, key2 + keylen,  iv_copy_len); | 
|  | 8472 | memcpy(transform->iv_dec, key2 + keylen + iv_copy_len, | 
|  | 8473 | iv_copy_len); | 
|  | 8474 | } else | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8475 | #endif /* MBEDTLS_SSL_CLI_C */ | 
|  | 8476 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8477 | if (endpoint == MBEDTLS_SSL_IS_SERVER) { | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8478 | key1 = keyblk + mac_key_len * 2 + keylen; | 
|  | 8479 | key2 = keyblk + mac_key_len * 2; | 
|  | 8480 |  | 
|  | 8481 | mac_enc = keyblk + mac_key_len; | 
|  | 8482 | mac_dec = keyblk; | 
|  | 8483 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8484 | iv_copy_len = (transform->fixed_ivlen) ? | 
|  | 8485 | transform->fixed_ivlen : transform->ivlen; | 
|  | 8486 | memcpy(transform->iv_dec, key1 + keylen,  iv_copy_len); | 
|  | 8487 | memcpy(transform->iv_enc, key1 + keylen + iv_copy_len, | 
|  | 8488 | iv_copy_len); | 
|  | 8489 | } else | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8490 | #endif /* MBEDTLS_SSL_SRV_C */ | 
|  | 8491 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8492 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8493 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 
|  | 8494 | goto end; | 
|  | 8495 | } | 
|  | 8496 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8497 | if (ssl != NULL && ssl->f_export_keys != NULL) { | 
|  | 8498 | ssl->f_export_keys(ssl->p_export_keys, | 
|  | 8499 | MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET, | 
|  | 8500 | master, 48, | 
|  | 8501 | randbytes + 32, | 
|  | 8502 | randbytes, | 
|  | 8503 | tls_prf_get_type(tls_prf)); | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8504 | } | 
|  | 8505 |  | 
|  | 8506 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8507 | transform->psa_alg = alg; | 
|  | 8508 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8509 | if (alg != MBEDTLS_SSL_NULL_CIPHER) { | 
|  | 8510 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); | 
|  | 8511 | psa_set_key_algorithm(&attributes, alg); | 
|  | 8512 | psa_set_key_type(&attributes, key_type); | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8513 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8514 | if ((status = psa_import_key(&attributes, | 
|  | 8515 | key1, | 
|  | 8516 | PSA_BITS_TO_BYTES(key_bits), | 
|  | 8517 | &transform->psa_key_enc)) != PSA_SUCCESS) { | 
|  | 8518 | MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status); | 
| Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 8519 | ret = PSA_TO_MBEDTLS_ERR(status); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8520 | MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret); | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8521 | goto end; | 
|  | 8522 | } | 
|  | 8523 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8524 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8525 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8526 | if ((status = psa_import_key(&attributes, | 
|  | 8527 | key2, | 
|  | 8528 | PSA_BITS_TO_BYTES(key_bits), | 
|  | 8529 | &transform->psa_key_dec)) != PSA_SUCCESS) { | 
| Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 8530 | ret = PSA_TO_MBEDTLS_ERR(status); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8531 | MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret); | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8532 | goto end; | 
|  | 8533 | } | 
|  | 8534 | } | 
|  | 8535 | #else | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8536 | if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc, | 
|  | 8537 | cipher_info)) != 0) { | 
|  | 8538 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret); | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8539 | goto end; | 
|  | 8540 | } | 
|  | 8541 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8542 | if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec, | 
|  | 8543 | cipher_info)) != 0) { | 
|  | 8544 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret); | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8545 | goto end; | 
|  | 8546 | } | 
|  | 8547 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8548 | if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1, | 
|  | 8549 | (int) mbedtls_cipher_info_get_key_bitlen(cipher_info), | 
|  | 8550 | MBEDTLS_ENCRYPT)) != 0) { | 
|  | 8551 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret); | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8552 | goto end; | 
|  | 8553 | } | 
|  | 8554 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8555 | if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2, | 
|  | 8556 | (int) mbedtls_cipher_info_get_key_bitlen(cipher_info), | 
|  | 8557 | MBEDTLS_DECRYPT)) != 0) { | 
|  | 8558 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret); | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8559 | goto end; | 
|  | 8560 | } | 
|  | 8561 |  | 
|  | 8562 | #if defined(MBEDTLS_CIPHER_MODE_CBC) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8563 | if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) { | 
|  | 8564 | if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc, | 
|  | 8565 | MBEDTLS_PADDING_NONE)) != 0) { | 
|  | 8566 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret); | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8567 | goto end; | 
|  | 8568 | } | 
|  | 8569 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8570 | if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec, | 
|  | 8571 | MBEDTLS_PADDING_NONE)) != 0) { | 
|  | 8572 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret); | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8573 | goto end; | 
|  | 8574 | } | 
|  | 8575 | } | 
|  | 8576 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ | 
|  | 8577 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 8578 |  | 
| Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8579 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) | 
|  | 8580 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. | 
|  | 8581 | For AEAD-based ciphersuites, there is nothing to do here. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8582 | if (mac_key_len != 0) { | 
| Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8583 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8584 | transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg); | 
| Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8585 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8586 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); | 
|  | 8587 | psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg)); | 
|  | 8588 | psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC); | 
| Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8589 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8590 | if ((status = psa_import_key(&attributes, | 
|  | 8591 | mac_enc, mac_key_len, | 
|  | 8592 | &transform->psa_mac_enc)) != PSA_SUCCESS) { | 
| Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 8593 | ret = PSA_TO_MBEDTLS_ERR(status); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8594 | MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret); | 
| Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8595 | goto end; | 
|  | 8596 | } | 
|  | 8597 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8598 | if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) || | 
|  | 8599 | ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING) | 
| Andrzej Kurek | 8c95ac4 | 2022-08-17 16:17:00 -0400 | [diff] [blame] | 8600 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8601 | && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED) | 
| Andrzej Kurek | 8c95ac4 | 2022-08-17 16:17:00 -0400 | [diff] [blame] | 8602 | #endif | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8603 | )) { | 
| Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8604 | /* mbedtls_ct_hmac() requires the key to be exportable */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8605 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT | | 
|  | 8606 | PSA_KEY_USAGE_VERIFY_HASH); | 
|  | 8607 | } else { | 
|  | 8608 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); | 
|  | 8609 | } | 
| Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8610 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8611 | if ((status = psa_import_key(&attributes, | 
|  | 8612 | mac_dec, mac_key_len, | 
|  | 8613 | &transform->psa_mac_dec)) != PSA_SUCCESS) { | 
| Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 8614 | ret = PSA_TO_MBEDTLS_ERR(status); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8615 | MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret); | 
| Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8616 | goto end; | 
|  | 8617 | } | 
|  | 8618 | #else | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8619 | ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len); | 
|  | 8620 | if (ret != 0) { | 
| Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8621 | goto end; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8622 | } | 
|  | 8623 | ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len); | 
|  | 8624 | if (ret != 0) { | 
| Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8625 | goto end; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8626 | } | 
| Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8627 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 8628 | } | 
|  | 8629 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ | 
|  | 8630 |  | 
|  | 8631 | ((void) mac_dec); | 
|  | 8632 | ((void) mac_enc); | 
|  | 8633 |  | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8634 | end: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8635 | mbedtls_platform_zeroize(keyblk, sizeof(keyblk)); | 
|  | 8636 | return ret; | 
| Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8637 | } | 
|  | 8638 |  | 
| Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8639 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \ | 
|  | 8640 | defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8641 | int mbedtls_psa_ecjpake_read_round( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8642 | psa_pake_operation_t *pake_ctx, | 
|  | 8643 | const unsigned char *buf, | 
|  | 8644 | size_t len, mbedtls_ecjpake_rounds_t round) | 
| Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8645 | { | 
|  | 8646 | psa_status_t status; | 
|  | 8647 | size_t input_offset = 0; | 
| Valerio Setti | 819de86 | 2022-11-17 18:05:19 +0100 | [diff] [blame] | 8648 | /* | 
| Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8649 | * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice | 
|  | 8650 | * At round two perform a single cycle | 
|  | 8651 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8652 | unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1; | 
| Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8653 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8654 | for (; remaining_steps > 0; remaining_steps--) { | 
|  | 8655 | for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE; | 
| Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8656 | step <= PSA_PAKE_STEP_ZK_PROOF; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8657 | ++step) { | 
| Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8658 | /* Length is stored at the first byte */ | 
|  | 8659 | size_t length = buf[input_offset]; | 
|  | 8660 | input_offset += 1; | 
|  | 8661 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8662 | if (input_offset + length > len) { | 
| Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8663 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; | 
|  | 8664 | } | 
|  | 8665 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8666 | status = psa_pake_input(pake_ctx, step, | 
|  | 8667 | buf + input_offset, length); | 
|  | 8668 | if (status != PSA_SUCCESS) { | 
| Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 8669 | return PSA_TO_MBEDTLS_ERR(status); | 
| Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8670 | } | 
|  | 8671 |  | 
|  | 8672 | input_offset += length; | 
|  | 8673 | } | 
|  | 8674 | } | 
|  | 8675 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8676 | if (input_offset != len) { | 
| Valerio Setti | 61ea17d | 2022-11-18 12:11:00 +0100 | [diff] [blame] | 8677 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8678 | } | 
| Valerio Setti | 30ebe11 | 2022-11-17 16:23:34 +0100 | [diff] [blame] | 8679 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8680 | return 0; | 
| Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8681 | } | 
|  | 8682 |  | 
| Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8683 | int mbedtls_psa_ecjpake_write_round( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8684 | psa_pake_operation_t *pake_ctx, | 
|  | 8685 | unsigned char *buf, | 
|  | 8686 | size_t len, size_t *olen, | 
|  | 8687 | mbedtls_ecjpake_rounds_t round) | 
| Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8688 | { | 
|  | 8689 | psa_status_t status; | 
|  | 8690 | size_t output_offset = 0; | 
|  | 8691 | size_t output_len; | 
| Valerio Setti | 819de86 | 2022-11-17 18:05:19 +0100 | [diff] [blame] | 8692 | /* | 
| Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8693 | * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice | 
|  | 8694 | * At round two perform a single cycle | 
|  | 8695 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8696 | unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1; | 
| Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8697 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8698 | for (; remaining_steps > 0; remaining_steps--) { | 
|  | 8699 | for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE; | 
|  | 8700 | step <= PSA_PAKE_STEP_ZK_PROOF; | 
|  | 8701 | ++step) { | 
| Valerio Setti | 79f6b6b | 2022-11-21 14:17:03 +0100 | [diff] [blame] | 8702 | /* | 
| Valerio Setti | d4a9b1a | 2022-11-22 11:11:10 +0100 | [diff] [blame] | 8703 | * For each step, prepend 1 byte with the length of the data as | 
|  | 8704 | * given by psa_pake_output(). | 
| Valerio Setti | 79f6b6b | 2022-11-21 14:17:03 +0100 | [diff] [blame] | 8705 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8706 | status = psa_pake_output(pake_ctx, step, | 
|  | 8707 | buf + output_offset + 1, | 
|  | 8708 | len - output_offset - 1, | 
|  | 8709 | &output_len); | 
|  | 8710 | if (status != PSA_SUCCESS) { | 
| Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 8711 | return PSA_TO_MBEDTLS_ERR(status); | 
| Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8712 | } | 
|  | 8713 |  | 
| Valerio Setti | 99d88c1 | 2022-11-22 16:03:43 +0100 | [diff] [blame] | 8714 | *(buf + output_offset) = (uint8_t) output_len; | 
| Valerio Setti | 79f6b6b | 2022-11-21 14:17:03 +0100 | [diff] [blame] | 8715 |  | 
|  | 8716 | output_offset += output_len + 1; | 
| Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8717 | } | 
|  | 8718 | } | 
|  | 8719 |  | 
|  | 8720 | *olen = output_offset; | 
|  | 8721 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8722 | return 0; | 
| Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8723 | } | 
| Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8724 | #endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO | 
|  | 8725 |  | 
| Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8726 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8727 | int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl, | 
|  | 8728 | unsigned char *hash, size_t *hashlen, | 
|  | 8729 | unsigned char *data, size_t data_len, | 
|  | 8730 | mbedtls_md_type_t md_alg) | 
| Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8731 | { | 
|  | 8732 | psa_status_t status; | 
|  | 8733 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; | 
| Manuel Pégourié-Gonnard | 2d6d993 | 2023-03-28 11:38:08 +0200 | [diff] [blame] | 8734 | psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(md_alg); | 
| Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8735 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8736 | MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange")); | 
| Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8737 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8738 | if ((status = psa_hash_setup(&hash_operation, | 
|  | 8739 | hash_alg)) != PSA_SUCCESS) { | 
|  | 8740 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status); | 
| Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8741 | goto exit; | 
|  | 8742 | } | 
|  | 8743 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8744 | if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes, | 
|  | 8745 | 64)) != PSA_SUCCESS) { | 
|  | 8746 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status); | 
| Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8747 | goto exit; | 
|  | 8748 | } | 
|  | 8749 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8750 | if ((status = psa_hash_update(&hash_operation, | 
|  | 8751 | data, data_len)) != PSA_SUCCESS) { | 
|  | 8752 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status); | 
| Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8753 | goto exit; | 
|  | 8754 | } | 
|  | 8755 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8756 | if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE, | 
|  | 8757 | hashlen)) != PSA_SUCCESS) { | 
|  | 8758 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status); | 
|  | 8759 | goto exit; | 
| Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8760 | } | 
|  | 8761 |  | 
|  | 8762 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8763 | if (status != PSA_SUCCESS) { | 
|  | 8764 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 8765 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); | 
|  | 8766 | switch (status) { | 
| Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8767 | case PSA_ERROR_NOT_SUPPORTED: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8768 | return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE; | 
| Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8769 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ | 
|  | 8770 | case PSA_ERROR_BUFFER_TOO_SMALL: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8771 | return MBEDTLS_ERR_MD_BAD_INPUT_DATA; | 
| Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8772 | case PSA_ERROR_INSUFFICIENT_MEMORY: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8773 | return MBEDTLS_ERR_MD_ALLOC_FAILED; | 
| Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8774 | default: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8775 | return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; | 
| Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8776 | } | 
|  | 8777 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8778 | return 0; | 
| Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8779 | } | 
|  | 8780 |  | 
|  | 8781 | #else | 
|  | 8782 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8783 | int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl, | 
|  | 8784 | unsigned char *hash, size_t *hashlen, | 
|  | 8785 | unsigned char *data, size_t data_len, | 
|  | 8786 | mbedtls_md_type_t md_alg) | 
| Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8787 | { | 
|  | 8788 | int ret = 0; | 
|  | 8789 | mbedtls_md_context_t ctx; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8790 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg); | 
|  | 8791 | *hashlen = mbedtls_md_get_size(md_info); | 
| Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8792 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8793 | MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange")); | 
| Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8794 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8795 | mbedtls_md_init(&ctx); | 
| Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8796 |  | 
|  | 8797 | /* | 
|  | 8798 | * digitally-signed struct { | 
|  | 8799 | *     opaque client_random[32]; | 
|  | 8800 | *     opaque server_random[32]; | 
|  | 8801 | *     ServerDHParams params; | 
|  | 8802 | * }; | 
|  | 8803 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8804 | if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) { | 
|  | 8805 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret); | 
| Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8806 | goto exit; | 
|  | 8807 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8808 | if ((ret = mbedtls_md_starts(&ctx)) != 0) { | 
|  | 8809 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret); | 
| Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8810 | goto exit; | 
|  | 8811 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8812 | if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) { | 
|  | 8813 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret); | 
| Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8814 | goto exit; | 
|  | 8815 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8816 | if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) { | 
|  | 8817 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret); | 
| Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8818 | goto exit; | 
|  | 8819 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8820 | if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) { | 
|  | 8821 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret); | 
| Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8822 | goto exit; | 
|  | 8823 | } | 
|  | 8824 |  | 
|  | 8825 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8826 | mbedtls_md_free(&ctx); | 
| Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8827 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8828 | if (ret != 0) { | 
|  | 8829 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 8830 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); | 
|  | 8831 | } | 
| Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8832 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8833 | return ret; | 
| Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8834 | } | 
|  | 8835 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 8836 |  | 
| Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8837 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) | 
|  | 8838 |  | 
| Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8839 | /* Find the preferred hash for a given signature algorithm. */ | 
|  | 8840 | unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8841 | mbedtls_ssl_context *ssl, | 
|  | 8842 | unsigned int sig_alg) | 
| Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8843 | { | 
| Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8844 | unsigned int i; | 
| Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8845 | uint16_t *received_sig_algs = ssl->handshake->received_sig_algs; | 
| Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8846 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8847 | if (sig_alg == MBEDTLS_SSL_SIG_ANON) { | 
|  | 8848 | return MBEDTLS_SSL_HASH_NONE; | 
|  | 8849 | } | 
| Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8850 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8851 | for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) { | 
| Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8852 | unsigned int hash_alg_received = | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8853 | MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG( | 
|  | 8854 | received_sig_algs[i]); | 
| Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8855 | unsigned int sig_alg_received = | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8856 | MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG( | 
|  | 8857 | received_sig_algs[i]); | 
| Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8858 |  | 
| Manuel Pégourié-Gonnard | 47bb380 | 2023-06-05 12:40:32 +0200 | [diff] [blame] | 8859 | mbedtls_md_type_t md_alg = | 
|  | 8860 | mbedtls_ssl_md_alg_from_hash((unsigned char) hash_alg_received); | 
|  | 8861 | if (md_alg == MBEDTLS_MD_NONE) { | 
|  | 8862 | continue; | 
|  | 8863 | } | 
|  | 8864 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8865 | if (sig_alg == sig_alg_received) { | 
| Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8866 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8867 | if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) { | 
| Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8868 | psa_algorithm_t psa_hash_alg = | 
| Manuel Pégourié-Gonnard | 47bb380 | 2023-06-05 12:40:32 +0200 | [diff] [blame] | 8869 | mbedtls_md_psa_alg_from_type(md_alg); | 
| Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8870 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8871 | if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA && | 
|  | 8872 | !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key, | 
|  | 8873 | PSA_ALG_ECDSA(psa_hash_alg), | 
|  | 8874 | PSA_KEY_USAGE_SIGN_HASH)) { | 
| Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8875 | continue; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8876 | } | 
| Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8877 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8878 | if (sig_alg_received == MBEDTLS_SSL_SIG_RSA && | 
|  | 8879 | !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key, | 
|  | 8880 | PSA_ALG_RSA_PKCS1V15_SIGN( | 
|  | 8881 | psa_hash_alg), | 
|  | 8882 | PSA_KEY_USAGE_SIGN_HASH)) { | 
| Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8883 | continue; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8884 | } | 
| Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8885 | } | 
| Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8886 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8887 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8888 | return hash_alg_received; | 
| Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8889 | } | 
| Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8890 | } | 
| Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8891 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8892 | return MBEDTLS_SSL_HASH_NONE; | 
| Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8893 | } | 
|  | 8894 |  | 
|  | 8895 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ | 
|  | 8896 |  | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8897 | /* Serialization of TLS 1.2 sessions: | 
|  | 8898 | * | 
|  | 8899 | * struct { | 
|  | 8900 | *    uint64 start_time; | 
|  | 8901 | *    uint8 ciphersuite[2];           // defined by the standard | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8902 | *    uint8 session_id_len;           // at most 32 | 
|  | 8903 | *    opaque session_id[32]; | 
|  | 8904 | *    opaque master[48];              // fixed length in the standard | 
|  | 8905 | *    uint32 verify_result; | 
|  | 8906 | *    opaque peer_cert<0..2^24-1>;    // length 0 means no peer cert | 
|  | 8907 | *    opaque ticket<0..2^24-1>;       // length 0 means no ticket | 
|  | 8908 | *    uint32 ticket_lifetime; | 
|  | 8909 | *    uint8 mfl_code;                 // up to 255 according to standard | 
|  | 8910 | *    uint8 encrypt_then_mac;         // 0 or 1 | 
|  | 8911 | * } serialized_session_tls12; | 
|  | 8912 | * | 
|  | 8913 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8914 | static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session, | 
|  | 8915 | unsigned char *buf, | 
|  | 8916 | size_t buf_len) | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8917 | { | 
|  | 8918 | unsigned char *p = buf; | 
|  | 8919 | size_t used = 0; | 
|  | 8920 |  | 
|  | 8921 | #if defined(MBEDTLS_HAVE_TIME) | 
|  | 8922 | uint64_t start; | 
|  | 8923 | #endif | 
|  | 8924 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 8925 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 8926 | size_t cert_len; | 
|  | 8927 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 8928 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
|  | 8929 |  | 
|  | 8930 | /* | 
|  | 8931 | * Time | 
|  | 8932 | */ | 
|  | 8933 | #if defined(MBEDTLS_HAVE_TIME) | 
|  | 8934 | used += 8; | 
|  | 8935 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8936 | if (used <= buf_len) { | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8937 | start = (uint64_t) session->start; | 
|  | 8938 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8939 | MBEDTLS_PUT_UINT64_BE(start, p, 0); | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8940 | p += 8; | 
|  | 8941 | } | 
|  | 8942 | #endif /* MBEDTLS_HAVE_TIME */ | 
|  | 8943 |  | 
|  | 8944 | /* | 
|  | 8945 | * Basic mandatory fields | 
|  | 8946 | */ | 
|  | 8947 | used += 2   /* ciphersuite */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8948 | + 1 /* id_len */ | 
|  | 8949 | + sizeof(session->id) | 
|  | 8950 | + sizeof(session->master) | 
|  | 8951 | + 4; /* verify_result */ | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8952 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8953 | if (used <= buf_len) { | 
|  | 8954 | MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0); | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8955 | p += 2; | 
|  | 8956 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8957 | *p++ = MBEDTLS_BYTE_0(session->id_len); | 
|  | 8958 | memcpy(p, session->id, 32); | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8959 | p += 32; | 
|  | 8960 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8961 | memcpy(p, session->master, 48); | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8962 | p += 48; | 
|  | 8963 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8964 | MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0); | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8965 | p += 4; | 
|  | 8966 | } | 
|  | 8967 |  | 
|  | 8968 | /* | 
|  | 8969 | * Peer's end-entity certificate | 
|  | 8970 | */ | 
|  | 8971 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 8972 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8973 | if (session->peer_cert == NULL) { | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8974 | cert_len = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8975 | } else { | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8976 | cert_len = session->peer_cert->raw.len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8977 | } | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8978 |  | 
|  | 8979 | used += 3 + cert_len; | 
|  | 8980 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8981 | if (used <= buf_len) { | 
|  | 8982 | *p++ = MBEDTLS_BYTE_2(cert_len); | 
|  | 8983 | *p++ = MBEDTLS_BYTE_1(cert_len); | 
|  | 8984 | *p++ = MBEDTLS_BYTE_0(cert_len); | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8985 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8986 | if (session->peer_cert != NULL) { | 
|  | 8987 | memcpy(p, session->peer_cert->raw.p, cert_len); | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8988 | p += cert_len; | 
|  | 8989 | } | 
|  | 8990 | } | 
|  | 8991 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8992 | if (session->peer_cert_digest != NULL) { | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8993 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8994 | if (used <= buf_len) { | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8995 | *p++ = (unsigned char) session->peer_cert_digest_type; | 
|  | 8996 | *p++ = (unsigned char) session->peer_cert_digest_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8997 | memcpy(p, session->peer_cert_digest, | 
|  | 8998 | session->peer_cert_digest_len); | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8999 | p += session->peer_cert_digest_len; | 
|  | 9000 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9001 | } else { | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9002 | used += 2; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9003 | if (used <= buf_len) { | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9004 | *p++ = (unsigned char) MBEDTLS_MD_NONE; | 
|  | 9005 | *p++ = 0; | 
|  | 9006 | } | 
|  | 9007 | } | 
|  | 9008 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 9009 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
|  | 9010 |  | 
|  | 9011 | /* | 
|  | 9012 | * Session ticket if any, plus associated data | 
|  | 9013 | */ | 
|  | 9014 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 9015 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ | 
|  | 9016 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9017 | if (used <= buf_len) { | 
|  | 9018 | *p++ = MBEDTLS_BYTE_2(session->ticket_len); | 
|  | 9019 | *p++ = MBEDTLS_BYTE_1(session->ticket_len); | 
|  | 9020 | *p++ = MBEDTLS_BYTE_0(session->ticket_len); | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9021 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9022 | if (session->ticket != NULL) { | 
|  | 9023 | memcpy(p, session->ticket, session->ticket_len); | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9024 | p += session->ticket_len; | 
|  | 9025 | } | 
|  | 9026 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9027 | MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0); | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9028 | p += 4; | 
|  | 9029 | } | 
|  | 9030 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ | 
|  | 9031 |  | 
|  | 9032 | /* | 
|  | 9033 | * Misc extension-related info | 
|  | 9034 | */ | 
|  | 9035 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
|  | 9036 | used += 1; | 
|  | 9037 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9038 | if (used <= buf_len) { | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9039 | *p++ = session->mfl_code; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9040 | } | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9041 | #endif | 
|  | 9042 |  | 
|  | 9043 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 9044 | used += 1; | 
|  | 9045 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9046 | if (used <= buf_len) { | 
|  | 9047 | *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac); | 
|  | 9048 | } | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9049 | #endif | 
|  | 9050 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9051 | return used; | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9052 | } | 
|  | 9053 |  | 
| Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 9054 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9055 | static int ssl_tls12_session_load(mbedtls_ssl_session *session, | 
|  | 9056 | const unsigned char *buf, | 
|  | 9057 | size_t len) | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9058 | { | 
|  | 9059 | #if defined(MBEDTLS_HAVE_TIME) | 
|  | 9060 | uint64_t start; | 
|  | 9061 | #endif | 
|  | 9062 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 9063 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 9064 | size_t cert_len; | 
|  | 9065 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 9066 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
|  | 9067 |  | 
|  | 9068 | const unsigned char *p = buf; | 
|  | 9069 | const unsigned char * const end = buf + len; | 
|  | 9070 |  | 
|  | 9071 | /* | 
|  | 9072 | * Time | 
|  | 9073 | */ | 
|  | 9074 | #if defined(MBEDTLS_HAVE_TIME) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9075 | if (8 > (size_t) (end - p)) { | 
|  | 9076 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 9077 | } | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9078 |  | 
| Thomas Daubney | f9f0ba8 | 2023-05-23 17:34:33 +0100 | [diff] [blame] | 9079 | start = MBEDTLS_GET_UINT64_BE(p, 0); | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9080 | p += 8; | 
|  | 9081 |  | 
|  | 9082 | session->start = (time_t) start; | 
|  | 9083 | #endif /* MBEDTLS_HAVE_TIME */ | 
|  | 9084 |  | 
|  | 9085 | /* | 
|  | 9086 | * Basic mandatory fields | 
|  | 9087 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9088 | if (2 + 1 + 32 + 48 + 4 > (size_t) (end - p)) { | 
|  | 9089 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 9090 | } | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9091 |  | 
| Dave Rodgman | a3d0f61 | 2023-11-03 23:34:02 +0000 | [diff] [blame] | 9092 | session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 0); | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9093 | p += 2; | 
|  | 9094 |  | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9095 | session->id_len = *p++; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9096 | memcpy(session->id, p, 32); | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9097 | p += 32; | 
|  | 9098 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9099 | memcpy(session->master, p, 48); | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9100 | p += 48; | 
|  | 9101 |  | 
| Thomas Daubney | f9f0ba8 | 2023-05-23 17:34:33 +0100 | [diff] [blame] | 9102 | session->verify_result = MBEDTLS_GET_UINT32_BE(p, 0); | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9103 | p += 4; | 
|  | 9104 |  | 
|  | 9105 | /* Immediately clear invalid pointer values that have been read, in case | 
|  | 9106 | * we exit early before we replaced them with valid ones. */ | 
|  | 9107 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 9108 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 9109 | session->peer_cert = NULL; | 
|  | 9110 | #else | 
|  | 9111 | session->peer_cert_digest = NULL; | 
|  | 9112 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 9113 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
|  | 9114 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 9115 | session->ticket = NULL; | 
|  | 9116 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ | 
|  | 9117 |  | 
|  | 9118 | /* | 
|  | 9119 | * Peer certificate | 
|  | 9120 | */ | 
|  | 9121 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 9122 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 9123 | /* Deserialize CRT from the end of the ticket. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9124 | if (3 > (size_t) (end - p)) { | 
|  | 9125 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 9126 | } | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9127 |  | 
| Dave Rodgman | a3d0f61 | 2023-11-03 23:34:02 +0000 | [diff] [blame] | 9128 | cert_len = MBEDTLS_GET_UINT24_BE(p, 0); | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9129 | p += 3; | 
|  | 9130 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9131 | if (cert_len != 0) { | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9132 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 9133 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9134 | if (cert_len > (size_t) (end - p)) { | 
|  | 9135 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 9136 | } | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9137 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9138 | session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt)); | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9139 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9140 | if (session->peer_cert == NULL) { | 
|  | 9141 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
|  | 9142 | } | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9143 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9144 | mbedtls_x509_crt_init(session->peer_cert); | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9145 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9146 | if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert, | 
|  | 9147 | p, cert_len)) != 0) { | 
|  | 9148 | mbedtls_x509_crt_free(session->peer_cert); | 
|  | 9149 | mbedtls_free(session->peer_cert); | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9150 | session->peer_cert = NULL; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9151 | return ret; | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9152 | } | 
|  | 9153 |  | 
|  | 9154 | p += cert_len; | 
|  | 9155 | } | 
|  | 9156 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 9157 | /* Deserialize CRT digest from the end of the ticket. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9158 | if (2 > (size_t) (end - p)) { | 
|  | 9159 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 9160 | } | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9161 |  | 
|  | 9162 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; | 
|  | 9163 | session->peer_cert_digest_len  = (size_t) *p++; | 
|  | 9164 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9165 | if (session->peer_cert_digest_len != 0) { | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9166 | const mbedtls_md_info_t *md_info = | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9167 | mbedtls_md_info_from_type(session->peer_cert_digest_type); | 
|  | 9168 | if (md_info == NULL) { | 
|  | 9169 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 9170 | } | 
|  | 9171 | if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) { | 
|  | 9172 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 9173 | } | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9174 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9175 | if (session->peer_cert_digest_len > (size_t) (end - p)) { | 
|  | 9176 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 9177 | } | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9178 |  | 
|  | 9179 | session->peer_cert_digest = | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9180 | mbedtls_calloc(1, session->peer_cert_digest_len); | 
|  | 9181 | if (session->peer_cert_digest == NULL) { | 
|  | 9182 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
|  | 9183 | } | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9184 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9185 | memcpy(session->peer_cert_digest, p, | 
|  | 9186 | session->peer_cert_digest_len); | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9187 | p += session->peer_cert_digest_len; | 
|  | 9188 | } | 
|  | 9189 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 9190 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
|  | 9191 |  | 
|  | 9192 | /* | 
|  | 9193 | * Session ticket and associated data | 
|  | 9194 | */ | 
|  | 9195 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9196 | if (3 > (size_t) (end - p)) { | 
|  | 9197 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 9198 | } | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9199 |  | 
| Dave Rodgman | a3d0f61 | 2023-11-03 23:34:02 +0000 | [diff] [blame] | 9200 | session->ticket_len = MBEDTLS_GET_UINT24_BE(p, 0); | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9201 | p += 3; | 
|  | 9202 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9203 | if (session->ticket_len != 0) { | 
|  | 9204 | if (session->ticket_len > (size_t) (end - p)) { | 
|  | 9205 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 9206 | } | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9207 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9208 | session->ticket = mbedtls_calloc(1, session->ticket_len); | 
|  | 9209 | if (session->ticket == NULL) { | 
|  | 9210 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
|  | 9211 | } | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9212 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9213 | memcpy(session->ticket, p, session->ticket_len); | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9214 | p += session->ticket_len; | 
|  | 9215 | } | 
|  | 9216 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9217 | if (4 > (size_t) (end - p)) { | 
|  | 9218 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 9219 | } | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9220 |  | 
| Thomas Daubney | f9f0ba8 | 2023-05-23 17:34:33 +0100 | [diff] [blame] | 9221 | session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0); | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9222 | p += 4; | 
|  | 9223 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ | 
|  | 9224 |  | 
|  | 9225 | /* | 
|  | 9226 | * Misc extension-related info | 
|  | 9227 | */ | 
|  | 9228 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9229 | if (1 > (size_t) (end - p)) { | 
|  | 9230 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 9231 | } | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9232 |  | 
|  | 9233 | session->mfl_code = *p++; | 
|  | 9234 | #endif | 
|  | 9235 |  | 
|  | 9236 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9237 | if (1 > (size_t) (end - p)) { | 
|  | 9238 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 9239 | } | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9240 |  | 
|  | 9241 | session->encrypt_then_mac = *p++; | 
|  | 9242 | #endif | 
|  | 9243 |  | 
|  | 9244 | /* Done, should have consumed entire buffer */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9245 | if (p != end) { | 
|  | 9246 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 9247 | } | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9248 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9249 | return 0; | 
| Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9250 | } | 
| Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 9251 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 9252 |  | 
| XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9253 | int mbedtls_ssl_validate_ciphersuite( | 
|  | 9254 | const mbedtls_ssl_context *ssl, | 
|  | 9255 | const mbedtls_ssl_ciphersuite_t *suite_info, | 
|  | 9256 | mbedtls_ssl_protocol_version min_tls_version, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9257 | mbedtls_ssl_protocol_version max_tls_version) | 
| XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9258 | { | 
|  | 9259 | (void) ssl; | 
|  | 9260 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9261 | if (suite_info == NULL) { | 
|  | 9262 | return -1; | 
|  | 9263 | } | 
| XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9264 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9265 | if ((suite_info->min_tls_version > max_tls_version) || | 
|  | 9266 | (suite_info->max_tls_version < min_tls_version)) { | 
|  | 9267 | return -1; | 
| XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9268 | } | 
|  | 9269 |  | 
| XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 9270 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C) | 
| XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9271 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 9272 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9273 | if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && | 
|  | 9274 | ssl->handshake->psa_pake_ctx_is_ok != 1) | 
| Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 9275 | #else | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9276 | if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && | 
|  | 9277 | mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0) | 
| Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 9278 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9279 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9280 | return -1; | 
| XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9281 | } | 
|  | 9282 | #endif | 
|  | 9283 |  | 
|  | 9284 | /* Don't suggest PSK-based ciphersuite if no PSK is available. */ | 
|  | 9285 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9286 | if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) && | 
|  | 9287 | mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) { | 
|  | 9288 | return -1; | 
| XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9289 | } | 
|  | 9290 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ | 
|  | 9291 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 9292 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9293 | return 0; | 
| XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9294 | } | 
|  | 9295 |  | 
| Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 9296 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) | 
| XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9297 | /* | 
|  | 9298 | * Function for writing a signature algorithm extension. | 
|  | 9299 | * | 
|  | 9300 | * The `extension_data` field of signature algorithm contains  a `SignatureSchemeList` | 
|  | 9301 | * value (TLS 1.3 RFC8446): | 
|  | 9302 | *      enum { | 
|  | 9303 | *         .... | 
|  | 9304 | *        ecdsa_secp256r1_sha256( 0x0403 ), | 
|  | 9305 | *        ecdsa_secp384r1_sha384( 0x0503 ), | 
|  | 9306 | *        ecdsa_secp521r1_sha512( 0x0603 ), | 
|  | 9307 | *         .... | 
|  | 9308 | *      } SignatureScheme; | 
|  | 9309 | * | 
|  | 9310 | *      struct { | 
|  | 9311 | *         SignatureScheme supported_signature_algorithms<2..2^16-2>; | 
|  | 9312 | *      } SignatureSchemeList; | 
|  | 9313 | * | 
|  | 9314 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` | 
|  | 9315 | * value (TLS 1.2 RFC5246): | 
|  | 9316 | *      enum { | 
|  | 9317 | *          none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), | 
|  | 9318 | *          sha512(6), (255) | 
|  | 9319 | *      } HashAlgorithm; | 
|  | 9320 | * | 
|  | 9321 | *      enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } | 
|  | 9322 | *        SignatureAlgorithm; | 
|  | 9323 | * | 
|  | 9324 | *      struct { | 
|  | 9325 | *          HashAlgorithm hash; | 
|  | 9326 | *          SignatureAlgorithm signature; | 
|  | 9327 | *      } SignatureAndHashAlgorithm; | 
|  | 9328 | * | 
|  | 9329 | *      SignatureAndHashAlgorithm | 
|  | 9330 | *        supported_signature_algorithms<2..2^16-2>; | 
|  | 9331 | * | 
|  | 9332 | * The TLS 1.3 signature algorithm extension was defined to be a compatible | 
|  | 9333 | * generalization of the TLS 1.2 signature algorithm extension. | 
|  | 9334 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by | 
|  | 9335 | * `SignatureScheme` field of TLS 1.3 | 
|  | 9336 | * | 
|  | 9337 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9338 | int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf, | 
|  | 9339 | const unsigned char *end, size_t *out_len) | 
| XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9340 | { | 
|  | 9341 | unsigned char *p = buf; | 
|  | 9342 | unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */ | 
|  | 9343 | size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */ | 
|  | 9344 |  | 
|  | 9345 | *out_len = 0; | 
|  | 9346 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9347 | MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension")); | 
| XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9348 |  | 
|  | 9349 | /* Check if we have space for header and length field: | 
|  | 9350 | * - extension_type         (2 bytes) | 
|  | 9351 | * - extension_data_length  (2 bytes) | 
|  | 9352 | * - supported_signature_algorithms_length   (2 bytes) | 
|  | 9353 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9354 | MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6); | 
| XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9355 | p += 6; | 
|  | 9356 |  | 
|  | 9357 | /* | 
|  | 9358 | * Write supported_signature_algorithms | 
|  | 9359 | */ | 
|  | 9360 | supported_sig_alg = p; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9361 | const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl); | 
|  | 9362 | if (sig_alg == NULL) { | 
|  | 9363 | return MBEDTLS_ERR_SSL_BAD_CONFIG; | 
|  | 9364 | } | 
| XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9365 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9366 | for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) { | 
|  | 9367 | MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s", | 
|  | 9368 | *sig_alg, | 
|  | 9369 | mbedtls_ssl_sig_alg_to_str(*sig_alg))); | 
|  | 9370 | if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) { | 
| XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9371 | continue; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9372 | } | 
|  | 9373 | MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2); | 
|  | 9374 | MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0); | 
| XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9375 | p += 2; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9376 | MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s", | 
|  | 9377 | *sig_alg, | 
|  | 9378 | mbedtls_ssl_sig_alg_to_str(*sig_alg))); | 
| XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9379 | } | 
|  | 9380 |  | 
|  | 9381 | /* Length of supported_signature_algorithms */ | 
| Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 9382 | supported_sig_alg_len = (size_t) (p - supported_sig_alg); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9383 | if (supported_sig_alg_len == 0) { | 
|  | 9384 | MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined.")); | 
|  | 9385 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 
| XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9386 | } | 
|  | 9387 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9388 | MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0); | 
|  | 9389 | MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2); | 
|  | 9390 | MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4); | 
| XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9391 |  | 
| Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 9392 | *out_len = (size_t) (p - buf); | 
| XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9393 |  | 
|  | 9394 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9395 | mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG); | 
| XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9396 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
| Jerry Yu | 0c354a2 | 2022-08-29 15:25:36 +0800 | [diff] [blame] | 9397 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9398 | return 0; | 
| XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9399 | } | 
| Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 9400 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ | 
| XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9401 |  | 
| XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9402 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9403 | /* | 
|  | 9404 | * mbedtls_ssl_parse_server_name_ext | 
|  | 9405 | * | 
|  | 9406 | * Structure of server_name extension: | 
|  | 9407 | * | 
|  | 9408 | *  enum { | 
|  | 9409 | *        host_name(0), (255) | 
|  | 9410 | *     } NameType; | 
|  | 9411 | *  opaque HostName<1..2^16-1>; | 
|  | 9412 | * | 
|  | 9413 | *  struct { | 
|  | 9414 | *          NameType name_type; | 
|  | 9415 | *          select (name_type) { | 
|  | 9416 | *             case host_name: HostName; | 
|  | 9417 | *           } name; | 
|  | 9418 | *     } ServerName; | 
|  | 9419 | *  struct { | 
|  | 9420 | *          ServerName server_name_list<1..2^16-1> | 
|  | 9421 | *     } ServerNameList; | 
|  | 9422 | */ | 
| Ronald Cron | ce7d76e | 2022-07-08 18:56:49 +0200 | [diff] [blame] | 9423 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9424 | int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl, | 
|  | 9425 | const unsigned char *buf, | 
|  | 9426 | const unsigned char *end) | 
| XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9427 | { | 
|  | 9428 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 9429 | const unsigned char *p = buf; | 
| XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9430 | size_t server_name_list_len, hostname_len; | 
|  | 9431 | const unsigned char *server_name_list_end; | 
| XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9432 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9433 | MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension")); | 
| XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9434 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9435 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2); | 
|  | 9436 | server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0); | 
| XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9437 | p += 2; | 
|  | 9438 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9439 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len); | 
| XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9440 | server_name_list_end = p + server_name_list_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9441 | while (p < server_name_list_end) { | 
|  | 9442 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3); | 
|  | 9443 | hostname_len = MBEDTLS_GET_UINT16_BE(p, 1); | 
|  | 9444 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, | 
|  | 9445 | hostname_len + 3); | 
| XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9446 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9447 | if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) { | 
| XiaokangQian | 75fe8c7 | 2022-06-15 09:42:45 +0000 | [diff] [blame] | 9448 | /* sni_name is intended to be used only during the parsing of the | 
|  | 9449 | * ClientHello message (it is reset to NULL before the end of | 
|  | 9450 | * the message parsing). Thus it is ok to just point to the | 
|  | 9451 | * reception buffer and not make a copy of it. | 
|  | 9452 | */ | 
| XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 9453 | ssl->handshake->sni_name = p + 3; | 
|  | 9454 | ssl->handshake->sni_name_len = hostname_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9455 | if (ssl->conf->f_sni == NULL) { | 
|  | 9456 | return 0; | 
| XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9457 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9458 | ret = ssl->conf->f_sni(ssl->conf->p_sni, | 
|  | 9459 | ssl, p + 3, hostname_len); | 
|  | 9460 | if (ret != 0) { | 
|  | 9461 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret); | 
|  | 9462 | MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME, | 
|  | 9463 | MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME); | 
|  | 9464 | return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME; | 
|  | 9465 | } | 
|  | 9466 | return 0; | 
| XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9467 | } | 
|  | 9468 |  | 
|  | 9469 | p += hostname_len + 3; | 
|  | 9470 | } | 
|  | 9471 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9472 | return 0; | 
| XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9473 | } | 
|  | 9474 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ | 
|  | 9475 |  | 
| XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9476 | #if defined(MBEDTLS_SSL_ALPN) | 
| Ronald Cron | ce7d76e | 2022-07-08 18:56:49 +0200 | [diff] [blame] | 9477 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9478 | int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl, | 
|  | 9479 | const unsigned char *buf, | 
|  | 9480 | const unsigned char *end) | 
| XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9481 | { | 
|  | 9482 | const unsigned char *p = buf; | 
| XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9483 | size_t protocol_name_list_len; | 
| XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9484 | const unsigned char *protocol_name_list; | 
|  | 9485 | const unsigned char *protocol_name_list_end; | 
| XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9486 | size_t protocol_name_len; | 
| XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9487 |  | 
|  | 9488 | /* If ALPN not configured, just ignore the extension */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9489 | if (ssl->conf->alpn_list == NULL) { | 
|  | 9490 | return 0; | 
|  | 9491 | } | 
| XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9492 |  | 
|  | 9493 | /* | 
| XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9494 | * RFC7301, section 3.1 | 
|  | 9495 | *      opaque ProtocolName<1..2^8-1>; | 
| XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9496 | * | 
| XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9497 | *      struct { | 
|  | 9498 | *          ProtocolName protocol_name_list<2..2^16-1> | 
|  | 9499 | *      } ProtocolNameList; | 
| XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9500 | */ | 
|  | 9501 |  | 
| XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9502 | /* | 
| XiaokangQian | 0b776e2 | 2022-06-24 09:04:59 +0000 | [diff] [blame] | 9503 | * protocol_name_list_len    2 bytes | 
|  | 9504 | * protocol_name_len         1 bytes | 
|  | 9505 | * protocol_name             >=1 byte | 
| XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9506 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9507 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4); | 
| XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9508 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9509 | protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0); | 
| XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9510 | p += 2; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9511 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len); | 
| XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9512 | protocol_name_list = p; | 
|  | 9513 | protocol_name_list_end = p + protocol_name_list_len; | 
| XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9514 |  | 
|  | 9515 | /* Validate peer's list (lengths) */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9516 | while (p < protocol_name_list_end) { | 
| XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9517 | protocol_name_len = *p++; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9518 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end, | 
|  | 9519 | protocol_name_len); | 
|  | 9520 | if (protocol_name_len == 0) { | 
| XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9521 | MBEDTLS_SSL_PEND_FATAL_ALERT( | 
|  | 9522 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9523 | MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER); | 
|  | 9524 | return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER; | 
| XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9525 | } | 
|  | 9526 |  | 
|  | 9527 | p += protocol_name_len; | 
| XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9528 | } | 
|  | 9529 |  | 
|  | 9530 | /* Use our order of preference */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9531 | for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) { | 
|  | 9532 | size_t const alpn_len = strlen(*alpn); | 
| XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9533 | p = protocol_name_list; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9534 | while (p < protocol_name_list_end) { | 
| XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9535 | protocol_name_len = *p++; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9536 | if (protocol_name_len == alpn_len && | 
|  | 9537 | memcmp(p, *alpn, alpn_len) == 0) { | 
| XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9538 | ssl->alpn_chosen = *alpn; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9539 | return 0; | 
| XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9540 | } | 
| XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9541 |  | 
|  | 9542 | p += protocol_name_len; | 
| XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9543 | } | 
|  | 9544 | } | 
|  | 9545 |  | 
| XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9546 | /* If we get here, no match was found */ | 
| XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9547 | MBEDTLS_SSL_PEND_FATAL_ALERT( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9548 | MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL, | 
|  | 9549 | MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL); | 
|  | 9550 | return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL; | 
| XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9551 | } | 
|  | 9552 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9553 | int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl, | 
|  | 9554 | unsigned char *buf, | 
|  | 9555 | unsigned char *end, | 
|  | 9556 | size_t *out_len) | 
| XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9557 | { | 
|  | 9558 | unsigned char *p = buf; | 
| XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9559 | size_t protocol_name_len; | 
| XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9560 | *out_len = 0; | 
| XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9561 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9562 | if (ssl->alpn_chosen == NULL) { | 
|  | 9563 | return 0; | 
| XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9564 | } | 
|  | 9565 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9566 | protocol_name_len = strlen(ssl->alpn_chosen); | 
|  | 9567 | MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len); | 
| XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9568 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9569 | MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension")); | 
| XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9570 | /* | 
|  | 9571 | * 0 . 1    ext identifier | 
|  | 9572 | * 2 . 3    ext length | 
|  | 9573 | * 4 . 5    protocol list length | 
|  | 9574 | * 6 . 6    protocol name length | 
|  | 9575 | * 7 . 7+n  protocol name | 
|  | 9576 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9577 | MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0); | 
| XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9578 |  | 
| XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9579 | *out_len = 7 + protocol_name_len; | 
| XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9580 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9581 | MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2); | 
|  | 9582 | MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4); | 
| XiaokangQian | 0b776e2 | 2022-06-24 09:04:59 +0000 | [diff] [blame] | 9583 | /* Note: the length of the chosen protocol has been checked to be less | 
|  | 9584 | * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`. | 
|  | 9585 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9586 | p[6] = MBEDTLS_BYTE_0(protocol_name_len); | 
| XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9587 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9588 | memcpy(p + 7, ssl->alpn_chosen, protocol_name_len); | 
| Jerry Yu | b95dd36 | 2022-11-08 21:19:34 +0800 | [diff] [blame] | 9589 |  | 
|  | 9590 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9591 | mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN); | 
| Jerry Yu | b95dd36 | 2022-11-08 21:19:34 +0800 | [diff] [blame] | 9592 | #endif | 
|  | 9593 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9594 | return 0; | 
| XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9595 | } | 
|  | 9596 | #endif /* MBEDTLS_SSL_ALPN */ | 
|  | 9597 |  | 
| Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9598 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ | 
| Xiaokang Qian | 0340929 | 2022-10-12 02:49:52 +0000 | [diff] [blame] | 9599 | defined(MBEDTLS_SSL_SESSION_TICKETS) && \ | 
| Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 9600 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \ | 
| Xiaokang Qian | ed3afcd | 2022-10-12 08:31:11 +0000 | [diff] [blame] | 9601 | defined(MBEDTLS_SSL_CLI_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9602 | int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session, | 
|  | 9603 | const char *hostname) | 
| Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9604 | { | 
|  | 9605 | /* Initialize to suppress unnecessary compiler warning */ | 
|  | 9606 | size_t hostname_len = 0; | 
|  | 9607 |  | 
|  | 9608 | /* Check if new hostname is valid before | 
|  | 9609 | * making any change to current one */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9610 | if (hostname != NULL) { | 
|  | 9611 | hostname_len = strlen(hostname); | 
| Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9612 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9613 | if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) { | 
|  | 9614 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | 
|  | 9615 | } | 
| Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9616 | } | 
|  | 9617 |  | 
|  | 9618 | /* Now it's clear that we will overwrite the old hostname, | 
|  | 9619 | * so we can free it safely */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9620 | if (session->hostname != NULL) { | 
| Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 9621 | mbedtls_zeroize_and_free(session->hostname, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9622 | strlen(session->hostname)); | 
| Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9623 | } | 
|  | 9624 |  | 
|  | 9625 | /* Passing NULL as hostname shall clear the old one */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9626 | if (hostname == NULL) { | 
| Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9627 | session->hostname = NULL; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9628 | } else { | 
|  | 9629 | session->hostname = mbedtls_calloc(1, hostname_len + 1); | 
|  | 9630 | if (session->hostname == NULL) { | 
|  | 9631 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
|  | 9632 | } | 
| Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9633 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9634 | memcpy(session->hostname, hostname, hostname_len); | 
| Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9635 | } | 
|  | 9636 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9637 | return 0; | 
| Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9638 | } | 
| Xiaokang Qian | 0340929 | 2022-10-12 02:49:52 +0000 | [diff] [blame] | 9639 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 && | 
|  | 9640 | MBEDTLS_SSL_SESSION_TICKETS && | 
| Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 9641 | MBEDTLS_SSL_SERVER_NAME_INDICATION && | 
| Xiaokang Qian | ed3afcd | 2022-10-12 08:31:11 +0000 | [diff] [blame] | 9642 | MBEDTLS_SSL_CLI_C */ | 
| Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9643 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9644 | #endif /* MBEDTLS_SSL_TLS_C */ |