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" |
Max Fillinger | 44042f0 | 2024-07-22 14:43:56 +0200 | [diff] [blame] | 22 | #include "ssl_tls13_keys.h" |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 23 | |
Valerio Setti | b4f5076 | 2024-01-17 10:24:52 +0100 | [diff] [blame] | 24 | #include "debug_internal.h" |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 25 | #include "mbedtls/error.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 26 | #include "mbedtls/platform_util.h" |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 27 | #include "mbedtls/version.h" |
Gabor Mezei | 765862c | 2021-10-19 12:22:25 +0200 | [diff] [blame] | 28 | #include "mbedtls/constant_time.h" |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 29 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 30 | #include <string.h> |
| 31 | |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 32 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Valerio Setti | 384fbde | 2024-01-02 13:26:40 +0100 | [diff] [blame] | 33 | #include "mbedtls/psa_util.h" |
Manuel Pégourié-Gonnard | 02b10d8 | 2023-03-28 12:33:20 +0200 | [diff] [blame] | 34 | #include "md_psa.h" |
Manuel Pégourié-Gonnard | 2be8c63 | 2023-06-07 13:07:21 +0200 | [diff] [blame] | 35 | #include "psa_util_internal.h" |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 36 | #include "psa/crypto.h" |
| 37 | #endif |
| 38 | |
Janos Follath | 23bdca0 | 2016-10-07 14:47:14 +0100 | [diff] [blame] | 39 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 40 | #include "mbedtls/oid.h" |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 41 | #endif |
| 42 | |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 43 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 0064484 | 2023-05-30 05:45:00 -0400 | [diff] [blame] | 44 | /* Define local translating functions to save code size by not using too many |
| 45 | * arguments in each translating place. */ |
| 46 | static int local_err_translation(psa_status_t status) |
| 47 | { |
| 48 | return psa_status_to_mbedtls(status, psa_to_ssl_errors, |
Andrzej Kurek | 1e4a030 | 2023-05-30 09:45:17 -0400 | [diff] [blame] | 49 | ARRAY_LENGTH(psa_to_ssl_errors), |
Andrzej Kurek | 0064484 | 2023-05-30 05:45:00 -0400 | [diff] [blame] | 50 | psa_generic_status_to_mbedtls); |
| 51 | } |
| 52 | #define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status) |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 53 | #endif |
| 54 | |
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 55 | #if defined(MBEDTLS_TEST_HOOKS) |
| 56 | static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args; |
| 57 | |
| 58 | void mbedtls_ssl_set_chk_buf_ptr_fail_args( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 59 | const uint8_t *cur, const uint8_t *end, size_t need) |
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 60 | { |
| 61 | chk_buf_ptr_fail_args.cur = cur; |
| 62 | chk_buf_ptr_fail_args.end = end; |
| 63 | chk_buf_ptr_fail_args.need = need; |
| 64 | } |
| 65 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 66 | void mbedtls_ssl_reset_chk_buf_ptr_fail_args(void) |
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 67 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 68 | 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] | 69 | } |
| 70 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 71 | 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] | 72 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 73 | return (chk_buf_ptr_fail_args.cur != args->cur) || |
| 74 | (chk_buf_ptr_fail_args.end != args->end) || |
| 75 | (chk_buf_ptr_fail_args.need != args->need); |
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 76 | } |
| 77 | #endif /* MBEDTLS_TEST_HOOKS */ |
| 78 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 79 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 80 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 81 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 82 | /* Top-level Connection ID API */ |
| 83 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 84 | int mbedtls_ssl_conf_cid(mbedtls_ssl_config *conf, |
| 85 | size_t len, |
| 86 | int ignore_other_cid) |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 87 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 88 | if (len > MBEDTLS_SSL_CID_IN_LEN_MAX) { |
| 89 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 90 | } |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 91 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 92 | if (ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL && |
| 93 | ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) { |
| 94 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | 611ac77 | 2019-05-14 11:45:26 +0100 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | conf->ignore_unexpected_cid = ignore_other_cid; |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 98 | conf->cid_len = len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 99 | return 0; |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 100 | } |
| 101 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 102 | int mbedtls_ssl_set_cid(mbedtls_ssl_context *ssl, |
| 103 | int enable, |
| 104 | unsigned char const *own_cid, |
| 105 | size_t own_cid_len) |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 106 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 107 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 108 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 109 | } |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 110 | |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 111 | ssl->negotiate_cid = enable; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 112 | if (enable == MBEDTLS_SSL_CID_DISABLED) { |
| 113 | MBEDTLS_SSL_DEBUG_MSG(3, ("Disable use of CID extension.")); |
| 114 | return 0; |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 115 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 116 | MBEDTLS_SSL_DEBUG_MSG(3, ("Enable use of CID extension.")); |
| 117 | MBEDTLS_SSL_DEBUG_BUF(3, "Own CID", own_cid, own_cid_len); |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 118 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 119 | if (own_cid_len != ssl->conf->cid_len) { |
| 120 | MBEDTLS_SSL_DEBUG_MSG(3, ("CID length %u does not match CID length %u in config", |
| 121 | (unsigned) own_cid_len, |
| 122 | (unsigned) ssl->conf->cid_len)); |
| 123 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 124 | } |
| 125 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 126 | memcpy(ssl->own_cid, own_cid, own_cid_len); |
Hanno Becker | b7ee0cf | 2019-04-30 14:07:31 +0100 | [diff] [blame] | 127 | /* Truncation is not an issue here because |
| 128 | * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */ |
| 129 | ssl->own_cid_len = (uint8_t) own_cid_len; |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 130 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 131 | return 0; |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 132 | } |
| 133 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 134 | int mbedtls_ssl_get_own_cid(mbedtls_ssl_context *ssl, |
| 135 | int *enabled, |
Sam Berry | 9722fd1 | 2024-06-11 14:34:17 +0100 | [diff] [blame] | 136 | unsigned char own_cid[MBEDTLS_SSL_CID_IN_LEN_MAX], |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 137 | size_t *own_cid_len) |
Paul Elliott | 0113cf1 | 2022-03-11 20:26:47 +0000 | [diff] [blame] | 138 | { |
| 139 | *enabled = MBEDTLS_SSL_CID_DISABLED; |
| 140 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 141 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 142 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 143 | } |
Paul Elliott | 0113cf1 | 2022-03-11 20:26:47 +0000 | [diff] [blame] | 144 | |
| 145 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is |
| 146 | * zero as this is indistinguishable from not requesting to use |
| 147 | * the CID extension. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 148 | if (ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) { |
| 149 | return 0; |
| 150 | } |
Paul Elliott | 0113cf1 | 2022-03-11 20:26:47 +0000 | [diff] [blame] | 151 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 152 | if (own_cid_len != NULL) { |
Paul Elliott | 0113cf1 | 2022-03-11 20:26:47 +0000 | [diff] [blame] | 153 | *own_cid_len = ssl->own_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 154 | if (own_cid != NULL) { |
| 155 | memcpy(own_cid, ssl->own_cid, ssl->own_cid_len); |
| 156 | } |
Paul Elliott | 0113cf1 | 2022-03-11 20:26:47 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | *enabled = MBEDTLS_SSL_CID_ENABLED; |
| 160 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 161 | return 0; |
Paul Elliott | 0113cf1 | 2022-03-11 20:26:47 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 164 | int mbedtls_ssl_get_peer_cid(mbedtls_ssl_context *ssl, |
| 165 | int *enabled, |
| 166 | unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX], |
| 167 | size_t *peer_cid_len) |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 168 | { |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 169 | *enabled = MBEDTLS_SSL_CID_DISABLED; |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 170 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 171 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
| 172 | mbedtls_ssl_is_handshake_over(ssl) == 0) { |
| 173 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 174 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 175 | |
Hanno Becker | c5f2422 | 2019-05-03 12:54:52 +0100 | [diff] [blame] | 176 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions |
| 177 | * were used, but client and server requested the empty CID. |
| 178 | * This is indistinguishable from not using the CID extension |
| 179 | * in the first place. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 180 | if (ssl->transform_in->in_cid_len == 0 && |
| 181 | ssl->transform_in->out_cid_len == 0) { |
| 182 | return 0; |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 183 | } |
| 184 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 185 | if (peer_cid_len != NULL) { |
Hanno Becker | 615ef17 | 2019-05-22 16:50:35 +0100 | [diff] [blame] | 186 | *peer_cid_len = ssl->transform_in->out_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 187 | if (peer_cid != NULL) { |
| 188 | memcpy(peer_cid, ssl->transform_in->out_cid, |
| 189 | ssl->transform_in->out_cid_len); |
Hanno Becker | 615ef17 | 2019-05-22 16:50:35 +0100 | [diff] [blame] | 190 | } |
| 191 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 192 | |
| 193 | *enabled = MBEDTLS_SSL_CID_ENABLED; |
| 194 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 195 | return 0; |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 196 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 197 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 198 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 199 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 200 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 201 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 202 | /* |
| 203 | * Convert max_fragment_length codes to length. |
| 204 | * RFC 6066 says: |
| 205 | * enum{ |
| 206 | * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255) |
| 207 | * } MaxFragmentLength; |
| 208 | * and we add 0 -> extension unused |
| 209 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 210 | static unsigned int ssl_mfl_code_to_length(int mfl) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 211 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 212 | switch (mfl) { |
| 213 | case MBEDTLS_SSL_MAX_FRAG_LEN_NONE: |
| 214 | return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN; |
| 215 | case MBEDTLS_SSL_MAX_FRAG_LEN_512: |
| 216 | return 512; |
| 217 | case MBEDTLS_SSL_MAX_FRAG_LEN_1024: |
| 218 | return 1024; |
| 219 | case MBEDTLS_SSL_MAX_FRAG_LEN_2048: |
| 220 | return 2048; |
| 221 | case MBEDTLS_SSL_MAX_FRAG_LEN_4096: |
| 222 | return 4096; |
| 223 | default: |
| 224 | return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 225 | } |
| 226 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 227 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 228 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 229 | int mbedtls_ssl_session_copy(mbedtls_ssl_session *dst, |
| 230 | const mbedtls_ssl_session *src) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 231 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 232 | mbedtls_ssl_session_free(dst); |
| 233 | memcpy(dst, src, sizeof(mbedtls_ssl_session)); |
吴敬辉 | 0b71611 | 2021-11-29 10:46:35 +0800 | [diff] [blame] | 234 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 235 | dst->ticket = NULL; |
Xiaokang Qian | 8730644 | 2022-10-12 09:47:38 +0000 | [diff] [blame] | 236 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 237 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 238 | dst->hostname = NULL; |
吴敬辉 | 0b71611 | 2021-11-29 10:46:35 +0800 | [diff] [blame] | 239 | #endif |
Xiaokang Qian | 8730644 | 2022-10-12 09:47:38 +0000 | [diff] [blame] | 240 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
吴敬辉 | 0b71611 | 2021-11-29 10:46:35 +0800 | [diff] [blame] | 241 | |
Waleed Elmelegy | 4dfb0e7 | 2024-03-14 01:48:40 +0000 | [diff] [blame] | 242 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_ALPN) && \ |
| 243 | defined(MBEDTLS_SSL_EARLY_DATA) |
| 244 | dst->ticket_alpn = NULL; |
| 245 | #endif |
| 246 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 247 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 248 | |
| 249 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 250 | if (src->peer_cert != NULL) { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 251 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 252 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 253 | dst->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt)); |
| 254 | if (dst->peer_cert == NULL) { |
| 255 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 256 | } |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 257 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 258 | mbedtls_x509_crt_init(dst->peer_cert); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 259 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 260 | if ((ret = mbedtls_x509_crt_parse_der(dst->peer_cert, src->peer_cert->raw.p, |
| 261 | src->peer_cert->raw.len)) != 0) { |
| 262 | mbedtls_free(dst->peer_cert); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 263 | dst->peer_cert = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 264 | return ret; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 265 | } |
| 266 | } |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 267 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 268 | if (src->peer_cert_digest != NULL) { |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 269 | dst->peer_cert_digest = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 270 | mbedtls_calloc(1, src->peer_cert_digest_len); |
| 271 | if (dst->peer_cert_digest == NULL) { |
| 272 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 273 | } |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 274 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 275 | memcpy(dst->peer_cert_digest, src->peer_cert_digest, |
| 276 | src->peer_cert_digest_len); |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 277 | dst->peer_cert_digest_type = src->peer_cert_digest_type; |
Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 278 | dst->peer_cert_digest_len = src->peer_cert_digest_len; |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 279 | } |
| 280 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 281 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 282 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 283 | |
Waleed Elmelegy | 4dfb0e7 | 2024-03-14 01:48:40 +0000 | [diff] [blame] | 284 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_ALPN) && \ |
| 285 | defined(MBEDTLS_SSL_EARLY_DATA) |
| 286 | { |
| 287 | int ret = mbedtls_ssl_session_set_ticket_alpn(dst, src->ticket_alpn); |
| 288 | if (ret != 0) { |
| 289 | return ret; |
| 290 | } |
| 291 | } |
| 292 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_ALPN && MBEDTLS_SSL_EARLY_DATA */ |
| 293 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 294 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 295 | if (src->ticket != NULL) { |
| 296 | dst->ticket = mbedtls_calloc(1, src->ticket_len); |
| 297 | if (dst->ticket == NULL) { |
| 298 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 299 | } |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 300 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 301 | memcpy(dst->ticket, src->ticket, src->ticket_len); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 302 | } |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 303 | |
| 304 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 305 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 306 | if (src->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 307 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 308 | ret = mbedtls_ssl_session_set_hostname(dst, src->hostname); |
| 309 | if (ret != 0) { |
| 310 | return ret; |
| 311 | } |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 312 | } |
| 313 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 && |
| 314 | MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 315 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 316 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 317 | return 0; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 318 | } |
| 319 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 320 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 321 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 322 | 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] | 323 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 324 | unsigned char *resized_buffer = mbedtls_calloc(1, len_new); |
| 325 | if (resized_buffer == NULL) { |
Yanray Wang | 365ee3e | 2023-11-22 10:28:28 +0800 | [diff] [blame] | 326 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 327 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 328 | |
| 329 | /* We want to copy len_new bytes when downsizing the buffer, and |
| 330 | * len_old bytes when upsizing, so we choose the smaller of two sizes, |
| 331 | * to fit one buffer into another. Size checks, ensuring that no data is |
| 332 | * lost, are done outside of this function. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 333 | memcpy(resized_buffer, *buffer, |
| 334 | (len_new < *len_old) ? len_new : *len_old); |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 335 | mbedtls_zeroize_and_free(*buffer, *len_old); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 336 | |
| 337 | *buffer = resized_buffer; |
| 338 | *len_old = len_new; |
| 339 | |
| 340 | return 0; |
| 341 | } |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 342 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 343 | static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing, |
| 344 | size_t in_buf_new_len, |
| 345 | size_t out_buf_new_len) |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 346 | { |
| 347 | int modified = 0; |
Deomid rojer Ryabkov | 3fc5a4d | 2024-03-10 02:11:03 +0000 | [diff] [blame] | 348 | size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0, hdr_in = 0; |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 349 | 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] | 350 | if (ssl->in_buf != NULL) { |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 351 | written_in = ssl->in_msg - ssl->in_buf; |
| 352 | iv_offset_in = ssl->in_iv - ssl->in_buf; |
| 353 | len_offset_in = ssl->in_len - ssl->in_buf; |
Deomid rojer Ryabkov | 3fc5a4d | 2024-03-10 02:11:03 +0000 | [diff] [blame] | 354 | hdr_in = ssl->in_hdr - ssl->in_buf; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 355 | if (downsizing ? |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 356 | 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] | 357 | ssl->in_buf_len < in_buf_new_len) { |
| 358 | if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != 0) { |
| 359 | MBEDTLS_SSL_DEBUG_MSG(1, ("input buffer resizing failed - out of memory")); |
| 360 | } else { |
| 361 | MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET, |
| 362 | in_buf_new_len)); |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 363 | modified = 1; |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 368 | if (ssl->out_buf != NULL) { |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 369 | written_out = ssl->out_msg - ssl->out_buf; |
| 370 | iv_offset_out = ssl->out_iv - ssl->out_buf; |
| 371 | len_offset_out = ssl->out_len - ssl->out_buf; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 372 | if (downsizing ? |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 373 | 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] | 374 | ssl->out_buf_len < out_buf_new_len) { |
| 375 | if (resize_buffer(&ssl->out_buf, out_buf_new_len, &ssl->out_buf_len) != 0) { |
| 376 | MBEDTLS_SSL_DEBUG_MSG(1, ("output buffer resizing failed - out of memory")); |
| 377 | } else { |
| 378 | MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET, |
| 379 | out_buf_new_len)); |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 380 | modified = 1; |
| 381 | } |
| 382 | } |
| 383 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 384 | if (modified) { |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 385 | /* Update pointers here to avoid doing it twice. */ |
Deomid rojer Ryabkov | 3fc5a4d | 2024-03-10 02:11:03 +0000 | [diff] [blame] | 386 | ssl->in_hdr = ssl->in_buf + hdr_in; |
| 387 | mbedtls_ssl_update_in_pointers(ssl); |
| 388 | mbedtls_ssl_reset_out_pointers(ssl); |
| 389 | |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 390 | /* Fields below might not be properly updated with record |
| 391 | * splitting or with CID, so they are manually updated here. */ |
| 392 | ssl->out_msg = ssl->out_buf + written_out; |
| 393 | ssl->out_len = ssl->out_buf + len_offset_out; |
| 394 | ssl->out_iv = ssl->out_buf + iv_offset_out; |
| 395 | |
| 396 | ssl->in_msg = ssl->in_buf + written_in; |
| 397 | ssl->in_len = ssl->in_buf + len_offset_in; |
| 398 | ssl->in_iv = ssl->in_buf + iv_offset_in; |
| 399 | } |
| 400 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 401 | #endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */ |
| 402 | |
Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 403 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 404 | |
| 405 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 406 | typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen, |
| 407 | const char *label, |
| 408 | const unsigned char *random, size_t rlen, |
| 409 | unsigned char *dstbuf, size_t dlen); |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 410 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 411 | static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id); |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 412 | |
| 413 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
| 414 | |
| 415 | /* Type for the TLS PRF */ |
| 416 | typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *, |
| 417 | const unsigned char *, size_t, |
| 418 | unsigned char *, size_t); |
| 419 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 420 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 421 | static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform, |
| 422 | int ciphersuite, |
| 423 | const unsigned char master[48], |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 424 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 425 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 426 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 427 | ssl_tls_prf_t tls_prf, |
| 428 | const unsigned char randbytes[64], |
| 429 | mbedtls_ssl_protocol_version tls_version, |
| 430 | unsigned endpoint, |
| 431 | const mbedtls_ssl_context *ssl); |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 432 | |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 433 | #if defined(MBEDTLS_MD_CAN_SHA256) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 434 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 435 | static int tls_prf_sha256(const unsigned char *secret, size_t slen, |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 436 | const char *label, |
| 437 | const unsigned char *random, size_t rlen, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 438 | unsigned char *dstbuf, size_t dlen); |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 439 | static int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *); |
| 440 | 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] | 441 | |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 442 | #endif /* MBEDTLS_MD_CAN_SHA256*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 443 | |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 444 | #if defined(MBEDTLS_MD_CAN_SHA384) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 445 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 446 | static int tls_prf_sha384(const unsigned char *secret, size_t slen, |
| 447 | const char *label, |
| 448 | const unsigned char *random, size_t rlen, |
| 449 | unsigned char *dstbuf, size_t dlen); |
| 450 | |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 451 | static int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *); |
| 452 | 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] | 453 | #endif /* MBEDTLS_MD_CAN_SHA384*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 454 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 455 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 456 | static int ssl_tls12_session_load(mbedtls_ssl_session *session, |
| 457 | const unsigned char *buf, |
| 458 | size_t len); |
| 459 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 460 | |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 461 | 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] | 462 | |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 463 | #if defined(MBEDTLS_MD_CAN_SHA256) |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 464 | 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] | 465 | #endif /* MBEDTLS_MD_CAN_SHA256*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 466 | |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 467 | #if defined(MBEDTLS_MD_CAN_SHA384) |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 468 | 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] | 469 | #endif /* MBEDTLS_MD_CAN_SHA384*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 470 | |
| 471 | int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf, |
| 472 | const unsigned char *secret, size_t slen, |
| 473 | const char *label, |
| 474 | const unsigned char *random, size_t rlen, |
| 475 | unsigned char *dstbuf, size_t dlen) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 476 | { |
| 477 | mbedtls_ssl_tls_prf_cb *tls_prf = NULL; |
| 478 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 479 | switch (prf) { |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 480 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 481 | #if defined(MBEDTLS_MD_CAN_SHA384) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 482 | case MBEDTLS_SSL_TLS_PRF_SHA384: |
| 483 | tls_prf = tls_prf_sha384; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 484 | break; |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 485 | #endif /* MBEDTLS_MD_CAN_SHA384*/ |
| 486 | #if defined(MBEDTLS_MD_CAN_SHA256) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 487 | case MBEDTLS_SSL_TLS_PRF_SHA256: |
| 488 | tls_prf = tls_prf_sha256; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 489 | break; |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 490 | #endif /* MBEDTLS_MD_CAN_SHA256*/ |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 491 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 492 | default: |
| 493 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 494 | } |
| 495 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 496 | return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen); |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 497 | } |
| 498 | |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 499 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 500 | static void ssl_clear_peer_cert(mbedtls_ssl_session *session) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 501 | { |
| 502 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 503 | if (session->peer_cert != NULL) { |
| 504 | mbedtls_x509_crt_free(session->peer_cert); |
| 505 | mbedtls_free(session->peer_cert); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 506 | session->peer_cert = NULL; |
| 507 | } |
| 508 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 509 | if (session->peer_cert_digest != NULL) { |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 510 | /* Zeroization is not necessary. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 511 | mbedtls_free(session->peer_cert_digest); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 512 | session->peer_cert_digest = NULL; |
| 513 | session->peer_cert_digest_type = MBEDTLS_MD_NONE; |
| 514 | session->peer_cert_digest_len = 0; |
| 515 | } |
| 516 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 517 | } |
| 518 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 519 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 520 | uint32_t mbedtls_ssl_get_extension_id(unsigned int extension_type) |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 521 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 522 | switch (extension_type) { |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 523 | case MBEDTLS_TLS_EXT_SERVERNAME: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 524 | return MBEDTLS_SSL_EXT_ID_SERVERNAME; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 525 | |
| 526 | case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 527 | return MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 528 | |
| 529 | case MBEDTLS_TLS_EXT_STATUS_REQUEST: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 530 | return MBEDTLS_SSL_EXT_ID_STATUS_REQUEST; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 531 | |
| 532 | case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 533 | return MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 534 | |
| 535 | case MBEDTLS_TLS_EXT_SIG_ALG: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 536 | return MBEDTLS_SSL_EXT_ID_SIG_ALG; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 537 | |
| 538 | case MBEDTLS_TLS_EXT_USE_SRTP: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 539 | return MBEDTLS_SSL_EXT_ID_USE_SRTP; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 540 | |
| 541 | case MBEDTLS_TLS_EXT_HEARTBEAT: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 542 | return MBEDTLS_SSL_EXT_ID_HEARTBEAT; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 543 | |
| 544 | case MBEDTLS_TLS_EXT_ALPN: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 545 | return MBEDTLS_SSL_EXT_ID_ALPN; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 546 | |
| 547 | case MBEDTLS_TLS_EXT_SCT: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 548 | return MBEDTLS_SSL_EXT_ID_SCT; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 549 | |
| 550 | case MBEDTLS_TLS_EXT_CLI_CERT_TYPE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 551 | return MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 552 | |
| 553 | case MBEDTLS_TLS_EXT_SERV_CERT_TYPE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 554 | return MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 555 | |
| 556 | case MBEDTLS_TLS_EXT_PADDING: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 557 | return MBEDTLS_SSL_EXT_ID_PADDING; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 558 | |
| 559 | case MBEDTLS_TLS_EXT_PRE_SHARED_KEY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 560 | return MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 561 | |
| 562 | case MBEDTLS_TLS_EXT_EARLY_DATA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 563 | return MBEDTLS_SSL_EXT_ID_EARLY_DATA; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 564 | |
| 565 | case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 566 | return MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 567 | |
| 568 | case MBEDTLS_TLS_EXT_COOKIE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 569 | return MBEDTLS_SSL_EXT_ID_COOKIE; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 570 | |
| 571 | case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 572 | return MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 573 | |
| 574 | case MBEDTLS_TLS_EXT_CERT_AUTH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 575 | return MBEDTLS_SSL_EXT_ID_CERT_AUTH; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 576 | |
| 577 | case MBEDTLS_TLS_EXT_OID_FILTERS: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 578 | return MBEDTLS_SSL_EXT_ID_OID_FILTERS; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 579 | |
| 580 | case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 581 | return MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 582 | |
| 583 | case MBEDTLS_TLS_EXT_SIG_ALG_CERT: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 584 | return MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 585 | |
| 586 | case MBEDTLS_TLS_EXT_KEY_SHARE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 587 | return MBEDTLS_SSL_EXT_ID_KEY_SHARE; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 588 | |
| 589 | case MBEDTLS_TLS_EXT_TRUNCATED_HMAC: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 590 | return MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 591 | |
| 592 | case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 593 | return MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 594 | |
| 595 | case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 596 | return MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 597 | |
| 598 | case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 599 | return MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 600 | |
Jan Bruckner | 151f642 | 2023-02-10 12:45:19 +0100 | [diff] [blame] | 601 | case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT: |
| 602 | return MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT; |
| 603 | |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 604 | case MBEDTLS_TLS_EXT_SESSION_TICKET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 605 | return MBEDTLS_SSL_EXT_ID_SESSION_TICKET; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 606 | |
| 607 | } |
| 608 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 609 | return MBEDTLS_SSL_EXT_ID_UNRECOGNIZED; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 610 | } |
| 611 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 612 | uint32_t mbedtls_ssl_get_extension_mask(unsigned int extension_type) |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 613 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 614 | return 1 << mbedtls_ssl_get_extension_id(extension_type); |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 615 | } |
| 616 | |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 617 | #if defined(MBEDTLS_DEBUG_C) |
| 618 | static const char *extension_name_table[] = { |
Jerry Yu | ea52ed9 | 2022-11-08 21:01:17 +0800 | [diff] [blame] | 619 | [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized", |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 620 | [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name", |
| 621 | [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length", |
| 622 | [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request", |
| 623 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups", |
| 624 | [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms", |
| 625 | [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp", |
| 626 | [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat", |
| 627 | [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation", |
| 628 | [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp", |
| 629 | [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type", |
| 630 | [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type", |
| 631 | [MBEDTLS_SSL_EXT_ID_PADDING] = "padding", |
| 632 | [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key", |
| 633 | [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data", |
| 634 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions", |
| 635 | [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie", |
| 636 | [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes", |
| 637 | [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities", |
| 638 | [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters", |
| 639 | [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth", |
| 640 | [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert", |
| 641 | [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share", |
| 642 | [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac", |
| 643 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats", |
| 644 | [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac", |
| 645 | [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret", |
Jan Bruckner | 151f642 | 2023-02-10 12:45:19 +0100 | [diff] [blame] | 646 | [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket", |
| 647 | [MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT] = "record_size_limit" |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 648 | }; |
| 649 | |
Chien Wong | 4e9683e | 2023-12-28 17:07:43 +0800 | [diff] [blame] | 650 | static const unsigned int extension_type_table[] = { |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 651 | [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff, |
| 652 | [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME, |
| 653 | [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, |
| 654 | [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST, |
| 655 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, |
| 656 | [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG, |
| 657 | [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP, |
| 658 | [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT, |
| 659 | [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN, |
| 660 | [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT, |
| 661 | [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE, |
| 662 | [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE, |
| 663 | [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING, |
| 664 | [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY, |
| 665 | [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA, |
| 666 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, |
| 667 | [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE, |
| 668 | [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES, |
| 669 | [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH, |
| 670 | [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS, |
| 671 | [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH, |
| 672 | [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT, |
| 673 | [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE, |
| 674 | [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC, |
| 675 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, |
| 676 | [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, |
| 677 | [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] | 678 | [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET, |
| 679 | [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] | 680 | }; |
| 681 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 682 | const char *mbedtls_ssl_get_extension_name(unsigned int extension_type) |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 683 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 684 | return extension_name_table[ |
| 685 | mbedtls_ssl_get_extension_id(extension_type)]; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 686 | } |
| 687 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 688 | 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] | 689 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 690 | switch (hs_msg_type) { |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 691 | case MBEDTLS_SSL_HS_CLIENT_HELLO: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 692 | return "ClientHello"; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 693 | case MBEDTLS_SSL_HS_SERVER_HELLO: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 694 | return "ServerHello"; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 695 | case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 696 | return "HelloRetryRequest"; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 697 | case MBEDTLS_SSL_HS_NEW_SESSION_TICKET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 698 | return "NewSessionTicket"; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 699 | case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 700 | return "EncryptedExtensions"; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 701 | case MBEDTLS_SSL_HS_CERTIFICATE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 702 | return "Certificate"; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 703 | case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 704 | return "CertificateRequest"; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 705 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 706 | return "Unknown"; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 707 | } |
| 708 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 709 | void mbedtls_ssl_print_extension(const mbedtls_ssl_context *ssl, |
| 710 | int level, const char *file, int line, |
| 711 | int hs_msg_type, unsigned int extension_type, |
| 712 | const char *extra_msg0, const char *extra_msg1) |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 713 | { |
| 714 | const char *extra_msg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 715 | if (extra_msg0 && extra_msg1) { |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 716 | mbedtls_debug_print_msg( |
| 717 | ssl, level, file, line, |
| 718 | "%s: %s(%u) extension %s %s.", |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 719 | ssl_tls13_get_hs_msg_name(hs_msg_type), |
| 720 | mbedtls_ssl_get_extension_name(extension_type), |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 721 | extension_type, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 722 | extra_msg0, extra_msg1); |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 723 | return; |
| 724 | } |
| 725 | |
| 726 | extra_msg = extra_msg0 ? extra_msg0 : extra_msg1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 727 | if (extra_msg) { |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 728 | mbedtls_debug_print_msg( |
| 729 | ssl, level, file, line, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 730 | "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name(hs_msg_type), |
| 731 | mbedtls_ssl_get_extension_name(extension_type), extension_type, |
| 732 | extra_msg); |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 733 | return; |
| 734 | } |
| 735 | |
| 736 | mbedtls_debug_print_msg( |
| 737 | ssl, level, file, line, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 738 | "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name(hs_msg_type), |
| 739 | mbedtls_ssl_get_extension_name(extension_type), extension_type); |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 740 | } |
| 741 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 742 | void mbedtls_ssl_print_extensions(const mbedtls_ssl_context *ssl, |
| 743 | int level, const char *file, int line, |
| 744 | int hs_msg_type, uint32_t extensions_mask, |
| 745 | const char *extra) |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 746 | { |
| 747 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 748 | for (unsigned i = 0; |
| 749 | i < sizeof(extension_name_table) / sizeof(extension_name_table[0]); |
| 750 | i++) { |
Jerry Yu | 79aa721 | 2022-11-08 21:30:21 +0800 | [diff] [blame] | 751 | mbedtls_ssl_print_extension( |
Jerry Yu | ea52ed9 | 2022-11-08 21:01:17 +0800 | [diff] [blame] | 752 | ssl, level, file, line, hs_msg_type, extension_type_table[i], |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 753 | extensions_mask & (1 << i) ? "exists" : "does not exist", extra); |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 754 | } |
| 755 | } |
| 756 | |
Pengyu Lv | ee455c0 | 2023-01-12 14:37:24 +0800 | [diff] [blame] | 757 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Pengyu Lv | ee455c0 | 2023-01-12 14:37:24 +0800 | [diff] [blame] | 758 | static const char *ticket_flag_name_table[] = |
| 759 | { |
| 760 | [0] = "ALLOW_PSK_RESUMPTION", |
| 761 | [2] = "ALLOW_PSK_EPHEMERAL_RESUMPTION", |
| 762 | [3] = "ALLOW_EARLY_DATA", |
| 763 | }; |
| 764 | |
Pengyu Lv | 4938a56 | 2023-01-16 11:28:49 +0800 | [diff] [blame] | 765 | void mbedtls_ssl_print_ticket_flags(const mbedtls_ssl_context *ssl, |
| 766 | int level, const char *file, int line, |
| 767 | unsigned int flags) |
Pengyu Lv | ee455c0 | 2023-01-12 14:37:24 +0800 | [diff] [blame] | 768 | { |
| 769 | size_t i; |
| 770 | |
| 771 | mbedtls_debug_print_msg(ssl, level, file, line, |
Pengyu Lv | 4938a56 | 2023-01-16 11:28:49 +0800 | [diff] [blame] | 772 | "print ticket_flags (0x%02x)", flags); |
| 773 | |
| 774 | flags = flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK; |
Pengyu Lv | ee455c0 | 2023-01-12 14:37:24 +0800 | [diff] [blame] | 775 | |
| 776 | for (i = 0; i < ARRAY_LENGTH(ticket_flag_name_table); i++) { |
Pengyu Lv | 4938a56 | 2023-01-16 11:28:49 +0800 | [diff] [blame] | 777 | if ((flags & (1 << i))) { |
Pengyu Lv | ee455c0 | 2023-01-12 14:37:24 +0800 | [diff] [blame] | 778 | mbedtls_debug_print_msg(ssl, level, file, line, "- %s is set.", |
| 779 | ticket_flag_name_table[i]); |
| 780 | } |
| 781 | } |
| 782 | } |
| 783 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */ |
| 784 | |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 785 | #endif /* MBEDTLS_DEBUG_C */ |
| 786 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 787 | void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl, |
| 788 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 789 | { |
| 790 | ((void) ciphersuite_info); |
| 791 | |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 792 | #if defined(MBEDTLS_MD_CAN_SHA384) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 793 | if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) { |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 794 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 795 | } else |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 796 | #endif |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 797 | #if defined(MBEDTLS_MD_CAN_SHA256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 798 | if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) { |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 799 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 800 | } else |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 801 | #endif |
| 802 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 803 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 804 | return; |
| 805 | } |
| 806 | } |
| 807 | |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 808 | 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] | 809 | unsigned hs_type, |
| 810 | size_t total_hs_len) |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 811 | { |
| 812 | unsigned char hs_hdr[4]; |
| 813 | |
| 814 | /* Build HS header for checksum update. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 815 | hs_hdr[0] = MBEDTLS_BYTE_0(hs_type); |
| 816 | hs_hdr[1] = MBEDTLS_BYTE_2(total_hs_len); |
| 817 | hs_hdr[2] = MBEDTLS_BYTE_1(total_hs_len); |
| 818 | hs_hdr[3] = MBEDTLS_BYTE_0(total_hs_len); |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 819 | |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 820 | return ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr)); |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 821 | } |
| 822 | |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 823 | 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] | 824 | unsigned hs_type, |
| 825 | unsigned char const *msg, |
| 826 | size_t msg_len) |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 827 | { |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 828 | int ret; |
| 829 | 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] | 830 | if (ret != 0) { |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 831 | return ret; |
Manuel Pégourié-Gonnard | 43cc127 | 2023-02-06 11:48:19 +0100 | [diff] [blame] | 832 | } |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 833 | return ssl->handshake->update_checksum(ssl, msg, msg_len); |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 834 | } |
| 835 | |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 836 | int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 837 | { |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 838 | #if defined(MBEDTLS_MD_CAN_SHA256) || \ |
| 839 | defined(MBEDTLS_MD_CAN_SHA384) |
Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 840 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 841 | psa_status_t status; |
| 842 | #else |
| 843 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 844 | #endif |
Manuel Pégourié-Gonnard | 626aaed | 2023-02-06 22:03:06 +0100 | [diff] [blame] | 845 | #else /* SHA-256 or SHA-384 */ |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 846 | ((void) ssl); |
Manuel Pégourié-Gonnard | 626aaed | 2023-02-06 22:03:06 +0100 | [diff] [blame] | 847 | #endif /* SHA-256 or SHA-384 */ |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 848 | #if defined(MBEDTLS_MD_CAN_SHA256) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 849 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 850 | status = psa_hash_abort(&ssl->handshake->fin_sha256_psa); |
| 851 | if (status != PSA_SUCCESS) { |
Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 852 | return mbedtls_md_error_from_psa(status); |
Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 853 | } |
| 854 | status = psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256); |
| 855 | if (status != PSA_SUCCESS) { |
Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 856 | return mbedtls_md_error_from_psa(status); |
Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 857 | } |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 858 | #else |
Manuel Pégourié-Gonnard | 947cee1 | 2023-03-06 11:59:59 +0100 | [diff] [blame] | 859 | mbedtls_md_free(&ssl->handshake->fin_sha256); |
| 860 | mbedtls_md_init(&ssl->handshake->fin_sha256); |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 861 | ret = mbedtls_md_setup(&ssl->handshake->fin_sha256, |
| 862 | mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), |
| 863 | 0); |
| 864 | if (ret != 0) { |
| 865 | return ret; |
| 866 | } |
| 867 | ret = mbedtls_md_starts(&ssl->handshake->fin_sha256); |
Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 868 | if (ret != 0) { |
| 869 | return ret; |
| 870 | } |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 871 | #endif |
| 872 | #endif |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 873 | #if defined(MBEDTLS_MD_CAN_SHA384) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 874 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 875 | status = psa_hash_abort(&ssl->handshake->fin_sha384_psa); |
| 876 | if (status != PSA_SUCCESS) { |
Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 877 | return mbedtls_md_error_from_psa(status); |
Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 878 | } |
| 879 | status = psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384); |
| 880 | if (status != PSA_SUCCESS) { |
Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 881 | return mbedtls_md_error_from_psa(status); |
Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 882 | } |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 883 | #else |
Manuel Pégourié-Gonnard | 947cee1 | 2023-03-06 11:59:59 +0100 | [diff] [blame] | 884 | mbedtls_md_free(&ssl->handshake->fin_sha384); |
| 885 | mbedtls_md_init(&ssl->handshake->fin_sha384); |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 886 | ret = mbedtls_md_setup(&ssl->handshake->fin_sha384, |
| 887 | mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0); |
| 888 | if (ret != 0) { |
| 889 | return ret; |
| 890 | } |
| 891 | ret = mbedtls_md_starts(&ssl->handshake->fin_sha384); |
Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 892 | if (ret != 0) { |
| 893 | return ret; |
| 894 | } |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 895 | #endif |
| 896 | #endif |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 897 | return 0; |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 898 | } |
| 899 | |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 900 | static int ssl_update_checksum_start(mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 43cc127 | 2023-02-06 11:48:19 +0100 | [diff] [blame] | 901 | const unsigned char *buf, size_t len) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 902 | { |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 903 | #if defined(MBEDTLS_MD_CAN_SHA256) || \ |
| 904 | defined(MBEDTLS_MD_CAN_SHA384) |
Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 905 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 906 | psa_status_t status; |
| 907 | #else |
| 908 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 909 | #endif |
Manuel Pégourié-Gonnard | 626aaed | 2023-02-06 22:03:06 +0100 | [diff] [blame] | 910 | #else /* SHA-256 or SHA-384 */ |
| 911 | ((void) ssl); |
| 912 | (void) buf; |
| 913 | (void) len; |
| 914 | #endif /* SHA-256 or SHA-384 */ |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 915 | #if defined(MBEDTLS_MD_CAN_SHA256) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 916 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 917 | status = psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len); |
| 918 | if (status != PSA_SUCCESS) { |
Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 919 | return mbedtls_md_error_from_psa(status); |
Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 920 | } |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 921 | #else |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 922 | ret = mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len); |
Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 923 | if (ret != 0) { |
| 924 | return ret; |
| 925 | } |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 926 | #endif |
| 927 | #endif |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 928 | #if defined(MBEDTLS_MD_CAN_SHA384) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 929 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 930 | status = psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len); |
| 931 | if (status != PSA_SUCCESS) { |
Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 932 | return mbedtls_md_error_from_psa(status); |
Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 933 | } |
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 | ret = mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len); |
Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 936 | if (ret != 0) { |
| 937 | return ret; |
| 938 | } |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 939 | #endif |
| 940 | #endif |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 941 | return 0; |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 942 | } |
| 943 | |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 944 | #if defined(MBEDTLS_MD_CAN_SHA256) |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 945 | static int ssl_update_checksum_sha256(mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 43cc127 | 2023-02-06 11:48:19 +0100 | [diff] [blame] | 946 | const unsigned char *buf, size_t len) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 947 | { |
| 948 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 949 | return mbedtls_md_error_from_psa(psa_hash_update( |
| 950 | &ssl->handshake->fin_sha256_psa, buf, len)); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 951 | #else |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 952 | return mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 953 | #endif |
| 954 | } |
| 955 | #endif |
| 956 | |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 957 | #if defined(MBEDTLS_MD_CAN_SHA384) |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 958 | static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 43cc127 | 2023-02-06 11:48:19 +0100 | [diff] [blame] | 959 | const unsigned char *buf, size_t len) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 960 | { |
| 961 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 962 | return mbedtls_md_error_from_psa(psa_hash_update( |
| 963 | &ssl->handshake->fin_sha384_psa, buf, len)); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 964 | #else |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 965 | return mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 966 | #endif |
| 967 | } |
| 968 | #endif |
| 969 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 970 | static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 971 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 972 | memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params)); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 973 | |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 974 | #if defined(MBEDTLS_MD_CAN_SHA256) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 975 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 976 | handshake->fin_sha256_psa = psa_hash_operation_init(); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 977 | #else |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 978 | mbedtls_md_init(&handshake->fin_sha256); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 979 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 980 | #endif |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 981 | #if defined(MBEDTLS_MD_CAN_SHA384) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 982 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 983 | handshake->fin_sha384_psa = psa_hash_operation_init(); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 984 | #else |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 985 | mbedtls_md_init(&handshake->fin_sha384); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 986 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 987 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 988 | |
| 989 | handshake->update_checksum = ssl_update_checksum_start; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 990 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 991 | #if defined(MBEDTLS_DHM_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 992 | mbedtls_dhm_init(&handshake->dhm_ctx); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 993 | #endif |
Valerio Setti | 7aeec54 | 2023-07-05 18:57:21 +0200 | [diff] [blame] | 994 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Valerio Setti | 6eb0054 | 2023-07-07 17:04:24 +0200 | [diff] [blame] | 995 | defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 996 | mbedtls_ecdh_init(&handshake->ecdh_ctx); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 997 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 998 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 999 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1000 | handshake->psa_pake_ctx = psa_pake_operation_init(); |
| 1001 | handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT; |
| 1002 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1003 | mbedtls_ecjpake_init(&handshake->ecjpake_ctx); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1004 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 1005 | #if defined(MBEDTLS_SSL_CLI_C) |
| 1006 | handshake->ecjpake_cache = NULL; |
| 1007 | handshake->ecjpake_cache_len = 0; |
| 1008 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 1009 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1010 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1011 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1012 | mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 1013 | #endif |
| 1014 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1015 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 1016 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 1017 | #endif |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 1018 | |
| 1019 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 1020 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1021 | mbedtls_pk_init(&handshake->peer_pubkey); |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 1022 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1023 | } |
| 1024 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1025 | void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1026 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1027 | memset(transform, 0, sizeof(mbedtls_ssl_transform)); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 1028 | |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 1029 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1030 | transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 1031 | transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 1032 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1033 | mbedtls_cipher_init(&transform->cipher_ctx_enc); |
| 1034 | mbedtls_cipher_init(&transform->cipher_ctx_dec); |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 1035 | #endif |
| 1036 | |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 1037 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 39b8e7d | 2022-02-23 09:24:45 +0100 | [diff] [blame] | 1038 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1039 | transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 1040 | transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 1041 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1042 | mbedtls_md_init(&transform->md_ctx_enc); |
| 1043 | mbedtls_md_init(&transform->md_ctx_dec); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1044 | #endif |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 1045 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1046 | } |
| 1047 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1048 | void mbedtls_ssl_session_init(mbedtls_ssl_session *session) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1049 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1050 | memset(session, 0, sizeof(mbedtls_ssl_session)); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1051 | } |
| 1052 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1053 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1054 | static int ssl_handshake_init(mbedtls_ssl_context *ssl) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1055 | { |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 1056 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1057 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1058 | /* Clear old handshake information if present */ |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1059 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1060 | if (ssl->transform_negotiate) { |
| 1061 | mbedtls_ssl_transform_free(ssl->transform_negotiate); |
| 1062 | } |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1063 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1064 | if (ssl->session_negotiate) { |
| 1065 | mbedtls_ssl_session_free(ssl->session_negotiate); |
| 1066 | } |
| 1067 | if (ssl->handshake) { |
| 1068 | mbedtls_ssl_handshake_free(ssl); |
| 1069 | } |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1070 | |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1071 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1072 | /* |
| 1073 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 1074 | * Now allocate missing structures. |
| 1075 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1076 | if (ssl->transform_negotiate == NULL) { |
| 1077 | ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform)); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 1078 | } |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1079 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1080 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1081 | if (ssl->session_negotiate == NULL) { |
| 1082 | ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session)); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 1083 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1084 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1085 | if (ssl->handshake == NULL) { |
| 1086 | ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params)); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 1087 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1088 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1089 | /* If the buffers are too small - reallocate */ |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 1090 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1091 | handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN, |
| 1092 | MBEDTLS_SSL_OUT_BUFFER_LEN); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1093 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1094 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1095 | /* All pointers should exist and can be directly freed without issue */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1096 | if (ssl->handshake == NULL || |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1097 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1098 | ssl->transform_negotiate == NULL || |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1099 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1100 | ssl->session_negotiate == NULL) { |
| 1101 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed")); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1102 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1103 | mbedtls_free(ssl->handshake); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1104 | ssl->handshake = NULL; |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1105 | |
| 1106 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1107 | mbedtls_free(ssl->transform_negotiate); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1108 | ssl->transform_negotiate = NULL; |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1109 | #endif |
| 1110 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1111 | mbedtls_free(ssl->session_negotiate); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1112 | ssl->session_negotiate = NULL; |
| 1113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1114 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
Jerry Yu | 4caf3ca | 2023-11-15 16:13:47 +0800 | [diff] [blame] | 1117 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 1118 | #if defined(MBEDTLS_SSL_CLI_C) |
Ronald Cron | 05d7cfb | 2024-03-03 15:39:30 +0100 | [diff] [blame] | 1119 | ssl->early_data_state = MBEDTLS_SSL_EARLY_DATA_STATE_IDLE; |
Ronald Cron | 5d0ae90 | 2024-01-05 14:20:35 +0100 | [diff] [blame] | 1120 | #endif |
Jerry Yu | 4caf3ca | 2023-11-15 16:13:47 +0800 | [diff] [blame] | 1121 | #if defined(MBEDTLS_SSL_SRV_C) |
| 1122 | ssl->discard_early_data_record = MBEDTLS_SSL_EARLY_DATA_NO_DISCARD; |
| 1123 | #endif |
Ronald Cron | 19bfe0a | 2024-02-26 16:43:01 +0100 | [diff] [blame] | 1124 | ssl->total_early_data_size = 0; |
Jerry Yu | 4caf3ca | 2023-11-15 16:13:47 +0800 | [diff] [blame] | 1125 | #endif /* MBEDTLS_SSL_EARLY_DATA */ |
Ronald Cron | 5d0ae90 | 2024-01-05 14:20:35 +0100 | [diff] [blame] | 1126 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1127 | /* Initialize structures */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1128 | mbedtls_ssl_session_init(ssl->session_negotiate); |
| 1129 | ssl_handshake_params_init(ssl->handshake); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 1130 | |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1131 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1132 | mbedtls_ssl_transform_init(ssl->transform_negotiate); |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1133 | #endif |
| 1134 | |
Manuel Pégourié-Gonnard | 537f231 | 2023-02-05 10:17:45 +0100 | [diff] [blame] | 1135 | /* Setup handshake checksums */ |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 1136 | ret = mbedtls_ssl_reset_checksum(ssl); |
| 1137 | if (ret != 0) { |
| 1138 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret); |
| 1139 | return ret; |
| 1140 | } |
Manuel Pégourié-Gonnard | 537f231 | 2023-02-05 10:17:45 +0100 | [diff] [blame] | 1141 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 1142 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 1143 | defined(MBEDTLS_SSL_SRV_C) && \ |
| 1144 | defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 1145 | ssl->handshake->new_session_tickets_count = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1146 | ssl->conf->new_session_tickets_count; |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 1147 | #endif |
| 1148 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1149 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1150 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1151 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1153 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1154 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1155 | } else { |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1156 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1157 | } |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1159 | mbedtls_ssl_set_timer(ssl, 0); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1160 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1161 | #endif |
| 1162 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1163 | /* |
| 1164 | * curve_list is translated to IANA TLS group identifiers here because |
| 1165 | * mbedtls_ssl_conf_curves returns void and so can't return |
| 1166 | * any error codes. |
| 1167 | */ |
Manuel Pégourié-Gonnard | f07ce3b | 2023-09-22 11:53:41 +0200 | [diff] [blame] | 1168 | #if defined(MBEDTLS_ECP_C) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1169 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 1170 | /* Heap allocate and translate curve_list from internal to IANA group ids */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1171 | if (ssl->conf->curve_list != NULL) { |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1172 | size_t length; |
| 1173 | const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list; |
| 1174 | |
Thomas Daubney | 850a079 | 2023-05-22 12:05:03 +0100 | [diff] [blame] | 1175 | for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE); length++) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1176 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1177 | |
| 1178 | /* Leave room for zero termination */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1179 | uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t)); |
| 1180 | if (group_list == NULL) { |
| 1181 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 1182 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1183 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1184 | for (size_t i = 0; i < length; i++) { |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 1185 | 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] | 1186 | curve_list[i]); |
| 1187 | if (tls_id == 0) { |
| 1188 | mbedtls_free(group_list); |
| 1189 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1190 | } |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 1191 | group_list[i] = tls_id; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | group_list[length] = 0; |
| 1195 | |
| 1196 | ssl->handshake->group_list = group_list; |
| 1197 | ssl->handshake->group_list_heap_allocated = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1198 | } else { |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1199 | ssl->handshake->group_list = ssl->conf->group_list; |
| 1200 | ssl->handshake->group_list_heap_allocated = 0; |
| 1201 | } |
| 1202 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Manuel Pégourié-Gonnard | f07ce3b | 2023-09-22 11:53:41 +0200 | [diff] [blame] | 1203 | #endif /* MBEDTLS_ECP_C */ |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1204 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1205 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1206 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 1207 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 1208 | /* Heap allocate and translate sig_hashes from internal hash identifiers to |
| 1209 | signature algorithms IANA identifiers. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1210 | if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) && |
| 1211 | ssl->conf->sig_hashes != NULL) { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1212 | const int *md; |
| 1213 | const int *sig_hashes = ssl->conf->sig_hashes; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1214 | size_t sig_algs_len = 0; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1215 | uint16_t *p; |
| 1216 | |
Tom Cosgrove | 6ef9bb3 | 2023-03-08 14:19:51 +0000 | [diff] [blame] | 1217 | MBEDTLS_STATIC_ASSERT(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN |
| 1218 | <= (SIZE_MAX - (2 * sizeof(uint16_t))), |
| 1219 | "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big"); |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 1220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1221 | for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) { |
| 1222 | if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1223 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1224 | } |
Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 1225 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1226 | sig_algs_len += sizeof(uint16_t); |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1227 | #endif |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 1228 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1229 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1230 | sig_algs_len += sizeof(uint16_t); |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1231 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1232 | if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) { |
| 1233 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
| 1234 | } |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1235 | } |
| 1236 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1237 | if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) { |
| 1238 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
| 1239 | } |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1240 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1241 | ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len + |
| 1242 | sizeof(uint16_t)); |
| 1243 | if (ssl->handshake->sig_algs == NULL) { |
| 1244 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 1245 | } |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1246 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1247 | p = (uint16_t *) ssl->handshake->sig_algs; |
| 1248 | for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) { |
| 1249 | unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md); |
| 1250 | if (hash == MBEDTLS_SSL_HASH_NONE) { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1251 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1252 | } |
Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 1253 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1254 | *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1255 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 1256 | #endif |
| 1257 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1258 | *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1259 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 1260 | #endif |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1261 | } |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 1262 | *p = MBEDTLS_TLS_SIG_NONE; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1263 | ssl->handshake->sig_algs_heap_allocated = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1264 | } else |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 1265 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1266 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1267 | ssl->handshake->sig_algs_heap_allocated = 0; |
| 1268 | } |
Jerry Yu | cc53910 | 2022-06-27 16:27:35 +0800 | [diff] [blame] | 1269 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1270 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1271 | return 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1274 | #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] | 1275 | /* Dummy cookie callbacks for defaults */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1276 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1277 | static int ssl_cookie_write_dummy(void *ctx, |
| 1278 | unsigned char **p, unsigned char *end, |
| 1279 | const unsigned char *cli_id, size_t cli_id_len) |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1280 | { |
| 1281 | ((void) ctx); |
| 1282 | ((void) p); |
| 1283 | ((void) end); |
| 1284 | ((void) cli_id); |
| 1285 | ((void) cli_id_len); |
| 1286 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1287 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1288 | } |
| 1289 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1290 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1291 | static int ssl_cookie_check_dummy(void *ctx, |
| 1292 | const unsigned char *cookie, size_t cookie_len, |
| 1293 | const unsigned char *cli_id, size_t cli_id_len) |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1294 | { |
| 1295 | ((void) ctx); |
| 1296 | ((void) cookie); |
| 1297 | ((void) cookie_len); |
| 1298 | ((void) cli_id); |
| 1299 | ((void) cli_id_len); |
| 1300 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1301 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1302 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1303 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1304 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1305 | /* |
| 1306 | * Initialize an SSL context |
| 1307 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1308 | void mbedtls_ssl_init(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1309 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1310 | memset(ssl, 0, sizeof(mbedtls_ssl_context)); |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1311 | } |
| 1312 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1313 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1314 | static int ssl_conf_version_check(const mbedtls_ssl_context *ssl) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1315 | { |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 1316 | const mbedtls_ssl_config *conf = ssl->conf; |
| 1317 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1318 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1319 | if (mbedtls_ssl_conf_is_tls13_only(conf)) { |
| 1320 | if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 1321 | MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported.")); |
| 1322 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1325 | MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only.")); |
| 1326 | return 0; |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1327 | } |
| 1328 | #endif |
| 1329 | |
| 1330 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1331 | if (mbedtls_ssl_conf_is_tls12_only(conf)) { |
| 1332 | MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only.")); |
| 1333 | return 0; |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1334 | } |
| 1335 | #endif |
| 1336 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1337 | #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] | 1338 | if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) { |
| 1339 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 1340 | MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2")); |
| 1341 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1344 | MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2.")); |
| 1345 | return 0; |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1346 | } |
| 1347 | #endif |
| 1348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1349 | MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid.")); |
| 1350 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1351 | } |
| 1352 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1353 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1354 | static int ssl_conf_check(const mbedtls_ssl_context *ssl) |
| 1355 | { |
| 1356 | int ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1357 | ret = ssl_conf_version_check(ssl); |
| 1358 | if (ret != 0) { |
| 1359 | return ret; |
| 1360 | } |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1361 | |
Yanray Wang | b422cab | 2023-12-01 16:18:10 +0800 | [diff] [blame] | 1362 | if (ssl->conf->f_rng == NULL) { |
| 1363 | MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided")); |
| 1364 | return MBEDTLS_ERR_SSL_NO_RNG; |
| 1365 | } |
| 1366 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1367 | /* Space for further checks */ |
| 1368 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1369 | return 0; |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1370 | } |
| 1371 | |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1372 | /* |
| 1373 | * Setup an SSL context |
| 1374 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 1375 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1376 | int mbedtls_ssl_setup(mbedtls_ssl_context *ssl, |
| 1377 | const mbedtls_ssl_config *conf) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1378 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1379 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1380 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1381 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1382 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 1383 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1385 | if ((ret = ssl_conf_check(ssl)) != 0) { |
| 1386 | return ret; |
| 1387 | } |
Ronald Cron | 8a12aee | 2023-03-08 15:30:43 +0100 | [diff] [blame] | 1388 | ssl->tls_version = ssl->conf->max_tls_version; |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1389 | |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1390 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 1391 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1392 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 1393 | |
| 1394 | /* Set to NULL in case of an error condition */ |
| 1395 | ssl->out_buf = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1396 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1397 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1398 | ssl->in_buf_len = in_buf_len; |
| 1399 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1400 | ssl->in_buf = mbedtls_calloc(1, in_buf_len); |
| 1401 | if (ssl->in_buf == NULL) { |
| 1402 | 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] | 1403 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1404 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1405 | } |
| 1406 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1407 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1408 | ssl->out_buf_len = out_buf_len; |
| 1409 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1410 | ssl->out_buf = mbedtls_calloc(1, out_buf_len); |
| 1411 | if (ssl->out_buf == NULL) { |
| 1412 | 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] | 1413 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1414 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
Deomid rojer Ryabkov | 96e2290 | 2025-01-26 10:43:42 +0200 | [diff] [blame] | 1417 | mbedtls_ssl_reset_in_pointers(ssl); |
| 1418 | mbedtls_ssl_reset_out_pointers(ssl); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 1419 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1420 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1421 | memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info)); |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1422 | #endif |
| 1423 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1424 | if ((ret = ssl_handshake_init(ssl)) != 0) { |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1425 | goto error; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1426 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1427 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1428 | return 0; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1429 | |
| 1430 | error: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1431 | mbedtls_free(ssl->in_buf); |
| 1432 | mbedtls_free(ssl->out_buf); |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1433 | |
| 1434 | ssl->conf = NULL; |
| 1435 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1436 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1437 | ssl->in_buf_len = 0; |
| 1438 | ssl->out_buf_len = 0; |
| 1439 | #endif |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1440 | ssl->in_buf = NULL; |
| 1441 | ssl->out_buf = NULL; |
| 1442 | |
| 1443 | ssl->in_hdr = NULL; |
| 1444 | ssl->in_ctr = NULL; |
| 1445 | ssl->in_len = NULL; |
| 1446 | ssl->in_iv = NULL; |
| 1447 | ssl->in_msg = NULL; |
| 1448 | |
| 1449 | ssl->out_hdr = NULL; |
| 1450 | ssl->out_ctr = NULL; |
| 1451 | ssl->out_len = NULL; |
| 1452 | ssl->out_iv = NULL; |
| 1453 | ssl->out_msg = NULL; |
| 1454 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1455 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1459 | * Reset an initialized and used SSL context for re-use while retaining |
| 1460 | * all application-set variables, function pointers and data. |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1461 | * |
| 1462 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 1463 | * (Use when a DTLS client reconnects from the same port.) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1464 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1465 | void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl, |
| 1466 | int partial) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1467 | { |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1468 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1469 | size_t in_buf_len = ssl->in_buf_len; |
| 1470 | size_t out_buf_len = ssl->out_buf_len; |
| 1471 | #else |
| 1472 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1473 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 1474 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1475 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1476 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C) |
| 1477 | partial = 0; |
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 1478 | #endif |
| 1479 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1480 | /* Cancel any possibly running timer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1481 | mbedtls_ssl_set_timer(ssl, 0); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1482 | |
Deomid rojer Ryabkov | 96e2290 | 2025-01-26 10:43:42 +0200 | [diff] [blame] | 1483 | mbedtls_ssl_reset_in_pointers(ssl); |
| 1484 | mbedtls_ssl_reset_out_pointers(ssl); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1485 | |
| 1486 | /* Reset incoming message parsing */ |
| 1487 | ssl->in_offt = NULL; |
| 1488 | ssl->nb_zero = 0; |
| 1489 | ssl->in_msgtype = 0; |
| 1490 | ssl->in_msglen = 0; |
| 1491 | ssl->in_hslen = 0; |
| 1492 | ssl->keep_current_message = 0; |
| 1493 | ssl->transform_in = NULL; |
| 1494 | |
Gilles Peskine | cb72cd2 | 2025-02-17 16:36:36 +0100 | [diff] [blame] | 1495 | /* TLS: reset in_hsfraglen, which is part of message parsing. |
| 1496 | * DTLS: on a client reconnect, don't reset badmac_seen. */ |
| 1497 | if (!partial) { |
| 1498 | ssl->badmac_seen_or_in_hsfraglen = 0; |
| 1499 | } |
| 1500 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1501 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1502 | ssl->next_record_offset = 0; |
| 1503 | ssl->in_epoch = 0; |
| 1504 | #endif |
| 1505 | |
| 1506 | /* Keep current datagram if partial == 1 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1507 | if (partial == 0) { |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1508 | ssl->in_left = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1509 | memset(ssl->in_buf, 0, in_buf_len); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1510 | } |
| 1511 | |
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 1512 | ssl->send_alert = 0; |
| 1513 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1514 | /* Reset outgoing message writing */ |
| 1515 | ssl->out_msgtype = 0; |
| 1516 | ssl->out_msglen = 0; |
| 1517 | ssl->out_left = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1518 | memset(ssl->out_buf, 0, out_buf_len); |
| 1519 | memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr)); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1520 | ssl->transform_out = NULL; |
| 1521 | |
| 1522 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1523 | mbedtls_ssl_dtls_replay_reset(ssl); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1524 | #endif |
| 1525 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1526 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1527 | if (ssl->transform) { |
| 1528 | mbedtls_ssl_transform_free(ssl->transform); |
| 1529 | mbedtls_free(ssl->transform); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1530 | ssl->transform = NULL; |
| 1531 | } |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1532 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 1533 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1534 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1535 | mbedtls_ssl_transform_free(ssl->transform_application); |
| 1536 | mbedtls_free(ssl->transform_application); |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1537 | ssl->transform_application = NULL; |
| 1538 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1539 | if (ssl->handshake != NULL) { |
Jerry Yu | 3d9b590 | 2022-11-04 14:07:25 +0800 | [diff] [blame] | 1540 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1541 | mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata); |
| 1542 | mbedtls_free(ssl->handshake->transform_earlydata); |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1543 | ssl->handshake->transform_earlydata = NULL; |
Jerry Yu | 3d9b590 | 2022-11-04 14:07:25 +0800 | [diff] [blame] | 1544 | #endif |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1545 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1546 | mbedtls_ssl_transform_free(ssl->handshake->transform_handshake); |
| 1547 | mbedtls_free(ssl->handshake->transform_handshake); |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1548 | ssl->handshake->transform_handshake = NULL; |
| 1549 | } |
| 1550 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1551 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1552 | } |
| 1553 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1554 | int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial) |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1555 | { |
| 1556 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1557 | |
Gilles Peskine | 49f179d | 2025-03-07 15:09:32 +0100 | [diff] [blame] | 1558 | mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_REQUEST); |
Ronald Cron | 195c0bc | 2024-02-08 08:51:20 +0100 | [diff] [blame] | 1559 | ssl->tls_version = ssl->conf->max_tls_version; |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1560 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1561 | mbedtls_ssl_session_reset_msg_layer(ssl, partial); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1562 | |
| 1563 | /* Reset renegotiation state */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1564 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1565 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1566 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1567 | |
| 1568 | ssl->verify_data_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1569 | memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN); |
| 1570 | 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] | 1571 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1572 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1573 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1574 | ssl->session_in = NULL; |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 1575 | ssl->session_out = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1576 | if (ssl->session) { |
| 1577 | mbedtls_ssl_session_free(ssl->session); |
| 1578 | mbedtls_free(ssl->session); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1579 | ssl->session = NULL; |
| 1580 | } |
| 1581 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1582 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 1583 | ssl->alpn_chosen = NULL; |
| 1584 | #endif |
| 1585 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1586 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 1587 | int free_cli_id = 1; |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1588 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1589 | free_cli_id = (partial == 0); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1590 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1591 | if (free_cli_id) { |
| 1592 | mbedtls_free(ssl->cli_id); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1593 | ssl->cli_id = NULL; |
| 1594 | ssl->cli_id_len = 0; |
| 1595 | } |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 1596 | #endif |
| 1597 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1598 | if ((ret = ssl_handshake_init(ssl)) != 0) { |
| 1599 | return ret; |
| 1600 | } |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1601 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1602 | return 0; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 1605 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1606 | * Reset an initialized and used SSL context for re-use while retaining |
| 1607 | * all application-set variables, function pointers and data. |
| 1608 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1609 | int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1610 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1611 | return mbedtls_ssl_session_reset_int(ssl, 0); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1612 | } |
| 1613 | |
| 1614 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1615 | * SSL set accessors |
| 1616 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1617 | void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1618 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1619 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1620 | } |
| 1621 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1622 | 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] | 1623 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1624 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 1625 | } |
| 1626 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1627 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1628 | 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] | 1629 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1630 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 1631 | } |
| 1632 | #endif |
| 1633 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1634 | 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] | 1635 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1636 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1637 | } |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1638 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1639 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1640 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1641 | void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl, |
| 1642 | unsigned allow_packing) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1643 | { |
| 1644 | ssl->disable_datagram_packing = !allow_packing; |
| 1645 | } |
| 1646 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1647 | void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf, |
| 1648 | uint32_t min, uint32_t max) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1649 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1650 | conf->hs_timeout_min = min; |
| 1651 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1652 | } |
| 1653 | #endif |
| 1654 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1655 | void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1656 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1657 | conf->authmode = authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1660 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1661 | void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf, |
| 1662 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 1663 | void *p_vrfy) |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1664 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1665 | conf->f_vrfy = f_vrfy; |
| 1666 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1667 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1668 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1669 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1670 | void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf, |
| 1671 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1672 | void *p_rng) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1673 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1674 | conf->f_rng = f_rng; |
| 1675 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1676 | } |
| 1677 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1678 | void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf, |
| 1679 | void (*f_dbg)(void *, int, const char *, int, const char *), |
| 1680 | void *p_dbg) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1681 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1682 | conf->f_dbg = f_dbg; |
| 1683 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1686 | void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl, |
| 1687 | void *p_bio, |
| 1688 | mbedtls_ssl_send_t *f_send, |
| 1689 | mbedtls_ssl_recv_t *f_recv, |
| 1690 | mbedtls_ssl_recv_timeout_t *f_recv_timeout) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1691 | { |
| 1692 | ssl->p_bio = p_bio; |
| 1693 | ssl->f_send = f_send; |
| 1694 | ssl->f_recv = f_recv; |
| 1695 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 1696 | } |
| 1697 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 1698 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1699 | 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] | 1700 | { |
| 1701 | ssl->mtu = mtu; |
| 1702 | } |
| 1703 | #endif |
| 1704 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1705 | 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] | 1706 | { |
| 1707 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1708 | } |
| 1709 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1710 | void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl, |
| 1711 | void *p_timer, |
| 1712 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 1713 | mbedtls_ssl_get_timer_t *f_get_timer) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1714 | { |
| 1715 | ssl->p_timer = p_timer; |
| 1716 | ssl->f_set_timer = f_set_timer; |
| 1717 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1718 | |
| 1719 | /* Make sure we start with no timer running */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1720 | mbedtls_ssl_set_timer(ssl, 0); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1721 | } |
| 1722 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1723 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1724 | void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf, |
| 1725 | void *p_cache, |
| 1726 | mbedtls_ssl_cache_get_t *f_get_cache, |
| 1727 | mbedtls_ssl_cache_set_t *f_set_cache) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1728 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 1729 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1730 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1731 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1732 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1733 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1734 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1735 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1736 | 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] | 1737 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1738 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1739 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1740 | if (ssl == NULL || |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1741 | session == NULL || |
| 1742 | ssl->session_negotiate == NULL || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1743 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) { |
| 1744 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1745 | } |
| 1746 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1747 | if (ssl->handshake->resume == 1) { |
| 1748 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 1749 | } |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 1750 | |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1751 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1752 | if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Ronald Cron | 8d63084 | 2024-04-04 14:05:21 +0200 | [diff] [blame] | 1753 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1754 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1755 | mbedtls_ssl_ciphersuite_from_id(session->ciphersuite); |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1756 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1757 | if (mbedtls_ssl_validate_ciphersuite( |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1758 | ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1759 | MBEDTLS_SSL_VERSION_TLS1_3) != 0) { |
| 1760 | MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.", |
| 1761 | session->ciphersuite)); |
| 1762 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1763 | } |
Ronald Cron | 8d63084 | 2024-04-04 14:05:21 +0200 | [diff] [blame] | 1764 | #else |
| 1765 | /* |
| 1766 | * If session tickets are not enabled, it is not possible to resume a |
| 1767 | * TLS 1.3 session, thus do not make any change to the SSL context in |
| 1768 | * the first place. |
| 1769 | */ |
| 1770 | return 0; |
| 1771 | #endif |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1772 | } |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1773 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1774 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1775 | if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate, |
| 1776 | session)) != 0) { |
| 1777 | return ret; |
| 1778 | } |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1779 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1780 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1781 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1782 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1783 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1784 | #endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1785 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1786 | void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf, |
| 1787 | const int *ciphersuites) |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1788 | { |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 1789 | conf->ciphersuite_list = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1790 | } |
| 1791 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1792 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1793 | void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf, |
| 1794 | const int kex_modes) |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1795 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1796 | 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] | 1797 | } |
Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame] | 1798 | |
| 1799 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
Yanray Wang | d5ed36f | 2023-11-07 11:40:43 +0800 | [diff] [blame] | 1800 | void mbedtls_ssl_conf_early_data(mbedtls_ssl_config *conf, |
| 1801 | int early_data_enabled) |
Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame] | 1802 | { |
| 1803 | conf->early_data_enabled = early_data_enabled; |
| 1804 | } |
Jerry Yu | cc4e007 | 2022-11-22 17:22:22 +0800 | [diff] [blame] | 1805 | |
| 1806 | #if defined(MBEDTLS_SSL_SRV_C) |
Yanray Wang | 0751761 | 2023-11-07 11:47:36 +0800 | [diff] [blame] | 1807 | void mbedtls_ssl_conf_max_early_data_size( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1808 | mbedtls_ssl_config *conf, uint32_t max_early_data_size) |
Jerry Yu | cc4e007 | 2022-11-22 17:22:22 +0800 | [diff] [blame] | 1809 | { |
Jerry Yu | 39da985 | 2022-12-06 16:58:36 +0800 | [diff] [blame] | 1810 | conf->max_early_data_size = max_early_data_size; |
Jerry Yu | cc4e007 | 2022-11-22 17:22:22 +0800 | [diff] [blame] | 1811 | } |
| 1812 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 1813 | |
Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame] | 1814 | #endif /* MBEDTLS_SSL_EARLY_DATA */ |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1815 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1816 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1817 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1818 | void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf, |
| 1819 | const mbedtls_x509_crt_profile *profile) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1820 | { |
| 1821 | conf->cert_profile = profile; |
| 1822 | } |
| 1823 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1824 | static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert) |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1825 | { |
| 1826 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
| 1827 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1828 | while (cur != NULL) { |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1829 | next = cur->next; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1830 | mbedtls_free(cur); |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1831 | cur = next; |
| 1832 | } |
| 1833 | } |
| 1834 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1835 | /* Append a new keycert entry to a (possibly empty) list */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1836 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1837 | static int ssl_append_key_cert(mbedtls_ssl_key_cert **head, |
| 1838 | mbedtls_x509_crt *cert, |
| 1839 | mbedtls_pk_context *key) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1840 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1841 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1842 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1843 | if (cert == NULL) { |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1844 | /* Free list if cert is null */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1845 | ssl_key_cert_free(*head); |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1846 | *head = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1847 | return 0; |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1848 | } |
| 1849 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1850 | new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert)); |
| 1851 | if (new_cert == NULL) { |
| 1852 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 1853 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1854 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1855 | new_cert->cert = cert; |
| 1856 | new_cert->key = key; |
| 1857 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1858 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1859 | /* Update head if the list was null, else add to the end */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1860 | if (*head == NULL) { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1861 | *head = new_cert; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1862 | } else { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1863 | mbedtls_ssl_key_cert *cur = *head; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1864 | while (cur->next != NULL) { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1865 | cur = cur->next; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1866 | } |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1867 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1868 | } |
| 1869 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1870 | return 0; |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1871 | } |
| 1872 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1873 | int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1874 | mbedtls_x509_crt *own_cert, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1875 | mbedtls_pk_context *pk_key) |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1876 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1877 | 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] | 1878 | } |
| 1879 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1880 | void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1881 | mbedtls_x509_crt *ca_chain, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1882 | mbedtls_x509_crl *ca_crl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1883 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1884 | conf->ca_chain = ca_chain; |
| 1885 | conf->ca_crl = ca_crl; |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1886 | |
| 1887 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1888 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1889 | * cannot be used together. */ |
| 1890 | conf->f_ca_cb = NULL; |
| 1891 | conf->p_ca_cb = NULL; |
| 1892 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1893 | } |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1894 | |
| 1895 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1896 | void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf, |
| 1897 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
| 1898 | void *p_ca_cb) |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1899 | { |
| 1900 | conf->f_ca_cb = f_ca_cb; |
| 1901 | conf->p_ca_cb = p_ca_cb; |
| 1902 | |
| 1903 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1904 | * cannot be used together. */ |
| 1905 | conf->ca_chain = NULL; |
| 1906 | conf->ca_crl = NULL; |
| 1907 | } |
| 1908 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1909 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 1910 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1911 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1912 | const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl, |
| 1913 | size_t *name_len) |
Glenn Strauss | 6989407 | 2022-01-24 12:58:00 -0500 | [diff] [blame] | 1914 | { |
| 1915 | *name_len = ssl->handshake->sni_name_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1916 | return ssl->handshake->sni_name; |
Glenn Strauss | 6989407 | 2022-01-24 12:58:00 -0500 | [diff] [blame] | 1917 | } |
| 1918 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1919 | int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl, |
| 1920 | mbedtls_x509_crt *own_cert, |
| 1921 | mbedtls_pk_context *pk_key) |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1922 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1923 | return ssl_append_key_cert(&ssl->handshake->sni_key_cert, |
| 1924 | own_cert, pk_key); |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1925 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 1926 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1927 | void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl, |
| 1928 | mbedtls_x509_crt *ca_chain, |
| 1929 | mbedtls_x509_crl *ca_crl) |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 1930 | { |
| 1931 | ssl->handshake->sni_ca_chain = ca_chain; |
| 1932 | ssl->handshake->sni_ca_crl = ca_crl; |
| 1933 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1934 | |
Glenn Strauss | 999ef70 | 2022-03-11 01:37:23 -0500 | [diff] [blame] | 1935 | #if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1936 | void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl, |
| 1937 | const mbedtls_x509_crt *crt) |
Glenn Strauss | 999ef70 | 2022-03-11 01:37:23 -0500 | [diff] [blame] | 1938 | { |
| 1939 | ssl->handshake->dn_hints = crt; |
| 1940 | } |
| 1941 | #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ |
| 1942 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1943 | void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl, |
| 1944 | int authmode) |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1945 | { |
| 1946 | ssl->handshake->sni_authmode = authmode; |
| 1947 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1948 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 1949 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1950 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1951 | void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl, |
| 1952 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 1953 | void *p_vrfy) |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1954 | { |
| 1955 | ssl->f_vrfy = f_vrfy; |
| 1956 | ssl->p_vrfy = p_vrfy; |
| 1957 | } |
| 1958 | #endif |
| 1959 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1960 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1961 | |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1962 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemek Stekiel | fde1128 | 2023-03-13 16:06:09 +0100 | [diff] [blame] | 1963 | static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' }; |
| 1964 | static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' }; |
| 1965 | |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1966 | static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1967 | mbedtls_ssl_context *ssl, |
| 1968 | mbedtls_svc_key_id_t pwd) |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1969 | { |
| 1970 | psa_status_t status; |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1971 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
Przemek Stekiel | fde1128 | 2023-03-13 16:06:09 +0100 | [diff] [blame] | 1972 | const uint8_t *user = NULL; |
Przemek Stekiel | 4cd2031 | 2023-03-01 11:11:28 +0100 | [diff] [blame] | 1973 | size_t user_len = 0; |
Przemek Stekiel | fde1128 | 2023-03-13 16:06:09 +0100 | [diff] [blame] | 1974 | const uint8_t *peer = NULL; |
Przemek Stekiel | 4cd2031 | 2023-03-01 11:11:28 +0100 | [diff] [blame] | 1975 | size_t peer_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1976 | psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE); |
| 1977 | psa_pake_cs_set_primitive(&cipher_suite, |
| 1978 | PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, |
| 1979 | PSA_ECC_FAMILY_SECP_R1, |
| 1980 | 256)); |
| 1981 | psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256); |
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_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite); |
| 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 | } |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1987 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1988 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Przemek Stekiel | fde1128 | 2023-03-13 16:06:09 +0100 | [diff] [blame] | 1989 | user = jpake_server_id; |
| 1990 | user_len = sizeof(jpake_server_id); |
| 1991 | peer = jpake_client_id; |
| 1992 | peer_len = sizeof(jpake_client_id); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1993 | } else { |
Przemek Stekiel | fde1128 | 2023-03-13 16:06:09 +0100 | [diff] [blame] | 1994 | user = jpake_client_id; |
| 1995 | user_len = sizeof(jpake_client_id); |
| 1996 | peer = jpake_server_id; |
| 1997 | peer_len = sizeof(jpake_server_id); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1998 | } |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1999 | |
Przemek Stekiel | 4cd2031 | 2023-03-01 11:11:28 +0100 | [diff] [blame] | 2000 | status = psa_pake_set_user(&ssl->handshake->psa_pake_ctx, user, user_len); |
| 2001 | if (status != PSA_SUCCESS) { |
| 2002 | return status; |
| 2003 | } |
| 2004 | |
| 2005 | 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] | 2006 | if (status != PSA_SUCCESS) { |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 2007 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2008 | } |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 2009 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2010 | status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd); |
| 2011 | if (status != PSA_SUCCESS) { |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 2012 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2013 | } |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 2014 | |
| 2015 | ssl->handshake->psa_pake_ctx_is_ok = 1; |
| 2016 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2017 | return PSA_SUCCESS; |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 2018 | } |
| 2019 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2020 | int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl, |
| 2021 | const unsigned char *pw, |
| 2022 | size_t pw_len) |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 2023 | { |
| 2024 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2025 | psa_status_t status; |
| 2026 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2027 | if (ssl->handshake == NULL || ssl->conf == NULL) { |
| 2028 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 2029 | } |
| 2030 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2031 | /* Empty password is not valid */ |
| 2032 | if ((pw == NULL) || (pw_len == 0)) { |
| 2033 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2034 | } |
| 2035 | |
| 2036 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 2037 | psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE); |
| 2038 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 2039 | |
| 2040 | status = psa_import_key(&attributes, pw, pw_len, |
| 2041 | &ssl->handshake->psa_pake_password); |
| 2042 | if (status != PSA_SUCCESS) { |
| 2043 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 2044 | } |
| 2045 | |
| 2046 | status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, |
| 2047 | ssl->handshake->psa_pake_password); |
| 2048 | if (status != PSA_SUCCESS) { |
| 2049 | psa_destroy_key(ssl->handshake->psa_pake_password); |
| 2050 | psa_pake_abort(&ssl->handshake->psa_pake_ctx); |
| 2051 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 2052 | } |
| 2053 | |
| 2054 | return 0; |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 2055 | } |
Valerio Setti | a9a97dc | 2022-11-28 18:26:16 +0100 | [diff] [blame] | 2056 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2057 | int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl, |
| 2058 | mbedtls_svc_key_id_t pwd) |
Valerio Setti | a9a97dc | 2022-11-28 18:26:16 +0100 | [diff] [blame] | 2059 | { |
Valerio Setti | a9a97dc | 2022-11-28 18:26:16 +0100 | [diff] [blame] | 2060 | psa_status_t status; |
| 2061 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2062 | if (ssl->handshake == NULL || ssl->conf == NULL) { |
| 2063 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 2064 | } |
| 2065 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2066 | if (mbedtls_svc_key_id_is_null(pwd)) { |
| 2067 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2068 | } |
| 2069 | |
| 2070 | status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd); |
| 2071 | if (status != PSA_SUCCESS) { |
| 2072 | psa_pake_abort(&ssl->handshake->psa_pake_ctx); |
| 2073 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 2074 | } |
| 2075 | |
| 2076 | return 0; |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 2077 | } |
| 2078 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2079 | int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl, |
| 2080 | const unsigned char *pw, |
| 2081 | size_t pw_len) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2082 | { |
| 2083 | mbedtls_ecjpake_role role; |
| 2084 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2085 | if (ssl->handshake == NULL || ssl->conf == NULL) { |
| 2086 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2087 | } |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2088 | |
Valerio Setti | c689ed8 | 2022-12-07 14:40:38 +0100 | [diff] [blame] | 2089 | /* Empty password is not valid */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2090 | if ((pw == NULL) || (pw_len == 0)) { |
| 2091 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2092 | } |
Valerio Setti | c689ed8 | 2022-12-07 14:40:38 +0100 | [diff] [blame] | 2093 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2094 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2095 | role = MBEDTLS_ECJPAKE_SERVER; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2096 | } else { |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2097 | role = MBEDTLS_ECJPAKE_CLIENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2098 | } |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2099 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2100 | return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx, |
| 2101 | role, |
| 2102 | MBEDTLS_MD_SHA256, |
| 2103 | MBEDTLS_ECP_DP_SECP256R1, |
| 2104 | pw, pw_len); |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2105 | } |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 2106 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 2107 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2108 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 2109 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2110 | int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2111 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2112 | if (conf->psk_identity == NULL || |
| 2113 | conf->psk_identity_len == 0) { |
| 2114 | return 0; |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 2115 | } |
| 2116 | |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2117 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2118 | if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) { |
| 2119 | return 1; |
| 2120 | } |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2121 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 2122 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2123 | if (conf->psk != NULL && conf->psk_len != 0) { |
| 2124 | return 1; |
| 2125 | } |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2126 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2127 | return 0; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2128 | } |
| 2129 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2130 | static void ssl_conf_remove_psk(mbedtls_ssl_config *conf) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2131 | { |
| 2132 | /* Remove reference to existing PSK, if any. */ |
| 2133 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2134 | if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) { |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2135 | /* The maintenance of the PSK key slot is the |
| 2136 | * user's responsibility. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2137 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2138 | } |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2139 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2140 | if (conf->psk != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 2141 | mbedtls_zeroize_and_free(conf->psk, conf->psk_len); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2142 | conf->psk = NULL; |
| 2143 | conf->psk_len = 0; |
| 2144 | } |
| 2145 | |
| 2146 | /* Remove reference to PSK identity, if any. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2147 | if (conf->psk_identity != NULL) { |
| 2148 | mbedtls_free(conf->psk_identity); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2149 | conf->psk_identity = NULL; |
| 2150 | conf->psk_identity_len = 0; |
| 2151 | } |
| 2152 | } |
| 2153 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2154 | /* This function assumes that PSK identity in the SSL config is unset. |
| 2155 | * It checks that the provided identity is well-formed and attempts |
| 2156 | * to make a copy of it in the SSL config. |
| 2157 | * On failure, the PSK identity in the config remains unset. */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 2158 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2159 | static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf, |
| 2160 | unsigned char const *psk_identity, |
| 2161 | size_t psk_identity_len) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2162 | { |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 2163 | /* Identity len will be encoded on two bytes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2164 | if (psk_identity == NULL || |
Ronald Cron | 2a87e9b | 2022-10-19 10:55:26 +0200 | [diff] [blame] | 2165 | psk_identity_len == 0 || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2166 | (psk_identity_len >> 16) != 0 || |
| 2167 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) { |
| 2168 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 2169 | } |
| 2170 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2171 | conf->psk_identity = mbedtls_calloc(1, psk_identity_len); |
| 2172 | if (conf->psk_identity == NULL) { |
| 2173 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2174 | } |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 2175 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 2176 | conf->psk_identity_len = psk_identity_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2177 | memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len); |
Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 2178 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2179 | return 0; |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 2180 | } |
| 2181 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2182 | int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf, |
| 2183 | const unsigned char *psk, size_t psk_len, |
| 2184 | const unsigned char *psk_identity, size_t psk_identity_len) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2185 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2186 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2187 | |
| 2188 | /* We currently only support one PSK, raw or opaque. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2189 | if (mbedtls_ssl_conf_has_static_psk(conf)) { |
| 2190 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 2191 | } |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2192 | |
| 2193 | /* Check and set raw PSK */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2194 | if (psk == NULL) { |
| 2195 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2196 | } |
| 2197 | if (psk_len == 0) { |
| 2198 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2199 | } |
| 2200 | if (psk_len > MBEDTLS_PSK_MAX_LEN) { |
| 2201 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2202 | } |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 2203 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2204 | if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) { |
| 2205 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2206 | } |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2207 | conf->psk_len = psk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2208 | memcpy(conf->psk, psk, conf->psk_len); |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2209 | |
| 2210 | /* Check and set PSK Identity */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2211 | ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len); |
| 2212 | if (ret != 0) { |
| 2213 | ssl_conf_remove_psk(conf); |
| 2214 | } |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2215 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2216 | return ret; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2217 | } |
| 2218 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2219 | static void ssl_remove_psk(mbedtls_ssl_context *ssl) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2220 | { |
| 2221 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2222 | if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2223 | /* The maintenance of the external PSK key slot is the |
| 2224 | * user's responsibility. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2225 | if (ssl->handshake->psk_opaque_is_internal) { |
| 2226 | psa_destroy_key(ssl->handshake->psk_opaque); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2227 | ssl->handshake->psk_opaque_is_internal = 0; |
| 2228 | } |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2229 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2230 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 2231 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2232 | if (ssl->handshake->psk != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 2233 | mbedtls_zeroize_and_free(ssl->handshake->psk, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2234 | ssl->handshake->psk_len); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2235 | ssl->handshake->psk_len = 0; |
lhuang04 | 6d4d94f | 2024-06-11 12:37:02 -0700 | [diff] [blame] | 2236 | ssl->handshake->psk = NULL; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2237 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 2238 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2239 | } |
| 2240 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2241 | int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl, |
| 2242 | const unsigned char *psk, size_t psk_len) |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2243 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2244 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2245 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
Jerry Yu | 5d01c05 | 2022-08-17 10:18:10 +0800 | [diff] [blame] | 2246 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 24b8c81 | 2022-08-20 19:06:56 +0800 | [diff] [blame] | 2247 | psa_algorithm_t alg = PSA_ALG_NONE; |
Jerry Yu | 5d01c05 | 2022-08-17 10:18:10 +0800 | [diff] [blame] | 2248 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2249 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2250 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2251 | if (psk == NULL || ssl->handshake == NULL) { |
| 2252 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2253 | } |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2254 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2255 | if (psk_len > MBEDTLS_PSK_MAX_LEN) { |
| 2256 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2257 | } |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2258 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2259 | ssl_remove_psk(ssl); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2260 | |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2261 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 2262 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2263 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) { |
| 2264 | if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) { |
| 2265 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
| 2266 | } else { |
| 2267 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 2268 | } |
| 2269 | psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE); |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 2270 | } |
| 2271 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2272 | |
Jerry Yu | 568ec25 | 2022-07-22 21:27:34 +0800 | [diff] [blame] | 2273 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2274 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
| 2275 | alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH); |
| 2276 | psa_set_key_usage_flags(&key_attributes, |
| 2277 | PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT); |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 2278 | } |
| 2279 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 2280 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2281 | psa_set_key_algorithm(&key_attributes, alg); |
| 2282 | psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2283 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2284 | status = psa_import_key(&key_attributes, psk, psk_len, &key); |
| 2285 | if (status != PSA_SUCCESS) { |
| 2286 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 2287 | } |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2288 | |
| 2289 | /* Allow calling psa_destroy_key() on psk remove */ |
| 2290 | ssl->handshake->psk_opaque_is_internal = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2291 | return mbedtls_ssl_set_hs_psk_opaque(ssl, key); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2292 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2293 | if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) { |
| 2294 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2295 | } |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2296 | |
| 2297 | ssl->handshake->psk_len = psk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2298 | memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2299 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2300 | return 0; |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2301 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2302 | } |
| 2303 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2304 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2305 | int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf, |
| 2306 | mbedtls_svc_key_id_t psk, |
| 2307 | const unsigned char *psk_identity, |
| 2308 | size_t psk_identity_len) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2309 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2310 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2311 | |
| 2312 | /* We currently only support one PSK, raw or opaque. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2313 | if (mbedtls_ssl_conf_has_static_psk(conf)) { |
| 2314 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 2315 | } |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2316 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2317 | /* Check and set opaque PSK */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2318 | if (mbedtls_svc_key_id_is_null(psk)) { |
| 2319 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2320 | } |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2321 | conf->psk_opaque = psk; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2322 | |
| 2323 | /* Check and set PSK Identity */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2324 | ret = ssl_conf_set_psk_identity(conf, psk_identity, |
| 2325 | psk_identity_len); |
| 2326 | if (ret != 0) { |
| 2327 | ssl_conf_remove_psk(conf); |
| 2328 | } |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2329 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2330 | return ret; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2331 | } |
| 2332 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2333 | int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl, |
| 2334 | mbedtls_svc_key_id_t psk) |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2335 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2336 | if ((mbedtls_svc_key_id_is_null(psk)) || |
| 2337 | (ssl->handshake == NULL)) { |
| 2338 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2339 | } |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2340 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2341 | ssl_remove_psk(ssl); |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2342 | ssl->handshake->psk_opaque = psk; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2343 | return 0; |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2344 | } |
| 2345 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2346 | |
Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 2347 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2348 | void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf, |
| 2349 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
| 2350 | size_t), |
| 2351 | void *p_psk) |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2352 | { |
| 2353 | conf->f_psk = f_psk; |
| 2354 | conf->p_psk = p_psk; |
| 2355 | } |
Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 2356 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 2357 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 2358 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2359 | |
| 2360 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2361 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2362 | psa_algorithm_t alg) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2363 | { |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2364 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2365 | if (alg == PSA_ALG_CBC_NO_PADDING) { |
| 2366 | return MBEDTLS_SSL_MODE_CBC; |
| 2367 | } |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2368 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2369 | if (PSA_ALG_IS_AEAD(alg)) { |
| 2370 | return MBEDTLS_SSL_MODE_AEAD; |
| 2371 | } |
| 2372 | return MBEDTLS_SSL_MODE_STREAM; |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2373 | } |
| 2374 | |
| 2375 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2376 | |
| 2377 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2378 | mbedtls_cipher_mode_t mode) |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2379 | { |
| 2380 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2381 | if (mode == MBEDTLS_MODE_CBC) { |
| 2382 | return MBEDTLS_SSL_MODE_CBC; |
| 2383 | } |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2384 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 2385 | |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2386 | #if defined(MBEDTLS_GCM_C) || \ |
| 2387 | defined(MBEDTLS_CCM_C) || \ |
| 2388 | defined(MBEDTLS_CHACHAPOLY_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2389 | if (mode == MBEDTLS_MODE_GCM || |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2390 | mode == MBEDTLS_MODE_CCM || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2391 | mode == MBEDTLS_MODE_CHACHAPOLY) { |
| 2392 | return MBEDTLS_SSL_MODE_AEAD; |
| 2393 | } |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2394 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2395 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2396 | return MBEDTLS_SSL_MODE_STREAM; |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2397 | } |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2398 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2399 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2400 | static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode( |
| 2401 | mbedtls_ssl_mode_t base_mode, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2402 | int encrypt_then_mac) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2403 | { |
| 2404 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2405 | if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED && |
| 2406 | base_mode == MBEDTLS_SSL_MODE_CBC) { |
| 2407 | return MBEDTLS_SSL_MODE_CBC_ETM; |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2408 | } |
| 2409 | #else |
| 2410 | (void) encrypt_then_mac; |
| 2411 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2412 | return base_mode; |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2413 | } |
| 2414 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 2415 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2416 | const mbedtls_ssl_transform *transform) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2417 | { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2418 | mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode( |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2419 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2420 | transform->psa_alg |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2421 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2422 | mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2423 | #endif |
| 2424 | ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2425 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2426 | int encrypt_then_mac = 0; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2427 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2428 | encrypt_then_mac = transform->encrypt_then_mac; |
| 2429 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2430 | return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2431 | } |
| 2432 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 2433 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2434 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2435 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2436 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2437 | const mbedtls_ssl_ciphersuite_t *suite) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2438 | { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2439 | mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM; |
| 2440 | |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2441 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2442 | psa_status_t status; |
| 2443 | psa_algorithm_t alg; |
| 2444 | psa_key_type_t type; |
| 2445 | size_t size; |
Dave Rodgman | 2eab462 | 2023-10-05 13:30:37 +0100 | [diff] [blame] | 2446 | status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) suite->cipher, |
| 2447 | 0, &alg, &type, &size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2448 | if (status == PSA_SUCCESS) { |
| 2449 | base_mode = mbedtls_ssl_get_base_mode(alg); |
| 2450 | } |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2451 | #else |
| 2452 | const mbedtls_cipher_info_t *cipher = |
Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 2453 | mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) suite->cipher); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2454 | if (cipher != NULL) { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2455 | base_mode = |
| 2456 | mbedtls_ssl_get_base_mode( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2457 | mbedtls_cipher_info_get_mode(cipher)); |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2458 | } |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2459 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2460 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2461 | #if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 2462 | int encrypt_then_mac = 0; |
| 2463 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2464 | return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2465 | } |
| 2466 | |
Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 2467 | #if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2468 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2469 | psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type, |
| 2470 | size_t taglen, |
| 2471 | psa_algorithm_t *alg, |
| 2472 | psa_key_type_t *key_type, |
| 2473 | size_t *key_size) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2474 | { |
Yanray Wang | 19e4dc8 | 2023-11-16 18:02:05 +0800 | [diff] [blame] | 2475 | #if !defined(MBEDTLS_SSL_HAVE_CCM) |
| 2476 | (void) taglen; |
| 2477 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2478 | switch (mbedtls_cipher_type) { |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2479 | #if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CBC) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2480 | case MBEDTLS_CIPHER_AES_128_CBC: |
| 2481 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2482 | *key_type = PSA_KEY_TYPE_AES; |
| 2483 | *key_size = 128; |
| 2484 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2485 | #endif |
| 2486 | #if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2487 | case MBEDTLS_CIPHER_AES_128_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2488 | *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] | 2489 | *key_type = PSA_KEY_TYPE_AES; |
| 2490 | *key_size = 128; |
| 2491 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2492 | #endif |
| 2493 | #if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_GCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2494 | case MBEDTLS_CIPHER_AES_128_GCM: |
| 2495 | *alg = PSA_ALG_GCM; |
| 2496 | *key_type = PSA_KEY_TYPE_AES; |
| 2497 | *key_size = 128; |
| 2498 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2499 | #endif |
| 2500 | #if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2501 | case MBEDTLS_CIPHER_AES_192_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2502 | *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] | 2503 | *key_type = PSA_KEY_TYPE_AES; |
| 2504 | *key_size = 192; |
| 2505 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2506 | #endif |
| 2507 | #if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_GCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2508 | case MBEDTLS_CIPHER_AES_192_GCM: |
| 2509 | *alg = PSA_ALG_GCM; |
| 2510 | *key_type = PSA_KEY_TYPE_AES; |
| 2511 | *key_size = 192; |
| 2512 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2513 | #endif |
| 2514 | #if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CBC) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2515 | case MBEDTLS_CIPHER_AES_256_CBC: |
| 2516 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2517 | *key_type = PSA_KEY_TYPE_AES; |
| 2518 | *key_size = 256; |
| 2519 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2520 | #endif |
| 2521 | #if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2522 | case MBEDTLS_CIPHER_AES_256_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2523 | *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] | 2524 | *key_type = PSA_KEY_TYPE_AES; |
| 2525 | *key_size = 256; |
| 2526 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2527 | #endif |
| 2528 | #if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_GCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2529 | case MBEDTLS_CIPHER_AES_256_GCM: |
| 2530 | *alg = PSA_ALG_GCM; |
| 2531 | *key_type = PSA_KEY_TYPE_AES; |
| 2532 | *key_size = 256; |
| 2533 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2534 | #endif |
| 2535 | #if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CBC) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2536 | case MBEDTLS_CIPHER_ARIA_128_CBC: |
| 2537 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2538 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2539 | *key_size = 128; |
| 2540 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2541 | #endif |
| 2542 | #if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2543 | case MBEDTLS_CIPHER_ARIA_128_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2544 | *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] | 2545 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2546 | *key_size = 128; |
| 2547 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2548 | #endif |
| 2549 | #if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_GCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2550 | case MBEDTLS_CIPHER_ARIA_128_GCM: |
| 2551 | *alg = PSA_ALG_GCM; |
| 2552 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2553 | *key_size = 128; |
| 2554 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2555 | #endif |
| 2556 | #if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2557 | case MBEDTLS_CIPHER_ARIA_192_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2558 | *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] | 2559 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2560 | *key_size = 192; |
| 2561 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2562 | #endif |
| 2563 | #if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_GCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2564 | case MBEDTLS_CIPHER_ARIA_192_GCM: |
| 2565 | *alg = PSA_ALG_GCM; |
| 2566 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2567 | *key_size = 192; |
| 2568 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2569 | #endif |
| 2570 | #if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CBC) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2571 | case MBEDTLS_CIPHER_ARIA_256_CBC: |
| 2572 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2573 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2574 | *key_size = 256; |
| 2575 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2576 | #endif |
| 2577 | #if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2578 | case MBEDTLS_CIPHER_ARIA_256_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2579 | *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] | 2580 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2581 | *key_size = 256; |
| 2582 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2583 | #endif |
| 2584 | #if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_GCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2585 | case MBEDTLS_CIPHER_ARIA_256_GCM: |
| 2586 | *alg = PSA_ALG_GCM; |
| 2587 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2588 | *key_size = 256; |
| 2589 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2590 | #endif |
| 2591 | #if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CBC) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2592 | case MBEDTLS_CIPHER_CAMELLIA_128_CBC: |
| 2593 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2594 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2595 | *key_size = 128; |
| 2596 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2597 | #endif |
| 2598 | #if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2599 | case MBEDTLS_CIPHER_CAMELLIA_128_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2600 | *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] | 2601 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2602 | *key_size = 128; |
| 2603 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2604 | #endif |
| 2605 | #if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_GCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2606 | case MBEDTLS_CIPHER_CAMELLIA_128_GCM: |
| 2607 | *alg = PSA_ALG_GCM; |
| 2608 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2609 | *key_size = 128; |
| 2610 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2611 | #endif |
| 2612 | #if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2613 | case MBEDTLS_CIPHER_CAMELLIA_192_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2614 | *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] | 2615 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2616 | *key_size = 192; |
| 2617 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2618 | #endif |
| 2619 | #if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_GCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2620 | case MBEDTLS_CIPHER_CAMELLIA_192_GCM: |
| 2621 | *alg = PSA_ALG_GCM; |
| 2622 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2623 | *key_size = 192; |
| 2624 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2625 | #endif |
| 2626 | #if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CBC) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2627 | case MBEDTLS_CIPHER_CAMELLIA_256_CBC: |
| 2628 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2629 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2630 | *key_size = 256; |
| 2631 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2632 | #endif |
| 2633 | #if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2634 | case MBEDTLS_CIPHER_CAMELLIA_256_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2635 | *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] | 2636 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2637 | *key_size = 256; |
| 2638 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2639 | #endif |
| 2640 | #if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_GCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2641 | case MBEDTLS_CIPHER_CAMELLIA_256_GCM: |
| 2642 | *alg = PSA_ALG_GCM; |
| 2643 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2644 | *key_size = 256; |
| 2645 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2646 | #endif |
| 2647 | #if defined(MBEDTLS_SSL_HAVE_CHACHAPOLY) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2648 | case MBEDTLS_CIPHER_CHACHA20_POLY1305: |
| 2649 | *alg = PSA_ALG_CHACHA20_POLY1305; |
| 2650 | *key_type = PSA_KEY_TYPE_CHACHA20; |
| 2651 | *key_size = 256; |
| 2652 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2653 | #endif |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2654 | case MBEDTLS_CIPHER_NULL: |
| 2655 | *alg = MBEDTLS_SSL_NULL_CIPHER; |
| 2656 | *key_type = 0; |
| 2657 | *key_size = 0; |
| 2658 | break; |
| 2659 | default: |
| 2660 | return PSA_ERROR_NOT_SUPPORTED; |
| 2661 | } |
| 2662 | |
| 2663 | return PSA_SUCCESS; |
| 2664 | } |
Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 2665 | #endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2666 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2667 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2668 | int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf, |
| 2669 | const unsigned char *dhm_P, size_t P_len, |
| 2670 | const unsigned char *dhm_G, size_t G_len) |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2671 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2672 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2673 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2674 | mbedtls_mpi_free(&conf->dhm_P); |
| 2675 | mbedtls_mpi_free(&conf->dhm_G); |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2676 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2677 | if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 || |
| 2678 | (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) { |
| 2679 | mbedtls_mpi_free(&conf->dhm_P); |
| 2680 | mbedtls_mpi_free(&conf->dhm_G); |
| 2681 | return ret; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2682 | } |
| 2683 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2684 | return 0; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2685 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2686 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2687 | 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] | 2688 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2689 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2690 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2691 | mbedtls_mpi_free(&conf->dhm_P); |
| 2692 | mbedtls_mpi_free(&conf->dhm_G); |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2693 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2694 | if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P, |
| 2695 | &conf->dhm_P)) != 0 || |
| 2696 | (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G, |
| 2697 | &conf->dhm_G)) != 0) { |
| 2698 | mbedtls_mpi_free(&conf->dhm_P); |
| 2699 | mbedtls_mpi_free(&conf->dhm_G); |
| 2700 | return ret; |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 2701 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2702 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2703 | return 0; |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2704 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2705 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2706 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 2707 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 2708 | /* |
| 2709 | * Set the minimum length for Diffie-Hellman parameters |
| 2710 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2711 | void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf, |
| 2712 | unsigned int bitlen) |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 2713 | { |
| 2714 | conf->dhm_min_bitlen = bitlen; |
| 2715 | } |
| 2716 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 2717 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2718 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2719 | #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] | 2720 | /* |
| 2721 | * Set allowed/preferred hashes for handshake signatures |
| 2722 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2723 | void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf, |
| 2724 | const int *hashes) |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 2725 | { |
| 2726 | conf->sig_hashes = hashes; |
| 2727 | } |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2728 | #endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2729 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2730 | /* Configure allowed signature algorithms for handshake */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2731 | void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf, |
| 2732 | const uint16_t *sig_algs) |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2733 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2734 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2735 | conf->sig_hashes = NULL; |
| 2736 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 2737 | conf->sig_algs = sig_algs; |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2738 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2739 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 2740 | |
Manuel Pégourié-Gonnard | f07ce3b | 2023-09-22 11:53:41 +0200 | [diff] [blame] | 2741 | #if defined(MBEDTLS_ECP_C) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2742 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2743 | /* |
| 2744 | * Set the allowed elliptic curves |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2745 | * |
| 2746 | * mbedtls_ssl_setup() takes the provided list |
| 2747 | * and translates it to a list of IANA TLS group identifiers, |
| 2748 | * stored in ssl->handshake->group_list. |
| 2749 | * |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2750 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2751 | void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf, |
| 2752 | const mbedtls_ecp_group_id *curve_list) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2753 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2754 | conf->curve_list = curve_list; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2755 | conf->group_list = NULL; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2756 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2757 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Manuel Pégourié-Gonnard | f07ce3b | 2023-09-22 11:53:41 +0200 | [diff] [blame] | 2758 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2759 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2760 | /* |
| 2761 | * Set the allowed groups |
| 2762 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2763 | void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf, |
| 2764 | const uint16_t *group_list) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2765 | { |
Manuel Pégourié-Gonnard | f07ce3b | 2023-09-22 11:53:41 +0200 | [diff] [blame] | 2766 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2767 | conf->curve_list = NULL; |
| 2768 | #endif |
| 2769 | conf->group_list = group_list; |
| 2770 | } |
| 2771 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2772 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | e61852e | 2025-02-12 23:28:48 +0100 | [diff] [blame] | 2773 | |
Gilles Peskine | 6b88594 | 2025-02-12 23:53:25 +0100 | [diff] [blame] | 2774 | /* A magic value for `ssl->hostname` indicating that |
| 2775 | * mbedtls_ssl_set_hostname() has been called with `NULL`. |
| 2776 | * If mbedtls_ssl_set_hostname() has never been called on `ssl`, then |
| 2777 | * `ssl->hostname == NULL`. */ |
| 2778 | static const char *const ssl_hostname_skip_cn_verification = ""; |
| 2779 | |
Gilles Peskine | e61852e | 2025-02-12 23:28:48 +0100 | [diff] [blame] | 2780 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
| 2781 | /** Whether mbedtls_ssl_set_hostname() has been called. |
| 2782 | * |
| 2783 | * \param[in] ssl SSL context |
| 2784 | * |
| 2785 | * \return \c 1 if mbedtls_ssl_set_hostname() has been called on \p ssl |
| 2786 | * (including `mbedtls_ssl_set_hostname(ssl, NULL)`), |
| 2787 | * otherwise \c 0. |
| 2788 | */ |
| 2789 | static int mbedtls_ssl_has_set_hostname_been_called( |
| 2790 | const mbedtls_ssl_context *ssl) |
| 2791 | { |
Gilles Peskine | e61852e | 2025-02-12 23:28:48 +0100 | [diff] [blame] | 2792 | return ssl->hostname != NULL; |
| 2793 | } |
| 2794 | #endif |
| 2795 | |
| 2796 | /* Micro-optimization: don't export this function if it isn't needed outside |
| 2797 | * of this source file. */ |
| 2798 | #if !defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 2799 | static |
| 2800 | #endif |
| 2801 | const char *mbedtls_ssl_get_hostname_pointer(const mbedtls_ssl_context *ssl) |
| 2802 | { |
Gilles Peskine | 6b88594 | 2025-02-12 23:53:25 +0100 | [diff] [blame] | 2803 | if (ssl->hostname == ssl_hostname_skip_cn_verification) { |
| 2804 | return NULL; |
| 2805 | } |
Gilles Peskine | e61852e | 2025-02-12 23:28:48 +0100 | [diff] [blame] | 2806 | return ssl->hostname; |
| 2807 | } |
| 2808 | |
| 2809 | static void mbedtls_ssl_free_hostname(mbedtls_ssl_context *ssl) |
| 2810 | { |
Gilles Peskine | 6b88594 | 2025-02-12 23:53:25 +0100 | [diff] [blame] | 2811 | if (ssl->hostname != NULL && |
| 2812 | ssl->hostname != ssl_hostname_skip_cn_verification) { |
Gilles Peskine | e61852e | 2025-02-12 23:28:48 +0100 | [diff] [blame] | 2813 | mbedtls_zeroize_and_free(ssl->hostname, strlen(ssl->hostname)); |
| 2814 | } |
| 2815 | ssl->hostname = NULL; |
| 2816 | } |
| 2817 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2818 | int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2819 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2820 | /* Initialize to suppress unnecessary compiler warning */ |
| 2821 | size_t hostname_len = 0; |
| 2822 | |
| 2823 | /* Check if new hostname is valid before |
| 2824 | * making any change to current one */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2825 | if (hostname != NULL) { |
| 2826 | hostname_len = strlen(hostname); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2827 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2828 | if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) { |
| 2829 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2830 | } |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2831 | } |
| 2832 | |
| 2833 | /* Now it's clear that we will overwrite the old hostname, |
| 2834 | * so we can free it safely */ |
Gilles Peskine | e61852e | 2025-02-12 23:28:48 +0100 | [diff] [blame] | 2835 | mbedtls_ssl_free_hostname(ssl); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2836 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2837 | if (hostname == NULL) { |
Gilles Peskine | 6b88594 | 2025-02-12 23:53:25 +0100 | [diff] [blame] | 2838 | /* Passing NULL as hostname clears the old one, but leaves a |
| 2839 | * special marker to indicate that mbedtls_ssl_set_hostname() |
| 2840 | * has been called. */ |
| 2841 | /* ssl->hostname should be const, but isn't. We won't actually |
| 2842 | * write to the buffer, so it's ok to cast away the const. */ |
| 2843 | ssl->hostname = (char *) ssl_hostname_skip_cn_verification; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2844 | } else { |
| 2845 | ssl->hostname = mbedtls_calloc(1, hostname_len + 1); |
| 2846 | if (ssl->hostname == NULL) { |
Gilles Peskine | 6b88594 | 2025-02-12 23:53:25 +0100 | [diff] [blame] | 2847 | /* mbedtls_ssl_set_hostname() has been called, but unsuccessfully. |
| 2848 | * Leave ssl->hostname in the same state as if the function had |
| 2849 | * not been called, i.e. a null pointer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2850 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2851 | } |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2852 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2853 | memcpy(ssl->hostname, hostname, hostname_len); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2854 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2855 | ssl->hostname[hostname_len] = '\0'; |
| 2856 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2857 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2858 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2859 | } |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 2860 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2861 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2862 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2863 | void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf, |
| 2864 | int (*f_sni)(void *, mbedtls_ssl_context *, |
| 2865 | const unsigned char *, size_t), |
| 2866 | void *p_sni) |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2867 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2868 | conf->f_sni = f_sni; |
| 2869 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2870 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2871 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2872 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2873 | #if defined(MBEDTLS_SSL_ALPN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2874 | 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] | 2875 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2876 | size_t cur_len, tot_len; |
| 2877 | const char **p; |
| 2878 | |
| 2879 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 2880 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 2881 | * MUST NOT be truncated." |
| 2882 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2883 | */ |
| 2884 | tot_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2885 | for (p = protos; *p != NULL; p++) { |
| 2886 | cur_len = strlen(*p); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2887 | tot_len += cur_len; |
| 2888 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2889 | if ((cur_len == 0) || |
| 2890 | (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) || |
| 2891 | (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) { |
| 2892 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2893 | } |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2894 | } |
| 2895 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2896 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2897 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2898 | return 0; |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2899 | } |
| 2900 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2901 | 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] | 2902 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2903 | return ssl->alpn_chosen; |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2904 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2905 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2906 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2907 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2908 | void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf, |
| 2909 | int support_mki_value) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2910 | { |
| 2911 | conf->dtls_srtp_mki_support = support_mki_value; |
| 2912 | } |
| 2913 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2914 | int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl, |
| 2915 | unsigned char *mki_value, |
| 2916 | uint16_t mki_len) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2917 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2918 | if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) { |
| 2919 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2920 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2921 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2922 | if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) { |
| 2923 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2924 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2925 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2926 | memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len); |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2927 | ssl->dtls_srtp_info.mki_len = mki_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2928 | return 0; |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2929 | } |
| 2930 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2931 | int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf, |
| 2932 | const mbedtls_ssl_srtp_profile *profiles) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2933 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2934 | const mbedtls_ssl_srtp_profile *p; |
| 2935 | size_t list_size = 0; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2936 | |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2937 | /* check the profiles list: all entry must be valid, |
| 2938 | * 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] | 2939 | for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET && |
| 2940 | list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH; |
| 2941 | p++) { |
| 2942 | if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) { |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 2943 | list_size++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2944 | } else { |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 2945 | /* unsupported value, stop parsing and set the size to an error value */ |
| 2946 | list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2947 | } |
| 2948 | } |
| 2949 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2950 | if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) { |
| 2951 | conf->dtls_srtp_profile_list = NULL; |
| 2952 | conf->dtls_srtp_profile_list_len = 0; |
| 2953 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2954 | } |
| 2955 | |
Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 2956 | conf->dtls_srtp_profile_list = profiles; |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2957 | conf->dtls_srtp_profile_list_len = list_size; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2958 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2959 | return 0; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2960 | } |
| 2961 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2962 | void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl, |
| 2963 | mbedtls_dtls_srtp_info *dtls_srtp_info) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2964 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2965 | dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile; |
| 2966 | /* do not copy the mki value if there is no chosen profile */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2967 | if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2968 | dtls_srtp_info->mki_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2969 | } else { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2970 | dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2971 | memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value, |
| 2972 | ssl->dtls_srtp_info.mki_len); |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2973 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2974 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2975 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 2976 | |
Aditya Patwardhan | 3096f33 | 2022-07-26 14:31:46 +0530 | [diff] [blame] | 2977 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2978 | 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] | 2979 | { |
Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 2980 | conf->max_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor); |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 2981 | } |
| 2982 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2983 | 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] | 2984 | { |
Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 2985 | conf->min_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor); |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2986 | } |
Aditya Patwardhan | 3096f33 | 2022-07-26 14:31:46 +0530 | [diff] [blame] | 2987 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2988 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 2989 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2990 | void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf, |
| 2991 | char cert_req_ca_list) |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 2992 | { |
| 2993 | conf->cert_req_ca_list = cert_req_ca_list; |
| 2994 | } |
| 2995 | #endif |
| 2996 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2997 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2998 | 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] | 2999 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3000 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 3001 | } |
| 3002 | #endif |
| 3003 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3004 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3005 | 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] | 3006 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3007 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 3008 | } |
| 3009 | #endif |
| 3010 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3011 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3012 | 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] | 3013 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3014 | if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
| 3015 | ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) { |
| 3016 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 3017 | } |
| 3018 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 3019 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 3020 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3021 | return 0; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 3022 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3023 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 3024 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3025 | 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] | 3026 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3027 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3028 | } |
| 3029 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3030 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3031 | 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] | 3032 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3033 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 3034 | } |
| 3035 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3036 | 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] | 3037 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3038 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 3039 | } |
| 3040 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3041 | void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf, |
| 3042 | const unsigned char period[8]) |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 3043 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3044 | memcpy(conf->renego_period, period, 8); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 3045 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3046 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3047 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3048 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3049 | #if defined(MBEDTLS_SSL_CLI_C) |
Ronald Cron | d67f801 | 2024-08-28 07:45:57 +0200 | [diff] [blame] | 3050 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3051 | 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] | 3052 | { |
Ronald Cron | d67f801 | 2024-08-28 07:45:57 +0200 | [diff] [blame] | 3053 | conf->session_tickets &= ~MBEDTLS_SSL_SESSION_TICKETS_TLS1_2_MASK; |
| 3054 | conf->session_tickets |= (use_tickets != 0) << |
| 3055 | MBEDTLS_SSL_SESSION_TICKETS_TLS1_2_BIT; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 3056 | } |
Ronald Cron | d67f801 | 2024-08-28 07:45:57 +0200 | [diff] [blame] | 3057 | |
Ronald Cron | bedddd7 | 2024-08-27 14:18:50 +0200 | [diff] [blame] | 3058 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Ronald Cron | 9f44c88 | 2024-08-28 16:44:10 +0200 | [diff] [blame] | 3059 | void mbedtls_ssl_conf_tls13_enable_signal_new_session_tickets( |
| 3060 | mbedtls_ssl_config *conf, int signal_new_session_tickets) |
Ronald Cron | bedddd7 | 2024-08-27 14:18:50 +0200 | [diff] [blame] | 3061 | { |
Ronald Cron | d67f801 | 2024-08-28 07:45:57 +0200 | [diff] [blame] | 3062 | conf->session_tickets &= ~MBEDTLS_SSL_SESSION_TICKETS_TLS1_3_MASK; |
Ronald Cron | 9f44c88 | 2024-08-28 16:44:10 +0200 | [diff] [blame] | 3063 | conf->session_tickets |= (signal_new_session_tickets != 0) << |
Ronald Cron | d67f801 | 2024-08-28 07:45:57 +0200 | [diff] [blame] | 3064 | MBEDTLS_SSL_SESSION_TICKETS_TLS1_3_BIT; |
| 3065 | } |
Ronald Cron | bedddd7 | 2024-08-27 14:18:50 +0200 | [diff] [blame] | 3066 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 3067 | #endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 3068 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3069 | #if defined(MBEDTLS_SSL_SRV_C) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 3070 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 3071 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3072 | void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf, |
| 3073 | uint16_t num_tickets) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 3074 | { |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 3075 | conf->new_session_tickets_count = num_tickets; |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 3076 | } |
| 3077 | #endif |
| 3078 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3079 | void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf, |
| 3080 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 3081 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 3082 | void *p_ticket) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 3083 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 3084 | conf->f_ticket_write = f_ticket_write; |
| 3085 | conf->f_ticket_parse = f_ticket_parse; |
| 3086 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 3087 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3088 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3089 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 3090 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3091 | void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl, |
| 3092 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 3093 | void *p_export_keys) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 3094 | { |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 3095 | ssl->f_export_keys = f_export_keys; |
| 3096 | ssl->p_export_keys = p_export_keys; |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 3097 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 3098 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3099 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3100 | void mbedtls_ssl_conf_async_private_cb( |
| 3101 | mbedtls_ssl_config *conf, |
| 3102 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 3103 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 3104 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 3105 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3106 | void *async_config_data) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3107 | { |
| 3108 | conf->f_async_sign_start = f_async_sign; |
| 3109 | conf->f_async_decrypt_start = f_async_decrypt; |
| 3110 | conf->f_async_resume = f_async_resume; |
| 3111 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3112 | conf->p_async_config_data = async_config_data; |
| 3113 | } |
| 3114 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3115 | 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] | 3116 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3117 | return conf->p_async_config_data; |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3118 | } |
| 3119 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3120 | void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3121 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3122 | if (ssl->handshake == NULL) { |
| 3123 | return NULL; |
| 3124 | } else { |
| 3125 | return ssl->handshake->user_async_ctx; |
| 3126 | } |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3127 | } |
| 3128 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3129 | void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl, |
| 3130 | void *ctx) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3131 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3132 | if (ssl->handshake != NULL) { |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3133 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3134 | } |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3135 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3136 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3137 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3138 | /* |
| 3139 | * SSL get accessors |
| 3140 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3141 | uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3142 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3143 | if (ssl->session != NULL) { |
| 3144 | return ssl->session->verify_result; |
| 3145 | } |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 3146 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3147 | if (ssl->session_negotiate != NULL) { |
| 3148 | return ssl->session_negotiate->verify_result; |
| 3149 | } |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 3150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3151 | return 0xFFFFFFFF; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3152 | } |
| 3153 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3154 | 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] | 3155 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3156 | if (ssl == NULL || ssl->session == NULL) { |
| 3157 | return 0; |
| 3158 | } |
Glenn Strauss | 8f52690 | 2022-01-13 00:04:49 -0500 | [diff] [blame] | 3159 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3160 | return ssl->session->ciphersuite; |
Glenn Strauss | 8f52690 | 2022-01-13 00:04:49 -0500 | [diff] [blame] | 3161 | } |
| 3162 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3163 | const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 3164 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3165 | if (ssl == NULL || ssl->session == NULL) { |
| 3166 | return NULL; |
| 3167 | } |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 3168 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3169 | return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 3170 | } |
| 3171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3172 | const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3173 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3174 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3175 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 3176 | switch (ssl->tls_version) { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3177 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3178 | return "DTLSv1.2"; |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 3179 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3180 | return "unknown (DTLS)"; |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 3181 | } |
| 3182 | } |
| 3183 | #endif |
| 3184 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3185 | switch (ssl->tls_version) { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3186 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3187 | return "TLSv1.2"; |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3188 | case MBEDTLS_SSL_VERSION_TLS1_3: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3189 | return "TLSv1.3"; |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3190 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3191 | return "unknown"; |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3192 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3193 | } |
| 3194 | |
Waleed Elmelegy | 6a971fd | 2023-12-28 17:48:16 +0000 | [diff] [blame] | 3195 | #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) |
| 3196 | |
| 3197 | size_t mbedtls_ssl_get_output_record_size_limit(const mbedtls_ssl_context *ssl) |
| 3198 | { |
| 3199 | const size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 3200 | size_t record_size_limit = max_len; |
| 3201 | |
| 3202 | if (ssl->session != NULL && |
| 3203 | ssl->session->record_size_limit >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN && |
| 3204 | ssl->session->record_size_limit < max_len) { |
| 3205 | record_size_limit = ssl->session->record_size_limit; |
| 3206 | } |
| 3207 | |
| 3208 | // TODO: this is currently untested |
| 3209 | /* During a handshake, use the value being negotiated */ |
| 3210 | if (ssl->session_negotiate != NULL && |
| 3211 | ssl->session_negotiate->record_size_limit >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN && |
| 3212 | ssl->session_negotiate->record_size_limit < max_len) { |
| 3213 | record_size_limit = ssl->session_negotiate->record_size_limit; |
| 3214 | } |
| 3215 | |
| 3216 | return record_size_limit; |
| 3217 | } |
| 3218 | #endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */ |
| 3219 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 3220 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3221 | 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] | 3222 | { |
David Horstmann | 95d516f | 2021-05-04 18:36:56 +0100 | [diff] [blame] | 3223 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3224 | size_t read_mfl; |
| 3225 | |
Jerry Yu | ddda050 | 2022-12-01 19:43:12 +0800 | [diff] [blame] | 3226 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3227 | /* 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] | 3228 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 3229 | ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) { |
| 3230 | return ssl_mfl_code_to_length(ssl->conf->mfl_code); |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3231 | } |
Jerry Yu | ddda050 | 2022-12-01 19:43:12 +0800 | [diff] [blame] | 3232 | #endif |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3233 | |
| 3234 | /* Check if a smaller max length was negotiated */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3235 | if (ssl->session_out != NULL) { |
| 3236 | read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code); |
| 3237 | if (read_mfl < max_len) { |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3238 | max_len = read_mfl; |
| 3239 | } |
| 3240 | } |
| 3241 | |
Jerry Yu | ddda050 | 2022-12-01 19:43:12 +0800 | [diff] [blame] | 3242 | /* During a handshake, use the value being negotiated */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3243 | if (ssl->session_negotiate != NULL) { |
| 3244 | read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code); |
| 3245 | if (read_mfl < max_len) { |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3246 | max_len = read_mfl; |
| 3247 | } |
| 3248 | } |
| 3249 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3250 | return max_len; |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3251 | } |
| 3252 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3253 | 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] | 3254 | { |
| 3255 | size_t max_len; |
| 3256 | |
| 3257 | /* |
| 3258 | * Assume mfl_code is correct since it was checked when set |
| 3259 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3260 | 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] | 3261 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 3262 | /* Check if a smaller max length was negotiated */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3263 | if (ssl->session_out != NULL && |
| 3264 | ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) { |
| 3265 | 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] | 3266 | } |
| 3267 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 3268 | /* During a handshake, use the value being negotiated */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3269 | if (ssl->session_negotiate != NULL && |
| 3270 | ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) { |
| 3271 | 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] | 3272 | } |
| 3273 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3274 | return max_len; |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 3275 | } |
| 3276 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 3277 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3278 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3279 | 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] | 3280 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 3281 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3282 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 3283 | (ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 3284 | ssl->state == MBEDTLS_SSL_SERVER_HELLO)) { |
| 3285 | return 0; |
| 3286 | } |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 3287 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3288 | if (ssl->handshake == NULL || ssl->handshake->mtu == 0) { |
| 3289 | return ssl->mtu; |
| 3290 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3291 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3292 | if (ssl->mtu == 0) { |
| 3293 | return ssl->handshake->mtu; |
| 3294 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3295 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3296 | return ssl->mtu < ssl->handshake->mtu ? |
| 3297 | ssl->mtu : ssl->handshake->mtu; |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3298 | } |
| 3299 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3300 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3301 | 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] | 3302 | { |
| 3303 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 3304 | |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 3305 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
Jan Bruckner | f482dcc | 2023-03-15 09:09:06 +0100 | [diff] [blame] | 3306 | !defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) && \ |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 3307 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3308 | (void) ssl; |
| 3309 | #endif |
| 3310 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3311 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3312 | 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] | 3313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3314 | if (max_len > mfl) { |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3315 | max_len = mfl; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3316 | } |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3317 | #endif |
| 3318 | |
Jan Bruckner | f482dcc | 2023-03-15 09:09:06 +0100 | [diff] [blame] | 3319 | #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) |
| 3320 | const size_t record_size_limit = mbedtls_ssl_get_output_record_size_limit(ssl); |
| 3321 | |
| 3322 | if (max_len > record_size_limit) { |
| 3323 | max_len = record_size_limit; |
| 3324 | } |
| 3325 | #endif |
| 3326 | |
Waleed Elmelegy | 6a971fd | 2023-12-28 17:48:16 +0000 | [diff] [blame] | 3327 | if (ssl->transform_out != NULL && |
| 3328 | ssl->transform_out->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Waleed Elmelegy | f501790 | 2024-01-09 14:18:34 +0000 | [diff] [blame] | 3329 | /* |
| 3330 | * In TLS 1.3 case, when records are protected, `max_len` as computed |
| 3331 | * above is the maximum length of the TLSInnerPlaintext structure that |
| 3332 | * along the plaintext payload contains the inner content type (one byte) |
| 3333 | * and some zero padding. Given the algorithm used for padding |
| 3334 | * in mbedtls_ssl_encrypt_buf(), compute the maximum length for |
| 3335 | * the plaintext payload. Round down to a multiple of |
| 3336 | * MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY and |
| 3337 | * subtract 1. |
Waleed Elmelegy | 6a971fd | 2023-12-28 17:48:16 +0000 | [diff] [blame] | 3338 | */ |
| 3339 | max_len = ((max_len / MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) * |
| 3340 | MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) - 1; |
| 3341 | } |
| 3342 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3343 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3344 | if (mbedtls_ssl_get_current_mtu(ssl) != 0) { |
| 3345 | const size_t mtu = mbedtls_ssl_get_current_mtu(ssl); |
| 3346 | const int ret = mbedtls_ssl_get_record_expansion(ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3347 | const size_t overhead = (size_t) ret; |
| 3348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3349 | if (ret < 0) { |
| 3350 | return ret; |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3351 | } |
| 3352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3353 | if (mtu <= overhead) { |
| 3354 | MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion")); |
| 3355 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 3356 | } |
| 3357 | |
| 3358 | if (max_len > mtu - overhead) { |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3359 | max_len = mtu - overhead; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3360 | } |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3361 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3362 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3363 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 3364 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
Waleed Elmelegy | 049cd30 | 2023-12-20 17:28:31 +0000 | [diff] [blame] | 3365 | !defined(MBEDTLS_SSL_PROTO_DTLS) && \ |
| 3366 | !defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 3367 | ((void) ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3368 | #endif |
| 3369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3370 | return (int) max_len; |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3371 | } |
| 3372 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3373 | 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] | 3374 | { |
| 3375 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
| 3376 | |
| 3377 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 3378 | (void) ssl; |
| 3379 | #endif |
| 3380 | |
| 3381 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3382 | const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl); |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 3383 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3384 | if (max_len > mfl) { |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 3385 | max_len = mfl; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3386 | } |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 3387 | #endif |
| 3388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3389 | return (int) max_len; |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 3390 | } |
| 3391 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3392 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3393 | 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] | 3394 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3395 | if (ssl == NULL || ssl->session == NULL) { |
| 3396 | return NULL; |
| 3397 | } |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3398 | |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 3399 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3400 | return ssl->session->peer_cert; |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 3401 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3402 | return NULL; |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 3403 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3404 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3405 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3406 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3407 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3408 | int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl, |
| 3409 | mbedtls_ssl_session *dst) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3410 | { |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3411 | int ret; |
| 3412 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3413 | if (ssl == NULL || |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3414 | dst == NULL || |
| 3415 | ssl->session == NULL || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3416 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) { |
| 3417 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3418 | } |
| 3419 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3420 | /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer |
| 3421 | * idempotent: Each session can only be exported once. |
| 3422 | * |
| 3423 | * (This is in preparation for TLS 1.3 support where we will |
| 3424 | * need the ability to export multiple sessions (aka tickets), |
| 3425 | * which will be achieved by calling mbedtls_ssl_get_session() |
| 3426 | * multiple times until it fails.) |
| 3427 | * |
| 3428 | * Check whether we have already exported the current session, |
| 3429 | * and fail if so. |
| 3430 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3431 | if (ssl->session->exported == 1) { |
| 3432 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 3433 | } |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3434 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3435 | ret = mbedtls_ssl_session_copy(dst, ssl->session); |
| 3436 | if (ret != 0) { |
| 3437 | return ret; |
| 3438 | } |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3439 | |
| 3440 | /* Remember that we've exported the session. */ |
| 3441 | ssl->session->exported = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3442 | return 0; |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3443 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3444 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3445 | |
David Horstmann | cb01b36 | 2024-02-29 17:31:46 +0000 | [diff] [blame] | 3446 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3447 | |
| 3448 | /* Serialization of TLS 1.2 sessions |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3449 | * |
David Horstmann | cb01b36 | 2024-02-29 17:31:46 +0000 | [diff] [blame] | 3450 | * For more detail, see the description of ssl_session_save(). |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3451 | */ |
| 3452 | static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session, |
| 3453 | unsigned char *buf, |
| 3454 | size_t buf_len) |
| 3455 | { |
| 3456 | unsigned char *p = buf; |
| 3457 | size_t used = 0; |
| 3458 | |
| 3459 | #if defined(MBEDTLS_HAVE_TIME) |
| 3460 | uint64_t start; |
| 3461 | #endif |
| 3462 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3463 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3464 | size_t cert_len; |
| 3465 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3466 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 3467 | |
| 3468 | /* |
| 3469 | * Time |
| 3470 | */ |
| 3471 | #if defined(MBEDTLS_HAVE_TIME) |
| 3472 | used += 8; |
| 3473 | |
| 3474 | if (used <= buf_len) { |
| 3475 | start = (uint64_t) session->start; |
| 3476 | |
| 3477 | MBEDTLS_PUT_UINT64_BE(start, p, 0); |
| 3478 | p += 8; |
| 3479 | } |
| 3480 | #endif /* MBEDTLS_HAVE_TIME */ |
| 3481 | |
| 3482 | /* |
| 3483 | * Basic mandatory fields |
| 3484 | */ |
| 3485 | used += 1 /* id_len */ |
| 3486 | + sizeof(session->id) |
| 3487 | + sizeof(session->master) |
| 3488 | + 4; /* verify_result */ |
| 3489 | |
| 3490 | if (used <= buf_len) { |
| 3491 | *p++ = MBEDTLS_BYTE_0(session->id_len); |
| 3492 | memcpy(p, session->id, 32); |
| 3493 | p += 32; |
| 3494 | |
| 3495 | memcpy(p, session->master, 48); |
| 3496 | p += 48; |
| 3497 | |
| 3498 | MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0); |
| 3499 | p += 4; |
| 3500 | } |
| 3501 | |
| 3502 | /* |
| 3503 | * Peer's end-entity certificate |
| 3504 | */ |
| 3505 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3506 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3507 | if (session->peer_cert == NULL) { |
| 3508 | cert_len = 0; |
| 3509 | } else { |
| 3510 | cert_len = session->peer_cert->raw.len; |
| 3511 | } |
| 3512 | |
| 3513 | used += 3 + cert_len; |
| 3514 | |
| 3515 | if (used <= buf_len) { |
| 3516 | *p++ = MBEDTLS_BYTE_2(cert_len); |
| 3517 | *p++ = MBEDTLS_BYTE_1(cert_len); |
| 3518 | *p++ = MBEDTLS_BYTE_0(cert_len); |
| 3519 | |
| 3520 | if (session->peer_cert != NULL) { |
| 3521 | memcpy(p, session->peer_cert->raw.p, cert_len); |
| 3522 | p += cert_len; |
| 3523 | } |
| 3524 | } |
| 3525 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3526 | if (session->peer_cert_digest != NULL) { |
| 3527 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; |
| 3528 | if (used <= buf_len) { |
| 3529 | *p++ = (unsigned char) session->peer_cert_digest_type; |
| 3530 | *p++ = (unsigned char) session->peer_cert_digest_len; |
| 3531 | memcpy(p, session->peer_cert_digest, |
| 3532 | session->peer_cert_digest_len); |
| 3533 | p += session->peer_cert_digest_len; |
| 3534 | } |
| 3535 | } else { |
| 3536 | used += 2; |
| 3537 | if (used <= buf_len) { |
| 3538 | *p++ = (unsigned char) MBEDTLS_MD_NONE; |
| 3539 | *p++ = 0; |
| 3540 | } |
| 3541 | } |
| 3542 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3543 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 3544 | |
| 3545 | /* |
| 3546 | * Session ticket if any, plus associated data |
| 3547 | */ |
| 3548 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 3549 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3550 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 3551 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ |
| 3552 | |
| 3553 | if (used <= buf_len) { |
| 3554 | *p++ = MBEDTLS_BYTE_2(session->ticket_len); |
| 3555 | *p++ = MBEDTLS_BYTE_1(session->ticket_len); |
| 3556 | *p++ = MBEDTLS_BYTE_0(session->ticket_len); |
| 3557 | |
| 3558 | if (session->ticket != NULL) { |
| 3559 | memcpy(p, session->ticket, session->ticket_len); |
| 3560 | p += session->ticket_len; |
| 3561 | } |
| 3562 | |
| 3563 | MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0); |
| 3564 | p += 4; |
| 3565 | } |
| 3566 | } |
| 3567 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 3568 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
| 3569 | if (session->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 3570 | used += 8; |
| 3571 | |
| 3572 | if (used <= buf_len) { |
| 3573 | MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_creation_time, p, 0); |
| 3574 | p += 8; |
| 3575 | } |
| 3576 | } |
| 3577 | #endif /* MBEDTLS_HAVE_TIME && MBEDTLS_SSL_SRV_C */ |
| 3578 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 3579 | |
| 3580 | /* |
| 3581 | * Misc extension-related info |
| 3582 | */ |
| 3583 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 3584 | used += 1; |
| 3585 | |
| 3586 | if (used <= buf_len) { |
| 3587 | *p++ = session->mfl_code; |
| 3588 | } |
| 3589 | #endif |
| 3590 | |
| 3591 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 3592 | used += 1; |
| 3593 | |
| 3594 | if (used <= buf_len) { |
| 3595 | *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac); |
| 3596 | } |
| 3597 | #endif |
| 3598 | |
| 3599 | return used; |
| 3600 | } |
| 3601 | |
| 3602 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 3603 | static int ssl_tls12_session_load(mbedtls_ssl_session *session, |
| 3604 | const unsigned char *buf, |
| 3605 | size_t len) |
| 3606 | { |
| 3607 | #if defined(MBEDTLS_HAVE_TIME) |
| 3608 | uint64_t start; |
| 3609 | #endif |
| 3610 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3611 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3612 | size_t cert_len; |
| 3613 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3614 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 3615 | |
| 3616 | const unsigned char *p = buf; |
| 3617 | const unsigned char * const end = buf + len; |
| 3618 | |
| 3619 | /* |
| 3620 | * Time |
| 3621 | */ |
| 3622 | #if defined(MBEDTLS_HAVE_TIME) |
| 3623 | if (8 > (size_t) (end - p)) { |
| 3624 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3625 | } |
| 3626 | |
| 3627 | start = MBEDTLS_GET_UINT64_BE(p, 0); |
| 3628 | p += 8; |
| 3629 | |
| 3630 | session->start = (time_t) start; |
| 3631 | #endif /* MBEDTLS_HAVE_TIME */ |
| 3632 | |
| 3633 | /* |
| 3634 | * Basic mandatory fields |
| 3635 | */ |
| 3636 | if (1 + 32 + 48 + 4 > (size_t) (end - p)) { |
| 3637 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3638 | } |
| 3639 | |
| 3640 | session->id_len = *p++; |
| 3641 | memcpy(session->id, p, 32); |
| 3642 | p += 32; |
| 3643 | |
| 3644 | memcpy(session->master, p, 48); |
| 3645 | p += 48; |
| 3646 | |
| 3647 | session->verify_result = MBEDTLS_GET_UINT32_BE(p, 0); |
| 3648 | p += 4; |
| 3649 | |
| 3650 | /* Immediately clear invalid pointer values that have been read, in case |
| 3651 | * we exit early before we replaced them with valid ones. */ |
| 3652 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3653 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3654 | session->peer_cert = NULL; |
| 3655 | #else |
| 3656 | session->peer_cert_digest = NULL; |
| 3657 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3658 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 3659 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 3660 | session->ticket = NULL; |
| 3661 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 3662 | |
| 3663 | /* |
| 3664 | * Peer certificate |
| 3665 | */ |
| 3666 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3667 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3668 | /* Deserialize CRT from the end of the ticket. */ |
| 3669 | if (3 > (size_t) (end - p)) { |
| 3670 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3671 | } |
| 3672 | |
| 3673 | cert_len = MBEDTLS_GET_UINT24_BE(p, 0); |
| 3674 | p += 3; |
| 3675 | |
| 3676 | if (cert_len != 0) { |
| 3677 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3678 | |
| 3679 | if (cert_len > (size_t) (end - p)) { |
| 3680 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3681 | } |
| 3682 | |
| 3683 | session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt)); |
| 3684 | |
| 3685 | if (session->peer_cert == NULL) { |
| 3686 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 3687 | } |
| 3688 | |
| 3689 | mbedtls_x509_crt_init(session->peer_cert); |
| 3690 | |
| 3691 | if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert, |
| 3692 | p, cert_len)) != 0) { |
| 3693 | mbedtls_x509_crt_free(session->peer_cert); |
| 3694 | mbedtls_free(session->peer_cert); |
| 3695 | session->peer_cert = NULL; |
| 3696 | return ret; |
| 3697 | } |
| 3698 | |
| 3699 | p += cert_len; |
| 3700 | } |
| 3701 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3702 | /* Deserialize CRT digest from the end of the ticket. */ |
| 3703 | if (2 > (size_t) (end - p)) { |
| 3704 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3705 | } |
| 3706 | |
| 3707 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; |
| 3708 | session->peer_cert_digest_len = (size_t) *p++; |
| 3709 | |
| 3710 | if (session->peer_cert_digest_len != 0) { |
| 3711 | const mbedtls_md_info_t *md_info = |
| 3712 | mbedtls_md_info_from_type(session->peer_cert_digest_type); |
| 3713 | if (md_info == NULL) { |
| 3714 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3715 | } |
| 3716 | if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) { |
| 3717 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3718 | } |
| 3719 | |
| 3720 | if (session->peer_cert_digest_len > (size_t) (end - p)) { |
| 3721 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3722 | } |
| 3723 | |
| 3724 | session->peer_cert_digest = |
| 3725 | mbedtls_calloc(1, session->peer_cert_digest_len); |
| 3726 | if (session->peer_cert_digest == NULL) { |
| 3727 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 3728 | } |
| 3729 | |
| 3730 | memcpy(session->peer_cert_digest, p, |
| 3731 | session->peer_cert_digest_len); |
| 3732 | p += session->peer_cert_digest_len; |
| 3733 | } |
| 3734 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3735 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 3736 | |
| 3737 | /* |
| 3738 | * Session ticket and associated data |
| 3739 | */ |
| 3740 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 3741 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3742 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 3743 | if (3 > (size_t) (end - p)) { |
| 3744 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3745 | } |
| 3746 | |
| 3747 | session->ticket_len = MBEDTLS_GET_UINT24_BE(p, 0); |
| 3748 | p += 3; |
| 3749 | |
| 3750 | if (session->ticket_len != 0) { |
| 3751 | if (session->ticket_len > (size_t) (end - p)) { |
| 3752 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3753 | } |
| 3754 | |
| 3755 | session->ticket = mbedtls_calloc(1, session->ticket_len); |
| 3756 | if (session->ticket == NULL) { |
| 3757 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 3758 | } |
| 3759 | |
| 3760 | memcpy(session->ticket, p, session->ticket_len); |
| 3761 | p += session->ticket_len; |
| 3762 | } |
| 3763 | |
| 3764 | if (4 > (size_t) (end - p)) { |
| 3765 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3766 | } |
| 3767 | |
| 3768 | session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0); |
| 3769 | p += 4; |
| 3770 | } |
| 3771 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 3772 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
| 3773 | if (session->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 3774 | if (8 > (size_t) (end - p)) { |
| 3775 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3776 | } |
| 3777 | session->ticket_creation_time = MBEDTLS_GET_UINT64_BE(p, 0); |
| 3778 | p += 8; |
| 3779 | } |
| 3780 | #endif /* MBEDTLS_HAVE_TIME && MBEDTLS_SSL_SRV_C */ |
| 3781 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 3782 | |
| 3783 | /* |
| 3784 | * Misc extension-related info |
| 3785 | */ |
| 3786 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 3787 | if (1 > (size_t) (end - p)) { |
| 3788 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3789 | } |
| 3790 | |
| 3791 | session->mfl_code = *p++; |
| 3792 | #endif |
| 3793 | |
| 3794 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 3795 | if (1 > (size_t) (end - p)) { |
| 3796 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3797 | } |
| 3798 | |
| 3799 | session->encrypt_then_mac = *p++; |
| 3800 | #endif |
| 3801 | |
| 3802 | /* Done, should have consumed entire buffer */ |
| 3803 | if (p != end) { |
| 3804 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3805 | } |
| 3806 | |
| 3807 | return 0; |
| 3808 | } |
| 3809 | |
| 3810 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3811 | |
| 3812 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3813 | /* Serialization of TLS 1.3 sessions: |
| 3814 | * |
David Horstmann | cb01b36 | 2024-02-29 17:31:46 +0000 | [diff] [blame] | 3815 | * For more detail, see the description of ssl_session_save(). |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3816 | */ |
| 3817 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 3818 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 3819 | static int ssl_tls13_session_save(const mbedtls_ssl_session *session, |
| 3820 | unsigned char *buf, |
| 3821 | size_t buf_len, |
| 3822 | size_t *olen) |
| 3823 | { |
| 3824 | unsigned char *p = buf; |
| 3825 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
| 3826 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 3827 | size_t hostname_len = (session->hostname == NULL) ? |
| 3828 | 0 : strlen(session->hostname) + 1; |
| 3829 | #endif |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3830 | |
| 3831 | #if defined(MBEDTLS_SSL_SRV_C) && \ |
| 3832 | defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) |
Waleed Elmelegy | daa4da7 | 2024-03-13 16:25:34 +0000 | [diff] [blame] | 3833 | const size_t alpn_len = (session->ticket_alpn == NULL) ? |
Waleed Elmelegy | b28ab0a | 2024-03-13 16:48:28 +0000 | [diff] [blame] | 3834 | 0 : strlen(session->ticket_alpn) + 1; |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3835 | #endif |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3836 | size_t needed = 4 /* ticket_age_add */ |
| 3837 | + 1 /* ticket_flags */ |
| 3838 | + 1; /* resumption_key length */ |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3839 | |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3840 | *olen = 0; |
| 3841 | |
| 3842 | if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) { |
| 3843 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3844 | } |
| 3845 | needed += session->resumption_key_len; /* resumption_key */ |
| 3846 | |
| 3847 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 3848 | needed += 4; /* max_early_data_size */ |
| 3849 | #endif |
| 3850 | #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) |
| 3851 | needed += 2; /* record_size_limit */ |
| 3852 | #endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */ |
| 3853 | |
| 3854 | #if defined(MBEDTLS_HAVE_TIME) |
| 3855 | needed += 8; /* ticket_creation_time or ticket_reception_time */ |
| 3856 | #endif |
| 3857 | |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3858 | #if defined(MBEDTLS_SSL_SRV_C) |
| 3859 | if (session->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 3860 | #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) |
Waleed Elmelegy | daa4da7 | 2024-03-13 16:25:34 +0000 | [diff] [blame] | 3861 | needed += 2 /* alpn_len */ |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3862 | + alpn_len; /* alpn */ |
| 3863 | #endif |
| 3864 | } |
| 3865 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 3866 | |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3867 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3868 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 3869 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 3870 | needed += 2 /* hostname_len */ |
| 3871 | + hostname_len; /* hostname */ |
| 3872 | #endif |
| 3873 | |
| 3874 | needed += 4 /* ticket_lifetime */ |
| 3875 | + 2; /* ticket_len */ |
| 3876 | |
| 3877 | /* Check size_t overflow */ |
| 3878 | if (session->ticket_len > SIZE_MAX - needed) { |
| 3879 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3880 | } |
| 3881 | |
| 3882 | needed += session->ticket_len; /* ticket */ |
| 3883 | } |
| 3884 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 3885 | |
| 3886 | *olen = needed; |
| 3887 | if (needed > buf_len) { |
| 3888 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 3889 | } |
| 3890 | |
| 3891 | MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 0); |
| 3892 | p[4] = session->ticket_flags; |
| 3893 | |
| 3894 | /* save resumption_key */ |
| 3895 | p[5] = session->resumption_key_len; |
| 3896 | p += 6; |
| 3897 | memcpy(p, session->resumption_key, session->resumption_key_len); |
| 3898 | p += session->resumption_key_len; |
| 3899 | |
| 3900 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 3901 | MBEDTLS_PUT_UINT32_BE(session->max_early_data_size, p, 0); |
| 3902 | p += 4; |
| 3903 | #endif |
| 3904 | #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) |
| 3905 | MBEDTLS_PUT_UINT16_BE(session->record_size_limit, p, 0); |
| 3906 | p += 2; |
| 3907 | #endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */ |
| 3908 | |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3909 | #if defined(MBEDTLS_SSL_SRV_C) |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3910 | if (session->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3911 | #if defined(MBEDTLS_HAVE_TIME) |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3912 | MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_creation_time, p, 0); |
| 3913 | p += 8; |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3914 | #endif /* MBEDTLS_HAVE_TIME */ |
| 3915 | |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3916 | #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) |
Waleed Elmelegy | daa4da7 | 2024-03-13 16:25:34 +0000 | [diff] [blame] | 3917 | MBEDTLS_PUT_UINT16_BE(alpn_len, p, 0); |
| 3918 | p += 2; |
| 3919 | |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3920 | if (alpn_len > 0) { |
| 3921 | /* save chosen alpn */ |
Waleed Elmelegy | 883f77c | 2024-03-06 19:09:41 +0000 | [diff] [blame] | 3922 | memcpy(p, session->ticket_alpn, alpn_len); |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3923 | p += alpn_len; |
| 3924 | } |
| 3925 | #endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */ |
| 3926 | } |
| 3927 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 3928 | |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3929 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3930 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 3931 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 3932 | MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0); |
| 3933 | p += 2; |
| 3934 | if (hostname_len > 0) { |
| 3935 | /* save host name */ |
| 3936 | memcpy(p, session->hostname, hostname_len); |
| 3937 | p += hostname_len; |
| 3938 | } |
| 3939 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 3940 | |
| 3941 | #if defined(MBEDTLS_HAVE_TIME) |
| 3942 | MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_reception_time, p, 0); |
| 3943 | p += 8; |
| 3944 | #endif |
| 3945 | MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0); |
| 3946 | p += 4; |
| 3947 | |
| 3948 | MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0); |
| 3949 | p += 2; |
| 3950 | |
| 3951 | if (session->ticket != NULL && session->ticket_len > 0) { |
| 3952 | memcpy(p, session->ticket, session->ticket_len); |
| 3953 | p += session->ticket_len; |
| 3954 | } |
| 3955 | } |
| 3956 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 3957 | return 0; |
| 3958 | } |
| 3959 | |
| 3960 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 3961 | static int ssl_tls13_session_load(mbedtls_ssl_session *session, |
| 3962 | const unsigned char *buf, |
| 3963 | size_t len) |
| 3964 | { |
| 3965 | const unsigned char *p = buf; |
| 3966 | const unsigned char *end = buf + len; |
| 3967 | |
| 3968 | if (end - p < 6) { |
| 3969 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3970 | } |
| 3971 | session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 0); |
| 3972 | session->ticket_flags = p[4]; |
| 3973 | |
| 3974 | /* load resumption_key */ |
| 3975 | session->resumption_key_len = p[5]; |
| 3976 | p += 6; |
| 3977 | |
| 3978 | if (end - p < session->resumption_key_len) { |
| 3979 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3980 | } |
| 3981 | |
| 3982 | if (sizeof(session->resumption_key) < session->resumption_key_len) { |
| 3983 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3984 | } |
| 3985 | memcpy(session->resumption_key, p, session->resumption_key_len); |
| 3986 | p += session->resumption_key_len; |
| 3987 | |
| 3988 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 3989 | if (end - p < 4) { |
| 3990 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3991 | } |
| 3992 | session->max_early_data_size = MBEDTLS_GET_UINT32_BE(p, 0); |
| 3993 | p += 4; |
| 3994 | #endif |
| 3995 | #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) |
| 3996 | if (end - p < 2) { |
| 3997 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3998 | } |
| 3999 | session->record_size_limit = MBEDTLS_GET_UINT16_BE(p, 0); |
| 4000 | p += 2; |
| 4001 | #endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */ |
| 4002 | |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 4003 | #if defined(MBEDTLS_SSL_SRV_C) |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 4004 | if (session->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 4005 | #if defined(MBEDTLS_HAVE_TIME) |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 4006 | if (end - p < 8) { |
| 4007 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4008 | } |
| 4009 | session->ticket_creation_time = MBEDTLS_GET_UINT64_BE(p, 0); |
| 4010 | p += 8; |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 4011 | #endif /* MBEDTLS_HAVE_TIME */ |
| 4012 | |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 4013 | #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) |
Waleed Elmelegy | daa4da7 | 2024-03-13 16:25:34 +0000 | [diff] [blame] | 4014 | size_t alpn_len; |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 4015 | |
Waleed Elmelegy | daa4da7 | 2024-03-13 16:25:34 +0000 | [diff] [blame] | 4016 | if (end - p < 2) { |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 4017 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4018 | } |
Waleed Elmelegy | 883f77c | 2024-03-06 19:09:41 +0000 | [diff] [blame] | 4019 | |
Waleed Elmelegy | daa4da7 | 2024-03-13 16:25:34 +0000 | [diff] [blame] | 4020 | alpn_len = MBEDTLS_GET_UINT16_BE(p, 0); |
| 4021 | p += 2; |
| 4022 | |
| 4023 | if (end - p < (long int) alpn_len) { |
Waleed Elmelegy | 7dfba34 | 2024-03-12 16:22:39 +0000 | [diff] [blame] | 4024 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4025 | } |
| 4026 | |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 4027 | if (alpn_len > 0) { |
Waleed Elmelegy | daa4da7 | 2024-03-13 16:25:34 +0000 | [diff] [blame] | 4028 | int ret = mbedtls_ssl_session_set_ticket_alpn(session, (char *) p); |
| 4029 | if (ret != 0) { |
| 4030 | return ret; |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 4031 | } |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 4032 | p += alpn_len; |
| 4033 | } |
| 4034 | #endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */ |
| 4035 | } |
| 4036 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 4037 | |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 4038 | #if defined(MBEDTLS_SSL_CLI_C) |
| 4039 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 4040 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 4041 | size_t hostname_len; |
| 4042 | /* load host name */ |
| 4043 | if (end - p < 2) { |
| 4044 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4045 | } |
| 4046 | hostname_len = MBEDTLS_GET_UINT16_BE(p, 0); |
| 4047 | p += 2; |
| 4048 | |
| 4049 | if (end - p < (long int) hostname_len) { |
| 4050 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4051 | } |
| 4052 | if (hostname_len > 0) { |
| 4053 | session->hostname = mbedtls_calloc(1, hostname_len); |
| 4054 | if (session->hostname == NULL) { |
| 4055 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 4056 | } |
| 4057 | memcpy(session->hostname, p, hostname_len); |
| 4058 | p += hostname_len; |
| 4059 | } |
| 4060 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 4061 | |
| 4062 | #if defined(MBEDTLS_HAVE_TIME) |
| 4063 | if (end - p < 8) { |
| 4064 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4065 | } |
| 4066 | session->ticket_reception_time = MBEDTLS_GET_UINT64_BE(p, 0); |
| 4067 | p += 8; |
| 4068 | #endif |
| 4069 | if (end - p < 4) { |
| 4070 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4071 | } |
| 4072 | session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0); |
| 4073 | p += 4; |
| 4074 | |
| 4075 | if (end - p < 2) { |
| 4076 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4077 | } |
| 4078 | session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0); |
| 4079 | p += 2; |
| 4080 | |
| 4081 | if (end - p < (long int) session->ticket_len) { |
| 4082 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4083 | } |
| 4084 | if (session->ticket_len > 0) { |
| 4085 | session->ticket = mbedtls_calloc(1, session->ticket_len); |
| 4086 | if (session->ticket == NULL) { |
| 4087 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 4088 | } |
| 4089 | memcpy(session->ticket, p, session->ticket_len); |
| 4090 | p += session->ticket_len; |
| 4091 | } |
| 4092 | } |
| 4093 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 4094 | |
| 4095 | return 0; |
| 4096 | |
| 4097 | } |
| 4098 | #else /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 4099 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 4100 | static int ssl_tls13_session_save(const mbedtls_ssl_session *session, |
| 4101 | unsigned char *buf, |
| 4102 | size_t buf_len, |
| 4103 | size_t *olen) |
| 4104 | { |
| 4105 | ((void) session); |
| 4106 | ((void) buf); |
| 4107 | ((void) buf_len); |
| 4108 | *olen = 0; |
| 4109 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 4110 | } |
| 4111 | |
| 4112 | static int ssl_tls13_session_load(const mbedtls_ssl_session *session, |
Norbert Fabritius | 93b2c32 | 2023-01-24 17:48:29 +0100 | [diff] [blame] | 4113 | const unsigned char *buf, |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 4114 | size_t buf_len) |
| 4115 | { |
| 4116 | ((void) session); |
| 4117 | ((void) buf); |
| 4118 | ((void) buf_len); |
| 4119 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 4120 | } |
| 4121 | #endif /* !MBEDTLS_SSL_SESSION_TICKETS */ |
| 4122 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 4123 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4124 | /* |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 4125 | * Define ticket header determining Mbed TLS version |
| 4126 | * and structure of the ticket. |
| 4127 | */ |
| 4128 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4129 | /* |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 4130 | * Define bitflag determining compile-time settings influencing |
| 4131 | * structure of serialized SSL sessions. |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4132 | */ |
| 4133 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 4134 | #if defined(MBEDTLS_HAVE_TIME) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4135 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 4136 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4137 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4138 | #endif /* MBEDTLS_HAVE_TIME */ |
| 4139 | |
| 4140 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4141 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4142 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4143 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4144 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 4145 | |
David Horstmann | 5c5a32f | 2024-02-13 17:53:35 +0000 | [diff] [blame] | 4146 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
David Horstmann | f686f1d | 2024-03-01 11:20:32 +0000 | [diff] [blame] | 4147 | #define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 1 |
David Horstmann | 5c5a32f | 2024-02-13 17:53:35 +0000 | [diff] [blame] | 4148 | #else |
David Horstmann | f686f1d | 2024-03-01 11:20:32 +0000 | [diff] [blame] | 4149 | #define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 0 |
David Horstmann | 5c5a32f | 2024-02-13 17:53:35 +0000 | [diff] [blame] | 4150 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 4151 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4152 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4153 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4154 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4155 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4156 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ |
| 4157 | |
| 4158 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4159 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4160 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4161 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4162 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 4163 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4164 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4165 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4166 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4167 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4168 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 4169 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4170 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4171 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 |
| 4172 | #else |
| 4173 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 |
| 4174 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 4175 | |
David Horstmann | 92b258b | 2024-02-13 17:23:34 +0000 | [diff] [blame] | 4176 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 4177 | #define SSL_SERIALIZED_SESSION_CONFIG_SNI 1 |
| 4178 | #else |
| 4179 | #define SSL_SERIALIZED_SESSION_CONFIG_SNI 0 |
| 4180 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 4181 | |
| 4182 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 4183 | #define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA 1 |
| 4184 | #else |
| 4185 | #define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA 0 |
| 4186 | #endif /* MBEDTLS_SSL_EARLY_DATA */ |
| 4187 | |
| 4188 | #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) |
| 4189 | #define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE 1 |
| 4190 | #else |
| 4191 | #define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE 0 |
| 4192 | #endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */ |
| 4193 | |
Waleed Elmelegy | 7dfba34 | 2024-03-12 16:22:39 +0000 | [diff] [blame] | 4194 | #if defined(MBEDTLS_SSL_ALPN) && defined(MBEDTLS_SSL_SRV_C) && \ |
| 4195 | defined(MBEDTLS_SSL_EARLY_DATA) |
Waleed Elmelegy | 1102563 | 2024-03-07 15:25:52 +0000 | [diff] [blame] | 4196 | #define SSL_SERIALIZED_SESSION_CONFIG_ALPN 1 |
| 4197 | #else |
| 4198 | #define SSL_SERIALIZED_SESSION_CONFIG_ALPN 0 |
| 4199 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4200 | |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4201 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0 |
| 4202 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1 |
| 4203 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 |
| 4204 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3 |
Hanno Becker | 37bdbe6 | 2021-08-01 05:38:58 +0100 | [diff] [blame] | 4205 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4 |
| 4206 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5 |
David Horstmann | f686f1d | 2024-03-01 11:20:32 +0000 | [diff] [blame] | 4207 | #define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT 6 |
David Horstmann | 92b258b | 2024-02-13 17:23:34 +0000 | [diff] [blame] | 4208 | #define SSL_SERIALIZED_SESSION_CONFIG_SNI_BIT 7 |
| 4209 | #define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA_BIT 8 |
| 4210 | #define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE_BIT 9 |
Waleed Elmelegy | 1102563 | 2024-03-07 15:25:52 +0000 | [diff] [blame] | 4211 | #define SSL_SERIALIZED_SESSION_CONFIG_ALPN_BIT 10 |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4212 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 4213 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4214 | ((uint16_t) ( \ |
| 4215 | (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \ |
| 4216 | (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \ |
| 4217 | (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \ |
| 4218 | SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \ |
| 4219 | (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \ |
| 4220 | (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \ |
David Horstmann | 5c5a32f | 2024-02-13 17:53:35 +0000 | [diff] [blame] | 4221 | (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT) | \ |
David Horstmann | 71fa1a9 | 2024-03-01 12:32:18 +0000 | [diff] [blame] | 4222 | (SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT << \ |
| 4223 | SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT) | \ |
David Horstmann | 92b258b | 2024-02-13 17:23:34 +0000 | [diff] [blame] | 4224 | (SSL_SERIALIZED_SESSION_CONFIG_SNI << SSL_SERIALIZED_SESSION_CONFIG_SNI_BIT) | \ |
| 4225 | (SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA << \ |
| 4226 | SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA_BIT) | \ |
| 4227 | (SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE << \ |
Waleed Elmelegy | 1102563 | 2024-03-07 15:25:52 +0000 | [diff] [blame] | 4228 | SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE_BIT) | \ |
Waleed Elmelegy | 75e33fa | 2024-03-07 16:57:58 +0000 | [diff] [blame] | 4229 | (SSL_SERIALIZED_SESSION_CONFIG_ALPN << \ |
Waleed Elmelegy | 1102563 | 2024-03-07 15:25:52 +0000 | [diff] [blame] | 4230 | SSL_SERIALIZED_SESSION_CONFIG_ALPN_BIT))) |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4231 | |
Chien Wong | 4e9683e | 2023-12-28 17:07:43 +0800 | [diff] [blame] | 4232 | static const unsigned char ssl_serialized_session_header[] = { |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4233 | MBEDTLS_VERSION_MAJOR, |
| 4234 | MBEDTLS_VERSION_MINOR, |
| 4235 | MBEDTLS_VERSION_PATCH, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4236 | MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG), |
| 4237 | MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG), |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 4238 | }; |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 4239 | |
| 4240 | /* |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4241 | * Serialize a session in the following format: |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4242 | * (in the presentation language of TLS, RFC 8446 section 3) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4243 | * |
David Horstmann | cb01b36 | 2024-02-29 17:31:46 +0000 | [diff] [blame] | 4244 | * TLS 1.2 session: |
| 4245 | * |
| 4246 | * struct { |
| 4247 | * #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4248 | * opaque ticket<0..2^24-1>; // length 0 means no ticket |
| 4249 | * uint32 ticket_lifetime; |
| 4250 | * #endif |
| 4251 | * } ClientOnlyData; |
| 4252 | * |
| 4253 | * struct { |
| 4254 | * #if defined(MBEDTLS_HAVE_TIME) |
| 4255 | * uint64 start_time; |
| 4256 | * #endif |
| 4257 | * uint8 session_id_len; // at most 32 |
| 4258 | * opaque session_id[32]; |
| 4259 | * opaque master[48]; // fixed length in the standard |
| 4260 | * uint32 verify_result; |
| 4261 | * #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE |
| 4262 | * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert |
| 4263 | * #else |
David Horstmann | 76ba26a | 2024-03-01 12:03:35 +0000 | [diff] [blame] | 4264 | * uint8 peer_cert_digest_type; |
David Horstmann | cb01b36 | 2024-02-29 17:31:46 +0000 | [diff] [blame] | 4265 | * opaque peer_cert_digest<0..2^8-1> |
| 4266 | * #endif |
| 4267 | * select (endpoint) { |
| 4268 | * case client: ClientOnlyData; |
| 4269 | * case server: uint64 ticket_creation_time; |
| 4270 | * }; |
| 4271 | * #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 4272 | * uint8 mfl_code; // up to 255 according to standard |
| 4273 | * #endif |
| 4274 | * #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 4275 | * uint8 encrypt_then_mac; // 0 or 1 |
| 4276 | * #endif |
| 4277 | * } serialized_session_tls12; |
| 4278 | * |
| 4279 | * |
| 4280 | * TLS 1.3 Session: |
| 4281 | * |
| 4282 | * struct { |
| 4283 | * #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 4284 | * opaque hostname<0..2^16-1>; |
| 4285 | * #endif |
| 4286 | * #if defined(MBEDTLS_HAVE_TIME) |
| 4287 | * uint64 ticket_reception_time; |
| 4288 | * #endif |
| 4289 | * uint32 ticket_lifetime; |
| 4290 | * opaque ticket<1..2^16-1>; |
| 4291 | * } ClientOnlyData; |
| 4292 | * |
| 4293 | * struct { |
| 4294 | * uint32 ticket_age_add; |
| 4295 | * uint8 ticket_flags; |
| 4296 | * opaque resumption_key<0..255>; |
| 4297 | * #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 4298 | * uint32 max_early_data_size; |
| 4299 | * #endif |
| 4300 | * #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) |
| 4301 | * uint16 record_size_limit; |
| 4302 | * #endif |
| 4303 | * select ( endpoint ) { |
| 4304 | * case client: ClientOnlyData; |
Waleed Elmelegy | 7dfba34 | 2024-03-12 16:22:39 +0000 | [diff] [blame] | 4305 | * case server: |
David Horstmann | cb01b36 | 2024-02-29 17:31:46 +0000 | [diff] [blame] | 4306 | * #if defined(MBEDTLS_HAVE_TIME) |
Waleed Elmelegy | 7dfba34 | 2024-03-12 16:22:39 +0000 | [diff] [blame] | 4307 | * uint64 ticket_creation_time; |
David Horstmann | cb01b36 | 2024-02-29 17:31:46 +0000 | [diff] [blame] | 4308 | * #endif |
Waleed Elmelegy | fe9ae08 | 2024-03-07 15:47:31 +0000 | [diff] [blame] | 4309 | * #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) |
Waleed Elmelegy | daa4da7 | 2024-03-13 16:25:34 +0000 | [diff] [blame] | 4310 | * opaque ticket_alpn<0..256>; |
David Horstmann | cb01b36 | 2024-02-29 17:31:46 +0000 | [diff] [blame] | 4311 | * #endif |
| 4312 | * }; |
| 4313 | * } serialized_session_tls13; |
| 4314 | * |
| 4315 | * |
| 4316 | * SSL session: |
| 4317 | * |
| 4318 | * struct { |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 4319 | * |
Hanno Becker | dce5097 | 2021-08-01 05:39:23 +0100 | [diff] [blame] | 4320 | * opaque mbedtls_version[3]; // library version: major, minor, patch |
| 4321 | * opaque session_format[2]; // library-version specific 16-bit field |
| 4322 | * // determining the format of the remaining |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4323 | * // serialized data. |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 4324 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4325 | * Note: When updating the format, remember to keep |
| 4326 | * these version+format bytes. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4327 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4328 | * // In this version, `session_format` determines |
| 4329 | * // the setting of those compile-time |
| 4330 | * // configuration options which influence |
| 4331 | * // the structure of mbedtls_ssl_session. |
| 4332 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 4333 | * uint8_t minor_ver; // Protocol minor version. Possible values: |
Jerry Yu | 0c2a738 | 2022-12-06 13:27:25 +0800 | [diff] [blame] | 4334 | * // - TLS 1.2 (0x0303) |
| 4335 | * // - TLS 1.3 (0x0304) |
David Horstmann | 531aca2 | 2024-02-29 18:14:28 +0000 | [diff] [blame] | 4336 | * uint8_t endpoint; |
| 4337 | * uint16_t ciphersuite; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4338 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 4339 | * select (serialized_session.tls_version) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4340 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 4341 | * case MBEDTLS_SSL_VERSION_TLS1_2: |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4342 | * serialized_session_tls12 data; |
Jerry Yu | 0c2a738 | 2022-12-06 13:27:25 +0800 | [diff] [blame] | 4343 | * case MBEDTLS_SSL_VERSION_TLS1_3: |
Jerry Yu | ddda050 | 2022-12-01 19:43:12 +0800 | [diff] [blame] | 4344 | * serialized_session_tls13 data; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4345 | * |
| 4346 | * }; |
| 4347 | * |
| 4348 | * } serialized_session; |
| 4349 | * |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4350 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4351 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4352 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4353 | static int ssl_session_save(const mbedtls_ssl_session *session, |
| 4354 | unsigned char omit_header, |
| 4355 | unsigned char *buf, |
| 4356 | size_t buf_len, |
| 4357 | size_t *olen) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4358 | { |
| 4359 | unsigned char *p = buf; |
| 4360 | size_t used = 0; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 4361 | size_t remaining_len; |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 4362 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4363 | size_t out_len; |
| 4364 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 4365 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4366 | if (session == NULL) { |
| 4367 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 4368 | } |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 4369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4370 | if (!omit_header) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4371 | /* |
| 4372 | * Add Mbed TLS version identifier |
| 4373 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4374 | used += sizeof(ssl_serialized_session_header); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4375 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4376 | if (used <= buf_len) { |
| 4377 | memcpy(p, ssl_serialized_session_header, |
| 4378 | sizeof(ssl_serialized_session_header)); |
| 4379 | p += sizeof(ssl_serialized_session_header); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4380 | } |
| 4381 | } |
| 4382 | |
| 4383 | /* |
Ronald Cron | 40a4ab0 | 2024-01-15 10:21:30 +0100 | [diff] [blame] | 4384 | * TLS version identifier, endpoint, ciphersuite |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4385 | */ |
Ronald Cron | 40a4ab0 | 2024-01-15 10:21:30 +0100 | [diff] [blame] | 4386 | used += 1 /* TLS version */ |
| 4387 | + 1 /* endpoint */ |
| 4388 | + 2; /* ciphersuite */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4389 | if (used <= buf_len) { |
| 4390 | *p++ = MBEDTLS_BYTE_0(session->tls_version); |
Ronald Cron | 40a4ab0 | 2024-01-15 10:21:30 +0100 | [diff] [blame] | 4391 | *p++ = session->endpoint; |
| 4392 | MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0); |
| 4393 | p += 2; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4394 | } |
| 4395 | |
| 4396 | /* Forward to version-specific serialization routine. */ |
Jerry Yu | fca4d57 | 2022-07-21 10:37:48 +0800 | [diff] [blame] | 4397 | remaining_len = (buf_len >= used) ? buf_len - used : 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4398 | switch (session->tls_version) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4399 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4400 | case MBEDTLS_SSL_VERSION_TLS1_2: |
| 4401 | used += ssl_tls12_session_save(session, p, remaining_len); |
| 4402 | break; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4403 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4404 | |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 4405 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4406 | case MBEDTLS_SSL_VERSION_TLS1_3: |
| 4407 | ret = ssl_tls13_session_save(session, p, remaining_len, &out_len); |
| 4408 | if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) { |
| 4409 | return ret; |
| 4410 | } |
| 4411 | used += out_len; |
| 4412 | break; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 4413 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 4414 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4415 | default: |
| 4416 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4417 | } |
| 4418 | |
| 4419 | *olen = used; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4420 | if (used > buf_len) { |
| 4421 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 4422 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4423 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4424 | return 0; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4425 | } |
| 4426 | |
| 4427 | /* |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4428 | * Public wrapper for ssl_session_save() |
| 4429 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4430 | int mbedtls_ssl_session_save(const mbedtls_ssl_session *session, |
| 4431 | unsigned char *buf, |
| 4432 | size_t buf_len, |
| 4433 | size_t *olen) |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4434 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4435 | return ssl_session_save(session, 0, buf, buf_len, olen); |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4436 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4437 | |
Jerry Yu | f1b23ca | 2022-02-18 11:48:47 +0800 | [diff] [blame] | 4438 | /* |
| 4439 | * Deserialize session, see mbedtls_ssl_session_save() for format. |
| 4440 | * |
| 4441 | * This internal version is wrapped by a public function that cleans up in |
| 4442 | * case of error, and has an extra option omit_header. |
| 4443 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4444 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4445 | static int ssl_session_load(mbedtls_ssl_session *session, |
| 4446 | unsigned char omit_header, |
| 4447 | const unsigned char *buf, |
| 4448 | size_t len) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4449 | { |
| 4450 | const unsigned char *p = buf; |
| 4451 | const unsigned char * const end = buf + len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 4452 | size_t remaining_len; |
| 4453 | |
| 4454 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4455 | if (session == NULL) { |
| 4456 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 4457 | } |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4458 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4459 | if (!omit_header) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4460 | /* |
| 4461 | * Check Mbed TLS version identifier |
| 4462 | */ |
| 4463 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4464 | if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) { |
| 4465 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4466 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4467 | |
| 4468 | if (memcmp(p, ssl_serialized_session_header, |
| 4469 | sizeof(ssl_serialized_session_header)) != 0) { |
| 4470 | return MBEDTLS_ERR_SSL_VERSION_MISMATCH; |
| 4471 | } |
| 4472 | p += sizeof(ssl_serialized_session_header); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4473 | } |
| 4474 | |
| 4475 | /* |
Ronald Cron | 40a4ab0 | 2024-01-15 10:21:30 +0100 | [diff] [blame] | 4476 | * TLS version identifier, endpoint, ciphersuite |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4477 | */ |
Ronald Cron | 40a4ab0 | 2024-01-15 10:21:30 +0100 | [diff] [blame] | 4478 | if (4 > (size_t) (end - p)) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4479 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4480 | } |
Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 4481 | session->tls_version = (mbedtls_ssl_protocol_version) (0x0300 | *p++); |
Ronald Cron | 40a4ab0 | 2024-01-15 10:21:30 +0100 | [diff] [blame] | 4482 | session->endpoint = *p++; |
| 4483 | session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 0); |
| 4484 | p += 2; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4485 | |
| 4486 | /* Dispatch according to TLS version. */ |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 4487 | remaining_len = (size_t) (end - p); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4488 | switch (session->tls_version) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4489 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4490 | case MBEDTLS_SSL_VERSION_TLS1_2: |
| 4491 | return ssl_tls12_session_load(session, p, remaining_len); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4492 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4493 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 4494 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4495 | case MBEDTLS_SSL_VERSION_TLS1_3: |
| 4496 | return ssl_tls13_session_load(session, p, remaining_len); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 4497 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 4498 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4499 | default: |
| 4500 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4501 | } |
| 4502 | } |
| 4503 | |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4504 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 4505 | * Deserialize session: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4506 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4507 | int mbedtls_ssl_session_load(mbedtls_ssl_session *session, |
| 4508 | const unsigned char *buf, |
| 4509 | size_t len) |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4510 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4511 | int ret = ssl_session_load(session, 0, buf, len); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4512 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4513 | if (ret != 0) { |
| 4514 | mbedtls_ssl_session_free(session); |
| 4515 | } |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4516 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4517 | return ret; |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4518 | } |
| 4519 | |
| 4520 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4521 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4522 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4523 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4524 | static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl) |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 4525 | { |
| 4526 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 4527 | |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 4528 | /* |
| 4529 | * 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] | 4530 | * that were written into the output buffer by the previous handshake step, |
| 4531 | * if the write to the network callback returned with the |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 4532 | * #MBEDTLS_ERR_SSL_WANT_WRITE error code. |
| 4533 | * We proceed to the next handshake step only when all data from the |
| 4534 | * previous one have been sent to the peer, thus we make sure that this is |
| 4535 | * the case here by calling `mbedtls_ssl_flush_output()`. The function may |
| 4536 | * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case |
| 4537 | * we have to wait before to go ahead. |
| 4538 | * In the case of TLS 1.3, handshake step handlers do not send data to the |
| 4539 | * peer. Data are only sent here and through |
| 4540 | * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an |
Andrzej Kurek | 5c65c57 | 2022-04-13 14:28:52 -0400 | [diff] [blame] | 4541 | * alert occurred. |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 4542 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4543 | if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) { |
| 4544 | return ret; |
| 4545 | } |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 4546 | |
| 4547 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4548 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 4549 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) { |
| 4550 | if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) { |
| 4551 | return ret; |
| 4552 | } |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 4553 | } |
| 4554 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4555 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4556 | return ret; |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 4557 | } |
| 4558 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4559 | int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4560 | { |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 4561 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4562 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4563 | if (ssl == NULL || |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 4564 | ssl->conf == NULL || |
| 4565 | ssl->handshake == NULL || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4566 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 4567 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 4568 | } |
| 4569 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4570 | ret = ssl_prepare_handshake_step(ssl); |
| 4571 | if (ret != 0) { |
| 4572 | return ret; |
| 4573 | } |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 4574 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4575 | ret = mbedtls_ssl_handle_pending_alert(ssl); |
| 4576 | if (ret != 0) { |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 4577 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4578 | } |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 4579 | |
Tom Cosgrove | 2fdc7b3 | 2022-09-21 12:33:17 +0100 | [diff] [blame] | 4580 | /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or |
| 4581 | * MBEDTLS_SSL_IS_SERVER, this is the return code we give */ |
| 4582 | ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4583 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4584 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4585 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 4586 | MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s", |
Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 4587 | mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state))); |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 4588 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4589 | switch (ssl->state) { |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 4590 | case MBEDTLS_SSL_HELLO_REQUEST: |
Gilles Peskine | 49f179d | 2025-03-07 15:09:32 +0100 | [diff] [blame] | 4591 | mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO); |
Tom Cosgrove | 87d9c6c | 2022-09-22 09:27:56 +0100 | [diff] [blame] | 4592 | ret = 0; |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 4593 | break; |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 4594 | |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 4595 | case MBEDTLS_SSL_CLIENT_HELLO: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4596 | ret = mbedtls_ssl_write_client_hello(ssl); |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 4597 | break; |
| 4598 | |
| 4599 | default: |
| 4600 | #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] | 4601 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
| 4602 | ret = mbedtls_ssl_tls13_handshake_client_step(ssl); |
| 4603 | } else { |
| 4604 | ret = mbedtls_ssl_handshake_client_step(ssl); |
| 4605 | } |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 4606 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4607 | ret = mbedtls_ssl_handshake_client_step(ssl); |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 4608 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4609 | ret = mbedtls_ssl_tls13_handshake_client_step(ssl); |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 4610 | #endif |
| 4611 | } |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 4612 | } |
Ronald Cron | 6291b23 | 2023-03-08 15:51:25 +0100 | [diff] [blame] | 4613 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 4614 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4615 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4616 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Ronald Cron | 6291b23 | 2023-03-08 15:51:25 +0100 | [diff] [blame] | 4617 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4618 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4619 | ret = mbedtls_ssl_tls13_handshake_server_step(ssl); |
Ronald Cron | 6291b23 | 2023-03-08 15:51:25 +0100 | [diff] [blame] | 4620 | } else { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4621 | ret = mbedtls_ssl_handshake_server_step(ssl); |
| 4622 | } |
Ronald Cron | 6291b23 | 2023-03-08 15:51:25 +0100 | [diff] [blame] | 4623 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4624 | ret = mbedtls_ssl_handshake_server_step(ssl); |
| 4625 | #else |
| 4626 | ret = mbedtls_ssl_tls13_handshake_server_step(ssl); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4627 | #endif |
Ronald Cron | 6291b23 | 2023-03-08 15:51:25 +0100 | [diff] [blame] | 4628 | } |
| 4629 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4630 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4631 | if (ret != 0) { |
Jerry Yu | bbd5a3f | 2021-09-18 20:50:22 +0800 | [diff] [blame] | 4632 | /* handshake_step return error. And it is same |
| 4633 | * with alert_reason. |
| 4634 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4635 | if (ssl->send_alert) { |
| 4636 | ret = mbedtls_ssl_handle_pending_alert(ssl); |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 4637 | goto cleanup; |
| 4638 | } |
| 4639 | } |
| 4640 | |
| 4641 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4642 | return ret; |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4643 | } |
| 4644 | |
| 4645 | /* |
| 4646 | * Perform the SSL handshake |
| 4647 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4648 | int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4649 | { |
| 4650 | int ret = 0; |
| 4651 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 4652 | /* Sanity checks */ |
| 4653 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4654 | if (ssl == NULL || ssl->conf == NULL) { |
| 4655 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4656 | } |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 4657 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 4658 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4659 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 4660 | (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) { |
| 4661 | MBEDTLS_SSL_DEBUG_MSG(1, ("You must use " |
| 4662 | "mbedtls_ssl_set_timer_cb() for DTLS")); |
| 4663 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 4664 | } |
| 4665 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4666 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4667 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake")); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4668 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 4669 | /* Main handshake loop */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4670 | while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 4671 | ret = mbedtls_ssl_handshake_step(ssl); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4672 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4673 | if (ret != 0) { |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4674 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4675 | } |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4676 | } |
| 4677 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4678 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4679 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4680 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4681 | } |
| 4682 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4683 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 4684 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4685 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4686 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4687 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4688 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4689 | static int ssl_write_hello_request(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4690 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4691 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4692 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4693 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request")); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4694 | |
| 4695 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4696 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 4697 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4698 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4699 | if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { |
| 4700 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret); |
| 4701 | return ret; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4702 | } |
| 4703 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4704 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request")); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4705 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4706 | return 0; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4707 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4708 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4709 | |
| 4710 | /* |
| 4711 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4712 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 4713 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 4714 | * - 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] | 4715 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4716 | * 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] | 4717 | * 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] | 4718 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4719 | int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4720 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4721 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4722 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4723 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate")); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4724 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4725 | if ((ret = ssl_handshake_init(ssl)) != 0) { |
| 4726 | return ret; |
| 4727 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4728 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 4729 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 4730 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4731 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4732 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 4733 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) { |
| 4734 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4735 | ssl->handshake->out_msg_seq = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4736 | } else { |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4737 | ssl->handshake->in_msg_seq = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4738 | } |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 4739 | } |
| 4740 | #endif |
| 4741 | |
Gilles Peskine | 49f179d | 2025-03-07 15:09:32 +0100 | [diff] [blame] | 4742 | mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_REQUEST); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4743 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4744 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4745 | if ((ret = mbedtls_ssl_handshake(ssl)) != 0) { |
| 4746 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret); |
| 4747 | return ret; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4748 | } |
| 4749 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4750 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate")); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4751 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4752 | return 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4753 | } |
| 4754 | |
| 4755 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4756 | * Renegotiate current connection on client, |
| 4757 | * or request renegotiation on server |
| 4758 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4759 | int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4760 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4761 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4762 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4763 | if (ssl == NULL || ssl->conf == NULL) { |
| 4764 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4765 | } |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 4766 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4767 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4768 | /* On server, just send the request */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4769 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 4770 | if (mbedtls_ssl_is_handshake_over(ssl) == 0) { |
| 4771 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4772 | } |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4773 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4774 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 4775 | |
| 4776 | /* Did we already try/start sending HelloRequest? */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4777 | if (ssl->out_left != 0) { |
| 4778 | return mbedtls_ssl_flush_output(ssl); |
| 4779 | } |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 4780 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4781 | return ssl_write_hello_request(ssl); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4782 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4783 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4784 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4785 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4786 | /* |
| 4787 | * On client, either start the renegotiation process or, |
| 4788 | * if already in progress, continue the handshake |
| 4789 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4790 | if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) { |
| 4791 | if (mbedtls_ssl_is_handshake_over(ssl) == 0) { |
| 4792 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4793 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4794 | |
| 4795 | if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) { |
| 4796 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret); |
| 4797 | return ret; |
| 4798 | } |
| 4799 | } else { |
| 4800 | if ((ret = mbedtls_ssl_handshake(ssl)) != 0) { |
| 4801 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret); |
| 4802 | return ret; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4803 | } |
| 4804 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4805 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4806 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4807 | return ret; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4808 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4809 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4810 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4811 | void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4812 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 4813 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 4814 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4815 | if (handshake == NULL) { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4816 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4817 | } |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4818 | |
Valerio Setti | 6f0441d | 2023-07-03 12:11:36 +0200 | [diff] [blame] | 4819 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4820 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4821 | if (ssl->handshake->group_list_heap_allocated) { |
| 4822 | mbedtls_free((void *) handshake->group_list); |
| 4823 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4824 | handshake->group_list = NULL; |
| 4825 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Valerio Setti | 6f0441d | 2023-07-03 12:11:36 +0200 | [diff] [blame] | 4826 | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4827 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4828 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4829 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4830 | if (ssl->handshake->sig_algs_heap_allocated) { |
| 4831 | mbedtls_free((void *) handshake->sig_algs); |
| 4832 | } |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4833 | handshake->sig_algs = NULL; |
| 4834 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Xiaofei Bai | c234ecf | 2022-02-08 09:59:23 +0000 | [diff] [blame] | 4835 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4836 | if (ssl->handshake->certificate_request_context) { |
| 4837 | mbedtls_free((void *) handshake->certificate_request_context); |
Xiaofei Bai | c234ecf | 2022-02-08 09:59:23 +0000 | [diff] [blame] | 4838 | } |
| 4839 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4840 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4841 | |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4842 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4843 | if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) { |
| 4844 | ssl->conf->f_async_cancel(ssl); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4845 | handshake->async_in_progress = 0; |
| 4846 | } |
| 4847 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 4848 | |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 4849 | #if defined(MBEDTLS_MD_CAN_SHA256) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4850 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4851 | psa_hash_abort(&handshake->fin_sha256_psa); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4852 | #else |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 4853 | mbedtls_md_free(&handshake->fin_sha256); |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 4854 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4855 | #endif |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 4856 | #if defined(MBEDTLS_MD_CAN_SHA384) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4857 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4858 | psa_hash_abort(&handshake->fin_sha384_psa); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4859 | #else |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 4860 | mbedtls_md_free(&handshake->fin_sha384); |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 4861 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4862 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 4863 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4864 | #if defined(MBEDTLS_DHM_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4865 | mbedtls_dhm_free(&handshake->dhm_ctx); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4866 | #endif |
Valerio Setti | 7aeec54 | 2023-07-05 18:57:21 +0200 | [diff] [blame] | 4867 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Valerio Setti | 6eb0054 | 2023-07-07 17:04:24 +0200 | [diff] [blame] | 4868 | defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4869 | mbedtls_ecdh_free(&handshake->ecdh_ctx); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 4870 | #endif |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 4871 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 4872 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 4873 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4874 | psa_pake_abort(&handshake->psa_pake_ctx); |
Valerio Setti | eb3f788 | 2022-12-08 18:42:58 +0100 | [diff] [blame] | 4875 | /* |
| 4876 | * Opaque keys are not stored in the handshake's data and it's the user |
| 4877 | * responsibility to destroy them. Clear ones, instead, are created by |
| 4878 | * the TLS library and should be destroyed at the same level |
| 4879 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4880 | if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) { |
| 4881 | psa_destroy_key(handshake->psa_pake_password); |
Valerio Setti | eb3f788 | 2022-12-08 18:42:58 +0100 | [diff] [blame] | 4882 | } |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 4883 | handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT; |
| 4884 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4885 | mbedtls_ecjpake_free(&handshake->ecjpake_ctx); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 4886 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 4887 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4888 | mbedtls_free(handshake->ecjpake_cache); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 4889 | handshake->ecjpake_cache = NULL; |
| 4890 | handshake->ecjpake_cache_len = 0; |
| 4891 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 4892 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 4893 | |
Valerio Setti | 7aeec54 | 2023-07-05 18:57:21 +0200 | [diff] [blame] | 4894 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_ANY_ENABLED) || \ |
Valerio Setti | 45d56f3 | 2023-07-13 17:23:20 +0200 | [diff] [blame] | 4895 | defined(MBEDTLS_KEY_EXCHANGE_WITH_ECDSA_ANY_ENABLED) || \ |
Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 4896 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 4897 | /* explicit void pointer cast for buggy MS compiler */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4898 | mbedtls_free((void *) handshake->curves_tls_id); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 4899 | #endif |
| 4900 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 4901 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4902 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4903 | if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4904 | /* The maintenance of the external PSK key slot is the |
| 4905 | * user's responsibility. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4906 | if (ssl->handshake->psk_opaque_is_internal) { |
| 4907 | psa_destroy_key(ssl->handshake->psk_opaque); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4908 | ssl->handshake->psk_opaque_is_internal = 0; |
| 4909 | } |
| 4910 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 4911 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 4912 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4913 | if (handshake->psk != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 4914 | mbedtls_zeroize_and_free(handshake->psk, handshake->psk_len); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 4915 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 4916 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 4917 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 4918 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4919 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 4920 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 4921 | /* |
| 4922 | * Free only the linked list wrapper, not the keys themselves |
| 4923 | * since the belong to the SNI callback |
| 4924 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4925 | ssl_key_cert_free(handshake->sni_key_cert); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4926 | #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] | 4927 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4928 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4929 | mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx); |
| 4930 | if (handshake->ecrs_peer_cert != NULL) { |
| 4931 | mbedtls_x509_crt_free(handshake->ecrs_peer_cert); |
| 4932 | mbedtls_free(handshake->ecrs_peer_cert); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 4933 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 4934 | #endif |
| 4935 | |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 4936 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 4937 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4938 | mbedtls_pk_free(&handshake->peer_pubkey); |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 4939 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4940 | |
XiaokangQian | 9b93c0d | 2022-02-09 06:02:25 +0000 | [diff] [blame] | 4941 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4942 | (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3)) |
| 4943 | mbedtls_free(handshake->cookie); |
XiaokangQian | 9b93c0d | 2022-02-09 06:02:25 +0000 | [diff] [blame] | 4944 | #endif /* MBEDTLS_SSL_CLI_C && |
| 4945 | ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */ |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 4946 | |
| 4947 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4948 | mbedtls_ssl_flight_free(handshake->flight); |
| 4949 | mbedtls_ssl_buffering_free(ssl); |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 4950 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 4951 | |
Valerio Setti | ea59c43 | 2023-07-25 11:14:03 +0200 | [diff] [blame] | 4952 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED) |
Przemek Stekiel | 7ac93be | 2023-07-04 10:02:38 +0200 | [diff] [blame] | 4953 | if (handshake->xxdh_psa_privkey_is_external == 0) { |
| 4954 | psa_destroy_key(handshake->xxdh_psa_privkey); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4955 | } |
Valerio Setti | ea59c43 | 2023-07-25 11:14:03 +0200 | [diff] [blame] | 4956 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED */ |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 4957 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4958 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4959 | mbedtls_ssl_transform_free(handshake->transform_handshake); |
| 4960 | mbedtls_free(handshake->transform_handshake); |
Jerry Yu | 3d9b590 | 2022-11-04 14:07:25 +0800 | [diff] [blame] | 4961 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4962 | mbedtls_ssl_transform_free(handshake->transform_earlydata); |
| 4963 | mbedtls_free(handshake->transform_earlydata); |
Jerry Yu | 3d9b590 | 2022-11-04 14:07:25 +0800 | [diff] [blame] | 4964 | #endif |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4965 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 4966 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4967 | |
| 4968 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4969 | /* If the buffers are too big - reallocate. Because of the way Mbed TLS |
| 4970 | * processes datagrams and the fact that a datagram is allowed to have |
| 4971 | * several records in it, it is possible that the I/O buffers are not |
| 4972 | * empty at this stage */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4973 | handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl), |
| 4974 | mbedtls_ssl_get_output_buflen(ssl)); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4975 | #endif |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 4976 | |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 4977 | /* mbedtls_platform_zeroize MUST be last one in this function */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4978 | mbedtls_platform_zeroize(handshake, |
| 4979 | sizeof(mbedtls_ssl_handshake_params)); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4980 | } |
| 4981 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4982 | void mbedtls_ssl_session_free(mbedtls_ssl_session *session) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4983 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4984 | if (session == NULL) { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4985 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4986 | } |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4987 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4988 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4989 | ssl_clear_peer_cert(session); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4990 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 4991 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4992 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Xiaokang Qian | bc663a0 | 2022-10-09 11:14:39 +0000 | [diff] [blame] | 4993 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 4994 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4995 | mbedtls_free(session->hostname); |
Xiaokang Qian | 281fd1b | 2022-09-20 11:35:41 +0000 | [diff] [blame] | 4996 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4997 | mbedtls_free(session->ticket); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 4998 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 4999 | |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 5000 | #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) && \ |
| 5001 | defined(MBEDTLS_SSL_SRV_C) |
Waleed Elmelegy | 883f77c | 2024-03-06 19:09:41 +0000 | [diff] [blame] | 5002 | mbedtls_free(session->ticket_alpn); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5003 | #endif |
| 5004 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5005 | mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session)); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5006 | } |
| 5007 | |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 5008 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 5009 | |
| 5010 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 5011 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u |
| 5012 | #else |
| 5013 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u |
| 5014 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 5015 | |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 5016 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 5017 | |
| 5018 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 5019 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u |
| 5020 | #else |
| 5021 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u |
| 5022 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 5023 | |
| 5024 | #if defined(MBEDTLS_SSL_ALPN) |
| 5025 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u |
| 5026 | #else |
| 5027 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u |
| 5028 | #endif /* MBEDTLS_SSL_ALPN */ |
| 5029 | |
| 5030 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0 |
| 5031 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1 |
| 5032 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2 |
| 5033 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3 |
| 5034 | |
| 5035 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5036 | ((uint32_t) ( \ |
| 5037 | (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \ |
| 5038 | SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \ |
| 5039 | (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \ |
| 5040 | SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \ |
| 5041 | (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \ |
| 5042 | SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \ |
| 5043 | (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \ |
| 5044 | 0u)) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 5045 | |
Chien Wong | 4e9683e | 2023-12-28 17:07:43 +0800 | [diff] [blame] | 5046 | static const unsigned char ssl_serialized_context_header[] = { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5047 | MBEDTLS_VERSION_MAJOR, |
| 5048 | MBEDTLS_VERSION_MINOR, |
| 5049 | MBEDTLS_VERSION_PATCH, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5050 | MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG), |
| 5051 | MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG), |
| 5052 | MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG), |
| 5053 | MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG), |
| 5054 | MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG), |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5055 | }; |
| 5056 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5057 | /* |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5058 | * Serialize a full SSL context |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 5059 | * |
| 5060 | * The format of the serialized data is: |
| 5061 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 5062 | * |
| 5063 | * // header |
| 5064 | * opaque mbedtls_version[3]; // major, minor, patch |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5065 | * opaque context_format[5]; // version-specific field determining |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 5066 | * // the format of the remaining |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5067 | * // serialized data. |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 5068 | * Note: When updating the format, remember to keep these |
| 5069 | * 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] | 5070 | * |
| 5071 | * // session sub-structure |
| 5072 | * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save() |
| 5073 | * // transform sub-structure |
| 5074 | * uint8 random[64]; // ServerHello.random+ClientHello.random |
| 5075 | * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value |
| 5076 | * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use |
| 5077 | * // fields from ssl_context |
Gilles Peskine | f6a676d | 2025-02-17 16:10:14 +0100 | [diff] [blame] | 5078 | * uint32 badmac_seen_or_in_hsfraglen; // DTLS: number of records with failing MAC |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 5079 | * uint64 in_window_top; // DTLS: last validated record seq_num |
| 5080 | * uint64 in_window; // DTLS: bitmask for replay protection |
| 5081 | * uint8 disable_datagram_packing; // DTLS: only one record per datagram |
| 5082 | * uint64 cur_out_ctr; // Record layer: outgoing sequence number |
| 5083 | * uint16 mtu; // DTLS: path mtu (max outgoing fragment size) |
| 5084 | * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol |
| 5085 | * |
| 5086 | * Note that many fields of the ssl_context or sub-structures are not |
| 5087 | * serialized, as they fall in one of the following categories: |
| 5088 | * |
| 5089 | * 1. forced value (eg in_left must be 0) |
| 5090 | * 2. pointer to dynamically-allocated memory (eg session, transform) |
| 5091 | * 3. value can be re-derived from other data (eg session keys from MS) |
| 5092 | * 4. value was temporary (eg content of input buffer) |
| 5093 | * 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] | 5094 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5095 | int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl, |
| 5096 | unsigned char *buf, |
| 5097 | size_t buf_len, |
| 5098 | size_t *olen) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5099 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5100 | unsigned char *p = buf; |
| 5101 | size_t used = 0; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5102 | size_t session_len; |
| 5103 | int ret = 0; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5104 | |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 5105 | /* |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5106 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in |
| 5107 | * this function's documentation. |
| 5108 | * |
| 5109 | * These are due to assumptions/limitations in the implementation. Some of |
| 5110 | * them are likely to stay (no handshake in progress) some might go away |
| 5111 | * (only DTLS) but are currently used to simplify the implementation. |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 5112 | */ |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5113 | /* The initial handshake must be over */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5114 | if (mbedtls_ssl_is_handshake_over(ssl) == 0) { |
| 5115 | MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over")); |
| 5116 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5117 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5118 | if (ssl->handshake != NULL) { |
| 5119 | MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed")); |
| 5120 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5121 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5122 | /* Double-check that sub-structures are indeed ready */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5123 | if (ssl->transform == NULL || ssl->session == NULL) { |
| 5124 | MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready")); |
| 5125 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5126 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5127 | /* There must be no pending incoming or outgoing data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5128 | if (mbedtls_ssl_check_pending(ssl) != 0) { |
| 5129 | MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data")); |
| 5130 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5131 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5132 | if (ssl->out_left != 0) { |
| 5133 | MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data")); |
| 5134 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5135 | } |
Dave Rodgman | 556e8a3 | 2022-12-06 16:31:25 +0000 | [diff] [blame] | 5136 | /* Protocol must be DTLS, not TLS */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5137 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 5138 | MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported")); |
| 5139 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5140 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5141 | /* Version must be 1.2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5142 | if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) { |
| 5143 | MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported")); |
| 5144 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5145 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5146 | /* We must be using an AEAD ciphersuite */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5147 | if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) { |
| 5148 | MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported")); |
| 5149 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5150 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5151 | /* Renegotiation must not be enabled */ |
| 5152 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5153 | if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) { |
| 5154 | MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled")); |
| 5155 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5156 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5157 | #endif |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5158 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5159 | /* |
| 5160 | * Version and format identifier |
| 5161 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5162 | used += sizeof(ssl_serialized_context_header); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5164 | if (used <= buf_len) { |
| 5165 | memcpy(p, ssl_serialized_context_header, |
| 5166 | sizeof(ssl_serialized_context_header)); |
| 5167 | p += sizeof(ssl_serialized_context_header); |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5168 | } |
| 5169 | |
| 5170 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5171 | * Session (length + data) |
| 5172 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5173 | ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len); |
| 5174 | if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) { |
| 5175 | return ret; |
| 5176 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5177 | |
| 5178 | used += 4 + session_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5179 | if (used <= buf_len) { |
| 5180 | MBEDTLS_PUT_UINT32_BE(session_len, p, 0); |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5181 | p += 4; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5183 | ret = ssl_session_save(ssl->session, 1, |
| 5184 | p, session_len, &session_len); |
| 5185 | if (ret != 0) { |
| 5186 | return ret; |
| 5187 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5188 | |
| 5189 | p += session_len; |
| 5190 | } |
| 5191 | |
| 5192 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5193 | * Transform |
| 5194 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5195 | used += sizeof(ssl->transform->randbytes); |
| 5196 | if (used <= buf_len) { |
| 5197 | memcpy(p, ssl->transform->randbytes, |
| 5198 | sizeof(ssl->transform->randbytes)); |
| 5199 | p += sizeof(ssl->transform->randbytes); |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5200 | } |
| 5201 | |
| 5202 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Dave Rodgman | c37ad44 | 2023-11-03 23:36:06 +0000 | [diff] [blame] | 5203 | used += 2U + ssl->transform->in_cid_len + ssl->transform->out_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5204 | if (used <= buf_len) { |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5205 | *p++ = ssl->transform->in_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5206 | 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] | 5207 | p += ssl->transform->in_cid_len; |
| 5208 | |
| 5209 | *p++ = ssl->transform->out_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5210 | 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] | 5211 | p += ssl->transform->out_cid_len; |
| 5212 | } |
| 5213 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 5214 | |
| 5215 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5216 | * Saved fields from top-level ssl_context structure |
| 5217 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5218 | used += 4; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5219 | if (used <= buf_len) { |
Gilles Peskine | f6a676d | 2025-02-17 16:10:14 +0100 | [diff] [blame] | 5220 | MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen_or_in_hsfraglen, p, 0); |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5221 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5222 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5223 | |
| 5224 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 5225 | used += 16; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5226 | if (used <= buf_len) { |
| 5227 | MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0); |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5228 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5230 | MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0); |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5231 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5232 | } |
| 5233 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 5234 | |
| 5235 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5236 | used += 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5237 | if (used <= buf_len) { |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5238 | *p++ = ssl->disable_datagram_packing; |
| 5239 | } |
| 5240 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5241 | |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 5242 | used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5243 | if (used <= buf_len) { |
| 5244 | memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN); |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 5245 | p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5246 | } |
| 5247 | |
| 5248 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5249 | used += 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5250 | if (used <= buf_len) { |
| 5251 | MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0); |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5252 | p += 2; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5253 | } |
| 5254 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5255 | |
| 5256 | #if defined(MBEDTLS_SSL_ALPN) |
| 5257 | { |
| 5258 | const uint8_t alpn_len = ssl->alpn_chosen |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5259 | ? (uint8_t) strlen(ssl->alpn_chosen) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5260 | : 0; |
| 5261 | |
| 5262 | used += 1 + alpn_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5263 | if (used <= buf_len) { |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5264 | *p++ = alpn_len; |
| 5265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5266 | if (ssl->alpn_chosen != NULL) { |
| 5267 | memcpy(p, ssl->alpn_chosen, alpn_len); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5268 | p += alpn_len; |
| 5269 | } |
| 5270 | } |
| 5271 | } |
| 5272 | #endif /* MBEDTLS_SSL_ALPN */ |
| 5273 | |
| 5274 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5275 | * Done |
| 5276 | */ |
| 5277 | *olen = used; |
| 5278 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5279 | if (used > buf_len) { |
| 5280 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 5281 | } |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5282 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5283 | MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5284 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5285 | return mbedtls_ssl_session_reset_int(ssl, 0); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5286 | } |
| 5287 | |
| 5288 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 5289 | * Deserialize context, see mbedtls_ssl_context_save() for format. |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5290 | * |
| 5291 | * This internal version is wrapped by a public function that cleans up in |
| 5292 | * case of error. |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5293 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5294 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5295 | static int ssl_context_load(mbedtls_ssl_context *ssl, |
| 5296 | const unsigned char *buf, |
| 5297 | size_t len) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5298 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5299 | const unsigned char *p = buf; |
| 5300 | const unsigned char * const end = buf + len; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5301 | size_t session_len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5302 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 5303 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 5304 | tls_prf_fn prf_func = NULL; |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 5305 | #endif |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5306 | |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 5307 | /* |
| 5308 | * The context should have been freshly setup or reset. |
| 5309 | * Give the user an error in case of obvious misuse. |
Manuel Pégourié-Gonnard | 4ca930f | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 5310 | * (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] | 5311 | * renegotiating, or if the user mistakenly loaded a session first.) |
| 5312 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5313 | if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST || |
| 5314 | ssl->session != NULL) { |
| 5315 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 5316 | } |
| 5317 | |
| 5318 | /* |
| 5319 | * We can't check that the config matches the initial one, but we can at |
| 5320 | * least check it matches the requirements for serializing. |
| 5321 | */ |
Dave Rodgman | e99b24d | 2023-09-14 15:45:03 +0100 | [diff] [blame] | 5322 | if ( |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 5323 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 5324 | ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 5325 | #endif |
Dave Rodgman | e99b24d | 2023-09-14 15:45:03 +0100 | [diff] [blame] | 5326 | ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
| 5327 | ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 || |
| 5328 | ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 |
| 5329 | ) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5330 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 5331 | } |
| 5332 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5333 | MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5334 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5335 | /* |
| 5336 | * Check version identifier |
| 5337 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5338 | if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) { |
| 5339 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5340 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5341 | |
| 5342 | if (memcmp(p, ssl_serialized_context_header, |
| 5343 | sizeof(ssl_serialized_context_header)) != 0) { |
| 5344 | return MBEDTLS_ERR_SSL_VERSION_MISMATCH; |
| 5345 | } |
| 5346 | p += sizeof(ssl_serialized_context_header); |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5347 | |
| 5348 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5349 | * Session |
| 5350 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5351 | if ((size_t) (end - p) < 4) { |
| 5352 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5353 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5354 | |
Thomas Daubney | f9f0ba8 | 2023-05-23 17:34:33 +0100 | [diff] [blame] | 5355 | session_len = MBEDTLS_GET_UINT32_BE(p, 0); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5356 | p += 4; |
| 5357 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 5358 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 5359 | * 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] | 5360 | ssl->session = ssl->session_negotiate; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5361 | ssl->session_in = ssl->session; |
| 5362 | ssl->session_out = ssl->session; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 5363 | ssl->session_negotiate = NULL; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5364 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5365 | if ((size_t) (end - p) < session_len) { |
| 5366 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5367 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5368 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5369 | ret = ssl_session_load(ssl->session, 1, p, session_len); |
| 5370 | if (ret != 0) { |
| 5371 | mbedtls_ssl_session_free(ssl->session); |
| 5372 | return ret; |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5373 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5374 | |
| 5375 | p += session_len; |
| 5376 | |
| 5377 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5378 | * Transform |
| 5379 | */ |
| 5380 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 5381 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 5382 | * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */ |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 5383 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 5384 | ssl->transform = ssl->transform_negotiate; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5385 | ssl->transform_in = ssl->transform; |
| 5386 | ssl->transform_out = ssl->transform; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 5387 | ssl->transform_negotiate = NULL; |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 5388 | #endif |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5389 | |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 5390 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5391 | prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite); |
| 5392 | if (prf_func == NULL) { |
| 5393 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5394 | } |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 5395 | |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5396 | /* Read random bytes and populate structure */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5397 | if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) { |
| 5398 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5399 | } |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 5400 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5401 | ret = ssl_tls12_populate_transform(ssl->transform, |
| 5402 | ssl->session->ciphersuite, |
| 5403 | ssl->session->master, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 5404 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5405 | ssl->session->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 5406 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5407 | prf_func, |
| 5408 | p, /* currently pointing to randbytes */ |
| 5409 | MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */ |
| 5410 | ssl->conf->endpoint, |
| 5411 | ssl); |
| 5412 | if (ret != 0) { |
| 5413 | return ret; |
| 5414 | } |
Jerry Yu | 840fbb2 | 2022-02-17 14:59:29 +0800 | [diff] [blame] | 5415 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5416 | p += sizeof(ssl->transform->randbytes); |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5417 | |
| 5418 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 5419 | /* Read connection IDs and store them */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5420 | if ((size_t) (end - p) < 1) { |
| 5421 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5422 | } |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5423 | |
| 5424 | ssl->transform->in_cid_len = *p++; |
| 5425 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5426 | if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) { |
| 5427 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5428 | } |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5429 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5430 | 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] | 5431 | p += ssl->transform->in_cid_len; |
| 5432 | |
| 5433 | ssl->transform->out_cid_len = *p++; |
| 5434 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5435 | if ((size_t) (end - p) < ssl->transform->out_cid_len) { |
| 5436 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5437 | } |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5438 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5439 | 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] | 5440 | p += ssl->transform->out_cid_len; |
| 5441 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 5442 | |
| 5443 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5444 | * Saved fields from top-level ssl_context structure |
| 5445 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5446 | if ((size_t) (end - p) < 4) { |
| 5447 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5448 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5449 | |
Gilles Peskine | f6a676d | 2025-02-17 16:10:14 +0100 | [diff] [blame] | 5450 | ssl->badmac_seen_or_in_hsfraglen = MBEDTLS_GET_UINT32_BE(p, 0); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5451 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5452 | |
| 5453 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5454 | if ((size_t) (end - p) < 16) { |
| 5455 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5456 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5457 | |
Thomas Daubney | f9f0ba8 | 2023-05-23 17:34:33 +0100 | [diff] [blame] | 5458 | ssl->in_window_top = MBEDTLS_GET_UINT64_BE(p, 0); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5459 | p += 8; |
| 5460 | |
Thomas Daubney | f9f0ba8 | 2023-05-23 17:34:33 +0100 | [diff] [blame] | 5461 | ssl->in_window = MBEDTLS_GET_UINT64_BE(p, 0); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5462 | p += 8; |
| 5463 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 5464 | |
| 5465 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5466 | if ((size_t) (end - p) < 1) { |
| 5467 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5468 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5469 | |
| 5470 | ssl->disable_datagram_packing = *p++; |
| 5471 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5472 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5473 | if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) { |
| 5474 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5475 | } |
| 5476 | memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr)); |
| 5477 | p += sizeof(ssl->cur_out_ctr); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5478 | |
| 5479 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5480 | if ((size_t) (end - p) < 2) { |
| 5481 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5482 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5483 | |
Dave Rodgman | a3d0f61 | 2023-11-03 23:34:02 +0000 | [diff] [blame] | 5484 | ssl->mtu = MBEDTLS_GET_UINT16_BE(p, 0); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5485 | p += 2; |
| 5486 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5487 | |
| 5488 | #if defined(MBEDTLS_SSL_ALPN) |
| 5489 | { |
| 5490 | uint8_t alpn_len; |
| 5491 | const char **cur; |
| 5492 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5493 | if ((size_t) (end - p) < 1) { |
| 5494 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5495 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5496 | |
| 5497 | alpn_len = *p++; |
| 5498 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5499 | if (alpn_len != 0 && ssl->conf->alpn_list != NULL) { |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5500 | /* alpn_chosen should point to an item in the configured list */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5501 | for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) { |
| 5502 | if (strlen(*cur) == alpn_len && |
Waleed Elmelegy | 131b2ff | 2024-03-14 01:39:39 +0000 | [diff] [blame] | 5503 | memcmp(p, *cur, alpn_len) == 0) { |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5504 | ssl->alpn_chosen = *cur; |
| 5505 | break; |
| 5506 | } |
| 5507 | } |
| 5508 | } |
| 5509 | |
| 5510 | /* can only happen on conf mismatch */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5511 | if (alpn_len != 0 && ssl->alpn_chosen == NULL) { |
| 5512 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5513 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5514 | |
| 5515 | p += alpn_len; |
| 5516 | } |
| 5517 | #endif /* MBEDTLS_SSL_ALPN */ |
| 5518 | |
| 5519 | /* |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 5520 | * Forced fields from top-level ssl_context structure |
| 5521 | * |
| 5522 | * Most of them already set to the correct value by mbedtls_ssl_init() and |
| 5523 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. |
| 5524 | */ |
Gilles Peskine | 49f179d | 2025-03-07 15:09:32 +0100 | [diff] [blame] | 5525 | mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER); |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 5526 | ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 5527 | |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 5528 | /* Adjust pointers for header fields of outgoing records to |
| 5529 | * the given transform, accounting for explicit IV and CID. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5530 | mbedtls_ssl_update_out_pointers(ssl, ssl->transform); |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 5531 | |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 5532 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5533 | ssl->in_epoch = 1; |
| 5534 | #endif |
| 5535 | |
| 5536 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, |
| 5537 | * 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] | 5538 | * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform() |
| 5539 | * inappropriately. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5540 | if (ssl->handshake != NULL) { |
| 5541 | mbedtls_ssl_handshake_free(ssl); |
| 5542 | mbedtls_free(ssl->handshake); |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 5543 | ssl->handshake = NULL; |
| 5544 | } |
| 5545 | |
| 5546 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5547 | * Done - should have consumed entire buffer |
| 5548 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5549 | if (p != end) { |
| 5550 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5551 | } |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5552 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5553 | return 0; |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5554 | } |
| 5555 | |
| 5556 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 5557 | * Deserialize context: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5558 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5559 | int mbedtls_ssl_context_load(mbedtls_ssl_context *context, |
| 5560 | const unsigned char *buf, |
| 5561 | size_t len) |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5562 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5563 | int ret = ssl_context_load(context, buf, len); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5564 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5565 | if (ret != 0) { |
| 5566 | mbedtls_ssl_free(context); |
| 5567 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5568 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5569 | return ret; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5570 | } |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 5571 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5572 | |
| 5573 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5574 | * Free an SSL context |
| 5575 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5576 | void mbedtls_ssl_free(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5577 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5578 | if (ssl == NULL) { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 5579 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5580 | } |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 5581 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5582 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> free")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5583 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5584 | if (ssl->out_buf != NULL) { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 5585 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 5586 | size_t out_buf_len = ssl->out_buf_len; |
| 5587 | #else |
| 5588 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 5589 | #endif |
| 5590 | |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 5591 | mbedtls_zeroize_and_free(ssl->out_buf, out_buf_len); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5592 | ssl->out_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5593 | } |
| 5594 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5595 | if (ssl->in_buf != NULL) { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 5596 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 5597 | size_t in_buf_len = ssl->in_buf_len; |
| 5598 | #else |
| 5599 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 5600 | #endif |
| 5601 | |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 5602 | mbedtls_zeroize_and_free(ssl->in_buf, in_buf_len); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5603 | ssl->in_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5604 | } |
| 5605 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5606 | if (ssl->transform) { |
| 5607 | mbedtls_ssl_transform_free(ssl->transform); |
| 5608 | mbedtls_free(ssl->transform); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5609 | } |
| 5610 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5611 | if (ssl->handshake) { |
| 5612 | mbedtls_ssl_handshake_free(ssl); |
| 5613 | mbedtls_free(ssl->handshake); |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 5614 | |
| 5615 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5616 | mbedtls_ssl_transform_free(ssl->transform_negotiate); |
| 5617 | mbedtls_free(ssl->transform_negotiate); |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 5618 | #endif |
| 5619 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5620 | mbedtls_ssl_session_free(ssl->session_negotiate); |
| 5621 | mbedtls_free(ssl->session_negotiate); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5622 | } |
| 5623 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5624 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5625 | mbedtls_ssl_transform_free(ssl->transform_application); |
| 5626 | mbedtls_free(ssl->transform_application); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5627 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 5628 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5629 | if (ssl->session) { |
| 5630 | mbedtls_ssl_session_free(ssl->session); |
| 5631 | mbedtls_free(ssl->session); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 5632 | } |
| 5633 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 5634 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | e61852e | 2025-02-12 23:28:48 +0100 | [diff] [blame] | 5635 | mbedtls_ssl_free_hostname(ssl); |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 5636 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5637 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 5638 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5639 | mbedtls_free(ssl->cli_id); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 5640 | #endif |
| 5641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5642 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= free")); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 5643 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 5644 | /* Actually clear after last debug message */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5645 | mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context)); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5646 | } |
| 5647 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5648 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 5649 | * Initialize mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5650 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5651 | void mbedtls_ssl_config_init(mbedtls_ssl_config *conf) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5652 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5653 | memset(conf, 0, sizeof(mbedtls_ssl_config)); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5654 | } |
| 5655 | |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 5656 | /* The selection should be the same as mbedtls_x509_crt_profile_default in |
| 5657 | * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters: |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 5658 | * curves with a lower resource usage come first. |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 5659 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 5660 | * about this list. |
| 5661 | */ |
Chien Wong | 4e9683e | 2023-12-28 17:07:43 +0800 | [diff] [blame] | 5662 | static const uint16_t ssl_preset_default_groups[] = { |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 5663 | #if defined(MBEDTLS_ECP_HAVE_CURVE25519) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5664 | MBEDTLS_SSL_IANA_TLS_GROUP_X25519, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 5665 | #endif |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 5666 | #if defined(MBEDTLS_ECP_HAVE_SECP256R1) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5667 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 5668 | #endif |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 5669 | #if defined(MBEDTLS_ECP_HAVE_SECP384R1) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5670 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 5671 | #endif |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 5672 | #if defined(MBEDTLS_ECP_HAVE_CURVE448) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5673 | MBEDTLS_SSL_IANA_TLS_GROUP_X448, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 5674 | #endif |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 5675 | #if defined(MBEDTLS_ECP_HAVE_SECP521R1) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5676 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 5677 | #endif |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 5678 | #if defined(MBEDTLS_ECP_HAVE_BP256R1) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5679 | MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 5680 | #endif |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 5681 | #if defined(MBEDTLS_ECP_HAVE_BP384R1) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5682 | MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 5683 | #endif |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 5684 | #if defined(MBEDTLS_ECP_HAVE_BP512R1) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5685 | MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 5686 | #endif |
Przemek Stekiel | 316c19e | 2023-05-31 15:25:11 +0200 | [diff] [blame] | 5687 | #if defined(PSA_WANT_ALG_FFDH) |
Przemek Stekiel | 383f471 | 2022-12-12 14:48:57 +0100 | [diff] [blame] | 5688 | MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048, |
| 5689 | MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072, |
| 5690 | MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096, |
| 5691 | MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144, |
| 5692 | MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192, |
| 5693 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5694 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 5695 | }; |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 5696 | |
Alexey Tsvetkov | 2ca3437 | 2022-06-07 10:21:05 +0300 | [diff] [blame] | 5697 | static const int ssl_preset_suiteb_ciphersuites[] = { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5698 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 5699 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 5700 | 0 |
| 5701 | }; |
| 5702 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5703 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 5704 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5705 | /* NOTICE: |
Jerry Yu | 0b994b8 | 2022-01-25 17:22:12 +0800 | [diff] [blame] | 5706 | * 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] | 5707 | * rules SHOULD be upheld. |
| 5708 | * - No duplicate entries. |
| 5709 | * - 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] | 5710 | * - ssl_tls12_preset* is for TLS 1.2 use only. |
Jerry Yu | 370e146 | 2022-01-25 10:36:53 +0800 | [diff] [blame] | 5711 | * - 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] | 5712 | */ |
Chien Wong | 4e9683e | 2023-12-28 17:07:43 +0800 | [diff] [blame] | 5713 | static const uint16_t ssl_preset_default_sig_algs[] = { |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5714 | |
Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5715 | #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] | 5716 | defined(MBEDTLS_MD_CAN_SHA256) && \ |
Valerio Setti | cf29c5d | 2023-09-01 09:03:41 +0200 | [diff] [blame] | 5717 | defined(PSA_WANT_ECC_SECP_R1_256) |
Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 5718 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5719 | // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256) |
| 5720 | #endif |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5721 | |
Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5722 | #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] | 5723 | defined(MBEDTLS_MD_CAN_SHA384) && \ |
Valerio Setti | cf29c5d | 2023-09-01 09:03:41 +0200 | [diff] [blame] | 5724 | defined(PSA_WANT_ECC_SECP_R1_384) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5725 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5726 | // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384) |
| 5727 | #endif |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5728 | |
Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5729 | #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] | 5730 | defined(MBEDTLS_MD_CAN_SHA512) && \ |
Valerio Setti | cf29c5d | 2023-09-01 09:03:41 +0200 | [diff] [blame] | 5731 | defined(PSA_WANT_ECC_SECP_R1_521) |
Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 5732 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512, |
Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5733 | // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512) |
| 5734 | #endif |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5735 | |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5736 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_MD_CAN_SHA512) |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 5737 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5738 | #endif |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 5739 | |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5740 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_MD_CAN_SHA384) |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 5741 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5742 | #endif |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 5743 | |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5744 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_MD_CAN_SHA256) |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 5745 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5746 | #endif |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 5747 | |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5748 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA512) |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 5749 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512, |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5750 | #endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA512 */ |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 5751 | |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5752 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA384) |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 5753 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384, |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5754 | #endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA384 */ |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 5755 | |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5756 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA256) |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 5757 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5758 | #endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA256 */ |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 5759 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5760 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5761 | }; |
| 5762 | |
| 5763 | /* NOTICE: see above */ |
| 5764 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Deomid rojer Ryabkov | e3aaf82 | 2024-03-18 12:32:30 +0000 | [diff] [blame] | 5765 | static const uint16_t ssl_tls12_preset_default_sig_algs[] = { |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5766 | |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5767 | #if defined(MBEDTLS_MD_CAN_SHA512) |
Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 5768 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5769 | 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] | 5770 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 5771 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 5772 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5773 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5774 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5775 | 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] | 5776 | #endif |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5777 | #endif /* MBEDTLS_MD_CAN_SHA512 */ |
| 5778 | |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5779 | #if defined(MBEDTLS_MD_CAN_SHA384) |
Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 5780 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5781 | 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] | 5782 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 5783 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 5784 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5785 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5786 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5787 | 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] | 5788 | #endif |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5789 | #endif /* MBEDTLS_MD_CAN_SHA384 */ |
| 5790 | |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5791 | #if defined(MBEDTLS_MD_CAN_SHA256) |
Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 5792 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5793 | 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] | 5794 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 5795 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 5796 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5797 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5798 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5799 | 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] | 5800 | #endif |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5801 | #endif /* MBEDTLS_MD_CAN_SHA256 */ |
| 5802 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5803 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5804 | }; |
| 5805 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5806 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5807 | /* NOTICE: see above */ |
Chien Wong | 4e9683e | 2023-12-28 17:07:43 +0800 | [diff] [blame] | 5808 | static const uint16_t ssl_preset_suiteb_sig_algs[] = { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5809 | |
Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5810 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \ |
Valerio Setti | 45d56f3 | 2023-07-13 17:23:20 +0200 | [diff] [blame] | 5811 | defined(MBEDTLS_MD_CAN_SHA256) && \ |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 5812 | defined(MBEDTLS_ECP_HAVE_SECP256R1) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5813 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5814 | // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256) |
| 5815 | #endif |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5816 | |
Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5817 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \ |
Valerio Setti | 45d56f3 | 2023-07-13 17:23:20 +0200 | [diff] [blame] | 5818 | defined(MBEDTLS_MD_CAN_SHA384) && \ |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 5819 | defined(MBEDTLS_ECP_HAVE_SECP384R1) |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 5820 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5821 | // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384) |
| 5822 | #endif |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5823 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5824 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 5825 | }; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 5826 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5827 | /* NOTICE: see above */ |
| 5828 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Deomid rojer Ryabkov | fb5e2e5 | 2024-03-20 00:43:34 +0000 | [diff] [blame] | 5829 | static const uint16_t ssl_tls12_preset_suiteb_sig_algs[] = { |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5830 | |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5831 | #if defined(MBEDTLS_MD_CAN_SHA256) |
Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 5832 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5833 | 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] | 5834 | #endif |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5835 | #endif /* MBEDTLS_MD_CAN_SHA256 */ |
Yanray Wang | 69ceb39 | 2023-11-22 16:32:39 +0800 | [diff] [blame] | 5836 | |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 5837 | #if defined(MBEDTLS_MD_CAN_SHA384) |
Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 5838 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5839 | 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] | 5840 | #endif |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5841 | #endif /* MBEDTLS_MD_CAN_SHA384 */ |
| 5842 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5843 | MBEDTLS_TLS_SIG_NONE |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 5844 | }; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5845 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5846 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5847 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5848 | |
Chien Wong | 4e9683e | 2023-12-28 17:07:43 +0800 | [diff] [blame] | 5849 | static const uint16_t ssl_preset_suiteb_groups[] = { |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 5850 | #if defined(MBEDTLS_ECP_HAVE_SECP256R1) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5851 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 5852 | #endif |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 5853 | #if defined(MBEDTLS_ECP_HAVE_SECP384R1) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5854 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 5855 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5856 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5857 | }; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5858 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5859 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5860 | /* 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] | 5861 | * to make sure there are no duplicated signature algorithm entries. */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5862 | MBEDTLS_CHECK_RETURN_CRITICAL |
Chien Wong | 4e9683e | 2023-12-28 17:07:43 +0800 | [diff] [blame] | 5863 | static int ssl_check_no_sig_alg_duplication(const uint16_t *sig_algs) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5864 | { |
| 5865 | size_t i, j; |
| 5866 | int ret = 0; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5867 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5868 | for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) { |
| 5869 | for (j = 0; j < i; j++) { |
| 5870 | if (sig_algs[i] != sig_algs[j]) { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 5871 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5872 | } |
| 5873 | mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET |
| 5874 | ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n", |
| 5875 | sig_algs[i], j, i); |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 5876 | ret = -1; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5877 | } |
| 5878 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5879 | return ret; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5880 | } |
| 5881 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5882 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5883 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5884 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 5885 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5886 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5887 | int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf, |
| 5888 | int endpoint, int transport, int preset) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5889 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 5890 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5891 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 5892 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5893 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5894 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5895 | if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) { |
| 5896 | mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n"); |
| 5897 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5898 | } |
| 5899 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5900 | if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) { |
| 5901 | mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n"); |
| 5902 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5903 | } |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5904 | |
| 5905 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5906 | if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) { |
| 5907 | mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n"); |
| 5908 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5909 | } |
| 5910 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5911 | if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) { |
| 5912 | mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n"); |
| 5913 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5914 | } |
| 5915 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5916 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5917 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 5918 | /* Use the functions here so that they are covered in tests, |
| 5919 | * but otherwise access member directly for efficiency */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5920 | mbedtls_ssl_conf_endpoint(conf, endpoint); |
| 5921 | mbedtls_ssl_conf_transport(conf, transport); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5922 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5923 | /* |
| 5924 | * Things that are common to all presets |
| 5925 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 5926 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5927 | if (endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 5928 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 5929 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Ronald Cron | d67f801 | 2024-08-28 07:45:57 +0200 | [diff] [blame] | 5930 | mbedtls_ssl_conf_session_tickets(conf, MBEDTLS_SSL_SESSION_TICKETS_ENABLED); |
Ronald Cron | bedddd7 | 2024-08-27 14:18:50 +0200 | [diff] [blame] | 5931 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Ronald Cron | c46edd4 | 2024-08-28 16:54:42 +0200 | [diff] [blame] | 5932 | /* Contrary to TLS 1.2 tickets, TLS 1.3 NewSessionTicket message |
| 5933 | * handling is disabled by default in Mbed TLS 3.6.x for backward |
| 5934 | * compatibility with client applications developed using Mbed TLS 3.5 |
| 5935 | * or earlier with the default configuration. |
| 5936 | * |
| 5937 | * Up to Mbed TLS 3.5, in the default configuration TLS 1.3 was |
| 5938 | * disabled, and a Mbed TLS client with the default configuration would |
| 5939 | * establish a TLS 1.2 connection with a TLS 1.2 and TLS 1.3 capable |
| 5940 | * server. |
| 5941 | * |
| 5942 | * Starting with Mbed TLS 3.6.0, TLS 1.3 is enabled by default, and thus |
| 5943 | * an Mbed TLS client with the default configuration establishes a |
| 5944 | * TLS 1.3 connection with a TLS 1.2 and TLS 1.3 capable server. If |
| 5945 | * following the handshake the TLS 1.3 server sends NewSessionTicket |
| 5946 | * messages and the Mbed TLS client processes them, this results in |
| 5947 | * Mbed TLS high level APIs (mbedtls_ssl_read(), |
| 5948 | * mbedtls_ssl_handshake(), ...) to eventually return an |
| 5949 | * #MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET non fatal error code |
| 5950 | * (see the documentation of mbedtls_ssl_read() for more information on |
| 5951 | * that error code). Applications unaware of that TLS 1.3 specific non |
| 5952 | * fatal error code are then failing. |
| 5953 | */ |
Ronald Cron | 9f44c88 | 2024-08-28 16:44:10 +0200 | [diff] [blame] | 5954 | mbedtls_ssl_conf_tls13_enable_signal_new_session_tickets( |
| 5955 | conf, MBEDTLS_SSL_TLS1_3_SIGNAL_NEW_SESSION_TICKETS_DISABLED); |
Ronald Cron | bedddd7 | 2024-08-27 14:18:50 +0200 | [diff] [blame] | 5956 | #endif |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 5957 | #endif |
| 5958 | } |
| 5959 | #endif |
| 5960 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5961 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 5962 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 5963 | #endif |
| 5964 | |
| 5965 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 5966 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 5967 | #endif |
| 5968 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 5969 | #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] | 5970 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 5971 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 5972 | #endif |
| 5973 | |
| 5974 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 5975 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 5976 | #endif |
| 5977 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 5978 | #if defined(MBEDTLS_SSL_SRV_C) |
| 5979 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
TRodziewicz | 3946f79 | 2021-06-14 12:11:18 +0200 | [diff] [blame] | 5980 | conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER; |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 5981 | #endif |
| 5982 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5983 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5984 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
| 5985 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
| 5986 | #endif |
| 5987 | |
| 5988 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 5989 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5990 | memset(conf->renego_period, 0x00, 2); |
| 5991 | memset(conf->renego_period + 2, 0xFF, 6); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5992 | #endif |
| 5993 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5994 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5995 | if (endpoint == MBEDTLS_SSL_IS_SERVER) { |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 5996 | const unsigned char dhm_p[] = |
| 5997 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 5998 | const unsigned char dhm_g[] = |
| 5999 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 6000 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6001 | if ((ret = mbedtls_ssl_conf_dh_param_bin(conf, |
| 6002 | dhm_p, sizeof(dhm_p), |
| 6003 | dhm_g, sizeof(dhm_g))) != 0) { |
| 6004 | return ret; |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 6005 | } |
| 6006 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 6007 | #endif |
| 6008 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 6009 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 6ee56aa | 2022-12-06 17:47:22 +0800 | [diff] [blame] | 6010 | |
| 6011 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
Yanray Wang | d5ed36f | 2023-11-07 11:40:43 +0800 | [diff] [blame] | 6012 | mbedtls_ssl_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED); |
Jerry Yu | 6ee56aa | 2022-12-06 17:47:22 +0800 | [diff] [blame] | 6013 | #if defined(MBEDTLS_SSL_SRV_C) |
Yanray Wang | 0751761 | 2023-11-07 11:47:36 +0800 | [diff] [blame] | 6014 | 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] | 6015 | #endif |
| 6016 | #endif /* MBEDTLS_SSL_EARLY_DATA */ |
| 6017 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 6018 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 6019 | mbedtls_ssl_conf_new_session_tickets( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6020 | conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS); |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 6021 | #endif |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 6022 | /* |
| 6023 | * Allow all TLS 1.3 key exchange modes by default. |
| 6024 | */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 6025 | conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL; |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 6026 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 6027 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6028 | if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 6029 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 6030 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 6031 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 6032 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6033 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 6034 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6035 | } else { |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 6036 | #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] | 6037 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 6038 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 6039 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 6040 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 6041 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 6042 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6043 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 6044 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 6045 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6046 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 6047 | #endif |
| 6048 | } |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 6049 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6050 | /* |
| 6051 | * Preset-specific defaults |
| 6052 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6053 | switch (preset) { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6054 | /* |
| 6055 | * NSA Suite B |
| 6056 | */ |
| 6057 | case MBEDTLS_SSL_PRESET_SUITEB: |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6058 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 6059 | conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6060 | |
| 6061 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6062 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6063 | #endif |
| 6064 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 6065 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6066 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6067 | if (mbedtls_ssl_conf_is_tls12_only(conf)) { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6068 | conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6069 | } else |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6070 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6071 | conf->sig_algs = ssl_preset_suiteb_sig_algs; |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 6072 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6073 | |
Manuel Pégourié-Gonnard | f07ce3b | 2023-09-22 11:53:41 +0200 | [diff] [blame] | 6074 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6075 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6076 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6077 | conf->group_list = ssl_preset_suiteb_groups; |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 6078 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6079 | |
| 6080 | /* |
| 6081 | * Default |
| 6082 | */ |
| 6083 | default: |
Ronald Cron | f660655 | 2022-03-15 11:23:25 +0100 | [diff] [blame] | 6084 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 6085 | conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites(); |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6086 | |
| 6087 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6088 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 6089 | #endif |
| 6090 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 6091 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6092 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6093 | if (mbedtls_ssl_conf_is_tls12_only(conf)) { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6094 | conf->sig_algs = ssl_tls12_preset_default_sig_algs; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6095 | } else |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6096 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6097 | conf->sig_algs = ssl_preset_default_sig_algs; |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 6098 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6099 | |
Manuel Pégourié-Gonnard | f07ce3b | 2023-09-22 11:53:41 +0200 | [diff] [blame] | 6100 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6101 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6102 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6103 | conf->group_list = ssl_preset_default_groups; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6104 | |
| 6105 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 6106 | conf->dhm_min_bitlen = 1024; |
| 6107 | #endif |
| 6108 | } |
| 6109 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6110 | return 0; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6111 | } |
| 6112 | |
| 6113 | /* |
| 6114 | * Free mbedtls_ssl_config |
| 6115 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6116 | void mbedtls_ssl_config_free(mbedtls_ssl_config *conf) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6117 | { |
Troy-Butler | da73abc | 2024-04-02 13:37:31 -0400 | [diff] [blame] | 6118 | if (conf == NULL) { |
| 6119 | return; |
| 6120 | } |
| 6121 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6122 | #if defined(MBEDTLS_DHM_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6123 | mbedtls_mpi_free(&conf->dhm_P); |
| 6124 | mbedtls_mpi_free(&conf->dhm_G); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6125 | #endif |
| 6126 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 6127 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 6128 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6129 | if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 6130 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 6131 | } |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 6132 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6133 | if (conf->psk != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 6134 | mbedtls_zeroize_and_free(conf->psk, conf->psk_len); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 6135 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6136 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 6137 | } |
| 6138 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6139 | if (conf->psk_identity != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 6140 | mbedtls_zeroize_and_free(conf->psk_identity, conf->psk_identity_len); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 6141 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6142 | conf->psk_identity_len = 0; |
| 6143 | } |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 6144 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6145 | |
| 6146 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6147 | ssl_key_cert_free(conf->key_cert); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6148 | #endif |
| 6149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6150 | mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config)); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6151 | } |
| 6152 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 6153 | #if defined(MBEDTLS_PK_C) && \ |
Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 6154 | (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] | 6155 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6156 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6157 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6158 | 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] | 6159 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6160 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6161 | if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) { |
| 6162 | return MBEDTLS_SSL_SIG_RSA; |
| 6163 | } |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6164 | #endif |
Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 6165 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6166 | if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) { |
| 6167 | return MBEDTLS_SSL_SIG_ECDSA; |
| 6168 | } |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6169 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6170 | return MBEDTLS_SSL_SIG_ANON; |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6171 | } |
| 6172 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6173 | 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] | 6174 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6175 | switch (type) { |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6176 | case MBEDTLS_PK_RSA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6177 | return MBEDTLS_SSL_SIG_RSA; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6178 | case MBEDTLS_PK_ECDSA: |
| 6179 | case MBEDTLS_PK_ECKEY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6180 | return MBEDTLS_SSL_SIG_ECDSA; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6181 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6182 | return MBEDTLS_SSL_SIG_ANON; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6183 | } |
| 6184 | } |
| 6185 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6186 | 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] | 6187 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6188 | switch (sig) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6189 | #if defined(MBEDTLS_RSA_C) |
| 6190 | case MBEDTLS_SSL_SIG_RSA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6191 | return MBEDTLS_PK_RSA; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6192 | #endif |
Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 6193 | #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] | 6194 | case MBEDTLS_SSL_SIG_ECDSA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6195 | return MBEDTLS_PK_ECDSA; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6196 | #endif |
| 6197 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6198 | return MBEDTLS_PK_NONE; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6199 | } |
| 6200 | } |
Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 6201 | #endif /* MBEDTLS_PK_C && |
| 6202 | ( 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] | 6203 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 6204 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6205 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 6206 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6207 | 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] | 6208 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6209 | switch (hash) { |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6210 | #if defined(MBEDTLS_MD_CAN_MD5) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6211 | case MBEDTLS_SSL_HASH_MD5: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6212 | return MBEDTLS_MD_MD5; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6213 | #endif |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6214 | #if defined(MBEDTLS_MD_CAN_SHA1) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6215 | case MBEDTLS_SSL_HASH_SHA1: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6216 | return MBEDTLS_MD_SHA1; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6217 | #endif |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6218 | #if defined(MBEDTLS_MD_CAN_SHA224) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6219 | case MBEDTLS_SSL_HASH_SHA224: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6220 | return MBEDTLS_MD_SHA224; |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 6221 | #endif |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6222 | #if defined(MBEDTLS_MD_CAN_SHA256) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6223 | case MBEDTLS_SSL_HASH_SHA256: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6224 | return MBEDTLS_MD_SHA256; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6225 | #endif |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6226 | #if defined(MBEDTLS_MD_CAN_SHA384) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6227 | case MBEDTLS_SSL_HASH_SHA384: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6228 | return MBEDTLS_MD_SHA384; |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 6229 | #endif |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6230 | #if defined(MBEDTLS_MD_CAN_SHA512) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6231 | case MBEDTLS_SSL_HASH_SHA512: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6232 | return MBEDTLS_MD_SHA512; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6233 | #endif |
| 6234 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6235 | return MBEDTLS_MD_NONE; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6236 | } |
| 6237 | } |
| 6238 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6239 | /* |
| 6240 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 6241 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6242 | unsigned char mbedtls_ssl_hash_from_md_alg(int md) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6243 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6244 | switch (md) { |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6245 | #if defined(MBEDTLS_MD_CAN_MD5) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6246 | case MBEDTLS_MD_MD5: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6247 | return MBEDTLS_SSL_HASH_MD5; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6248 | #endif |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6249 | #if defined(MBEDTLS_MD_CAN_SHA1) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6250 | case MBEDTLS_MD_SHA1: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6251 | return MBEDTLS_SSL_HASH_SHA1; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6252 | #endif |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6253 | #if defined(MBEDTLS_MD_CAN_SHA224) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6254 | case MBEDTLS_MD_SHA224: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6255 | return MBEDTLS_SSL_HASH_SHA224; |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 6256 | #endif |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6257 | #if defined(MBEDTLS_MD_CAN_SHA256) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6258 | case MBEDTLS_MD_SHA256: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6259 | return MBEDTLS_SSL_HASH_SHA256; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6260 | #endif |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6261 | #if defined(MBEDTLS_MD_CAN_SHA384) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6262 | case MBEDTLS_MD_SHA384: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6263 | return MBEDTLS_SSL_HASH_SHA384; |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 6264 | #endif |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6265 | #if defined(MBEDTLS_MD_CAN_SHA512) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6266 | case MBEDTLS_MD_SHA512: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6267 | return MBEDTLS_SSL_HASH_SHA512; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6268 | #endif |
| 6269 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6270 | return MBEDTLS_SSL_HASH_NONE; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6271 | } |
| 6272 | } |
| 6273 | |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 6274 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6275 | * 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] | 6276 | * 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] | 6277 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6278 | 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] | 6279 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6280 | const uint16_t *group_list = mbedtls_ssl_get_groups(ssl); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 6281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6282 | if (group_list == NULL) { |
| 6283 | return -1; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6284 | } |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 6285 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6286 | for (; *group_list != 0; group_list++) { |
| 6287 | if (*group_list == tls_id) { |
| 6288 | return 0; |
| 6289 | } |
| 6290 | } |
| 6291 | |
| 6292 | return -1; |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 6293 | } |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 6294 | |
Valerio Setti | 49e6907 | 2023-06-27 17:27:51 +0200 | [diff] [blame] | 6295 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 6296 | /* |
| 6297 | * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id. |
| 6298 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6299 | 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] | 6300 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6301 | 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] | 6302 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6303 | if (tls_id == 0) { |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6304 | return -1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6305 | } |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6306 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6307 | return mbedtls_ssl_check_curve_tls_id(ssl, tls_id); |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 6308 | } |
Valerio Setti | 49e6907 | 2023-06-27 17:27:51 +0200 | [diff] [blame] | 6309 | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ |
Valerio Setti | 67419f0 | 2023-01-04 16:12:42 +0100 | [diff] [blame] | 6310 | |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6311 | static const struct { |
| 6312 | uint16_t tls_id; |
| 6313 | mbedtls_ecp_group_id ecp_group_id; |
| 6314 | psa_ecc_family_t psa_family; |
| 6315 | uint16_t bits; |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6316 | } tls_id_match_table[] = |
| 6317 | { |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 6318 | #if defined(MBEDTLS_ECP_HAVE_SECP521R1) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6319 | { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6320 | #endif |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 6321 | #if defined(MBEDTLS_ECP_HAVE_BP512R1) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6322 | { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6323 | #endif |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 6324 | #if defined(MBEDTLS_ECP_HAVE_SECP384R1) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6325 | { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6326 | #endif |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 6327 | #if defined(MBEDTLS_ECP_HAVE_BP384R1) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6328 | { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6329 | #endif |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 6330 | #if defined(MBEDTLS_ECP_HAVE_SECP256R1) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6331 | { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6332 | #endif |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 6333 | #if defined(MBEDTLS_ECP_HAVE_SECP256K1) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6334 | { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6335 | #endif |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 6336 | #if defined(MBEDTLS_ECP_HAVE_BP256R1) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6337 | { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6338 | #endif |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 6339 | #if defined(MBEDTLS_ECP_HAVE_SECP224R1) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6340 | { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6341 | #endif |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 6342 | #if defined(MBEDTLS_ECP_HAVE_SECP224K1) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6343 | { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6344 | #endif |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 6345 | #if defined(MBEDTLS_ECP_HAVE_SECP192R1) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6346 | { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6347 | #endif |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 6348 | #if defined(MBEDTLS_ECP_HAVE_SECP192K1) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6349 | { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6350 | #endif |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 6351 | #if defined(MBEDTLS_ECP_HAVE_CURVE25519) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6352 | { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6353 | #endif |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 6354 | #if defined(MBEDTLS_ECP_HAVE_CURVE448) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6355 | { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6356 | #endif |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6357 | { 0, MBEDTLS_ECP_DP_NONE, 0, 0 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6358 | }; |
| 6359 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6360 | 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] | 6361 | psa_key_type_t *type, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6362 | size_t *bits) |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6363 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6364 | for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) { |
| 6365 | if (tls_id_match_table[i].tls_id == tls_id) { |
Przemek Stekiel | da4fba6 | 2023-06-02 14:52:28 +0200 | [diff] [blame] | 6366 | if (type != NULL) { |
| 6367 | *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] | 6368 | } |
| 6369 | if (bits != NULL) { |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6370 | *bits = tls_id_match_table[i].bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6371 | } |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6372 | return PSA_SUCCESS; |
| 6373 | } |
| 6374 | } |
| 6375 | |
| 6376 | return PSA_ERROR_NOT_SUPPORTED; |
| 6377 | } |
| 6378 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6379 | 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] | 6380 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6381 | for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) { |
| 6382 | if (tls_id_match_table[i].tls_id == tls_id) { |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6383 | return tls_id_match_table[i].ecp_group_id; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6384 | } |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6385 | } |
| 6386 | |
| 6387 | return MBEDTLS_ECP_DP_NONE; |
| 6388 | } |
| 6389 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6390 | 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] | 6391 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6392 | for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE; |
| 6393 | i++) { |
| 6394 | if (tls_id_match_table[i].ecp_group_id == grp_id) { |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6395 | return tls_id_match_table[i].tls_id; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6396 | } |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6397 | } |
| 6398 | |
| 6399 | return 0; |
| 6400 | } |
| 6401 | |
Valerio Setti | 67419f0 | 2023-01-04 16:12:42 +0100 | [diff] [blame] | 6402 | #if defined(MBEDTLS_DEBUG_C) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6403 | static const struct { |
| 6404 | uint16_t tls_id; |
| 6405 | const char *name; |
| 6406 | } tls_id_curve_name_table[] = |
| 6407 | { |
Valerio Setti | 54e2379 | 2023-07-07 10:49:27 +0200 | [diff] [blame] | 6408 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, "secp521r1" }, |
| 6409 | { MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, "brainpoolP512r1" }, |
| 6410 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, "secp384r1" }, |
| 6411 | { MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, "brainpoolP384r1" }, |
| 6412 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, "secp256r1" }, |
| 6413 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256K1, "secp256k1" }, |
| 6414 | { MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, "brainpoolP256r1" }, |
| 6415 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, "secp224r1" }, |
| 6416 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224K1, "secp224k1" }, |
| 6417 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, "secp192r1" }, |
| 6418 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192K1, "secp192k1" }, |
| 6419 | { MBEDTLS_SSL_IANA_TLS_GROUP_X25519, "x25519" }, |
| 6420 | { MBEDTLS_SSL_IANA_TLS_GROUP_X448, "x448" }, |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6421 | { 0, NULL }, |
| 6422 | }; |
| 6423 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6424 | 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] | 6425 | { |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6426 | for (int i = 0; tls_id_curve_name_table[i].tls_id != 0; i++) { |
| 6427 | if (tls_id_curve_name_table[i].tls_id == tls_id) { |
| 6428 | return tls_id_curve_name_table[i].name; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6429 | } |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6430 | } |
| 6431 | |
| 6432 | return NULL; |
| 6433 | } |
Valerio Setti | 67419f0 | 2023-01-04 16:12:42 +0100 | [diff] [blame] | 6434 | #endif |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6435 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 6436 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6437 | int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl, |
| 6438 | const mbedtls_md_type_t md, |
| 6439 | unsigned char *dst, |
| 6440 | size_t dst_len, |
| 6441 | size_t *olen) |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 6442 | { |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 6443 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 6444 | psa_hash_operation_t *hash_operation_to_clone; |
| 6445 | psa_hash_operation_t hash_operation = psa_hash_operation_init(); |
| 6446 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 6447 | *olen = 0; |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 6448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6449 | switch (md) { |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6450 | #if defined(MBEDTLS_MD_CAN_SHA384) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6451 | case MBEDTLS_MD_SHA384: |
| 6452 | hash_operation_to_clone = &ssl->handshake->fin_sha384_psa; |
| 6453 | break; |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 6454 | #endif |
| 6455 | |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6456 | #if defined(MBEDTLS_MD_CAN_SHA256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6457 | case MBEDTLS_MD_SHA256: |
| 6458 | hash_operation_to_clone = &ssl->handshake->fin_sha256_psa; |
| 6459 | break; |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 6460 | #endif |
| 6461 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6462 | default: |
| 6463 | goto exit; |
| 6464 | } |
| 6465 | |
| 6466 | status = psa_hash_clone(hash_operation_to_clone, &hash_operation); |
| 6467 | if (status != PSA_SUCCESS) { |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 6468 | goto exit; |
| 6469 | } |
| 6470 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6471 | status = psa_hash_finish(&hash_operation, dst, dst_len, olen); |
| 6472 | if (status != PSA_SUCCESS) { |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 6473 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6474 | } |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 6475 | |
| 6476 | exit: |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6477 | #if !defined(MBEDTLS_MD_CAN_SHA384) && \ |
| 6478 | !defined(MBEDTLS_MD_CAN_SHA256) |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 6479 | (void) ssl; |
| 6480 | #endif |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 6481 | return PSA_TO_MBEDTLS_ERR(status); |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 6482 | } |
| 6483 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6484 | |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6485 | #if defined(MBEDTLS_MD_CAN_SHA384) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6486 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6487 | static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl, |
| 6488 | unsigned char *dst, |
| 6489 | size_t dst_len, |
| 6490 | size_t *olen) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6491 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6492 | int ret; |
Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame] | 6493 | mbedtls_md_context_t sha384; |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6494 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6495 | if (dst_len < 48) { |
| 6496 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 6497 | } |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6498 | |
Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame] | 6499 | mbedtls_md_init(&sha384); |
| 6500 | 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] | 6501 | if (ret != 0) { |
| 6502 | goto exit; |
| 6503 | } |
Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame] | 6504 | ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384); |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 6505 | if (ret != 0) { |
| 6506 | goto exit; |
| 6507 | } |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6508 | |
Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame] | 6509 | if ((ret = mbedtls_md_finish(&sha384, dst)) != 0) { |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 6510 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6511 | goto exit; |
| 6512 | } |
| 6513 | |
| 6514 | *olen = 48; |
| 6515 | |
| 6516 | exit: |
| 6517 | |
Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame] | 6518 | mbedtls_md_free(&sha384); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6519 | return ret; |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6520 | } |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6521 | #endif /* MBEDTLS_MD_CAN_SHA384 */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6522 | |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6523 | #if defined(MBEDTLS_MD_CAN_SHA256) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6524 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6525 | static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl, |
| 6526 | unsigned char *dst, |
| 6527 | size_t dst_len, |
| 6528 | size_t *olen) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6529 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6530 | int ret; |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 6531 | mbedtls_md_context_t sha256; |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6532 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6533 | if (dst_len < 32) { |
| 6534 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 6535 | } |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6536 | |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 6537 | mbedtls_md_init(&sha256); |
| 6538 | ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0); |
| 6539 | if (ret != 0) { |
| 6540 | goto exit; |
| 6541 | } |
| 6542 | ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256); |
| 6543 | if (ret != 0) { |
| 6544 | goto exit; |
| 6545 | } |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 6546 | |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 6547 | if ((ret = mbedtls_md_finish(&sha256, dst)) != 0) { |
| 6548 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6549 | goto exit; |
| 6550 | } |
| 6551 | |
| 6552 | *olen = 32; |
| 6553 | |
| 6554 | exit: |
| 6555 | |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 6556 | mbedtls_md_free(&sha256); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6557 | return ret; |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6558 | } |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6559 | #endif /* MBEDTLS_MD_CAN_SHA256 */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6560 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6561 | int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl, |
| 6562 | const mbedtls_md_type_t md, |
| 6563 | unsigned char *dst, |
| 6564 | size_t dst_len, |
| 6565 | size_t *olen) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6566 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6567 | switch (md) { |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 6568 | |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6569 | #if defined(MBEDTLS_MD_CAN_SHA384) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6570 | case MBEDTLS_MD_SHA384: |
| 6571 | 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] | 6572 | #endif /* MBEDTLS_MD_CAN_SHA384*/ |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 6573 | |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6574 | #if defined(MBEDTLS_MD_CAN_SHA256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6575 | case MBEDTLS_MD_SHA256: |
| 6576 | 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] | 6577 | #endif /* MBEDTLS_MD_CAN_SHA256*/ |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 6578 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6579 | default: |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6580 | #if !defined(MBEDTLS_MD_CAN_SHA384) && \ |
| 6581 | !defined(MBEDTLS_MD_CAN_SHA256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6582 | (void) ssl; |
| 6583 | (void) dst; |
| 6584 | (void) dst_len; |
| 6585 | (void) olen; |
Andrzej Kurek | 409248a | 2022-10-24 10:33:21 -0400 | [diff] [blame] | 6586 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6587 | break; |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 6588 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6589 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6590 | } |
XiaokangQian | 647719a | 2021-12-07 09:16:29 +0000 | [diff] [blame] | 6591 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 6592 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6593 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 6594 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6595 | /* mbedtls_ssl_parse_sig_alg_ext() |
| 6596 | * |
| 6597 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 6598 | * value (TLS 1.3 RFC8446): |
| 6599 | * enum { |
| 6600 | * .... |
| 6601 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 6602 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 6603 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 6604 | * .... |
| 6605 | * } SignatureScheme; |
| 6606 | * |
| 6607 | * struct { |
| 6608 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 6609 | * } SignatureSchemeList; |
| 6610 | * |
| 6611 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 6612 | * value (TLS 1.2 RFC5246): |
| 6613 | * enum { |
| 6614 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 6615 | * sha512(6), (255) |
| 6616 | * } HashAlgorithm; |
| 6617 | * |
| 6618 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 6619 | * SignatureAlgorithm; |
| 6620 | * |
| 6621 | * struct { |
| 6622 | * HashAlgorithm hash; |
| 6623 | * SignatureAlgorithm signature; |
| 6624 | * } SignatureAndHashAlgorithm; |
| 6625 | * |
| 6626 | * SignatureAndHashAlgorithm |
| 6627 | * supported_signature_algorithms<2..2^16-2>; |
| 6628 | * |
| 6629 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 6630 | * generalization of the TLS 1.2 signature algorithm extension. |
| 6631 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 6632 | * `SignatureScheme` field of TLS 1.3 |
| 6633 | * |
| 6634 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6635 | int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl, |
| 6636 | const unsigned char *buf, |
| 6637 | const unsigned char *end) |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6638 | { |
| 6639 | const unsigned char *p = buf; |
| 6640 | size_t supported_sig_algs_len = 0; |
| 6641 | const unsigned char *supported_sig_algs_end; |
| 6642 | uint16_t sig_alg; |
| 6643 | uint32_t common_idx = 0; |
| 6644 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6645 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2); |
| 6646 | supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6647 | p += 2; |
| 6648 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6649 | memset(ssl->handshake->received_sig_algs, 0, |
| 6650 | sizeof(ssl->handshake->received_sig_algs)); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6651 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6652 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6653 | supported_sig_algs_end = p + supported_sig_algs_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6654 | while (p < supported_sig_algs_end) { |
| 6655 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2); |
| 6656 | sig_alg = MBEDTLS_GET_UINT16_BE(p, 0); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6657 | p += 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6658 | MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s", |
| 6659 | sig_alg, |
| 6660 | mbedtls_ssl_sig_alg_to_str(sig_alg))); |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 6661 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6662 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 && |
| 6663 | (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) && |
| 6664 | mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6665 | continue; |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 6666 | } |
| 6667 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6668 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6669 | MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s", |
| 6670 | mbedtls_ssl_sig_alg_to_str(sig_alg))); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6671 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6672 | if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6673 | ssl->handshake->received_sig_algs[common_idx] = sig_alg; |
| 6674 | common_idx += 1; |
| 6675 | } |
| 6676 | } |
| 6677 | /* Check that we consumed all the message. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6678 | if (p != end) { |
| 6679 | MBEDTLS_SSL_DEBUG_MSG(1, |
| 6680 | ("Signature algorithms extension length misaligned")); |
| 6681 | MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, |
| 6682 | MBEDTLS_ERR_SSL_DECODE_ERROR); |
| 6683 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6684 | } |
| 6685 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6686 | if (common_idx == 0) { |
| 6687 | MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common")); |
| 6688 | MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE, |
| 6689 | MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE); |
| 6690 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6691 | } |
| 6692 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 6693 | ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6694 | return 0; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6695 | } |
| 6696 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 6697 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6698 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6699 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6700 | |
| 6701 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6702 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6703 | static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation, |
| 6704 | mbedtls_svc_key_id_t key, |
| 6705 | psa_algorithm_t alg, |
| 6706 | const unsigned char *raw_psk, size_t raw_psk_length, |
| 6707 | const unsigned char *seed, size_t seed_length, |
| 6708 | const unsigned char *label, size_t label_length, |
| 6709 | const unsigned char *other_secret, |
| 6710 | size_t other_secret_length, |
| 6711 | size_t capacity) |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6712 | { |
| 6713 | psa_status_t status; |
| 6714 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6715 | status = psa_key_derivation_setup(derivation, alg); |
| 6716 | if (status != PSA_SUCCESS) { |
| 6717 | return status; |
| 6718 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6719 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6720 | if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 6721 | status = psa_key_derivation_input_bytes(derivation, |
| 6722 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 6723 | seed, seed_length); |
| 6724 | if (status != PSA_SUCCESS) { |
| 6725 | return status; |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 6726 | } |
| 6727 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6728 | if (other_secret != NULL) { |
| 6729 | status = psa_key_derivation_input_bytes(derivation, |
| 6730 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 6731 | other_secret, other_secret_length); |
| 6732 | if (status != PSA_SUCCESS) { |
| 6733 | return status; |
| 6734 | } |
| 6735 | } |
| 6736 | |
| 6737 | if (mbedtls_svc_key_id_is_null(key)) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6738 | status = psa_key_derivation_input_bytes( |
| 6739 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6740 | raw_psk, raw_psk_length); |
| 6741 | } else { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6742 | status = psa_key_derivation_input_key( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6743 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6744 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6745 | if (status != PSA_SUCCESS) { |
| 6746 | return status; |
| 6747 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6748 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6749 | status = psa_key_derivation_input_bytes(derivation, |
| 6750 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 6751 | label, label_length); |
| 6752 | if (status != PSA_SUCCESS) { |
| 6753 | return status; |
| 6754 | } |
| 6755 | } else { |
| 6756 | return PSA_ERROR_NOT_SUPPORTED; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6757 | } |
| 6758 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6759 | status = psa_key_derivation_set_capacity(derivation, capacity); |
| 6760 | if (status != PSA_SUCCESS) { |
| 6761 | return status; |
| 6762 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6763 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6764 | return PSA_SUCCESS; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6765 | } |
| 6766 | |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 6767 | #if defined(PSA_WANT_ALG_SHA_384) || \ |
| 6768 | defined(PSA_WANT_ALG_SHA_256) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6769 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6770 | static int tls_prf_generic(mbedtls_md_type_t md_type, |
| 6771 | const unsigned char *secret, size_t slen, |
Max Fillinger | 76077e2 | 2024-10-23 15:47:23 +0200 | [diff] [blame] | 6772 | const char *label, size_t label_len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6773 | const unsigned char *random, size_t rlen, |
| 6774 | unsigned char *dstbuf, size_t dlen) |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6775 | { |
| 6776 | psa_status_t status; |
| 6777 | psa_algorithm_t alg; |
| 6778 | mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6779 | psa_key_derivation_operation_t derivation = |
| 6780 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 6781 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6782 | if (md_type == MBEDTLS_MD_SHA384) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6783 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6784 | } else { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6785 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6786 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6787 | |
| 6788 | /* Normally a "secret" should be long enough to be impossible to |
| 6789 | * find by brute force, and in particular should not be empty. But |
| 6790 | * this PRF is also used to derive an IV, in particular in EAP-TLS, |
| 6791 | * and for this use case it makes sense to have a 0-length "secret". |
| 6792 | * Since the key API doesn't allow importing a key of length 0, |
| 6793 | * keep master_key=0, which setup_psa_key_derivation() understands |
| 6794 | * to mean a 0-length "secret" input. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6795 | if (slen != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6796 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6797 | psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE); |
| 6798 | psa_set_key_algorithm(&key_attributes, alg); |
| 6799 | psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6800 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6801 | status = psa_import_key(&key_attributes, secret, slen, &master_key); |
| 6802 | if (status != PSA_SUCCESS) { |
| 6803 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 6804 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6805 | } |
| 6806 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6807 | status = setup_psa_key_derivation(&derivation, |
| 6808 | master_key, alg, |
| 6809 | NULL, 0, |
| 6810 | random, rlen, |
| 6811 | (unsigned char const *) label, |
Max Fillinger | 76077e2 | 2024-10-23 15:47:23 +0200 | [diff] [blame] | 6812 | label_len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6813 | NULL, 0, |
| 6814 | dlen); |
| 6815 | if (status != PSA_SUCCESS) { |
| 6816 | psa_key_derivation_abort(&derivation); |
| 6817 | psa_destroy_key(master_key); |
| 6818 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6819 | } |
| 6820 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6821 | status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen); |
| 6822 | if (status != PSA_SUCCESS) { |
| 6823 | psa_key_derivation_abort(&derivation); |
| 6824 | psa_destroy_key(master_key); |
| 6825 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6826 | } |
| 6827 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6828 | status = psa_key_derivation_abort(&derivation); |
| 6829 | if (status != PSA_SUCCESS) { |
| 6830 | psa_destroy_key(master_key); |
| 6831 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6832 | } |
| 6833 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6834 | if (!mbedtls_svc_key_id_is_null(master_key)) { |
| 6835 | status = psa_destroy_key(master_key); |
| 6836 | } |
| 6837 | if (status != PSA_SUCCESS) { |
| 6838 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 6839 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6840 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6841 | return 0; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6842 | } |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 6843 | #endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6844 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6845 | |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 6846 | #if defined(MBEDTLS_MD_C) && \ |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6847 | (defined(MBEDTLS_MD_CAN_SHA256) || \ |
| 6848 | defined(MBEDTLS_MD_CAN_SHA384)) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6849 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6850 | static int tls_prf_generic(mbedtls_md_type_t md_type, |
| 6851 | const unsigned char *secret, size_t slen, |
Max Fillinger | 40c2024 | 2025-04-15 21:18:20 +0200 | [diff] [blame] | 6852 | const char *label, size_t label_len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6853 | const unsigned char *random, size_t rlen, |
| 6854 | unsigned char *dstbuf, size_t dlen) |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6855 | { |
| 6856 | size_t nb; |
| 6857 | size_t i, j, k, md_len; |
| 6858 | unsigned char *tmp; |
| 6859 | size_t tmp_len = 0; |
| 6860 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 6861 | const mbedtls_md_info_t *md_info; |
| 6862 | mbedtls_md_context_t md_ctx; |
| 6863 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6864 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6865 | mbedtls_md_init(&md_ctx); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6866 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6867 | if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) { |
| 6868 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 6869 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6870 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6871 | md_len = mbedtls_md_get_size(md_info); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6872 | |
Max Fillinger | 40c2024 | 2025-04-15 21:18:20 +0200 | [diff] [blame] | 6873 | tmp_len = md_len + label_len + rlen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6874 | tmp = mbedtls_calloc(1, tmp_len); |
| 6875 | if (tmp == NULL) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6876 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 6877 | goto exit; |
| 6878 | } |
| 6879 | |
Max Fillinger | 40c2024 | 2025-04-15 21:18:20 +0200 | [diff] [blame] | 6880 | nb = label_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6881 | memcpy(tmp + md_len, label, nb); |
| 6882 | memcpy(tmp + md_len + nb, random, rlen); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6883 | nb += rlen; |
| 6884 | |
| 6885 | /* |
| 6886 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 6887 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6888 | if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6889 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6890 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6891 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6892 | ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen); |
| 6893 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6894 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6895 | } |
| 6896 | ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb); |
| 6897 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6898 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6899 | } |
| 6900 | ret = mbedtls_md_hmac_finish(&md_ctx, tmp); |
| 6901 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6902 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6903 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6904 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6905 | for (i = 0; i < dlen; i += md_len) { |
| 6906 | ret = mbedtls_md_hmac_reset(&md_ctx); |
| 6907 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6908 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6909 | } |
| 6910 | ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb); |
| 6911 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6912 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6913 | } |
| 6914 | ret = mbedtls_md_hmac_finish(&md_ctx, h_i); |
| 6915 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6916 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6917 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6918 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6919 | ret = mbedtls_md_hmac_reset(&md_ctx); |
| 6920 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6921 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6922 | } |
| 6923 | ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len); |
| 6924 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6925 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6926 | } |
| 6927 | ret = mbedtls_md_hmac_finish(&md_ctx, tmp); |
| 6928 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6929 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6930 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6931 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6932 | k = (i + md_len > dlen) ? dlen % md_len : md_len; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6933 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6934 | for (j = 0; j < k; j++) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6935 | dstbuf[i + j] = h_i[j]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6936 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6937 | } |
| 6938 | |
| 6939 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6940 | mbedtls_md_free(&md_ctx); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6941 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6942 | if (tmp != NULL) { |
| 6943 | mbedtls_platform_zeroize(tmp, tmp_len); |
| 6944 | } |
Dave Rodgman | 29b9b2b | 2022-11-01 16:08:14 +0000 | [diff] [blame] | 6945 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6946 | mbedtls_platform_zeroize(h_i, sizeof(h_i)); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6947 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6948 | mbedtls_free(tmp); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6949 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6950 | return ret; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6951 | } |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6952 | #endif /* MBEDTLS_MD_C && ( MBEDTLS_MD_CAN_SHA256 || MBEDTLS_MD_CAN_SHA384 ) */ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6953 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6954 | |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6955 | #if defined(MBEDTLS_MD_CAN_SHA256) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6956 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6957 | static int tls_prf_sha256(const unsigned char *secret, size_t slen, |
| 6958 | const char *label, |
| 6959 | const unsigned char *random, size_t rlen, |
| 6960 | unsigned char *dstbuf, size_t dlen) |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6961 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6962 | return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen, |
Max Fillinger | 76077e2 | 2024-10-23 15:47:23 +0200 | [diff] [blame] | 6963 | label, strlen(label), random, rlen, dstbuf, dlen); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6964 | } |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6965 | #endif /* MBEDTLS_MD_CAN_SHA256*/ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6966 | |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6967 | #if defined(MBEDTLS_MD_CAN_SHA384) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6968 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6969 | static int tls_prf_sha384(const unsigned char *secret, size_t slen, |
| 6970 | const char *label, |
| 6971 | const unsigned char *random, size_t rlen, |
| 6972 | unsigned char *dstbuf, size_t dlen) |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6973 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6974 | return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen, |
Max Fillinger | 76077e2 | 2024-10-23 15:47:23 +0200 | [diff] [blame] | 6975 | label, strlen(label), random, rlen, dstbuf, dlen); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6976 | } |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6977 | #endif /* MBEDTLS_MD_CAN_SHA384*/ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6978 | |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6979 | /* |
| 6980 | * Set appropriate PRF function and other SSL / TLS1.2 functions |
| 6981 | * |
| 6982 | * Inputs: |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6983 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
| 6984 | * |
| 6985 | * Outputs: |
| 6986 | * - the tls_prf, calc_verify and calc_finished members of handshake structure |
| 6987 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6988 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6989 | static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake, |
| 6990 | mbedtls_md_type_t hash) |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6991 | { |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6992 | #if defined(MBEDTLS_MD_CAN_SHA384) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6993 | if (hash == MBEDTLS_MD_SHA384) { |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6994 | handshake->tls_prf = tls_prf_sha384; |
| 6995 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 6996 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6997 | } else |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6998 | #endif |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 6999 | #if defined(MBEDTLS_MD_CAN_SHA256) |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 7000 | { |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 7001 | (void) hash; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 7002 | handshake->tls_prf = tls_prf_sha256; |
| 7003 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 7004 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
| 7005 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 7006 | #else |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 7007 | { |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 7008 | (void) handshake; |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 7009 | (void) hash; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7010 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 7011 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 7012 | #endif |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 7013 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7014 | return 0; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 7015 | } |
Jerry Yu | d6ab235 | 2022-02-17 14:03:43 +0800 | [diff] [blame] | 7016 | |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7017 | /* |
| 7018 | * Compute master secret if needed |
| 7019 | * |
| 7020 | * Parameters: |
| 7021 | * [in/out] handshake |
| 7022 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf |
| 7023 | * (PSA-PSK) ciphersuite_info, psk_opaque |
| 7024 | * [out] premaster (cleared) |
| 7025 | * [out] master |
| 7026 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK |
| 7027 | * debug: conf->f_dbg, conf->p_dbg |
| 7028 | * EMS: passed to calc_verify (debug + session_negotiate) |
Ronald Cron | a25cf58 | 2022-03-07 11:10:36 +0100 | [diff] [blame] | 7029 | * PSA-PSA: conf |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7030 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7031 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7032 | static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake, |
| 7033 | unsigned char *master, |
| 7034 | const mbedtls_ssl_context *ssl) |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7035 | { |
| 7036 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7037 | |
| 7038 | /* cf. RFC 5246, Section 8.1: |
| 7039 | * "The master secret is always exactly 48 bytes in length." */ |
| 7040 | size_t const master_secret_len = 48; |
| 7041 | |
| 7042 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 7043 | unsigned char session_hash[48]; |
| 7044 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 7045 | |
| 7046 | /* The label for the KDF used for key expansion. |
| 7047 | * This is either "master secret" or "extended master secret" |
| 7048 | * depending on whether the Extended Master Secret extension |
| 7049 | * is used. */ |
| 7050 | char const *lbl = "master secret"; |
| 7051 | |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 7052 | /* The seed for the KDF used for key expansion. |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7053 | * - If the Extended Master Secret extension is not used, |
| 7054 | * this is ClientHello.Random + ServerHello.Random |
| 7055 | * (see Sect. 8.1 in RFC 5246). |
| 7056 | * - If the Extended Master Secret extension is used, |
| 7057 | * this is the transcript of the handshake so far. |
| 7058 | * (see Sect. 4 in RFC 7627). */ |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 7059 | unsigned char const *seed = handshake->randbytes; |
| 7060 | size_t seed_len = 64; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7061 | |
| 7062 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 7063 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ |
| 7064 | !(defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7065 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7066 | ssl = NULL; /* make sure we don't use it except for those cases */ |
| 7067 | (void) ssl; |
| 7068 | #endif |
| 7069 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7070 | if (handshake->resume != 0) { |
| 7071 | MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)")); |
| 7072 | return 0; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7073 | } |
| 7074 | |
| 7075 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7076 | if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) { |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7077 | lbl = "extended master secret"; |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 7078 | seed = session_hash; |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 7079 | ret = handshake->calc_verify(ssl, session_hash, &seed_len); |
| 7080 | if (ret != 0) { |
| 7081 | MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret); |
| 7082 | } |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7083 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7084 | MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret", |
| 7085 | session_hash, seed_len); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7086 | } |
Przemek Stekiel | 169bf0b | 2022-04-29 07:53:29 +0200 | [diff] [blame] | 7087 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7088 | |
Przemek Stekiel | 99114f3 | 2022-04-22 11:20:09 +0200 | [diff] [blame] | 7089 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Przemek Stekiel | 8a4b7fd | 2022-04-28 09:22:22 +0200 | [diff] [blame] | 7090 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7091 | if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) { |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7092 | /* Perform PSK-to-MS expansion in a single step. */ |
| 7093 | psa_status_t status; |
| 7094 | psa_algorithm_t alg; |
| 7095 | mbedtls_svc_key_id_t psk; |
| 7096 | psa_key_derivation_operation_t derivation = |
| 7097 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Dave Rodgman | 2eab462 | 2023-10-05 13:30:37 +0100 | [diff] [blame] | 7098 | 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] | 7099 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7100 | MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion")); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7101 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7102 | psk = mbedtls_ssl_get_opaque_psk(ssl); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7103 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7104 | if (hash_alg == MBEDTLS_MD_SHA384) { |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7105 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7106 | } else { |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7107 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7108 | } |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7109 | |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 7110 | size_t other_secret_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7111 | unsigned char *other_secret = NULL; |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 7112 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7113 | switch (handshake->ciphersuite_info->key_exchange) { |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 7114 | /* Provide other secret. |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 7115 | * Other secret is stored in premaster, where first 2 bytes hold the |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 7116 | * length of the other key. |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 7117 | */ |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 7118 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 7119 | /* For RSA-PSK other key length is always 48 bytes. */ |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 7120 | other_secret_len = 48; |
| 7121 | other_secret = handshake->premaster + 2; |
| 7122 | break; |
| 7123 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 7124 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 7125 | other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0); |
| 7126 | other_secret = handshake->premaster + 2; |
| 7127 | break; |
| 7128 | default: |
| 7129 | break; |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 7130 | } |
| 7131 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7132 | status = setup_psa_key_derivation(&derivation, psk, alg, |
| 7133 | ssl->conf->psk, ssl->conf->psk_len, |
| 7134 | seed, seed_len, |
| 7135 | (unsigned char const *) lbl, |
| 7136 | (size_t) strlen(lbl), |
| 7137 | other_secret, other_secret_len, |
| 7138 | master_secret_len); |
| 7139 | if (status != PSA_SUCCESS) { |
| 7140 | psa_key_derivation_abort(&derivation); |
| 7141 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7142 | } |
| 7143 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7144 | status = psa_key_derivation_output_bytes(&derivation, |
| 7145 | master, |
| 7146 | master_secret_len); |
| 7147 | if (status != PSA_SUCCESS) { |
| 7148 | psa_key_derivation_abort(&derivation); |
| 7149 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7150 | } |
| 7151 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7152 | status = psa_key_derivation_abort(&derivation); |
| 7153 | if (status != PSA_SUCCESS) { |
| 7154 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 7155 | } |
| 7156 | } else |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7157 | #endif |
| 7158 | { |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 7159 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7160 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 7161 | if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) { |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 7162 | psa_status_t status; |
| 7163 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 7164 | psa_key_derivation_operation_t derivation = |
| 7165 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7166 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7167 | MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE")); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 7168 | |
| 7169 | handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE; |
| 7170 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7171 | status = psa_key_derivation_setup(&derivation, alg); |
| 7172 | if (status != PSA_SUCCESS) { |
| 7173 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 7174 | } |
| 7175 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7176 | status = psa_key_derivation_set_capacity(&derivation, |
| 7177 | PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE); |
| 7178 | if (status != PSA_SUCCESS) { |
| 7179 | psa_key_derivation_abort(&derivation); |
| 7180 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 7181 | } |
| 7182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7183 | status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx, |
| 7184 | &derivation); |
| 7185 | if (status != PSA_SUCCESS) { |
| 7186 | psa_key_derivation_abort(&derivation); |
| 7187 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 7188 | } |
| 7189 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7190 | status = psa_key_derivation_output_bytes(&derivation, |
| 7191 | handshake->premaster, |
| 7192 | handshake->pmslen); |
| 7193 | if (status != PSA_SUCCESS) { |
| 7194 | psa_key_derivation_abort(&derivation); |
| 7195 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 7196 | } |
| 7197 | |
| 7198 | status = psa_key_derivation_abort(&derivation); |
| 7199 | if (status != PSA_SUCCESS) { |
| 7200 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 7201 | } |
| 7202 | } |
| 7203 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7204 | ret = handshake->tls_prf(handshake->premaster, handshake->pmslen, |
| 7205 | lbl, seed, seed_len, |
| 7206 | master, |
| 7207 | master_secret_len); |
| 7208 | if (ret != 0) { |
| 7209 | MBEDTLS_SSL_DEBUG_RET(1, "prf", ret); |
| 7210 | return ret; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7211 | } |
| 7212 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7213 | MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret", |
| 7214 | handshake->premaster, |
| 7215 | handshake->pmslen); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7216 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7217 | mbedtls_platform_zeroize(handshake->premaster, |
| 7218 | sizeof(handshake->premaster)); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7219 | } |
| 7220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7221 | return 0; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7222 | } |
| 7223 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7224 | int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl) |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 7225 | { |
| 7226 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7227 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 7228 | ssl->handshake->ciphersuite_info; |
| 7229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7230 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys")); |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 7231 | |
| 7232 | /* Set PRF, calc_verify and calc_finished function pointers */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7233 | ret = ssl_set_handshake_prfs(ssl->handshake, |
Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 7234 | (mbedtls_md_type_t) ciphersuite_info->mac); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7235 | if (ret != 0) { |
| 7236 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret); |
| 7237 | return ret; |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 7238 | } |
| 7239 | |
| 7240 | /* Compute master secret if needed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7241 | ret = ssl_compute_master(ssl->handshake, |
| 7242 | ssl->session_negotiate->master, |
| 7243 | ssl); |
| 7244 | if (ret != 0) { |
| 7245 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret); |
| 7246 | return ret; |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 7247 | } |
| 7248 | |
| 7249 | /* Swap the client and server random values: |
| 7250 | * - MS derivation wanted client+server (RFC 5246 8.1) |
| 7251 | * - key derivation wants server+client (RFC 5246 6.3) */ |
| 7252 | { |
| 7253 | unsigned char tmp[64]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7254 | memcpy(tmp, ssl->handshake->randbytes, 64); |
| 7255 | memcpy(ssl->handshake->randbytes, tmp + 32, 32); |
| 7256 | memcpy(ssl->handshake->randbytes + 32, tmp, 32); |
| 7257 | mbedtls_platform_zeroize(tmp, sizeof(tmp)); |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 7258 | } |
| 7259 | |
| 7260 | /* Populate transform structure */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7261 | ret = ssl_tls12_populate_transform(ssl->transform_negotiate, |
| 7262 | ssl->session_negotiate->ciphersuite, |
| 7263 | ssl->session_negotiate->master, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7264 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7265 | ssl->session_negotiate->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7266 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7267 | ssl->handshake->tls_prf, |
| 7268 | ssl->handshake->randbytes, |
| 7269 | ssl->tls_version, |
| 7270 | ssl->conf->endpoint, |
| 7271 | ssl); |
| 7272 | if (ret != 0) { |
| 7273 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret); |
| 7274 | return ret; |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 7275 | } |
| 7276 | |
| 7277 | /* We no longer need Server/ClientHello.random values */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7278 | mbedtls_platform_zeroize(ssl->handshake->randbytes, |
| 7279 | sizeof(ssl->handshake->randbytes)); |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 7280 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7281 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys")); |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 7282 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7283 | return 0; |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 7284 | } |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 7285 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7286 | 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] | 7287 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7288 | switch (md) { |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 7289 | #if defined(MBEDTLS_MD_CAN_SHA384) |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 7290 | case MBEDTLS_SSL_HASH_SHA384: |
| 7291 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 7292 | break; |
| 7293 | #endif |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 7294 | #if defined(MBEDTLS_MD_CAN_SHA256) |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 7295 | case MBEDTLS_SSL_HASH_SHA256: |
| 7296 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 7297 | break; |
| 7298 | #endif |
| 7299 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7300 | return -1; |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 7301 | } |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 7302 | #if !defined(MBEDTLS_MD_CAN_SHA384) && \ |
| 7303 | !defined(MBEDTLS_MD_CAN_SHA256) |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 7304 | (void) ssl; |
| 7305 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7306 | return 0; |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 7307 | } |
| 7308 | |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7309 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7310 | static int ssl_calc_verify_tls_psa(const mbedtls_ssl_context *ssl, |
| 7311 | const psa_hash_operation_t *hs_op, |
| 7312 | size_t buffer_size, |
| 7313 | unsigned char *hash, |
| 7314 | size_t *hlen) |
| 7315 | { |
| 7316 | psa_status_t status; |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7317 | psa_hash_operation_t cloned_op = psa_hash_operation_init(); |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7318 | |
| 7319 | #if !defined(MBEDTLS_DEBUG_C) |
| 7320 | (void) ssl; |
| 7321 | #endif |
| 7322 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify")); |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7323 | status = psa_hash_clone(hs_op, &cloned_op); |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7324 | if (status != PSA_SUCCESS) { |
| 7325 | goto exit; |
| 7326 | } |
| 7327 | |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7328 | status = psa_hash_finish(&cloned_op, hash, buffer_size, hlen); |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7329 | if (status != PSA_SUCCESS) { |
| 7330 | goto exit; |
| 7331 | } |
| 7332 | |
| 7333 | MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen); |
| 7334 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify")); |
| 7335 | |
| 7336 | exit: |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7337 | psa_hash_abort(&cloned_op); |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7338 | return mbedtls_md_error_from_psa(status); |
| 7339 | } |
| 7340 | #else |
| 7341 | static int ssl_calc_verify_tls_legacy(const mbedtls_ssl_context *ssl, |
| 7342 | const mbedtls_md_context_t *hs_ctx, |
| 7343 | unsigned char *hash, |
| 7344 | size_t *hlen) |
| 7345 | { |
| 7346 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7347 | mbedtls_md_context_t cloned_ctx; |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7348 | |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7349 | mbedtls_md_init(&cloned_ctx); |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7350 | |
| 7351 | #if !defined(MBEDTLS_DEBUG_C) |
| 7352 | (void) ssl; |
| 7353 | #endif |
| 7354 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify")); |
| 7355 | |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7356 | 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] | 7357 | if (ret != 0) { |
| 7358 | goto exit; |
| 7359 | } |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7360 | ret = mbedtls_md_clone(&cloned_ctx, hs_ctx); |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7361 | if (ret != 0) { |
| 7362 | goto exit; |
| 7363 | } |
| 7364 | |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7365 | ret = mbedtls_md_finish(&cloned_ctx, hash); |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7366 | if (ret != 0) { |
| 7367 | goto exit; |
| 7368 | } |
| 7369 | |
| 7370 | *hlen = mbedtls_md_get_size(mbedtls_md_info_from_ctx(hs_ctx)); |
| 7371 | |
| 7372 | MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen); |
| 7373 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify")); |
| 7374 | |
| 7375 | exit: |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7376 | mbedtls_md_free(&cloned_ctx); |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7377 | return ret; |
| 7378 | } |
| 7379 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7380 | |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 7381 | #if defined(MBEDTLS_MD_CAN_SHA256) |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 7382 | int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl, |
| 7383 | unsigned char *hash, |
| 7384 | size_t *hlen) |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 7385 | { |
| 7386 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7387 | return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha256_psa, 32, |
| 7388 | hash, hlen); |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 7389 | #else |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7390 | return ssl_calc_verify_tls_legacy(ssl, &ssl->handshake->fin_sha256, |
| 7391 | hash, hlen); |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 7392 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 7393 | } |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 7394 | #endif /* MBEDTLS_MD_CAN_SHA256 */ |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 7395 | |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 7396 | #if defined(MBEDTLS_MD_CAN_SHA384) |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 7397 | int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl, |
| 7398 | unsigned char *hash, |
| 7399 | size_t *hlen) |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 7400 | { |
| 7401 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7402 | return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha384_psa, 48, |
| 7403 | hash, hlen); |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 7404 | #else |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7405 | return ssl_calc_verify_tls_legacy(ssl, &ssl->handshake->fin_sha384, |
| 7406 | hash, hlen); |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 7407 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 7408 | } |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 7409 | #endif /* MBEDTLS_MD_CAN_SHA384 */ |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 7410 | |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 7411 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 7412 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7413 | 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] | 7414 | { |
| 7415 | unsigned char *p = ssl->handshake->premaster; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7416 | unsigned char *end = p + sizeof(ssl->handshake->premaster); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7417 | const unsigned char *psk = NULL; |
| 7418 | size_t psk_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7419 | int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7420 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7421 | if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) { |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7422 | /* |
| 7423 | * This should never happen because the existence of a PSK is always |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 7424 | * checked before calling this function. |
| 7425 | * |
| 7426 | * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with |
Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 7427 | * the shared secret without PSK. |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7428 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7429 | if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) { |
| 7430 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 7431 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 7432 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7433 | } |
| 7434 | |
| 7435 | /* |
| 7436 | * PMS = struct { |
| 7437 | * opaque other_secret<0..2^16-1>; |
| 7438 | * opaque psk<0..2^16-1>; |
| 7439 | * }; |
| 7440 | * with "other_secret" depending on the particular key exchange |
| 7441 | */ |
| 7442 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7443 | if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) { |
| 7444 | if (end - p < 2) { |
| 7445 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 7446 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7447 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7448 | MBEDTLS_PUT_UINT16_BE(psk_len, p, 0); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7449 | p += 2; |
| 7450 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7451 | if (end < p || (size_t) (end - p) < psk_len) { |
| 7452 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 7453 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7454 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7455 | memset(p, 0, psk_len); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7456 | p += psk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7457 | } else |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7458 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 7459 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7460 | if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) { |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7461 | /* |
| 7462 | * other_secret already set by the ClientKeyExchange message, |
| 7463 | * and is 48 bytes long |
| 7464 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7465 | if (end - p < 2) { |
| 7466 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 7467 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7468 | |
| 7469 | *p++ = 0; |
| 7470 | *p++ = 48; |
| 7471 | p += 48; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7472 | } else |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7473 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 7474 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7475 | if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) { |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7476 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7477 | size_t len; |
| 7478 | |
| 7479 | /* Write length only when we know the actual value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7480 | if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx, |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 7481 | p + 2, (size_t) (end - (p + 2)), &len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7482 | ssl->conf->f_rng, ssl->conf->p_rng)) != 0) { |
| 7483 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret); |
| 7484 | return ret; |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7485 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7486 | MBEDTLS_PUT_UINT16_BE(len, p, 0); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7487 | p += 2 + len; |
| 7488 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7489 | MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K); |
| 7490 | } else |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7491 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 7492 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7493 | if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) { |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7494 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7495 | size_t zlen; |
| 7496 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7497 | if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen, |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 7498 | p + 2, (size_t) (end - (p + 2)), |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7499 | ssl->conf->f_rng, ssl->conf->p_rng)) != 0) { |
| 7500 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret); |
| 7501 | return ret; |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7502 | } |
| 7503 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7504 | MBEDTLS_PUT_UINT16_BE(zlen, p, 0); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7505 | p += 2 + zlen; |
| 7506 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7507 | MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx, |
| 7508 | MBEDTLS_DEBUG_ECDH_Z); |
| 7509 | } else |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 7510 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7511 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7512 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 7513 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7514 | } |
| 7515 | |
| 7516 | /* opaque psk<0..2^16-1>; */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7517 | if (end - p < 2) { |
| 7518 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 7519 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7520 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7521 | MBEDTLS_PUT_UINT16_BE(psk_len, p, 0); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7522 | p += 2; |
| 7523 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7524 | if (end < p || (size_t) (end - p) < psk_len) { |
| 7525 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 7526 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7527 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7528 | memcpy(p, psk, psk_len); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7529 | p += psk_len; |
| 7530 | |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 7531 | ssl->handshake->pmslen = (size_t) (p - ssl->handshake->premaster); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7532 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7533 | return 0; |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7534 | } |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 7535 | #endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 7536 | |
| 7537 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7538 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7539 | static int ssl_write_hello_request(mbedtls_ssl_context *ssl); |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 7540 | |
| 7541 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7542 | int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl) |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 7543 | { |
| 7544 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 7545 | * timeout if we were using the usual handshake doubling scheme */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7546 | if (ssl->conf->renego_max_records < 0) { |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 7547 | uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1; |
| 7548 | unsigned char doublings = 1; |
| 7549 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7550 | while (ratio != 0) { |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 7551 | ++doublings; |
| 7552 | ratio >>= 1; |
| 7553 | } |
| 7554 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7555 | if (++ssl->renego_records_seen > doublings) { |
| 7556 | MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request")); |
| 7557 | return 0; |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 7558 | } |
| 7559 | } |
| 7560 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7561 | return ssl_write_hello_request(ssl); |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 7562 | } |
| 7563 | #endif |
| 7564 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7565 | |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7566 | /* |
| 7567 | * Handshake functions |
| 7568 | */ |
| 7569 | #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 7570 | /* No certificate support -> dummy functions */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7571 | int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7572 | { |
| 7573 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 7574 | ssl->handshake->ciphersuite_info; |
| 7575 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7576 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7577 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7578 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { |
| 7579 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate")); |
Gilles Peskine | 49f179d | 2025-03-07 15:09:32 +0100 | [diff] [blame] | 7580 | mbedtls_ssl_handshake_increment_state(ssl); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7581 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7582 | } |
| 7583 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7584 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 7585 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7586 | } |
| 7587 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7588 | int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7589 | { |
| 7590 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 7591 | ssl->handshake->ciphersuite_info; |
| 7592 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7593 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7594 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7595 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { |
| 7596 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate")); |
Gilles Peskine | 49f179d | 2025-03-07 15:09:32 +0100 | [diff] [blame] | 7597 | mbedtls_ssl_handshake_increment_state(ssl); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7598 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7599 | } |
| 7600 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7601 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 7602 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7603 | } |
| 7604 | |
| 7605 | #else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 7606 | /* Some certificate support -> implement write and parse */ |
| 7607 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7608 | int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7609 | { |
| 7610 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 7611 | size_t i, n; |
| 7612 | const mbedtls_x509_crt *crt; |
| 7613 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 7614 | ssl->handshake->ciphersuite_info; |
| 7615 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7616 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate")); |
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 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { |
| 7619 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate")); |
Gilles Peskine | 49f179d | 2025-03-07 15:09:32 +0100 | [diff] [blame] | 7620 | mbedtls_ssl_handshake_increment_state(ssl); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7621 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7622 | } |
| 7623 | |
| 7624 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7625 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 7626 | if (ssl->handshake->client_auth == 0) { |
| 7627 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate")); |
Gilles Peskine | 49f179d | 2025-03-07 15:09:32 +0100 | [diff] [blame] | 7628 | mbedtls_ssl_handshake_increment_state(ssl); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7629 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7630 | } |
| 7631 | } |
| 7632 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 7633 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7634 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 7635 | if (mbedtls_ssl_own_cert(ssl) == NULL) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7636 | /* Should never happen because we shouldn't have picked the |
| 7637 | * ciphersuite if we don't have a certificate. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7638 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7639 | } |
| 7640 | } |
| 7641 | #endif |
| 7642 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7643 | MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl)); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7644 | |
| 7645 | /* |
| 7646 | * 0 . 0 handshake type |
| 7647 | * 1 . 3 handshake length |
| 7648 | * 4 . 6 length of all certs |
| 7649 | * 7 . 9 length of cert. 1 |
| 7650 | * 10 . n-1 peer certificate |
| 7651 | * n . n+2 length of cert. 2 |
| 7652 | * n+3 . ... upper level cert, etc. |
| 7653 | */ |
| 7654 | i = 7; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7655 | crt = mbedtls_ssl_own_cert(ssl); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7656 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7657 | while (crt != NULL) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7658 | n = crt->raw.len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7659 | if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) { |
| 7660 | MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET |
| 7661 | " > %" MBEDTLS_PRINTF_SIZET, |
| 7662 | i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN)); |
| 7663 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7664 | } |
| 7665 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7666 | ssl->out_msg[i] = MBEDTLS_BYTE_2(n); |
| 7667 | ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n); |
| 7668 | ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7669 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7670 | i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7671 | i += n; crt = crt->next; |
| 7672 | } |
| 7673 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7674 | ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7); |
| 7675 | ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7); |
| 7676 | ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7677 | |
| 7678 | ssl->out_msglen = i; |
| 7679 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 7680 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
| 7681 | |
Gilles Peskine | 49f179d | 2025-03-07 15:09:32 +0100 | [diff] [blame] | 7682 | mbedtls_ssl_handshake_increment_state(ssl); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7683 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7684 | if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { |
| 7685 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret); |
| 7686 | return ret; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7687 | } |
| 7688 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7689 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7690 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7691 | return ret; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7692 | } |
| 7693 | |
| 7694 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 7695 | |
| 7696 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7697 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7698 | static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl, |
| 7699 | unsigned char *crt_buf, |
| 7700 | size_t crt_buf_len) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7701 | { |
| 7702 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; |
| 7703 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7704 | if (peer_crt == NULL) { |
| 7705 | return -1; |
| 7706 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7707 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7708 | if (peer_crt->raw.len != crt_buf_len) { |
| 7709 | return -1; |
| 7710 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7711 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7712 | return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7713 | } |
| 7714 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7715 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7716 | static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl, |
| 7717 | unsigned char *crt_buf, |
| 7718 | size_t crt_buf_len) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7719 | { |
| 7720 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7721 | unsigned char const * const peer_cert_digest = |
| 7722 | ssl->session->peer_cert_digest; |
| 7723 | mbedtls_md_type_t const peer_cert_digest_type = |
| 7724 | ssl->session->peer_cert_digest_type; |
| 7725 | mbedtls_md_info_t const * const digest_info = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7726 | mbedtls_md_info_from_type(peer_cert_digest_type); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7727 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; |
| 7728 | size_t digest_len; |
| 7729 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7730 | if (peer_cert_digest == NULL || digest_info == NULL) { |
| 7731 | return -1; |
| 7732 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7733 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7734 | digest_len = mbedtls_md_get_size(digest_info); |
| 7735 | if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) { |
| 7736 | return -1; |
| 7737 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7738 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7739 | ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest); |
| 7740 | if (ret != 0) { |
| 7741 | return -1; |
| 7742 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7743 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7744 | return memcmp(tmp_digest, peer_cert_digest, digest_len); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7745 | } |
| 7746 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7747 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 7748 | |
| 7749 | /* |
| 7750 | * Once the certificate message is read, parse it into a cert chain and |
| 7751 | * perform basic checks, but leave actual verification to the caller |
| 7752 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7753 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7754 | static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl, |
| 7755 | mbedtls_x509_crt *chain) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7756 | { |
| 7757 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7758 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7759 | int crt_cnt = 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7760 | #endif |
| 7761 | size_t i, n; |
| 7762 | uint8_t alert; |
| 7763 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7764 | if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) { |
| 7765 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7766 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7767 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
| 7768 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7769 | } |
| 7770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7771 | if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) { |
| 7772 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7773 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
| 7774 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7775 | } |
| 7776 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7777 | if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) { |
| 7778 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7779 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7780 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
| 7781 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7782 | } |
| 7783 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7784 | i = mbedtls_ssl_hs_hdr_len(ssl); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7785 | |
| 7786 | /* |
| 7787 | * Same message structure as in mbedtls_ssl_write_certificate() |
| 7788 | */ |
Dave Rodgman | a3d0f61 | 2023-11-03 23:34:02 +0000 | [diff] [blame] | 7789 | n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7790 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7791 | if (ssl->in_msg[i] != 0 || |
| 7792 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) { |
| 7793 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7794 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7795 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
| 7796 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7797 | } |
| 7798 | |
| 7799 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 7800 | i += 3; |
| 7801 | |
| 7802 | /* Iterate through and parse the CRTs in the provided chain. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7803 | while (i < ssl->in_hslen) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7804 | /* Check that there's room for the next CRT's length fields. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7805 | if (i + 3 > ssl->in_hslen) { |
| 7806 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7807 | mbedtls_ssl_send_alert_message(ssl, |
| 7808 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7809 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
| 7810 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7811 | } |
| 7812 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 7813 | * anything beyond 2**16 ~ 64K. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7814 | if (ssl->in_msg[i] != 0) { |
| 7815 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7816 | mbedtls_ssl_send_alert_message(ssl, |
| 7817 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7818 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT); |
| 7819 | return MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7820 | } |
| 7821 | |
| 7822 | /* Read length of the next CRT in the chain. */ |
Dave Rodgman | a3d0f61 | 2023-11-03 23:34:02 +0000 | [diff] [blame] | 7823 | n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7824 | i += 3; |
| 7825 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7826 | if (n < 128 || i + n > ssl->in_hslen) { |
| 7827 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7828 | mbedtls_ssl_send_alert_message(ssl, |
| 7829 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7830 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
| 7831 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7832 | } |
| 7833 | |
| 7834 | /* Check if we're handling the first CRT in the chain. */ |
| 7835 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7836 | if (crt_cnt++ == 0 && |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7837 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7838 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7839 | /* During client-side renegotiation, check that the server's |
| 7840 | * end-CRTs hasn't changed compared to the initial handshake, |
| 7841 | * mitigating the triple handshake attack. On success, reuse |
| 7842 | * the original end-CRT instead of parsing it again. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7843 | MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation")); |
| 7844 | if (ssl_check_peer_crt_unchanged(ssl, |
| 7845 | &ssl->in_msg[i], |
| 7846 | n) != 0) { |
| 7847 | MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation")); |
| 7848 | mbedtls_ssl_send_alert_message(ssl, |
| 7849 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7850 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED); |
| 7851 | return MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7852 | } |
| 7853 | |
| 7854 | /* Now we can safely free the original chain. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7855 | ssl_clear_peer_cert(ssl->session); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7856 | } |
| 7857 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 7858 | |
| 7859 | /* Parse the next certificate in the chain. */ |
| 7860 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7861 | ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7862 | #else |
| 7863 | /* If we don't need to store the CRT chain permanently, parse |
| 7864 | * it in-place from the input buffer instead of making a copy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7865 | 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] | 7866 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7867 | switch (ret) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7868 | case 0: /*ok*/ |
| 7869 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 7870 | /* Ignore certificate with an unknown algorithm: maybe a |
| 7871 | prior certificate was already trusted. */ |
| 7872 | break; |
| 7873 | |
| 7874 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 7875 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 7876 | goto crt_parse_der_failed; |
| 7877 | |
| 7878 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 7879 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7880 | goto crt_parse_der_failed; |
| 7881 | |
| 7882 | default: |
| 7883 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7884 | crt_parse_der_failed: |
| 7885 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert); |
| 7886 | MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret); |
| 7887 | return ret; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7888 | } |
| 7889 | |
| 7890 | i += n; |
| 7891 | } |
| 7892 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7893 | MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain); |
| 7894 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7895 | } |
| 7896 | |
| 7897 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7898 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7899 | 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] | 7900 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7901 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 7902 | return -1; |
| 7903 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7904 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7905 | if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) && |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7906 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 7907 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7908 | memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) { |
| 7909 | MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate")); |
| 7910 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7911 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7912 | return -1; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7913 | } |
| 7914 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7915 | |
| 7916 | /* Check if a certificate message is expected. |
| 7917 | * Return either |
| 7918 | * - SSL_CERTIFICATE_EXPECTED, or |
| 7919 | * - SSL_CERTIFICATE_SKIP |
| 7920 | * indicating whether a Certificate message is expected or not. |
| 7921 | */ |
| 7922 | #define SSL_CERTIFICATE_EXPECTED 0 |
| 7923 | #define SSL_CERTIFICATE_SKIP 1 |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7924 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7925 | static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl, |
| 7926 | int authmode) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7927 | { |
| 7928 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 7929 | ssl->handshake->ciphersuite_info; |
| 7930 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7931 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { |
| 7932 | return SSL_CERTIFICATE_SKIP; |
| 7933 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7934 | |
| 7935 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7936 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 7937 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) { |
| 7938 | return SSL_CERTIFICATE_SKIP; |
| 7939 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7940 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7941 | if (authmode == MBEDTLS_SSL_VERIFY_NONE) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7942 | ssl->session_negotiate->verify_result = |
| 7943 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7944 | return SSL_CERTIFICATE_SKIP; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7945 | } |
| 7946 | } |
| 7947 | #else |
| 7948 | ((void) authmode); |
| 7949 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7950 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7951 | return SSL_CERTIFICATE_EXPECTED; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7952 | } |
| 7953 | |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7954 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7955 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7956 | static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl, |
| 7957 | unsigned char *start, size_t len) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7958 | { |
| 7959 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7960 | /* Remember digest of the peer's end-CRT. */ |
| 7961 | ssl->session_negotiate->peer_cert_digest = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7962 | mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN); |
| 7963 | if (ssl->session_negotiate->peer_cert_digest == NULL) { |
| 7964 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed", |
| 7965 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN)); |
| 7966 | mbedtls_ssl_send_alert_message(ssl, |
| 7967 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7968 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7969 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7970 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7971 | } |
| 7972 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7973 | ret = mbedtls_md(mbedtls_md_info_from_type( |
| 7974 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE), |
| 7975 | start, len, |
| 7976 | ssl->session_negotiate->peer_cert_digest); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7977 | |
| 7978 | ssl->session_negotiate->peer_cert_digest_type = |
| 7979 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 7980 | ssl->session_negotiate->peer_cert_digest_len = |
| 7981 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 7982 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7983 | return ret; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7984 | } |
| 7985 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7986 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7987 | static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl, |
| 7988 | unsigned char *start, size_t len) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7989 | { |
| 7990 | unsigned char *end = start + len; |
| 7991 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7992 | |
| 7993 | /* Make a copy of the peer's raw public key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7994 | mbedtls_pk_init(&ssl->handshake->peer_pubkey); |
| 7995 | ret = mbedtls_pk_parse_subpubkey(&start, end, |
| 7996 | &ssl->handshake->peer_pubkey); |
| 7997 | if (ret != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7998 | /* We should have parsed the public key before. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7999 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8000 | } |
| 8001 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8002 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8003 | } |
| 8004 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8005 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8006 | int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8007 | { |
| 8008 | int ret = 0; |
| 8009 | int crt_expected; |
Manuel Pégourié-Gonnard | e1cc926 | 2024-08-14 09:47:38 +0200 | [diff] [blame] | 8010 | /* Authmode: precedence order is SNI if used else configuration */ |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8011 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 8012 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 8013 | ? ssl->handshake->sni_authmode |
| 8014 | : ssl->conf->authmode; |
| 8015 | #else |
| 8016 | const int authmode = ssl->conf->authmode; |
| 8017 | #endif |
| 8018 | void *rs_ctx = NULL; |
| 8019 | mbedtls_x509_crt *chain = NULL; |
| 8020 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8021 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8022 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8023 | crt_expected = ssl_parse_certificate_coordinate(ssl, authmode); |
| 8024 | if (crt_expected == SSL_CERTIFICATE_SKIP) { |
| 8025 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8026 | goto exit; |
| 8027 | } |
| 8028 | |
| 8029 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8030 | if (ssl->handshake->ecrs_enabled && |
| 8031 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8032 | chain = ssl->handshake->ecrs_peer_cert; |
| 8033 | ssl->handshake->ecrs_peer_cert = NULL; |
| 8034 | goto crt_verify; |
| 8035 | } |
| 8036 | #endif |
| 8037 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8038 | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8039 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 8040 | let it decide whether to alert. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8041 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8042 | goto exit; |
| 8043 | } |
| 8044 | |
| 8045 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8046 | if (ssl_srv_check_client_no_crt_notification(ssl) == 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8047 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
| 8048 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8049 | if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8050 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8051 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8052 | |
| 8053 | goto exit; |
| 8054 | } |
| 8055 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 8056 | |
| 8057 | /* Clear existing peer CRT structure in case we tried to |
| 8058 | * reuse a session but it failed, and allocate a new one. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8059 | ssl_clear_peer_cert(ssl->session_negotiate); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8060 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8061 | chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt)); |
| 8062 | if (chain == NULL) { |
| 8063 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", |
| 8064 | sizeof(mbedtls_x509_crt))); |
| 8065 | mbedtls_ssl_send_alert_message(ssl, |
| 8066 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8067 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8068 | |
| 8069 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 8070 | goto exit; |
| 8071 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8072 | mbedtls_x509_crt_init(chain); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8073 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8074 | ret = ssl_parse_certificate_chain(ssl, chain); |
| 8075 | if (ret != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8076 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8077 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8078 | |
| 8079 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8080 | if (ssl->handshake->ecrs_enabled) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8081 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8082 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8083 | |
| 8084 | crt_verify: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8085 | if (ssl->handshake->ecrs_enabled) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8086 | rs_ctx = &ssl->handshake->ecrs_ctx; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8087 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8088 | #endif |
| 8089 | |
Manuel Pégourié-Gonnard | ce60330 | 2024-08-16 11:03:42 +0200 | [diff] [blame] | 8090 | ret = mbedtls_ssl_verify_certificate(ssl, authmode, chain, |
| 8091 | ssl->handshake->ciphersuite_info, |
| 8092 | rs_ctx); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8093 | if (ret != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8094 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8095 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8096 | |
| 8097 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8098 | { |
| 8099 | unsigned char *crt_start, *pk_start; |
| 8100 | size_t crt_len, pk_len; |
| 8101 | |
| 8102 | /* We parse the CRT chain without copying, so |
| 8103 | * these pointers point into the input buffer, |
| 8104 | * and are hence still valid after freeing the |
| 8105 | * CRT chain. */ |
| 8106 | |
| 8107 | crt_start = chain->raw.p; |
| 8108 | crt_len = chain->raw.len; |
| 8109 | |
| 8110 | pk_start = chain->pk_raw.p; |
| 8111 | pk_len = chain->pk_raw.len; |
| 8112 | |
| 8113 | /* Free the CRT structures before computing |
| 8114 | * digest and copying the peer's public key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8115 | mbedtls_x509_crt_free(chain); |
| 8116 | mbedtls_free(chain); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8117 | chain = NULL; |
| 8118 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8119 | ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len); |
| 8120 | if (ret != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8121 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8122 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8123 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8124 | ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len); |
| 8125 | if (ret != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8126 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8127 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8128 | } |
| 8129 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8130 | /* Pass ownership to session structure. */ |
| 8131 | ssl->session_negotiate->peer_cert = chain; |
| 8132 | chain = NULL; |
| 8133 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8135 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8136 | |
| 8137 | exit: |
| 8138 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8139 | if (ret == 0) { |
Gilles Peskine | 49f179d | 2025-03-07 15:09:32 +0100 | [diff] [blame] | 8140 | mbedtls_ssl_handshake_increment_state(ssl); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8141 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8142 | |
| 8143 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8144 | if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8145 | ssl->handshake->ecrs_peer_cert = chain; |
| 8146 | chain = NULL; |
| 8147 | } |
| 8148 | #endif |
| 8149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8150 | if (chain != NULL) { |
| 8151 | mbedtls_x509_crt_free(chain); |
| 8152 | mbedtls_free(chain); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8153 | } |
| 8154 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8155 | return ret; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8156 | } |
| 8157 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 8158 | |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8159 | static int ssl_calc_finished_tls_generic(mbedtls_ssl_context *ssl, void *ctx, |
| 8160 | unsigned char *padbuf, size_t hlen, |
| 8161 | unsigned char *buf, int from) |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8162 | { |
Dave Rodgman | c37ad44 | 2023-11-03 23:36:06 +0000 | [diff] [blame] | 8163 | unsigned int len = 12; |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8164 | const char *sender; |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8165 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8166 | psa_status_t status; |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8167 | psa_hash_operation_t *hs_op = ctx; |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 8168 | psa_hash_operation_t cloned_op = PSA_HASH_OPERATION_INIT; |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8169 | size_t hash_size; |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8170 | #else |
Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 8171 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8172 | mbedtls_md_context_t *hs_ctx = ctx; |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 8173 | mbedtls_md_context_t cloned_ctx; |
| 8174 | mbedtls_md_init(&cloned_ctx); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8175 | #endif |
| 8176 | |
| 8177 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8178 | if (!session) { |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8179 | session = ssl->session; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8180 | } |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8181 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8182 | sender = (from == MBEDTLS_SSL_IS_CLIENT) |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8183 | ? "client finished" |
| 8184 | : "server finished"; |
| 8185 | |
| 8186 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8187 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls")); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8188 | |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 8189 | status = psa_hash_clone(hs_op, &cloned_op); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8190 | if (status != PSA_SUCCESS) { |
Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 8191 | goto exit; |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8192 | } |
| 8193 | |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 8194 | status = psa_hash_finish(&cloned_op, padbuf, hlen, &hash_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8195 | if (status != PSA_SUCCESS) { |
Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 8196 | goto exit; |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8197 | } |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8198 | MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, hlen); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8199 | #else |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8200 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls")); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8201 | |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 8202 | 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] | 8203 | if (ret != 0) { |
| 8204 | goto exit; |
| 8205 | } |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 8206 | ret = mbedtls_md_clone(&cloned_ctx, hs_ctx); |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 8207 | if (ret != 0) { |
| 8208 | goto exit; |
| 8209 | } |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8210 | |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 8211 | ret = mbedtls_md_finish(&cloned_ctx, padbuf); |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8212 | if (ret != 0) { |
| 8213 | goto exit; |
| 8214 | } |
| 8215 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8216 | |
| 8217 | MBEDTLS_SSL_DEBUG_BUF(4, "finished output", padbuf, hlen); |
| 8218 | |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8219 | /* |
| 8220 | * TLSv1.2: |
| 8221 | * hash = PRF( master, finished_label, |
| 8222 | * Hash( handshake ) )[0.11] |
| 8223 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8224 | ssl->handshake->tls_prf(session->master, 48, sender, |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8225 | padbuf, hlen, buf, len); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8226 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8227 | MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8228 | |
Paul Elliott | ecb95be | 2023-08-11 16:41:04 +0100 | [diff] [blame] | 8229 | mbedtls_platform_zeroize(padbuf, hlen); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8230 | |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8231 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished")); |
Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 8232 | |
| 8233 | exit: |
| 8234 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 8235 | psa_hash_abort(&cloned_op); |
Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 8236 | return mbedtls_md_error_from_psa(status); |
Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 8237 | #else |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 8238 | mbedtls_md_free(&cloned_ctx); |
Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 8239 | return ret; |
| 8240 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8241 | } |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8242 | |
| 8243 | #if defined(MBEDTLS_MD_CAN_SHA256) |
| 8244 | static int ssl_calc_finished_tls_sha256( |
| 8245 | mbedtls_ssl_context *ssl, unsigned char *buf, int from) |
| 8246 | { |
| 8247 | unsigned char padbuf[32]; |
| 8248 | return ssl_calc_finished_tls_generic(ssl, |
| 8249 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8250 | &ssl->handshake->fin_sha256_psa, |
| 8251 | #else |
| 8252 | &ssl->handshake->fin_sha256, |
| 8253 | #endif |
| 8254 | padbuf, sizeof(padbuf), |
| 8255 | buf, from); |
| 8256 | } |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 8257 | #endif /* MBEDTLS_MD_CAN_SHA256*/ |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8258 | |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 8259 | |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 8260 | #if defined(MBEDTLS_MD_CAN_SHA384) |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 8261 | static int ssl_calc_finished_tls_sha384( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8262 | mbedtls_ssl_context *ssl, unsigned char *buf, int from) |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 8263 | { |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 8264 | unsigned char padbuf[48]; |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8265 | return ssl_calc_finished_tls_generic(ssl, |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 8266 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8267 | &ssl->handshake->fin_sha384_psa, |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 8268 | #else |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8269 | &ssl->handshake->fin_sha384, |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 8270 | #endif |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8271 | padbuf, sizeof(padbuf), |
| 8272 | buf, from); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 8273 | } |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 8274 | #endif /* MBEDTLS_MD_CAN_SHA384*/ |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 8275 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8276 | void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl) |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 8277 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8278 | MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free")); |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 8279 | |
| 8280 | /* |
| 8281 | * Free our handshake params |
| 8282 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8283 | mbedtls_ssl_handshake_free(ssl); |
| 8284 | mbedtls_free(ssl->handshake); |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 8285 | ssl->handshake = NULL; |
| 8286 | |
| 8287 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 8288 | * Free the previous transform and switch in the current one |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 8289 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8290 | if (ssl->transform) { |
| 8291 | mbedtls_ssl_transform_free(ssl->transform); |
| 8292 | mbedtls_free(ssl->transform); |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 8293 | } |
| 8294 | ssl->transform = ssl->transform_negotiate; |
| 8295 | ssl->transform_negotiate = NULL; |
| 8296 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8297 | MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free")); |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 8298 | } |
| 8299 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8300 | void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl) |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8301 | { |
| 8302 | int resume = ssl->handshake->resume; |
| 8303 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8304 | MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup")); |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8305 | |
| 8306 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8307 | if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) { |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8308 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
| 8309 | ssl->renego_records_seen = 0; |
| 8310 | } |
| 8311 | #endif |
| 8312 | |
| 8313 | /* |
| 8314 | * Free the previous session and switch in the current one |
| 8315 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8316 | if (ssl->session) { |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8317 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 8318 | /* RFC 7366 3.1: keep the EtM state */ |
| 8319 | ssl->session_negotiate->encrypt_then_mac = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8320 | ssl->session->encrypt_then_mac; |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8321 | #endif |
| 8322 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8323 | mbedtls_ssl_session_free(ssl->session); |
| 8324 | mbedtls_free(ssl->session); |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8325 | } |
| 8326 | ssl->session = ssl->session_negotiate; |
| 8327 | ssl->session_negotiate = NULL; |
| 8328 | |
| 8329 | /* |
| 8330 | * Add cache entry |
| 8331 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8332 | if (ssl->conf->f_set_cache != NULL && |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8333 | ssl->session->id_len != 0 && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8334 | resume == 0) { |
| 8335 | if (ssl->conf->f_set_cache(ssl->conf->p_cache, |
| 8336 | ssl->session->id, |
| 8337 | ssl->session->id_len, |
| 8338 | ssl->session) != 0) { |
| 8339 | MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session")); |
| 8340 | } |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8341 | } |
| 8342 | |
| 8343 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8344 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 8345 | ssl->handshake->flight != NULL) { |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8346 | /* Cancel handshake timer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8347 | mbedtls_ssl_set_timer(ssl, 0); |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8348 | |
| 8349 | /* Keep last flight around in case we need to resend it: |
| 8350 | * we need the handshake and transform structures for that */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8351 | MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform")); |
| 8352 | } else |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8353 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8354 | mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl); |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8355 | |
Gilles Peskine | 49f179d | 2025-03-07 15:09:32 +0100 | [diff] [blame] | 8356 | mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER); |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8358 | MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup")); |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8359 | } |
| 8360 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8361 | int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl) |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8362 | { |
Dave Rodgman | c37ad44 | 2023-11-03 23:36:06 +0000 | [diff] [blame] | 8363 | int ret; |
| 8364 | unsigned int hash_len; |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8365 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8366 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished")); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8367 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8368 | mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8369 | |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 8370 | ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint); |
| 8371 | if (ret != 0) { |
| 8372 | MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret); |
David Horstmann | b5ef7da | 2025-03-07 17:20:59 +0000 | [diff] [blame] | 8373 | return ret; |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 8374 | } |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8375 | |
| 8376 | /* |
| 8377 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 8378 | * may define some other value. Currently (early 2016), no defined |
| 8379 | * ciphersuite does this (and this is unlikely to change as activity has |
| 8380 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 8381 | */ |
| 8382 | hash_len = 12; |
| 8383 | |
| 8384 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 8385 | ssl->verify_data_len = hash_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8386 | memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8387 | #endif |
| 8388 | |
| 8389 | ssl->out_msglen = 4 + hash_len; |
| 8390 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 8391 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
| 8392 | |
| 8393 | /* |
| 8394 | * In case of session resuming, invert the client and server |
| 8395 | * ChangeCipherSpec messages order. |
| 8396 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8397 | if (ssl->handshake->resume != 0) { |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8398 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8399 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Gilles Peskine | 49f179d | 2025-03-07 15:09:32 +0100 | [diff] [blame] | 8400 | mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8401 | } |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8402 | #endif |
| 8403 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8404 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Gilles Peskine | 49f179d | 2025-03-07 15:09:32 +0100 | [diff] [blame] | 8405 | mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8406 | } |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8407 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8408 | } else { |
Gilles Peskine | 49f179d | 2025-03-07 15:09:32 +0100 | [diff] [blame] | 8409 | mbedtls_ssl_handshake_increment_state(ssl); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8410 | } |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8411 | |
| 8412 | /* |
| 8413 | * Switch to our negotiated transform and session parameters for outbound |
| 8414 | * data. |
| 8415 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8416 | 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] | 8417 | |
| 8418 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8419 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8420 | unsigned char i; |
| 8421 | |
| 8422 | /* Remember current epoch settings for resending */ |
| 8423 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8424 | memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, |
| 8425 | sizeof(ssl->handshake->alt_out_ctr)); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8426 | |
| 8427 | /* Set sequence_number to zero */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8428 | 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] | 8429 | |
| 8430 | |
| 8431 | /* Increment epoch */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8432 | for (i = 2; i > 0; i--) { |
| 8433 | if (++ssl->cur_out_ctr[i - 1] != 0) { |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8434 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8435 | } |
| 8436 | } |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8437 | |
| 8438 | /* The loop goes to its end iff the counter is wrapping */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8439 | if (i == 0) { |
| 8440 | MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap")); |
| 8441 | return MBEDTLS_ERR_SSL_COUNTER_WRAPPING; |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8442 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8443 | } else |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8444 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8445 | memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr)); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8446 | |
| 8447 | ssl->transform_out = ssl->transform_negotiate; |
| 8448 | ssl->session_out = ssl->session_negotiate; |
| 8449 | |
| 8450 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8451 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 8452 | mbedtls_ssl_send_flight_completed(ssl); |
| 8453 | } |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8454 | #endif |
| 8455 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8456 | if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { |
| 8457 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret); |
| 8458 | return ret; |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8459 | } |
| 8460 | |
| 8461 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8462 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 8463 | (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) { |
| 8464 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret); |
| 8465 | return ret; |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8466 | } |
| 8467 | #endif |
| 8468 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8469 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished")); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8470 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8471 | return 0; |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8472 | } |
| 8473 | |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8474 | #define SSL_MAX_HASH_LEN 12 |
| 8475 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8476 | int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl) |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8477 | { |
| 8478 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 8479 | unsigned int hash_len = 12; |
| 8480 | unsigned char buf[SSL_MAX_HASH_LEN]; |
| 8481 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8482 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished")); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8483 | |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 8484 | ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1); |
| 8485 | if (ret != 0) { |
| 8486 | MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret); |
David Horstmann | b5ef7da | 2025-03-07 17:20:59 +0000 | [diff] [blame] | 8487 | return ret; |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 8488 | } |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8489 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8490 | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { |
| 8491 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8492 | goto exit; |
| 8493 | } |
| 8494 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8495 | if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) { |
| 8496 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message")); |
| 8497 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8498 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8499 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 8500 | goto exit; |
| 8501 | } |
| 8502 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8503 | if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) { |
| 8504 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8505 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8506 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 8507 | goto exit; |
| 8508 | } |
| 8509 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8510 | if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) { |
| 8511 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message")); |
| 8512 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8513 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8514 | ret = MBEDTLS_ERR_SSL_DECODE_ERROR; |
| 8515 | goto exit; |
| 8516 | } |
| 8517 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8518 | if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), |
| 8519 | buf, hash_len) != 0) { |
| 8520 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message")); |
| 8521 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8522 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8523 | ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
| 8524 | goto exit; |
| 8525 | } |
| 8526 | |
| 8527 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 8528 | ssl->verify_data_len = hash_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8529 | memcpy(ssl->peer_verify_data, buf, hash_len); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8530 | #endif |
| 8531 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8532 | if (ssl->handshake->resume != 0) { |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8533 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8534 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Gilles Peskine | 49f179d | 2025-03-07 15:09:32 +0100 | [diff] [blame] | 8535 | mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8536 | } |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8537 | #endif |
| 8538 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8539 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Gilles Peskine | 49f179d | 2025-03-07 15:09:32 +0100 | [diff] [blame] | 8540 | mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8541 | } |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8542 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8543 | } else { |
Gilles Peskine | 49f179d | 2025-03-07 15:09:32 +0100 | [diff] [blame] | 8544 | mbedtls_ssl_handshake_increment_state(ssl); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8545 | } |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8546 | |
| 8547 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8548 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 8549 | mbedtls_ssl_recv_flight_completed(ssl); |
| 8550 | } |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8551 | #endif |
| 8552 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8553 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished")); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8554 | |
| 8555 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8556 | mbedtls_platform_zeroize(buf, hash_len); |
| 8557 | return ret; |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8558 | } |
| 8559 | |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 8560 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 8561 | /* |
| 8562 | * Helper to get TLS 1.2 PRF from ciphersuite |
| 8563 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) |
| 8564 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8565 | static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id) |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 8566 | { |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 8567 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8568 | mbedtls_ssl_ciphersuite_from_id(ciphersuite_id); |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 8569 | #if defined(MBEDTLS_MD_CAN_SHA384) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8570 | if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) { |
| 8571 | return tls_prf_sha384; |
| 8572 | } else |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 8573 | #endif |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 8574 | #if defined(MBEDTLS_MD_CAN_SHA256) |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 8575 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8576 | if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) { |
| 8577 | return tls_prf_sha256; |
| 8578 | } |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 8579 | } |
| 8580 | #endif |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 8581 | #if !defined(MBEDTLS_MD_CAN_SHA384) && \ |
| 8582 | !defined(MBEDTLS_MD_CAN_SHA256) |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 8583 | (void) ciphersuite_info; |
| 8584 | #endif |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 8585 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8586 | return NULL; |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 8587 | } |
| 8588 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 8589 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8590 | 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] | 8591 | { |
| 8592 | ((void) tls_prf); |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 8593 | #if defined(MBEDTLS_MD_CAN_SHA384) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8594 | if (tls_prf == tls_prf_sha384) { |
| 8595 | return MBEDTLS_SSL_TLS_PRF_SHA384; |
| 8596 | } else |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 8597 | #endif |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 8598 | #if defined(MBEDTLS_MD_CAN_SHA256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8599 | if (tls_prf == tls_prf_sha256) { |
| 8600 | return MBEDTLS_SSL_TLS_PRF_SHA256; |
| 8601 | } else |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 8602 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8603 | return MBEDTLS_SSL_TLS_PRF_NONE; |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 8604 | } |
| 8605 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8606 | /* |
| 8607 | * Populate a transform structure with session keys and all the other |
| 8608 | * necessary information. |
| 8609 | * |
| 8610 | * Parameters: |
| 8611 | * - [in/out]: transform: structure to populate |
| 8612 | * [in] must be just initialised with mbedtls_ssl_transform_init() |
| 8613 | * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf() |
| 8614 | * - [in] ciphersuite |
| 8615 | * - [in] master |
| 8616 | * - [in] encrypt_then_mac |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8617 | * - [in] tls_prf: pointer to PRF to use for key derivation |
| 8618 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 8619 | * - [in] tls_version: TLS version |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8620 | * - [in] endpoint: client or server |
| 8621 | * - [in] ssl: used for: |
| 8622 | * - ssl->conf->{f,p}_export_keys |
| 8623 | * [in] optionally used for: |
| 8624 | * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg |
| 8625 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 8626 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8627 | static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform, |
| 8628 | int ciphersuite, |
| 8629 | const unsigned char master[48], |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8630 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8631 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8632 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8633 | ssl_tls_prf_t tls_prf, |
| 8634 | const unsigned char randbytes[64], |
| 8635 | mbedtls_ssl_protocol_version tls_version, |
| 8636 | unsigned endpoint, |
| 8637 | const mbedtls_ssl_context *ssl) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8638 | { |
| 8639 | int ret = 0; |
| 8640 | unsigned char keyblk[256]; |
| 8641 | unsigned char *key1; |
| 8642 | unsigned char *key2; |
| 8643 | unsigned char *mac_enc; |
| 8644 | unsigned char *mac_dec; |
| 8645 | size_t mac_key_len = 0; |
| 8646 | size_t iv_copy_len; |
| 8647 | size_t keylen; |
| 8648 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8649 | mbedtls_ssl_mode_t ssl_mode; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8650 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8651 | const mbedtls_cipher_info_t *cipher_info; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8652 | const mbedtls_md_info_t *md_info; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8653 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8654 | |
| 8655 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8656 | psa_key_type_t key_type; |
| 8657 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8658 | psa_algorithm_t alg; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8659 | psa_algorithm_t mac_alg = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8660 | size_t key_bits; |
| 8661 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 8662 | #endif |
| 8663 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8664 | /* |
| 8665 | * Some data just needs copying into the structure |
| 8666 | */ |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8667 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8668 | transform->encrypt_then_mac = encrypt_then_mac; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8669 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 8670 | transform->tls_version = tls_version; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8671 | |
Max Fillinger | 951b886 | 2024-10-25 00:52:24 +0200 | [diff] [blame] | 8672 | #if defined(MBEDTLS_SSL_KEEP_RANDBYTES) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8673 | memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes)); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8674 | #endif |
| 8675 | |
| 8676 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8677 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8678 | /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform |
| 8679 | * generation separate. This should never happen. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8680 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8681 | } |
| 8682 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 8683 | |
| 8684 | /* |
| 8685 | * Get various info structures |
| 8686 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8687 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite); |
| 8688 | if (ciphersuite_info == NULL) { |
| 8689 | MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found", |
| 8690 | ciphersuite)); |
| 8691 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8692 | } |
| 8693 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 8694 | ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8695 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8696 | encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8697 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8698 | ciphersuite_info); |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8699 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8700 | if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) { |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8701 | transform->taglen = |
| 8702 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8703 | } |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8704 | |
| 8705 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Dave Rodgman | 2eab462 | 2023-10-05 13:30:37 +0100 | [diff] [blame] | 8706 | 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] | 8707 | transform->taglen, |
| 8708 | &alg, |
| 8709 | &key_type, |
| 8710 | &key_bits)) != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 8711 | ret = PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8712 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8713 | goto end; |
| 8714 | } |
| 8715 | #else |
Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 8716 | 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] | 8717 | if (cipher_info == NULL) { |
| 8718 | MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found", |
| 8719 | ciphersuite_info->cipher)); |
| 8720 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8721 | } |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8722 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8723 | |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8724 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Dave Rodgman | 2eab462 | 2023-10-05 13:30:37 +0100 | [diff] [blame] | 8725 | 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] | 8726 | if (mac_alg == 0) { |
Manuel Pégourié-Gonnard | 2d6d993 | 2023-03-28 11:38:08 +0200 | [diff] [blame] | 8727 | 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] | 8728 | (unsigned) ciphersuite_info->mac)); |
| 8729 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8730 | } |
| 8731 | #else |
Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 8732 | 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] | 8733 | if (md_info == NULL) { |
| 8734 | MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found", |
| 8735 | (unsigned) ciphersuite_info->mac)); |
| 8736 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8737 | } |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8738 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8739 | |
| 8740 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 8741 | /* Copy own and peer's CID if the use of the CID |
| 8742 | * extension has been negotiated. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8743 | if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) { |
| 8744 | MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform")); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8745 | |
| 8746 | transform->in_cid_len = ssl->own_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8747 | memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len); |
| 8748 | MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid, |
| 8749 | transform->in_cid_len); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8750 | |
| 8751 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8752 | memcpy(transform->out_cid, ssl->handshake->peer_cid, |
| 8753 | ssl->handshake->peer_cid_len); |
| 8754 | MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid, |
| 8755 | transform->out_cid_len); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8756 | } |
| 8757 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 8758 | |
| 8759 | /* |
| 8760 | * Compute key block using the PRF |
| 8761 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8762 | ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256); |
| 8763 | if (ret != 0) { |
| 8764 | MBEDTLS_SSL_DEBUG_RET(1, "prf", ret); |
| 8765 | return ret; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8766 | } |
| 8767 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8768 | MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s", |
| 8769 | mbedtls_ssl_get_ciphersuite_name(ciphersuite))); |
| 8770 | MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48); |
| 8771 | MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64); |
| 8772 | MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8773 | |
| 8774 | /* |
| 8775 | * Determine the appropriate key, IV and MAC length. |
| 8776 | */ |
| 8777 | |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8778 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8779 | keylen = PSA_BITS_TO_BYTES(key_bits); |
| 8780 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8781 | keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8782 | #endif |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8783 | |
Valerio Setti | e570704 | 2023-10-11 11:54:42 +0200 | [diff] [blame] | 8784 | #if defined(MBEDTLS_SSL_HAVE_AEAD) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8785 | if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8786 | size_t explicit_ivlen; |
| 8787 | |
| 8788 | transform->maclen = 0; |
| 8789 | mac_key_len = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8790 | |
| 8791 | /* All modes haves 96-bit IVs, but the length of the static parts vary |
| 8792 | * with mode and version: |
| 8793 | * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes |
| 8794 | * (to be concatenated with a dynamically chosen IV of 8 Bytes) |
| 8795 | * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's |
| 8796 | * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record |
| 8797 | * sequence number). |
| 8798 | */ |
| 8799 | transform->ivlen = 12; |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 8800 | |
| 8801 | int is_chachapoly = 0; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8802 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8803 | is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8804 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8805 | is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info) |
| 8806 | == MBEDTLS_MODE_CHACHAPOLY); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8807 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 8808 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8809 | if (is_chachapoly) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8810 | transform->fixed_ivlen = 12; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8811 | } else { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8812 | transform->fixed_ivlen = 4; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8813 | } |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8814 | |
| 8815 | /* Minimum length of encrypted record */ |
| 8816 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
| 8817 | transform->minlen = explicit_ivlen + transform->taglen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8818 | } else |
Valerio Setti | e570704 | 2023-10-11 11:54:42 +0200 | [diff] [blame] | 8819 | #endif /* MBEDTLS_SSL_HAVE_AEAD */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8820 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8821 | if (ssl_mode == MBEDTLS_SSL_MODE_STREAM || |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8822 | ssl_mode == MBEDTLS_SSL_MODE_CBC || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8823 | ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) { |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8824 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8825 | size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8826 | #else |
Dave Rodgman | 85a8813 | 2023-06-24 11:41:50 +0100 | [diff] [blame] | 8827 | size_t block_size = mbedtls_cipher_info_get_block_size(cipher_info); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8828 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8829 | |
| 8830 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8831 | /* Get MAC length */ |
| 8832 | mac_key_len = PSA_HASH_LENGTH(mac_alg); |
| 8833 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8834 | /* Initialize HMAC contexts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8835 | if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 || |
| 8836 | (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) { |
| 8837 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8838 | goto end; |
| 8839 | } |
| 8840 | |
| 8841 | /* Get MAC length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8842 | mac_key_len = mbedtls_md_get_size(md_info); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8843 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8844 | transform->maclen = mac_key_len; |
| 8845 | |
| 8846 | /* IV length */ |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8847 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8848 | transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8849 | #else |
Dave Rodgman | bb521fd | 2023-06-24 11:21:25 +0100 | [diff] [blame] | 8850 | transform->ivlen = mbedtls_cipher_info_get_iv_size(cipher_info); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8851 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8852 | |
| 8853 | /* Minimum length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8854 | if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8855 | transform->minlen = transform->maclen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8856 | } else { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8857 | /* |
| 8858 | * GenericBlockCipher: |
| 8859 | * 1. if EtM is in use: one block plus MAC |
| 8860 | * otherwise: * first multiple of blocklen greater than maclen |
| 8861 | * 2. IV |
| 8862 | */ |
| 8863 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8864 | if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8865 | transform->minlen = transform->maclen |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8866 | + block_size; |
| 8867 | } else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8868 | #endif |
| 8869 | { |
| 8870 | transform->minlen = transform->maclen |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8871 | + block_size |
| 8872 | - transform->maclen % block_size; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8873 | } |
| 8874 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8875 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8876 | transform->minlen += transform->ivlen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8877 | } else { |
| 8878 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8879 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 8880 | goto end; |
| 8881 | } |
| 8882 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8883 | } else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8884 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 8885 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8886 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 8887 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8888 | } |
| 8889 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8890 | MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 8891 | (unsigned) keylen, |
| 8892 | (unsigned) transform->minlen, |
| 8893 | (unsigned) transform->ivlen, |
| 8894 | (unsigned) transform->maclen)); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8895 | |
| 8896 | /* |
| 8897 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 8898 | */ |
| 8899 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8900 | if (endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8901 | key1 = keyblk + mac_key_len * 2; |
| 8902 | key2 = keyblk + mac_key_len * 2 + keylen; |
| 8903 | |
| 8904 | mac_enc = keyblk; |
| 8905 | mac_dec = keyblk + mac_key_len; |
| 8906 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8907 | iv_copy_len = (transform->fixed_ivlen) ? |
| 8908 | transform->fixed_ivlen : transform->ivlen; |
| 8909 | memcpy(transform->iv_enc, key2 + keylen, iv_copy_len); |
| 8910 | memcpy(transform->iv_dec, key2 + keylen + iv_copy_len, |
| 8911 | iv_copy_len); |
| 8912 | } else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8913 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 8914 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8915 | if (endpoint == MBEDTLS_SSL_IS_SERVER) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8916 | key1 = keyblk + mac_key_len * 2 + keylen; |
| 8917 | key2 = keyblk + mac_key_len * 2; |
| 8918 | |
| 8919 | mac_enc = keyblk + mac_key_len; |
| 8920 | mac_dec = keyblk; |
| 8921 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8922 | iv_copy_len = (transform->fixed_ivlen) ? |
| 8923 | transform->fixed_ivlen : transform->ivlen; |
| 8924 | memcpy(transform->iv_dec, key1 + keylen, iv_copy_len); |
| 8925 | memcpy(transform->iv_enc, key1 + keylen + iv_copy_len, |
| 8926 | iv_copy_len); |
| 8927 | } else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8928 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 8929 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8930 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8931 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 8932 | goto end; |
| 8933 | } |
| 8934 | |
Paul Elliott | 4fb1955 | 2023-10-18 12:15:30 +0100 | [diff] [blame] | 8935 | if (ssl->f_export_keys != NULL) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8936 | ssl->f_export_keys(ssl->p_export_keys, |
| 8937 | MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET, |
| 8938 | master, 48, |
| 8939 | randbytes + 32, |
| 8940 | randbytes, |
| 8941 | tls_prf_get_type(tls_prf)); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8942 | } |
| 8943 | |
| 8944 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8945 | transform->psa_alg = alg; |
| 8946 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8947 | if (alg != MBEDTLS_SSL_NULL_CIPHER) { |
| 8948 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 8949 | psa_set_key_algorithm(&attributes, alg); |
| 8950 | psa_set_key_type(&attributes, key_type); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8951 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8952 | if ((status = psa_import_key(&attributes, |
| 8953 | key1, |
| 8954 | PSA_BITS_TO_BYTES(key_bits), |
| 8955 | &transform->psa_key_enc)) != PSA_SUCCESS) { |
| 8956 | MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status); |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 8957 | ret = PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8958 | MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8959 | goto end; |
| 8960 | } |
| 8961 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8962 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8963 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8964 | if ((status = psa_import_key(&attributes, |
| 8965 | key2, |
| 8966 | PSA_BITS_TO_BYTES(key_bits), |
| 8967 | &transform->psa_key_dec)) != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 8968 | ret = PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8969 | MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8970 | goto end; |
| 8971 | } |
| 8972 | } |
| 8973 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8974 | if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc, |
| 8975 | cipher_info)) != 0) { |
| 8976 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8977 | goto end; |
| 8978 | } |
| 8979 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8980 | if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec, |
| 8981 | cipher_info)) != 0) { |
| 8982 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8983 | goto end; |
| 8984 | } |
| 8985 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8986 | if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1, |
| 8987 | (int) mbedtls_cipher_info_get_key_bitlen(cipher_info), |
| 8988 | MBEDTLS_ENCRYPT)) != 0) { |
| 8989 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8990 | goto end; |
| 8991 | } |
| 8992 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8993 | if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2, |
| 8994 | (int) mbedtls_cipher_info_get_key_bitlen(cipher_info), |
| 8995 | MBEDTLS_DECRYPT)) != 0) { |
| 8996 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8997 | goto end; |
| 8998 | } |
| 8999 | |
| 9000 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9001 | if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) { |
| 9002 | if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc, |
| 9003 | MBEDTLS_PADDING_NONE)) != 0) { |
| 9004 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 9005 | goto end; |
| 9006 | } |
| 9007 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9008 | if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec, |
| 9009 | MBEDTLS_PADDING_NONE)) != 0) { |
| 9010 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 9011 | goto end; |
| 9012 | } |
| 9013 | } |
| 9014 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 9015 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 9016 | |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 9017 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 9018 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 9019 | For AEAD-based ciphersuites, there is nothing to do here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9020 | if (mac_key_len != 0) { |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 9021 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9022 | transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 9023 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9024 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 9025 | psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg)); |
| 9026 | psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 9027 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9028 | if ((status = psa_import_key(&attributes, |
| 9029 | mac_enc, mac_key_len, |
| 9030 | &transform->psa_mac_enc)) != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 9031 | ret = PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9032 | MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 9033 | goto end; |
| 9034 | } |
| 9035 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9036 | if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) || |
| 9037 | ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING) |
Andrzej Kurek | 8c95ac4 | 2022-08-17 16:17:00 -0400 | [diff] [blame] | 9038 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9039 | && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED) |
Andrzej Kurek | 8c95ac4 | 2022-08-17 16:17:00 -0400 | [diff] [blame] | 9040 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9041 | )) { |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 9042 | /* mbedtls_ct_hmac() requires the key to be exportable */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9043 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT | |
| 9044 | PSA_KEY_USAGE_VERIFY_HASH); |
| 9045 | } else { |
| 9046 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 9047 | } |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 9048 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9049 | if ((status = psa_import_key(&attributes, |
| 9050 | mac_dec, mac_key_len, |
| 9051 | &transform->psa_mac_dec)) != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 9052 | ret = PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9053 | MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 9054 | goto end; |
| 9055 | } |
| 9056 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9057 | ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len); |
| 9058 | if (ret != 0) { |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 9059 | goto end; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9060 | } |
| 9061 | ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len); |
| 9062 | if (ret != 0) { |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 9063 | goto end; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9064 | } |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 9065 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 9066 | } |
| 9067 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 9068 | |
| 9069 | ((void) mac_dec); |
| 9070 | ((void) mac_enc); |
| 9071 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 9072 | end: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9073 | mbedtls_platform_zeroize(keyblk, sizeof(keyblk)); |
| 9074 | return ret; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 9075 | } |
| 9076 | |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 9077 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \ |
| 9078 | defined(MBEDTLS_USE_PSA_CRYPTO) |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 9079 | int mbedtls_psa_ecjpake_read_round( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9080 | psa_pake_operation_t *pake_ctx, |
| 9081 | const unsigned char *buf, |
| 9082 | size_t len, mbedtls_ecjpake_rounds_t round) |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 9083 | { |
| 9084 | psa_status_t status; |
| 9085 | size_t input_offset = 0; |
Valerio Setti | 819de86 | 2022-11-17 18:05:19 +0100 | [diff] [blame] | 9086 | /* |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 9087 | * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice |
| 9088 | * At round two perform a single cycle |
| 9089 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9090 | unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 9091 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9092 | for (; remaining_steps > 0; remaining_steps--) { |
| 9093 | for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 9094 | step <= PSA_PAKE_STEP_ZK_PROOF; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9095 | ++step) { |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 9096 | /* Length is stored at the first byte */ |
| 9097 | size_t length = buf[input_offset]; |
| 9098 | input_offset += 1; |
| 9099 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9100 | if (input_offset + length > len) { |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 9101 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
| 9102 | } |
| 9103 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9104 | status = psa_pake_input(pake_ctx, step, |
| 9105 | buf + input_offset, length); |
| 9106 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 9107 | return PSA_TO_MBEDTLS_ERR(status); |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 9108 | } |
| 9109 | |
| 9110 | input_offset += length; |
| 9111 | } |
| 9112 | } |
| 9113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9114 | if (input_offset != len) { |
Valerio Setti | 61ea17d | 2022-11-18 12:11:00 +0100 | [diff] [blame] | 9115 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9116 | } |
Valerio Setti | 30ebe11 | 2022-11-17 16:23:34 +0100 | [diff] [blame] | 9117 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9118 | return 0; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 9119 | } |
| 9120 | |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 9121 | int mbedtls_psa_ecjpake_write_round( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9122 | psa_pake_operation_t *pake_ctx, |
| 9123 | unsigned char *buf, |
| 9124 | size_t len, size_t *olen, |
| 9125 | mbedtls_ecjpake_rounds_t round) |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 9126 | { |
| 9127 | psa_status_t status; |
| 9128 | size_t output_offset = 0; |
| 9129 | size_t output_len; |
Valerio Setti | 819de86 | 2022-11-17 18:05:19 +0100 | [diff] [blame] | 9130 | /* |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 9131 | * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice |
| 9132 | * At round two perform a single cycle |
| 9133 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9134 | unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 9135 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9136 | for (; remaining_steps > 0; remaining_steps--) { |
| 9137 | for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE; |
| 9138 | step <= PSA_PAKE_STEP_ZK_PROOF; |
| 9139 | ++step) { |
Valerio Setti | 79f6b6b | 2022-11-21 14:17:03 +0100 | [diff] [blame] | 9140 | /* |
Valerio Setti | d4a9b1a | 2022-11-22 11:11:10 +0100 | [diff] [blame] | 9141 | * For each step, prepend 1 byte with the length of the data as |
| 9142 | * given by psa_pake_output(). |
Valerio Setti | 79f6b6b | 2022-11-21 14:17:03 +0100 | [diff] [blame] | 9143 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9144 | status = psa_pake_output(pake_ctx, step, |
| 9145 | buf + output_offset + 1, |
| 9146 | len - output_offset - 1, |
| 9147 | &output_len); |
| 9148 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 9149 | return PSA_TO_MBEDTLS_ERR(status); |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 9150 | } |
| 9151 | |
Valerio Setti | 99d88c1 | 2022-11-22 16:03:43 +0100 | [diff] [blame] | 9152 | *(buf + output_offset) = (uint8_t) output_len; |
Valerio Setti | 79f6b6b | 2022-11-21 14:17:03 +0100 | [diff] [blame] | 9153 | |
| 9154 | output_offset += output_len + 1; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 9155 | } |
| 9156 | } |
| 9157 | |
| 9158 | *olen = output_offset; |
| 9159 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9160 | return 0; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 9161 | } |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 9162 | #endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO |
| 9163 | |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9164 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9165 | int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl, |
| 9166 | unsigned char *hash, size_t *hashlen, |
| 9167 | unsigned char *data, size_t data_len, |
| 9168 | mbedtls_md_type_t md_alg) |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9169 | { |
| 9170 | psa_status_t status; |
| 9171 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; |
Manuel Pégourié-Gonnard | 2d6d993 | 2023-03-28 11:38:08 +0200 | [diff] [blame] | 9172 | 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] | 9173 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9174 | 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] | 9175 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9176 | if ((status = psa_hash_setup(&hash_operation, |
| 9177 | hash_alg)) != PSA_SUCCESS) { |
| 9178 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9179 | goto exit; |
| 9180 | } |
| 9181 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9182 | if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes, |
| 9183 | 64)) != PSA_SUCCESS) { |
| 9184 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9185 | goto exit; |
| 9186 | } |
| 9187 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9188 | if ((status = psa_hash_update(&hash_operation, |
| 9189 | data, data_len)) != PSA_SUCCESS) { |
| 9190 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9191 | goto exit; |
| 9192 | } |
| 9193 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9194 | if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE, |
| 9195 | hashlen)) != PSA_SUCCESS) { |
| 9196 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status); |
| 9197 | goto exit; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9198 | } |
| 9199 | |
| 9200 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9201 | if (status != PSA_SUCCESS) { |
| 9202 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 9203 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
| 9204 | switch (status) { |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9205 | case PSA_ERROR_NOT_SUPPORTED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9206 | return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9207 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ |
| 9208 | case PSA_ERROR_BUFFER_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9209 | return MBEDTLS_ERR_MD_BAD_INPUT_DATA; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9210 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9211 | return MBEDTLS_ERR_MD_ALLOC_FAILED; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9212 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9213 | return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9214 | } |
| 9215 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9216 | return 0; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9217 | } |
| 9218 | |
| 9219 | #else |
| 9220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9221 | int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl, |
| 9222 | unsigned char *hash, size_t *hashlen, |
| 9223 | unsigned char *data, size_t data_len, |
| 9224 | mbedtls_md_type_t md_alg) |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9225 | { |
| 9226 | int ret = 0; |
| 9227 | mbedtls_md_context_t ctx; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9228 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg); |
| 9229 | *hashlen = mbedtls_md_get_size(md_info); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9230 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9231 | 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] | 9232 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9233 | mbedtls_md_init(&ctx); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9234 | |
| 9235 | /* |
| 9236 | * digitally-signed struct { |
| 9237 | * opaque client_random[32]; |
| 9238 | * opaque server_random[32]; |
| 9239 | * ServerDHParams params; |
| 9240 | * }; |
| 9241 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9242 | if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) { |
| 9243 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9244 | goto exit; |
| 9245 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9246 | if ((ret = mbedtls_md_starts(&ctx)) != 0) { |
| 9247 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9248 | goto exit; |
| 9249 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9250 | if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) { |
| 9251 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9252 | goto exit; |
| 9253 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9254 | if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) { |
| 9255 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9256 | goto exit; |
| 9257 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9258 | if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) { |
| 9259 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9260 | goto exit; |
| 9261 | } |
| 9262 | |
| 9263 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9264 | mbedtls_md_free(&ctx); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9266 | if (ret != 0) { |
| 9267 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 9268 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
| 9269 | } |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9270 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9271 | return ret; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9272 | } |
| 9273 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 9274 | |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 9275 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 9276 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 9277 | /* Find the preferred hash for a given signature algorithm. */ |
| 9278 | unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9279 | mbedtls_ssl_context *ssl, |
| 9280 | unsigned int sig_alg) |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 9281 | { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 9282 | unsigned int i; |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 9283 | uint16_t *received_sig_algs = ssl->handshake->received_sig_algs; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 9284 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9285 | if (sig_alg == MBEDTLS_SSL_SIG_ANON) { |
| 9286 | return MBEDTLS_SSL_HASH_NONE; |
| 9287 | } |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 9288 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9289 | for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) { |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 9290 | unsigned int hash_alg_received = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9291 | MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG( |
| 9292 | received_sig_algs[i]); |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 9293 | unsigned int sig_alg_received = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9294 | MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG( |
| 9295 | received_sig_algs[i]); |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 9296 | |
Manuel Pégourié-Gonnard | 47bb380 | 2023-06-05 12:40:32 +0200 | [diff] [blame] | 9297 | mbedtls_md_type_t md_alg = |
| 9298 | mbedtls_ssl_md_alg_from_hash((unsigned char) hash_alg_received); |
| 9299 | if (md_alg == MBEDTLS_MD_NONE) { |
| 9300 | continue; |
| 9301 | } |
| 9302 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9303 | if (sig_alg == sig_alg_received) { |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 9304 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9305 | if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) { |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 9306 | psa_algorithm_t psa_hash_alg = |
Manuel Pégourié-Gonnard | 47bb380 | 2023-06-05 12:40:32 +0200 | [diff] [blame] | 9307 | mbedtls_md_psa_alg_from_type(md_alg); |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 9308 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9309 | if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA && |
| 9310 | !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key, |
| 9311 | PSA_ALG_ECDSA(psa_hash_alg), |
| 9312 | PSA_KEY_USAGE_SIGN_HASH)) { |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 9313 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9314 | } |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 9315 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9316 | if (sig_alg_received == MBEDTLS_SSL_SIG_RSA && |
| 9317 | !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key, |
| 9318 | PSA_ALG_RSA_PKCS1V15_SIGN( |
| 9319 | psa_hash_alg), |
| 9320 | PSA_KEY_USAGE_SIGN_HASH)) { |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 9321 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9322 | } |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 9323 | } |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 9324 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 9325 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9326 | return hash_alg_received; |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 9327 | } |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 9328 | } |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 9329 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9330 | return MBEDTLS_SSL_HASH_NONE; |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 9331 | } |
| 9332 | |
| 9333 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 9334 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 9335 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 9336 | |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9337 | int mbedtls_ssl_validate_ciphersuite( |
| 9338 | const mbedtls_ssl_context *ssl, |
| 9339 | const mbedtls_ssl_ciphersuite_t *suite_info, |
| 9340 | mbedtls_ssl_protocol_version min_tls_version, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9341 | mbedtls_ssl_protocol_version max_tls_version) |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9342 | { |
| 9343 | (void) ssl; |
| 9344 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9345 | if (suite_info == NULL) { |
| 9346 | return -1; |
| 9347 | } |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9349 | if ((suite_info->min_tls_version > max_tls_version) || |
| 9350 | (suite_info->max_tls_version < min_tls_version)) { |
| 9351 | return -1; |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9352 | } |
| 9353 | |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 9354 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C) |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9355 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 9356 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9357 | if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
| 9358 | ssl->handshake->psa_pake_ctx_is_ok != 1) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 9359 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9360 | if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
| 9361 | mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 9362 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9363 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9364 | return -1; |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9365 | } |
| 9366 | #endif |
| 9367 | |
| 9368 | /* Don't suggest PSK-based ciphersuite if no PSK is available. */ |
| 9369 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9370 | if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) && |
| 9371 | mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) { |
| 9372 | return -1; |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9373 | } |
| 9374 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
| 9375 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 9376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9377 | return 0; |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9378 | } |
| 9379 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 9380 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9381 | /* |
| 9382 | * Function for writing a signature algorithm extension. |
| 9383 | * |
| 9384 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 9385 | * value (TLS 1.3 RFC8446): |
| 9386 | * enum { |
| 9387 | * .... |
| 9388 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 9389 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 9390 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 9391 | * .... |
| 9392 | * } SignatureScheme; |
| 9393 | * |
| 9394 | * struct { |
| 9395 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 9396 | * } SignatureSchemeList; |
| 9397 | * |
| 9398 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 9399 | * value (TLS 1.2 RFC5246): |
| 9400 | * enum { |
| 9401 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 9402 | * sha512(6), (255) |
| 9403 | * } HashAlgorithm; |
| 9404 | * |
| 9405 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 9406 | * SignatureAlgorithm; |
| 9407 | * |
| 9408 | * struct { |
| 9409 | * HashAlgorithm hash; |
| 9410 | * SignatureAlgorithm signature; |
| 9411 | * } SignatureAndHashAlgorithm; |
| 9412 | * |
| 9413 | * SignatureAndHashAlgorithm |
| 9414 | * supported_signature_algorithms<2..2^16-2>; |
| 9415 | * |
| 9416 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 9417 | * generalization of the TLS 1.2 signature algorithm extension. |
| 9418 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 9419 | * `SignatureScheme` field of TLS 1.3 |
| 9420 | * |
| 9421 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9422 | int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf, |
| 9423 | const unsigned char *end, size_t *out_len) |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9424 | { |
| 9425 | unsigned char *p = buf; |
| 9426 | unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */ |
| 9427 | size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */ |
| 9428 | |
| 9429 | *out_len = 0; |
| 9430 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9431 | MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension")); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9432 | |
| 9433 | /* Check if we have space for header and length field: |
| 9434 | * - extension_type (2 bytes) |
| 9435 | * - extension_data_length (2 bytes) |
| 9436 | * - supported_signature_algorithms_length (2 bytes) |
| 9437 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9438 | MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9439 | p += 6; |
| 9440 | |
| 9441 | /* |
| 9442 | * Write supported_signature_algorithms |
| 9443 | */ |
| 9444 | supported_sig_alg = p; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9445 | const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl); |
| 9446 | if (sig_alg == NULL) { |
| 9447 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
| 9448 | } |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9449 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9450 | for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) { |
| 9451 | MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s", |
| 9452 | *sig_alg, |
| 9453 | mbedtls_ssl_sig_alg_to_str(*sig_alg))); |
| 9454 | if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) { |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9455 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9456 | } |
| 9457 | MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2); |
| 9458 | MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9459 | p += 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9460 | MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s", |
| 9461 | *sig_alg, |
| 9462 | mbedtls_ssl_sig_alg_to_str(*sig_alg))); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9463 | } |
| 9464 | |
| 9465 | /* Length of supported_signature_algorithms */ |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 9466 | supported_sig_alg_len = (size_t) (p - supported_sig_alg); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9467 | if (supported_sig_alg_len == 0) { |
| 9468 | MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined.")); |
| 9469 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9470 | } |
| 9471 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9472 | MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0); |
| 9473 | MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2); |
| 9474 | MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9475 | |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 9476 | *out_len = (size_t) (p - buf); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9477 | |
| 9478 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9479 | 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] | 9480 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | 0c354a2 | 2022-08-29 15:25:36 +0800 | [diff] [blame] | 9481 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9482 | return 0; |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9483 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 9484 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9485 | |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9486 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9487 | /* |
| 9488 | * mbedtls_ssl_parse_server_name_ext |
| 9489 | * |
| 9490 | * Structure of server_name extension: |
| 9491 | * |
| 9492 | * enum { |
| 9493 | * host_name(0), (255) |
| 9494 | * } NameType; |
| 9495 | * opaque HostName<1..2^16-1>; |
| 9496 | * |
| 9497 | * struct { |
| 9498 | * NameType name_type; |
| 9499 | * select (name_type) { |
| 9500 | * case host_name: HostName; |
| 9501 | * } name; |
| 9502 | * } ServerName; |
| 9503 | * struct { |
| 9504 | * ServerName server_name_list<1..2^16-1> |
| 9505 | * } ServerNameList; |
| 9506 | */ |
Ronald Cron | ce7d76e | 2022-07-08 18:56:49 +0200 | [diff] [blame] | 9507 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9508 | int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl, |
| 9509 | const unsigned char *buf, |
| 9510 | const unsigned char *end) |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9511 | { |
| 9512 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 9513 | const unsigned char *p = buf; |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9514 | size_t server_name_list_len, hostname_len; |
| 9515 | const unsigned char *server_name_list_end; |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9516 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9517 | MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension")); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9518 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9519 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2); |
| 9520 | server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9521 | p += 2; |
| 9522 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9523 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len); |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9524 | server_name_list_end = p + server_name_list_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9525 | while (p < server_name_list_end) { |
| 9526 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3); |
| 9527 | hostname_len = MBEDTLS_GET_UINT16_BE(p, 1); |
| 9528 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, |
| 9529 | hostname_len + 3); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9530 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9531 | if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) { |
XiaokangQian | 75fe8c7 | 2022-06-15 09:42:45 +0000 | [diff] [blame] | 9532 | /* sni_name is intended to be used only during the parsing of the |
| 9533 | * ClientHello message (it is reset to NULL before the end of |
| 9534 | * the message parsing). Thus it is ok to just point to the |
| 9535 | * reception buffer and not make a copy of it. |
| 9536 | */ |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 9537 | ssl->handshake->sni_name = p + 3; |
| 9538 | ssl->handshake->sni_name_len = hostname_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9539 | if (ssl->conf->f_sni == NULL) { |
| 9540 | return 0; |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9541 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9542 | ret = ssl->conf->f_sni(ssl->conf->p_sni, |
| 9543 | ssl, p + 3, hostname_len); |
| 9544 | if (ret != 0) { |
| 9545 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret); |
| 9546 | MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME, |
| 9547 | MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME); |
| 9548 | return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME; |
| 9549 | } |
| 9550 | return 0; |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9551 | } |
| 9552 | |
| 9553 | p += hostname_len + 3; |
| 9554 | } |
| 9555 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9556 | return 0; |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9557 | } |
| 9558 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 9559 | |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9560 | #if defined(MBEDTLS_SSL_ALPN) |
Ronald Cron | ce7d76e | 2022-07-08 18:56:49 +0200 | [diff] [blame] | 9561 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9562 | int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl, |
| 9563 | const unsigned char *buf, |
| 9564 | const unsigned char *end) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9565 | { |
| 9566 | const unsigned char *p = buf; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9567 | size_t protocol_name_list_len; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9568 | const unsigned char *protocol_name_list; |
| 9569 | const unsigned char *protocol_name_list_end; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9570 | size_t protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9571 | |
| 9572 | /* If ALPN not configured, just ignore the extension */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9573 | if (ssl->conf->alpn_list == NULL) { |
| 9574 | return 0; |
| 9575 | } |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9576 | |
| 9577 | /* |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9578 | * RFC7301, section 3.1 |
| 9579 | * opaque ProtocolName<1..2^8-1>; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9580 | * |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9581 | * struct { |
| 9582 | * ProtocolName protocol_name_list<2..2^16-1> |
| 9583 | * } ProtocolNameList; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9584 | */ |
| 9585 | |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9586 | /* |
XiaokangQian | 0b776e2 | 2022-06-24 09:04:59 +0000 | [diff] [blame] | 9587 | * protocol_name_list_len 2 bytes |
| 9588 | * protocol_name_len 1 bytes |
| 9589 | * protocol_name >=1 byte |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9590 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9591 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9592 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9593 | protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9594 | p += 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9595 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9596 | protocol_name_list = p; |
| 9597 | protocol_name_list_end = p + protocol_name_list_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9598 | |
| 9599 | /* Validate peer's list (lengths) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9600 | while (p < protocol_name_list_end) { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9601 | protocol_name_len = *p++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9602 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end, |
| 9603 | protocol_name_len); |
| 9604 | if (protocol_name_len == 0) { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9605 | MBEDTLS_SSL_PEND_FATAL_ALERT( |
| 9606 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9607 | MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER); |
| 9608 | return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9609 | } |
| 9610 | |
| 9611 | p += protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9612 | } |
| 9613 | |
| 9614 | /* Use our order of preference */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9615 | for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) { |
| 9616 | size_t const alpn_len = strlen(*alpn); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9617 | p = protocol_name_list; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9618 | while (p < protocol_name_list_end) { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9619 | protocol_name_len = *p++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9620 | if (protocol_name_len == alpn_len && |
| 9621 | memcmp(p, *alpn, alpn_len) == 0) { |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9622 | ssl->alpn_chosen = *alpn; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9623 | return 0; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9624 | } |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9625 | |
| 9626 | p += protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9627 | } |
| 9628 | } |
| 9629 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9630 | /* If we get here, no match was found */ |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9631 | MBEDTLS_SSL_PEND_FATAL_ALERT( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9632 | MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL, |
| 9633 | MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL); |
| 9634 | return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9635 | } |
| 9636 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9637 | int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl, |
| 9638 | unsigned char *buf, |
| 9639 | unsigned char *end, |
| 9640 | size_t *out_len) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9641 | { |
| 9642 | unsigned char *p = buf; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9643 | size_t protocol_name_len; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9644 | *out_len = 0; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9645 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9646 | if (ssl->alpn_chosen == NULL) { |
| 9647 | return 0; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9648 | } |
| 9649 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9650 | protocol_name_len = strlen(ssl->alpn_chosen); |
| 9651 | MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9652 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9653 | MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension")); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9654 | /* |
| 9655 | * 0 . 1 ext identifier |
| 9656 | * 2 . 3 ext length |
| 9657 | * 4 . 5 protocol list length |
| 9658 | * 6 . 6 protocol name length |
| 9659 | * 7 . 7+n protocol name |
| 9660 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9661 | MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9662 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9663 | *out_len = 7 + protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9664 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9665 | MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2); |
| 9666 | MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4); |
XiaokangQian | 0b776e2 | 2022-06-24 09:04:59 +0000 | [diff] [blame] | 9667 | /* Note: the length of the chosen protocol has been checked to be less |
| 9668 | * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`. |
| 9669 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9670 | p[6] = MBEDTLS_BYTE_0(protocol_name_len); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9671 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9672 | memcpy(p + 7, ssl->alpn_chosen, protocol_name_len); |
Jerry Yu | b95dd36 | 2022-11-08 21:19:34 +0800 | [diff] [blame] | 9673 | |
| 9674 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9675 | 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] | 9676 | #endif |
| 9677 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9678 | return 0; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9679 | } |
| 9680 | #endif /* MBEDTLS_SSL_ALPN */ |
| 9681 | |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9682 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
Xiaokang Qian | 0340929 | 2022-10-12 02:49:52 +0000 | [diff] [blame] | 9683 | defined(MBEDTLS_SSL_SESSION_TICKETS) && \ |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 9684 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \ |
Xiaokang Qian | ed3afcd | 2022-10-12 08:31:11 +0000 | [diff] [blame] | 9685 | defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9686 | int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session, |
| 9687 | const char *hostname) |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9688 | { |
| 9689 | /* Initialize to suppress unnecessary compiler warning */ |
| 9690 | size_t hostname_len = 0; |
| 9691 | |
| 9692 | /* Check if new hostname is valid before |
| 9693 | * making any change to current one */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9694 | if (hostname != NULL) { |
| 9695 | hostname_len = strlen(hostname); |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9696 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9697 | if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) { |
| 9698 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9699 | } |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9700 | } |
| 9701 | |
| 9702 | /* Now it's clear that we will overwrite the old hostname, |
| 9703 | * so we can free it safely */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9704 | if (session->hostname != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 9705 | mbedtls_zeroize_and_free(session->hostname, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9706 | strlen(session->hostname)); |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9707 | } |
| 9708 | |
| 9709 | /* Passing NULL as hostname shall clear the old one */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9710 | if (hostname == NULL) { |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9711 | session->hostname = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9712 | } else { |
| 9713 | session->hostname = mbedtls_calloc(1, hostname_len + 1); |
| 9714 | if (session->hostname == NULL) { |
| 9715 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 9716 | } |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9717 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9718 | memcpy(session->hostname, hostname, hostname_len); |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9719 | } |
| 9720 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9721 | return 0; |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9722 | } |
Xiaokang Qian | 0340929 | 2022-10-12 02:49:52 +0000 | [diff] [blame] | 9723 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 && |
| 9724 | MBEDTLS_SSL_SESSION_TICKETS && |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 9725 | MBEDTLS_SSL_SERVER_NAME_INDICATION && |
Xiaokang Qian | ed3afcd | 2022-10-12 08:31:11 +0000 | [diff] [blame] | 9726 | MBEDTLS_SSL_CLI_C */ |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9727 | |
Waleed Elmelegy | 883f77c | 2024-03-06 19:09:41 +0000 | [diff] [blame] | 9728 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_EARLY_DATA) && \ |
| 9729 | defined(MBEDTLS_SSL_ALPN) |
Waleed Elmelegy | 7dfba34 | 2024-03-12 16:22:39 +0000 | [diff] [blame] | 9730 | int mbedtls_ssl_session_set_ticket_alpn(mbedtls_ssl_session *session, |
| 9731 | const char *alpn) |
Waleed Elmelegy | 883f77c | 2024-03-06 19:09:41 +0000 | [diff] [blame] | 9732 | { |
| 9733 | size_t alpn_len = 0; |
| 9734 | |
| 9735 | if (alpn != NULL) { |
| 9736 | alpn_len = strlen(alpn); |
| 9737 | |
| 9738 | if (alpn_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) { |
| 9739 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9740 | } |
| 9741 | } |
| 9742 | |
| 9743 | if (session->ticket_alpn != NULL) { |
| 9744 | mbedtls_zeroize_and_free(session->ticket_alpn, |
| 9745 | strlen(session->ticket_alpn)); |
Waleed Elmelegy | 7dfba34 | 2024-03-12 16:22:39 +0000 | [diff] [blame] | 9746 | session->ticket_alpn = NULL; |
Waleed Elmelegy | 883f77c | 2024-03-06 19:09:41 +0000 | [diff] [blame] | 9747 | } |
| 9748 | |
Waleed Elmelegy | 7dfba34 | 2024-03-12 16:22:39 +0000 | [diff] [blame] | 9749 | if (alpn != NULL) { |
| 9750 | session->ticket_alpn = mbedtls_calloc(alpn_len + 1, 1); |
Waleed Elmelegy | 883f77c | 2024-03-06 19:09:41 +0000 | [diff] [blame] | 9751 | if (session->ticket_alpn == NULL) { |
| 9752 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 9753 | } |
Waleed Elmelegy | 7dfba34 | 2024-03-12 16:22:39 +0000 | [diff] [blame] | 9754 | memcpy(session->ticket_alpn, alpn, alpn_len); |
Waleed Elmelegy | 883f77c | 2024-03-06 19:09:41 +0000 | [diff] [blame] | 9755 | } |
| 9756 | |
| 9757 | return 0; |
| 9758 | } |
| 9759 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | c32a4a2 | 2024-08-20 12:14:43 +0200 | [diff] [blame] | 9760 | |
| 9761 | /* |
| 9762 | * The following functions are used by 1.2 and 1.3, client and server. |
| 9763 | */ |
| 9764 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
| 9765 | int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert, |
| 9766 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
| 9767 | int recv_endpoint, |
| 9768 | mbedtls_ssl_protocol_version tls_version, |
| 9769 | uint32_t *flags) |
| 9770 | { |
| 9771 | int ret = 0; |
| 9772 | unsigned int usage = 0; |
| 9773 | const char *ext_oid; |
| 9774 | size_t ext_len; |
| 9775 | |
| 9776 | /* |
| 9777 | * keyUsage |
| 9778 | */ |
| 9779 | |
| 9780 | /* Note: don't guard this with MBEDTLS_SSL_CLI_C because the server wants |
| 9781 | * to check what a compliant client will think while choosing which cert |
| 9782 | * to send to the client. */ |
| 9783 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 9784 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2 && |
| 9785 | recv_endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 9786 | /* TLS 1.2 server part of the key exchange */ |
| 9787 | switch (ciphersuite->key_exchange) { |
| 9788 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 9789 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
| 9790 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
| 9791 | break; |
| 9792 | |
| 9793 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 9794 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 9795 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 9796 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
| 9797 | break; |
| 9798 | |
| 9799 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 9800 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
| 9801 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
| 9802 | break; |
| 9803 | |
| 9804 | /* Don't use default: we want warnings when adding new values */ |
| 9805 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 9806 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 9807 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 9808 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
| 9809 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
| 9810 | usage = 0; |
| 9811 | } |
| 9812 | } else |
| 9813 | #endif |
| 9814 | { |
| 9815 | /* This is either TLS 1.3 authentication, which always uses signatures, |
| 9816 | * or 1.2 client auth: rsa_sign and mbedtls_ecdsa_sign are the only |
| 9817 | * options we implement, both using signatures. */ |
| 9818 | (void) tls_version; |
| 9819 | (void) ciphersuite; |
| 9820 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
| 9821 | } |
| 9822 | |
| 9823 | if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) { |
| 9824 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
| 9825 | ret = -1; |
| 9826 | } |
| 9827 | |
| 9828 | /* |
| 9829 | * extKeyUsage |
| 9830 | */ |
| 9831 | |
| 9832 | if (recv_endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 9833 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
| 9834 | ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH); |
| 9835 | } else { |
| 9836 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
| 9837 | ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH); |
| 9838 | } |
| 9839 | |
| 9840 | if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) { |
| 9841 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
| 9842 | ret = -1; |
| 9843 | } |
| 9844 | |
| 9845 | return ret; |
| 9846 | } |
| 9847 | |
Gilles Peskine | e61852e | 2025-02-12 23:28:48 +0100 | [diff] [blame] | 9848 | static int get_hostname_for_verification(mbedtls_ssl_context *ssl, |
| 9849 | const char **hostname) |
| 9850 | { |
| 9851 | if (!mbedtls_ssl_has_set_hostname_been_called(ssl)) { |
| 9852 | MBEDTLS_SSL_DEBUG_MSG(1, ("Certificate verification without having set hostname")); |
Gilles Peskine | 2c33c75 | 2025-02-13 14:39:02 +0100 | [diff] [blame] | 9853 | #if !defined(MBEDTLS_SSL_CLI_ALLOW_WEAK_CERTIFICATE_VERIFICATION_WITHOUT_HOSTNAME) |
| 9854 | if (mbedtls_ssl_conf_get_endpoint(ssl->conf) == MBEDTLS_SSL_IS_CLIENT && |
| 9855 | ssl->conf->authmode == MBEDTLS_SSL_VERIFY_REQUIRED) { |
| 9856 | return MBEDTLS_ERR_SSL_CERTIFICATE_VERIFICATION_WITHOUT_HOSTNAME; |
| 9857 | } |
| 9858 | #endif |
Gilles Peskine | e61852e | 2025-02-12 23:28:48 +0100 | [diff] [blame] | 9859 | } |
| 9860 | |
| 9861 | *hostname = mbedtls_ssl_get_hostname_pointer(ssl); |
| 9862 | if (*hostname == NULL) { |
| 9863 | MBEDTLS_SSL_DEBUG_MSG(2, ("Certificate verification without CN verification")); |
| 9864 | } |
| 9865 | |
| 9866 | return 0; |
| 9867 | } |
| 9868 | |
Manuel Pégourié-Gonnard | c32a4a2 | 2024-08-20 12:14:43 +0200 | [diff] [blame] | 9869 | int mbedtls_ssl_verify_certificate(mbedtls_ssl_context *ssl, |
| 9870 | int authmode, |
| 9871 | mbedtls_x509_crt *chain, |
| 9872 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info, |
| 9873 | void *rs_ctx) |
| 9874 | { |
| 9875 | if (authmode == MBEDTLS_SSL_VERIFY_NONE) { |
| 9876 | return 0; |
| 9877 | } |
| 9878 | |
| 9879 | /* |
| 9880 | * Primary check: use the appropriate X.509 verification function |
| 9881 | */ |
| 9882 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); |
| 9883 | void *p_vrfy; |
| 9884 | if (ssl->f_vrfy != NULL) { |
| 9885 | MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback")); |
| 9886 | f_vrfy = ssl->f_vrfy; |
| 9887 | p_vrfy = ssl->p_vrfy; |
| 9888 | } else { |
| 9889 | MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback")); |
| 9890 | f_vrfy = ssl->conf->f_vrfy; |
| 9891 | p_vrfy = ssl->conf->p_vrfy; |
| 9892 | } |
| 9893 | |
Gilles Peskine | e61852e | 2025-02-12 23:28:48 +0100 | [diff] [blame] | 9894 | const char *hostname = ""; |
| 9895 | int ret = get_hostname_for_verification(ssl, &hostname); |
| 9896 | if (ret != 0) { |
| 9897 | MBEDTLS_SSL_DEBUG_RET(1, "get_hostname_for_verification", ret); |
| 9898 | return ret; |
| 9899 | } |
| 9900 | |
Manuel Pégourié-Gonnard | c32a4a2 | 2024-08-20 12:14:43 +0200 | [diff] [blame] | 9901 | int have_ca_chain_or_callback = 0; |
| 9902 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 9903 | if (ssl->conf->f_ca_cb != NULL) { |
| 9904 | ((void) rs_ctx); |
| 9905 | have_ca_chain_or_callback = 1; |
| 9906 | |
| 9907 | MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification")); |
| 9908 | ret = mbedtls_x509_crt_verify_with_ca_cb( |
| 9909 | chain, |
| 9910 | ssl->conf->f_ca_cb, |
| 9911 | ssl->conf->p_ca_cb, |
| 9912 | ssl->conf->cert_profile, |
Gilles Peskine | e61852e | 2025-02-12 23:28:48 +0100 | [diff] [blame] | 9913 | hostname, |
Manuel Pégourié-Gonnard | c32a4a2 | 2024-08-20 12:14:43 +0200 | [diff] [blame] | 9914 | &ssl->session_negotiate->verify_result, |
| 9915 | f_vrfy, p_vrfy); |
| 9916 | } else |
| 9917 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 9918 | { |
| 9919 | mbedtls_x509_crt *ca_chain; |
| 9920 | mbedtls_x509_crl *ca_crl; |
| 9921 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 9922 | if (ssl->handshake->sni_ca_chain != NULL) { |
| 9923 | ca_chain = ssl->handshake->sni_ca_chain; |
| 9924 | ca_crl = ssl->handshake->sni_ca_crl; |
| 9925 | } else |
| 9926 | #endif |
| 9927 | { |
| 9928 | ca_chain = ssl->conf->ca_chain; |
| 9929 | ca_crl = ssl->conf->ca_crl; |
| 9930 | } |
| 9931 | |
| 9932 | if (ca_chain != NULL) { |
| 9933 | have_ca_chain_or_callback = 1; |
| 9934 | } |
| 9935 | |
| 9936 | ret = mbedtls_x509_crt_verify_restartable( |
| 9937 | chain, |
| 9938 | ca_chain, ca_crl, |
| 9939 | ssl->conf->cert_profile, |
Gilles Peskine | e61852e | 2025-02-12 23:28:48 +0100 | [diff] [blame] | 9940 | hostname, |
Manuel Pégourié-Gonnard | c32a4a2 | 2024-08-20 12:14:43 +0200 | [diff] [blame] | 9941 | &ssl->session_negotiate->verify_result, |
| 9942 | f_vrfy, p_vrfy, rs_ctx); |
| 9943 | } |
| 9944 | |
| 9945 | if (ret != 0) { |
| 9946 | MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret); |
| 9947 | } |
| 9948 | |
| 9949 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 9950 | if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) { |
| 9951 | return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; |
| 9952 | } |
| 9953 | #endif |
| 9954 | |
| 9955 | /* |
| 9956 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 9957 | */ |
| 9958 | |
| 9959 | /* With TLS 1.2 and ECC certs, check that the curve used by the |
| 9960 | * certificate is on our list of acceptable curves. |
| 9961 | * |
| 9962 | * With TLS 1.3 this is not needed because the curve is part of the |
| 9963 | * signature algorithm (eg ecdsa_secp256r1_sha256) which is checked when |
| 9964 | * we validate the signature made with the key associated to this cert. |
| 9965 | */ |
| 9966 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 9967 | defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
| 9968 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 && |
| 9969 | mbedtls_pk_can_do(&chain->pk, MBEDTLS_PK_ECKEY)) { |
| 9970 | if (mbedtls_ssl_check_curve(ssl, mbedtls_pk_get_ec_group_id(&chain->pk)) != 0) { |
| 9971 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)")); |
| 9972 | ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY; |
| 9973 | if (ret == 0) { |
| 9974 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 9975 | } |
| 9976 | } |
| 9977 | } |
| 9978 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_PK_HAVE_ECC_KEYS */ |
| 9979 | |
| 9980 | /* Check X.509 usage extensions (keyUsage, extKeyUsage) */ |
| 9981 | if (mbedtls_ssl_check_cert_usage(chain, |
| 9982 | ciphersuite_info, |
| 9983 | ssl->conf->endpoint, |
| 9984 | ssl->tls_version, |
| 9985 | &ssl->session_negotiate->verify_result) != 0) { |
| 9986 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)")); |
| 9987 | if (ret == 0) { |
| 9988 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 9989 | } |
| 9990 | } |
| 9991 | |
| 9992 | /* With authmode optional, we want to keep going if the certificate was |
| 9993 | * unacceptable, but still fail on other errors (out of memory etc), |
| 9994 | * including fatal errors from the f_vrfy callback. |
| 9995 | * |
| 9996 | * The only acceptable errors are: |
| 9997 | * - MBEDTLS_ERR_X509_CERT_VERIFY_FAILED: cert rejected by primary check; |
| 9998 | * - MBEDTLS_ERR_SSL_BAD_CERTIFICATE: cert rejected by secondary checks. |
| 9999 | * Anything else is a fatal error. */ |
| 10000 | if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
| 10001 | (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
| 10002 | ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) { |
| 10003 | ret = 0; |
| 10004 | } |
| 10005 | |
| 10006 | /* Return a specific error as this is a user error: inconsistent |
| 10007 | * configuration - can't verify without trust anchors. */ |
| 10008 | if (have_ca_chain_or_callback == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) { |
| 10009 | MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain")); |
| 10010 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
| 10011 | } |
| 10012 | |
| 10013 | if (ret != 0) { |
| 10014 | uint8_t alert; |
| 10015 | |
| 10016 | /* The certificate may have been rejected for several reasons. |
| 10017 | Pick one and send the corresponding alert. Which alert to send |
| 10018 | may be a subject of debate in some cases. */ |
| 10019 | if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) { |
| 10020 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
| 10021 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) { |
| 10022 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 10023 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) { |
| 10024 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 10025 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) { |
| 10026 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 10027 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) { |
| 10028 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 10029 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) { |
| 10030 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 10031 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) { |
| 10032 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
| 10033 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) { |
| 10034 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
| 10035 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) { |
| 10036 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
| 10037 | } else { |
| 10038 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
| 10039 | } |
| 10040 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 10041 | alert); |
| 10042 | } |
| 10043 | |
| 10044 | #if defined(MBEDTLS_DEBUG_C) |
| 10045 | if (ssl->session_negotiate->verify_result != 0) { |
| 10046 | MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x", |
| 10047 | (unsigned int) ssl->session_negotiate->verify_result)); |
| 10048 | } else { |
| 10049 | MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear")); |
| 10050 | } |
| 10051 | #endif /* MBEDTLS_DEBUG_C */ |
| 10052 | |
| 10053 | return ret; |
| 10054 | } |
| 10055 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
| 10056 | |
Max Fillinger | 951b886 | 2024-10-25 00:52:24 +0200 | [diff] [blame] | 10057 | #if defined(MBEDTLS_SSL_KEYING_MATERIAL_EXPORT) |
Max Fillinger | 7b52328 | 2024-10-23 18:35:09 +0200 | [diff] [blame] | 10058 | |
Max Fillinger | 0118293 | 2024-09-21 11:06:28 +0200 | [diff] [blame] | 10059 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Max Fillinger | 44042f0 | 2024-07-22 14:43:56 +0200 | [diff] [blame] | 10060 | static int mbedtls_ssl_tls12_export_keying_material(const mbedtls_ssl_context *ssl, |
| 10061 | const mbedtls_md_type_t hash_alg, |
Max Fillinger | 9359f4d | 2024-09-21 10:48:57 +0200 | [diff] [blame] | 10062 | uint8_t *out, |
| 10063 | const size_t key_len, |
| 10064 | const char *label, |
| 10065 | const size_t label_len, |
| 10066 | const unsigned char *context, |
| 10067 | const size_t context_len, |
Max Fillinger | 44042f0 | 2024-07-22 14:43:56 +0200 | [diff] [blame] | 10068 | const int use_context) |
| 10069 | { |
| 10070 | int ret = 0; |
Max Fillinger | 44042f0 | 2024-07-22 14:43:56 +0200 | [diff] [blame] | 10071 | unsigned char *prf_input = NULL; |
Max Fillinger | 44042f0 | 2024-07-22 14:43:56 +0200 | [diff] [blame] | 10072 | |
Max Fillinger | 44042f0 | 2024-07-22 14:43:56 +0200 | [diff] [blame] | 10073 | /* The input to the PRF is client_random, then server_random. |
| 10074 | * If a context is provided, this is then followed by the context length |
| 10075 | * as a 16-bit big-endian integer, and then the context itself. */ |
Max Fillinger | 6c02ea8 | 2024-10-23 16:32:54 +0200 | [diff] [blame] | 10076 | const size_t randbytes_len = MBEDTLS_CLIENT_HELLO_RANDOM_LEN + MBEDTLS_SERVER_HELLO_RANDOM_LEN; |
| 10077 | size_t prf_input_len = randbytes_len; |
Max Fillinger | 44042f0 | 2024-07-22 14:43:56 +0200 | [diff] [blame] | 10078 | if (use_context) { |
Max Fillinger | 6c02ea8 | 2024-10-23 16:32:54 +0200 | [diff] [blame] | 10079 | if (context_len > UINT16_MAX) { |
| 10080 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 10081 | } |
| 10082 | |
| 10083 | /* This does not overflow a 32-bit size_t because the current value of |
| 10084 | * prf_input_len is 64 (length of client_random + server_random) and |
| 10085 | * context_len fits into two bytes (checked above). */ |
| 10086 | prf_input_len += sizeof(uint16_t) + context_len; |
Max Fillinger | 44042f0 | 2024-07-22 14:43:56 +0200 | [diff] [blame] | 10087 | } |
Max Fillinger | 6c02ea8 | 2024-10-23 16:32:54 +0200 | [diff] [blame] | 10088 | |
| 10089 | prf_input = mbedtls_calloc(prf_input_len, sizeof(unsigned char)); |
| 10090 | if (prf_input == NULL) { |
| 10091 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 10092 | } |
| 10093 | |
| 10094 | memcpy(prf_input, |
| 10095 | ssl->transform->randbytes + MBEDTLS_SERVER_HELLO_RANDOM_LEN, |
| 10096 | MBEDTLS_CLIENT_HELLO_RANDOM_LEN); |
| 10097 | memcpy(prf_input + MBEDTLS_CLIENT_HELLO_RANDOM_LEN, |
| 10098 | ssl->transform->randbytes, |
| 10099 | MBEDTLS_SERVER_HELLO_RANDOM_LEN); |
| 10100 | if (use_context) { |
| 10101 | MBEDTLS_PUT_UINT16_BE(context_len, prf_input, randbytes_len); |
| 10102 | memcpy(prf_input + randbytes_len + sizeof(uint16_t), context, context_len); |
| 10103 | } |
| 10104 | ret = tls_prf_generic(hash_alg, ssl->session->master, sizeof(ssl->session->master), |
Max Fillinger | 76077e2 | 2024-10-23 15:47:23 +0200 | [diff] [blame] | 10105 | label, label_len, |
Max Fillinger | 44042f0 | 2024-07-22 14:43:56 +0200 | [diff] [blame] | 10106 | prf_input, prf_input_len, |
| 10107 | out, key_len); |
Max Fillinger | 44042f0 | 2024-07-22 14:43:56 +0200 | [diff] [blame] | 10108 | mbedtls_free(prf_input); |
Max Fillinger | 44042f0 | 2024-07-22 14:43:56 +0200 | [diff] [blame] | 10109 | return ret; |
| 10110 | } |
Max Fillinger | 7b52328 | 2024-10-23 18:35:09 +0200 | [diff] [blame] | 10111 | #endif /* defined(MBEDTLS_SSL_PROTO_TLS1_2) */ |
Max Fillinger | 44042f0 | 2024-07-22 14:43:56 +0200 | [diff] [blame] | 10112 | |
Max Fillinger | 0118293 | 2024-09-21 11:06:28 +0200 | [diff] [blame] | 10113 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Max Fillinger | 44042f0 | 2024-07-22 14:43:56 +0200 | [diff] [blame] | 10114 | static int mbedtls_ssl_tls13_export_keying_material(mbedtls_ssl_context *ssl, |
| 10115 | const mbedtls_md_type_t hash_alg, |
Max Fillinger | 9359f4d | 2024-09-21 10:48:57 +0200 | [diff] [blame] | 10116 | uint8_t *out, |
| 10117 | const size_t key_len, |
| 10118 | const char *label, |
| 10119 | const size_t label_len, |
| 10120 | const unsigned char *context, |
| 10121 | const size_t context_len) |
Max Fillinger | 44042f0 | 2024-07-22 14:43:56 +0200 | [diff] [blame] | 10122 | { |
| 10123 | const psa_algorithm_t psa_hash_alg = mbedtls_md_psa_alg_from_type(hash_alg); |
| 10124 | const size_t hash_len = PSA_HASH_LENGTH(hash_alg); |
| 10125 | const unsigned char *secret = ssl->session->app_secrets.exporter_master_secret; |
| 10126 | |
Max Fillinger | 76bb753 | 2024-11-21 12:33:46 +0100 | [diff] [blame] | 10127 | /* The length of the label must be at most 249 bytes to fit into the HkdfLabel |
Max Fillinger | 8ee2141 | 2024-11-01 16:05:34 +0100 | [diff] [blame] | 10128 | * struct as defined in RFC 8446, Section 7.1. |
Max Fillinger | e95edbf | 2024-10-29 19:18:54 +0100 | [diff] [blame] | 10129 | * |
Max Fillinger | 8ee2141 | 2024-11-01 16:05:34 +0100 | [diff] [blame] | 10130 | * The length of the context is unlimited even though the context field in the |
Max Fillinger | 76bb753 | 2024-11-21 12:33:46 +0100 | [diff] [blame] | 10131 | * struct can only hold up to 255 bytes. This is because we place a *hash* of |
Max Fillinger | 8ee2141 | 2024-11-01 16:05:34 +0100 | [diff] [blame] | 10132 | * the context in the field. */ |
Max Fillinger | 76bb753 | 2024-11-21 12:33:46 +0100 | [diff] [blame] | 10133 | if (label_len > 249) { |
Max Fillinger | 44042f0 | 2024-07-22 14:43:56 +0200 | [diff] [blame] | 10134 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 10135 | } |
| 10136 | |
| 10137 | return mbedtls_ssl_tls13_exporter(psa_hash_alg, secret, hash_len, |
Max Fillinger | 9359f4d | 2024-09-21 10:48:57 +0200 | [diff] [blame] | 10138 | (const unsigned char *) label, label_len, |
Max Fillinger | 44042f0 | 2024-07-22 14:43:56 +0200 | [diff] [blame] | 10139 | context, context_len, out, key_len); |
| 10140 | } |
Max Fillinger | 7b52328 | 2024-10-23 18:35:09 +0200 | [diff] [blame] | 10141 | #endif /* defined(MBEDTLS_SSL_PROTO_TLS1_3) */ |
Max Fillinger | 44042f0 | 2024-07-22 14:43:56 +0200 | [diff] [blame] | 10142 | |
| 10143 | int mbedtls_ssl_export_keying_material(mbedtls_ssl_context *ssl, |
Max Fillinger | 3be83a7 | 2024-08-14 16:44:50 +0200 | [diff] [blame] | 10144 | uint8_t *out, const size_t key_len, |
| 10145 | const char *label, const size_t label_len, |
| 10146 | const unsigned char *context, const size_t context_len, |
| 10147 | const int use_context) |
Max Fillinger | 44042f0 | 2024-07-22 14:43:56 +0200 | [diff] [blame] | 10148 | { |
| 10149 | if (!mbedtls_ssl_is_handshake_over(ssl)) { |
Max Fillinger | 97a2879 | 2024-11-18 18:22:51 +0100 | [diff] [blame] | 10150 | /* TODO: Change this to a more appropriate error code when one is available. */ |
Max Fillinger | 44042f0 | 2024-07-22 14:43:56 +0200 | [diff] [blame] | 10151 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 10152 | } |
| 10153 | |
Max Fillinger | 8ee2141 | 2024-11-01 16:05:34 +0100 | [diff] [blame] | 10154 | if (key_len > MBEDTLS_SSL_EXPORT_MAX_KEY_LEN) { |
| 10155 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 10156 | } |
| 10157 | |
Max Fillinger | 44042f0 | 2024-07-22 14:43:56 +0200 | [diff] [blame] | 10158 | int ciphersuite_id = mbedtls_ssl_get_ciphersuite_id_from_ssl(ssl); |
| 10159 | const mbedtls_ssl_ciphersuite_t *ciphersuite = mbedtls_ssl_ciphersuite_from_id(ciphersuite_id); |
| 10160 | const mbedtls_md_type_t hash_alg = ciphersuite->mac; |
| 10161 | |
| 10162 | switch (mbedtls_ssl_get_version_number(ssl)) { |
Max Fillinger | 0118293 | 2024-09-21 11:06:28 +0200 | [diff] [blame] | 10163 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Max Fillinger | 44042f0 | 2024-07-22 14:43:56 +0200 | [diff] [blame] | 10164 | case MBEDTLS_SSL_VERSION_TLS1_2: |
| 10165 | return mbedtls_ssl_tls12_export_keying_material(ssl, hash_alg, out, key_len, |
| 10166 | label, label_len, |
| 10167 | context, context_len, use_context); |
Max Fillinger | 0118293 | 2024-09-21 11:06:28 +0200 | [diff] [blame] | 10168 | #endif |
| 10169 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Max Fillinger | 44042f0 | 2024-07-22 14:43:56 +0200 | [diff] [blame] | 10170 | case MBEDTLS_SSL_VERSION_TLS1_3: |
Max Fillinger | 9359f4d | 2024-09-21 10:48:57 +0200 | [diff] [blame] | 10171 | return mbedtls_ssl_tls13_export_keying_material(ssl, |
| 10172 | hash_alg, |
| 10173 | out, |
| 10174 | key_len, |
| 10175 | label, |
| 10176 | label_len, |
Max Fillinger | 44042f0 | 2024-07-22 14:43:56 +0200 | [diff] [blame] | 10177 | use_context ? context : NULL, |
| 10178 | use_context ? context_len : 0); |
Max Fillinger | 0118293 | 2024-09-21 11:06:28 +0200 | [diff] [blame] | 10179 | #endif |
Max Fillinger | 44042f0 | 2024-07-22 14:43:56 +0200 | [diff] [blame] | 10180 | default: |
| 10181 | return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION; |
| 10182 | } |
| 10183 | } |
| 10184 | |
Max Fillinger | 951b886 | 2024-10-25 00:52:24 +0200 | [diff] [blame] | 10185 | #endif /* defined(MBEDTLS_SSL_KEYING_MATERIAL_EXPORT) */ |
Max Fillinger | 7b52328 | 2024-10-23 18:35:09 +0200 | [diff] [blame] | 10186 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10187 | #endif /* MBEDTLS_SSL_TLS_C */ |