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 | |
Harry Ramsey | 0f6bc41 | 2024-10-04 10:36:54 +0100 | [diff] [blame] | 12 | #include "ssl_misc.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" |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 21 | |
Valerio Setti | b4f5076 | 2024-01-17 10:24:52 +0100 | [diff] [blame] | 22 | #include "debug_internal.h" |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 23 | #include "mbedtls/error.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 24 | #include "mbedtls/platform_util.h" |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 25 | #include "mbedtls/version.h" |
Gabor Mezei | 765862c | 2021-10-19 12:22:25 +0200 | [diff] [blame] | 26 | #include "mbedtls/constant_time.h" |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 27 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 28 | #include <string.h> |
| 29 | |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 30 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Valerio Setti | 384fbde | 2024-01-02 13:26:40 +0100 | [diff] [blame] | 31 | #include "mbedtls/psa_util.h" |
Manuel Pégourié-Gonnard | 02b10d8 | 2023-03-28 12:33:20 +0200 | [diff] [blame] | 32 | #include "md_psa.h" |
Manuel Pégourié-Gonnard | 2be8c63 | 2023-06-07 13:07:21 +0200 | [diff] [blame] | 33 | #include "psa_util_internal.h" |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 34 | #include "psa/crypto.h" |
| 35 | #endif |
| 36 | |
Janos Follath | 23bdca0 | 2016-10-07 14:47:14 +0100 | [diff] [blame] | 37 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 38 | #include "mbedtls/oid.h" |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 39 | #endif |
| 40 | |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 41 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 0064484 | 2023-05-30 05:45:00 -0400 | [diff] [blame] | 42 | /* Define local translating functions to save code size by not using too many |
| 43 | * arguments in each translating place. */ |
| 44 | static int local_err_translation(psa_status_t status) |
| 45 | { |
| 46 | return psa_status_to_mbedtls(status, psa_to_ssl_errors, |
Andrzej Kurek | 1e4a030 | 2023-05-30 09:45:17 -0400 | [diff] [blame] | 47 | ARRAY_LENGTH(psa_to_ssl_errors), |
Andrzej Kurek | 0064484 | 2023-05-30 05:45:00 -0400 | [diff] [blame] | 48 | psa_generic_status_to_mbedtls); |
| 49 | } |
| 50 | #define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status) |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 51 | #endif |
| 52 | |
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 53 | #if defined(MBEDTLS_TEST_HOOKS) |
| 54 | static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args; |
| 55 | |
| 56 | void mbedtls_ssl_set_chk_buf_ptr_fail_args( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 57 | const uint8_t *cur, const uint8_t *end, size_t need) |
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 58 | { |
| 59 | chk_buf_ptr_fail_args.cur = cur; |
| 60 | chk_buf_ptr_fail_args.end = end; |
| 61 | chk_buf_ptr_fail_args.need = need; |
| 62 | } |
| 63 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 64 | void mbedtls_ssl_reset_chk_buf_ptr_fail_args(void) |
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 65 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 66 | memset(&chk_buf_ptr_fail_args, 0, sizeof(chk_buf_ptr_fail_args)); |
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 67 | } |
| 68 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 69 | int mbedtls_ssl_cmp_chk_buf_ptr_fail_args(mbedtls_ssl_chk_buf_ptr_args *args) |
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 70 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 71 | return (chk_buf_ptr_fail_args.cur != args->cur) || |
| 72 | (chk_buf_ptr_fail_args.end != args->end) || |
| 73 | (chk_buf_ptr_fail_args.need != args->need); |
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 74 | } |
| 75 | #endif /* MBEDTLS_TEST_HOOKS */ |
| 76 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 77 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 78 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 79 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 80 | /* Top-level Connection ID API */ |
| 81 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 82 | int mbedtls_ssl_conf_cid(mbedtls_ssl_config *conf, |
| 83 | size_t len, |
| 84 | int ignore_other_cid) |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 85 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 86 | if (len > MBEDTLS_SSL_CID_IN_LEN_MAX) { |
| 87 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 88 | } |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 89 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 90 | if (ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL && |
| 91 | ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) { |
| 92 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | 611ac77 | 2019-05-14 11:45:26 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | conf->ignore_unexpected_cid = ignore_other_cid; |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 96 | conf->cid_len = len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 97 | return 0; |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 98 | } |
| 99 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 100 | int mbedtls_ssl_set_cid(mbedtls_ssl_context *ssl, |
| 101 | int enable, |
| 102 | unsigned char const *own_cid, |
| 103 | size_t own_cid_len) |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 104 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 105 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 106 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 107 | } |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 108 | |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 109 | ssl->negotiate_cid = enable; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 110 | if (enable == MBEDTLS_SSL_CID_DISABLED) { |
| 111 | MBEDTLS_SSL_DEBUG_MSG(3, ("Disable use of CID extension.")); |
| 112 | return 0; |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 113 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 114 | MBEDTLS_SSL_DEBUG_MSG(3, ("Enable use of CID extension.")); |
| 115 | MBEDTLS_SSL_DEBUG_BUF(3, "Own CID", own_cid, own_cid_len); |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 116 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 117 | if (own_cid_len != ssl->conf->cid_len) { |
| 118 | MBEDTLS_SSL_DEBUG_MSG(3, ("CID length %u does not match CID length %u in config", |
| 119 | (unsigned) own_cid_len, |
| 120 | (unsigned) ssl->conf->cid_len)); |
| 121 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 122 | } |
| 123 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 124 | memcpy(ssl->own_cid, own_cid, own_cid_len); |
Hanno Becker | b7ee0cf | 2019-04-30 14:07:31 +0100 | [diff] [blame] | 125 | /* Truncation is not an issue here because |
| 126 | * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */ |
| 127 | ssl->own_cid_len = (uint8_t) own_cid_len; |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 128 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 129 | return 0; |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 132 | int mbedtls_ssl_get_own_cid(mbedtls_ssl_context *ssl, |
| 133 | int *enabled, |
Sam Berry | 3504c88 | 2024-06-11 14:34:17 +0100 | [diff] [blame] | 134 | unsigned char own_cid[MBEDTLS_SSL_CID_IN_LEN_MAX], |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 135 | size_t *own_cid_len) |
Paul Elliott | 0113cf1 | 2022-03-11 20:26:47 +0000 | [diff] [blame] | 136 | { |
| 137 | *enabled = MBEDTLS_SSL_CID_DISABLED; |
| 138 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 139 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 140 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 141 | } |
Paul Elliott | 0113cf1 | 2022-03-11 20:26:47 +0000 | [diff] [blame] | 142 | |
| 143 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is |
| 144 | * zero as this is indistinguishable from not requesting to use |
| 145 | * the CID extension. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 146 | if (ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) { |
| 147 | return 0; |
| 148 | } |
Paul Elliott | 0113cf1 | 2022-03-11 20:26:47 +0000 | [diff] [blame] | 149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 150 | if (own_cid_len != NULL) { |
Paul Elliott | 0113cf1 | 2022-03-11 20:26:47 +0000 | [diff] [blame] | 151 | *own_cid_len = ssl->own_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 152 | if (own_cid != NULL) { |
| 153 | memcpy(own_cid, ssl->own_cid, ssl->own_cid_len); |
| 154 | } |
Paul Elliott | 0113cf1 | 2022-03-11 20:26:47 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | *enabled = MBEDTLS_SSL_CID_ENABLED; |
| 158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 159 | return 0; |
Paul Elliott | 0113cf1 | 2022-03-11 20:26:47 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 162 | int mbedtls_ssl_get_peer_cid(mbedtls_ssl_context *ssl, |
| 163 | int *enabled, |
| 164 | unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX], |
| 165 | size_t *peer_cid_len) |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 166 | { |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 167 | *enabled = MBEDTLS_SSL_CID_DISABLED; |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 168 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 169 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
| 170 | mbedtls_ssl_is_handshake_over(ssl) == 0) { |
| 171 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 172 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 173 | |
Hanno Becker | c5f2422 | 2019-05-03 12:54:52 +0100 | [diff] [blame] | 174 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions |
| 175 | * were used, but client and server requested the empty CID. |
| 176 | * This is indistinguishable from not using the CID extension |
| 177 | * in the first place. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 178 | if (ssl->transform_in->in_cid_len == 0 && |
| 179 | ssl->transform_in->out_cid_len == 0) { |
| 180 | return 0; |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 181 | } |
| 182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 183 | if (peer_cid_len != NULL) { |
Hanno Becker | 615ef17 | 2019-05-22 16:50:35 +0100 | [diff] [blame] | 184 | *peer_cid_len = ssl->transform_in->out_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 185 | if (peer_cid != NULL) { |
| 186 | memcpy(peer_cid, ssl->transform_in->out_cid, |
| 187 | ssl->transform_in->out_cid_len); |
Hanno Becker | 615ef17 | 2019-05-22 16:50:35 +0100 | [diff] [blame] | 188 | } |
| 189 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 190 | |
| 191 | *enabled = MBEDTLS_SSL_CID_ENABLED; |
| 192 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 193 | return 0; |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 194 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 195 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 196 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 197 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 198 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 199 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 200 | /* |
| 201 | * Convert max_fragment_length codes to length. |
| 202 | * RFC 6066 says: |
| 203 | * enum{ |
| 204 | * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255) |
| 205 | * } MaxFragmentLength; |
| 206 | * and we add 0 -> extension unused |
| 207 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 208 | static unsigned int ssl_mfl_code_to_length(int mfl) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 209 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 210 | switch (mfl) { |
| 211 | case MBEDTLS_SSL_MAX_FRAG_LEN_NONE: |
| 212 | return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN; |
| 213 | case MBEDTLS_SSL_MAX_FRAG_LEN_512: |
| 214 | return 512; |
| 215 | case MBEDTLS_SSL_MAX_FRAG_LEN_1024: |
| 216 | return 1024; |
| 217 | case MBEDTLS_SSL_MAX_FRAG_LEN_2048: |
| 218 | return 2048; |
| 219 | case MBEDTLS_SSL_MAX_FRAG_LEN_4096: |
| 220 | return 4096; |
| 221 | default: |
| 222 | return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 223 | } |
| 224 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 225 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 226 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 227 | int mbedtls_ssl_session_copy(mbedtls_ssl_session *dst, |
| 228 | const mbedtls_ssl_session *src) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 229 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 230 | mbedtls_ssl_session_free(dst); |
| 231 | memcpy(dst, src, sizeof(mbedtls_ssl_session)); |
吴敬辉 | 0b71611 | 2021-11-29 10:46:35 +0800 | [diff] [blame] | 232 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 233 | dst->ticket = NULL; |
Xiaokang Qian | 8730644 | 2022-10-12 09:47:38 +0000 | [diff] [blame] | 234 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 235 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 236 | dst->hostname = NULL; |
吴敬辉 | 0b71611 | 2021-11-29 10:46:35 +0800 | [diff] [blame] | 237 | #endif |
Xiaokang Qian | 8730644 | 2022-10-12 09:47:38 +0000 | [diff] [blame] | 238 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
吴敬辉 | 0b71611 | 2021-11-29 10:46:35 +0800 | [diff] [blame] | 239 | |
Waleed Elmelegy | 4dfb0e7 | 2024-03-14 01:48:40 +0000 | [diff] [blame] | 240 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_ALPN) && \ |
| 241 | defined(MBEDTLS_SSL_EARLY_DATA) |
| 242 | dst->ticket_alpn = NULL; |
| 243 | #endif |
| 244 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 245 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 246 | |
| 247 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 248 | if (src->peer_cert != NULL) { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 249 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 250 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 251 | dst->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt)); |
| 252 | if (dst->peer_cert == NULL) { |
| 253 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 254 | } |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 256 | mbedtls_x509_crt_init(dst->peer_cert); |
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 | if ((ret = mbedtls_x509_crt_parse_der(dst->peer_cert, src->peer_cert->raw.p, |
| 259 | src->peer_cert->raw.len)) != 0) { |
| 260 | mbedtls_free(dst->peer_cert); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 261 | dst->peer_cert = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 262 | return ret; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 263 | } |
| 264 | } |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 265 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 266 | if (src->peer_cert_digest != NULL) { |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 267 | dst->peer_cert_digest = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 268 | mbedtls_calloc(1, src->peer_cert_digest_len); |
| 269 | if (dst->peer_cert_digest == NULL) { |
| 270 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 271 | } |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 272 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 273 | memcpy(dst->peer_cert_digest, src->peer_cert_digest, |
| 274 | src->peer_cert_digest_len); |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 275 | dst->peer_cert_digest_type = src->peer_cert_digest_type; |
Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 276 | dst->peer_cert_digest_len = src->peer_cert_digest_len; |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 277 | } |
| 278 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 279 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 280 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 281 | |
Waleed Elmelegy | 4dfb0e7 | 2024-03-14 01:48:40 +0000 | [diff] [blame] | 282 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_ALPN) && \ |
| 283 | defined(MBEDTLS_SSL_EARLY_DATA) |
| 284 | { |
| 285 | int ret = mbedtls_ssl_session_set_ticket_alpn(dst, src->ticket_alpn); |
| 286 | if (ret != 0) { |
| 287 | return ret; |
| 288 | } |
| 289 | } |
| 290 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_ALPN && MBEDTLS_SSL_EARLY_DATA */ |
| 291 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 292 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 293 | if (src->ticket != NULL) { |
| 294 | dst->ticket = mbedtls_calloc(1, src->ticket_len); |
| 295 | if (dst->ticket == NULL) { |
| 296 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 297 | } |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 298 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 299 | memcpy(dst->ticket, src->ticket, src->ticket_len); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 300 | } |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 301 | |
| 302 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 303 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 304 | if (src->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 305 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 306 | ret = mbedtls_ssl_session_set_hostname(dst, src->hostname); |
| 307 | if (ret != 0) { |
| 308 | return ret; |
| 309 | } |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 310 | } |
| 311 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 && |
| 312 | MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 313 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 314 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 315 | return 0; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 316 | } |
| 317 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 318 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 319 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 320 | 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] | 321 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 322 | unsigned char *resized_buffer = mbedtls_calloc(1, len_new); |
| 323 | if (resized_buffer == NULL) { |
Yanray Wang | 365ee3e | 2023-11-22 10:28:28 +0800 | [diff] [blame] | 324 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 325 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 326 | |
| 327 | /* We want to copy len_new bytes when downsizing the buffer, and |
| 328 | * len_old bytes when upsizing, so we choose the smaller of two sizes, |
| 329 | * to fit one buffer into another. Size checks, ensuring that no data is |
| 330 | * lost, are done outside of this function. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 331 | memcpy(resized_buffer, *buffer, |
| 332 | (len_new < *len_old) ? len_new : *len_old); |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 333 | mbedtls_zeroize_and_free(*buffer, *len_old); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 334 | |
| 335 | *buffer = resized_buffer; |
| 336 | *len_old = len_new; |
| 337 | |
| 338 | return 0; |
| 339 | } |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 340 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 341 | static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing, |
| 342 | size_t in_buf_new_len, |
| 343 | size_t out_buf_new_len) |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 344 | { |
| 345 | int modified = 0; |
Deomid rojer Ryabkov | ac2cf1f | 2024-03-10 02:11:03 +0000 | [diff] [blame] | 346 | 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] | 347 | size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0; |
Deomid rojer Ryabkov | ac2cf1f | 2024-03-10 02:11:03 +0000 | [diff] [blame] | 348 | size_t hshdr_in = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 349 | if (ssl->in_buf != NULL) { |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 350 | written_in = ssl->in_msg - ssl->in_buf; |
| 351 | iv_offset_in = ssl->in_iv - ssl->in_buf; |
| 352 | len_offset_in = ssl->in_len - ssl->in_buf; |
Deomid rojer Ryabkov | ac2cf1f | 2024-03-10 02:11:03 +0000 | [diff] [blame] | 353 | hdr_in = ssl->in_hdr - ssl->in_buf; |
| 354 | if (ssl->in_hshdr != NULL) { |
| 355 | hshdr_in = ssl->in_hshdr - ssl->in_buf; |
| 356 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 357 | if (downsizing ? |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 358 | 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] | 359 | ssl->in_buf_len < in_buf_new_len) { |
| 360 | if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != 0) { |
| 361 | MBEDTLS_SSL_DEBUG_MSG(1, ("input buffer resizing failed - out of memory")); |
| 362 | } else { |
| 363 | MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET, |
| 364 | in_buf_new_len)); |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 365 | modified = 1; |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 370 | if (ssl->out_buf != NULL) { |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 371 | written_out = ssl->out_msg - ssl->out_buf; |
| 372 | iv_offset_out = ssl->out_iv - ssl->out_buf; |
| 373 | len_offset_out = ssl->out_len - ssl->out_buf; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 374 | if (downsizing ? |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 375 | 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] | 376 | ssl->out_buf_len < out_buf_new_len) { |
| 377 | if (resize_buffer(&ssl->out_buf, out_buf_new_len, &ssl->out_buf_len) != 0) { |
| 378 | MBEDTLS_SSL_DEBUG_MSG(1, ("output buffer resizing failed - out of memory")); |
| 379 | } else { |
| 380 | MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET, |
| 381 | out_buf_new_len)); |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 382 | modified = 1; |
| 383 | } |
| 384 | } |
| 385 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 386 | if (modified) { |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 387 | /* Update pointers here to avoid doing it twice. */ |
Deomid rojer Ryabkov | ac2cf1f | 2024-03-10 02:11:03 +0000 | [diff] [blame] | 388 | ssl->in_hdr = ssl->in_buf + hdr_in; |
| 389 | mbedtls_ssl_update_in_pointers(ssl); |
| 390 | mbedtls_ssl_reset_out_pointers(ssl); |
| 391 | |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 392 | /* Fields below might not be properly updated with record |
| 393 | * splitting or with CID, so they are manually updated here. */ |
| 394 | ssl->out_msg = ssl->out_buf + written_out; |
| 395 | ssl->out_len = ssl->out_buf + len_offset_out; |
| 396 | ssl->out_iv = ssl->out_buf + iv_offset_out; |
| 397 | |
| 398 | ssl->in_msg = ssl->in_buf + written_in; |
| 399 | ssl->in_len = ssl->in_buf + len_offset_in; |
| 400 | ssl->in_iv = ssl->in_buf + iv_offset_in; |
Deomid rojer Ryabkov | ac2cf1f | 2024-03-10 02:11:03 +0000 | [diff] [blame] | 401 | if (ssl->in_hshdr != NULL) { |
| 402 | ssl->in_hshdr = ssl->in_buf + hshdr_in; |
| 403 | } |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 404 | } |
| 405 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 406 | #endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */ |
| 407 | |
Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 408 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 409 | |
| 410 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 411 | typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen, |
| 412 | const char *label, |
| 413 | const unsigned char *random, size_t rlen, |
| 414 | unsigned char *dstbuf, size_t dlen); |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 415 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 416 | static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id); |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 417 | |
| 418 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
| 419 | |
| 420 | /* Type for the TLS PRF */ |
| 421 | typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *, |
| 422 | const unsigned char *, size_t, |
| 423 | unsigned char *, size_t); |
| 424 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 425 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 426 | static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform, |
| 427 | int ciphersuite, |
| 428 | const unsigned char master[48], |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 429 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 430 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 431 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 432 | ssl_tls_prf_t tls_prf, |
| 433 | const unsigned char randbytes[64], |
| 434 | mbedtls_ssl_protocol_version tls_version, |
| 435 | unsigned endpoint, |
| 436 | const mbedtls_ssl_context *ssl); |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 437 | |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 438 | #if defined(PSA_WANT_ALG_SHA_256) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 439 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 440 | static int tls_prf_sha256(const unsigned char *secret, size_t slen, |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 441 | const char *label, |
| 442 | const unsigned char *random, size_t rlen, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 443 | unsigned char *dstbuf, size_t dlen); |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 444 | static int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *); |
| 445 | 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] | 446 | |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 447 | #endif /* PSA_WANT_ALG_SHA_256*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 448 | |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 449 | #if defined(PSA_WANT_ALG_SHA_384) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 450 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 451 | static int tls_prf_sha384(const unsigned char *secret, size_t slen, |
| 452 | const char *label, |
| 453 | const unsigned char *random, size_t rlen, |
| 454 | unsigned char *dstbuf, size_t dlen); |
| 455 | |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 456 | static int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *); |
| 457 | static int ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int); |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 458 | #endif /* PSA_WANT_ALG_SHA_384*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 459 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 460 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 461 | static int ssl_tls12_session_load(mbedtls_ssl_session *session, |
| 462 | const unsigned char *buf, |
| 463 | size_t len); |
| 464 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 465 | |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 466 | 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] | 467 | |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 468 | #if defined(PSA_WANT_ALG_SHA_256) |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 469 | static int ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t); |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 470 | #endif /* PSA_WANT_ALG_SHA_256*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 471 | |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 472 | #if defined(PSA_WANT_ALG_SHA_384) |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 473 | static int ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t); |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 474 | #endif /* PSA_WANT_ALG_SHA_384*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 475 | |
| 476 | int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf, |
| 477 | const unsigned char *secret, size_t slen, |
| 478 | const char *label, |
| 479 | const unsigned char *random, size_t rlen, |
| 480 | unsigned char *dstbuf, size_t dlen) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 481 | { |
| 482 | mbedtls_ssl_tls_prf_cb *tls_prf = NULL; |
| 483 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 484 | switch (prf) { |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 485 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 486 | #if defined(PSA_WANT_ALG_SHA_384) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 487 | case MBEDTLS_SSL_TLS_PRF_SHA384: |
| 488 | tls_prf = tls_prf_sha384; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 489 | break; |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 490 | #endif /* PSA_WANT_ALG_SHA_384*/ |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 491 | #if defined(PSA_WANT_ALG_SHA_256) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 492 | case MBEDTLS_SSL_TLS_PRF_SHA256: |
| 493 | tls_prf = tls_prf_sha256; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 494 | break; |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 495 | #endif /* PSA_WANT_ALG_SHA_256*/ |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 496 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 497 | default: |
| 498 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 499 | } |
| 500 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 501 | return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen); |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 502 | } |
| 503 | |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 504 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 505 | static void ssl_clear_peer_cert(mbedtls_ssl_session *session) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 506 | { |
| 507 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 508 | if (session->peer_cert != NULL) { |
| 509 | mbedtls_x509_crt_free(session->peer_cert); |
| 510 | mbedtls_free(session->peer_cert); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 511 | session->peer_cert = NULL; |
| 512 | } |
| 513 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 514 | if (session->peer_cert_digest != NULL) { |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 515 | /* Zeroization is not necessary. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 516 | mbedtls_free(session->peer_cert_digest); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 517 | session->peer_cert_digest = NULL; |
| 518 | session->peer_cert_digest_type = MBEDTLS_MD_NONE; |
| 519 | session->peer_cert_digest_len = 0; |
| 520 | } |
| 521 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 522 | } |
| 523 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 524 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 525 | uint32_t mbedtls_ssl_get_extension_id(unsigned int extension_type) |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 526 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 527 | switch (extension_type) { |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 528 | case MBEDTLS_TLS_EXT_SERVERNAME: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 529 | return MBEDTLS_SSL_EXT_ID_SERVERNAME; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 530 | |
| 531 | case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 532 | return MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 533 | |
| 534 | case MBEDTLS_TLS_EXT_STATUS_REQUEST: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 535 | return MBEDTLS_SSL_EXT_ID_STATUS_REQUEST; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 536 | |
| 537 | case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 538 | return MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 539 | |
| 540 | case MBEDTLS_TLS_EXT_SIG_ALG: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 541 | return MBEDTLS_SSL_EXT_ID_SIG_ALG; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 542 | |
| 543 | case MBEDTLS_TLS_EXT_USE_SRTP: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 544 | return MBEDTLS_SSL_EXT_ID_USE_SRTP; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 545 | |
| 546 | case MBEDTLS_TLS_EXT_HEARTBEAT: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 547 | return MBEDTLS_SSL_EXT_ID_HEARTBEAT; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 548 | |
| 549 | case MBEDTLS_TLS_EXT_ALPN: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 550 | return MBEDTLS_SSL_EXT_ID_ALPN; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 551 | |
| 552 | case MBEDTLS_TLS_EXT_SCT: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 553 | return MBEDTLS_SSL_EXT_ID_SCT; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 554 | |
| 555 | case MBEDTLS_TLS_EXT_CLI_CERT_TYPE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 556 | return MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 557 | |
| 558 | case MBEDTLS_TLS_EXT_SERV_CERT_TYPE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 559 | return MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 560 | |
| 561 | case MBEDTLS_TLS_EXT_PADDING: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 562 | return MBEDTLS_SSL_EXT_ID_PADDING; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 563 | |
| 564 | case MBEDTLS_TLS_EXT_PRE_SHARED_KEY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 565 | return MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 566 | |
| 567 | case MBEDTLS_TLS_EXT_EARLY_DATA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 568 | return MBEDTLS_SSL_EXT_ID_EARLY_DATA; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 569 | |
| 570 | case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 571 | return MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 572 | |
| 573 | case MBEDTLS_TLS_EXT_COOKIE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 574 | return MBEDTLS_SSL_EXT_ID_COOKIE; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 575 | |
| 576 | case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 577 | return MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 578 | |
| 579 | case MBEDTLS_TLS_EXT_CERT_AUTH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 580 | return MBEDTLS_SSL_EXT_ID_CERT_AUTH; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 581 | |
| 582 | case MBEDTLS_TLS_EXT_OID_FILTERS: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 583 | return MBEDTLS_SSL_EXT_ID_OID_FILTERS; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 584 | |
| 585 | case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 586 | return MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 587 | |
| 588 | case MBEDTLS_TLS_EXT_SIG_ALG_CERT: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 589 | return MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 590 | |
| 591 | case MBEDTLS_TLS_EXT_KEY_SHARE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 592 | return MBEDTLS_SSL_EXT_ID_KEY_SHARE; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 593 | |
| 594 | case MBEDTLS_TLS_EXT_TRUNCATED_HMAC: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 595 | return MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 596 | |
| 597 | case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 598 | return MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 599 | |
| 600 | case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 601 | return MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 602 | |
| 603 | case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 604 | return MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 605 | |
Jan Bruckner | 151f642 | 2023-02-10 12:45:19 +0100 | [diff] [blame] | 606 | case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT: |
| 607 | return MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT; |
| 608 | |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 609 | case MBEDTLS_TLS_EXT_SESSION_TICKET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 610 | return MBEDTLS_SSL_EXT_ID_SESSION_TICKET; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 611 | |
| 612 | } |
| 613 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 614 | return MBEDTLS_SSL_EXT_ID_UNRECOGNIZED; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 615 | } |
| 616 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 617 | uint32_t mbedtls_ssl_get_extension_mask(unsigned int extension_type) |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 618 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 619 | return 1 << mbedtls_ssl_get_extension_id(extension_type); |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 620 | } |
| 621 | |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 622 | #if defined(MBEDTLS_DEBUG_C) |
| 623 | static const char *extension_name_table[] = { |
Jerry Yu | ea52ed9 | 2022-11-08 21:01:17 +0800 | [diff] [blame] | 624 | [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized", |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 625 | [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name", |
| 626 | [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length", |
| 627 | [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request", |
| 628 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups", |
| 629 | [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms", |
| 630 | [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp", |
| 631 | [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat", |
| 632 | [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation", |
| 633 | [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp", |
| 634 | [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type", |
| 635 | [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type", |
| 636 | [MBEDTLS_SSL_EXT_ID_PADDING] = "padding", |
| 637 | [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key", |
| 638 | [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data", |
| 639 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions", |
| 640 | [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie", |
| 641 | [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes", |
| 642 | [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities", |
| 643 | [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters", |
| 644 | [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth", |
| 645 | [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert", |
| 646 | [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share", |
| 647 | [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac", |
| 648 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats", |
| 649 | [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac", |
| 650 | [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret", |
Jan Bruckner | 151f642 | 2023-02-10 12:45:19 +0100 | [diff] [blame] | 651 | [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket", |
| 652 | [MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT] = "record_size_limit" |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 653 | }; |
| 654 | |
Chien Wong | 4e9683e | 2023-12-28 17:07:43 +0800 | [diff] [blame] | 655 | static const unsigned int extension_type_table[] = { |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 656 | [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff, |
| 657 | [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME, |
| 658 | [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, |
| 659 | [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST, |
| 660 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, |
| 661 | [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG, |
| 662 | [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP, |
| 663 | [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT, |
| 664 | [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN, |
| 665 | [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT, |
| 666 | [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE, |
| 667 | [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE, |
| 668 | [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING, |
| 669 | [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY, |
| 670 | [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA, |
| 671 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, |
| 672 | [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE, |
| 673 | [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES, |
| 674 | [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH, |
| 675 | [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS, |
| 676 | [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH, |
| 677 | [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT, |
| 678 | [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE, |
| 679 | [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC, |
| 680 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, |
| 681 | [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, |
| 682 | [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] | 683 | [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET, |
| 684 | [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] | 685 | }; |
| 686 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 687 | const char *mbedtls_ssl_get_extension_name(unsigned int extension_type) |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 688 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 689 | return extension_name_table[ |
| 690 | mbedtls_ssl_get_extension_id(extension_type)]; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 691 | } |
| 692 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 693 | 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] | 694 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 695 | switch (hs_msg_type) { |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 696 | case MBEDTLS_SSL_HS_CLIENT_HELLO: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 697 | return "ClientHello"; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 698 | case MBEDTLS_SSL_HS_SERVER_HELLO: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 699 | return "ServerHello"; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 700 | case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 701 | return "HelloRetryRequest"; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 702 | case MBEDTLS_SSL_HS_NEW_SESSION_TICKET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 703 | return "NewSessionTicket"; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 704 | case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 705 | return "EncryptedExtensions"; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 706 | case MBEDTLS_SSL_HS_CERTIFICATE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 707 | return "Certificate"; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 708 | case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 709 | return "CertificateRequest"; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 710 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 711 | return "Unknown"; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 712 | } |
| 713 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 714 | void mbedtls_ssl_print_extension(const mbedtls_ssl_context *ssl, |
| 715 | int level, const char *file, int line, |
| 716 | int hs_msg_type, unsigned int extension_type, |
| 717 | const char *extra_msg0, const char *extra_msg1) |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 718 | { |
| 719 | const char *extra_msg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 720 | if (extra_msg0 && extra_msg1) { |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 721 | mbedtls_debug_print_msg( |
| 722 | ssl, level, file, line, |
| 723 | "%s: %s(%u) extension %s %s.", |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 724 | ssl_tls13_get_hs_msg_name(hs_msg_type), |
| 725 | mbedtls_ssl_get_extension_name(extension_type), |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 726 | extension_type, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 727 | extra_msg0, extra_msg1); |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 728 | return; |
| 729 | } |
| 730 | |
| 731 | extra_msg = extra_msg0 ? extra_msg0 : extra_msg1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 732 | if (extra_msg) { |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 733 | mbedtls_debug_print_msg( |
| 734 | ssl, level, file, line, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 735 | "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name(hs_msg_type), |
| 736 | mbedtls_ssl_get_extension_name(extension_type), extension_type, |
| 737 | extra_msg); |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 738 | return; |
| 739 | } |
| 740 | |
| 741 | mbedtls_debug_print_msg( |
| 742 | ssl, level, file, line, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 743 | "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name(hs_msg_type), |
| 744 | mbedtls_ssl_get_extension_name(extension_type), extension_type); |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 745 | } |
| 746 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 747 | void mbedtls_ssl_print_extensions(const mbedtls_ssl_context *ssl, |
| 748 | int level, const char *file, int line, |
| 749 | int hs_msg_type, uint32_t extensions_mask, |
| 750 | const char *extra) |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 751 | { |
| 752 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 753 | for (unsigned i = 0; |
| 754 | i < sizeof(extension_name_table) / sizeof(extension_name_table[0]); |
| 755 | i++) { |
Jerry Yu | 79aa721 | 2022-11-08 21:30:21 +0800 | [diff] [blame] | 756 | mbedtls_ssl_print_extension( |
Jerry Yu | ea52ed9 | 2022-11-08 21:01:17 +0800 | [diff] [blame] | 757 | ssl, level, file, line, hs_msg_type, extension_type_table[i], |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 758 | extensions_mask & (1 << i) ? "exists" : "does not exist", extra); |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 759 | } |
| 760 | } |
| 761 | |
Pengyu Lv | ee455c0 | 2023-01-12 14:37:24 +0800 | [diff] [blame] | 762 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Pengyu Lv | ee455c0 | 2023-01-12 14:37:24 +0800 | [diff] [blame] | 763 | static const char *ticket_flag_name_table[] = |
| 764 | { |
| 765 | [0] = "ALLOW_PSK_RESUMPTION", |
| 766 | [2] = "ALLOW_PSK_EPHEMERAL_RESUMPTION", |
| 767 | [3] = "ALLOW_EARLY_DATA", |
| 768 | }; |
| 769 | |
Pengyu Lv | 4938a56 | 2023-01-16 11:28:49 +0800 | [diff] [blame] | 770 | void mbedtls_ssl_print_ticket_flags(const mbedtls_ssl_context *ssl, |
| 771 | int level, const char *file, int line, |
| 772 | unsigned int flags) |
Pengyu Lv | ee455c0 | 2023-01-12 14:37:24 +0800 | [diff] [blame] | 773 | { |
| 774 | size_t i; |
| 775 | |
| 776 | mbedtls_debug_print_msg(ssl, level, file, line, |
Pengyu Lv | 4938a56 | 2023-01-16 11:28:49 +0800 | [diff] [blame] | 777 | "print ticket_flags (0x%02x)", flags); |
| 778 | |
| 779 | flags = flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK; |
Pengyu Lv | ee455c0 | 2023-01-12 14:37:24 +0800 | [diff] [blame] | 780 | |
| 781 | for (i = 0; i < ARRAY_LENGTH(ticket_flag_name_table); i++) { |
Pengyu Lv | 4938a56 | 2023-01-16 11:28:49 +0800 | [diff] [blame] | 782 | if ((flags & (1 << i))) { |
Pengyu Lv | ee455c0 | 2023-01-12 14:37:24 +0800 | [diff] [blame] | 783 | mbedtls_debug_print_msg(ssl, level, file, line, "- %s is set.", |
| 784 | ticket_flag_name_table[i]); |
| 785 | } |
| 786 | } |
| 787 | } |
| 788 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */ |
| 789 | |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 790 | #endif /* MBEDTLS_DEBUG_C */ |
| 791 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 792 | void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl, |
| 793 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 794 | { |
| 795 | ((void) ciphersuite_info); |
| 796 | |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 797 | #if defined(PSA_WANT_ALG_SHA_384) |
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_sha384; |
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 |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 802 | #if defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 803 | if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) { |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 804 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 805 | } else |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 806 | #endif |
| 807 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 808 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 809 | return; |
| 810 | } |
| 811 | } |
| 812 | |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 813 | 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] | 814 | unsigned hs_type, |
| 815 | size_t total_hs_len) |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 816 | { |
| 817 | unsigned char hs_hdr[4]; |
| 818 | |
| 819 | /* Build HS header for checksum update. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 820 | hs_hdr[0] = MBEDTLS_BYTE_0(hs_type); |
| 821 | hs_hdr[1] = MBEDTLS_BYTE_2(total_hs_len); |
| 822 | hs_hdr[2] = MBEDTLS_BYTE_1(total_hs_len); |
| 823 | hs_hdr[3] = MBEDTLS_BYTE_0(total_hs_len); |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 824 | |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 825 | return ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr)); |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 826 | } |
| 827 | |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 828 | 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] | 829 | unsigned hs_type, |
| 830 | unsigned char const *msg, |
| 831 | size_t msg_len) |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 832 | { |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 833 | int ret; |
| 834 | 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] | 835 | if (ret != 0) { |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 836 | return ret; |
Manuel Pégourié-Gonnard | 43cc127 | 2023-02-06 11:48:19 +0100 | [diff] [blame] | 837 | } |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 838 | return ssl->handshake->update_checksum(ssl, msg, msg_len); |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 839 | } |
| 840 | |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 841 | int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 842 | { |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 843 | #if defined(PSA_WANT_ALG_SHA_256) || \ |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 844 | defined(PSA_WANT_ALG_SHA_384) |
Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 845 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 846 | psa_status_t status; |
| 847 | #else |
| 848 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 849 | #endif |
Manuel Pégourié-Gonnard | 626aaed | 2023-02-06 22:03:06 +0100 | [diff] [blame] | 850 | #else /* SHA-256 or SHA-384 */ |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 851 | ((void) ssl); |
Manuel Pégourié-Gonnard | 626aaed | 2023-02-06 22:03:06 +0100 | [diff] [blame] | 852 | #endif /* SHA-256 or SHA-384 */ |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 853 | #if defined(PSA_WANT_ALG_SHA_256) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 854 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 855 | status = psa_hash_abort(&ssl->handshake->fin_sha256_psa); |
| 856 | if (status != PSA_SUCCESS) { |
Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 857 | return mbedtls_md_error_from_psa(status); |
Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 858 | } |
| 859 | status = psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256); |
| 860 | if (status != PSA_SUCCESS) { |
Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 861 | return mbedtls_md_error_from_psa(status); |
Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 862 | } |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 863 | #else |
Manuel Pégourié-Gonnard | 947cee1 | 2023-03-06 11:59:59 +0100 | [diff] [blame] | 864 | mbedtls_md_free(&ssl->handshake->fin_sha256); |
| 865 | mbedtls_md_init(&ssl->handshake->fin_sha256); |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 866 | ret = mbedtls_md_setup(&ssl->handshake->fin_sha256, |
| 867 | mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), |
| 868 | 0); |
| 869 | if (ret != 0) { |
| 870 | return ret; |
| 871 | } |
| 872 | ret = mbedtls_md_starts(&ssl->handshake->fin_sha256); |
Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 873 | if (ret != 0) { |
| 874 | return ret; |
| 875 | } |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 876 | #endif |
| 877 | #endif |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 878 | #if defined(PSA_WANT_ALG_SHA_384) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 879 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 880 | status = psa_hash_abort(&ssl->handshake->fin_sha384_psa); |
| 881 | if (status != PSA_SUCCESS) { |
Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 882 | return mbedtls_md_error_from_psa(status); |
Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 883 | } |
| 884 | status = psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384); |
| 885 | if (status != PSA_SUCCESS) { |
Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 886 | return mbedtls_md_error_from_psa(status); |
Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 887 | } |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 888 | #else |
Manuel Pégourié-Gonnard | 947cee1 | 2023-03-06 11:59:59 +0100 | [diff] [blame] | 889 | mbedtls_md_free(&ssl->handshake->fin_sha384); |
| 890 | mbedtls_md_init(&ssl->handshake->fin_sha384); |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 891 | ret = mbedtls_md_setup(&ssl->handshake->fin_sha384, |
| 892 | mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0); |
| 893 | if (ret != 0) { |
| 894 | return ret; |
| 895 | } |
| 896 | ret = mbedtls_md_starts(&ssl->handshake->fin_sha384); |
Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 897 | if (ret != 0) { |
| 898 | return ret; |
| 899 | } |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 900 | #endif |
| 901 | #endif |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 902 | return 0; |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 903 | } |
| 904 | |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 905 | static int ssl_update_checksum_start(mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 43cc127 | 2023-02-06 11:48:19 +0100 | [diff] [blame] | 906 | const unsigned char *buf, size_t len) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 907 | { |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 908 | #if defined(PSA_WANT_ALG_SHA_256) || \ |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 909 | defined(PSA_WANT_ALG_SHA_384) |
Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 910 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 911 | psa_status_t status; |
| 912 | #else |
| 913 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 914 | #endif |
Manuel Pégourié-Gonnard | 626aaed | 2023-02-06 22:03:06 +0100 | [diff] [blame] | 915 | #else /* SHA-256 or SHA-384 */ |
| 916 | ((void) ssl); |
| 917 | (void) buf; |
| 918 | (void) len; |
| 919 | #endif /* SHA-256 or SHA-384 */ |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 920 | #if defined(PSA_WANT_ALG_SHA_256) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 921 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 922 | status = psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len); |
| 923 | if (status != PSA_SUCCESS) { |
Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 924 | return mbedtls_md_error_from_psa(status); |
Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 925 | } |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 926 | #else |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 927 | ret = mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len); |
Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 928 | if (ret != 0) { |
| 929 | return ret; |
| 930 | } |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 931 | #endif |
| 932 | #endif |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 933 | #if defined(PSA_WANT_ALG_SHA_384) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 934 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 935 | status = psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len); |
| 936 | if (status != PSA_SUCCESS) { |
Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 937 | return mbedtls_md_error_from_psa(status); |
Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 938 | } |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 939 | #else |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 940 | ret = mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len); |
Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 941 | if (ret != 0) { |
| 942 | return ret; |
| 943 | } |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 944 | #endif |
| 945 | #endif |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 946 | return 0; |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 947 | } |
| 948 | |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 949 | #if defined(PSA_WANT_ALG_SHA_256) |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 950 | static int ssl_update_checksum_sha256(mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 43cc127 | 2023-02-06 11:48:19 +0100 | [diff] [blame] | 951 | const unsigned char *buf, size_t len) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 952 | { |
| 953 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 954 | return mbedtls_md_error_from_psa(psa_hash_update( |
| 955 | &ssl->handshake->fin_sha256_psa, buf, len)); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 956 | #else |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 957 | return mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 958 | #endif |
| 959 | } |
| 960 | #endif |
| 961 | |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 962 | #if defined(PSA_WANT_ALG_SHA_384) |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 963 | static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 43cc127 | 2023-02-06 11:48:19 +0100 | [diff] [blame] | 964 | const unsigned char *buf, size_t len) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 965 | { |
| 966 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 967 | return mbedtls_md_error_from_psa(psa_hash_update( |
| 968 | &ssl->handshake->fin_sha384_psa, buf, len)); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 969 | #else |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 970 | return mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 971 | #endif |
| 972 | } |
| 973 | #endif |
| 974 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 975 | static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 976 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 977 | memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params)); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 978 | |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 979 | #if defined(PSA_WANT_ALG_SHA_256) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 980 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 981 | handshake->fin_sha256_psa = psa_hash_operation_init(); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 982 | #else |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 983 | mbedtls_md_init(&handshake->fin_sha256); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 984 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 985 | #endif |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 986 | #if defined(PSA_WANT_ALG_SHA_384) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 987 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 988 | handshake->fin_sha384_psa = psa_hash_operation_init(); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 989 | #else |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 990 | mbedtls_md_init(&handshake->fin_sha384); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 991 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 992 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 993 | |
| 994 | handshake->update_checksum = ssl_update_checksum_start; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 995 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 996 | #if defined(MBEDTLS_DHM_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 997 | mbedtls_dhm_init(&handshake->dhm_ctx); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 998 | #endif |
Valerio Setti | 7aeec54 | 2023-07-05 18:57:21 +0200 | [diff] [blame] | 999 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Valerio Setti | 6eb0054 | 2023-07-07 17:04:24 +0200 | [diff] [blame] | 1000 | defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1001 | mbedtls_ecdh_init(&handshake->ecdh_ctx); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1002 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1003 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1004 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1005 | handshake->psa_pake_ctx = psa_pake_operation_init(); |
| 1006 | handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT; |
| 1007 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1008 | mbedtls_ecjpake_init(&handshake->ecjpake_ctx); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1009 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 1010 | #if defined(MBEDTLS_SSL_CLI_C) |
| 1011 | handshake->ecjpake_cache = NULL; |
| 1012 | handshake->ecjpake_cache_len = 0; |
| 1013 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 1014 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1015 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1016 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1017 | mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 1018 | #endif |
| 1019 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1020 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 1021 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 1022 | #endif |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 1023 | |
| 1024 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 1025 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1026 | mbedtls_pk_init(&handshake->peer_pubkey); |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 1027 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1028 | } |
| 1029 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1030 | void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1031 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1032 | memset(transform, 0, sizeof(mbedtls_ssl_transform)); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 1033 | |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 1034 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1035 | transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 1036 | transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 1037 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1038 | mbedtls_cipher_init(&transform->cipher_ctx_enc); |
| 1039 | mbedtls_cipher_init(&transform->cipher_ctx_dec); |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 1040 | #endif |
| 1041 | |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 1042 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 39b8e7d | 2022-02-23 09:24:45 +0100 | [diff] [blame] | 1043 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1044 | transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 1045 | transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 1046 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1047 | mbedtls_md_init(&transform->md_ctx_enc); |
| 1048 | mbedtls_md_init(&transform->md_ctx_dec); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1049 | #endif |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 1050 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1051 | } |
| 1052 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1053 | void mbedtls_ssl_session_init(mbedtls_ssl_session *session) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1054 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1055 | memset(session, 0, sizeof(mbedtls_ssl_session)); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1056 | } |
| 1057 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1058 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1059 | static int ssl_handshake_init(mbedtls_ssl_context *ssl) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1060 | { |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 1061 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1062 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1063 | /* Clear old handshake information if present */ |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1064 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1065 | if (ssl->transform_negotiate) { |
| 1066 | mbedtls_ssl_transform_free(ssl->transform_negotiate); |
| 1067 | } |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1068 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1069 | if (ssl->session_negotiate) { |
| 1070 | mbedtls_ssl_session_free(ssl->session_negotiate); |
| 1071 | } |
| 1072 | if (ssl->handshake) { |
| 1073 | mbedtls_ssl_handshake_free(ssl); |
| 1074 | } |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1075 | |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1076 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1077 | /* |
| 1078 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 1079 | * Now allocate missing structures. |
| 1080 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1081 | if (ssl->transform_negotiate == NULL) { |
| 1082 | ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform)); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 1083 | } |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1084 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1085 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1086 | if (ssl->session_negotiate == NULL) { |
| 1087 | ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session)); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 1088 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1089 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1090 | if (ssl->handshake == NULL) { |
| 1091 | ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params)); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 1092 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1093 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1094 | /* If the buffers are too small - reallocate */ |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 1095 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1096 | handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN, |
| 1097 | MBEDTLS_SSL_OUT_BUFFER_LEN); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1098 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1099 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1100 | /* All pointers should exist and can be directly freed without issue */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1101 | if (ssl->handshake == NULL || |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1102 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1103 | ssl->transform_negotiate == NULL || |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1104 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1105 | ssl->session_negotiate == NULL) { |
| 1106 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed")); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1107 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1108 | mbedtls_free(ssl->handshake); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1109 | ssl->handshake = NULL; |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1110 | |
| 1111 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1112 | mbedtls_free(ssl->transform_negotiate); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1113 | ssl->transform_negotiate = NULL; |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1114 | #endif |
| 1115 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1116 | mbedtls_free(ssl->session_negotiate); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1117 | ssl->session_negotiate = NULL; |
| 1118 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1119 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
Jerry Yu | 4caf3ca | 2023-11-15 16:13:47 +0800 | [diff] [blame] | 1122 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 1123 | #if defined(MBEDTLS_SSL_CLI_C) |
Ronald Cron | 05d7cfb | 2024-03-03 15:39:30 +0100 | [diff] [blame] | 1124 | ssl->early_data_state = MBEDTLS_SSL_EARLY_DATA_STATE_IDLE; |
Ronald Cron | 5d0ae90 | 2024-01-05 14:20:35 +0100 | [diff] [blame] | 1125 | #endif |
Jerry Yu | 4caf3ca | 2023-11-15 16:13:47 +0800 | [diff] [blame] | 1126 | #if defined(MBEDTLS_SSL_SRV_C) |
| 1127 | ssl->discard_early_data_record = MBEDTLS_SSL_EARLY_DATA_NO_DISCARD; |
| 1128 | #endif |
Ronald Cron | 19bfe0a | 2024-02-26 16:43:01 +0100 | [diff] [blame] | 1129 | ssl->total_early_data_size = 0; |
Jerry Yu | 4caf3ca | 2023-11-15 16:13:47 +0800 | [diff] [blame] | 1130 | #endif /* MBEDTLS_SSL_EARLY_DATA */ |
Ronald Cron | 5d0ae90 | 2024-01-05 14:20:35 +0100 | [diff] [blame] | 1131 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1132 | /* Initialize structures */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1133 | mbedtls_ssl_session_init(ssl->session_negotiate); |
| 1134 | ssl_handshake_params_init(ssl->handshake); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 1135 | |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1136 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1137 | mbedtls_ssl_transform_init(ssl->transform_negotiate); |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1138 | #endif |
| 1139 | |
Manuel Pégourié-Gonnard | 537f231 | 2023-02-05 10:17:45 +0100 | [diff] [blame] | 1140 | /* Setup handshake checksums */ |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 1141 | ret = mbedtls_ssl_reset_checksum(ssl); |
| 1142 | if (ret != 0) { |
| 1143 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret); |
| 1144 | return ret; |
| 1145 | } |
Manuel Pégourié-Gonnard | 537f231 | 2023-02-05 10:17:45 +0100 | [diff] [blame] | 1146 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 1147 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 1148 | defined(MBEDTLS_SSL_SRV_C) && \ |
| 1149 | defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 1150 | ssl->handshake->new_session_tickets_count = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1151 | ssl->conf->new_session_tickets_count; |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 1152 | #endif |
| 1153 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1154 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1155 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1156 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1157 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1158 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1159 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1160 | } else { |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1161 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1162 | } |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1164 | mbedtls_ssl_set_timer(ssl, 0); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1165 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1166 | #endif |
| 1167 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1168 | /* |
| 1169 | * curve_list is translated to IANA TLS group identifiers here because |
| 1170 | * mbedtls_ssl_conf_curves returns void and so can't return |
| 1171 | * any error codes. |
| 1172 | */ |
Manuel Pégourié-Gonnard | f07ce3b | 2023-09-22 11:53:41 +0200 | [diff] [blame] | 1173 | #if defined(MBEDTLS_ECP_C) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1174 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 1175 | /* Heap allocate and translate curve_list from internal to IANA group ids */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1176 | if (ssl->conf->curve_list != NULL) { |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1177 | size_t length; |
| 1178 | const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list; |
| 1179 | |
Thomas Daubney | 850a079 | 2023-05-22 12:05:03 +0100 | [diff] [blame] | 1180 | for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE); length++) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1181 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1182 | |
| 1183 | /* Leave room for zero termination */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1184 | uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t)); |
| 1185 | if (group_list == NULL) { |
| 1186 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 1187 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1188 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1189 | for (size_t i = 0; i < length; i++) { |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 1190 | 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] | 1191 | curve_list[i]); |
| 1192 | if (tls_id == 0) { |
| 1193 | mbedtls_free(group_list); |
| 1194 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1195 | } |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 1196 | group_list[i] = tls_id; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | group_list[length] = 0; |
| 1200 | |
| 1201 | ssl->handshake->group_list = group_list; |
| 1202 | ssl->handshake->group_list_heap_allocated = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1203 | } else { |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1204 | ssl->handshake->group_list = ssl->conf->group_list; |
| 1205 | ssl->handshake->group_list_heap_allocated = 0; |
| 1206 | } |
| 1207 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Manuel Pégourié-Gonnard | f07ce3b | 2023-09-22 11:53:41 +0200 | [diff] [blame] | 1208 | #endif /* MBEDTLS_ECP_C */ |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1209 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1210 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1211 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 1212 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 1213 | /* Heap allocate and translate sig_hashes from internal hash identifiers to |
| 1214 | signature algorithms IANA identifiers. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1215 | if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) && |
| 1216 | ssl->conf->sig_hashes != NULL) { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1217 | const int *md; |
| 1218 | const int *sig_hashes = ssl->conf->sig_hashes; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1219 | size_t sig_algs_len = 0; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1220 | uint16_t *p; |
| 1221 | |
Tom Cosgrove | 6ef9bb3 | 2023-03-08 14:19:51 +0000 | [diff] [blame] | 1222 | MBEDTLS_STATIC_ASSERT(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN |
| 1223 | <= (SIZE_MAX - (2 * sizeof(uint16_t))), |
| 1224 | "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big"); |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 1225 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1226 | for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) { |
| 1227 | if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1228 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1229 | } |
Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 1230 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1231 | sig_algs_len += sizeof(uint16_t); |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1232 | #endif |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 1233 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1234 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1235 | sig_algs_len += sizeof(uint16_t); |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1236 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1237 | if (sig_algs_len > MBEDTLS_SSL_MAX_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 | } |
| 1241 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1242 | if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) { |
| 1243 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
| 1244 | } |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1245 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1246 | ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len + |
| 1247 | sizeof(uint16_t)); |
| 1248 | if (ssl->handshake->sig_algs == NULL) { |
| 1249 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 1250 | } |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1252 | p = (uint16_t *) ssl->handshake->sig_algs; |
| 1253 | for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) { |
| 1254 | unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md); |
| 1255 | if (hash == MBEDTLS_SSL_HASH_NONE) { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1256 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1257 | } |
Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 1258 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1259 | *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1260 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 1261 | #endif |
| 1262 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1263 | *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1264 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 1265 | #endif |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1266 | } |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 1267 | *p = MBEDTLS_TLS_SIG_NONE; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1268 | ssl->handshake->sig_algs_heap_allocated = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1269 | } else |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 1270 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1271 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1272 | ssl->handshake->sig_algs_heap_allocated = 0; |
| 1273 | } |
Jerry Yu | cc53910 | 2022-06-27 16:27:35 +0800 | [diff] [blame] | 1274 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1275 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1276 | return 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1279 | #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] | 1280 | /* Dummy cookie callbacks for defaults */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1281 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1282 | static int ssl_cookie_write_dummy(void *ctx, |
| 1283 | unsigned char **p, unsigned char *end, |
| 1284 | const unsigned char *cli_id, size_t cli_id_len) |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1285 | { |
| 1286 | ((void) ctx); |
| 1287 | ((void) p); |
| 1288 | ((void) end); |
| 1289 | ((void) cli_id); |
| 1290 | ((void) cli_id_len); |
| 1291 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1292 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1293 | } |
| 1294 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1295 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1296 | static int ssl_cookie_check_dummy(void *ctx, |
| 1297 | const unsigned char *cookie, size_t cookie_len, |
| 1298 | const unsigned char *cli_id, size_t cli_id_len) |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1299 | { |
| 1300 | ((void) ctx); |
| 1301 | ((void) cookie); |
| 1302 | ((void) cookie_len); |
| 1303 | ((void) cli_id); |
| 1304 | ((void) cli_id_len); |
| 1305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1306 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1307 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1308 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1309 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1310 | /* |
| 1311 | * Initialize an SSL context |
| 1312 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1313 | void mbedtls_ssl_init(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1314 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1315 | memset(ssl, 0, sizeof(mbedtls_ssl_context)); |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1316 | } |
| 1317 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1318 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1319 | static int ssl_conf_version_check(const mbedtls_ssl_context *ssl) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1320 | { |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 1321 | const mbedtls_ssl_config *conf = ssl->conf; |
| 1322 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1323 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1324 | if (mbedtls_ssl_conf_is_tls13_only(conf)) { |
| 1325 | if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 1326 | MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported.")); |
| 1327 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1330 | MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only.")); |
| 1331 | return 0; |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1332 | } |
| 1333 | #endif |
| 1334 | |
| 1335 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1336 | if (mbedtls_ssl_conf_is_tls12_only(conf)) { |
| 1337 | MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only.")); |
| 1338 | return 0; |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1339 | } |
| 1340 | #endif |
| 1341 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1342 | #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] | 1343 | if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) { |
| 1344 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 1345 | MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2")); |
| 1346 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 1347 | } |
| 1348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1349 | MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2.")); |
| 1350 | return 0; |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1351 | } |
| 1352 | #endif |
| 1353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1354 | MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid.")); |
| 1355 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1356 | } |
| 1357 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1358 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1359 | static int ssl_conf_check(const mbedtls_ssl_context *ssl) |
| 1360 | { |
| 1361 | int ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1362 | ret = ssl_conf_version_check(ssl); |
| 1363 | if (ret != 0) { |
| 1364 | return ret; |
| 1365 | } |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1366 | |
Yanray Wang | b422cab | 2023-12-01 16:18:10 +0800 | [diff] [blame] | 1367 | if (ssl->conf->f_rng == NULL) { |
| 1368 | MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided")); |
| 1369 | return MBEDTLS_ERR_SSL_NO_RNG; |
| 1370 | } |
| 1371 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1372 | /* Space for further checks */ |
| 1373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1374 | return 0; |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1375 | } |
| 1376 | |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1377 | /* |
| 1378 | * Setup an SSL context |
| 1379 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 1380 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1381 | int mbedtls_ssl_setup(mbedtls_ssl_context *ssl, |
| 1382 | const mbedtls_ssl_config *conf) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1383 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1384 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1385 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1386 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1387 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 1388 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1389 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1390 | if ((ret = ssl_conf_check(ssl)) != 0) { |
| 1391 | return ret; |
| 1392 | } |
Ronald Cron | 8a12aee | 2023-03-08 15:30:43 +0100 | [diff] [blame] | 1393 | ssl->tls_version = ssl->conf->max_tls_version; |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1394 | |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1395 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 1396 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1397 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 1398 | |
| 1399 | /* Set to NULL in case of an error condition */ |
| 1400 | ssl->out_buf = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1401 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1402 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1403 | ssl->in_buf_len = in_buf_len; |
| 1404 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1405 | ssl->in_buf = mbedtls_calloc(1, in_buf_len); |
| 1406 | if (ssl->in_buf == NULL) { |
| 1407 | 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] | 1408 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1409 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1410 | } |
| 1411 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1412 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1413 | ssl->out_buf_len = out_buf_len; |
| 1414 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1415 | ssl->out_buf = mbedtls_calloc(1, out_buf_len); |
| 1416 | if (ssl->out_buf == NULL) { |
| 1417 | 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] | 1418 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1419 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
Deomid rojer Ryabkov | 3dfe75e | 2025-01-26 10:43:42 +0200 | [diff] [blame] | 1422 | mbedtls_ssl_reset_in_pointers(ssl); |
| 1423 | mbedtls_ssl_reset_out_pointers(ssl); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 1424 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1425 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1426 | memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info)); |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1427 | #endif |
| 1428 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1429 | if ((ret = ssl_handshake_init(ssl)) != 0) { |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1430 | goto error; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1431 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1433 | return 0; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1434 | |
| 1435 | error: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1436 | mbedtls_free(ssl->in_buf); |
| 1437 | mbedtls_free(ssl->out_buf); |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1438 | |
| 1439 | ssl->conf = NULL; |
| 1440 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1441 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1442 | ssl->in_buf_len = 0; |
| 1443 | ssl->out_buf_len = 0; |
| 1444 | #endif |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1445 | ssl->in_buf = NULL; |
| 1446 | ssl->out_buf = NULL; |
| 1447 | |
| 1448 | ssl->in_hdr = NULL; |
| 1449 | ssl->in_ctr = NULL; |
| 1450 | ssl->in_len = NULL; |
| 1451 | ssl->in_iv = NULL; |
| 1452 | ssl->in_msg = NULL; |
| 1453 | |
| 1454 | ssl->out_hdr = NULL; |
| 1455 | ssl->out_ctr = NULL; |
| 1456 | ssl->out_len = NULL; |
| 1457 | ssl->out_iv = NULL; |
| 1458 | ssl->out_msg = NULL; |
| 1459 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1460 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1464 | * Reset an initialized and used SSL context for re-use while retaining |
| 1465 | * all application-set variables, function pointers and data. |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1466 | * |
| 1467 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 1468 | * (Use when a DTLS client reconnects from the same port.) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1469 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1470 | void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl, |
| 1471 | int partial) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1472 | { |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1473 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1474 | size_t in_buf_len = ssl->in_buf_len; |
| 1475 | size_t out_buf_len = ssl->out_buf_len; |
| 1476 | #else |
| 1477 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1478 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 1479 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1480 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1481 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C) |
| 1482 | partial = 0; |
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 1483 | #endif |
| 1484 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1485 | /* Cancel any possibly running timer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1486 | mbedtls_ssl_set_timer(ssl, 0); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1487 | |
Deomid rojer Ryabkov | 3dfe75e | 2025-01-26 10:43:42 +0200 | [diff] [blame] | 1488 | mbedtls_ssl_reset_in_pointers(ssl); |
| 1489 | mbedtls_ssl_reset_out_pointers(ssl); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1490 | |
| 1491 | /* Reset incoming message parsing */ |
| 1492 | ssl->in_offt = NULL; |
| 1493 | ssl->nb_zero = 0; |
| 1494 | ssl->in_msgtype = 0; |
| 1495 | ssl->in_msglen = 0; |
| 1496 | ssl->in_hslen = 0; |
| 1497 | ssl->keep_current_message = 0; |
| 1498 | ssl->transform_in = NULL; |
Deomid rojer Ryabkov | ac2cf1f | 2024-03-10 02:11:03 +0000 | [diff] [blame] | 1499 | ssl->in_hshdr = NULL; |
| 1500 | ssl->in_hsfraglen = 0; |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1501 | |
| 1502 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1503 | ssl->next_record_offset = 0; |
| 1504 | ssl->in_epoch = 0; |
| 1505 | #endif |
| 1506 | |
| 1507 | /* Keep current datagram if partial == 1 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1508 | if (partial == 0) { |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1509 | ssl->in_left = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1510 | memset(ssl->in_buf, 0, in_buf_len); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1511 | } |
| 1512 | |
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 1513 | ssl->send_alert = 0; |
| 1514 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1515 | /* Reset outgoing message writing */ |
| 1516 | ssl->out_msgtype = 0; |
| 1517 | ssl->out_msglen = 0; |
| 1518 | ssl->out_left = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1519 | memset(ssl->out_buf, 0, out_buf_len); |
| 1520 | memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr)); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1521 | ssl->transform_out = NULL; |
| 1522 | |
| 1523 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1524 | mbedtls_ssl_dtls_replay_reset(ssl); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1525 | #endif |
| 1526 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1527 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1528 | if (ssl->transform) { |
| 1529 | mbedtls_ssl_transform_free(ssl->transform); |
| 1530 | mbedtls_free(ssl->transform); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1531 | ssl->transform = NULL; |
| 1532 | } |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1533 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 1534 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1535 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1536 | mbedtls_ssl_transform_free(ssl->transform_application); |
| 1537 | mbedtls_free(ssl->transform_application); |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1538 | ssl->transform_application = NULL; |
| 1539 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1540 | if (ssl->handshake != NULL) { |
Jerry Yu | 3d9b590 | 2022-11-04 14:07:25 +0800 | [diff] [blame] | 1541 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1542 | mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata); |
| 1543 | mbedtls_free(ssl->handshake->transform_earlydata); |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1544 | ssl->handshake->transform_earlydata = NULL; |
Jerry Yu | 3d9b590 | 2022-11-04 14:07:25 +0800 | [diff] [blame] | 1545 | #endif |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1546 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1547 | mbedtls_ssl_transform_free(ssl->handshake->transform_handshake); |
| 1548 | mbedtls_free(ssl->handshake->transform_handshake); |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1549 | ssl->handshake->transform_handshake = NULL; |
| 1550 | } |
| 1551 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1552 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1553 | } |
| 1554 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1555 | int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial) |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1556 | { |
| 1557 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1558 | |
| 1559 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
Ronald Cron | 195c0bc | 2024-02-08 08:51:20 +0100 | [diff] [blame] | 1560 | ssl->tls_version = ssl->conf->max_tls_version; |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1562 | mbedtls_ssl_session_reset_msg_layer(ssl, partial); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1563 | |
| 1564 | /* Reset renegotiation state */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1565 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1566 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1567 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1568 | |
| 1569 | ssl->verify_data_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1570 | memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN); |
| 1571 | 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] | 1572 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1573 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1574 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1575 | ssl->session_in = NULL; |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 1576 | ssl->session_out = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1577 | if (ssl->session) { |
| 1578 | mbedtls_ssl_session_free(ssl->session); |
| 1579 | mbedtls_free(ssl->session); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1580 | ssl->session = NULL; |
| 1581 | } |
| 1582 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1583 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 1584 | ssl->alpn_chosen = NULL; |
| 1585 | #endif |
| 1586 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1587 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 1588 | int free_cli_id = 1; |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1589 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1590 | free_cli_id = (partial == 0); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1591 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1592 | if (free_cli_id) { |
| 1593 | mbedtls_free(ssl->cli_id); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1594 | ssl->cli_id = NULL; |
| 1595 | ssl->cli_id_len = 0; |
| 1596 | } |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 1597 | #endif |
| 1598 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1599 | if ((ret = ssl_handshake_init(ssl)) != 0) { |
| 1600 | return ret; |
| 1601 | } |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1602 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1603 | return 0; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 1606 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1607 | * Reset an initialized and used SSL context for re-use while retaining |
| 1608 | * all application-set variables, function pointers and data. |
| 1609 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1610 | int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1611 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1612 | return mbedtls_ssl_session_reset_int(ssl, 0); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1616 | * SSL set accessors |
| 1617 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1618 | void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1619 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1620 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1621 | } |
| 1622 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1623 | 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] | 1624 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1625 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 1626 | } |
| 1627 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1628 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1629 | 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] | 1630 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1631 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 1632 | } |
| 1633 | #endif |
| 1634 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1635 | 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] | 1636 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1637 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1638 | } |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1639 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1640 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1642 | void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl, |
| 1643 | unsigned allow_packing) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1644 | { |
| 1645 | ssl->disable_datagram_packing = !allow_packing; |
| 1646 | } |
| 1647 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1648 | void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf, |
| 1649 | uint32_t min, uint32_t max) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1650 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1651 | conf->hs_timeout_min = min; |
| 1652 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1653 | } |
| 1654 | #endif |
| 1655 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1656 | void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1657 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1658 | conf->authmode = authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1661 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1662 | void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf, |
| 1663 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 1664 | void *p_vrfy) |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1665 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1666 | conf->f_vrfy = f_vrfy; |
| 1667 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1668 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1669 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1670 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1671 | void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf, |
| 1672 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1673 | void *p_rng) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1674 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1675 | conf->f_rng = f_rng; |
| 1676 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1679 | void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf, |
| 1680 | void (*f_dbg)(void *, int, const char *, int, const char *), |
| 1681 | void *p_dbg) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1682 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1683 | conf->f_dbg = f_dbg; |
| 1684 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1687 | void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl, |
| 1688 | void *p_bio, |
| 1689 | mbedtls_ssl_send_t *f_send, |
| 1690 | mbedtls_ssl_recv_t *f_recv, |
| 1691 | mbedtls_ssl_recv_timeout_t *f_recv_timeout) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1692 | { |
| 1693 | ssl->p_bio = p_bio; |
| 1694 | ssl->f_send = f_send; |
| 1695 | ssl->f_recv = f_recv; |
| 1696 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 1697 | } |
| 1698 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 1699 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1700 | 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] | 1701 | { |
| 1702 | ssl->mtu = mtu; |
| 1703 | } |
| 1704 | #endif |
| 1705 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1706 | 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] | 1707 | { |
| 1708 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1709 | } |
| 1710 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1711 | void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl, |
| 1712 | void *p_timer, |
| 1713 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 1714 | mbedtls_ssl_get_timer_t *f_get_timer) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1715 | { |
| 1716 | ssl->p_timer = p_timer; |
| 1717 | ssl->f_set_timer = f_set_timer; |
| 1718 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1719 | |
| 1720 | /* Make sure we start with no timer running */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1721 | mbedtls_ssl_set_timer(ssl, 0); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1722 | } |
| 1723 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1724 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1725 | void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf, |
| 1726 | void *p_cache, |
| 1727 | mbedtls_ssl_cache_get_t *f_get_cache, |
| 1728 | mbedtls_ssl_cache_set_t *f_set_cache) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1729 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 1730 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1731 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1732 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1733 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1734 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1735 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1736 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1737 | 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] | 1738 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1739 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1740 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1741 | if (ssl == NULL || |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1742 | session == NULL || |
| 1743 | ssl->session_negotiate == NULL || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1744 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) { |
| 1745 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1746 | } |
| 1747 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1748 | if (ssl->handshake->resume == 1) { |
| 1749 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 1750 | } |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 1751 | |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1752 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1753 | if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Ronald Cron | 233fcaa | 2024-04-04 14:05:21 +0200 | [diff] [blame] | 1754 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1755 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1756 | mbedtls_ssl_ciphersuite_from_id(session->ciphersuite); |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1757 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1758 | if (mbedtls_ssl_validate_ciphersuite( |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1759 | ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1760 | MBEDTLS_SSL_VERSION_TLS1_3) != 0) { |
| 1761 | MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.", |
| 1762 | session->ciphersuite)); |
| 1763 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1764 | } |
Ronald Cron | 233fcaa | 2024-04-04 14:05:21 +0200 | [diff] [blame] | 1765 | #else |
| 1766 | /* |
| 1767 | * If session tickets are not enabled, it is not possible to resume a |
| 1768 | * TLS 1.3 session, thus do not make any change to the SSL context in |
| 1769 | * the first place. |
| 1770 | */ |
| 1771 | return 0; |
| 1772 | #endif |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1773 | } |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1774 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1775 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1776 | if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate, |
| 1777 | session)) != 0) { |
| 1778 | return ret; |
| 1779 | } |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1780 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1781 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1782 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1783 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1784 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1785 | #endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1786 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1787 | void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf, |
| 1788 | const int *ciphersuites) |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1789 | { |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 1790 | conf->ciphersuite_list = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1793 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1794 | void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf, |
| 1795 | const int kex_modes) |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1796 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1797 | 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] | 1798 | } |
Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame] | 1799 | |
| 1800 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
Yanray Wang | d5ed36f | 2023-11-07 11:40:43 +0800 | [diff] [blame] | 1801 | void mbedtls_ssl_conf_early_data(mbedtls_ssl_config *conf, |
| 1802 | int early_data_enabled) |
Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame] | 1803 | { |
| 1804 | conf->early_data_enabled = early_data_enabled; |
| 1805 | } |
Jerry Yu | cc4e007 | 2022-11-22 17:22:22 +0800 | [diff] [blame] | 1806 | |
| 1807 | #if defined(MBEDTLS_SSL_SRV_C) |
Yanray Wang | 0751761 | 2023-11-07 11:47:36 +0800 | [diff] [blame] | 1808 | void mbedtls_ssl_conf_max_early_data_size( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1809 | mbedtls_ssl_config *conf, uint32_t max_early_data_size) |
Jerry Yu | cc4e007 | 2022-11-22 17:22:22 +0800 | [diff] [blame] | 1810 | { |
Jerry Yu | 39da985 | 2022-12-06 16:58:36 +0800 | [diff] [blame] | 1811 | conf->max_early_data_size = max_early_data_size; |
Jerry Yu | cc4e007 | 2022-11-22 17:22:22 +0800 | [diff] [blame] | 1812 | } |
| 1813 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 1814 | |
Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame] | 1815 | #endif /* MBEDTLS_SSL_EARLY_DATA */ |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1816 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1817 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1818 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1819 | void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf, |
| 1820 | const mbedtls_x509_crt_profile *profile) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1821 | { |
| 1822 | conf->cert_profile = profile; |
| 1823 | } |
| 1824 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1825 | static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert) |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1826 | { |
| 1827 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
| 1828 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1829 | while (cur != NULL) { |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1830 | next = cur->next; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1831 | mbedtls_free(cur); |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1832 | cur = next; |
| 1833 | } |
| 1834 | } |
| 1835 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1836 | /* Append a new keycert entry to a (possibly empty) list */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1837 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1838 | static int ssl_append_key_cert(mbedtls_ssl_key_cert **head, |
| 1839 | mbedtls_x509_crt *cert, |
| 1840 | mbedtls_pk_context *key) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1841 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1842 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1843 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1844 | if (cert == NULL) { |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1845 | /* Free list if cert is null */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1846 | ssl_key_cert_free(*head); |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1847 | *head = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1848 | return 0; |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1849 | } |
| 1850 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1851 | new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert)); |
| 1852 | if (new_cert == NULL) { |
| 1853 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 1854 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1855 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1856 | new_cert->cert = cert; |
| 1857 | new_cert->key = key; |
| 1858 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1859 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1860 | /* Update head if the list was null, else add to the end */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1861 | if (*head == NULL) { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1862 | *head = new_cert; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1863 | } else { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1864 | mbedtls_ssl_key_cert *cur = *head; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1865 | while (cur->next != NULL) { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1866 | cur = cur->next; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1867 | } |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1868 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1869 | } |
| 1870 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1871 | return 0; |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1872 | } |
| 1873 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1874 | int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1875 | mbedtls_x509_crt *own_cert, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1876 | mbedtls_pk_context *pk_key) |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1877 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1878 | 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] | 1879 | } |
| 1880 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1881 | void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1882 | mbedtls_x509_crt *ca_chain, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1883 | mbedtls_x509_crl *ca_crl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1884 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1885 | conf->ca_chain = ca_chain; |
| 1886 | conf->ca_crl = ca_crl; |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1887 | |
| 1888 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1889 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1890 | * cannot be used together. */ |
| 1891 | conf->f_ca_cb = NULL; |
| 1892 | conf->p_ca_cb = NULL; |
| 1893 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1894 | } |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1895 | |
| 1896 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1897 | void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf, |
| 1898 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
| 1899 | void *p_ca_cb) |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1900 | { |
| 1901 | conf->f_ca_cb = f_ca_cb; |
| 1902 | conf->p_ca_cb = p_ca_cb; |
| 1903 | |
| 1904 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1905 | * cannot be used together. */ |
| 1906 | conf->ca_chain = NULL; |
| 1907 | conf->ca_crl = NULL; |
| 1908 | } |
| 1909 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1910 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 1911 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1912 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1913 | const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl, |
| 1914 | size_t *name_len) |
Glenn Strauss | 6989407 | 2022-01-24 12:58:00 -0500 | [diff] [blame] | 1915 | { |
| 1916 | *name_len = ssl->handshake->sni_name_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1917 | return ssl->handshake->sni_name; |
Glenn Strauss | 6989407 | 2022-01-24 12:58:00 -0500 | [diff] [blame] | 1918 | } |
| 1919 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1920 | int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl, |
| 1921 | mbedtls_x509_crt *own_cert, |
| 1922 | mbedtls_pk_context *pk_key) |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1923 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1924 | return ssl_append_key_cert(&ssl->handshake->sni_key_cert, |
| 1925 | own_cert, pk_key); |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1926 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 1927 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1928 | void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl, |
| 1929 | mbedtls_x509_crt *ca_chain, |
| 1930 | mbedtls_x509_crl *ca_crl) |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 1931 | { |
| 1932 | ssl->handshake->sni_ca_chain = ca_chain; |
| 1933 | ssl->handshake->sni_ca_crl = ca_crl; |
| 1934 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1935 | |
Glenn Strauss | 999ef70 | 2022-03-11 01:37:23 -0500 | [diff] [blame] | 1936 | #if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1937 | void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl, |
| 1938 | const mbedtls_x509_crt *crt) |
Glenn Strauss | 999ef70 | 2022-03-11 01:37:23 -0500 | [diff] [blame] | 1939 | { |
| 1940 | ssl->handshake->dn_hints = crt; |
| 1941 | } |
| 1942 | #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ |
| 1943 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1944 | void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl, |
| 1945 | int authmode) |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1946 | { |
| 1947 | ssl->handshake->sni_authmode = authmode; |
| 1948 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1949 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 1950 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1951 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1952 | void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl, |
| 1953 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 1954 | void *p_vrfy) |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1955 | { |
| 1956 | ssl->f_vrfy = f_vrfy; |
| 1957 | ssl->p_vrfy = p_vrfy; |
| 1958 | } |
| 1959 | #endif |
| 1960 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1961 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1962 | |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1963 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemek Stekiel | fde1128 | 2023-03-13 16:06:09 +0100 | [diff] [blame] | 1964 | static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' }; |
| 1965 | static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' }; |
| 1966 | |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1967 | static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1968 | mbedtls_ssl_context *ssl, |
| 1969 | mbedtls_svc_key_id_t pwd) |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1970 | { |
| 1971 | psa_status_t status; |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1972 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
Przemek Stekiel | fde1128 | 2023-03-13 16:06:09 +0100 | [diff] [blame] | 1973 | const uint8_t *user = NULL; |
Przemek Stekiel | 4cd2031 | 2023-03-01 11:11:28 +0100 | [diff] [blame] | 1974 | size_t user_len = 0; |
Przemek Stekiel | fde1128 | 2023-03-13 16:06:09 +0100 | [diff] [blame] | 1975 | const uint8_t *peer = NULL; |
Przemek Stekiel | 4cd2031 | 2023-03-01 11:11:28 +0100 | [diff] [blame] | 1976 | size_t peer_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1977 | psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE); |
| 1978 | psa_pake_cs_set_primitive(&cipher_suite, |
| 1979 | PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, |
| 1980 | PSA_ECC_FAMILY_SECP_R1, |
| 1981 | 256)); |
| 1982 | psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256); |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1983 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1984 | status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite); |
| 1985 | if (status != PSA_SUCCESS) { |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1986 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1987 | } |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1988 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1989 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Przemek Stekiel | fde1128 | 2023-03-13 16:06:09 +0100 | [diff] [blame] | 1990 | user = jpake_server_id; |
| 1991 | user_len = sizeof(jpake_server_id); |
| 1992 | peer = jpake_client_id; |
| 1993 | peer_len = sizeof(jpake_client_id); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1994 | } else { |
Przemek Stekiel | fde1128 | 2023-03-13 16:06:09 +0100 | [diff] [blame] | 1995 | user = jpake_client_id; |
| 1996 | user_len = sizeof(jpake_client_id); |
| 1997 | peer = jpake_server_id; |
| 1998 | peer_len = sizeof(jpake_server_id); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1999 | } |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 2000 | |
Przemek Stekiel | 4cd2031 | 2023-03-01 11:11:28 +0100 | [diff] [blame] | 2001 | status = psa_pake_set_user(&ssl->handshake->psa_pake_ctx, user, user_len); |
| 2002 | if (status != PSA_SUCCESS) { |
| 2003 | return status; |
| 2004 | } |
| 2005 | |
| 2006 | 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] | 2007 | if (status != PSA_SUCCESS) { |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 2008 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2009 | } |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 2010 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2011 | status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd); |
| 2012 | if (status != PSA_SUCCESS) { |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 2013 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2014 | } |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 2015 | |
| 2016 | ssl->handshake->psa_pake_ctx_is_ok = 1; |
| 2017 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2018 | return PSA_SUCCESS; |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 2019 | } |
| 2020 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2021 | int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl, |
| 2022 | const unsigned char *pw, |
| 2023 | size_t pw_len) |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 2024 | { |
| 2025 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2026 | psa_status_t status; |
| 2027 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2028 | if (ssl->handshake == NULL || ssl->conf == NULL) { |
| 2029 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 2030 | } |
| 2031 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2032 | /* Empty password is not valid */ |
| 2033 | if ((pw == NULL) || (pw_len == 0)) { |
| 2034 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2035 | } |
| 2036 | |
| 2037 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 2038 | psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE); |
| 2039 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 2040 | |
| 2041 | status = psa_import_key(&attributes, pw, pw_len, |
| 2042 | &ssl->handshake->psa_pake_password); |
| 2043 | if (status != PSA_SUCCESS) { |
| 2044 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 2045 | } |
| 2046 | |
| 2047 | status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, |
| 2048 | ssl->handshake->psa_pake_password); |
| 2049 | if (status != PSA_SUCCESS) { |
| 2050 | psa_destroy_key(ssl->handshake->psa_pake_password); |
| 2051 | psa_pake_abort(&ssl->handshake->psa_pake_ctx); |
| 2052 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 2053 | } |
| 2054 | |
| 2055 | return 0; |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 2056 | } |
Valerio Setti | a9a97dc | 2022-11-28 18:26:16 +0100 | [diff] [blame] | 2057 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2058 | int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl, |
| 2059 | mbedtls_svc_key_id_t pwd) |
Valerio Setti | a9a97dc | 2022-11-28 18:26:16 +0100 | [diff] [blame] | 2060 | { |
Valerio Setti | a9a97dc | 2022-11-28 18:26:16 +0100 | [diff] [blame] | 2061 | psa_status_t status; |
| 2062 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2063 | if (ssl->handshake == NULL || ssl->conf == NULL) { |
| 2064 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 2065 | } |
| 2066 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2067 | if (mbedtls_svc_key_id_is_null(pwd)) { |
| 2068 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2069 | } |
| 2070 | |
| 2071 | status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd); |
| 2072 | if (status != PSA_SUCCESS) { |
| 2073 | psa_pake_abort(&ssl->handshake->psa_pake_ctx); |
| 2074 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 2075 | } |
| 2076 | |
| 2077 | return 0; |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 2078 | } |
| 2079 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2080 | int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl, |
| 2081 | const unsigned char *pw, |
| 2082 | size_t pw_len) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2083 | { |
| 2084 | mbedtls_ecjpake_role role; |
| 2085 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2086 | if (ssl->handshake == NULL || ssl->conf == NULL) { |
| 2087 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2088 | } |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2089 | |
Valerio Setti | c689ed8 | 2022-12-07 14:40:38 +0100 | [diff] [blame] | 2090 | /* Empty password is not valid */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2091 | if ((pw == NULL) || (pw_len == 0)) { |
| 2092 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2093 | } |
Valerio Setti | c689ed8 | 2022-12-07 14:40:38 +0100 | [diff] [blame] | 2094 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2095 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2096 | role = MBEDTLS_ECJPAKE_SERVER; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2097 | } else { |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2098 | role = MBEDTLS_ECJPAKE_CLIENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2099 | } |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2100 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2101 | return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx, |
| 2102 | role, |
| 2103 | MBEDTLS_MD_SHA256, |
| 2104 | MBEDTLS_ECP_DP_SECP256R1, |
| 2105 | pw, pw_len); |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2106 | } |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 2107 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 2108 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2109 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 2110 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2111 | int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2112 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2113 | if (conf->psk_identity == NULL || |
| 2114 | conf->psk_identity_len == 0) { |
| 2115 | return 0; |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 2116 | } |
| 2117 | |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2118 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2119 | if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) { |
| 2120 | return 1; |
| 2121 | } |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2122 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 2123 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2124 | if (conf->psk != NULL && conf->psk_len != 0) { |
| 2125 | return 1; |
| 2126 | } |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2127 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2128 | return 0; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2129 | } |
| 2130 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2131 | static void ssl_conf_remove_psk(mbedtls_ssl_config *conf) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2132 | { |
| 2133 | /* Remove reference to existing PSK, if any. */ |
| 2134 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2135 | if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) { |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2136 | /* The maintenance of the PSK key slot is the |
| 2137 | * user's responsibility. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2138 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2139 | } |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2140 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2141 | if (conf->psk != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 2142 | mbedtls_zeroize_and_free(conf->psk, conf->psk_len); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2143 | conf->psk = NULL; |
| 2144 | conf->psk_len = 0; |
| 2145 | } |
| 2146 | |
| 2147 | /* Remove reference to PSK identity, if any. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2148 | if (conf->psk_identity != NULL) { |
| 2149 | mbedtls_free(conf->psk_identity); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2150 | conf->psk_identity = NULL; |
| 2151 | conf->psk_identity_len = 0; |
| 2152 | } |
| 2153 | } |
| 2154 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2155 | /* This function assumes that PSK identity in the SSL config is unset. |
| 2156 | * It checks that the provided identity is well-formed and attempts |
| 2157 | * to make a copy of it in the SSL config. |
| 2158 | * On failure, the PSK identity in the config remains unset. */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 2159 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2160 | static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf, |
| 2161 | unsigned char const *psk_identity, |
| 2162 | size_t psk_identity_len) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2163 | { |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 2164 | /* Identity len will be encoded on two bytes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2165 | if (psk_identity == NULL || |
Ronald Cron | 2a87e9b | 2022-10-19 10:55:26 +0200 | [diff] [blame] | 2166 | psk_identity_len == 0 || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2167 | (psk_identity_len >> 16) != 0 || |
| 2168 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) { |
| 2169 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 2170 | } |
| 2171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2172 | conf->psk_identity = mbedtls_calloc(1, psk_identity_len); |
| 2173 | if (conf->psk_identity == NULL) { |
| 2174 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2175 | } |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 2176 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 2177 | conf->psk_identity_len = psk_identity_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2178 | memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len); |
Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 2179 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2180 | return 0; |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 2181 | } |
| 2182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2183 | int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf, |
| 2184 | const unsigned char *psk, size_t psk_len, |
| 2185 | const unsigned char *psk_identity, size_t psk_identity_len) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2186 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2187 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2188 | |
| 2189 | /* We currently only support one PSK, raw or opaque. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2190 | if (mbedtls_ssl_conf_has_static_psk(conf)) { |
| 2191 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 2192 | } |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2193 | |
| 2194 | /* Check and set raw PSK */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2195 | if (psk == NULL) { |
| 2196 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2197 | } |
| 2198 | if (psk_len == 0) { |
| 2199 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2200 | } |
| 2201 | if (psk_len > MBEDTLS_PSK_MAX_LEN) { |
| 2202 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2203 | } |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 2204 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2205 | if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) { |
| 2206 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2207 | } |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2208 | conf->psk_len = psk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2209 | memcpy(conf->psk, psk, conf->psk_len); |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2210 | |
| 2211 | /* Check and set PSK Identity */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2212 | ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len); |
| 2213 | if (ret != 0) { |
| 2214 | ssl_conf_remove_psk(conf); |
| 2215 | } |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2216 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2217 | return ret; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2218 | } |
| 2219 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2220 | static void ssl_remove_psk(mbedtls_ssl_context *ssl) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2221 | { |
| 2222 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2223 | if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2224 | /* The maintenance of the external PSK key slot is the |
| 2225 | * user's responsibility. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2226 | if (ssl->handshake->psk_opaque_is_internal) { |
| 2227 | psa_destroy_key(ssl->handshake->psk_opaque); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2228 | ssl->handshake->psk_opaque_is_internal = 0; |
| 2229 | } |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2230 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2231 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 2232 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2233 | if (ssl->handshake->psk != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 2234 | mbedtls_zeroize_and_free(ssl->handshake->psk, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2235 | ssl->handshake->psk_len); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2236 | ssl->handshake->psk_len = 0; |
lhuang04 | 54adeab | 2024-06-10 12:16:29 -0700 | [diff] [blame] | 2237 | ssl->handshake->psk = NULL; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2238 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 2239 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2240 | } |
| 2241 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2242 | int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl, |
| 2243 | const unsigned char *psk, size_t psk_len) |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2244 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2245 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2246 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
Jerry Yu | 5d01c05 | 2022-08-17 10:18:10 +0800 | [diff] [blame] | 2247 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 24b8c81 | 2022-08-20 19:06:56 +0800 | [diff] [blame] | 2248 | psa_algorithm_t alg = PSA_ALG_NONE; |
Jerry Yu | 5d01c05 | 2022-08-17 10:18:10 +0800 | [diff] [blame] | 2249 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2250 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2252 | if (psk == NULL || ssl->handshake == NULL) { |
| 2253 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2254 | } |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2256 | if (psk_len > MBEDTLS_PSK_MAX_LEN) { |
| 2257 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2258 | } |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2259 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2260 | ssl_remove_psk(ssl); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2261 | |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2262 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 2263 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2264 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) { |
| 2265 | if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) { |
| 2266 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
| 2267 | } else { |
| 2268 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 2269 | } |
| 2270 | psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE); |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 2271 | } |
| 2272 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2273 | |
Jerry Yu | 568ec25 | 2022-07-22 21:27:34 +0800 | [diff] [blame] | 2274 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2275 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
| 2276 | alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH); |
| 2277 | psa_set_key_usage_flags(&key_attributes, |
| 2278 | PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT); |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 2279 | } |
| 2280 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 2281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2282 | psa_set_key_algorithm(&key_attributes, alg); |
| 2283 | psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2284 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2285 | status = psa_import_key(&key_attributes, psk, psk_len, &key); |
| 2286 | if (status != PSA_SUCCESS) { |
| 2287 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 2288 | } |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2289 | |
| 2290 | /* Allow calling psa_destroy_key() on psk remove */ |
| 2291 | ssl->handshake->psk_opaque_is_internal = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2292 | return mbedtls_ssl_set_hs_psk_opaque(ssl, key); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2293 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2294 | if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) { |
| 2295 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2296 | } |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2297 | |
| 2298 | ssl->handshake->psk_len = psk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2299 | memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2300 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2301 | return 0; |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2302 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2303 | } |
| 2304 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2305 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2306 | int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf, |
| 2307 | mbedtls_svc_key_id_t psk, |
| 2308 | const unsigned char *psk_identity, |
| 2309 | size_t psk_identity_len) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2310 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2311 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2312 | |
| 2313 | /* We currently only support one PSK, raw or opaque. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2314 | if (mbedtls_ssl_conf_has_static_psk(conf)) { |
| 2315 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 2316 | } |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2317 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2318 | /* Check and set opaque PSK */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2319 | if (mbedtls_svc_key_id_is_null(psk)) { |
| 2320 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2321 | } |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2322 | conf->psk_opaque = psk; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2323 | |
| 2324 | /* Check and set PSK Identity */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2325 | ret = ssl_conf_set_psk_identity(conf, psk_identity, |
| 2326 | psk_identity_len); |
| 2327 | if (ret != 0) { |
| 2328 | ssl_conf_remove_psk(conf); |
| 2329 | } |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2330 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2331 | return ret; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2332 | } |
| 2333 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2334 | int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl, |
| 2335 | mbedtls_svc_key_id_t psk) |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2336 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2337 | if ((mbedtls_svc_key_id_is_null(psk)) || |
| 2338 | (ssl->handshake == NULL)) { |
| 2339 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2340 | } |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2341 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2342 | ssl_remove_psk(ssl); |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2343 | ssl->handshake->psk_opaque = psk; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2344 | return 0; |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2345 | } |
| 2346 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2347 | |
Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 2348 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2349 | void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf, |
| 2350 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
| 2351 | size_t), |
| 2352 | void *p_psk) |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2353 | { |
| 2354 | conf->f_psk = f_psk; |
| 2355 | conf->p_psk = p_psk; |
| 2356 | } |
Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 2357 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 2358 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 2359 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2360 | |
| 2361 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2362 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2363 | psa_algorithm_t alg) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2364 | { |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2365 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2366 | if (alg == PSA_ALG_CBC_NO_PADDING) { |
| 2367 | return MBEDTLS_SSL_MODE_CBC; |
| 2368 | } |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2369 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2370 | if (PSA_ALG_IS_AEAD(alg)) { |
| 2371 | return MBEDTLS_SSL_MODE_AEAD; |
| 2372 | } |
| 2373 | return MBEDTLS_SSL_MODE_STREAM; |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2374 | } |
| 2375 | |
| 2376 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2377 | |
| 2378 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2379 | mbedtls_cipher_mode_t mode) |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2380 | { |
| 2381 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2382 | if (mode == MBEDTLS_MODE_CBC) { |
| 2383 | return MBEDTLS_SSL_MODE_CBC; |
| 2384 | } |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2385 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 2386 | |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2387 | #if defined(MBEDTLS_GCM_C) || \ |
| 2388 | defined(MBEDTLS_CCM_C) || \ |
| 2389 | defined(MBEDTLS_CHACHAPOLY_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2390 | if (mode == MBEDTLS_MODE_GCM || |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2391 | mode == MBEDTLS_MODE_CCM || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2392 | mode == MBEDTLS_MODE_CHACHAPOLY) { |
| 2393 | return MBEDTLS_SSL_MODE_AEAD; |
| 2394 | } |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2395 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2396 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2397 | return MBEDTLS_SSL_MODE_STREAM; |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2398 | } |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2399 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2400 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2401 | static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode( |
| 2402 | mbedtls_ssl_mode_t base_mode, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2403 | int encrypt_then_mac) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2404 | { |
| 2405 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2406 | if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED && |
| 2407 | base_mode == MBEDTLS_SSL_MODE_CBC) { |
| 2408 | return MBEDTLS_SSL_MODE_CBC_ETM; |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2409 | } |
| 2410 | #else |
| 2411 | (void) encrypt_then_mac; |
| 2412 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2413 | return base_mode; |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2414 | } |
| 2415 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 2416 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2417 | const mbedtls_ssl_transform *transform) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2418 | { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2419 | mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode( |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2420 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2421 | transform->psa_alg |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2422 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2423 | mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2424 | #endif |
| 2425 | ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2426 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2427 | int encrypt_then_mac = 0; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2428 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2429 | encrypt_then_mac = transform->encrypt_then_mac; |
| 2430 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2431 | return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2432 | } |
| 2433 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 2434 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2435 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2436 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2437 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2438 | const mbedtls_ssl_ciphersuite_t *suite) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2439 | { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2440 | mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM; |
| 2441 | |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2442 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2443 | psa_status_t status; |
| 2444 | psa_algorithm_t alg; |
| 2445 | psa_key_type_t type; |
| 2446 | size_t size; |
Dave Rodgman | 2eab462 | 2023-10-05 13:30:37 +0100 | [diff] [blame] | 2447 | status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) suite->cipher, |
| 2448 | 0, &alg, &type, &size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2449 | if (status == PSA_SUCCESS) { |
| 2450 | base_mode = mbedtls_ssl_get_base_mode(alg); |
| 2451 | } |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2452 | #else |
| 2453 | const mbedtls_cipher_info_t *cipher = |
Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 2454 | mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) suite->cipher); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2455 | if (cipher != NULL) { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2456 | base_mode = |
| 2457 | mbedtls_ssl_get_base_mode( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2458 | mbedtls_cipher_info_get_mode(cipher)); |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2459 | } |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2460 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2461 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2462 | #if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 2463 | int encrypt_then_mac = 0; |
| 2464 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2465 | return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2466 | } |
| 2467 | |
Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 2468 | #if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2469 | |
Ronald Cron | 8b592d2 | 2024-11-12 18:08:25 +0100 | [diff] [blame] | 2470 | const mbedtls_error_pair_t psa_to_ssl_errors[] = |
| 2471 | { |
| 2472 | { PSA_SUCCESS, 0 }, |
| 2473 | { PSA_ERROR_INSUFFICIENT_MEMORY, MBEDTLS_ERR_SSL_ALLOC_FAILED }, |
| 2474 | { PSA_ERROR_NOT_SUPPORTED, MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE }, |
| 2475 | { PSA_ERROR_INVALID_SIGNATURE, MBEDTLS_ERR_SSL_INVALID_MAC }, |
| 2476 | { PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_SSL_BAD_INPUT_DATA }, |
| 2477 | { PSA_ERROR_BAD_STATE, MBEDTLS_ERR_SSL_INTERNAL_ERROR }, |
| 2478 | { PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL } |
| 2479 | }; |
| 2480 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2481 | psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type, |
| 2482 | size_t taglen, |
| 2483 | psa_algorithm_t *alg, |
| 2484 | psa_key_type_t *key_type, |
| 2485 | size_t *key_size) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2486 | { |
Elena Uziunaite | c256172 | 2024-07-05 11:37:33 +0100 | [diff] [blame] | 2487 | #if !defined(PSA_WANT_ALG_CCM) |
Yanray Wang | 19e4dc8 | 2023-11-16 18:02:05 +0800 | [diff] [blame] | 2488 | (void) taglen; |
| 2489 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2490 | switch (mbedtls_cipher_type) { |
Elena Uziunaite | 74342c7 | 2024-07-05 11:31:29 +0100 | [diff] [blame] | 2491 | #if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_CBC_NO_PADDING) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2492 | case MBEDTLS_CIPHER_AES_128_CBC: |
| 2493 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2494 | *key_type = PSA_KEY_TYPE_AES; |
| 2495 | *key_size = 128; |
| 2496 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2497 | #endif |
Elena Uziunaite | c256172 | 2024-07-05 11:37:33 +0100 | [diff] [blame] | 2498 | #if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2499 | case MBEDTLS_CIPHER_AES_128_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2500 | *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] | 2501 | *key_type = PSA_KEY_TYPE_AES; |
| 2502 | *key_size = 128; |
| 2503 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2504 | #endif |
Elena Uziunaite | 83a0d9d | 2024-07-05 11:41:22 +0100 | [diff] [blame] | 2505 | #if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_GCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2506 | case MBEDTLS_CIPHER_AES_128_GCM: |
| 2507 | *alg = PSA_ALG_GCM; |
| 2508 | *key_type = PSA_KEY_TYPE_AES; |
| 2509 | *key_size = 128; |
| 2510 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2511 | #endif |
Elena Uziunaite | c256172 | 2024-07-05 11:37:33 +0100 | [diff] [blame] | 2512 | #if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2513 | case MBEDTLS_CIPHER_AES_192_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2514 | *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] | 2515 | *key_type = PSA_KEY_TYPE_AES; |
| 2516 | *key_size = 192; |
| 2517 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2518 | #endif |
Elena Uziunaite | 83a0d9d | 2024-07-05 11:41:22 +0100 | [diff] [blame] | 2519 | #if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_GCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2520 | case MBEDTLS_CIPHER_AES_192_GCM: |
| 2521 | *alg = PSA_ALG_GCM; |
| 2522 | *key_type = PSA_KEY_TYPE_AES; |
| 2523 | *key_size = 192; |
| 2524 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2525 | #endif |
Elena Uziunaite | 74342c7 | 2024-07-05 11:31:29 +0100 | [diff] [blame] | 2526 | #if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_CBC_NO_PADDING) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2527 | case MBEDTLS_CIPHER_AES_256_CBC: |
| 2528 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2529 | *key_type = PSA_KEY_TYPE_AES; |
| 2530 | *key_size = 256; |
| 2531 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2532 | #endif |
Elena Uziunaite | c256172 | 2024-07-05 11:37:33 +0100 | [diff] [blame] | 2533 | #if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2534 | case MBEDTLS_CIPHER_AES_256_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2535 | *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] | 2536 | *key_type = PSA_KEY_TYPE_AES; |
| 2537 | *key_size = 256; |
| 2538 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2539 | #endif |
Elena Uziunaite | 83a0d9d | 2024-07-05 11:41:22 +0100 | [diff] [blame] | 2540 | #if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_GCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2541 | case MBEDTLS_CIPHER_AES_256_GCM: |
| 2542 | *alg = PSA_ALG_GCM; |
| 2543 | *key_type = PSA_KEY_TYPE_AES; |
| 2544 | *key_size = 256; |
| 2545 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2546 | #endif |
Elena Uziunaite | 51c85a0 | 2024-07-05 11:20:17 +0100 | [diff] [blame] | 2547 | #if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_CBC_NO_PADDING) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2548 | case MBEDTLS_CIPHER_ARIA_128_CBC: |
| 2549 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2550 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2551 | *key_size = 128; |
| 2552 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2553 | #endif |
Elena Uziunaite | 51c85a0 | 2024-07-05 11:20:17 +0100 | [diff] [blame] | 2554 | #if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2555 | case MBEDTLS_CIPHER_ARIA_128_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2556 | *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] | 2557 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2558 | *key_size = 128; |
| 2559 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2560 | #endif |
Elena Uziunaite | 51c85a0 | 2024-07-05 11:20:17 +0100 | [diff] [blame] | 2561 | #if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_GCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2562 | case MBEDTLS_CIPHER_ARIA_128_GCM: |
| 2563 | *alg = PSA_ALG_GCM; |
| 2564 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2565 | *key_size = 128; |
| 2566 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2567 | #endif |
Elena Uziunaite | 51c85a0 | 2024-07-05 11:20:17 +0100 | [diff] [blame] | 2568 | #if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2569 | case MBEDTLS_CIPHER_ARIA_192_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2570 | *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] | 2571 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2572 | *key_size = 192; |
| 2573 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2574 | #endif |
Elena Uziunaite | 51c85a0 | 2024-07-05 11:20:17 +0100 | [diff] [blame] | 2575 | #if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_GCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2576 | case MBEDTLS_CIPHER_ARIA_192_GCM: |
| 2577 | *alg = PSA_ALG_GCM; |
| 2578 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2579 | *key_size = 192; |
| 2580 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2581 | #endif |
Elena Uziunaite | 51c85a0 | 2024-07-05 11:20:17 +0100 | [diff] [blame] | 2582 | #if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_CBC_NO_PADDING) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2583 | case MBEDTLS_CIPHER_ARIA_256_CBC: |
| 2584 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2585 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2586 | *key_size = 256; |
| 2587 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2588 | #endif |
Elena Uziunaite | 51c85a0 | 2024-07-05 11:20:17 +0100 | [diff] [blame] | 2589 | #if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2590 | case MBEDTLS_CIPHER_ARIA_256_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2591 | *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] | 2592 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2593 | *key_size = 256; |
| 2594 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2595 | #endif |
Elena Uziunaite | 51c85a0 | 2024-07-05 11:20:17 +0100 | [diff] [blame] | 2596 | #if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_GCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2597 | case MBEDTLS_CIPHER_ARIA_256_GCM: |
| 2598 | *alg = PSA_ALG_GCM; |
| 2599 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2600 | *key_size = 256; |
| 2601 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2602 | #endif |
Elena Uziunaite | da41b60 | 2024-07-05 11:27:21 +0100 | [diff] [blame] | 2603 | #if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_CBC_NO_PADDING) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2604 | case MBEDTLS_CIPHER_CAMELLIA_128_CBC: |
| 2605 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2606 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2607 | *key_size = 128; |
| 2608 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2609 | #endif |
Elena Uziunaite | da41b60 | 2024-07-05 11:27:21 +0100 | [diff] [blame] | 2610 | #if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2611 | case MBEDTLS_CIPHER_CAMELLIA_128_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2612 | *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] | 2613 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2614 | *key_size = 128; |
| 2615 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2616 | #endif |
Elena Uziunaite | da41b60 | 2024-07-05 11:27:21 +0100 | [diff] [blame] | 2617 | #if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_GCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2618 | case MBEDTLS_CIPHER_CAMELLIA_128_GCM: |
| 2619 | *alg = PSA_ALG_GCM; |
| 2620 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2621 | *key_size = 128; |
| 2622 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2623 | #endif |
Elena Uziunaite | da41b60 | 2024-07-05 11:27:21 +0100 | [diff] [blame] | 2624 | #if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2625 | case MBEDTLS_CIPHER_CAMELLIA_192_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2626 | *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] | 2627 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2628 | *key_size = 192; |
| 2629 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2630 | #endif |
Elena Uziunaite | da41b60 | 2024-07-05 11:27:21 +0100 | [diff] [blame] | 2631 | #if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_GCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2632 | case MBEDTLS_CIPHER_CAMELLIA_192_GCM: |
| 2633 | *alg = PSA_ALG_GCM; |
| 2634 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2635 | *key_size = 192; |
| 2636 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2637 | #endif |
Elena Uziunaite | da41b60 | 2024-07-05 11:27:21 +0100 | [diff] [blame] | 2638 | #if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_CBC_NO_PADDING) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2639 | case MBEDTLS_CIPHER_CAMELLIA_256_CBC: |
| 2640 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2641 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2642 | *key_size = 256; |
| 2643 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2644 | #endif |
Elena Uziunaite | da41b60 | 2024-07-05 11:27:21 +0100 | [diff] [blame] | 2645 | #if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2646 | case MBEDTLS_CIPHER_CAMELLIA_256_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2647 | *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] | 2648 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2649 | *key_size = 256; |
| 2650 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2651 | #endif |
Elena Uziunaite | da41b60 | 2024-07-05 11:27:21 +0100 | [diff] [blame] | 2652 | #if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_GCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2653 | case MBEDTLS_CIPHER_CAMELLIA_256_GCM: |
| 2654 | *alg = PSA_ALG_GCM; |
| 2655 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2656 | *key_size = 256; |
| 2657 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2658 | #endif |
Elena Uziunaite | 5c70c30 | 2024-07-05 11:44:44 +0100 | [diff] [blame] | 2659 | #if defined(PSA_WANT_ALG_CHACHA20_POLY1305) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2660 | case MBEDTLS_CIPHER_CHACHA20_POLY1305: |
| 2661 | *alg = PSA_ALG_CHACHA20_POLY1305; |
| 2662 | *key_type = PSA_KEY_TYPE_CHACHA20; |
| 2663 | *key_size = 256; |
| 2664 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2665 | #endif |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2666 | case MBEDTLS_CIPHER_NULL: |
| 2667 | *alg = MBEDTLS_SSL_NULL_CIPHER; |
| 2668 | *key_type = 0; |
| 2669 | *key_size = 0; |
| 2670 | break; |
| 2671 | default: |
| 2672 | return PSA_ERROR_NOT_SUPPORTED; |
| 2673 | } |
| 2674 | |
| 2675 | return PSA_SUCCESS; |
| 2676 | } |
Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 2677 | #endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2678 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2679 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2680 | int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf, |
| 2681 | const unsigned char *dhm_P, size_t P_len, |
| 2682 | const unsigned char *dhm_G, size_t G_len) |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2683 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2684 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2685 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2686 | mbedtls_mpi_free(&conf->dhm_P); |
| 2687 | mbedtls_mpi_free(&conf->dhm_G); |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2688 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2689 | if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 || |
| 2690 | (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) { |
| 2691 | mbedtls_mpi_free(&conf->dhm_P); |
| 2692 | mbedtls_mpi_free(&conf->dhm_G); |
| 2693 | return ret; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2694 | } |
| 2695 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2696 | return 0; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2697 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2698 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2699 | 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] | 2700 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2701 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
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 | mbedtls_mpi_free(&conf->dhm_P); |
| 2704 | mbedtls_mpi_free(&conf->dhm_G); |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2705 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2706 | if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P, |
| 2707 | &conf->dhm_P)) != 0 || |
| 2708 | (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G, |
| 2709 | &conf->dhm_G)) != 0) { |
| 2710 | mbedtls_mpi_free(&conf->dhm_P); |
| 2711 | mbedtls_mpi_free(&conf->dhm_G); |
| 2712 | return ret; |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 2713 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2714 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2715 | return 0; |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2716 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2717 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2718 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 2719 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 2720 | /* |
| 2721 | * Set the minimum length for Diffie-Hellman parameters |
| 2722 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2723 | void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf, |
| 2724 | unsigned int bitlen) |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 2725 | { |
| 2726 | conf->dhm_min_bitlen = bitlen; |
| 2727 | } |
| 2728 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 2729 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2730 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2731 | #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] | 2732 | /* |
| 2733 | * Set allowed/preferred hashes for handshake signatures |
| 2734 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2735 | void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf, |
| 2736 | const int *hashes) |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 2737 | { |
| 2738 | conf->sig_hashes = hashes; |
| 2739 | } |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2740 | #endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2741 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2742 | /* Configure allowed signature algorithms for handshake */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2743 | void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf, |
| 2744 | const uint16_t *sig_algs) |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2745 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2746 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2747 | conf->sig_hashes = NULL; |
| 2748 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 2749 | conf->sig_algs = sig_algs; |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2750 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2751 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 2752 | |
Manuel Pégourié-Gonnard | f07ce3b | 2023-09-22 11:53:41 +0200 | [diff] [blame] | 2753 | #if defined(MBEDTLS_ECP_C) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2754 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2755 | /* |
| 2756 | * Set the allowed elliptic curves |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2757 | * |
| 2758 | * mbedtls_ssl_setup() takes the provided list |
| 2759 | * and translates it to a list of IANA TLS group identifiers, |
| 2760 | * stored in ssl->handshake->group_list. |
| 2761 | * |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2762 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2763 | void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf, |
| 2764 | const mbedtls_ecp_group_id *curve_list) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2765 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2766 | conf->curve_list = curve_list; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2767 | conf->group_list = NULL; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2768 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2769 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Manuel Pégourié-Gonnard | f07ce3b | 2023-09-22 11:53:41 +0200 | [diff] [blame] | 2770 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2771 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2772 | /* |
| 2773 | * Set the allowed groups |
| 2774 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2775 | void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf, |
| 2776 | const uint16_t *group_list) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2777 | { |
Manuel Pégourié-Gonnard | f07ce3b | 2023-09-22 11:53:41 +0200 | [diff] [blame] | 2778 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2779 | conf->curve_list = NULL; |
| 2780 | #endif |
| 2781 | conf->group_list = group_list; |
| 2782 | } |
| 2783 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2784 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2785 | int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2786 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2787 | /* Initialize to suppress unnecessary compiler warning */ |
| 2788 | size_t hostname_len = 0; |
| 2789 | |
| 2790 | /* Check if new hostname is valid before |
| 2791 | * making any change to current one */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2792 | if (hostname != NULL) { |
| 2793 | hostname_len = strlen(hostname); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2794 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2795 | if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) { |
| 2796 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2797 | } |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2798 | } |
| 2799 | |
| 2800 | /* Now it's clear that we will overwrite the old hostname, |
| 2801 | * so we can free it safely */ |
| 2802 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2803 | if (ssl->hostname != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 2804 | mbedtls_zeroize_and_free(ssl->hostname, strlen(ssl->hostname)); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2805 | } |
| 2806 | |
| 2807 | /* Passing NULL as hostname shall clear the old one */ |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 2808 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2809 | if (hostname == NULL) { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2810 | ssl->hostname = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2811 | } else { |
| 2812 | ssl->hostname = mbedtls_calloc(1, hostname_len + 1); |
| 2813 | if (ssl->hostname == NULL) { |
| 2814 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2815 | } |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2816 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2817 | memcpy(ssl->hostname, hostname, hostname_len); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2818 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2819 | ssl->hostname[hostname_len] = '\0'; |
| 2820 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2821 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2822 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2823 | } |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 2824 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2825 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2826 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2827 | void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf, |
| 2828 | int (*f_sni)(void *, mbedtls_ssl_context *, |
| 2829 | const unsigned char *, size_t), |
| 2830 | void *p_sni) |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2831 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2832 | conf->f_sni = f_sni; |
| 2833 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2834 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2835 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2836 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2837 | #if defined(MBEDTLS_SSL_ALPN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2838 | 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] | 2839 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2840 | size_t cur_len, tot_len; |
| 2841 | const char **p; |
| 2842 | |
| 2843 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 2844 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 2845 | * MUST NOT be truncated." |
| 2846 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2847 | */ |
| 2848 | tot_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2849 | for (p = protos; *p != NULL; p++) { |
| 2850 | cur_len = strlen(*p); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2851 | tot_len += cur_len; |
| 2852 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2853 | if ((cur_len == 0) || |
| 2854 | (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) || |
| 2855 | (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) { |
| 2856 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2857 | } |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2858 | } |
| 2859 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2860 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2861 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2862 | return 0; |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2863 | } |
| 2864 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2865 | 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] | 2866 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2867 | return ssl->alpn_chosen; |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2868 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2869 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2870 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2871 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2872 | void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf, |
| 2873 | int support_mki_value) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2874 | { |
| 2875 | conf->dtls_srtp_mki_support = support_mki_value; |
| 2876 | } |
| 2877 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2878 | int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl, |
| 2879 | unsigned char *mki_value, |
| 2880 | uint16_t mki_len) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2881 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2882 | if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) { |
| 2883 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2884 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2885 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2886 | if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) { |
| 2887 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2888 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2889 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2890 | memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len); |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2891 | ssl->dtls_srtp_info.mki_len = mki_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2892 | return 0; |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2893 | } |
| 2894 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2895 | int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf, |
| 2896 | const mbedtls_ssl_srtp_profile *profiles) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2897 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2898 | const mbedtls_ssl_srtp_profile *p; |
| 2899 | size_t list_size = 0; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2900 | |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2901 | /* check the profiles list: all entry must be valid, |
| 2902 | * 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] | 2903 | for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET && |
| 2904 | list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH; |
| 2905 | p++) { |
| 2906 | if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) { |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 2907 | list_size++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2908 | } else { |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 2909 | /* unsupported value, stop parsing and set the size to an error value */ |
| 2910 | list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2911 | } |
| 2912 | } |
| 2913 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2914 | if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) { |
| 2915 | conf->dtls_srtp_profile_list = NULL; |
| 2916 | conf->dtls_srtp_profile_list_len = 0; |
| 2917 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2918 | } |
| 2919 | |
Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 2920 | conf->dtls_srtp_profile_list = profiles; |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2921 | conf->dtls_srtp_profile_list_len = list_size; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2922 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2923 | return 0; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2924 | } |
| 2925 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2926 | void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl, |
| 2927 | mbedtls_dtls_srtp_info *dtls_srtp_info) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2928 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2929 | dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile; |
| 2930 | /* do not copy the mki value if there is no chosen profile */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2931 | if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2932 | dtls_srtp_info->mki_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2933 | } else { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2934 | dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2935 | memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value, |
| 2936 | ssl->dtls_srtp_info.mki_len); |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2937 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2938 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2939 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 2940 | |
Aditya Patwardhan | 3096f33 | 2022-07-26 14:31:46 +0530 | [diff] [blame] | 2941 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2942 | 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] | 2943 | { |
Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 2944 | conf->max_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor); |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 2945 | } |
| 2946 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2947 | 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] | 2948 | { |
Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 2949 | conf->min_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor); |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2950 | } |
Aditya Patwardhan | 3096f33 | 2022-07-26 14:31:46 +0530 | [diff] [blame] | 2951 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2952 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 2953 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2954 | void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf, |
| 2955 | char cert_req_ca_list) |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 2956 | { |
| 2957 | conf->cert_req_ca_list = cert_req_ca_list; |
| 2958 | } |
| 2959 | #endif |
| 2960 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2961 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2962 | 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] | 2963 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2964 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2965 | } |
| 2966 | #endif |
| 2967 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2968 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2969 | 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] | 2970 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2971 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2972 | } |
| 2973 | #endif |
| 2974 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2975 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2976 | 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] | 2977 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2978 | if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
| 2979 | ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) { |
| 2980 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2981 | } |
| 2982 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 2983 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2984 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2985 | return 0; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2986 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2987 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2988 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2989 | 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] | 2990 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2991 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2992 | } |
| 2993 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2994 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2995 | 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] | 2996 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2997 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2998 | } |
| 2999 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3000 | 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] | 3001 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3002 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 3003 | } |
| 3004 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3005 | void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf, |
| 3006 | const unsigned char period[8]) |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 3007 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3008 | memcpy(conf->renego_period, period, 8); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 3009 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3010 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3011 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3012 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3013 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3014 | 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] | 3015 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 3016 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 3017 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3018 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 3019 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3020 | #if defined(MBEDTLS_SSL_SRV_C) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 3021 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 3022 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3023 | void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf, |
| 3024 | uint16_t num_tickets) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 3025 | { |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 3026 | conf->new_session_tickets_count = num_tickets; |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 3027 | } |
| 3028 | #endif |
| 3029 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3030 | void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf, |
| 3031 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 3032 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 3033 | void *p_ticket) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 3034 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 3035 | conf->f_ticket_write = f_ticket_write; |
| 3036 | conf->f_ticket_parse = f_ticket_parse; |
| 3037 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 3038 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3039 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3040 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 3041 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3042 | void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl, |
| 3043 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 3044 | void *p_export_keys) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 3045 | { |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 3046 | ssl->f_export_keys = f_export_keys; |
| 3047 | ssl->p_export_keys = p_export_keys; |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 3048 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 3049 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3050 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3051 | void mbedtls_ssl_conf_async_private_cb( |
| 3052 | mbedtls_ssl_config *conf, |
| 3053 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 3054 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 3055 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 3056 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3057 | void *async_config_data) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3058 | { |
| 3059 | conf->f_async_sign_start = f_async_sign; |
| 3060 | conf->f_async_decrypt_start = f_async_decrypt; |
| 3061 | conf->f_async_resume = f_async_resume; |
| 3062 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3063 | conf->p_async_config_data = async_config_data; |
| 3064 | } |
| 3065 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3066 | 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] | 3067 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3068 | return conf->p_async_config_data; |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3069 | } |
| 3070 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3071 | void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3072 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3073 | if (ssl->handshake == NULL) { |
| 3074 | return NULL; |
| 3075 | } else { |
| 3076 | return ssl->handshake->user_async_ctx; |
| 3077 | } |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3078 | } |
| 3079 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3080 | void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl, |
| 3081 | void *ctx) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3082 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3083 | if (ssl->handshake != NULL) { |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3084 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3085 | } |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3086 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3087 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3088 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3089 | /* |
| 3090 | * SSL get accessors |
| 3091 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3092 | uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3093 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3094 | if (ssl->session != NULL) { |
| 3095 | return ssl->session->verify_result; |
| 3096 | } |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 3097 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3098 | if (ssl->session_negotiate != NULL) { |
| 3099 | return ssl->session_negotiate->verify_result; |
| 3100 | } |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 3101 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3102 | return 0xFFFFFFFF; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3103 | } |
| 3104 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3105 | 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] | 3106 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3107 | if (ssl == NULL || ssl->session == NULL) { |
| 3108 | return 0; |
| 3109 | } |
Glenn Strauss | 8f52690 | 2022-01-13 00:04:49 -0500 | [diff] [blame] | 3110 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3111 | return ssl->session->ciphersuite; |
Glenn Strauss | 8f52690 | 2022-01-13 00:04:49 -0500 | [diff] [blame] | 3112 | } |
| 3113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3114 | const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 3115 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3116 | if (ssl == NULL || ssl->session == NULL) { |
| 3117 | return NULL; |
| 3118 | } |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 3119 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3120 | return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 3121 | } |
| 3122 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3123 | const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3124 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3125 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3126 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 3127 | switch (ssl->tls_version) { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3128 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3129 | return "DTLSv1.2"; |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 3130 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3131 | return "unknown (DTLS)"; |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 3132 | } |
| 3133 | } |
| 3134 | #endif |
| 3135 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3136 | switch (ssl->tls_version) { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3137 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3138 | return "TLSv1.2"; |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3139 | case MBEDTLS_SSL_VERSION_TLS1_3: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3140 | return "TLSv1.3"; |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3141 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3142 | return "unknown"; |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3143 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3144 | } |
| 3145 | |
Waleed Elmelegy | 6a971fd | 2023-12-28 17:48:16 +0000 | [diff] [blame] | 3146 | #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) |
| 3147 | |
| 3148 | size_t mbedtls_ssl_get_output_record_size_limit(const mbedtls_ssl_context *ssl) |
| 3149 | { |
| 3150 | const size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 3151 | size_t record_size_limit = max_len; |
| 3152 | |
| 3153 | if (ssl->session != NULL && |
| 3154 | ssl->session->record_size_limit >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN && |
| 3155 | ssl->session->record_size_limit < max_len) { |
| 3156 | record_size_limit = ssl->session->record_size_limit; |
| 3157 | } |
| 3158 | |
| 3159 | // TODO: this is currently untested |
| 3160 | /* During a handshake, use the value being negotiated */ |
| 3161 | if (ssl->session_negotiate != NULL && |
| 3162 | ssl->session_negotiate->record_size_limit >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN && |
| 3163 | ssl->session_negotiate->record_size_limit < max_len) { |
| 3164 | record_size_limit = ssl->session_negotiate->record_size_limit; |
| 3165 | } |
| 3166 | |
| 3167 | return record_size_limit; |
| 3168 | } |
| 3169 | #endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */ |
| 3170 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 3171 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3172 | 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] | 3173 | { |
David Horstmann | 95d516f | 2021-05-04 18:36:56 +0100 | [diff] [blame] | 3174 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3175 | size_t read_mfl; |
| 3176 | |
Jerry Yu | ddda050 | 2022-12-01 19:43:12 +0800 | [diff] [blame] | 3177 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3178 | /* 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] | 3179 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 3180 | ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) { |
| 3181 | return ssl_mfl_code_to_length(ssl->conf->mfl_code); |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3182 | } |
Jerry Yu | ddda050 | 2022-12-01 19:43:12 +0800 | [diff] [blame] | 3183 | #endif |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3184 | |
| 3185 | /* Check if a smaller max length was negotiated */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3186 | if (ssl->session_out != NULL) { |
| 3187 | read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code); |
| 3188 | if (read_mfl < max_len) { |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3189 | max_len = read_mfl; |
| 3190 | } |
| 3191 | } |
| 3192 | |
Jerry Yu | ddda050 | 2022-12-01 19:43:12 +0800 | [diff] [blame] | 3193 | /* During a handshake, use the value being negotiated */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3194 | if (ssl->session_negotiate != NULL) { |
| 3195 | read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code); |
| 3196 | if (read_mfl < max_len) { |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3197 | max_len = read_mfl; |
| 3198 | } |
| 3199 | } |
| 3200 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3201 | return max_len; |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3202 | } |
| 3203 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3204 | 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] | 3205 | { |
| 3206 | size_t max_len; |
| 3207 | |
| 3208 | /* |
| 3209 | * Assume mfl_code is correct since it was checked when set |
| 3210 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3211 | 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] | 3212 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 3213 | /* Check if a smaller max length was negotiated */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3214 | if (ssl->session_out != NULL && |
| 3215 | ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) { |
| 3216 | 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] | 3217 | } |
| 3218 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 3219 | /* During a handshake, use the value being negotiated */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3220 | if (ssl->session_negotiate != NULL && |
| 3221 | ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) { |
| 3222 | 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] | 3223 | } |
| 3224 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3225 | return max_len; |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 3226 | } |
| 3227 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 3228 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3229 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3230 | 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] | 3231 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 3232 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3233 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 3234 | (ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 3235 | ssl->state == MBEDTLS_SSL_SERVER_HELLO)) { |
| 3236 | return 0; |
| 3237 | } |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 3238 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3239 | if (ssl->handshake == NULL || ssl->handshake->mtu == 0) { |
| 3240 | return ssl->mtu; |
| 3241 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3242 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3243 | if (ssl->mtu == 0) { |
| 3244 | return ssl->handshake->mtu; |
| 3245 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3246 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3247 | return ssl->mtu < ssl->handshake->mtu ? |
| 3248 | ssl->mtu : ssl->handshake->mtu; |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3249 | } |
| 3250 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3252 | 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] | 3253 | { |
| 3254 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 3255 | |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 3256 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
Jan Bruckner | f482dcc | 2023-03-15 09:09:06 +0100 | [diff] [blame] | 3257 | !defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) && \ |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 3258 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3259 | (void) ssl; |
| 3260 | #endif |
| 3261 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3262 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3263 | 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] | 3264 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3265 | if (max_len > mfl) { |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3266 | max_len = mfl; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3267 | } |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3268 | #endif |
| 3269 | |
Jan Bruckner | f482dcc | 2023-03-15 09:09:06 +0100 | [diff] [blame] | 3270 | #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) |
| 3271 | const size_t record_size_limit = mbedtls_ssl_get_output_record_size_limit(ssl); |
| 3272 | |
| 3273 | if (max_len > record_size_limit) { |
| 3274 | max_len = record_size_limit; |
| 3275 | } |
| 3276 | #endif |
| 3277 | |
Waleed Elmelegy | 6a971fd | 2023-12-28 17:48:16 +0000 | [diff] [blame] | 3278 | if (ssl->transform_out != NULL && |
| 3279 | ssl->transform_out->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Waleed Elmelegy | f501790 | 2024-01-09 14:18:34 +0000 | [diff] [blame] | 3280 | /* |
| 3281 | * In TLS 1.3 case, when records are protected, `max_len` as computed |
| 3282 | * above is the maximum length of the TLSInnerPlaintext structure that |
| 3283 | * along the plaintext payload contains the inner content type (one byte) |
| 3284 | * and some zero padding. Given the algorithm used for padding |
| 3285 | * in mbedtls_ssl_encrypt_buf(), compute the maximum length for |
| 3286 | * the plaintext payload. Round down to a multiple of |
| 3287 | * MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY and |
| 3288 | * subtract 1. |
Waleed Elmelegy | 6a971fd | 2023-12-28 17:48:16 +0000 | [diff] [blame] | 3289 | */ |
| 3290 | max_len = ((max_len / MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) * |
| 3291 | MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) - 1; |
| 3292 | } |
| 3293 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3294 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3295 | if (mbedtls_ssl_get_current_mtu(ssl) != 0) { |
| 3296 | const size_t mtu = mbedtls_ssl_get_current_mtu(ssl); |
| 3297 | const int ret = mbedtls_ssl_get_record_expansion(ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3298 | const size_t overhead = (size_t) ret; |
| 3299 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3300 | if (ret < 0) { |
| 3301 | return ret; |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3302 | } |
| 3303 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3304 | if (mtu <= overhead) { |
| 3305 | MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion")); |
| 3306 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 3307 | } |
| 3308 | |
| 3309 | if (max_len > mtu - overhead) { |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3310 | max_len = mtu - overhead; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3311 | } |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3312 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3313 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3314 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 3315 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
Waleed Elmelegy | 049cd30 | 2023-12-20 17:28:31 +0000 | [diff] [blame] | 3316 | !defined(MBEDTLS_SSL_PROTO_DTLS) && \ |
| 3317 | !defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 3318 | ((void) ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3319 | #endif |
| 3320 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3321 | return (int) max_len; |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3322 | } |
| 3323 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3324 | 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] | 3325 | { |
| 3326 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
| 3327 | |
| 3328 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 3329 | (void) ssl; |
| 3330 | #endif |
| 3331 | |
| 3332 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3333 | const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl); |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 3334 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3335 | if (max_len > mfl) { |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 3336 | max_len = mfl; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3337 | } |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 3338 | #endif |
| 3339 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3340 | return (int) max_len; |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 3341 | } |
| 3342 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3343 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3344 | 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] | 3345 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3346 | if (ssl == NULL || ssl->session == NULL) { |
| 3347 | return NULL; |
| 3348 | } |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3349 | |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 3350 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3351 | return ssl->session->peer_cert; |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 3352 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3353 | return NULL; |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 3354 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3355 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3356 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3357 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3358 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3359 | int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl, |
| 3360 | mbedtls_ssl_session *dst) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3361 | { |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3362 | int ret; |
| 3363 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3364 | if (ssl == NULL || |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3365 | dst == NULL || |
| 3366 | ssl->session == NULL || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3367 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) { |
| 3368 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3369 | } |
| 3370 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3371 | /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer |
| 3372 | * idempotent: Each session can only be exported once. |
| 3373 | * |
| 3374 | * (This is in preparation for TLS 1.3 support where we will |
| 3375 | * need the ability to export multiple sessions (aka tickets), |
| 3376 | * which will be achieved by calling mbedtls_ssl_get_session() |
| 3377 | * multiple times until it fails.) |
| 3378 | * |
| 3379 | * Check whether we have already exported the current session, |
| 3380 | * and fail if so. |
| 3381 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3382 | if (ssl->session->exported == 1) { |
| 3383 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 3384 | } |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3386 | ret = mbedtls_ssl_session_copy(dst, ssl->session); |
| 3387 | if (ret != 0) { |
| 3388 | return ret; |
| 3389 | } |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3390 | |
| 3391 | /* Remember that we've exported the session. */ |
| 3392 | ssl->session->exported = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3393 | return 0; |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3394 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3395 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3396 | |
David Horstmann | cb01b36 | 2024-02-29 17:31:46 +0000 | [diff] [blame] | 3397 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3398 | |
| 3399 | /* Serialization of TLS 1.2 sessions |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3400 | * |
David Horstmann | cb01b36 | 2024-02-29 17:31:46 +0000 | [diff] [blame] | 3401 | * For more detail, see the description of ssl_session_save(). |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3402 | */ |
| 3403 | static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session, |
| 3404 | unsigned char *buf, |
| 3405 | size_t buf_len) |
| 3406 | { |
| 3407 | unsigned char *p = buf; |
| 3408 | size_t used = 0; |
| 3409 | |
| 3410 | #if defined(MBEDTLS_HAVE_TIME) |
| 3411 | uint64_t start; |
| 3412 | #endif |
| 3413 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3414 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3415 | size_t cert_len; |
| 3416 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3417 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 3418 | |
| 3419 | /* |
| 3420 | * Time |
| 3421 | */ |
| 3422 | #if defined(MBEDTLS_HAVE_TIME) |
| 3423 | used += 8; |
| 3424 | |
| 3425 | if (used <= buf_len) { |
| 3426 | start = (uint64_t) session->start; |
| 3427 | |
| 3428 | MBEDTLS_PUT_UINT64_BE(start, p, 0); |
| 3429 | p += 8; |
| 3430 | } |
| 3431 | #endif /* MBEDTLS_HAVE_TIME */ |
| 3432 | |
| 3433 | /* |
| 3434 | * Basic mandatory fields |
| 3435 | */ |
| 3436 | used += 1 /* id_len */ |
| 3437 | + sizeof(session->id) |
| 3438 | + sizeof(session->master) |
| 3439 | + 4; /* verify_result */ |
| 3440 | |
| 3441 | if (used <= buf_len) { |
| 3442 | *p++ = MBEDTLS_BYTE_0(session->id_len); |
| 3443 | memcpy(p, session->id, 32); |
| 3444 | p += 32; |
| 3445 | |
| 3446 | memcpy(p, session->master, 48); |
| 3447 | p += 48; |
| 3448 | |
| 3449 | MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0); |
| 3450 | p += 4; |
| 3451 | } |
| 3452 | |
| 3453 | /* |
| 3454 | * Peer's end-entity certificate |
| 3455 | */ |
| 3456 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3457 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3458 | if (session->peer_cert == NULL) { |
| 3459 | cert_len = 0; |
| 3460 | } else { |
| 3461 | cert_len = session->peer_cert->raw.len; |
| 3462 | } |
| 3463 | |
| 3464 | used += 3 + cert_len; |
| 3465 | |
| 3466 | if (used <= buf_len) { |
| 3467 | *p++ = MBEDTLS_BYTE_2(cert_len); |
| 3468 | *p++ = MBEDTLS_BYTE_1(cert_len); |
| 3469 | *p++ = MBEDTLS_BYTE_0(cert_len); |
| 3470 | |
| 3471 | if (session->peer_cert != NULL) { |
| 3472 | memcpy(p, session->peer_cert->raw.p, cert_len); |
| 3473 | p += cert_len; |
| 3474 | } |
| 3475 | } |
| 3476 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3477 | if (session->peer_cert_digest != NULL) { |
| 3478 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; |
| 3479 | if (used <= buf_len) { |
| 3480 | *p++ = (unsigned char) session->peer_cert_digest_type; |
| 3481 | *p++ = (unsigned char) session->peer_cert_digest_len; |
| 3482 | memcpy(p, session->peer_cert_digest, |
| 3483 | session->peer_cert_digest_len); |
| 3484 | p += session->peer_cert_digest_len; |
| 3485 | } |
| 3486 | } else { |
| 3487 | used += 2; |
| 3488 | if (used <= buf_len) { |
| 3489 | *p++ = (unsigned char) MBEDTLS_MD_NONE; |
| 3490 | *p++ = 0; |
| 3491 | } |
| 3492 | } |
| 3493 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3494 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 3495 | |
| 3496 | /* |
| 3497 | * Session ticket if any, plus associated data |
| 3498 | */ |
| 3499 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 3500 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3501 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 3502 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ |
| 3503 | |
| 3504 | if (used <= buf_len) { |
| 3505 | *p++ = MBEDTLS_BYTE_2(session->ticket_len); |
| 3506 | *p++ = MBEDTLS_BYTE_1(session->ticket_len); |
| 3507 | *p++ = MBEDTLS_BYTE_0(session->ticket_len); |
| 3508 | |
| 3509 | if (session->ticket != NULL) { |
| 3510 | memcpy(p, session->ticket, session->ticket_len); |
| 3511 | p += session->ticket_len; |
| 3512 | } |
| 3513 | |
| 3514 | MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0); |
| 3515 | p += 4; |
| 3516 | } |
| 3517 | } |
| 3518 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 3519 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
| 3520 | if (session->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 3521 | used += 8; |
| 3522 | |
| 3523 | if (used <= buf_len) { |
| 3524 | MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_creation_time, p, 0); |
| 3525 | p += 8; |
| 3526 | } |
| 3527 | } |
| 3528 | #endif /* MBEDTLS_HAVE_TIME && MBEDTLS_SSL_SRV_C */ |
| 3529 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 3530 | |
| 3531 | /* |
| 3532 | * Misc extension-related info |
| 3533 | */ |
| 3534 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 3535 | used += 1; |
| 3536 | |
| 3537 | if (used <= buf_len) { |
| 3538 | *p++ = session->mfl_code; |
| 3539 | } |
| 3540 | #endif |
| 3541 | |
| 3542 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 3543 | used += 1; |
| 3544 | |
| 3545 | if (used <= buf_len) { |
| 3546 | *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac); |
| 3547 | } |
| 3548 | #endif |
| 3549 | |
| 3550 | return used; |
| 3551 | } |
| 3552 | |
| 3553 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 3554 | static int ssl_tls12_session_load(mbedtls_ssl_session *session, |
| 3555 | const unsigned char *buf, |
| 3556 | size_t len) |
| 3557 | { |
| 3558 | #if defined(MBEDTLS_HAVE_TIME) |
| 3559 | uint64_t start; |
| 3560 | #endif |
| 3561 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3562 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3563 | size_t cert_len; |
| 3564 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3565 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 3566 | |
| 3567 | const unsigned char *p = buf; |
| 3568 | const unsigned char * const end = buf + len; |
| 3569 | |
| 3570 | /* |
| 3571 | * Time |
| 3572 | */ |
| 3573 | #if defined(MBEDTLS_HAVE_TIME) |
| 3574 | if (8 > (size_t) (end - p)) { |
| 3575 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3576 | } |
| 3577 | |
| 3578 | start = MBEDTLS_GET_UINT64_BE(p, 0); |
| 3579 | p += 8; |
| 3580 | |
| 3581 | session->start = (time_t) start; |
| 3582 | #endif /* MBEDTLS_HAVE_TIME */ |
| 3583 | |
| 3584 | /* |
| 3585 | * Basic mandatory fields |
| 3586 | */ |
| 3587 | if (1 + 32 + 48 + 4 > (size_t) (end - p)) { |
| 3588 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3589 | } |
| 3590 | |
| 3591 | session->id_len = *p++; |
| 3592 | memcpy(session->id, p, 32); |
| 3593 | p += 32; |
| 3594 | |
| 3595 | memcpy(session->master, p, 48); |
| 3596 | p += 48; |
| 3597 | |
| 3598 | session->verify_result = MBEDTLS_GET_UINT32_BE(p, 0); |
| 3599 | p += 4; |
| 3600 | |
| 3601 | /* Immediately clear invalid pointer values that have been read, in case |
| 3602 | * we exit early before we replaced them with valid ones. */ |
| 3603 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3604 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3605 | session->peer_cert = NULL; |
| 3606 | #else |
| 3607 | session->peer_cert_digest = NULL; |
| 3608 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3609 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 3610 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 3611 | session->ticket = NULL; |
| 3612 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 3613 | |
| 3614 | /* |
| 3615 | * Peer certificate |
| 3616 | */ |
| 3617 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3618 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3619 | /* Deserialize CRT from the end of the ticket. */ |
| 3620 | if (3 > (size_t) (end - p)) { |
| 3621 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3622 | } |
| 3623 | |
| 3624 | cert_len = MBEDTLS_GET_UINT24_BE(p, 0); |
| 3625 | p += 3; |
| 3626 | |
| 3627 | if (cert_len != 0) { |
| 3628 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3629 | |
| 3630 | if (cert_len > (size_t) (end - p)) { |
| 3631 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3632 | } |
| 3633 | |
| 3634 | session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt)); |
| 3635 | |
| 3636 | if (session->peer_cert == NULL) { |
| 3637 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 3638 | } |
| 3639 | |
| 3640 | mbedtls_x509_crt_init(session->peer_cert); |
| 3641 | |
| 3642 | if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert, |
| 3643 | p, cert_len)) != 0) { |
| 3644 | mbedtls_x509_crt_free(session->peer_cert); |
| 3645 | mbedtls_free(session->peer_cert); |
| 3646 | session->peer_cert = NULL; |
| 3647 | return ret; |
| 3648 | } |
| 3649 | |
| 3650 | p += cert_len; |
| 3651 | } |
| 3652 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3653 | /* Deserialize CRT digest from the end of the ticket. */ |
| 3654 | if (2 > (size_t) (end - p)) { |
| 3655 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3656 | } |
| 3657 | |
| 3658 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; |
| 3659 | session->peer_cert_digest_len = (size_t) *p++; |
| 3660 | |
| 3661 | if (session->peer_cert_digest_len != 0) { |
| 3662 | const mbedtls_md_info_t *md_info = |
| 3663 | mbedtls_md_info_from_type(session->peer_cert_digest_type); |
| 3664 | if (md_info == NULL) { |
| 3665 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3666 | } |
| 3667 | if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) { |
| 3668 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3669 | } |
| 3670 | |
| 3671 | if (session->peer_cert_digest_len > (size_t) (end - p)) { |
| 3672 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3673 | } |
| 3674 | |
| 3675 | session->peer_cert_digest = |
| 3676 | mbedtls_calloc(1, session->peer_cert_digest_len); |
| 3677 | if (session->peer_cert_digest == NULL) { |
| 3678 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 3679 | } |
| 3680 | |
| 3681 | memcpy(session->peer_cert_digest, p, |
| 3682 | session->peer_cert_digest_len); |
| 3683 | p += session->peer_cert_digest_len; |
| 3684 | } |
| 3685 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3686 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 3687 | |
| 3688 | /* |
| 3689 | * Session ticket and associated data |
| 3690 | */ |
| 3691 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 3692 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3693 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 3694 | if (3 > (size_t) (end - p)) { |
| 3695 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3696 | } |
| 3697 | |
| 3698 | session->ticket_len = MBEDTLS_GET_UINT24_BE(p, 0); |
| 3699 | p += 3; |
| 3700 | |
| 3701 | if (session->ticket_len != 0) { |
| 3702 | if (session->ticket_len > (size_t) (end - p)) { |
| 3703 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3704 | } |
| 3705 | |
| 3706 | session->ticket = mbedtls_calloc(1, session->ticket_len); |
| 3707 | if (session->ticket == NULL) { |
| 3708 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 3709 | } |
| 3710 | |
| 3711 | memcpy(session->ticket, p, session->ticket_len); |
| 3712 | p += session->ticket_len; |
| 3713 | } |
| 3714 | |
| 3715 | if (4 > (size_t) (end - p)) { |
| 3716 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3717 | } |
| 3718 | |
| 3719 | session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0); |
| 3720 | p += 4; |
| 3721 | } |
| 3722 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 3723 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
| 3724 | if (session->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 3725 | if (8 > (size_t) (end - p)) { |
| 3726 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3727 | } |
| 3728 | session->ticket_creation_time = MBEDTLS_GET_UINT64_BE(p, 0); |
| 3729 | p += 8; |
| 3730 | } |
| 3731 | #endif /* MBEDTLS_HAVE_TIME && MBEDTLS_SSL_SRV_C */ |
| 3732 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 3733 | |
| 3734 | /* |
| 3735 | * Misc extension-related info |
| 3736 | */ |
| 3737 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 3738 | if (1 > (size_t) (end - p)) { |
| 3739 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3740 | } |
| 3741 | |
| 3742 | session->mfl_code = *p++; |
| 3743 | #endif |
| 3744 | |
| 3745 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 3746 | if (1 > (size_t) (end - p)) { |
| 3747 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3748 | } |
| 3749 | |
| 3750 | session->encrypt_then_mac = *p++; |
| 3751 | #endif |
| 3752 | |
| 3753 | /* Done, should have consumed entire buffer */ |
| 3754 | if (p != end) { |
| 3755 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3756 | } |
| 3757 | |
| 3758 | return 0; |
| 3759 | } |
| 3760 | |
| 3761 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3762 | |
| 3763 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3764 | /* Serialization of TLS 1.3 sessions: |
| 3765 | * |
David Horstmann | cb01b36 | 2024-02-29 17:31:46 +0000 | [diff] [blame] | 3766 | * For more detail, see the description of ssl_session_save(). |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3767 | */ |
| 3768 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 3769 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 3770 | static int ssl_tls13_session_save(const mbedtls_ssl_session *session, |
| 3771 | unsigned char *buf, |
| 3772 | size_t buf_len, |
| 3773 | size_t *olen) |
| 3774 | { |
| 3775 | unsigned char *p = buf; |
| 3776 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
| 3777 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 3778 | size_t hostname_len = (session->hostname == NULL) ? |
| 3779 | 0 : strlen(session->hostname) + 1; |
| 3780 | #endif |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3781 | |
| 3782 | #if defined(MBEDTLS_SSL_SRV_C) && \ |
| 3783 | defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) |
Waleed Elmelegy | daa4da7 | 2024-03-13 16:25:34 +0000 | [diff] [blame] | 3784 | const size_t alpn_len = (session->ticket_alpn == NULL) ? |
Waleed Elmelegy | b28ab0a | 2024-03-13 16:48:28 +0000 | [diff] [blame] | 3785 | 0 : strlen(session->ticket_alpn) + 1; |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3786 | #endif |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3787 | size_t needed = 4 /* ticket_age_add */ |
| 3788 | + 1 /* ticket_flags */ |
| 3789 | + 1; /* resumption_key length */ |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3790 | |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3791 | *olen = 0; |
| 3792 | |
| 3793 | if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) { |
| 3794 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3795 | } |
| 3796 | needed += session->resumption_key_len; /* resumption_key */ |
| 3797 | |
| 3798 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 3799 | needed += 4; /* max_early_data_size */ |
| 3800 | #endif |
| 3801 | #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) |
| 3802 | needed += 2; /* record_size_limit */ |
| 3803 | #endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */ |
| 3804 | |
| 3805 | #if defined(MBEDTLS_HAVE_TIME) |
| 3806 | needed += 8; /* ticket_creation_time or ticket_reception_time */ |
| 3807 | #endif |
| 3808 | |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3809 | #if defined(MBEDTLS_SSL_SRV_C) |
| 3810 | if (session->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 3811 | #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) |
Waleed Elmelegy | daa4da7 | 2024-03-13 16:25:34 +0000 | [diff] [blame] | 3812 | needed += 2 /* alpn_len */ |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3813 | + alpn_len; /* alpn */ |
| 3814 | #endif |
| 3815 | } |
| 3816 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 3817 | |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3818 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3819 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 3820 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 3821 | needed += 2 /* hostname_len */ |
| 3822 | + hostname_len; /* hostname */ |
| 3823 | #endif |
| 3824 | |
| 3825 | needed += 4 /* ticket_lifetime */ |
| 3826 | + 2; /* ticket_len */ |
| 3827 | |
| 3828 | /* Check size_t overflow */ |
| 3829 | if (session->ticket_len > SIZE_MAX - needed) { |
| 3830 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3831 | } |
| 3832 | |
| 3833 | needed += session->ticket_len; /* ticket */ |
| 3834 | } |
| 3835 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 3836 | |
| 3837 | *olen = needed; |
| 3838 | if (needed > buf_len) { |
| 3839 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 3840 | } |
| 3841 | |
| 3842 | MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 0); |
| 3843 | p[4] = session->ticket_flags; |
| 3844 | |
| 3845 | /* save resumption_key */ |
| 3846 | p[5] = session->resumption_key_len; |
| 3847 | p += 6; |
| 3848 | memcpy(p, session->resumption_key, session->resumption_key_len); |
| 3849 | p += session->resumption_key_len; |
| 3850 | |
| 3851 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 3852 | MBEDTLS_PUT_UINT32_BE(session->max_early_data_size, p, 0); |
| 3853 | p += 4; |
| 3854 | #endif |
| 3855 | #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) |
| 3856 | MBEDTLS_PUT_UINT16_BE(session->record_size_limit, p, 0); |
| 3857 | p += 2; |
| 3858 | #endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */ |
| 3859 | |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3860 | #if defined(MBEDTLS_SSL_SRV_C) |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3861 | if (session->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3862 | #if defined(MBEDTLS_HAVE_TIME) |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3863 | MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_creation_time, p, 0); |
| 3864 | p += 8; |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3865 | #endif /* MBEDTLS_HAVE_TIME */ |
| 3866 | |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3867 | #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) |
Waleed Elmelegy | daa4da7 | 2024-03-13 16:25:34 +0000 | [diff] [blame] | 3868 | MBEDTLS_PUT_UINT16_BE(alpn_len, p, 0); |
| 3869 | p += 2; |
| 3870 | |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3871 | if (alpn_len > 0) { |
| 3872 | /* save chosen alpn */ |
Waleed Elmelegy | 883f77c | 2024-03-06 19:09:41 +0000 | [diff] [blame] | 3873 | memcpy(p, session->ticket_alpn, alpn_len); |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3874 | p += alpn_len; |
| 3875 | } |
| 3876 | #endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */ |
| 3877 | } |
| 3878 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 3879 | |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3880 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3881 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 3882 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 3883 | MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0); |
| 3884 | p += 2; |
| 3885 | if (hostname_len > 0) { |
| 3886 | /* save host name */ |
| 3887 | memcpy(p, session->hostname, hostname_len); |
| 3888 | p += hostname_len; |
| 3889 | } |
| 3890 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 3891 | |
| 3892 | #if defined(MBEDTLS_HAVE_TIME) |
| 3893 | MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_reception_time, p, 0); |
| 3894 | p += 8; |
| 3895 | #endif |
| 3896 | MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0); |
| 3897 | p += 4; |
| 3898 | |
| 3899 | MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0); |
| 3900 | p += 2; |
| 3901 | |
| 3902 | if (session->ticket != NULL && session->ticket_len > 0) { |
| 3903 | memcpy(p, session->ticket, session->ticket_len); |
| 3904 | p += session->ticket_len; |
| 3905 | } |
| 3906 | } |
| 3907 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 3908 | return 0; |
| 3909 | } |
| 3910 | |
| 3911 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 3912 | static int ssl_tls13_session_load(mbedtls_ssl_session *session, |
| 3913 | const unsigned char *buf, |
| 3914 | size_t len) |
| 3915 | { |
| 3916 | const unsigned char *p = buf; |
| 3917 | const unsigned char *end = buf + len; |
| 3918 | |
| 3919 | if (end - p < 6) { |
| 3920 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3921 | } |
| 3922 | session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 0); |
| 3923 | session->ticket_flags = p[4]; |
| 3924 | |
| 3925 | /* load resumption_key */ |
| 3926 | session->resumption_key_len = p[5]; |
| 3927 | p += 6; |
| 3928 | |
| 3929 | if (end - p < session->resumption_key_len) { |
| 3930 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3931 | } |
| 3932 | |
| 3933 | if (sizeof(session->resumption_key) < session->resumption_key_len) { |
| 3934 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3935 | } |
| 3936 | memcpy(session->resumption_key, p, session->resumption_key_len); |
| 3937 | p += session->resumption_key_len; |
| 3938 | |
| 3939 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 3940 | if (end - p < 4) { |
| 3941 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3942 | } |
| 3943 | session->max_early_data_size = MBEDTLS_GET_UINT32_BE(p, 0); |
| 3944 | p += 4; |
| 3945 | #endif |
| 3946 | #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) |
| 3947 | if (end - p < 2) { |
| 3948 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3949 | } |
| 3950 | session->record_size_limit = MBEDTLS_GET_UINT16_BE(p, 0); |
| 3951 | p += 2; |
| 3952 | #endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */ |
| 3953 | |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3954 | #if defined(MBEDTLS_SSL_SRV_C) |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3955 | if (session->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3956 | #if defined(MBEDTLS_HAVE_TIME) |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3957 | if (end - p < 8) { |
| 3958 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3959 | } |
| 3960 | session->ticket_creation_time = MBEDTLS_GET_UINT64_BE(p, 0); |
| 3961 | p += 8; |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3962 | #endif /* MBEDTLS_HAVE_TIME */ |
| 3963 | |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3964 | #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) |
Waleed Elmelegy | daa4da7 | 2024-03-13 16:25:34 +0000 | [diff] [blame] | 3965 | size_t alpn_len; |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3966 | |
Waleed Elmelegy | daa4da7 | 2024-03-13 16:25:34 +0000 | [diff] [blame] | 3967 | if (end - p < 2) { |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3968 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3969 | } |
Waleed Elmelegy | 883f77c | 2024-03-06 19:09:41 +0000 | [diff] [blame] | 3970 | |
Waleed Elmelegy | daa4da7 | 2024-03-13 16:25:34 +0000 | [diff] [blame] | 3971 | alpn_len = MBEDTLS_GET_UINT16_BE(p, 0); |
| 3972 | p += 2; |
| 3973 | |
| 3974 | if (end - p < (long int) alpn_len) { |
Waleed Elmelegy | 7dfba34 | 2024-03-12 16:22:39 +0000 | [diff] [blame] | 3975 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3976 | } |
| 3977 | |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3978 | if (alpn_len > 0) { |
Waleed Elmelegy | daa4da7 | 2024-03-13 16:25:34 +0000 | [diff] [blame] | 3979 | int ret = mbedtls_ssl_session_set_ticket_alpn(session, (char *) p); |
| 3980 | if (ret != 0) { |
| 3981 | return ret; |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3982 | } |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3983 | p += alpn_len; |
| 3984 | } |
| 3985 | #endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */ |
| 3986 | } |
| 3987 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 3988 | |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3989 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3990 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 3991 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 3992 | size_t hostname_len; |
| 3993 | /* load host name */ |
| 3994 | if (end - p < 2) { |
| 3995 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3996 | } |
| 3997 | hostname_len = MBEDTLS_GET_UINT16_BE(p, 0); |
| 3998 | p += 2; |
| 3999 | |
| 4000 | if (end - p < (long int) hostname_len) { |
| 4001 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4002 | } |
| 4003 | if (hostname_len > 0) { |
| 4004 | session->hostname = mbedtls_calloc(1, hostname_len); |
| 4005 | if (session->hostname == NULL) { |
| 4006 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 4007 | } |
| 4008 | memcpy(session->hostname, p, hostname_len); |
| 4009 | p += hostname_len; |
| 4010 | } |
| 4011 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 4012 | |
| 4013 | #if defined(MBEDTLS_HAVE_TIME) |
| 4014 | if (end - p < 8) { |
| 4015 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4016 | } |
| 4017 | session->ticket_reception_time = MBEDTLS_GET_UINT64_BE(p, 0); |
| 4018 | p += 8; |
| 4019 | #endif |
| 4020 | if (end - p < 4) { |
| 4021 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4022 | } |
| 4023 | session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0); |
| 4024 | p += 4; |
| 4025 | |
| 4026 | if (end - p < 2) { |
| 4027 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4028 | } |
| 4029 | session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0); |
| 4030 | p += 2; |
| 4031 | |
| 4032 | if (end - p < (long int) session->ticket_len) { |
| 4033 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4034 | } |
| 4035 | if (session->ticket_len > 0) { |
| 4036 | session->ticket = mbedtls_calloc(1, session->ticket_len); |
| 4037 | if (session->ticket == NULL) { |
| 4038 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 4039 | } |
| 4040 | memcpy(session->ticket, p, session->ticket_len); |
| 4041 | p += session->ticket_len; |
| 4042 | } |
| 4043 | } |
| 4044 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 4045 | |
| 4046 | return 0; |
| 4047 | |
| 4048 | } |
| 4049 | #else /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 4050 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 4051 | static int ssl_tls13_session_save(const mbedtls_ssl_session *session, |
| 4052 | unsigned char *buf, |
| 4053 | size_t buf_len, |
| 4054 | size_t *olen) |
| 4055 | { |
| 4056 | ((void) session); |
| 4057 | ((void) buf); |
| 4058 | ((void) buf_len); |
| 4059 | *olen = 0; |
| 4060 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 4061 | } |
| 4062 | |
| 4063 | static int ssl_tls13_session_load(const mbedtls_ssl_session *session, |
Norbert Fabritius | d36913a | 2023-01-24 17:48:29 +0100 | [diff] [blame] | 4064 | const unsigned char *buf, |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 4065 | size_t buf_len) |
| 4066 | { |
| 4067 | ((void) session); |
| 4068 | ((void) buf); |
| 4069 | ((void) buf_len); |
| 4070 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 4071 | } |
| 4072 | #endif /* !MBEDTLS_SSL_SESSION_TICKETS */ |
| 4073 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 4074 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4075 | /* |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 4076 | * Define ticket header determining Mbed TLS version |
| 4077 | * and structure of the ticket. |
| 4078 | */ |
| 4079 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4080 | /* |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 4081 | * Define bitflag determining compile-time settings influencing |
| 4082 | * structure of serialized SSL sessions. |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4083 | */ |
| 4084 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 4085 | #if defined(MBEDTLS_HAVE_TIME) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4086 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 4087 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4088 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4089 | #endif /* MBEDTLS_HAVE_TIME */ |
| 4090 | |
| 4091 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4092 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4093 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4094 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4095 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 4096 | |
David Horstmann | 5c5a32f | 2024-02-13 17:53:35 +0000 | [diff] [blame] | 4097 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
David Horstmann | f686f1d | 2024-03-01 11:20:32 +0000 | [diff] [blame] | 4098 | #define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 1 |
David Horstmann | 5c5a32f | 2024-02-13 17:53:35 +0000 | [diff] [blame] | 4099 | #else |
David Horstmann | f686f1d | 2024-03-01 11:20:32 +0000 | [diff] [blame] | 4100 | #define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 0 |
David Horstmann | 5c5a32f | 2024-02-13 17:53:35 +0000 | [diff] [blame] | 4101 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 4102 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4103 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4104 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4105 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4106 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4107 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ |
| 4108 | |
| 4109 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4110 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4111 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4112 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4113 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 4114 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4115 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4116 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4117 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4118 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4119 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 4120 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4121 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4122 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 |
| 4123 | #else |
| 4124 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 |
| 4125 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 4126 | |
David Horstmann | 92b258b | 2024-02-13 17:23:34 +0000 | [diff] [blame] | 4127 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 4128 | #define SSL_SERIALIZED_SESSION_CONFIG_SNI 1 |
| 4129 | #else |
| 4130 | #define SSL_SERIALIZED_SESSION_CONFIG_SNI 0 |
| 4131 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 4132 | |
| 4133 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 4134 | #define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA 1 |
| 4135 | #else |
| 4136 | #define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA 0 |
| 4137 | #endif /* MBEDTLS_SSL_EARLY_DATA */ |
| 4138 | |
| 4139 | #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) |
| 4140 | #define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE 1 |
| 4141 | #else |
| 4142 | #define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE 0 |
| 4143 | #endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */ |
| 4144 | |
Waleed Elmelegy | 7dfba34 | 2024-03-12 16:22:39 +0000 | [diff] [blame] | 4145 | #if defined(MBEDTLS_SSL_ALPN) && defined(MBEDTLS_SSL_SRV_C) && \ |
| 4146 | defined(MBEDTLS_SSL_EARLY_DATA) |
Waleed Elmelegy | 1102563 | 2024-03-07 15:25:52 +0000 | [diff] [blame] | 4147 | #define SSL_SERIALIZED_SESSION_CONFIG_ALPN 1 |
| 4148 | #else |
| 4149 | #define SSL_SERIALIZED_SESSION_CONFIG_ALPN 0 |
| 4150 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4151 | |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4152 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0 |
| 4153 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1 |
| 4154 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 |
| 4155 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3 |
Hanno Becker | 37bdbe6 | 2021-08-01 05:38:58 +0100 | [diff] [blame] | 4156 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4 |
| 4157 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5 |
David Horstmann | f686f1d | 2024-03-01 11:20:32 +0000 | [diff] [blame] | 4158 | #define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT 6 |
David Horstmann | 92b258b | 2024-02-13 17:23:34 +0000 | [diff] [blame] | 4159 | #define SSL_SERIALIZED_SESSION_CONFIG_SNI_BIT 7 |
| 4160 | #define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA_BIT 8 |
| 4161 | #define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE_BIT 9 |
Waleed Elmelegy | 1102563 | 2024-03-07 15:25:52 +0000 | [diff] [blame] | 4162 | #define SSL_SERIALIZED_SESSION_CONFIG_ALPN_BIT 10 |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4163 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 4164 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4165 | ((uint16_t) ( \ |
| 4166 | (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \ |
| 4167 | (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \ |
| 4168 | (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \ |
| 4169 | SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \ |
| 4170 | (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \ |
| 4171 | (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \ |
David Horstmann | 5c5a32f | 2024-02-13 17:53:35 +0000 | [diff] [blame] | 4172 | (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT) | \ |
David Horstmann | 71fa1a9 | 2024-03-01 12:32:18 +0000 | [diff] [blame] | 4173 | (SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT << \ |
| 4174 | SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT) | \ |
David Horstmann | 92b258b | 2024-02-13 17:23:34 +0000 | [diff] [blame] | 4175 | (SSL_SERIALIZED_SESSION_CONFIG_SNI << SSL_SERIALIZED_SESSION_CONFIG_SNI_BIT) | \ |
| 4176 | (SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA << \ |
| 4177 | SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA_BIT) | \ |
| 4178 | (SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE << \ |
Waleed Elmelegy | 1102563 | 2024-03-07 15:25:52 +0000 | [diff] [blame] | 4179 | SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE_BIT) | \ |
Waleed Elmelegy | 75e33fa | 2024-03-07 16:57:58 +0000 | [diff] [blame] | 4180 | (SSL_SERIALIZED_SESSION_CONFIG_ALPN << \ |
Waleed Elmelegy | 1102563 | 2024-03-07 15:25:52 +0000 | [diff] [blame] | 4181 | SSL_SERIALIZED_SESSION_CONFIG_ALPN_BIT))) |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4182 | |
Chien Wong | 4e9683e | 2023-12-28 17:07:43 +0800 | [diff] [blame] | 4183 | static const unsigned char ssl_serialized_session_header[] = { |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4184 | MBEDTLS_VERSION_MAJOR, |
| 4185 | MBEDTLS_VERSION_MINOR, |
| 4186 | MBEDTLS_VERSION_PATCH, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4187 | MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG), |
| 4188 | MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG), |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 4189 | }; |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 4190 | |
| 4191 | /* |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4192 | * Serialize a session in the following format: |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4193 | * (in the presentation language of TLS, RFC 8446 section 3) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4194 | * |
David Horstmann | cb01b36 | 2024-02-29 17:31:46 +0000 | [diff] [blame] | 4195 | * TLS 1.2 session: |
| 4196 | * |
| 4197 | * struct { |
| 4198 | * #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4199 | * opaque ticket<0..2^24-1>; // length 0 means no ticket |
| 4200 | * uint32 ticket_lifetime; |
| 4201 | * #endif |
| 4202 | * } ClientOnlyData; |
| 4203 | * |
| 4204 | * struct { |
| 4205 | * #if defined(MBEDTLS_HAVE_TIME) |
| 4206 | * uint64 start_time; |
| 4207 | * #endif |
| 4208 | * uint8 session_id_len; // at most 32 |
| 4209 | * opaque session_id[32]; |
| 4210 | * opaque master[48]; // fixed length in the standard |
| 4211 | * uint32 verify_result; |
| 4212 | * #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE |
| 4213 | * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert |
| 4214 | * #else |
David Horstmann | 76ba26a | 2024-03-01 12:03:35 +0000 | [diff] [blame] | 4215 | * uint8 peer_cert_digest_type; |
David Horstmann | cb01b36 | 2024-02-29 17:31:46 +0000 | [diff] [blame] | 4216 | * opaque peer_cert_digest<0..2^8-1> |
| 4217 | * #endif |
| 4218 | * select (endpoint) { |
| 4219 | * case client: ClientOnlyData; |
| 4220 | * case server: uint64 ticket_creation_time; |
| 4221 | * }; |
| 4222 | * #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 4223 | * uint8 mfl_code; // up to 255 according to standard |
| 4224 | * #endif |
| 4225 | * #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 4226 | * uint8 encrypt_then_mac; // 0 or 1 |
| 4227 | * #endif |
| 4228 | * } serialized_session_tls12; |
| 4229 | * |
| 4230 | * |
| 4231 | * TLS 1.3 Session: |
| 4232 | * |
| 4233 | * struct { |
| 4234 | * #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 4235 | * opaque hostname<0..2^16-1>; |
| 4236 | * #endif |
| 4237 | * #if defined(MBEDTLS_HAVE_TIME) |
| 4238 | * uint64 ticket_reception_time; |
| 4239 | * #endif |
| 4240 | * uint32 ticket_lifetime; |
| 4241 | * opaque ticket<1..2^16-1>; |
| 4242 | * } ClientOnlyData; |
| 4243 | * |
| 4244 | * struct { |
| 4245 | * uint32 ticket_age_add; |
| 4246 | * uint8 ticket_flags; |
| 4247 | * opaque resumption_key<0..255>; |
| 4248 | * #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 4249 | * uint32 max_early_data_size; |
| 4250 | * #endif |
| 4251 | * #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) |
| 4252 | * uint16 record_size_limit; |
| 4253 | * #endif |
| 4254 | * select ( endpoint ) { |
| 4255 | * case client: ClientOnlyData; |
Waleed Elmelegy | 7dfba34 | 2024-03-12 16:22:39 +0000 | [diff] [blame] | 4256 | * case server: |
David Horstmann | cb01b36 | 2024-02-29 17:31:46 +0000 | [diff] [blame] | 4257 | * #if defined(MBEDTLS_HAVE_TIME) |
Waleed Elmelegy | 7dfba34 | 2024-03-12 16:22:39 +0000 | [diff] [blame] | 4258 | * uint64 ticket_creation_time; |
David Horstmann | cb01b36 | 2024-02-29 17:31:46 +0000 | [diff] [blame] | 4259 | * #endif |
Waleed Elmelegy | fe9ae08 | 2024-03-07 15:47:31 +0000 | [diff] [blame] | 4260 | * #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) |
Waleed Elmelegy | daa4da7 | 2024-03-13 16:25:34 +0000 | [diff] [blame] | 4261 | * opaque ticket_alpn<0..256>; |
David Horstmann | cb01b36 | 2024-02-29 17:31:46 +0000 | [diff] [blame] | 4262 | * #endif |
| 4263 | * }; |
| 4264 | * } serialized_session_tls13; |
| 4265 | * |
| 4266 | * |
| 4267 | * SSL session: |
| 4268 | * |
| 4269 | * struct { |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 4270 | * |
Hanno Becker | dce5097 | 2021-08-01 05:39:23 +0100 | [diff] [blame] | 4271 | * opaque mbedtls_version[3]; // library version: major, minor, patch |
| 4272 | * opaque session_format[2]; // library-version specific 16-bit field |
| 4273 | * // determining the format of the remaining |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4274 | * // serialized data. |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 4275 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4276 | * Note: When updating the format, remember to keep |
| 4277 | * these version+format bytes. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4278 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4279 | * // In this version, `session_format` determines |
| 4280 | * // the setting of those compile-time |
| 4281 | * // configuration options which influence |
| 4282 | * // the structure of mbedtls_ssl_session. |
| 4283 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 4284 | * uint8_t minor_ver; // Protocol minor version. Possible values: |
Jerry Yu | 0c2a738 | 2022-12-06 13:27:25 +0800 | [diff] [blame] | 4285 | * // - TLS 1.2 (0x0303) |
| 4286 | * // - TLS 1.3 (0x0304) |
David Horstmann | 531aca2 | 2024-02-29 18:14:28 +0000 | [diff] [blame] | 4287 | * uint8_t endpoint; |
| 4288 | * uint16_t ciphersuite; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4289 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 4290 | * select (serialized_session.tls_version) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4291 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 4292 | * case MBEDTLS_SSL_VERSION_TLS1_2: |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4293 | * serialized_session_tls12 data; |
Jerry Yu | 0c2a738 | 2022-12-06 13:27:25 +0800 | [diff] [blame] | 4294 | * case MBEDTLS_SSL_VERSION_TLS1_3: |
Jerry Yu | ddda050 | 2022-12-01 19:43:12 +0800 | [diff] [blame] | 4295 | * serialized_session_tls13 data; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4296 | * |
| 4297 | * }; |
| 4298 | * |
| 4299 | * } serialized_session; |
| 4300 | * |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4301 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4302 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4303 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4304 | static int ssl_session_save(const mbedtls_ssl_session *session, |
| 4305 | unsigned char omit_header, |
| 4306 | unsigned char *buf, |
| 4307 | size_t buf_len, |
| 4308 | size_t *olen) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4309 | { |
| 4310 | unsigned char *p = buf; |
| 4311 | size_t used = 0; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 4312 | size_t remaining_len; |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 4313 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4314 | size_t out_len; |
| 4315 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 4316 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4317 | if (session == NULL) { |
| 4318 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 4319 | } |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 4320 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4321 | if (!omit_header) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4322 | /* |
| 4323 | * Add Mbed TLS version identifier |
| 4324 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4325 | used += sizeof(ssl_serialized_session_header); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4326 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4327 | if (used <= buf_len) { |
| 4328 | memcpy(p, ssl_serialized_session_header, |
| 4329 | sizeof(ssl_serialized_session_header)); |
| 4330 | p += sizeof(ssl_serialized_session_header); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4331 | } |
| 4332 | } |
| 4333 | |
| 4334 | /* |
Ronald Cron | 40a4ab0 | 2024-01-15 10:21:30 +0100 | [diff] [blame] | 4335 | * TLS version identifier, endpoint, ciphersuite |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4336 | */ |
Ronald Cron | 40a4ab0 | 2024-01-15 10:21:30 +0100 | [diff] [blame] | 4337 | used += 1 /* TLS version */ |
| 4338 | + 1 /* endpoint */ |
| 4339 | + 2; /* ciphersuite */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4340 | if (used <= buf_len) { |
| 4341 | *p++ = MBEDTLS_BYTE_0(session->tls_version); |
Ronald Cron | 40a4ab0 | 2024-01-15 10:21:30 +0100 | [diff] [blame] | 4342 | *p++ = session->endpoint; |
| 4343 | MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0); |
| 4344 | p += 2; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4345 | } |
| 4346 | |
| 4347 | /* Forward to version-specific serialization routine. */ |
Jerry Yu | fca4d57 | 2022-07-21 10:37:48 +0800 | [diff] [blame] | 4348 | remaining_len = (buf_len >= used) ? buf_len - used : 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4349 | switch (session->tls_version) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4350 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4351 | case MBEDTLS_SSL_VERSION_TLS1_2: |
| 4352 | used += ssl_tls12_session_save(session, p, remaining_len); |
| 4353 | break; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4354 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4355 | |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 4356 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4357 | case MBEDTLS_SSL_VERSION_TLS1_3: |
| 4358 | ret = ssl_tls13_session_save(session, p, remaining_len, &out_len); |
| 4359 | if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) { |
| 4360 | return ret; |
| 4361 | } |
| 4362 | used += out_len; |
| 4363 | break; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 4364 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 4365 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4366 | default: |
| 4367 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4368 | } |
| 4369 | |
| 4370 | *olen = used; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4371 | if (used > buf_len) { |
| 4372 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 4373 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4374 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4375 | return 0; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4376 | } |
| 4377 | |
| 4378 | /* |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4379 | * Public wrapper for ssl_session_save() |
| 4380 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4381 | int mbedtls_ssl_session_save(const mbedtls_ssl_session *session, |
| 4382 | unsigned char *buf, |
| 4383 | size_t buf_len, |
| 4384 | size_t *olen) |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4385 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4386 | return ssl_session_save(session, 0, buf, buf_len, olen); |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4387 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4388 | |
Jerry Yu | f1b23ca | 2022-02-18 11:48:47 +0800 | [diff] [blame] | 4389 | /* |
| 4390 | * Deserialize session, see mbedtls_ssl_session_save() for format. |
| 4391 | * |
| 4392 | * This internal version is wrapped by a public function that cleans up in |
| 4393 | * case of error, and has an extra option omit_header. |
| 4394 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4395 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4396 | static int ssl_session_load(mbedtls_ssl_session *session, |
| 4397 | unsigned char omit_header, |
| 4398 | const unsigned char *buf, |
| 4399 | size_t len) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4400 | { |
| 4401 | const unsigned char *p = buf; |
| 4402 | const unsigned char * const end = buf + len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 4403 | size_t remaining_len; |
| 4404 | |
| 4405 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4406 | if (session == NULL) { |
| 4407 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 4408 | } |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4409 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4410 | if (!omit_header) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4411 | /* |
| 4412 | * Check Mbed TLS version identifier |
| 4413 | */ |
| 4414 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4415 | if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) { |
| 4416 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4417 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4418 | |
| 4419 | if (memcmp(p, ssl_serialized_session_header, |
| 4420 | sizeof(ssl_serialized_session_header)) != 0) { |
| 4421 | return MBEDTLS_ERR_SSL_VERSION_MISMATCH; |
| 4422 | } |
| 4423 | p += sizeof(ssl_serialized_session_header); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4424 | } |
| 4425 | |
| 4426 | /* |
Ronald Cron | 40a4ab0 | 2024-01-15 10:21:30 +0100 | [diff] [blame] | 4427 | * TLS version identifier, endpoint, ciphersuite |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4428 | */ |
Ronald Cron | 40a4ab0 | 2024-01-15 10:21:30 +0100 | [diff] [blame] | 4429 | if (4 > (size_t) (end - p)) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4430 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4431 | } |
Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 4432 | session->tls_version = (mbedtls_ssl_protocol_version) (0x0300 | *p++); |
Ronald Cron | 40a4ab0 | 2024-01-15 10:21:30 +0100 | [diff] [blame] | 4433 | session->endpoint = *p++; |
| 4434 | session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 0); |
| 4435 | p += 2; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4436 | |
| 4437 | /* Dispatch according to TLS version. */ |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 4438 | remaining_len = (size_t) (end - p); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4439 | switch (session->tls_version) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4440 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4441 | case MBEDTLS_SSL_VERSION_TLS1_2: |
| 4442 | return ssl_tls12_session_load(session, p, remaining_len); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4443 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4444 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 4445 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4446 | case MBEDTLS_SSL_VERSION_TLS1_3: |
| 4447 | return ssl_tls13_session_load(session, p, remaining_len); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 4448 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 4449 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4450 | default: |
| 4451 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4452 | } |
| 4453 | } |
| 4454 | |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4455 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 4456 | * Deserialize session: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4457 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4458 | int mbedtls_ssl_session_load(mbedtls_ssl_session *session, |
| 4459 | const unsigned char *buf, |
| 4460 | size_t len) |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4461 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4462 | int ret = ssl_session_load(session, 0, buf, len); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4463 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4464 | if (ret != 0) { |
| 4465 | mbedtls_ssl_session_free(session); |
| 4466 | } |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4467 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4468 | return ret; |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4469 | } |
| 4470 | |
| 4471 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4472 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4473 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4474 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4475 | static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl) |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 4476 | { |
| 4477 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 4478 | |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 4479 | /* |
| 4480 | * 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] | 4481 | * that were written into the output buffer by the previous handshake step, |
| 4482 | * if the write to the network callback returned with the |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 4483 | * #MBEDTLS_ERR_SSL_WANT_WRITE error code. |
| 4484 | * We proceed to the next handshake step only when all data from the |
| 4485 | * previous one have been sent to the peer, thus we make sure that this is |
| 4486 | * the case here by calling `mbedtls_ssl_flush_output()`. The function may |
| 4487 | * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case |
| 4488 | * we have to wait before to go ahead. |
| 4489 | * In the case of TLS 1.3, handshake step handlers do not send data to the |
| 4490 | * peer. Data are only sent here and through |
| 4491 | * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an |
Andrzej Kurek | 5c65c57 | 2022-04-13 14:28:52 -0400 | [diff] [blame] | 4492 | * alert occurred. |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 4493 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4494 | if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) { |
| 4495 | return ret; |
| 4496 | } |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 4497 | |
| 4498 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4499 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 4500 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) { |
| 4501 | if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) { |
| 4502 | return ret; |
| 4503 | } |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 4504 | } |
| 4505 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4506 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4507 | return ret; |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 4508 | } |
| 4509 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4510 | int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4511 | { |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 4512 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4513 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4514 | if (ssl == NULL || |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 4515 | ssl->conf == NULL || |
| 4516 | ssl->handshake == NULL || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4517 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 4518 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 4519 | } |
| 4520 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4521 | ret = ssl_prepare_handshake_step(ssl); |
| 4522 | if (ret != 0) { |
| 4523 | return ret; |
| 4524 | } |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 4525 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4526 | ret = mbedtls_ssl_handle_pending_alert(ssl); |
| 4527 | if (ret != 0) { |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 4528 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4529 | } |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 4530 | |
Tom Cosgrove | 2fdc7b3 | 2022-09-21 12:33:17 +0100 | [diff] [blame] | 4531 | /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or |
| 4532 | * MBEDTLS_SSL_IS_SERVER, this is the return code we give */ |
| 4533 | ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4534 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4535 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4536 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 4537 | MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s", |
Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 4538 | mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state))); |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 4539 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4540 | switch (ssl->state) { |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 4541 | case MBEDTLS_SSL_HELLO_REQUEST: |
| 4542 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; |
Tom Cosgrove | 87d9c6c | 2022-09-22 09:27:56 +0100 | [diff] [blame] | 4543 | ret = 0; |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 4544 | break; |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 4545 | |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 4546 | case MBEDTLS_SSL_CLIENT_HELLO: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4547 | ret = mbedtls_ssl_write_client_hello(ssl); |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 4548 | break; |
| 4549 | |
| 4550 | default: |
| 4551 | #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] | 4552 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
| 4553 | ret = mbedtls_ssl_tls13_handshake_client_step(ssl); |
| 4554 | } else { |
| 4555 | ret = mbedtls_ssl_handshake_client_step(ssl); |
| 4556 | } |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 4557 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4558 | ret = mbedtls_ssl_handshake_client_step(ssl); |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 4559 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4560 | ret = mbedtls_ssl_tls13_handshake_client_step(ssl); |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 4561 | #endif |
| 4562 | } |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 4563 | } |
Ronald Cron | 6291b23 | 2023-03-08 15:51:25 +0100 | [diff] [blame] | 4564 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 4565 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4566 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4567 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Ronald Cron | 6291b23 | 2023-03-08 15:51:25 +0100 | [diff] [blame] | 4568 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4569 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4570 | ret = mbedtls_ssl_tls13_handshake_server_step(ssl); |
Ronald Cron | 6291b23 | 2023-03-08 15:51:25 +0100 | [diff] [blame] | 4571 | } else { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4572 | ret = mbedtls_ssl_handshake_server_step(ssl); |
| 4573 | } |
Ronald Cron | 6291b23 | 2023-03-08 15:51:25 +0100 | [diff] [blame] | 4574 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4575 | ret = mbedtls_ssl_handshake_server_step(ssl); |
| 4576 | #else |
| 4577 | ret = mbedtls_ssl_tls13_handshake_server_step(ssl); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4578 | #endif |
Ronald Cron | 6291b23 | 2023-03-08 15:51:25 +0100 | [diff] [blame] | 4579 | } |
| 4580 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4581 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4582 | if (ret != 0) { |
Jerry Yu | bbd5a3f | 2021-09-18 20:50:22 +0800 | [diff] [blame] | 4583 | /* handshake_step return error. And it is same |
| 4584 | * with alert_reason. |
| 4585 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4586 | if (ssl->send_alert) { |
| 4587 | ret = mbedtls_ssl_handle_pending_alert(ssl); |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 4588 | goto cleanup; |
| 4589 | } |
| 4590 | } |
| 4591 | |
| 4592 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4593 | return ret; |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4594 | } |
| 4595 | |
| 4596 | /* |
| 4597 | * Perform the SSL handshake |
| 4598 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4599 | int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4600 | { |
| 4601 | int ret = 0; |
| 4602 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 4603 | /* Sanity checks */ |
| 4604 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4605 | if (ssl == NULL || ssl->conf == NULL) { |
| 4606 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4607 | } |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 4608 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 4609 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4610 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 4611 | (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) { |
| 4612 | MBEDTLS_SSL_DEBUG_MSG(1, ("You must use " |
| 4613 | "mbedtls_ssl_set_timer_cb() for DTLS")); |
| 4614 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 4615 | } |
| 4616 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4617 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4618 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake")); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4619 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 4620 | /* Main handshake loop */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4621 | while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 4622 | ret = mbedtls_ssl_handshake_step(ssl); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4623 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4624 | if (ret != 0) { |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4625 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4626 | } |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4627 | } |
| 4628 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4629 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake")); |
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 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4632 | } |
| 4633 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4634 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 4635 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4636 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4637 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4638 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4639 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4640 | static int ssl_write_hello_request(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4641 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4642 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4643 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4644 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request")); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4645 | |
| 4646 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4647 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 4648 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4649 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4650 | if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { |
| 4651 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret); |
| 4652 | return ret; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4653 | } |
| 4654 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4655 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request")); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4656 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4657 | return 0; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4658 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4659 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4660 | |
| 4661 | /* |
| 4662 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4663 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 4664 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 4665 | * - 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] | 4666 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4667 | * 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] | 4668 | * 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] | 4669 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4670 | int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4671 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4672 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4673 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4674 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate")); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4676 | if ((ret = ssl_handshake_init(ssl)) != 0) { |
| 4677 | return ret; |
| 4678 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4679 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 4680 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 4681 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4682 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4683 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 4684 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) { |
| 4685 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4686 | ssl->handshake->out_msg_seq = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4687 | } else { |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4688 | ssl->handshake->in_msg_seq = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4689 | } |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 4690 | } |
| 4691 | #endif |
| 4692 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4693 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 4694 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4695 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4696 | if ((ret = mbedtls_ssl_handshake(ssl)) != 0) { |
| 4697 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret); |
| 4698 | return ret; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4699 | } |
| 4700 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4701 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate")); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4702 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4703 | return 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4704 | } |
| 4705 | |
| 4706 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4707 | * Renegotiate current connection on client, |
| 4708 | * or request renegotiation on server |
| 4709 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4710 | int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4711 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4712 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4713 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4714 | if (ssl == NULL || ssl->conf == NULL) { |
| 4715 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4716 | } |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 4717 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4718 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4719 | /* On server, just send the request */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4720 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 4721 | if (mbedtls_ssl_is_handshake_over(ssl) == 0) { |
| 4722 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4723 | } |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4724 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4725 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 4726 | |
| 4727 | /* Did we already try/start sending HelloRequest? */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4728 | if (ssl->out_left != 0) { |
| 4729 | return mbedtls_ssl_flush_output(ssl); |
| 4730 | } |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 4731 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4732 | return ssl_write_hello_request(ssl); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4733 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4734 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4735 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4736 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4737 | /* |
| 4738 | * On client, either start the renegotiation process or, |
| 4739 | * if already in progress, continue the handshake |
| 4740 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4741 | if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) { |
| 4742 | if (mbedtls_ssl_is_handshake_over(ssl) == 0) { |
| 4743 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4744 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4745 | |
| 4746 | if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) { |
| 4747 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret); |
| 4748 | return ret; |
| 4749 | } |
| 4750 | } else { |
| 4751 | if ((ret = mbedtls_ssl_handshake(ssl)) != 0) { |
| 4752 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret); |
| 4753 | return ret; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4754 | } |
| 4755 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4756 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4757 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4758 | return ret; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4759 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4760 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4761 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4762 | void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4763 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 4764 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 4765 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4766 | if (handshake == NULL) { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4767 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4768 | } |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4769 | |
Elena Uziunaite | 8dde3b3 | 2024-07-05 12:10:21 +0100 | [diff] [blame] | 4770 | #if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4771 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4772 | if (ssl->handshake->group_list_heap_allocated) { |
| 4773 | mbedtls_free((void *) handshake->group_list); |
| 4774 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4775 | handshake->group_list = NULL; |
| 4776 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Elena Uziunaite | 8dde3b3 | 2024-07-05 12:10:21 +0100 | [diff] [blame] | 4777 | #endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */ |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4778 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4779 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4780 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4781 | if (ssl->handshake->sig_algs_heap_allocated) { |
| 4782 | mbedtls_free((void *) handshake->sig_algs); |
| 4783 | } |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4784 | handshake->sig_algs = NULL; |
| 4785 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Xiaofei Bai | c234ecf | 2022-02-08 09:59:23 +0000 | [diff] [blame] | 4786 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4787 | if (ssl->handshake->certificate_request_context) { |
| 4788 | mbedtls_free((void *) handshake->certificate_request_context); |
Xiaofei Bai | c234ecf | 2022-02-08 09:59:23 +0000 | [diff] [blame] | 4789 | } |
| 4790 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4791 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4792 | |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4793 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4794 | if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) { |
| 4795 | ssl->conf->f_async_cancel(ssl); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4796 | handshake->async_in_progress = 0; |
| 4797 | } |
| 4798 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 4799 | |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 4800 | #if defined(PSA_WANT_ALG_SHA_256) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4801 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4802 | psa_hash_abort(&handshake->fin_sha256_psa); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4803 | #else |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 4804 | mbedtls_md_free(&handshake->fin_sha256); |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 4805 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4806 | #endif |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 4807 | #if defined(PSA_WANT_ALG_SHA_384) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4808 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4809 | psa_hash_abort(&handshake->fin_sha384_psa); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4810 | #else |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 4811 | mbedtls_md_free(&handshake->fin_sha384); |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 4812 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4813 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 4814 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4815 | #if defined(MBEDTLS_DHM_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4816 | mbedtls_dhm_free(&handshake->dhm_ctx); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4817 | #endif |
Valerio Setti | 7aeec54 | 2023-07-05 18:57:21 +0200 | [diff] [blame] | 4818 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Valerio Setti | 6eb0054 | 2023-07-07 17:04:24 +0200 | [diff] [blame] | 4819 | defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4820 | mbedtls_ecdh_free(&handshake->ecdh_ctx); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 4821 | #endif |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 4822 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 4823 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 4824 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4825 | psa_pake_abort(&handshake->psa_pake_ctx); |
Valerio Setti | eb3f788 | 2022-12-08 18:42:58 +0100 | [diff] [blame] | 4826 | /* |
| 4827 | * Opaque keys are not stored in the handshake's data and it's the user |
| 4828 | * responsibility to destroy them. Clear ones, instead, are created by |
| 4829 | * the TLS library and should be destroyed at the same level |
| 4830 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4831 | if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) { |
| 4832 | psa_destroy_key(handshake->psa_pake_password); |
Valerio Setti | eb3f788 | 2022-12-08 18:42:58 +0100 | [diff] [blame] | 4833 | } |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 4834 | handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT; |
| 4835 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4836 | mbedtls_ecjpake_free(&handshake->ecjpake_ctx); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 4837 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 4838 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4839 | mbedtls_free(handshake->ecjpake_cache); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 4840 | handshake->ecjpake_cache = NULL; |
| 4841 | handshake->ecjpake_cache_len = 0; |
| 4842 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 4843 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 4844 | |
Valerio Setti | 7aeec54 | 2023-07-05 18:57:21 +0200 | [diff] [blame] | 4845 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_ANY_ENABLED) || \ |
Valerio Setti | 45d56f3 | 2023-07-13 17:23:20 +0200 | [diff] [blame] | 4846 | defined(MBEDTLS_KEY_EXCHANGE_WITH_ECDSA_ANY_ENABLED) || \ |
Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 4847 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 4848 | /* explicit void pointer cast for buggy MS compiler */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4849 | mbedtls_free((void *) handshake->curves_tls_id); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 4850 | #endif |
| 4851 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 4852 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4853 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4854 | if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4855 | /* The maintenance of the external PSK key slot is the |
| 4856 | * user's responsibility. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4857 | if (ssl->handshake->psk_opaque_is_internal) { |
| 4858 | psa_destroy_key(ssl->handshake->psk_opaque); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4859 | ssl->handshake->psk_opaque_is_internal = 0; |
| 4860 | } |
| 4861 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 4862 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 4863 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4864 | if (handshake->psk != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 4865 | mbedtls_zeroize_and_free(handshake->psk, handshake->psk_len); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 4866 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 4867 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 4868 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 4869 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4870 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 4871 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 4872 | /* |
| 4873 | * Free only the linked list wrapper, not the keys themselves |
| 4874 | * since the belong to the SNI callback |
| 4875 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4876 | ssl_key_cert_free(handshake->sni_key_cert); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4877 | #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] | 4878 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4879 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4880 | mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx); |
| 4881 | if (handshake->ecrs_peer_cert != NULL) { |
| 4882 | mbedtls_x509_crt_free(handshake->ecrs_peer_cert); |
| 4883 | mbedtls_free(handshake->ecrs_peer_cert); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 4884 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 4885 | #endif |
| 4886 | |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 4887 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 4888 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4889 | mbedtls_pk_free(&handshake->peer_pubkey); |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 4890 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4891 | |
XiaokangQian | 9b93c0d | 2022-02-09 06:02:25 +0000 | [diff] [blame] | 4892 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4893 | (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3)) |
| 4894 | mbedtls_free(handshake->cookie); |
XiaokangQian | 9b93c0d | 2022-02-09 06:02:25 +0000 | [diff] [blame] | 4895 | #endif /* MBEDTLS_SSL_CLI_C && |
| 4896 | ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */ |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 4897 | |
| 4898 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4899 | mbedtls_ssl_flight_free(handshake->flight); |
| 4900 | mbedtls_ssl_buffering_free(ssl); |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 4901 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 4902 | |
Valerio Setti | ea59c43 | 2023-07-25 11:14:03 +0200 | [diff] [blame] | 4903 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED) |
Przemek Stekiel | 7ac93be | 2023-07-04 10:02:38 +0200 | [diff] [blame] | 4904 | if (handshake->xxdh_psa_privkey_is_external == 0) { |
| 4905 | psa_destroy_key(handshake->xxdh_psa_privkey); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4906 | } |
Valerio Setti | ea59c43 | 2023-07-25 11:14:03 +0200 | [diff] [blame] | 4907 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED */ |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 4908 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4909 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4910 | mbedtls_ssl_transform_free(handshake->transform_handshake); |
| 4911 | mbedtls_free(handshake->transform_handshake); |
Jerry Yu | 3d9b590 | 2022-11-04 14:07:25 +0800 | [diff] [blame] | 4912 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4913 | mbedtls_ssl_transform_free(handshake->transform_earlydata); |
| 4914 | mbedtls_free(handshake->transform_earlydata); |
Jerry Yu | 3d9b590 | 2022-11-04 14:07:25 +0800 | [diff] [blame] | 4915 | #endif |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4916 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 4917 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4918 | |
| 4919 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4920 | /* If the buffers are too big - reallocate. Because of the way Mbed TLS |
| 4921 | * processes datagrams and the fact that a datagram is allowed to have |
| 4922 | * several records in it, it is possible that the I/O buffers are not |
| 4923 | * empty at this stage */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4924 | handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl), |
| 4925 | mbedtls_ssl_get_output_buflen(ssl)); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4926 | #endif |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 4927 | |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 4928 | /* mbedtls_platform_zeroize MUST be last one in this function */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4929 | mbedtls_platform_zeroize(handshake, |
| 4930 | sizeof(mbedtls_ssl_handshake_params)); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4931 | } |
| 4932 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4933 | void mbedtls_ssl_session_free(mbedtls_ssl_session *session) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4934 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4935 | if (session == NULL) { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4936 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4937 | } |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4938 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4939 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4940 | ssl_clear_peer_cert(session); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4941 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 4942 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4943 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Xiaokang Qian | bc663a0 | 2022-10-09 11:14:39 +0000 | [diff] [blame] | 4944 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 4945 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4946 | mbedtls_free(session->hostname); |
Xiaokang Qian | 281fd1b | 2022-09-20 11:35:41 +0000 | [diff] [blame] | 4947 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4948 | mbedtls_free(session->ticket); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 4949 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 4950 | |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 4951 | #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) && \ |
| 4952 | defined(MBEDTLS_SSL_SRV_C) |
Waleed Elmelegy | 883f77c | 2024-03-06 19:09:41 +0000 | [diff] [blame] | 4953 | mbedtls_free(session->ticket_alpn); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4954 | #endif |
| 4955 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4956 | mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session)); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4957 | } |
| 4958 | |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 4959 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4960 | |
| 4961 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4962 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u |
| 4963 | #else |
| 4964 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u |
| 4965 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4966 | |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4967 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4968 | |
| 4969 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4970 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u |
| 4971 | #else |
| 4972 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u |
| 4973 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 4974 | |
| 4975 | #if defined(MBEDTLS_SSL_ALPN) |
| 4976 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u |
| 4977 | #else |
| 4978 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u |
| 4979 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4980 | |
| 4981 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0 |
| 4982 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1 |
| 4983 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2 |
| 4984 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3 |
| 4985 | |
| 4986 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4987 | ((uint32_t) ( \ |
| 4988 | (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \ |
| 4989 | SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \ |
| 4990 | (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \ |
| 4991 | SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \ |
| 4992 | (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \ |
| 4993 | SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \ |
| 4994 | (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \ |
| 4995 | 0u)) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4996 | |
Chien Wong | 4e9683e | 2023-12-28 17:07:43 +0800 | [diff] [blame] | 4997 | static const unsigned char ssl_serialized_context_header[] = { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4998 | MBEDTLS_VERSION_MAJOR, |
| 4999 | MBEDTLS_VERSION_MINOR, |
| 5000 | MBEDTLS_VERSION_PATCH, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5001 | MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG), |
| 5002 | MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG), |
| 5003 | MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG), |
| 5004 | MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG), |
| 5005 | MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG), |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5006 | }; |
| 5007 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5008 | /* |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5009 | * Serialize a full SSL context |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 5010 | * |
| 5011 | * The format of the serialized data is: |
| 5012 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 5013 | * |
| 5014 | * // header |
| 5015 | * opaque mbedtls_version[3]; // major, minor, patch |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5016 | * opaque context_format[5]; // version-specific field determining |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 5017 | * // the format of the remaining |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5018 | * // serialized data. |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 5019 | * Note: When updating the format, remember to keep these |
| 5020 | * 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] | 5021 | * |
| 5022 | * // session sub-structure |
| 5023 | * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save() |
| 5024 | * // transform sub-structure |
| 5025 | * uint8 random[64]; // ServerHello.random+ClientHello.random |
| 5026 | * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value |
| 5027 | * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use |
| 5028 | * // fields from ssl_context |
| 5029 | * uint32 badmac_seen; // DTLS: number of records with failing MAC |
| 5030 | * uint64 in_window_top; // DTLS: last validated record seq_num |
| 5031 | * uint64 in_window; // DTLS: bitmask for replay protection |
| 5032 | * uint8 disable_datagram_packing; // DTLS: only one record per datagram |
| 5033 | * uint64 cur_out_ctr; // Record layer: outgoing sequence number |
| 5034 | * uint16 mtu; // DTLS: path mtu (max outgoing fragment size) |
| 5035 | * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol |
| 5036 | * |
| 5037 | * Note that many fields of the ssl_context or sub-structures are not |
| 5038 | * serialized, as they fall in one of the following categories: |
| 5039 | * |
| 5040 | * 1. forced value (eg in_left must be 0) |
| 5041 | * 2. pointer to dynamically-allocated memory (eg session, transform) |
| 5042 | * 3. value can be re-derived from other data (eg session keys from MS) |
| 5043 | * 4. value was temporary (eg content of input buffer) |
| 5044 | * 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] | 5045 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5046 | int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl, |
| 5047 | unsigned char *buf, |
| 5048 | size_t buf_len, |
| 5049 | size_t *olen) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5050 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5051 | unsigned char *p = buf; |
| 5052 | size_t used = 0; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5053 | size_t session_len; |
| 5054 | int ret = 0; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5055 | |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 5056 | /* |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5057 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in |
| 5058 | * this function's documentation. |
| 5059 | * |
| 5060 | * These are due to assumptions/limitations in the implementation. Some of |
| 5061 | * them are likely to stay (no handshake in progress) some might go away |
| 5062 | * (only DTLS) but are currently used to simplify the implementation. |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 5063 | */ |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5064 | /* The initial handshake must be over */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5065 | if (mbedtls_ssl_is_handshake_over(ssl) == 0) { |
| 5066 | MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over")); |
| 5067 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5068 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5069 | if (ssl->handshake != NULL) { |
| 5070 | MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed")); |
| 5071 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5072 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5073 | /* Double-check that sub-structures are indeed ready */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5074 | if (ssl->transform == NULL || ssl->session == NULL) { |
| 5075 | MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready")); |
| 5076 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5077 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5078 | /* There must be no pending incoming or outgoing data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5079 | if (mbedtls_ssl_check_pending(ssl) != 0) { |
| 5080 | MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data")); |
| 5081 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5082 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5083 | if (ssl->out_left != 0) { |
| 5084 | MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data")); |
| 5085 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5086 | } |
Dave Rodgman | 556e8a3 | 2022-12-06 16:31:25 +0000 | [diff] [blame] | 5087 | /* Protocol must be DTLS, not TLS */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5088 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 5089 | MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported")); |
| 5090 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5091 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5092 | /* Version must be 1.2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5093 | if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) { |
| 5094 | MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported")); |
| 5095 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5096 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5097 | /* We must be using an AEAD ciphersuite */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5098 | if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) { |
| 5099 | MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported")); |
| 5100 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5101 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5102 | /* Renegotiation must not be enabled */ |
| 5103 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5104 | if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) { |
| 5105 | MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled")); |
| 5106 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5107 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5108 | #endif |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5109 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5110 | /* |
| 5111 | * Version and format identifier |
| 5112 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5113 | used += sizeof(ssl_serialized_context_header); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5114 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5115 | if (used <= buf_len) { |
| 5116 | memcpy(p, ssl_serialized_context_header, |
| 5117 | sizeof(ssl_serialized_context_header)); |
| 5118 | p += sizeof(ssl_serialized_context_header); |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5119 | } |
| 5120 | |
| 5121 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5122 | * Session (length + data) |
| 5123 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5124 | ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len); |
| 5125 | if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) { |
| 5126 | return ret; |
| 5127 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5128 | |
| 5129 | used += 4 + session_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5130 | if (used <= buf_len) { |
| 5131 | MBEDTLS_PUT_UINT32_BE(session_len, p, 0); |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5132 | p += 4; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5133 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5134 | ret = ssl_session_save(ssl->session, 1, |
| 5135 | p, session_len, &session_len); |
| 5136 | if (ret != 0) { |
| 5137 | return ret; |
| 5138 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5139 | |
| 5140 | p += session_len; |
| 5141 | } |
| 5142 | |
| 5143 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5144 | * Transform |
| 5145 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5146 | used += sizeof(ssl->transform->randbytes); |
| 5147 | if (used <= buf_len) { |
| 5148 | memcpy(p, ssl->transform->randbytes, |
| 5149 | sizeof(ssl->transform->randbytes)); |
| 5150 | p += sizeof(ssl->transform->randbytes); |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5151 | } |
| 5152 | |
| 5153 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Dave Rodgman | c37ad44 | 2023-11-03 23:36:06 +0000 | [diff] [blame] | 5154 | used += 2U + ssl->transform->in_cid_len + ssl->transform->out_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5155 | if (used <= buf_len) { |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5156 | *p++ = ssl->transform->in_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5157 | 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] | 5158 | p += ssl->transform->in_cid_len; |
| 5159 | |
| 5160 | *p++ = ssl->transform->out_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5161 | 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] | 5162 | p += ssl->transform->out_cid_len; |
| 5163 | } |
| 5164 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 5165 | |
| 5166 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5167 | * Saved fields from top-level ssl_context structure |
| 5168 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5169 | used += 4; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5170 | if (used <= buf_len) { |
| 5171 | MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0); |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5172 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5173 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5174 | |
| 5175 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 5176 | used += 16; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5177 | if (used <= buf_len) { |
| 5178 | MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0); |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5179 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5181 | MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0); |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5182 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5183 | } |
| 5184 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 5185 | |
| 5186 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5187 | used += 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5188 | if (used <= buf_len) { |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5189 | *p++ = ssl->disable_datagram_packing; |
| 5190 | } |
| 5191 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5192 | |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 5193 | used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5194 | if (used <= buf_len) { |
| 5195 | memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN); |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 5196 | p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5197 | } |
| 5198 | |
| 5199 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5200 | used += 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5201 | if (used <= buf_len) { |
| 5202 | MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0); |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5203 | p += 2; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5204 | } |
| 5205 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5206 | |
| 5207 | #if defined(MBEDTLS_SSL_ALPN) |
| 5208 | { |
| 5209 | const uint8_t alpn_len = ssl->alpn_chosen |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5210 | ? (uint8_t) strlen(ssl->alpn_chosen) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5211 | : 0; |
| 5212 | |
| 5213 | used += 1 + alpn_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5214 | if (used <= buf_len) { |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5215 | *p++ = alpn_len; |
| 5216 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5217 | if (ssl->alpn_chosen != NULL) { |
| 5218 | memcpy(p, ssl->alpn_chosen, alpn_len); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5219 | p += alpn_len; |
| 5220 | } |
| 5221 | } |
| 5222 | } |
| 5223 | #endif /* MBEDTLS_SSL_ALPN */ |
| 5224 | |
| 5225 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5226 | * Done |
| 5227 | */ |
| 5228 | *olen = used; |
| 5229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5230 | if (used > buf_len) { |
| 5231 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 5232 | } |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5233 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5234 | MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5235 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5236 | return mbedtls_ssl_session_reset_int(ssl, 0); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5237 | } |
| 5238 | |
| 5239 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 5240 | * Deserialize context, see mbedtls_ssl_context_save() for format. |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5241 | * |
| 5242 | * This internal version is wrapped by a public function that cleans up in |
| 5243 | * case of error. |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5244 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5245 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5246 | static int ssl_context_load(mbedtls_ssl_context *ssl, |
| 5247 | const unsigned char *buf, |
| 5248 | size_t len) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5249 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5250 | const unsigned char *p = buf; |
| 5251 | const unsigned char * const end = buf + len; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5252 | size_t session_len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5253 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 5254 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 5255 | tls_prf_fn prf_func = NULL; |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 5256 | #endif |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5257 | |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 5258 | /* |
| 5259 | * The context should have been freshly setup or reset. |
| 5260 | * Give the user an error in case of obvious misuse. |
Manuel Pégourié-Gonnard | 4ca930f | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 5261 | * (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] | 5262 | * renegotiating, or if the user mistakenly loaded a session first.) |
| 5263 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5264 | if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST || |
| 5265 | ssl->session != NULL) { |
| 5266 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 5267 | } |
| 5268 | |
| 5269 | /* |
| 5270 | * We can't check that the config matches the initial one, but we can at |
| 5271 | * least check it matches the requirements for serializing. |
| 5272 | */ |
Dave Rodgman | e99b24d | 2023-09-14 15:45:03 +0100 | [diff] [blame] | 5273 | if ( |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 5274 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 5275 | ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 5276 | #endif |
Dave Rodgman | e99b24d | 2023-09-14 15:45:03 +0100 | [diff] [blame] | 5277 | ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
| 5278 | ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 || |
| 5279 | ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 |
| 5280 | ) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5281 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 5282 | } |
| 5283 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5284 | MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5285 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5286 | /* |
| 5287 | * Check version identifier |
| 5288 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5289 | if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) { |
| 5290 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5291 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5292 | |
| 5293 | if (memcmp(p, ssl_serialized_context_header, |
| 5294 | sizeof(ssl_serialized_context_header)) != 0) { |
| 5295 | return MBEDTLS_ERR_SSL_VERSION_MISMATCH; |
| 5296 | } |
| 5297 | p += sizeof(ssl_serialized_context_header); |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5298 | |
| 5299 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5300 | * Session |
| 5301 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5302 | if ((size_t) (end - p) < 4) { |
| 5303 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5304 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5305 | |
Thomas Daubney | f9f0ba8 | 2023-05-23 17:34:33 +0100 | [diff] [blame] | 5306 | session_len = MBEDTLS_GET_UINT32_BE(p, 0); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5307 | p += 4; |
| 5308 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 5309 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 5310 | * 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] | 5311 | ssl->session = ssl->session_negotiate; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5312 | ssl->session_in = ssl->session; |
| 5313 | ssl->session_out = ssl->session; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 5314 | ssl->session_negotiate = NULL; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5315 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5316 | if ((size_t) (end - p) < session_len) { |
| 5317 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5318 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5319 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5320 | ret = ssl_session_load(ssl->session, 1, p, session_len); |
| 5321 | if (ret != 0) { |
| 5322 | mbedtls_ssl_session_free(ssl->session); |
| 5323 | return ret; |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5324 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5325 | |
| 5326 | p += session_len; |
| 5327 | |
| 5328 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5329 | * Transform |
| 5330 | */ |
| 5331 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 5332 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 5333 | * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */ |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 5334 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 5335 | ssl->transform = ssl->transform_negotiate; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5336 | ssl->transform_in = ssl->transform; |
| 5337 | ssl->transform_out = ssl->transform; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 5338 | ssl->transform_negotiate = NULL; |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 5339 | #endif |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5340 | |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 5341 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5342 | prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite); |
| 5343 | if (prf_func == NULL) { |
| 5344 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5345 | } |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 5346 | |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5347 | /* Read random bytes and populate structure */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5348 | if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) { |
| 5349 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5350 | } |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 5351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5352 | ret = ssl_tls12_populate_transform(ssl->transform, |
| 5353 | ssl->session->ciphersuite, |
| 5354 | ssl->session->master, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 5355 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5356 | ssl->session->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 5357 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5358 | prf_func, |
| 5359 | p, /* currently pointing to randbytes */ |
| 5360 | MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */ |
| 5361 | ssl->conf->endpoint, |
| 5362 | ssl); |
| 5363 | if (ret != 0) { |
| 5364 | return ret; |
| 5365 | } |
Jerry Yu | 840fbb2 | 2022-02-17 14:59:29 +0800 | [diff] [blame] | 5366 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5367 | p += sizeof(ssl->transform->randbytes); |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5368 | |
| 5369 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 5370 | /* Read connection IDs and store them */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5371 | if ((size_t) (end - p) < 1) { |
| 5372 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5373 | } |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5374 | |
| 5375 | ssl->transform->in_cid_len = *p++; |
| 5376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5377 | if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) { |
| 5378 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5379 | } |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5380 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5381 | 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] | 5382 | p += ssl->transform->in_cid_len; |
| 5383 | |
| 5384 | ssl->transform->out_cid_len = *p++; |
| 5385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5386 | if ((size_t) (end - p) < ssl->transform->out_cid_len) { |
| 5387 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5388 | } |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5389 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5390 | 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] | 5391 | p += ssl->transform->out_cid_len; |
| 5392 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 5393 | |
| 5394 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5395 | * Saved fields from top-level ssl_context structure |
| 5396 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5397 | if ((size_t) (end - p) < 4) { |
| 5398 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5399 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5400 | |
Thomas Daubney | f9f0ba8 | 2023-05-23 17:34:33 +0100 | [diff] [blame] | 5401 | ssl->badmac_seen = MBEDTLS_GET_UINT32_BE(p, 0); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5402 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5403 | |
| 5404 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5405 | if ((size_t) (end - p) < 16) { |
| 5406 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5407 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5408 | |
Thomas Daubney | f9f0ba8 | 2023-05-23 17:34:33 +0100 | [diff] [blame] | 5409 | ssl->in_window_top = MBEDTLS_GET_UINT64_BE(p, 0); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5410 | p += 8; |
| 5411 | |
Thomas Daubney | f9f0ba8 | 2023-05-23 17:34:33 +0100 | [diff] [blame] | 5412 | ssl->in_window = MBEDTLS_GET_UINT64_BE(p, 0); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5413 | p += 8; |
| 5414 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 5415 | |
| 5416 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5417 | if ((size_t) (end - p) < 1) { |
| 5418 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5419 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5420 | |
| 5421 | ssl->disable_datagram_packing = *p++; |
| 5422 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5423 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5424 | if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) { |
| 5425 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5426 | } |
| 5427 | memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr)); |
| 5428 | p += sizeof(ssl->cur_out_ctr); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5429 | |
| 5430 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5431 | if ((size_t) (end - p) < 2) { |
| 5432 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5433 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5434 | |
Dave Rodgman | a3d0f61 | 2023-11-03 23:34:02 +0000 | [diff] [blame] | 5435 | ssl->mtu = MBEDTLS_GET_UINT16_BE(p, 0); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5436 | p += 2; |
| 5437 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5438 | |
| 5439 | #if defined(MBEDTLS_SSL_ALPN) |
| 5440 | { |
| 5441 | uint8_t alpn_len; |
| 5442 | const char **cur; |
| 5443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5444 | if ((size_t) (end - p) < 1) { |
| 5445 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5446 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5447 | |
| 5448 | alpn_len = *p++; |
| 5449 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5450 | if (alpn_len != 0 && ssl->conf->alpn_list != NULL) { |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5451 | /* alpn_chosen should point to an item in the configured list */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5452 | for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) { |
| 5453 | if (strlen(*cur) == alpn_len && |
Waleed Elmelegy | 131b2ff | 2024-03-14 01:39:39 +0000 | [diff] [blame] | 5454 | memcmp(p, *cur, alpn_len) == 0) { |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5455 | ssl->alpn_chosen = *cur; |
| 5456 | break; |
| 5457 | } |
| 5458 | } |
| 5459 | } |
| 5460 | |
| 5461 | /* can only happen on conf mismatch */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5462 | if (alpn_len != 0 && ssl->alpn_chosen == NULL) { |
| 5463 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5464 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5465 | |
| 5466 | p += alpn_len; |
| 5467 | } |
| 5468 | #endif /* MBEDTLS_SSL_ALPN */ |
| 5469 | |
| 5470 | /* |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 5471 | * Forced fields from top-level ssl_context structure |
| 5472 | * |
| 5473 | * Most of them already set to the correct value by mbedtls_ssl_init() and |
| 5474 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. |
| 5475 | */ |
| 5476 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 5477 | ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 5478 | |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 5479 | /* Adjust pointers for header fields of outgoing records to |
| 5480 | * the given transform, accounting for explicit IV and CID. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5481 | mbedtls_ssl_update_out_pointers(ssl, ssl->transform); |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 5482 | |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 5483 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5484 | ssl->in_epoch = 1; |
| 5485 | #endif |
| 5486 | |
| 5487 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, |
| 5488 | * 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] | 5489 | * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform() |
| 5490 | * inappropriately. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5491 | if (ssl->handshake != NULL) { |
| 5492 | mbedtls_ssl_handshake_free(ssl); |
| 5493 | mbedtls_free(ssl->handshake); |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 5494 | ssl->handshake = NULL; |
| 5495 | } |
| 5496 | |
| 5497 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5498 | * Done - should have consumed entire buffer |
| 5499 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5500 | if (p != end) { |
| 5501 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5502 | } |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5503 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5504 | return 0; |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5505 | } |
| 5506 | |
| 5507 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 5508 | * Deserialize context: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5509 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5510 | int mbedtls_ssl_context_load(mbedtls_ssl_context *context, |
| 5511 | const unsigned char *buf, |
| 5512 | size_t len) |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5513 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5514 | int ret = ssl_context_load(context, buf, len); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5515 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5516 | if (ret != 0) { |
| 5517 | mbedtls_ssl_free(context); |
| 5518 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5519 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5520 | return ret; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5521 | } |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 5522 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5523 | |
| 5524 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5525 | * Free an SSL context |
| 5526 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5527 | void mbedtls_ssl_free(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5528 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5529 | if (ssl == NULL) { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 5530 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5531 | } |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 5532 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5533 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> free")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5534 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5535 | if (ssl->out_buf != NULL) { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 5536 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 5537 | size_t out_buf_len = ssl->out_buf_len; |
| 5538 | #else |
| 5539 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 5540 | #endif |
| 5541 | |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 5542 | mbedtls_zeroize_and_free(ssl->out_buf, out_buf_len); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5543 | ssl->out_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5544 | } |
| 5545 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5546 | if (ssl->in_buf != NULL) { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 5547 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 5548 | size_t in_buf_len = ssl->in_buf_len; |
| 5549 | #else |
| 5550 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 5551 | #endif |
| 5552 | |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 5553 | mbedtls_zeroize_and_free(ssl->in_buf, in_buf_len); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5554 | ssl->in_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5555 | } |
| 5556 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5557 | if (ssl->transform) { |
| 5558 | mbedtls_ssl_transform_free(ssl->transform); |
| 5559 | mbedtls_free(ssl->transform); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5560 | } |
| 5561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5562 | if (ssl->handshake) { |
| 5563 | mbedtls_ssl_handshake_free(ssl); |
| 5564 | mbedtls_free(ssl->handshake); |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 5565 | |
| 5566 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5567 | mbedtls_ssl_transform_free(ssl->transform_negotiate); |
| 5568 | mbedtls_free(ssl->transform_negotiate); |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 5569 | #endif |
| 5570 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5571 | mbedtls_ssl_session_free(ssl->session_negotiate); |
| 5572 | mbedtls_free(ssl->session_negotiate); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5573 | } |
| 5574 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5575 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5576 | mbedtls_ssl_transform_free(ssl->transform_application); |
| 5577 | mbedtls_free(ssl->transform_application); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5578 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 5579 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5580 | if (ssl->session) { |
| 5581 | mbedtls_ssl_session_free(ssl->session); |
| 5582 | mbedtls_free(ssl->session); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 5583 | } |
| 5584 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 5585 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5586 | if (ssl->hostname != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 5587 | mbedtls_zeroize_and_free(ssl->hostname, strlen(ssl->hostname)); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5588 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 5589 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5590 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 5591 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5592 | mbedtls_free(ssl->cli_id); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 5593 | #endif |
| 5594 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5595 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= free")); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 5596 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 5597 | /* Actually clear after last debug message */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5598 | mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context)); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5599 | } |
| 5600 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5601 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 5602 | * Initialize mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5603 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5604 | void mbedtls_ssl_config_init(mbedtls_ssl_config *conf) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5605 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5606 | memset(conf, 0, sizeof(mbedtls_ssl_config)); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5607 | } |
| 5608 | |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 5609 | /* The selection should be the same as mbedtls_x509_crt_profile_default in |
| 5610 | * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters: |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 5611 | * curves with a lower resource usage come first. |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 5612 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 5613 | * about this list. |
| 5614 | */ |
Chien Wong | 4e9683e | 2023-12-28 17:07:43 +0800 | [diff] [blame] | 5615 | static const uint16_t ssl_preset_default_groups[] = { |
Elena Uziunaite | 0b5d48e | 2024-07-05 11:48:23 +0100 | [diff] [blame] | 5616 | #if defined(PSA_WANT_ECC_MONTGOMERY_255) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5617 | MBEDTLS_SSL_IANA_TLS_GROUP_X25519, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 5618 | #endif |
Elena Uziunaite | 417d05f | 2024-07-04 17:46:30 +0100 | [diff] [blame] | 5619 | #if defined(PSA_WANT_ECC_SECP_R1_256) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5620 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 5621 | #endif |
Elena Uziunaite | 6b4cd48 | 2024-07-04 17:50:11 +0100 | [diff] [blame] | 5622 | #if defined(PSA_WANT_ECC_SECP_R1_384) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5623 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 5624 | #endif |
Elena Uziunaite | 0b5d48e | 2024-07-05 11:48:23 +0100 | [diff] [blame] | 5625 | #if defined(PSA_WANT_ECC_MONTGOMERY_448) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5626 | MBEDTLS_SSL_IANA_TLS_GROUP_X448, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 5627 | #endif |
Elena Uziunaite | 24e24f2 | 2024-07-04 17:53:40 +0100 | [diff] [blame] | 5628 | #if defined(PSA_WANT_ECC_SECP_R1_521) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5629 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 5630 | #endif |
Elena Uziunaite | b8d1087 | 2024-07-05 10:51:40 +0100 | [diff] [blame] | 5631 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5632 | MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 5633 | #endif |
Elena Uziunaite | b8d1087 | 2024-07-05 10:51:40 +0100 | [diff] [blame] | 5634 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5635 | MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 5636 | #endif |
Elena Uziunaite | b8d1087 | 2024-07-05 10:51:40 +0100 | [diff] [blame] | 5637 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5638 | MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 5639 | #endif |
Przemek Stekiel | 316c19e | 2023-05-31 15:25:11 +0200 | [diff] [blame] | 5640 | #if defined(PSA_WANT_ALG_FFDH) |
Przemek Stekiel | 383f471 | 2022-12-12 14:48:57 +0100 | [diff] [blame] | 5641 | MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048, |
| 5642 | MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072, |
| 5643 | MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096, |
| 5644 | MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144, |
| 5645 | MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192, |
| 5646 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5647 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 5648 | }; |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 5649 | |
Alexey Tsvetkov | 2ca3437 | 2022-06-07 10:21:05 +0300 | [diff] [blame] | 5650 | static const int ssl_preset_suiteb_ciphersuites[] = { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5651 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 5652 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 5653 | 0 |
| 5654 | }; |
| 5655 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5656 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 5657 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5658 | /* NOTICE: |
Jerry Yu | 0b994b8 | 2022-01-25 17:22:12 +0800 | [diff] [blame] | 5659 | * 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] | 5660 | * rules SHOULD be upheld. |
| 5661 | * - No duplicate entries. |
| 5662 | * - 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] | 5663 | * - ssl_tls12_preset* is for TLS 1.2 use only. |
Jerry Yu | 370e146 | 2022-01-25 10:36:53 +0800 | [diff] [blame] | 5664 | * - 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] | 5665 | */ |
Chien Wong | 4e9683e | 2023-12-28 17:07:43 +0800 | [diff] [blame] | 5666 | static const uint16_t ssl_preset_default_sig_algs[] = { |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5667 | |
Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5668 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \ |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 5669 | defined(PSA_WANT_ALG_SHA_256) && \ |
Valerio Setti | cf29c5d | 2023-09-01 09:03:41 +0200 | [diff] [blame] | 5670 | defined(PSA_WANT_ECC_SECP_R1_256) |
Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 5671 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5672 | // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256) |
| 5673 | #endif |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5674 | |
Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5675 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \ |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 5676 | defined(PSA_WANT_ALG_SHA_384) && \ |
Valerio Setti | cf29c5d | 2023-09-01 09:03:41 +0200 | [diff] [blame] | 5677 | defined(PSA_WANT_ECC_SECP_R1_384) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5678 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5679 | // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384) |
| 5680 | #endif |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5681 | |
Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5682 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \ |
Gabor Mezei | c15ef93 | 2024-06-13 12:53:54 +0200 | [diff] [blame] | 5683 | defined(PSA_WANT_ALG_SHA_512) && \ |
Valerio Setti | cf29c5d | 2023-09-01 09:03:41 +0200 | [diff] [blame] | 5684 | defined(PSA_WANT_ECC_SECP_R1_521) |
Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 5685 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512, |
Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5686 | // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512) |
| 5687 | #endif |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5688 | |
Gabor Mezei | c15ef93 | 2024-06-13 12:53:54 +0200 | [diff] [blame] | 5689 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(PSA_WANT_ALG_SHA_512) |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 5690 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5691 | #endif |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 5692 | |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 5693 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(PSA_WANT_ALG_SHA_384) |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 5694 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5695 | #endif |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 5696 | |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 5697 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(PSA_WANT_ALG_SHA_256) |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 5698 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5699 | #endif |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 5700 | |
Gabor Mezei | c15ef93 | 2024-06-13 12:53:54 +0200 | [diff] [blame] | 5701 | #if defined(MBEDTLS_RSA_C) && defined(PSA_WANT_ALG_SHA_512) |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 5702 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512, |
Gabor Mezei | c15ef93 | 2024-06-13 12:53:54 +0200 | [diff] [blame] | 5703 | #endif /* MBEDTLS_RSA_C && PSA_WANT_ALG_SHA_512 */ |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 5704 | |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 5705 | #if defined(MBEDTLS_RSA_C) && defined(PSA_WANT_ALG_SHA_384) |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 5706 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384, |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 5707 | #endif /* MBEDTLS_RSA_C && PSA_WANT_ALG_SHA_384 */ |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 5708 | |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 5709 | #if defined(MBEDTLS_RSA_C) && defined(PSA_WANT_ALG_SHA_256) |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 5710 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 5711 | #endif /* MBEDTLS_RSA_C && PSA_WANT_ALG_SHA_256 */ |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 5712 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5713 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5714 | }; |
| 5715 | |
| 5716 | /* NOTICE: see above */ |
| 5717 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5718 | static uint16_t ssl_tls12_preset_default_sig_algs[] = { |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5719 | |
Gabor Mezei | c15ef93 | 2024-06-13 12:53:54 +0200 | [diff] [blame] | 5720 | #if defined(PSA_WANT_ALG_SHA_512) |
Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 5721 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5722 | 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] | 5723 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 5724 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 5725 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5726 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5727 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5728 | 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] | 5729 | #endif |
Gabor Mezei | c15ef93 | 2024-06-13 12:53:54 +0200 | [diff] [blame] | 5730 | #endif /* PSA_WANT_ALG_SHA_512 */ |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5731 | |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 5732 | #if defined(PSA_WANT_ALG_SHA_384) |
Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 5733 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5734 | 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] | 5735 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 5736 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 5737 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5738 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5739 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5740 | 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] | 5741 | #endif |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 5742 | #endif /* PSA_WANT_ALG_SHA_384 */ |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5743 | |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 5744 | #if defined(PSA_WANT_ALG_SHA_256) |
Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 5745 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5746 | 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] | 5747 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 5748 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 5749 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5750 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5751 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5752 | 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] | 5753 | #endif |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 5754 | #endif /* PSA_WANT_ALG_SHA_256 */ |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5755 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5756 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5757 | }; |
| 5758 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5759 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5760 | /* NOTICE: see above */ |
Chien Wong | 4e9683e | 2023-12-28 17:07:43 +0800 | [diff] [blame] | 5761 | static const uint16_t ssl_preset_suiteb_sig_algs[] = { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5762 | |
Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5763 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \ |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 5764 | defined(PSA_WANT_ALG_SHA_256) && \ |
Elena Uziunaite | 417d05f | 2024-07-04 17:46:30 +0100 | [diff] [blame] | 5765 | defined(PSA_WANT_ECC_SECP_R1_256) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5766 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5767 | // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256) |
| 5768 | #endif |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5769 | |
Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5770 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \ |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 5771 | defined(PSA_WANT_ALG_SHA_384) && \ |
Elena Uziunaite | 6b4cd48 | 2024-07-04 17:50:11 +0100 | [diff] [blame] | 5772 | defined(PSA_WANT_ECC_SECP_R1_384) |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 5773 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5774 | // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384) |
| 5775 | #endif |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5776 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5777 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 5778 | }; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 5779 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5780 | /* NOTICE: see above */ |
| 5781 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5782 | static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = { |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5783 | |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 5784 | #if defined(PSA_WANT_ALG_SHA_256) |
Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 5785 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5786 | 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] | 5787 | #endif |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 5788 | #endif /* PSA_WANT_ALG_SHA_256 */ |
Yanray Wang | 69ceb39 | 2023-11-22 16:32:39 +0800 | [diff] [blame] | 5789 | |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 5790 | #if defined(PSA_WANT_ALG_SHA_384) |
Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 5791 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5792 | 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] | 5793 | #endif |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 5794 | #endif /* PSA_WANT_ALG_SHA_384 */ |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5795 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5796 | MBEDTLS_TLS_SIG_NONE |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 5797 | }; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5798 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5799 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5800 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5801 | |
Chien Wong | 4e9683e | 2023-12-28 17:07:43 +0800 | [diff] [blame] | 5802 | static const uint16_t ssl_preset_suiteb_groups[] = { |
Elena Uziunaite | 417d05f | 2024-07-04 17:46:30 +0100 | [diff] [blame] | 5803 | #if defined(PSA_WANT_ECC_SECP_R1_256) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5804 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 5805 | #endif |
Elena Uziunaite | 6b4cd48 | 2024-07-04 17:50:11 +0100 | [diff] [blame] | 5806 | #if defined(PSA_WANT_ECC_SECP_R1_384) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5807 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 5808 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5809 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5810 | }; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5811 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5812 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5813 | /* 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] | 5814 | * to make sure there are no duplicated signature algorithm entries. */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5815 | MBEDTLS_CHECK_RETURN_CRITICAL |
Chien Wong | 4e9683e | 2023-12-28 17:07:43 +0800 | [diff] [blame] | 5816 | 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] | 5817 | { |
| 5818 | size_t i, j; |
| 5819 | int ret = 0; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5820 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5821 | for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) { |
| 5822 | for (j = 0; j < i; j++) { |
| 5823 | if (sig_algs[i] != sig_algs[j]) { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 5824 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5825 | } |
| 5826 | mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET |
| 5827 | ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n", |
| 5828 | sig_algs[i], j, i); |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 5829 | ret = -1; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5830 | } |
| 5831 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5832 | return ret; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5833 | } |
| 5834 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5835 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5836 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5837 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 5838 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5839 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5840 | int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf, |
| 5841 | int endpoint, int transport, int preset) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5842 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 5843 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5844 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 5845 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5846 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5847 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5848 | if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) { |
| 5849 | mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n"); |
| 5850 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5851 | } |
| 5852 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5853 | if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) { |
| 5854 | mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n"); |
| 5855 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5856 | } |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5857 | |
| 5858 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5859 | if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) { |
| 5860 | mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n"); |
| 5861 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5862 | } |
| 5863 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5864 | if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) { |
| 5865 | mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n"); |
| 5866 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5867 | } |
| 5868 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5869 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5870 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 5871 | /* Use the functions here so that they are covered in tests, |
| 5872 | * but otherwise access member directly for efficiency */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5873 | mbedtls_ssl_conf_endpoint(conf, endpoint); |
| 5874 | mbedtls_ssl_conf_transport(conf, transport); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5875 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5876 | /* |
| 5877 | * Things that are common to all presets |
| 5878 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 5879 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5880 | if (endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 5881 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 5882 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 5883 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
| 5884 | #endif |
| 5885 | } |
| 5886 | #endif |
| 5887 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5888 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 5889 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 5890 | #endif |
| 5891 | |
| 5892 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 5893 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 5894 | #endif |
| 5895 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 5896 | #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] | 5897 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 5898 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 5899 | #endif |
| 5900 | |
| 5901 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 5902 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 5903 | #endif |
| 5904 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 5905 | #if defined(MBEDTLS_SSL_SRV_C) |
| 5906 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
TRodziewicz | 3946f79 | 2021-06-14 12:11:18 +0200 | [diff] [blame] | 5907 | conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER; |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 5908 | #endif |
| 5909 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5910 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5911 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
| 5912 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
| 5913 | #endif |
| 5914 | |
| 5915 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 5916 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5917 | memset(conf->renego_period, 0x00, 2); |
| 5918 | memset(conf->renego_period + 2, 0xFF, 6); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5919 | #endif |
| 5920 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5921 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5922 | if (endpoint == MBEDTLS_SSL_IS_SERVER) { |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 5923 | const unsigned char dhm_p[] = |
| 5924 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 5925 | const unsigned char dhm_g[] = |
| 5926 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 5927 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5928 | if ((ret = mbedtls_ssl_conf_dh_param_bin(conf, |
| 5929 | dhm_p, sizeof(dhm_p), |
| 5930 | dhm_g, sizeof(dhm_g))) != 0) { |
| 5931 | return ret; |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 5932 | } |
| 5933 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 5934 | #endif |
| 5935 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5936 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 6ee56aa | 2022-12-06 17:47:22 +0800 | [diff] [blame] | 5937 | |
| 5938 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
Yanray Wang | d5ed36f | 2023-11-07 11:40:43 +0800 | [diff] [blame] | 5939 | mbedtls_ssl_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED); |
Jerry Yu | 6ee56aa | 2022-12-06 17:47:22 +0800 | [diff] [blame] | 5940 | #if defined(MBEDTLS_SSL_SRV_C) |
Yanray Wang | 0751761 | 2023-11-07 11:47:36 +0800 | [diff] [blame] | 5941 | 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] | 5942 | #endif |
| 5943 | #endif /* MBEDTLS_SSL_EARLY_DATA */ |
| 5944 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 5945 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 5946 | mbedtls_ssl_conf_new_session_tickets( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5947 | conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS); |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 5948 | #endif |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 5949 | /* |
| 5950 | * Allow all TLS 1.3 key exchange modes by default. |
| 5951 | */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 5952 | conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL; |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5953 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 5954 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5955 | if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 5956 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5957 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 5958 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5959 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5960 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 5961 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5962 | } else { |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5963 | #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] | 5964 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5965 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5966 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 5967 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 5968 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 5969 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5970 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5971 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5972 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5973 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5974 | #endif |
| 5975 | } |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 5976 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5977 | /* |
| 5978 | * Preset-specific defaults |
| 5979 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5980 | switch (preset) { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5981 | /* |
| 5982 | * NSA Suite B |
| 5983 | */ |
| 5984 | case MBEDTLS_SSL_PRESET_SUITEB: |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5985 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 5986 | conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5987 | |
| 5988 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5989 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5990 | #endif |
| 5991 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5992 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5993 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5994 | if (mbedtls_ssl_conf_is_tls12_only(conf)) { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5995 | conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5996 | } else |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5997 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5998 | conf->sig_algs = ssl_preset_suiteb_sig_algs; |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5999 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6000 | |
Manuel Pégourié-Gonnard | f07ce3b | 2023-09-22 11:53:41 +0200 | [diff] [blame] | 6001 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6002 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6003 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6004 | conf->group_list = ssl_preset_suiteb_groups; |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 6005 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6006 | |
| 6007 | /* |
| 6008 | * Default |
| 6009 | */ |
| 6010 | default: |
Ronald Cron | f660655 | 2022-03-15 11:23:25 +0100 | [diff] [blame] | 6011 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 6012 | conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites(); |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6013 | |
| 6014 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6015 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 6016 | #endif |
| 6017 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 6018 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6019 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6020 | if (mbedtls_ssl_conf_is_tls12_only(conf)) { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6021 | conf->sig_algs = ssl_tls12_preset_default_sig_algs; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6022 | } else |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6023 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6024 | conf->sig_algs = ssl_preset_default_sig_algs; |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 6025 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6026 | |
Manuel Pégourié-Gonnard | f07ce3b | 2023-09-22 11:53:41 +0200 | [diff] [blame] | 6027 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6028 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6029 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6030 | conf->group_list = ssl_preset_default_groups; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6031 | |
| 6032 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 6033 | conf->dhm_min_bitlen = 1024; |
| 6034 | #endif |
| 6035 | } |
| 6036 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6037 | return 0; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6038 | } |
| 6039 | |
| 6040 | /* |
| 6041 | * Free mbedtls_ssl_config |
| 6042 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6043 | void mbedtls_ssl_config_free(mbedtls_ssl_config *conf) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6044 | { |
Troy-Butler | da73abc | 2024-04-02 13:37:31 -0400 | [diff] [blame] | 6045 | if (conf == NULL) { |
| 6046 | return; |
| 6047 | } |
| 6048 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6049 | #if defined(MBEDTLS_DHM_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6050 | mbedtls_mpi_free(&conf->dhm_P); |
| 6051 | mbedtls_mpi_free(&conf->dhm_G); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6052 | #endif |
| 6053 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 6054 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 6055 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6056 | if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 6057 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 6058 | } |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 6059 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6060 | if (conf->psk != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 6061 | mbedtls_zeroize_and_free(conf->psk, conf->psk_len); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 6062 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6063 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 6064 | } |
| 6065 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6066 | if (conf->psk_identity != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 6067 | mbedtls_zeroize_and_free(conf->psk_identity, conf->psk_identity_len); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 6068 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6069 | conf->psk_identity_len = 0; |
| 6070 | } |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 6071 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6072 | |
| 6073 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6074 | ssl_key_cert_free(conf->key_cert); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6075 | #endif |
| 6076 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6077 | mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config)); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6078 | } |
| 6079 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 6080 | #if defined(MBEDTLS_PK_C) && \ |
Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 6081 | (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] | 6082 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6083 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6084 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6085 | 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] | 6086 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6087 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6088 | if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) { |
| 6089 | return MBEDTLS_SSL_SIG_RSA; |
| 6090 | } |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6091 | #endif |
Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 6092 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6093 | if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) { |
| 6094 | return MBEDTLS_SSL_SIG_ECDSA; |
| 6095 | } |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6096 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6097 | return MBEDTLS_SSL_SIG_ANON; |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6098 | } |
| 6099 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6100 | 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] | 6101 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6102 | switch (type) { |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6103 | case MBEDTLS_PK_RSA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6104 | return MBEDTLS_SSL_SIG_RSA; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6105 | case MBEDTLS_PK_ECDSA: |
| 6106 | case MBEDTLS_PK_ECKEY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6107 | return MBEDTLS_SSL_SIG_ECDSA; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6108 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6109 | return MBEDTLS_SSL_SIG_ANON; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6110 | } |
| 6111 | } |
| 6112 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6113 | 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] | 6114 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6115 | switch (sig) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6116 | #if defined(MBEDTLS_RSA_C) |
| 6117 | case MBEDTLS_SSL_SIG_RSA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6118 | return MBEDTLS_PK_RSA; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6119 | #endif |
Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 6120 | #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] | 6121 | case MBEDTLS_SSL_SIG_ECDSA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6122 | return MBEDTLS_PK_ECDSA; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6123 | #endif |
| 6124 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6125 | return MBEDTLS_PK_NONE; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6126 | } |
| 6127 | } |
Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 6128 | #endif /* MBEDTLS_PK_C && |
| 6129 | ( 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] | 6130 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 6131 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6132 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 6133 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6134 | 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] | 6135 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6136 | switch (hash) { |
Elena Uziunaite | b66a991 | 2024-05-10 14:25:58 +0100 | [diff] [blame] | 6137 | #if defined(PSA_WANT_ALG_MD5) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6138 | case MBEDTLS_SSL_HASH_MD5: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6139 | return MBEDTLS_MD_MD5; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6140 | #endif |
Elena Uziunaite | 9fc5be0 | 2024-09-04 18:12:59 +0100 | [diff] [blame] | 6141 | #if defined(PSA_WANT_ALG_SHA_1) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6142 | case MBEDTLS_SSL_HASH_SHA1: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6143 | return MBEDTLS_MD_SHA1; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6144 | #endif |
Elena Uziunaite | fcc9afa | 2024-05-23 14:43:22 +0100 | [diff] [blame] | 6145 | #if defined(PSA_WANT_ALG_SHA_224) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6146 | case MBEDTLS_SSL_HASH_SHA224: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6147 | return MBEDTLS_MD_SHA224; |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 6148 | #endif |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 6149 | #if defined(PSA_WANT_ALG_SHA_256) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6150 | case MBEDTLS_SSL_HASH_SHA256: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6151 | return MBEDTLS_MD_SHA256; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6152 | #endif |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 6153 | #if defined(PSA_WANT_ALG_SHA_384) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6154 | case MBEDTLS_SSL_HASH_SHA384: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6155 | return MBEDTLS_MD_SHA384; |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 6156 | #endif |
Gabor Mezei | c15ef93 | 2024-06-13 12:53:54 +0200 | [diff] [blame] | 6157 | #if defined(PSA_WANT_ALG_SHA_512) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6158 | case MBEDTLS_SSL_HASH_SHA512: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6159 | return MBEDTLS_MD_SHA512; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6160 | #endif |
| 6161 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6162 | return MBEDTLS_MD_NONE; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6163 | } |
| 6164 | } |
| 6165 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6166 | /* |
| 6167 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 6168 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6169 | unsigned char mbedtls_ssl_hash_from_md_alg(int md) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6170 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6171 | switch (md) { |
Elena Uziunaite | b66a991 | 2024-05-10 14:25:58 +0100 | [diff] [blame] | 6172 | #if defined(PSA_WANT_ALG_MD5) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6173 | case MBEDTLS_MD_MD5: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6174 | return MBEDTLS_SSL_HASH_MD5; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6175 | #endif |
Elena Uziunaite | 9fc5be0 | 2024-09-04 18:12:59 +0100 | [diff] [blame] | 6176 | #if defined(PSA_WANT_ALG_SHA_1) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6177 | case MBEDTLS_MD_SHA1: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6178 | return MBEDTLS_SSL_HASH_SHA1; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6179 | #endif |
Elena Uziunaite | fcc9afa | 2024-05-23 14:43:22 +0100 | [diff] [blame] | 6180 | #if defined(PSA_WANT_ALG_SHA_224) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6181 | case MBEDTLS_MD_SHA224: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6182 | return MBEDTLS_SSL_HASH_SHA224; |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 6183 | #endif |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 6184 | #if defined(PSA_WANT_ALG_SHA_256) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6185 | case MBEDTLS_MD_SHA256: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6186 | return MBEDTLS_SSL_HASH_SHA256; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6187 | #endif |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 6188 | #if defined(PSA_WANT_ALG_SHA_384) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6189 | case MBEDTLS_MD_SHA384: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6190 | return MBEDTLS_SSL_HASH_SHA384; |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 6191 | #endif |
Gabor Mezei | c15ef93 | 2024-06-13 12:53:54 +0200 | [diff] [blame] | 6192 | #if defined(PSA_WANT_ALG_SHA_512) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6193 | case MBEDTLS_MD_SHA512: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6194 | return MBEDTLS_SSL_HASH_SHA512; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6195 | #endif |
| 6196 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6197 | return MBEDTLS_SSL_HASH_NONE; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6198 | } |
| 6199 | } |
| 6200 | |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 6201 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6202 | * 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] | 6203 | * 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] | 6204 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6205 | 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] | 6206 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6207 | const uint16_t *group_list = mbedtls_ssl_get_groups(ssl); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 6208 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6209 | if (group_list == NULL) { |
| 6210 | return -1; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6211 | } |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 6212 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6213 | for (; *group_list != 0; group_list++) { |
| 6214 | if (*group_list == tls_id) { |
| 6215 | return 0; |
| 6216 | } |
| 6217 | } |
| 6218 | |
| 6219 | return -1; |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 6220 | } |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 6221 | |
Elena Uziunaite | 8dde3b3 | 2024-07-05 12:10:21 +0100 | [diff] [blame] | 6222 | #if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 6223 | /* |
| 6224 | * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id. |
| 6225 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6226 | 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] | 6227 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6228 | 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] | 6229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6230 | if (tls_id == 0) { |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6231 | return -1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6232 | } |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6233 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6234 | return mbedtls_ssl_check_curve_tls_id(ssl, tls_id); |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 6235 | } |
Elena Uziunaite | 8dde3b3 | 2024-07-05 12:10:21 +0100 | [diff] [blame] | 6236 | #endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */ |
Valerio Setti | 67419f0 | 2023-01-04 16:12:42 +0100 | [diff] [blame] | 6237 | |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6238 | static const struct { |
| 6239 | uint16_t tls_id; |
| 6240 | mbedtls_ecp_group_id ecp_group_id; |
| 6241 | psa_ecc_family_t psa_family; |
| 6242 | uint16_t bits; |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6243 | } tls_id_match_table[] = |
| 6244 | { |
Elena Uziunaite | 24e24f2 | 2024-07-04 17:53:40 +0100 | [diff] [blame] | 6245 | #if defined(PSA_WANT_ECC_SECP_R1_521) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6246 | { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6247 | #endif |
Elena Uziunaite | b8d1087 | 2024-07-05 10:51:40 +0100 | [diff] [blame] | 6248 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6249 | { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6250 | #endif |
Elena Uziunaite | 6b4cd48 | 2024-07-04 17:50:11 +0100 | [diff] [blame] | 6251 | #if defined(PSA_WANT_ECC_SECP_R1_384) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6252 | { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6253 | #endif |
Elena Uziunaite | b8d1087 | 2024-07-05 10:51:40 +0100 | [diff] [blame] | 6254 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6255 | { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6256 | #endif |
Elena Uziunaite | 417d05f | 2024-07-04 17:46:30 +0100 | [diff] [blame] | 6257 | #if defined(PSA_WANT_ECC_SECP_R1_256) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6258 | { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6259 | #endif |
Elena Uziunaite | 9e85c9f | 2024-07-04 18:37:34 +0100 | [diff] [blame] | 6260 | #if defined(PSA_WANT_ECC_SECP_K1_256) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6261 | { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6262 | #endif |
Elena Uziunaite | b8d1087 | 2024-07-05 10:51:40 +0100 | [diff] [blame] | 6263 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6264 | { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6265 | #endif |
Elena Uziunaite | eaa0cf0 | 2024-07-04 17:41:25 +0100 | [diff] [blame] | 6266 | #if defined(PSA_WANT_ECC_SECP_R1_224) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6267 | { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6268 | #endif |
Elena Uziunaite | 63cb13e | 2024-09-05 12:43:14 +0100 | [diff] [blame] | 6269 | #if defined(PSA_WANT_ECC_SECP_K1_224) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6270 | { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6271 | #endif |
Elena Uziunaite | a363286 | 2024-07-04 13:52:43 +0100 | [diff] [blame] | 6272 | #if defined(PSA_WANT_ECC_SECP_R1_192) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6273 | { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6274 | #endif |
Elena Uziunaite | 9e85c9f | 2024-07-04 18:37:34 +0100 | [diff] [blame] | 6275 | #if defined(PSA_WANT_ECC_SECP_K1_192) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6276 | { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6277 | #endif |
Elena Uziunaite | 0b5d48e | 2024-07-05 11:48:23 +0100 | [diff] [blame] | 6278 | #if defined(PSA_WANT_ECC_MONTGOMERY_255) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6279 | { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6280 | #endif |
Elena Uziunaite | 0b5d48e | 2024-07-05 11:48:23 +0100 | [diff] [blame] | 6281 | #if defined(PSA_WANT_ECC_MONTGOMERY_448) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6282 | { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6283 | #endif |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6284 | { 0, MBEDTLS_ECP_DP_NONE, 0, 0 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6285 | }; |
| 6286 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6287 | 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] | 6288 | psa_key_type_t *type, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6289 | size_t *bits) |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6290 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6291 | for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) { |
| 6292 | if (tls_id_match_table[i].tls_id == tls_id) { |
Przemek Stekiel | da4fba6 | 2023-06-02 14:52:28 +0200 | [diff] [blame] | 6293 | if (type != NULL) { |
| 6294 | *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] | 6295 | } |
| 6296 | if (bits != NULL) { |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6297 | *bits = tls_id_match_table[i].bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6298 | } |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6299 | return PSA_SUCCESS; |
| 6300 | } |
| 6301 | } |
| 6302 | |
| 6303 | return PSA_ERROR_NOT_SUPPORTED; |
| 6304 | } |
| 6305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6306 | 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] | 6307 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6308 | for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) { |
| 6309 | if (tls_id_match_table[i].tls_id == tls_id) { |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6310 | return tls_id_match_table[i].ecp_group_id; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6311 | } |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6312 | } |
| 6313 | |
| 6314 | return MBEDTLS_ECP_DP_NONE; |
| 6315 | } |
| 6316 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6317 | 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] | 6318 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6319 | for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE; |
| 6320 | i++) { |
| 6321 | if (tls_id_match_table[i].ecp_group_id == grp_id) { |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6322 | return tls_id_match_table[i].tls_id; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6323 | } |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6324 | } |
| 6325 | |
| 6326 | return 0; |
| 6327 | } |
| 6328 | |
Valerio Setti | 67419f0 | 2023-01-04 16:12:42 +0100 | [diff] [blame] | 6329 | #if defined(MBEDTLS_DEBUG_C) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6330 | static const struct { |
| 6331 | uint16_t tls_id; |
| 6332 | const char *name; |
| 6333 | } tls_id_curve_name_table[] = |
| 6334 | { |
Valerio Setti | 54e2379 | 2023-07-07 10:49:27 +0200 | [diff] [blame] | 6335 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, "secp521r1" }, |
| 6336 | { MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, "brainpoolP512r1" }, |
| 6337 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, "secp384r1" }, |
| 6338 | { MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, "brainpoolP384r1" }, |
| 6339 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, "secp256r1" }, |
| 6340 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256K1, "secp256k1" }, |
| 6341 | { MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, "brainpoolP256r1" }, |
| 6342 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, "secp224r1" }, |
| 6343 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224K1, "secp224k1" }, |
| 6344 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, "secp192r1" }, |
| 6345 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192K1, "secp192k1" }, |
| 6346 | { MBEDTLS_SSL_IANA_TLS_GROUP_X25519, "x25519" }, |
| 6347 | { MBEDTLS_SSL_IANA_TLS_GROUP_X448, "x448" }, |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6348 | { 0, NULL }, |
| 6349 | }; |
| 6350 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6351 | 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] | 6352 | { |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6353 | for (int i = 0; tls_id_curve_name_table[i].tls_id != 0; i++) { |
| 6354 | if (tls_id_curve_name_table[i].tls_id == tls_id) { |
| 6355 | return tls_id_curve_name_table[i].name; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6356 | } |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6357 | } |
| 6358 | |
| 6359 | return NULL; |
| 6360 | } |
Valerio Setti | 67419f0 | 2023-01-04 16:12:42 +0100 | [diff] [blame] | 6361 | #endif |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6362 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 6363 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6364 | int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl, |
| 6365 | const mbedtls_md_type_t md, |
| 6366 | unsigned char *dst, |
| 6367 | size_t dst_len, |
| 6368 | size_t *olen) |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 6369 | { |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 6370 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 6371 | psa_hash_operation_t *hash_operation_to_clone; |
| 6372 | psa_hash_operation_t hash_operation = psa_hash_operation_init(); |
| 6373 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 6374 | *olen = 0; |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 6375 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6376 | switch (md) { |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 6377 | #if defined(PSA_WANT_ALG_SHA_384) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6378 | case MBEDTLS_MD_SHA384: |
| 6379 | hash_operation_to_clone = &ssl->handshake->fin_sha384_psa; |
| 6380 | break; |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 6381 | #endif |
| 6382 | |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 6383 | #if defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6384 | case MBEDTLS_MD_SHA256: |
| 6385 | hash_operation_to_clone = &ssl->handshake->fin_sha256_psa; |
| 6386 | break; |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 6387 | #endif |
| 6388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6389 | default: |
| 6390 | goto exit; |
| 6391 | } |
| 6392 | |
| 6393 | status = psa_hash_clone(hash_operation_to_clone, &hash_operation); |
| 6394 | if (status != PSA_SUCCESS) { |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 6395 | goto exit; |
| 6396 | } |
| 6397 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6398 | status = psa_hash_finish(&hash_operation, dst, dst_len, olen); |
| 6399 | if (status != PSA_SUCCESS) { |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 6400 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6401 | } |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 6402 | |
| 6403 | exit: |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 6404 | #if !defined(PSA_WANT_ALG_SHA_384) && \ |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 6405 | !defined(PSA_WANT_ALG_SHA_256) |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 6406 | (void) ssl; |
| 6407 | #endif |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 6408 | return PSA_TO_MBEDTLS_ERR(status); |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 6409 | } |
| 6410 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6411 | |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 6412 | #if defined(PSA_WANT_ALG_SHA_384) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6413 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6414 | static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl, |
| 6415 | unsigned char *dst, |
| 6416 | size_t dst_len, |
| 6417 | size_t *olen) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6418 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6419 | int ret; |
Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame] | 6420 | mbedtls_md_context_t sha384; |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6421 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6422 | if (dst_len < 48) { |
| 6423 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 6424 | } |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6425 | |
Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame] | 6426 | mbedtls_md_init(&sha384); |
| 6427 | 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] | 6428 | if (ret != 0) { |
| 6429 | goto exit; |
| 6430 | } |
Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame] | 6431 | ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384); |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 6432 | if (ret != 0) { |
| 6433 | goto exit; |
| 6434 | } |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6435 | |
Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame] | 6436 | if ((ret = mbedtls_md_finish(&sha384, dst)) != 0) { |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 6437 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6438 | goto exit; |
| 6439 | } |
| 6440 | |
| 6441 | *olen = 48; |
| 6442 | |
| 6443 | exit: |
| 6444 | |
Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame] | 6445 | mbedtls_md_free(&sha384); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6446 | return ret; |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6447 | } |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 6448 | #endif /* PSA_WANT_ALG_SHA_384 */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6449 | |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 6450 | #if defined(PSA_WANT_ALG_SHA_256) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6451 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6452 | static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl, |
| 6453 | unsigned char *dst, |
| 6454 | size_t dst_len, |
| 6455 | size_t *olen) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6456 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6457 | int ret; |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 6458 | mbedtls_md_context_t sha256; |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6459 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6460 | if (dst_len < 32) { |
| 6461 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 6462 | } |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6463 | |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 6464 | mbedtls_md_init(&sha256); |
| 6465 | ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0); |
| 6466 | if (ret != 0) { |
| 6467 | goto exit; |
| 6468 | } |
| 6469 | ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256); |
| 6470 | if (ret != 0) { |
| 6471 | goto exit; |
| 6472 | } |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 6473 | |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 6474 | if ((ret = mbedtls_md_finish(&sha256, dst)) != 0) { |
| 6475 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6476 | goto exit; |
| 6477 | } |
| 6478 | |
| 6479 | *olen = 32; |
| 6480 | |
| 6481 | exit: |
| 6482 | |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 6483 | mbedtls_md_free(&sha256); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6484 | return ret; |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6485 | } |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 6486 | #endif /* PSA_WANT_ALG_SHA_256 */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6487 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6488 | int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl, |
| 6489 | const mbedtls_md_type_t md, |
| 6490 | unsigned char *dst, |
| 6491 | size_t dst_len, |
| 6492 | size_t *olen) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6493 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6494 | switch (md) { |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 6495 | |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 6496 | #if defined(PSA_WANT_ALG_SHA_384) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6497 | case MBEDTLS_MD_SHA384: |
| 6498 | return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen); |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 6499 | #endif /* PSA_WANT_ALG_SHA_384*/ |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 6500 | |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 6501 | #if defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6502 | case MBEDTLS_MD_SHA256: |
| 6503 | return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen); |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 6504 | #endif /* PSA_WANT_ALG_SHA_256*/ |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 6505 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6506 | default: |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 6507 | #if !defined(PSA_WANT_ALG_SHA_384) && \ |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 6508 | !defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6509 | (void) ssl; |
| 6510 | (void) dst; |
| 6511 | (void) dst_len; |
| 6512 | (void) olen; |
Andrzej Kurek | 409248a | 2022-10-24 10:33:21 -0400 | [diff] [blame] | 6513 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6514 | break; |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 6515 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6516 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6517 | } |
XiaokangQian | 647719a | 2021-12-07 09:16:29 +0000 | [diff] [blame] | 6518 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 6519 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6520 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 6521 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6522 | /* mbedtls_ssl_parse_sig_alg_ext() |
| 6523 | * |
| 6524 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 6525 | * value (TLS 1.3 RFC8446): |
| 6526 | * enum { |
| 6527 | * .... |
| 6528 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 6529 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 6530 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 6531 | * .... |
| 6532 | * } SignatureScheme; |
| 6533 | * |
| 6534 | * struct { |
| 6535 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 6536 | * } SignatureSchemeList; |
| 6537 | * |
| 6538 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 6539 | * value (TLS 1.2 RFC5246): |
| 6540 | * enum { |
| 6541 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 6542 | * sha512(6), (255) |
| 6543 | * } HashAlgorithm; |
| 6544 | * |
| 6545 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 6546 | * SignatureAlgorithm; |
| 6547 | * |
| 6548 | * struct { |
| 6549 | * HashAlgorithm hash; |
| 6550 | * SignatureAlgorithm signature; |
| 6551 | * } SignatureAndHashAlgorithm; |
| 6552 | * |
| 6553 | * SignatureAndHashAlgorithm |
| 6554 | * supported_signature_algorithms<2..2^16-2>; |
| 6555 | * |
| 6556 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 6557 | * generalization of the TLS 1.2 signature algorithm extension. |
| 6558 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 6559 | * `SignatureScheme` field of TLS 1.3 |
| 6560 | * |
| 6561 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6562 | int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl, |
| 6563 | const unsigned char *buf, |
| 6564 | const unsigned char *end) |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6565 | { |
| 6566 | const unsigned char *p = buf; |
| 6567 | size_t supported_sig_algs_len = 0; |
| 6568 | const unsigned char *supported_sig_algs_end; |
| 6569 | uint16_t sig_alg; |
| 6570 | uint32_t common_idx = 0; |
| 6571 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6572 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2); |
| 6573 | supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6574 | p += 2; |
| 6575 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6576 | memset(ssl->handshake->received_sig_algs, 0, |
| 6577 | sizeof(ssl->handshake->received_sig_algs)); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6578 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6579 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6580 | supported_sig_algs_end = p + supported_sig_algs_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6581 | while (p < supported_sig_algs_end) { |
| 6582 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2); |
| 6583 | sig_alg = MBEDTLS_GET_UINT16_BE(p, 0); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6584 | p += 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6585 | MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s", |
| 6586 | sig_alg, |
| 6587 | mbedtls_ssl_sig_alg_to_str(sig_alg))); |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 6588 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6589 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 && |
| 6590 | (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) && |
| 6591 | mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6592 | continue; |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 6593 | } |
| 6594 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6595 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6596 | MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s", |
| 6597 | mbedtls_ssl_sig_alg_to_str(sig_alg))); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6598 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6599 | if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6600 | ssl->handshake->received_sig_algs[common_idx] = sig_alg; |
| 6601 | common_idx += 1; |
| 6602 | } |
| 6603 | } |
| 6604 | /* Check that we consumed all the message. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6605 | if (p != end) { |
| 6606 | MBEDTLS_SSL_DEBUG_MSG(1, |
| 6607 | ("Signature algorithms extension length misaligned")); |
| 6608 | MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, |
| 6609 | MBEDTLS_ERR_SSL_DECODE_ERROR); |
| 6610 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6611 | } |
| 6612 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6613 | if (common_idx == 0) { |
| 6614 | MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common")); |
| 6615 | MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE, |
| 6616 | MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE); |
| 6617 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6618 | } |
| 6619 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 6620 | ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6621 | return 0; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6622 | } |
| 6623 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 6624 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6625 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6626 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6627 | |
| 6628 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6629 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6630 | static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation, |
| 6631 | mbedtls_svc_key_id_t key, |
| 6632 | psa_algorithm_t alg, |
| 6633 | const unsigned char *raw_psk, size_t raw_psk_length, |
| 6634 | const unsigned char *seed, size_t seed_length, |
| 6635 | const unsigned char *label, size_t label_length, |
| 6636 | const unsigned char *other_secret, |
| 6637 | size_t other_secret_length, |
| 6638 | size_t capacity) |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6639 | { |
| 6640 | psa_status_t status; |
| 6641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6642 | status = psa_key_derivation_setup(derivation, alg); |
| 6643 | if (status != PSA_SUCCESS) { |
| 6644 | return status; |
| 6645 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6646 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6647 | if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 6648 | status = psa_key_derivation_input_bytes(derivation, |
| 6649 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 6650 | seed, seed_length); |
| 6651 | if (status != PSA_SUCCESS) { |
| 6652 | return status; |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 6653 | } |
| 6654 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6655 | if (other_secret != NULL) { |
| 6656 | status = psa_key_derivation_input_bytes(derivation, |
| 6657 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 6658 | other_secret, other_secret_length); |
| 6659 | if (status != PSA_SUCCESS) { |
| 6660 | return status; |
| 6661 | } |
| 6662 | } |
| 6663 | |
| 6664 | if (mbedtls_svc_key_id_is_null(key)) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6665 | status = psa_key_derivation_input_bytes( |
| 6666 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6667 | raw_psk, raw_psk_length); |
| 6668 | } else { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6669 | status = psa_key_derivation_input_key( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6670 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6671 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6672 | if (status != PSA_SUCCESS) { |
| 6673 | return status; |
| 6674 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6676 | status = psa_key_derivation_input_bytes(derivation, |
| 6677 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 6678 | label, label_length); |
| 6679 | if (status != PSA_SUCCESS) { |
| 6680 | return status; |
| 6681 | } |
| 6682 | } else { |
| 6683 | return PSA_ERROR_NOT_SUPPORTED; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6684 | } |
| 6685 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6686 | status = psa_key_derivation_set_capacity(derivation, capacity); |
| 6687 | if (status != PSA_SUCCESS) { |
| 6688 | return status; |
| 6689 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6690 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6691 | return PSA_SUCCESS; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6692 | } |
| 6693 | |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 6694 | #if defined(PSA_WANT_ALG_SHA_384) || \ |
| 6695 | defined(PSA_WANT_ALG_SHA_256) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6696 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6697 | static int tls_prf_generic(mbedtls_md_type_t md_type, |
| 6698 | const unsigned char *secret, size_t slen, |
| 6699 | const char *label, |
| 6700 | const unsigned char *random, size_t rlen, |
| 6701 | unsigned char *dstbuf, size_t dlen) |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6702 | { |
| 6703 | psa_status_t status; |
| 6704 | psa_algorithm_t alg; |
| 6705 | mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6706 | psa_key_derivation_operation_t derivation = |
| 6707 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 6708 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6709 | if (md_type == MBEDTLS_MD_SHA384) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6710 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6711 | } else { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6712 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6713 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6714 | |
| 6715 | /* Normally a "secret" should be long enough to be impossible to |
| 6716 | * find by brute force, and in particular should not be empty. But |
| 6717 | * this PRF is also used to derive an IV, in particular in EAP-TLS, |
| 6718 | * and for this use case it makes sense to have a 0-length "secret". |
| 6719 | * Since the key API doesn't allow importing a key of length 0, |
| 6720 | * keep master_key=0, which setup_psa_key_derivation() understands |
| 6721 | * to mean a 0-length "secret" input. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6722 | if (slen != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6723 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6724 | psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE); |
| 6725 | psa_set_key_algorithm(&key_attributes, alg); |
| 6726 | psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6727 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6728 | status = psa_import_key(&key_attributes, secret, slen, &master_key); |
| 6729 | if (status != PSA_SUCCESS) { |
| 6730 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 6731 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6732 | } |
| 6733 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6734 | status = setup_psa_key_derivation(&derivation, |
| 6735 | master_key, alg, |
| 6736 | NULL, 0, |
| 6737 | random, rlen, |
| 6738 | (unsigned char const *) label, |
| 6739 | (size_t) strlen(label), |
| 6740 | NULL, 0, |
| 6741 | dlen); |
| 6742 | if (status != PSA_SUCCESS) { |
| 6743 | psa_key_derivation_abort(&derivation); |
| 6744 | psa_destroy_key(master_key); |
| 6745 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6746 | } |
| 6747 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6748 | status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen); |
| 6749 | if (status != PSA_SUCCESS) { |
| 6750 | psa_key_derivation_abort(&derivation); |
| 6751 | psa_destroy_key(master_key); |
| 6752 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6753 | } |
| 6754 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6755 | status = psa_key_derivation_abort(&derivation); |
| 6756 | if (status != PSA_SUCCESS) { |
| 6757 | psa_destroy_key(master_key); |
| 6758 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6759 | } |
| 6760 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6761 | if (!mbedtls_svc_key_id_is_null(master_key)) { |
| 6762 | status = psa_destroy_key(master_key); |
| 6763 | } |
| 6764 | if (status != PSA_SUCCESS) { |
| 6765 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 6766 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6767 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6768 | return 0; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6769 | } |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 6770 | #endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6771 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6772 | |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 6773 | #if defined(MBEDTLS_MD_C) && \ |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 6774 | (defined(PSA_WANT_ALG_SHA_256) || \ |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 6775 | defined(PSA_WANT_ALG_SHA_384)) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6776 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6777 | static int tls_prf_generic(mbedtls_md_type_t md_type, |
| 6778 | const unsigned char *secret, size_t slen, |
| 6779 | const char *label, |
| 6780 | const unsigned char *random, size_t rlen, |
| 6781 | unsigned char *dstbuf, size_t dlen) |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6782 | { |
| 6783 | size_t nb; |
| 6784 | size_t i, j, k, md_len; |
| 6785 | unsigned char *tmp; |
| 6786 | size_t tmp_len = 0; |
| 6787 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 6788 | const mbedtls_md_info_t *md_info; |
| 6789 | mbedtls_md_context_t md_ctx; |
| 6790 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6791 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6792 | mbedtls_md_init(&md_ctx); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6793 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6794 | if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) { |
| 6795 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 6796 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6797 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6798 | md_len = mbedtls_md_get_size(md_info); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6799 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6800 | tmp_len = md_len + strlen(label) + rlen; |
| 6801 | tmp = mbedtls_calloc(1, tmp_len); |
| 6802 | if (tmp == NULL) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6803 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 6804 | goto exit; |
| 6805 | } |
| 6806 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6807 | nb = strlen(label); |
| 6808 | memcpy(tmp + md_len, label, nb); |
| 6809 | memcpy(tmp + md_len + nb, random, rlen); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6810 | nb += rlen; |
| 6811 | |
| 6812 | /* |
| 6813 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 6814 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6815 | if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6816 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6817 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6818 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6819 | ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen); |
| 6820 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6821 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6822 | } |
| 6823 | ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb); |
| 6824 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6825 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6826 | } |
| 6827 | ret = mbedtls_md_hmac_finish(&md_ctx, tmp); |
| 6828 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6829 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6830 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6831 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6832 | for (i = 0; i < dlen; i += md_len) { |
| 6833 | ret = mbedtls_md_hmac_reset(&md_ctx); |
| 6834 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6835 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6836 | } |
| 6837 | ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb); |
| 6838 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6839 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6840 | } |
| 6841 | ret = mbedtls_md_hmac_finish(&md_ctx, h_i); |
| 6842 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6843 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6844 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6845 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6846 | ret = mbedtls_md_hmac_reset(&md_ctx); |
| 6847 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6848 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6849 | } |
| 6850 | ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len); |
| 6851 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6852 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6853 | } |
| 6854 | ret = mbedtls_md_hmac_finish(&md_ctx, tmp); |
| 6855 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6856 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6857 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6858 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6859 | k = (i + md_len > dlen) ? dlen % md_len : md_len; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6860 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6861 | for (j = 0; j < k; j++) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6862 | dstbuf[i + j] = h_i[j]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6863 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6864 | } |
| 6865 | |
| 6866 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6867 | mbedtls_md_free(&md_ctx); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6868 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6869 | if (tmp != NULL) { |
| 6870 | mbedtls_platform_zeroize(tmp, tmp_len); |
| 6871 | } |
Dave Rodgman | 29b9b2b | 2022-11-01 16:08:14 +0000 | [diff] [blame] | 6872 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6873 | mbedtls_platform_zeroize(h_i, sizeof(h_i)); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6874 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6875 | mbedtls_free(tmp); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6876 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6877 | return ret; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6878 | } |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 6879 | #endif /* MBEDTLS_MD_C && ( PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 ) */ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6880 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6881 | |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 6882 | #if defined(PSA_WANT_ALG_SHA_256) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6883 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6884 | static int tls_prf_sha256(const unsigned char *secret, size_t slen, |
| 6885 | const char *label, |
| 6886 | const unsigned char *random, size_t rlen, |
| 6887 | unsigned char *dstbuf, size_t dlen) |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6888 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6889 | return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen, |
| 6890 | label, random, rlen, dstbuf, dlen); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6891 | } |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 6892 | #endif /* PSA_WANT_ALG_SHA_256*/ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6893 | |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 6894 | #if defined(PSA_WANT_ALG_SHA_384) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6895 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6896 | static int tls_prf_sha384(const unsigned char *secret, size_t slen, |
| 6897 | const char *label, |
| 6898 | const unsigned char *random, size_t rlen, |
| 6899 | unsigned char *dstbuf, size_t dlen) |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6900 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6901 | return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen, |
| 6902 | label, random, rlen, dstbuf, dlen); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6903 | } |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 6904 | #endif /* PSA_WANT_ALG_SHA_384*/ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6905 | |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6906 | /* |
| 6907 | * Set appropriate PRF function and other SSL / TLS1.2 functions |
| 6908 | * |
| 6909 | * Inputs: |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6910 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
| 6911 | * |
| 6912 | * Outputs: |
| 6913 | * - the tls_prf, calc_verify and calc_finished members of handshake structure |
| 6914 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6915 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6916 | static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake, |
| 6917 | mbedtls_md_type_t hash) |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6918 | { |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 6919 | #if defined(PSA_WANT_ALG_SHA_384) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6920 | if (hash == MBEDTLS_MD_SHA384) { |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6921 | handshake->tls_prf = tls_prf_sha384; |
| 6922 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 6923 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6924 | } else |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6925 | #endif |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 6926 | #if defined(PSA_WANT_ALG_SHA_256) |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6927 | { |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 6928 | (void) hash; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6929 | handshake->tls_prf = tls_prf_sha256; |
| 6930 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 6931 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
| 6932 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 6933 | #else |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6934 | { |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6935 | (void) handshake; |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 6936 | (void) hash; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6937 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6938 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 6939 | #endif |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6940 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6941 | return 0; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6942 | } |
Jerry Yu | d6ab235 | 2022-02-17 14:03:43 +0800 | [diff] [blame] | 6943 | |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6944 | /* |
| 6945 | * Compute master secret if needed |
| 6946 | * |
| 6947 | * Parameters: |
| 6948 | * [in/out] handshake |
| 6949 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf |
| 6950 | * (PSA-PSK) ciphersuite_info, psk_opaque |
| 6951 | * [out] premaster (cleared) |
| 6952 | * [out] master |
| 6953 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK |
| 6954 | * debug: conf->f_dbg, conf->p_dbg |
| 6955 | * EMS: passed to calc_verify (debug + session_negotiate) |
Ronald Cron | a25cf58 | 2022-03-07 11:10:36 +0100 | [diff] [blame] | 6956 | * PSA-PSA: conf |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6957 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6958 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6959 | static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake, |
| 6960 | unsigned char *master, |
| 6961 | const mbedtls_ssl_context *ssl) |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6962 | { |
| 6963 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6964 | |
| 6965 | /* cf. RFC 5246, Section 8.1: |
| 6966 | * "The master secret is always exactly 48 bytes in length." */ |
| 6967 | size_t const master_secret_len = 48; |
| 6968 | |
| 6969 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 6970 | unsigned char session_hash[48]; |
| 6971 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 6972 | |
| 6973 | /* The label for the KDF used for key expansion. |
| 6974 | * This is either "master secret" or "extended master secret" |
| 6975 | * depending on whether the Extended Master Secret extension |
| 6976 | * is used. */ |
| 6977 | char const *lbl = "master secret"; |
| 6978 | |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 6979 | /* The seed for the KDF used for key expansion. |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6980 | * - If the Extended Master Secret extension is not used, |
| 6981 | * this is ClientHello.Random + ServerHello.Random |
| 6982 | * (see Sect. 8.1 in RFC 5246). |
| 6983 | * - If the Extended Master Secret extension is used, |
| 6984 | * this is the transcript of the handshake so far. |
| 6985 | * (see Sect. 4 in RFC 7627). */ |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 6986 | unsigned char const *seed = handshake->randbytes; |
| 6987 | size_t seed_len = 64; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6988 | |
| 6989 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 6990 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ |
| 6991 | !(defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6992 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6993 | ssl = NULL; /* make sure we don't use it except for those cases */ |
| 6994 | (void) ssl; |
| 6995 | #endif |
| 6996 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6997 | if (handshake->resume != 0) { |
| 6998 | MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)")); |
| 6999 | return 0; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7000 | } |
| 7001 | |
| 7002 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7003 | if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) { |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7004 | lbl = "extended master secret"; |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 7005 | seed = session_hash; |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 7006 | ret = handshake->calc_verify(ssl, session_hash, &seed_len); |
| 7007 | if (ret != 0) { |
| 7008 | MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret); |
| 7009 | } |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7010 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7011 | MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret", |
| 7012 | session_hash, seed_len); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7013 | } |
Przemek Stekiel | 169bf0b | 2022-04-29 07:53:29 +0200 | [diff] [blame] | 7014 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7015 | |
Przemek Stekiel | 99114f3 | 2022-04-22 11:20:09 +0200 | [diff] [blame] | 7016 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Przemek Stekiel | 8a4b7fd | 2022-04-28 09:22:22 +0200 | [diff] [blame] | 7017 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7018 | if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) { |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7019 | /* Perform PSK-to-MS expansion in a single step. */ |
| 7020 | psa_status_t status; |
| 7021 | psa_algorithm_t alg; |
| 7022 | mbedtls_svc_key_id_t psk; |
| 7023 | psa_key_derivation_operation_t derivation = |
| 7024 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Dave Rodgman | 2eab462 | 2023-10-05 13:30:37 +0100 | [diff] [blame] | 7025 | 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] | 7026 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7027 | MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion")); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7028 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7029 | psk = mbedtls_ssl_get_opaque_psk(ssl); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7030 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7031 | if (hash_alg == MBEDTLS_MD_SHA384) { |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7032 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7033 | } else { |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7034 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7035 | } |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7036 | |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 7037 | size_t other_secret_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7038 | unsigned char *other_secret = NULL; |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 7039 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7040 | switch (handshake->ciphersuite_info->key_exchange) { |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 7041 | /* Provide other secret. |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 7042 | * Other secret is stored in premaster, where first 2 bytes hold the |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 7043 | * length of the other key. |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 7044 | */ |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 7045 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 7046 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 7047 | other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0); |
| 7048 | other_secret = handshake->premaster + 2; |
| 7049 | break; |
| 7050 | default: |
| 7051 | break; |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 7052 | } |
| 7053 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7054 | status = setup_psa_key_derivation(&derivation, psk, alg, |
| 7055 | ssl->conf->psk, ssl->conf->psk_len, |
| 7056 | seed, seed_len, |
| 7057 | (unsigned char const *) lbl, |
| 7058 | (size_t) strlen(lbl), |
| 7059 | other_secret, other_secret_len, |
| 7060 | master_secret_len); |
| 7061 | if (status != PSA_SUCCESS) { |
| 7062 | psa_key_derivation_abort(&derivation); |
| 7063 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7064 | } |
| 7065 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7066 | status = psa_key_derivation_output_bytes(&derivation, |
| 7067 | master, |
| 7068 | master_secret_len); |
| 7069 | if (status != PSA_SUCCESS) { |
| 7070 | psa_key_derivation_abort(&derivation); |
| 7071 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7072 | } |
| 7073 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7074 | status = psa_key_derivation_abort(&derivation); |
| 7075 | if (status != PSA_SUCCESS) { |
| 7076 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 7077 | } |
| 7078 | } else |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7079 | #endif |
| 7080 | { |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 7081 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7082 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 7083 | if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) { |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 7084 | psa_status_t status; |
| 7085 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 7086 | psa_key_derivation_operation_t derivation = |
| 7087 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7088 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7089 | MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE")); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 7090 | |
| 7091 | handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE; |
| 7092 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7093 | status = psa_key_derivation_setup(&derivation, alg); |
| 7094 | if (status != PSA_SUCCESS) { |
| 7095 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 7096 | } |
| 7097 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7098 | status = psa_key_derivation_set_capacity(&derivation, |
| 7099 | PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE); |
| 7100 | if (status != PSA_SUCCESS) { |
| 7101 | psa_key_derivation_abort(&derivation); |
| 7102 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 7103 | } |
| 7104 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7105 | status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx, |
| 7106 | &derivation); |
| 7107 | if (status != PSA_SUCCESS) { |
| 7108 | psa_key_derivation_abort(&derivation); |
| 7109 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 7110 | } |
| 7111 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7112 | status = psa_key_derivation_output_bytes(&derivation, |
| 7113 | handshake->premaster, |
| 7114 | handshake->pmslen); |
| 7115 | if (status != PSA_SUCCESS) { |
| 7116 | psa_key_derivation_abort(&derivation); |
| 7117 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 7118 | } |
| 7119 | |
| 7120 | status = psa_key_derivation_abort(&derivation); |
| 7121 | if (status != PSA_SUCCESS) { |
| 7122 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 7123 | } |
| 7124 | } |
| 7125 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7126 | ret = handshake->tls_prf(handshake->premaster, handshake->pmslen, |
| 7127 | lbl, seed, seed_len, |
| 7128 | master, |
| 7129 | master_secret_len); |
| 7130 | if (ret != 0) { |
| 7131 | MBEDTLS_SSL_DEBUG_RET(1, "prf", ret); |
| 7132 | return ret; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7133 | } |
| 7134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7135 | MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret", |
| 7136 | handshake->premaster, |
| 7137 | handshake->pmslen); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7138 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7139 | mbedtls_platform_zeroize(handshake->premaster, |
| 7140 | sizeof(handshake->premaster)); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7141 | } |
| 7142 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7143 | return 0; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7144 | } |
| 7145 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7146 | int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl) |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 7147 | { |
| 7148 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7149 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 7150 | ssl->handshake->ciphersuite_info; |
| 7151 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7152 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys")); |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 7153 | |
| 7154 | /* Set PRF, calc_verify and calc_finished function pointers */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7155 | ret = ssl_set_handshake_prfs(ssl->handshake, |
Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 7156 | (mbedtls_md_type_t) ciphersuite_info->mac); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7157 | if (ret != 0) { |
| 7158 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret); |
| 7159 | return ret; |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 7160 | } |
| 7161 | |
| 7162 | /* Compute master secret if needed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7163 | ret = ssl_compute_master(ssl->handshake, |
| 7164 | ssl->session_negotiate->master, |
| 7165 | ssl); |
| 7166 | if (ret != 0) { |
| 7167 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret); |
| 7168 | return ret; |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 7169 | } |
| 7170 | |
| 7171 | /* Swap the client and server random values: |
| 7172 | * - MS derivation wanted client+server (RFC 5246 8.1) |
| 7173 | * - key derivation wants server+client (RFC 5246 6.3) */ |
| 7174 | { |
| 7175 | unsigned char tmp[64]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7176 | memcpy(tmp, ssl->handshake->randbytes, 64); |
| 7177 | memcpy(ssl->handshake->randbytes, tmp + 32, 32); |
| 7178 | memcpy(ssl->handshake->randbytes + 32, tmp, 32); |
| 7179 | mbedtls_platform_zeroize(tmp, sizeof(tmp)); |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 7180 | } |
| 7181 | |
| 7182 | /* Populate transform structure */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7183 | ret = ssl_tls12_populate_transform(ssl->transform_negotiate, |
| 7184 | ssl->session_negotiate->ciphersuite, |
| 7185 | ssl->session_negotiate->master, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7186 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7187 | ssl->session_negotiate->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7188 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7189 | ssl->handshake->tls_prf, |
| 7190 | ssl->handshake->randbytes, |
| 7191 | ssl->tls_version, |
| 7192 | ssl->conf->endpoint, |
| 7193 | ssl); |
| 7194 | if (ret != 0) { |
| 7195 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret); |
| 7196 | return ret; |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 7197 | } |
| 7198 | |
| 7199 | /* We no longer need Server/ClientHello.random values */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7200 | mbedtls_platform_zeroize(ssl->handshake->randbytes, |
| 7201 | sizeof(ssl->handshake->randbytes)); |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 7202 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7203 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys")); |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 7204 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7205 | return 0; |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 7206 | } |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 7207 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7208 | 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] | 7209 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7210 | switch (md) { |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 7211 | #if defined(PSA_WANT_ALG_SHA_384) |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 7212 | case MBEDTLS_SSL_HASH_SHA384: |
| 7213 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 7214 | break; |
| 7215 | #endif |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 7216 | #if defined(PSA_WANT_ALG_SHA_256) |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 7217 | case MBEDTLS_SSL_HASH_SHA256: |
| 7218 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 7219 | break; |
| 7220 | #endif |
| 7221 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7222 | return -1; |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 7223 | } |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 7224 | #if !defined(PSA_WANT_ALG_SHA_384) && \ |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 7225 | !defined(PSA_WANT_ALG_SHA_256) |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 7226 | (void) ssl; |
| 7227 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7228 | return 0; |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 7229 | } |
| 7230 | |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7231 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7232 | static int ssl_calc_verify_tls_psa(const mbedtls_ssl_context *ssl, |
| 7233 | const psa_hash_operation_t *hs_op, |
| 7234 | size_t buffer_size, |
| 7235 | unsigned char *hash, |
| 7236 | size_t *hlen) |
| 7237 | { |
| 7238 | psa_status_t status; |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7239 | psa_hash_operation_t cloned_op = psa_hash_operation_init(); |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7240 | |
| 7241 | #if !defined(MBEDTLS_DEBUG_C) |
| 7242 | (void) ssl; |
| 7243 | #endif |
| 7244 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify")); |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7245 | status = psa_hash_clone(hs_op, &cloned_op); |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7246 | if (status != PSA_SUCCESS) { |
| 7247 | goto exit; |
| 7248 | } |
| 7249 | |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7250 | status = psa_hash_finish(&cloned_op, hash, buffer_size, hlen); |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7251 | if (status != PSA_SUCCESS) { |
| 7252 | goto exit; |
| 7253 | } |
| 7254 | |
| 7255 | MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen); |
| 7256 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify")); |
| 7257 | |
| 7258 | exit: |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7259 | psa_hash_abort(&cloned_op); |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7260 | return mbedtls_md_error_from_psa(status); |
| 7261 | } |
| 7262 | #else |
| 7263 | static int ssl_calc_verify_tls_legacy(const mbedtls_ssl_context *ssl, |
| 7264 | const mbedtls_md_context_t *hs_ctx, |
| 7265 | unsigned char *hash, |
| 7266 | size_t *hlen) |
| 7267 | { |
| 7268 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7269 | mbedtls_md_context_t cloned_ctx; |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7270 | |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7271 | mbedtls_md_init(&cloned_ctx); |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7272 | |
| 7273 | #if !defined(MBEDTLS_DEBUG_C) |
| 7274 | (void) ssl; |
| 7275 | #endif |
| 7276 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify")); |
| 7277 | |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7278 | 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] | 7279 | if (ret != 0) { |
| 7280 | goto exit; |
| 7281 | } |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7282 | ret = mbedtls_md_clone(&cloned_ctx, hs_ctx); |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7283 | if (ret != 0) { |
| 7284 | goto exit; |
| 7285 | } |
| 7286 | |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7287 | ret = mbedtls_md_finish(&cloned_ctx, hash); |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7288 | if (ret != 0) { |
| 7289 | goto exit; |
| 7290 | } |
| 7291 | |
| 7292 | *hlen = mbedtls_md_get_size(mbedtls_md_info_from_ctx(hs_ctx)); |
| 7293 | |
| 7294 | MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen); |
| 7295 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify")); |
| 7296 | |
| 7297 | exit: |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7298 | mbedtls_md_free(&cloned_ctx); |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7299 | return ret; |
| 7300 | } |
| 7301 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7302 | |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 7303 | #if defined(PSA_WANT_ALG_SHA_256) |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 7304 | int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl, |
| 7305 | unsigned char *hash, |
| 7306 | size_t *hlen) |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 7307 | { |
| 7308 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7309 | return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha256_psa, 32, |
| 7310 | hash, hlen); |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 7311 | #else |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7312 | return ssl_calc_verify_tls_legacy(ssl, &ssl->handshake->fin_sha256, |
| 7313 | hash, hlen); |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 7314 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 7315 | } |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 7316 | #endif /* PSA_WANT_ALG_SHA_256 */ |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 7317 | |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 7318 | #if defined(PSA_WANT_ALG_SHA_384) |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 7319 | int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl, |
| 7320 | unsigned char *hash, |
| 7321 | size_t *hlen) |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 7322 | { |
| 7323 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7324 | return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha384_psa, 48, |
| 7325 | hash, hlen); |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 7326 | #else |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7327 | return ssl_calc_verify_tls_legacy(ssl, &ssl->handshake->fin_sha384, |
| 7328 | hash, hlen); |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 7329 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 7330 | } |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 7331 | #endif /* PSA_WANT_ALG_SHA_384 */ |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 7332 | |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 7333 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 7334 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7335 | 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] | 7336 | { |
| 7337 | unsigned char *p = ssl->handshake->premaster; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7338 | unsigned char *end = p + sizeof(ssl->handshake->premaster); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7339 | const unsigned char *psk = NULL; |
| 7340 | size_t psk_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7341 | int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7342 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7343 | if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) { |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7344 | /* |
| 7345 | * This should never happen because the existence of a PSK is always |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 7346 | * checked before calling this function. |
| 7347 | * |
| 7348 | * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with |
Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 7349 | * the shared secret without PSK. |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7350 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7351 | if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) { |
| 7352 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 7353 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 7354 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7355 | } |
| 7356 | |
| 7357 | /* |
| 7358 | * PMS = struct { |
| 7359 | * opaque other_secret<0..2^16-1>; |
| 7360 | * opaque psk<0..2^16-1>; |
| 7361 | * }; |
| 7362 | * with "other_secret" depending on the particular key exchange |
| 7363 | */ |
| 7364 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7365 | if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) { |
| 7366 | if (end - p < 2) { |
| 7367 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 7368 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7370 | MBEDTLS_PUT_UINT16_BE(psk_len, p, 0); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7371 | p += 2; |
| 7372 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7373 | if (end < p || (size_t) (end - p) < psk_len) { |
| 7374 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 7375 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7377 | memset(p, 0, psk_len); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7378 | p += psk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7379 | } else |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7380 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7381 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7382 | if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) { |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7383 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7384 | size_t len; |
| 7385 | |
| 7386 | /* Write length only when we know the actual value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7387 | if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx, |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 7388 | p + 2, (size_t) (end - (p + 2)), &len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7389 | ssl->conf->f_rng, ssl->conf->p_rng)) != 0) { |
| 7390 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret); |
| 7391 | return ret; |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7392 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7393 | MBEDTLS_PUT_UINT16_BE(len, p, 0); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7394 | p += 2 + len; |
| 7395 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7396 | MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K); |
| 7397 | } else |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7398 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 7399 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7400 | if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) { |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7401 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7402 | size_t zlen; |
| 7403 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7404 | if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen, |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 7405 | p + 2, (size_t) (end - (p + 2)), |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7406 | ssl->conf->f_rng, ssl->conf->p_rng)) != 0) { |
| 7407 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret); |
| 7408 | return ret; |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7409 | } |
| 7410 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7411 | MBEDTLS_PUT_UINT16_BE(zlen, p, 0); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7412 | p += 2 + zlen; |
| 7413 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7414 | MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx, |
| 7415 | MBEDTLS_DEBUG_ECDH_Z); |
| 7416 | } else |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 7417 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7418 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7419 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 7420 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7421 | } |
| 7422 | |
| 7423 | /* opaque psk<0..2^16-1>; */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7424 | if (end - p < 2) { |
| 7425 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 7426 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7427 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7428 | MBEDTLS_PUT_UINT16_BE(psk_len, p, 0); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7429 | p += 2; |
| 7430 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7431 | if (end < p || (size_t) (end - p) < psk_len) { |
| 7432 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 7433 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7434 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7435 | memcpy(p, psk, psk_len); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7436 | p += psk_len; |
| 7437 | |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 7438 | ssl->handshake->pmslen = (size_t) (p - ssl->handshake->premaster); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7439 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7440 | return 0; |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7441 | } |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 7442 | #endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 7443 | |
| 7444 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7445 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7446 | static int ssl_write_hello_request(mbedtls_ssl_context *ssl); |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 7447 | |
| 7448 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7449 | int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl) |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 7450 | { |
| 7451 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 7452 | * timeout if we were using the usual handshake doubling scheme */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7453 | if (ssl->conf->renego_max_records < 0) { |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 7454 | uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1; |
| 7455 | unsigned char doublings = 1; |
| 7456 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7457 | while (ratio != 0) { |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 7458 | ++doublings; |
| 7459 | ratio >>= 1; |
| 7460 | } |
| 7461 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7462 | if (++ssl->renego_records_seen > doublings) { |
| 7463 | MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request")); |
| 7464 | return 0; |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 7465 | } |
| 7466 | } |
| 7467 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7468 | return ssl_write_hello_request(ssl); |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 7469 | } |
| 7470 | #endif |
| 7471 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7472 | |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7473 | /* |
| 7474 | * Handshake functions |
| 7475 | */ |
| 7476 | #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 7477 | /* No certificate support -> dummy functions */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7478 | int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7479 | { |
| 7480 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 7481 | ssl->handshake->ciphersuite_info; |
| 7482 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7483 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7484 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7485 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { |
| 7486 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7487 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7488 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7489 | } |
| 7490 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7491 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 7492 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7493 | } |
| 7494 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7495 | int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7496 | { |
| 7497 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 7498 | ssl->handshake->ciphersuite_info; |
| 7499 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7500 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7501 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7502 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { |
| 7503 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7504 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7505 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7506 | } |
| 7507 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7508 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 7509 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7510 | } |
| 7511 | |
| 7512 | #else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 7513 | /* Some certificate support -> implement write and parse */ |
| 7514 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7515 | int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7516 | { |
| 7517 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 7518 | size_t i, n; |
| 7519 | const mbedtls_x509_crt *crt; |
| 7520 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 7521 | ssl->handshake->ciphersuite_info; |
| 7522 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7523 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7524 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7525 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { |
| 7526 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7527 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7528 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7529 | } |
| 7530 | |
| 7531 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7532 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 7533 | if (ssl->handshake->client_auth == 0) { |
| 7534 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7535 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7536 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7537 | } |
| 7538 | } |
| 7539 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 7540 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7541 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 7542 | if (mbedtls_ssl_own_cert(ssl) == NULL) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7543 | /* Should never happen because we shouldn't have picked the |
| 7544 | * ciphersuite if we don't have a certificate. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7545 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7546 | } |
| 7547 | } |
| 7548 | #endif |
| 7549 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7550 | MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl)); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7551 | |
| 7552 | /* |
| 7553 | * 0 . 0 handshake type |
| 7554 | * 1 . 3 handshake length |
| 7555 | * 4 . 6 length of all certs |
| 7556 | * 7 . 9 length of cert. 1 |
| 7557 | * 10 . n-1 peer certificate |
| 7558 | * n . n+2 length of cert. 2 |
| 7559 | * n+3 . ... upper level cert, etc. |
| 7560 | */ |
| 7561 | i = 7; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7562 | crt = mbedtls_ssl_own_cert(ssl); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7563 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7564 | while (crt != NULL) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7565 | n = crt->raw.len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7566 | if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) { |
| 7567 | MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET |
| 7568 | " > %" MBEDTLS_PRINTF_SIZET, |
| 7569 | i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN)); |
| 7570 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7571 | } |
| 7572 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7573 | ssl->out_msg[i] = MBEDTLS_BYTE_2(n); |
| 7574 | ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n); |
| 7575 | ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7576 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7577 | i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7578 | i += n; crt = crt->next; |
| 7579 | } |
| 7580 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7581 | ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7); |
| 7582 | ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7); |
| 7583 | ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7584 | |
| 7585 | ssl->out_msglen = i; |
| 7586 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 7587 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
| 7588 | |
| 7589 | ssl->state++; |
| 7590 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7591 | if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { |
| 7592 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret); |
| 7593 | return ret; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7594 | } |
| 7595 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7596 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7597 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7598 | return ret; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7599 | } |
| 7600 | |
| 7601 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 7602 | |
| 7603 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7604 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7605 | static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl, |
| 7606 | unsigned char *crt_buf, |
| 7607 | size_t crt_buf_len) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7608 | { |
| 7609 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; |
| 7610 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7611 | if (peer_crt == NULL) { |
| 7612 | return -1; |
| 7613 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7614 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7615 | if (peer_crt->raw.len != crt_buf_len) { |
| 7616 | return -1; |
| 7617 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7618 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7619 | return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7620 | } |
| 7621 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7622 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7623 | static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl, |
| 7624 | unsigned char *crt_buf, |
| 7625 | size_t crt_buf_len) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7626 | { |
| 7627 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7628 | unsigned char const * const peer_cert_digest = |
| 7629 | ssl->session->peer_cert_digest; |
| 7630 | mbedtls_md_type_t const peer_cert_digest_type = |
| 7631 | ssl->session->peer_cert_digest_type; |
| 7632 | mbedtls_md_info_t const * const digest_info = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7633 | mbedtls_md_info_from_type(peer_cert_digest_type); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7634 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; |
| 7635 | size_t digest_len; |
| 7636 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7637 | if (peer_cert_digest == NULL || digest_info == NULL) { |
| 7638 | return -1; |
| 7639 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7640 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7641 | digest_len = mbedtls_md_get_size(digest_info); |
| 7642 | if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) { |
| 7643 | return -1; |
| 7644 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7645 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7646 | ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest); |
| 7647 | if (ret != 0) { |
| 7648 | return -1; |
| 7649 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7650 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7651 | return memcmp(tmp_digest, peer_cert_digest, digest_len); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7652 | } |
| 7653 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7654 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 7655 | |
| 7656 | /* |
| 7657 | * Once the certificate message is read, parse it into a cert chain and |
| 7658 | * perform basic checks, but leave actual verification to the caller |
| 7659 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7660 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7661 | static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl, |
| 7662 | mbedtls_x509_crt *chain) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7663 | { |
| 7664 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7665 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7666 | int crt_cnt = 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7667 | #endif |
| 7668 | size_t i, n; |
| 7669 | uint8_t alert; |
| 7670 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7671 | if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) { |
| 7672 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7673 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7674 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
| 7675 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7676 | } |
| 7677 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7678 | if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) { |
| 7679 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7680 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
| 7681 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7682 | } |
| 7683 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7684 | if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) { |
| 7685 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7686 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7687 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
| 7688 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7689 | } |
| 7690 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7691 | i = mbedtls_ssl_hs_hdr_len(ssl); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7692 | |
| 7693 | /* |
| 7694 | * Same message structure as in mbedtls_ssl_write_certificate() |
| 7695 | */ |
Dave Rodgman | a3d0f61 | 2023-11-03 23:34:02 +0000 | [diff] [blame] | 7696 | n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7697 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7698 | if (ssl->in_msg[i] != 0 || |
| 7699 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) { |
| 7700 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7701 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7702 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
| 7703 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7704 | } |
| 7705 | |
| 7706 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 7707 | i += 3; |
| 7708 | |
| 7709 | /* Iterate through and parse the CRTs in the provided chain. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7710 | while (i < ssl->in_hslen) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7711 | /* Check that there's room for the next CRT's length fields. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7712 | if (i + 3 > ssl->in_hslen) { |
| 7713 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7714 | mbedtls_ssl_send_alert_message(ssl, |
| 7715 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7716 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
| 7717 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7718 | } |
| 7719 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 7720 | * anything beyond 2**16 ~ 64K. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7721 | if (ssl->in_msg[i] != 0) { |
| 7722 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7723 | mbedtls_ssl_send_alert_message(ssl, |
| 7724 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7725 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT); |
| 7726 | return MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7727 | } |
| 7728 | |
| 7729 | /* Read length of the next CRT in the chain. */ |
Dave Rodgman | a3d0f61 | 2023-11-03 23:34:02 +0000 | [diff] [blame] | 7730 | n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7731 | i += 3; |
| 7732 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7733 | if (n < 128 || i + n > ssl->in_hslen) { |
| 7734 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7735 | mbedtls_ssl_send_alert_message(ssl, |
| 7736 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7737 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
| 7738 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7739 | } |
| 7740 | |
| 7741 | /* Check if we're handling the first CRT in the chain. */ |
| 7742 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7743 | if (crt_cnt++ == 0 && |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7744 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7745 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7746 | /* During client-side renegotiation, check that the server's |
| 7747 | * end-CRTs hasn't changed compared to the initial handshake, |
| 7748 | * mitigating the triple handshake attack. On success, reuse |
| 7749 | * the original end-CRT instead of parsing it again. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7750 | MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation")); |
| 7751 | if (ssl_check_peer_crt_unchanged(ssl, |
| 7752 | &ssl->in_msg[i], |
| 7753 | n) != 0) { |
| 7754 | MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation")); |
| 7755 | mbedtls_ssl_send_alert_message(ssl, |
| 7756 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7757 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED); |
| 7758 | return MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7759 | } |
| 7760 | |
| 7761 | /* Now we can safely free the original chain. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7762 | ssl_clear_peer_cert(ssl->session); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7763 | } |
| 7764 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 7765 | |
| 7766 | /* Parse the next certificate in the chain. */ |
| 7767 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7768 | ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7769 | #else |
| 7770 | /* If we don't need to store the CRT chain permanently, parse |
| 7771 | * it in-place from the input buffer instead of making a copy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7772 | 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] | 7773 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7774 | switch (ret) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7775 | case 0: /*ok*/ |
| 7776 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 7777 | /* Ignore certificate with an unknown algorithm: maybe a |
| 7778 | prior certificate was already trusted. */ |
| 7779 | break; |
| 7780 | |
| 7781 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 7782 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 7783 | goto crt_parse_der_failed; |
| 7784 | |
| 7785 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 7786 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7787 | goto crt_parse_der_failed; |
| 7788 | |
| 7789 | default: |
| 7790 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7791 | crt_parse_der_failed: |
| 7792 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert); |
| 7793 | MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret); |
| 7794 | return ret; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7795 | } |
| 7796 | |
| 7797 | i += n; |
| 7798 | } |
| 7799 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7800 | MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain); |
| 7801 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7802 | } |
| 7803 | |
| 7804 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7805 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7806 | 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] | 7807 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7808 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 7809 | return -1; |
| 7810 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7811 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7812 | if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) && |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7813 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 7814 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7815 | memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) { |
| 7816 | MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate")); |
| 7817 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7818 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7819 | return -1; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7820 | } |
| 7821 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7822 | |
| 7823 | /* Check if a certificate message is expected. |
| 7824 | * Return either |
| 7825 | * - SSL_CERTIFICATE_EXPECTED, or |
| 7826 | * - SSL_CERTIFICATE_SKIP |
| 7827 | * indicating whether a Certificate message is expected or not. |
| 7828 | */ |
| 7829 | #define SSL_CERTIFICATE_EXPECTED 0 |
| 7830 | #define SSL_CERTIFICATE_SKIP 1 |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7831 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7832 | static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl, |
| 7833 | int authmode) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7834 | { |
| 7835 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 7836 | ssl->handshake->ciphersuite_info; |
| 7837 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7838 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { |
| 7839 | return SSL_CERTIFICATE_SKIP; |
| 7840 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7841 | |
| 7842 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7843 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7844 | if (authmode == MBEDTLS_SSL_VERIFY_NONE) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7845 | ssl->session_negotiate->verify_result = |
| 7846 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7847 | return SSL_CERTIFICATE_SKIP; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7848 | } |
| 7849 | } |
| 7850 | #else |
| 7851 | ((void) authmode); |
| 7852 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7853 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7854 | return SSL_CERTIFICATE_EXPECTED; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7855 | } |
| 7856 | |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7857 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7858 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7859 | static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl, |
| 7860 | unsigned char *start, size_t len) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7861 | { |
| 7862 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7863 | /* Remember digest of the peer's end-CRT. */ |
| 7864 | ssl->session_negotiate->peer_cert_digest = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7865 | mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN); |
| 7866 | if (ssl->session_negotiate->peer_cert_digest == NULL) { |
| 7867 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed", |
| 7868 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN)); |
| 7869 | mbedtls_ssl_send_alert_message(ssl, |
| 7870 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7871 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7872 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7873 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7874 | } |
| 7875 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7876 | ret = mbedtls_md(mbedtls_md_info_from_type( |
| 7877 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE), |
| 7878 | start, len, |
| 7879 | ssl->session_negotiate->peer_cert_digest); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7880 | |
| 7881 | ssl->session_negotiate->peer_cert_digest_type = |
| 7882 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 7883 | ssl->session_negotiate->peer_cert_digest_len = |
| 7884 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 7885 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7886 | return ret; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7887 | } |
| 7888 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7889 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7890 | static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl, |
| 7891 | unsigned char *start, size_t len) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7892 | { |
| 7893 | unsigned char *end = start + len; |
| 7894 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7895 | |
| 7896 | /* Make a copy of the peer's raw public key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7897 | mbedtls_pk_init(&ssl->handshake->peer_pubkey); |
| 7898 | ret = mbedtls_pk_parse_subpubkey(&start, end, |
| 7899 | &ssl->handshake->peer_pubkey); |
| 7900 | if (ret != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7901 | /* We should have parsed the public key before. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7902 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7903 | } |
| 7904 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7905 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7906 | } |
| 7907 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7908 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7909 | int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7910 | { |
| 7911 | int ret = 0; |
| 7912 | int crt_expected; |
Manuel Pégourié-Gonnard | 58ab9ba | 2024-08-14 09:47:38 +0200 | [diff] [blame] | 7913 | /* Authmode: precedence order is SNI if used else configuration */ |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7914 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 7915 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 7916 | ? ssl->handshake->sni_authmode |
| 7917 | : ssl->conf->authmode; |
| 7918 | #else |
| 7919 | const int authmode = ssl->conf->authmode; |
| 7920 | #endif |
| 7921 | void *rs_ctx = NULL; |
| 7922 | mbedtls_x509_crt *chain = NULL; |
| 7923 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7924 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7925 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7926 | crt_expected = ssl_parse_certificate_coordinate(ssl, authmode); |
| 7927 | if (crt_expected == SSL_CERTIFICATE_SKIP) { |
| 7928 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7929 | goto exit; |
| 7930 | } |
| 7931 | |
| 7932 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7933 | if (ssl->handshake->ecrs_enabled && |
| 7934 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7935 | chain = ssl->handshake->ecrs_peer_cert; |
| 7936 | ssl->handshake->ecrs_peer_cert = NULL; |
| 7937 | goto crt_verify; |
| 7938 | } |
| 7939 | #endif |
| 7940 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7941 | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7942 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 7943 | let it decide whether to alert. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7944 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7945 | goto exit; |
| 7946 | } |
| 7947 | |
| 7948 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7949 | if (ssl_srv_check_client_no_crt_notification(ssl) == 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7950 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
| 7951 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7952 | if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7953 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7954 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7955 | |
| 7956 | goto exit; |
| 7957 | } |
| 7958 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7959 | |
| 7960 | /* Clear existing peer CRT structure in case we tried to |
| 7961 | * reuse a session but it failed, and allocate a new one. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7962 | ssl_clear_peer_cert(ssl->session_negotiate); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7963 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7964 | chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt)); |
| 7965 | if (chain == NULL) { |
| 7966 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", |
| 7967 | sizeof(mbedtls_x509_crt))); |
| 7968 | mbedtls_ssl_send_alert_message(ssl, |
| 7969 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7970 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7971 | |
| 7972 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 7973 | goto exit; |
| 7974 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7975 | mbedtls_x509_crt_init(chain); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7976 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7977 | ret = ssl_parse_certificate_chain(ssl, chain); |
| 7978 | if (ret != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7979 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7980 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7981 | |
| 7982 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7983 | if (ssl->handshake->ecrs_enabled) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7984 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7985 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7986 | |
| 7987 | crt_verify: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7988 | if (ssl->handshake->ecrs_enabled) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7989 | rs_ctx = &ssl->handshake->ecrs_ctx; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7990 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7991 | #endif |
| 7992 | |
Manuel Pégourié-Gonnard | 19dd9f5 | 2024-08-16 11:03:42 +0200 | [diff] [blame] | 7993 | ret = mbedtls_ssl_verify_certificate(ssl, authmode, chain, |
| 7994 | ssl->handshake->ciphersuite_info, |
| 7995 | rs_ctx); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7996 | if (ret != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7997 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7998 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7999 | |
| 8000 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8001 | { |
| 8002 | unsigned char *crt_start, *pk_start; |
| 8003 | size_t crt_len, pk_len; |
| 8004 | |
| 8005 | /* We parse the CRT chain without copying, so |
| 8006 | * these pointers point into the input buffer, |
| 8007 | * and are hence still valid after freeing the |
| 8008 | * CRT chain. */ |
| 8009 | |
| 8010 | crt_start = chain->raw.p; |
| 8011 | crt_len = chain->raw.len; |
| 8012 | |
| 8013 | pk_start = chain->pk_raw.p; |
| 8014 | pk_len = chain->pk_raw.len; |
| 8015 | |
| 8016 | /* Free the CRT structures before computing |
| 8017 | * digest and copying the peer's public key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8018 | mbedtls_x509_crt_free(chain); |
| 8019 | mbedtls_free(chain); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8020 | chain = NULL; |
| 8021 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8022 | ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len); |
| 8023 | if (ret != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8024 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8025 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8026 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8027 | ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len); |
| 8028 | if (ret != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8029 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8030 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8031 | } |
| 8032 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8033 | /* Pass ownership to session structure. */ |
| 8034 | ssl->session_negotiate->peer_cert = chain; |
| 8035 | chain = NULL; |
| 8036 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8037 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8038 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8039 | |
| 8040 | exit: |
| 8041 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8042 | if (ret == 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8043 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8044 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8045 | |
| 8046 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8047 | if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8048 | ssl->handshake->ecrs_peer_cert = chain; |
| 8049 | chain = NULL; |
| 8050 | } |
| 8051 | #endif |
| 8052 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8053 | if (chain != NULL) { |
| 8054 | mbedtls_x509_crt_free(chain); |
| 8055 | mbedtls_free(chain); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8056 | } |
| 8057 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8058 | return ret; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8059 | } |
| 8060 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 8061 | |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8062 | static int ssl_calc_finished_tls_generic(mbedtls_ssl_context *ssl, void *ctx, |
| 8063 | unsigned char *padbuf, size_t hlen, |
| 8064 | unsigned char *buf, int from) |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8065 | { |
Dave Rodgman | c37ad44 | 2023-11-03 23:36:06 +0000 | [diff] [blame] | 8066 | unsigned int len = 12; |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8067 | const char *sender; |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8068 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8069 | psa_status_t status; |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8070 | psa_hash_operation_t *hs_op = ctx; |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 8071 | psa_hash_operation_t cloned_op = PSA_HASH_OPERATION_INIT; |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8072 | size_t hash_size; |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8073 | #else |
Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 8074 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8075 | mbedtls_md_context_t *hs_ctx = ctx; |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 8076 | mbedtls_md_context_t cloned_ctx; |
| 8077 | mbedtls_md_init(&cloned_ctx); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8078 | #endif |
| 8079 | |
| 8080 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8081 | if (!session) { |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8082 | session = ssl->session; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8083 | } |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8084 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8085 | sender = (from == MBEDTLS_SSL_IS_CLIENT) |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8086 | ? "client finished" |
| 8087 | : "server finished"; |
| 8088 | |
| 8089 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8090 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls")); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8091 | |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 8092 | status = psa_hash_clone(hs_op, &cloned_op); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8093 | if (status != PSA_SUCCESS) { |
Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 8094 | goto exit; |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8095 | } |
| 8096 | |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 8097 | status = psa_hash_finish(&cloned_op, padbuf, hlen, &hash_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8098 | if (status != PSA_SUCCESS) { |
Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 8099 | goto exit; |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8100 | } |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8101 | MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, hlen); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8102 | #else |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8103 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls")); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8104 | |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 8105 | 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] | 8106 | if (ret != 0) { |
| 8107 | goto exit; |
| 8108 | } |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 8109 | ret = mbedtls_md_clone(&cloned_ctx, hs_ctx); |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 8110 | if (ret != 0) { |
| 8111 | goto exit; |
| 8112 | } |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8113 | |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 8114 | ret = mbedtls_md_finish(&cloned_ctx, padbuf); |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8115 | if (ret != 0) { |
| 8116 | goto exit; |
| 8117 | } |
| 8118 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8119 | |
| 8120 | MBEDTLS_SSL_DEBUG_BUF(4, "finished output", padbuf, hlen); |
| 8121 | |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8122 | /* |
| 8123 | * TLSv1.2: |
| 8124 | * hash = PRF( master, finished_label, |
| 8125 | * Hash( handshake ) )[0.11] |
| 8126 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8127 | ssl->handshake->tls_prf(session->master, 48, sender, |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8128 | padbuf, hlen, buf, len); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8129 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8130 | MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8131 | |
Paul Elliott | ecb95be | 2023-08-11 16:41:04 +0100 | [diff] [blame] | 8132 | mbedtls_platform_zeroize(padbuf, hlen); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8133 | |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8134 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished")); |
Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 8135 | |
| 8136 | exit: |
| 8137 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 8138 | psa_hash_abort(&cloned_op); |
Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 8139 | return mbedtls_md_error_from_psa(status); |
Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 8140 | #else |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 8141 | mbedtls_md_free(&cloned_ctx); |
Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 8142 | return ret; |
| 8143 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8144 | } |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8145 | |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 8146 | #if defined(PSA_WANT_ALG_SHA_256) |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8147 | static int ssl_calc_finished_tls_sha256( |
| 8148 | mbedtls_ssl_context *ssl, unsigned char *buf, int from) |
| 8149 | { |
| 8150 | unsigned char padbuf[32]; |
| 8151 | return ssl_calc_finished_tls_generic(ssl, |
| 8152 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8153 | &ssl->handshake->fin_sha256_psa, |
| 8154 | #else |
| 8155 | &ssl->handshake->fin_sha256, |
| 8156 | #endif |
| 8157 | padbuf, sizeof(padbuf), |
| 8158 | buf, from); |
| 8159 | } |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 8160 | #endif /* PSA_WANT_ALG_SHA_256*/ |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8161 | |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 8162 | |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 8163 | #if defined(PSA_WANT_ALG_SHA_384) |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 8164 | static int ssl_calc_finished_tls_sha384( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8165 | mbedtls_ssl_context *ssl, unsigned char *buf, int from) |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 8166 | { |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 8167 | unsigned char padbuf[48]; |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8168 | return ssl_calc_finished_tls_generic(ssl, |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 8169 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8170 | &ssl->handshake->fin_sha384_psa, |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 8171 | #else |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8172 | &ssl->handshake->fin_sha384, |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 8173 | #endif |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8174 | padbuf, sizeof(padbuf), |
| 8175 | buf, from); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 8176 | } |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 8177 | #endif /* PSA_WANT_ALG_SHA_384*/ |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 8178 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8179 | void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl) |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 8180 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8181 | MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free")); |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 8182 | |
| 8183 | /* |
| 8184 | * Free our handshake params |
| 8185 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8186 | mbedtls_ssl_handshake_free(ssl); |
| 8187 | mbedtls_free(ssl->handshake); |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 8188 | ssl->handshake = NULL; |
| 8189 | |
| 8190 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 8191 | * Free the previous transform and switch in the current one |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 8192 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8193 | if (ssl->transform) { |
| 8194 | mbedtls_ssl_transform_free(ssl->transform); |
| 8195 | mbedtls_free(ssl->transform); |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 8196 | } |
| 8197 | ssl->transform = ssl->transform_negotiate; |
| 8198 | ssl->transform_negotiate = NULL; |
| 8199 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8200 | MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free")); |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 8201 | } |
| 8202 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8203 | void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl) |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8204 | { |
| 8205 | int resume = ssl->handshake->resume; |
| 8206 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8207 | MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup")); |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8208 | |
| 8209 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8210 | if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) { |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8211 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
| 8212 | ssl->renego_records_seen = 0; |
| 8213 | } |
| 8214 | #endif |
| 8215 | |
| 8216 | /* |
| 8217 | * Free the previous session and switch in the current one |
| 8218 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8219 | if (ssl->session) { |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8220 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 8221 | /* RFC 7366 3.1: keep the EtM state */ |
| 8222 | ssl->session_negotiate->encrypt_then_mac = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8223 | ssl->session->encrypt_then_mac; |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8224 | #endif |
| 8225 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8226 | mbedtls_ssl_session_free(ssl->session); |
| 8227 | mbedtls_free(ssl->session); |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8228 | } |
| 8229 | ssl->session = ssl->session_negotiate; |
| 8230 | ssl->session_negotiate = NULL; |
| 8231 | |
| 8232 | /* |
| 8233 | * Add cache entry |
| 8234 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8235 | if (ssl->conf->f_set_cache != NULL && |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8236 | ssl->session->id_len != 0 && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8237 | resume == 0) { |
| 8238 | if (ssl->conf->f_set_cache(ssl->conf->p_cache, |
| 8239 | ssl->session->id, |
| 8240 | ssl->session->id_len, |
| 8241 | ssl->session) != 0) { |
| 8242 | MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session")); |
| 8243 | } |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8244 | } |
| 8245 | |
| 8246 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8247 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 8248 | ssl->handshake->flight != NULL) { |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8249 | /* Cancel handshake timer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8250 | mbedtls_ssl_set_timer(ssl, 0); |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8251 | |
| 8252 | /* Keep last flight around in case we need to resend it: |
| 8253 | * we need the handshake and transform structures for that */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8254 | MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform")); |
| 8255 | } else |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8256 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8257 | mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl); |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8258 | |
Jerry Yu | 5ed73ff | 2022-10-27 13:08:42 +0800 | [diff] [blame] | 8259 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8260 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8261 | MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup")); |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8262 | } |
| 8263 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8264 | int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl) |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8265 | { |
Dave Rodgman | c37ad44 | 2023-11-03 23:36:06 +0000 | [diff] [blame] | 8266 | int ret; |
| 8267 | unsigned int hash_len; |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8268 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8269 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished")); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8270 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8271 | mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8272 | |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 8273 | ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint); |
| 8274 | if (ret != 0) { |
| 8275 | MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret); |
| 8276 | } |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8277 | |
| 8278 | /* |
| 8279 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 8280 | * may define some other value. Currently (early 2016), no defined |
| 8281 | * ciphersuite does this (and this is unlikely to change as activity has |
| 8282 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 8283 | */ |
| 8284 | hash_len = 12; |
| 8285 | |
| 8286 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 8287 | ssl->verify_data_len = hash_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8288 | memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8289 | #endif |
| 8290 | |
| 8291 | ssl->out_msglen = 4 + hash_len; |
| 8292 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 8293 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
| 8294 | |
| 8295 | /* |
| 8296 | * In case of session resuming, invert the client and server |
| 8297 | * ChangeCipherSpec messages order. |
| 8298 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8299 | if (ssl->handshake->resume != 0) { |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8300 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8301 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8302 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8303 | } |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8304 | #endif |
| 8305 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8306 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8307 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8308 | } |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8309 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8310 | } else { |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8311 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8312 | } |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8313 | |
| 8314 | /* |
| 8315 | * Switch to our negotiated transform and session parameters for outbound |
| 8316 | * data. |
| 8317 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8318 | 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] | 8319 | |
| 8320 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8321 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8322 | unsigned char i; |
| 8323 | |
| 8324 | /* Remember current epoch settings for resending */ |
| 8325 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8326 | memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, |
| 8327 | sizeof(ssl->handshake->alt_out_ctr)); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8328 | |
| 8329 | /* Set sequence_number to zero */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8330 | 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] | 8331 | |
| 8332 | |
| 8333 | /* Increment epoch */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8334 | for (i = 2; i > 0; i--) { |
| 8335 | if (++ssl->cur_out_ctr[i - 1] != 0) { |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8336 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8337 | } |
| 8338 | } |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8339 | |
| 8340 | /* The loop goes to its end iff the counter is wrapping */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8341 | if (i == 0) { |
| 8342 | MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap")); |
| 8343 | return MBEDTLS_ERR_SSL_COUNTER_WRAPPING; |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8344 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8345 | } else |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8346 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8347 | memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr)); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8348 | |
| 8349 | ssl->transform_out = ssl->transform_negotiate; |
| 8350 | ssl->session_out = ssl->session_negotiate; |
| 8351 | |
| 8352 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8353 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 8354 | mbedtls_ssl_send_flight_completed(ssl); |
| 8355 | } |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8356 | #endif |
| 8357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8358 | if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { |
| 8359 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret); |
| 8360 | return ret; |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8361 | } |
| 8362 | |
| 8363 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8364 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 8365 | (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) { |
| 8366 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret); |
| 8367 | return ret; |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8368 | } |
| 8369 | #endif |
| 8370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8371 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished")); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8372 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8373 | return 0; |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8374 | } |
| 8375 | |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8376 | #define SSL_MAX_HASH_LEN 12 |
| 8377 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8378 | int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl) |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8379 | { |
| 8380 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 8381 | unsigned int hash_len = 12; |
| 8382 | unsigned char buf[SSL_MAX_HASH_LEN]; |
| 8383 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8384 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished")); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8385 | |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 8386 | ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1); |
| 8387 | if (ret != 0) { |
| 8388 | MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret); |
| 8389 | } |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8390 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8391 | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { |
| 8392 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8393 | goto exit; |
| 8394 | } |
| 8395 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8396 | if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) { |
| 8397 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message")); |
| 8398 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8399 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8400 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 8401 | goto exit; |
| 8402 | } |
| 8403 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8404 | if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) { |
| 8405 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8406 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8407 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 8408 | goto exit; |
| 8409 | } |
| 8410 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8411 | if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) { |
| 8412 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message")); |
| 8413 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8414 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8415 | ret = MBEDTLS_ERR_SSL_DECODE_ERROR; |
| 8416 | goto exit; |
| 8417 | } |
| 8418 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8419 | if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), |
| 8420 | buf, hash_len) != 0) { |
| 8421 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message")); |
| 8422 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8423 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8424 | ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
| 8425 | goto exit; |
| 8426 | } |
| 8427 | |
| 8428 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 8429 | ssl->verify_data_len = hash_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8430 | memcpy(ssl->peer_verify_data, buf, hash_len); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8431 | #endif |
| 8432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8433 | if (ssl->handshake->resume != 0) { |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8434 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8435 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8436 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8437 | } |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8438 | #endif |
| 8439 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8440 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8441 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8442 | } |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8443 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8444 | } else { |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8445 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8446 | } |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8447 | |
| 8448 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8449 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 8450 | mbedtls_ssl_recv_flight_completed(ssl); |
| 8451 | } |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8452 | #endif |
| 8453 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8454 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished")); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8455 | |
| 8456 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8457 | mbedtls_platform_zeroize(buf, hash_len); |
| 8458 | return ret; |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8459 | } |
| 8460 | |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 8461 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 8462 | /* |
| 8463 | * Helper to get TLS 1.2 PRF from ciphersuite |
| 8464 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) |
| 8465 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8466 | static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id) |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 8467 | { |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 8468 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8469 | mbedtls_ssl_ciphersuite_from_id(ciphersuite_id); |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 8470 | #if defined(PSA_WANT_ALG_SHA_384) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8471 | if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) { |
| 8472 | return tls_prf_sha384; |
| 8473 | } else |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 8474 | #endif |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 8475 | #if defined(PSA_WANT_ALG_SHA_256) |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 8476 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8477 | if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) { |
| 8478 | return tls_prf_sha256; |
| 8479 | } |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 8480 | } |
| 8481 | #endif |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 8482 | #if !defined(PSA_WANT_ALG_SHA_384) && \ |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 8483 | !defined(PSA_WANT_ALG_SHA_256) |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 8484 | (void) ciphersuite_info; |
| 8485 | #endif |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 8486 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8487 | return NULL; |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 8488 | } |
| 8489 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 8490 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8491 | 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] | 8492 | { |
| 8493 | ((void) tls_prf); |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 8494 | #if defined(PSA_WANT_ALG_SHA_384) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8495 | if (tls_prf == tls_prf_sha384) { |
| 8496 | return MBEDTLS_SSL_TLS_PRF_SHA384; |
| 8497 | } else |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 8498 | #endif |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 8499 | #if defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8500 | if (tls_prf == tls_prf_sha256) { |
| 8501 | return MBEDTLS_SSL_TLS_PRF_SHA256; |
| 8502 | } else |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 8503 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8504 | return MBEDTLS_SSL_TLS_PRF_NONE; |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 8505 | } |
| 8506 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8507 | /* |
| 8508 | * Populate a transform structure with session keys and all the other |
| 8509 | * necessary information. |
| 8510 | * |
| 8511 | * Parameters: |
| 8512 | * - [in/out]: transform: structure to populate |
| 8513 | * [in] must be just initialised with mbedtls_ssl_transform_init() |
| 8514 | * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf() |
| 8515 | * - [in] ciphersuite |
| 8516 | * - [in] master |
| 8517 | * - [in] encrypt_then_mac |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8518 | * - [in] tls_prf: pointer to PRF to use for key derivation |
| 8519 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 8520 | * - [in] tls_version: TLS version |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8521 | * - [in] endpoint: client or server |
| 8522 | * - [in] ssl: used for: |
| 8523 | * - ssl->conf->{f,p}_export_keys |
| 8524 | * [in] optionally used for: |
| 8525 | * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg |
| 8526 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 8527 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8528 | static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform, |
| 8529 | int ciphersuite, |
| 8530 | const unsigned char master[48], |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8531 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8532 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8533 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8534 | ssl_tls_prf_t tls_prf, |
| 8535 | const unsigned char randbytes[64], |
| 8536 | mbedtls_ssl_protocol_version tls_version, |
| 8537 | unsigned endpoint, |
| 8538 | const mbedtls_ssl_context *ssl) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8539 | { |
| 8540 | int ret = 0; |
| 8541 | unsigned char keyblk[256]; |
| 8542 | unsigned char *key1; |
| 8543 | unsigned char *key2; |
| 8544 | unsigned char *mac_enc; |
| 8545 | unsigned char *mac_dec; |
| 8546 | size_t mac_key_len = 0; |
| 8547 | size_t iv_copy_len; |
| 8548 | size_t keylen; |
| 8549 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8550 | mbedtls_ssl_mode_t ssl_mode; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8551 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8552 | const mbedtls_cipher_info_t *cipher_info; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8553 | const mbedtls_md_info_t *md_info; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8554 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8555 | |
| 8556 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8557 | psa_key_type_t key_type; |
| 8558 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8559 | psa_algorithm_t alg; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8560 | psa_algorithm_t mac_alg = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8561 | size_t key_bits; |
| 8562 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 8563 | #endif |
| 8564 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8565 | /* |
| 8566 | * Some data just needs copying into the structure |
| 8567 | */ |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8568 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8569 | transform->encrypt_then_mac = encrypt_then_mac; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8570 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 8571 | transform->tls_version = tls_version; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8572 | |
| 8573 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8574 | memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes)); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8575 | #endif |
| 8576 | |
| 8577 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8578 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8579 | /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform |
| 8580 | * generation separate. This should never happen. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8581 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8582 | } |
| 8583 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 8584 | |
| 8585 | /* |
| 8586 | * Get various info structures |
| 8587 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8588 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite); |
| 8589 | if (ciphersuite_info == NULL) { |
| 8590 | MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found", |
| 8591 | ciphersuite)); |
| 8592 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8593 | } |
| 8594 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 8595 | ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8596 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8597 | encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8598 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8599 | ciphersuite_info); |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8600 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8601 | if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) { |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8602 | transform->taglen = |
| 8603 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8604 | } |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8605 | |
| 8606 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Dave Rodgman | 2eab462 | 2023-10-05 13:30:37 +0100 | [diff] [blame] | 8607 | 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] | 8608 | transform->taglen, |
| 8609 | &alg, |
| 8610 | &key_type, |
| 8611 | &key_bits)) != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 8612 | ret = PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8613 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8614 | goto end; |
| 8615 | } |
| 8616 | #else |
Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 8617 | 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] | 8618 | if (cipher_info == NULL) { |
| 8619 | MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found", |
| 8620 | ciphersuite_info->cipher)); |
| 8621 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8622 | } |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8623 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8624 | |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8625 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Dave Rodgman | 2eab462 | 2023-10-05 13:30:37 +0100 | [diff] [blame] | 8626 | 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] | 8627 | if (mac_alg == 0) { |
Manuel Pégourié-Gonnard | 2d6d993 | 2023-03-28 11:38:08 +0200 | [diff] [blame] | 8628 | 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] | 8629 | (unsigned) ciphersuite_info->mac)); |
| 8630 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8631 | } |
| 8632 | #else |
Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 8633 | 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] | 8634 | if (md_info == NULL) { |
| 8635 | MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found", |
| 8636 | (unsigned) ciphersuite_info->mac)); |
| 8637 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8638 | } |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8639 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8640 | |
| 8641 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 8642 | /* Copy own and peer's CID if the use of the CID |
| 8643 | * extension has been negotiated. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8644 | if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) { |
| 8645 | MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform")); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8646 | |
| 8647 | transform->in_cid_len = ssl->own_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8648 | memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len); |
| 8649 | MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid, |
| 8650 | transform->in_cid_len); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8651 | |
| 8652 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8653 | memcpy(transform->out_cid, ssl->handshake->peer_cid, |
| 8654 | ssl->handshake->peer_cid_len); |
| 8655 | MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid, |
| 8656 | transform->out_cid_len); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8657 | } |
| 8658 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 8659 | |
| 8660 | /* |
| 8661 | * Compute key block using the PRF |
| 8662 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8663 | ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256); |
| 8664 | if (ret != 0) { |
| 8665 | MBEDTLS_SSL_DEBUG_RET(1, "prf", ret); |
| 8666 | return ret; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8667 | } |
| 8668 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8669 | MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s", |
| 8670 | mbedtls_ssl_get_ciphersuite_name(ciphersuite))); |
| 8671 | MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48); |
| 8672 | MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64); |
| 8673 | MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8674 | |
| 8675 | /* |
| 8676 | * Determine the appropriate key, IV and MAC length. |
| 8677 | */ |
| 8678 | |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8679 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8680 | keylen = PSA_BITS_TO_BYTES(key_bits); |
| 8681 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8682 | keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8683 | #endif |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8684 | |
Valerio Setti | e570704 | 2023-10-11 11:54:42 +0200 | [diff] [blame] | 8685 | #if defined(MBEDTLS_SSL_HAVE_AEAD) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8686 | if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8687 | size_t explicit_ivlen; |
| 8688 | |
| 8689 | transform->maclen = 0; |
| 8690 | mac_key_len = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8691 | |
| 8692 | /* All modes haves 96-bit IVs, but the length of the static parts vary |
| 8693 | * with mode and version: |
| 8694 | * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes |
| 8695 | * (to be concatenated with a dynamically chosen IV of 8 Bytes) |
| 8696 | * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's |
| 8697 | * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record |
| 8698 | * sequence number). |
| 8699 | */ |
| 8700 | transform->ivlen = 12; |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 8701 | |
| 8702 | int is_chachapoly = 0; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8703 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8704 | is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8705 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8706 | is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info) |
| 8707 | == MBEDTLS_MODE_CHACHAPOLY); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8708 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 8709 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8710 | if (is_chachapoly) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8711 | transform->fixed_ivlen = 12; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8712 | } else { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8713 | transform->fixed_ivlen = 4; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8714 | } |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8715 | |
| 8716 | /* Minimum length of encrypted record */ |
| 8717 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
| 8718 | transform->minlen = explicit_ivlen + transform->taglen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8719 | } else |
Valerio Setti | e570704 | 2023-10-11 11:54:42 +0200 | [diff] [blame] | 8720 | #endif /* MBEDTLS_SSL_HAVE_AEAD */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8721 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8722 | if (ssl_mode == MBEDTLS_SSL_MODE_STREAM || |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8723 | ssl_mode == MBEDTLS_SSL_MODE_CBC || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8724 | ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) { |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8725 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8726 | size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8727 | #else |
Dave Rodgman | 85a8813 | 2023-06-24 11:41:50 +0100 | [diff] [blame] | 8728 | size_t block_size = mbedtls_cipher_info_get_block_size(cipher_info); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8729 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8730 | |
| 8731 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8732 | /* Get MAC length */ |
| 8733 | mac_key_len = PSA_HASH_LENGTH(mac_alg); |
| 8734 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8735 | /* Initialize HMAC contexts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8736 | if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 || |
| 8737 | (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) { |
| 8738 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8739 | goto end; |
| 8740 | } |
| 8741 | |
| 8742 | /* Get MAC length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8743 | mac_key_len = mbedtls_md_get_size(md_info); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8744 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8745 | transform->maclen = mac_key_len; |
| 8746 | |
| 8747 | /* IV length */ |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8748 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8749 | transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8750 | #else |
Dave Rodgman | bb521fd | 2023-06-24 11:21:25 +0100 | [diff] [blame] | 8751 | transform->ivlen = mbedtls_cipher_info_get_iv_size(cipher_info); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8752 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8753 | |
| 8754 | /* Minimum length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8755 | if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8756 | transform->minlen = transform->maclen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8757 | } else { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8758 | /* |
| 8759 | * GenericBlockCipher: |
| 8760 | * 1. if EtM is in use: one block plus MAC |
| 8761 | * otherwise: * first multiple of blocklen greater than maclen |
| 8762 | * 2. IV |
| 8763 | */ |
| 8764 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8765 | if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8766 | transform->minlen = transform->maclen |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8767 | + block_size; |
| 8768 | } else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8769 | #endif |
| 8770 | { |
| 8771 | transform->minlen = transform->maclen |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8772 | + block_size |
| 8773 | - transform->maclen % block_size; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8774 | } |
| 8775 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8776 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8777 | transform->minlen += transform->ivlen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8778 | } else { |
| 8779 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8780 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 8781 | goto end; |
| 8782 | } |
| 8783 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8784 | } else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8785 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 8786 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8787 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 8788 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8789 | } |
| 8790 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8791 | MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 8792 | (unsigned) keylen, |
| 8793 | (unsigned) transform->minlen, |
| 8794 | (unsigned) transform->ivlen, |
| 8795 | (unsigned) transform->maclen)); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8796 | |
| 8797 | /* |
| 8798 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 8799 | */ |
| 8800 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8801 | if (endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8802 | key1 = keyblk + mac_key_len * 2; |
| 8803 | key2 = keyblk + mac_key_len * 2 + keylen; |
| 8804 | |
| 8805 | mac_enc = keyblk; |
| 8806 | mac_dec = keyblk + mac_key_len; |
| 8807 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8808 | iv_copy_len = (transform->fixed_ivlen) ? |
| 8809 | transform->fixed_ivlen : transform->ivlen; |
| 8810 | memcpy(transform->iv_enc, key2 + keylen, iv_copy_len); |
| 8811 | memcpy(transform->iv_dec, key2 + keylen + iv_copy_len, |
| 8812 | iv_copy_len); |
| 8813 | } else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8814 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 8815 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8816 | if (endpoint == MBEDTLS_SSL_IS_SERVER) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8817 | key1 = keyblk + mac_key_len * 2 + keylen; |
| 8818 | key2 = keyblk + mac_key_len * 2; |
| 8819 | |
| 8820 | mac_enc = keyblk + mac_key_len; |
| 8821 | mac_dec = keyblk; |
| 8822 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8823 | iv_copy_len = (transform->fixed_ivlen) ? |
| 8824 | transform->fixed_ivlen : transform->ivlen; |
| 8825 | memcpy(transform->iv_dec, key1 + keylen, iv_copy_len); |
| 8826 | memcpy(transform->iv_enc, key1 + keylen + iv_copy_len, |
| 8827 | iv_copy_len); |
| 8828 | } else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8829 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 8830 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8831 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8832 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 8833 | goto end; |
| 8834 | } |
| 8835 | |
Paul Elliott | 4fb1955 | 2023-10-18 12:15:30 +0100 | [diff] [blame] | 8836 | if (ssl->f_export_keys != NULL) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8837 | ssl->f_export_keys(ssl->p_export_keys, |
| 8838 | MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET, |
| 8839 | master, 48, |
| 8840 | randbytes + 32, |
| 8841 | randbytes, |
| 8842 | tls_prf_get_type(tls_prf)); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8843 | } |
| 8844 | |
| 8845 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8846 | transform->psa_alg = alg; |
| 8847 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8848 | if (alg != MBEDTLS_SSL_NULL_CIPHER) { |
| 8849 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 8850 | psa_set_key_algorithm(&attributes, alg); |
| 8851 | psa_set_key_type(&attributes, key_type); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8852 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8853 | if ((status = psa_import_key(&attributes, |
| 8854 | key1, |
| 8855 | PSA_BITS_TO_BYTES(key_bits), |
| 8856 | &transform->psa_key_enc)) != PSA_SUCCESS) { |
| 8857 | MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status); |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 8858 | ret = PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8859 | MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8860 | goto end; |
| 8861 | } |
| 8862 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8863 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8864 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8865 | if ((status = psa_import_key(&attributes, |
| 8866 | key2, |
| 8867 | PSA_BITS_TO_BYTES(key_bits), |
| 8868 | &transform->psa_key_dec)) != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 8869 | ret = PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8870 | MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8871 | goto end; |
| 8872 | } |
| 8873 | } |
| 8874 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8875 | if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc, |
| 8876 | cipher_info)) != 0) { |
| 8877 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8878 | goto end; |
| 8879 | } |
| 8880 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8881 | if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec, |
| 8882 | cipher_info)) != 0) { |
| 8883 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8884 | goto end; |
| 8885 | } |
| 8886 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8887 | if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1, |
| 8888 | (int) mbedtls_cipher_info_get_key_bitlen(cipher_info), |
| 8889 | MBEDTLS_ENCRYPT)) != 0) { |
| 8890 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8891 | goto end; |
| 8892 | } |
| 8893 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8894 | if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2, |
| 8895 | (int) mbedtls_cipher_info_get_key_bitlen(cipher_info), |
| 8896 | MBEDTLS_DECRYPT)) != 0) { |
| 8897 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8898 | goto end; |
| 8899 | } |
| 8900 | |
| 8901 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8902 | if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) { |
| 8903 | if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc, |
| 8904 | MBEDTLS_PADDING_NONE)) != 0) { |
| 8905 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8906 | goto end; |
| 8907 | } |
| 8908 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8909 | if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec, |
| 8910 | MBEDTLS_PADDING_NONE)) != 0) { |
| 8911 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8912 | goto end; |
| 8913 | } |
| 8914 | } |
| 8915 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 8916 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8917 | |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8918 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 8919 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 8920 | For AEAD-based ciphersuites, there is nothing to do here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8921 | if (mac_key_len != 0) { |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8922 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8923 | transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8924 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8925 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 8926 | psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg)); |
| 8927 | psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8928 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8929 | if ((status = psa_import_key(&attributes, |
| 8930 | mac_enc, mac_key_len, |
| 8931 | &transform->psa_mac_enc)) != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 8932 | ret = PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8933 | MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8934 | goto end; |
| 8935 | } |
| 8936 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8937 | if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) || |
| 8938 | ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING) |
Andrzej Kurek | 8c95ac4 | 2022-08-17 16:17:00 -0400 | [diff] [blame] | 8939 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8940 | && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED) |
Andrzej Kurek | 8c95ac4 | 2022-08-17 16:17:00 -0400 | [diff] [blame] | 8941 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8942 | )) { |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8943 | /* mbedtls_ct_hmac() requires the key to be exportable */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8944 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT | |
| 8945 | PSA_KEY_USAGE_VERIFY_HASH); |
| 8946 | } else { |
| 8947 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 8948 | } |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8949 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8950 | if ((status = psa_import_key(&attributes, |
| 8951 | mac_dec, mac_key_len, |
| 8952 | &transform->psa_mac_dec)) != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 8953 | ret = PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8954 | MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8955 | goto end; |
| 8956 | } |
| 8957 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8958 | ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len); |
| 8959 | if (ret != 0) { |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8960 | goto end; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8961 | } |
| 8962 | ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len); |
| 8963 | if (ret != 0) { |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8964 | goto end; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8965 | } |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8966 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8967 | } |
| 8968 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 8969 | |
| 8970 | ((void) mac_dec); |
| 8971 | ((void) mac_enc); |
| 8972 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8973 | end: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8974 | mbedtls_platform_zeroize(keyblk, sizeof(keyblk)); |
| 8975 | return ret; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8976 | } |
| 8977 | |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8978 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \ |
| 8979 | defined(MBEDTLS_USE_PSA_CRYPTO) |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8980 | int mbedtls_psa_ecjpake_read_round( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8981 | psa_pake_operation_t *pake_ctx, |
| 8982 | const unsigned char *buf, |
| 8983 | size_t len, mbedtls_ecjpake_rounds_t round) |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8984 | { |
| 8985 | psa_status_t status; |
| 8986 | size_t input_offset = 0; |
Valerio Setti | 819de86 | 2022-11-17 18:05:19 +0100 | [diff] [blame] | 8987 | /* |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8988 | * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice |
| 8989 | * At round two perform a single cycle |
| 8990 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8991 | unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8992 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8993 | for (; remaining_steps > 0; remaining_steps--) { |
| 8994 | for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8995 | step <= PSA_PAKE_STEP_ZK_PROOF; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8996 | ++step) { |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8997 | /* Length is stored at the first byte */ |
| 8998 | size_t length = buf[input_offset]; |
| 8999 | input_offset += 1; |
| 9000 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9001 | if (input_offset + length > len) { |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 9002 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
| 9003 | } |
| 9004 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9005 | status = psa_pake_input(pake_ctx, step, |
| 9006 | buf + input_offset, length); |
| 9007 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 9008 | return PSA_TO_MBEDTLS_ERR(status); |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 9009 | } |
| 9010 | |
| 9011 | input_offset += length; |
| 9012 | } |
| 9013 | } |
| 9014 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9015 | if (input_offset != len) { |
Valerio Setti | 61ea17d | 2022-11-18 12:11:00 +0100 | [diff] [blame] | 9016 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9017 | } |
Valerio Setti | 30ebe11 | 2022-11-17 16:23:34 +0100 | [diff] [blame] | 9018 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9019 | return 0; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 9020 | } |
| 9021 | |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 9022 | int mbedtls_psa_ecjpake_write_round( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9023 | psa_pake_operation_t *pake_ctx, |
| 9024 | unsigned char *buf, |
| 9025 | size_t len, size_t *olen, |
| 9026 | mbedtls_ecjpake_rounds_t round) |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 9027 | { |
| 9028 | psa_status_t status; |
| 9029 | size_t output_offset = 0; |
| 9030 | size_t output_len; |
Valerio Setti | 819de86 | 2022-11-17 18:05:19 +0100 | [diff] [blame] | 9031 | /* |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 9032 | * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice |
| 9033 | * At round two perform a single cycle |
| 9034 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9035 | unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 9036 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9037 | for (; remaining_steps > 0; remaining_steps--) { |
| 9038 | for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE; |
| 9039 | step <= PSA_PAKE_STEP_ZK_PROOF; |
| 9040 | ++step) { |
Valerio Setti | 79f6b6b | 2022-11-21 14:17:03 +0100 | [diff] [blame] | 9041 | /* |
Valerio Setti | d4a9b1a | 2022-11-22 11:11:10 +0100 | [diff] [blame] | 9042 | * For each step, prepend 1 byte with the length of the data as |
| 9043 | * given by psa_pake_output(). |
Valerio Setti | 79f6b6b | 2022-11-21 14:17:03 +0100 | [diff] [blame] | 9044 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9045 | status = psa_pake_output(pake_ctx, step, |
| 9046 | buf + output_offset + 1, |
| 9047 | len - output_offset - 1, |
| 9048 | &output_len); |
| 9049 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 9050 | return PSA_TO_MBEDTLS_ERR(status); |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 9051 | } |
| 9052 | |
Valerio Setti | 99d88c1 | 2022-11-22 16:03:43 +0100 | [diff] [blame] | 9053 | *(buf + output_offset) = (uint8_t) output_len; |
Valerio Setti | 79f6b6b | 2022-11-21 14:17:03 +0100 | [diff] [blame] | 9054 | |
| 9055 | output_offset += output_len + 1; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 9056 | } |
| 9057 | } |
| 9058 | |
| 9059 | *olen = output_offset; |
| 9060 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9061 | return 0; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 9062 | } |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 9063 | #endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO |
| 9064 | |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9065 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9066 | int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl, |
| 9067 | unsigned char *hash, size_t *hashlen, |
| 9068 | unsigned char *data, size_t data_len, |
| 9069 | mbedtls_md_type_t md_alg) |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9070 | { |
| 9071 | psa_status_t status; |
| 9072 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; |
Manuel Pégourié-Gonnard | 2d6d993 | 2023-03-28 11:38:08 +0200 | [diff] [blame] | 9073 | 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] | 9074 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9075 | 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] | 9076 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9077 | if ((status = psa_hash_setup(&hash_operation, |
| 9078 | hash_alg)) != PSA_SUCCESS) { |
| 9079 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9080 | goto exit; |
| 9081 | } |
| 9082 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9083 | if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes, |
| 9084 | 64)) != PSA_SUCCESS) { |
| 9085 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9086 | goto exit; |
| 9087 | } |
| 9088 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9089 | if ((status = psa_hash_update(&hash_operation, |
| 9090 | data, data_len)) != PSA_SUCCESS) { |
| 9091 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9092 | goto exit; |
| 9093 | } |
| 9094 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9095 | if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE, |
| 9096 | hashlen)) != PSA_SUCCESS) { |
| 9097 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status); |
| 9098 | goto exit; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9099 | } |
| 9100 | |
| 9101 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9102 | if (status != PSA_SUCCESS) { |
| 9103 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 9104 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
| 9105 | switch (status) { |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9106 | case PSA_ERROR_NOT_SUPPORTED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9107 | return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9108 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ |
| 9109 | case PSA_ERROR_BUFFER_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9110 | return MBEDTLS_ERR_MD_BAD_INPUT_DATA; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9111 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9112 | return MBEDTLS_ERR_MD_ALLOC_FAILED; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9113 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9114 | return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9115 | } |
| 9116 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9117 | return 0; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9118 | } |
| 9119 | |
| 9120 | #else |
| 9121 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9122 | int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl, |
| 9123 | unsigned char *hash, size_t *hashlen, |
| 9124 | unsigned char *data, size_t data_len, |
| 9125 | mbedtls_md_type_t md_alg) |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9126 | { |
| 9127 | int ret = 0; |
| 9128 | mbedtls_md_context_t ctx; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9129 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg); |
| 9130 | *hashlen = mbedtls_md_get_size(md_info); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9131 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9132 | 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] | 9133 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9134 | mbedtls_md_init(&ctx); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9135 | |
| 9136 | /* |
| 9137 | * digitally-signed struct { |
| 9138 | * opaque client_random[32]; |
| 9139 | * opaque server_random[32]; |
| 9140 | * ServerDHParams params; |
| 9141 | * }; |
| 9142 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9143 | if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) { |
| 9144 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9145 | goto exit; |
| 9146 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9147 | if ((ret = mbedtls_md_starts(&ctx)) != 0) { |
| 9148 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9149 | goto exit; |
| 9150 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9151 | if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) { |
| 9152 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9153 | goto exit; |
| 9154 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9155 | if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) { |
| 9156 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9157 | goto exit; |
| 9158 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9159 | if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) { |
| 9160 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9161 | goto exit; |
| 9162 | } |
| 9163 | |
| 9164 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9165 | mbedtls_md_free(&ctx); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9166 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9167 | if (ret != 0) { |
| 9168 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 9169 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
| 9170 | } |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9172 | return ret; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9173 | } |
| 9174 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 9175 | |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 9176 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 9177 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 9178 | /* Find the preferred hash for a given signature algorithm. */ |
| 9179 | unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9180 | mbedtls_ssl_context *ssl, |
| 9181 | unsigned int sig_alg) |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 9182 | { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 9183 | unsigned int i; |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 9184 | uint16_t *received_sig_algs = ssl->handshake->received_sig_algs; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 9185 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9186 | if (sig_alg == MBEDTLS_SSL_SIG_ANON) { |
| 9187 | return MBEDTLS_SSL_HASH_NONE; |
| 9188 | } |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 9189 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9190 | for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) { |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 9191 | unsigned int hash_alg_received = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9192 | MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG( |
| 9193 | received_sig_algs[i]); |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 9194 | unsigned int sig_alg_received = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9195 | MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG( |
| 9196 | received_sig_algs[i]); |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 9197 | |
Manuel Pégourié-Gonnard | 47bb380 | 2023-06-05 12:40:32 +0200 | [diff] [blame] | 9198 | mbedtls_md_type_t md_alg = |
| 9199 | mbedtls_ssl_md_alg_from_hash((unsigned char) hash_alg_received); |
| 9200 | if (md_alg == MBEDTLS_MD_NONE) { |
| 9201 | continue; |
| 9202 | } |
| 9203 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9204 | if (sig_alg == sig_alg_received) { |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 9205 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9206 | if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) { |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 9207 | psa_algorithm_t psa_hash_alg = |
Manuel Pégourié-Gonnard | 47bb380 | 2023-06-05 12:40:32 +0200 | [diff] [blame] | 9208 | mbedtls_md_psa_alg_from_type(md_alg); |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 9209 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9210 | if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA && |
| 9211 | !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key, |
| 9212 | PSA_ALG_ECDSA(psa_hash_alg), |
| 9213 | PSA_KEY_USAGE_SIGN_HASH)) { |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 9214 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9215 | } |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 9216 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9217 | if (sig_alg_received == MBEDTLS_SSL_SIG_RSA && |
| 9218 | !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key, |
| 9219 | PSA_ALG_RSA_PKCS1V15_SIGN( |
| 9220 | psa_hash_alg), |
| 9221 | PSA_KEY_USAGE_SIGN_HASH)) { |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 9222 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9223 | } |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 9224 | } |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 9225 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 9226 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9227 | return hash_alg_received; |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 9228 | } |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 9229 | } |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 9230 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9231 | return MBEDTLS_SSL_HASH_NONE; |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 9232 | } |
| 9233 | |
| 9234 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 9235 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 9236 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 9237 | |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9238 | int mbedtls_ssl_validate_ciphersuite( |
| 9239 | const mbedtls_ssl_context *ssl, |
| 9240 | const mbedtls_ssl_ciphersuite_t *suite_info, |
| 9241 | mbedtls_ssl_protocol_version min_tls_version, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9242 | mbedtls_ssl_protocol_version max_tls_version) |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9243 | { |
| 9244 | (void) ssl; |
| 9245 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9246 | if (suite_info == NULL) { |
| 9247 | return -1; |
| 9248 | } |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9249 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9250 | if ((suite_info->min_tls_version > max_tls_version) || |
| 9251 | (suite_info->max_tls_version < min_tls_version)) { |
| 9252 | return -1; |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9253 | } |
| 9254 | |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 9255 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C) |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9256 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 9257 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9258 | if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
| 9259 | ssl->handshake->psa_pake_ctx_is_ok != 1) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 9260 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9261 | if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
| 9262 | mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 9263 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9264 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9265 | return -1; |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9266 | } |
| 9267 | #endif |
| 9268 | |
| 9269 | /* Don't suggest PSK-based ciphersuite if no PSK is available. */ |
| 9270 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9271 | if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) && |
| 9272 | mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) { |
| 9273 | return -1; |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9274 | } |
| 9275 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
| 9276 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 9277 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9278 | return 0; |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9279 | } |
| 9280 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 9281 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9282 | /* |
| 9283 | * Function for writing a signature algorithm extension. |
| 9284 | * |
| 9285 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 9286 | * value (TLS 1.3 RFC8446): |
| 9287 | * enum { |
| 9288 | * .... |
| 9289 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 9290 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 9291 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 9292 | * .... |
| 9293 | * } SignatureScheme; |
| 9294 | * |
| 9295 | * struct { |
| 9296 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 9297 | * } SignatureSchemeList; |
| 9298 | * |
| 9299 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 9300 | * value (TLS 1.2 RFC5246): |
| 9301 | * enum { |
| 9302 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 9303 | * sha512(6), (255) |
| 9304 | * } HashAlgorithm; |
| 9305 | * |
| 9306 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 9307 | * SignatureAlgorithm; |
| 9308 | * |
| 9309 | * struct { |
| 9310 | * HashAlgorithm hash; |
| 9311 | * SignatureAlgorithm signature; |
| 9312 | * } SignatureAndHashAlgorithm; |
| 9313 | * |
| 9314 | * SignatureAndHashAlgorithm |
| 9315 | * supported_signature_algorithms<2..2^16-2>; |
| 9316 | * |
| 9317 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 9318 | * generalization of the TLS 1.2 signature algorithm extension. |
| 9319 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 9320 | * `SignatureScheme` field of TLS 1.3 |
| 9321 | * |
| 9322 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9323 | int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf, |
| 9324 | const unsigned char *end, size_t *out_len) |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9325 | { |
| 9326 | unsigned char *p = buf; |
| 9327 | unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */ |
| 9328 | size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */ |
| 9329 | |
| 9330 | *out_len = 0; |
| 9331 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9332 | MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension")); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9333 | |
| 9334 | /* Check if we have space for header and length field: |
| 9335 | * - extension_type (2 bytes) |
| 9336 | * - extension_data_length (2 bytes) |
| 9337 | * - supported_signature_algorithms_length (2 bytes) |
| 9338 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9339 | MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9340 | p += 6; |
| 9341 | |
| 9342 | /* |
| 9343 | * Write supported_signature_algorithms |
| 9344 | */ |
| 9345 | supported_sig_alg = p; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9346 | const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl); |
| 9347 | if (sig_alg == NULL) { |
| 9348 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
| 9349 | } |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9350 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9351 | for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) { |
| 9352 | MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s", |
| 9353 | *sig_alg, |
| 9354 | mbedtls_ssl_sig_alg_to_str(*sig_alg))); |
| 9355 | if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) { |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9356 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9357 | } |
| 9358 | MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2); |
| 9359 | MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9360 | p += 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9361 | MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s", |
| 9362 | *sig_alg, |
| 9363 | mbedtls_ssl_sig_alg_to_str(*sig_alg))); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9364 | } |
| 9365 | |
| 9366 | /* Length of supported_signature_algorithms */ |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 9367 | supported_sig_alg_len = (size_t) (p - supported_sig_alg); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9368 | if (supported_sig_alg_len == 0) { |
| 9369 | MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined.")); |
| 9370 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9371 | } |
| 9372 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9373 | MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0); |
| 9374 | MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2); |
| 9375 | MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9376 | |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 9377 | *out_len = (size_t) (p - buf); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9378 | |
| 9379 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9380 | 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] | 9381 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | 0c354a2 | 2022-08-29 15:25:36 +0800 | [diff] [blame] | 9382 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9383 | return 0; |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9384 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 9385 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9386 | |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9387 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9388 | /* |
| 9389 | * mbedtls_ssl_parse_server_name_ext |
| 9390 | * |
| 9391 | * Structure of server_name extension: |
| 9392 | * |
| 9393 | * enum { |
| 9394 | * host_name(0), (255) |
| 9395 | * } NameType; |
| 9396 | * opaque HostName<1..2^16-1>; |
| 9397 | * |
| 9398 | * struct { |
| 9399 | * NameType name_type; |
| 9400 | * select (name_type) { |
| 9401 | * case host_name: HostName; |
| 9402 | * } name; |
| 9403 | * } ServerName; |
| 9404 | * struct { |
| 9405 | * ServerName server_name_list<1..2^16-1> |
| 9406 | * } ServerNameList; |
| 9407 | */ |
Ronald Cron | ce7d76e | 2022-07-08 18:56:49 +0200 | [diff] [blame] | 9408 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9409 | int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl, |
| 9410 | const unsigned char *buf, |
| 9411 | const unsigned char *end) |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9412 | { |
| 9413 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 9414 | const unsigned char *p = buf; |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9415 | size_t server_name_list_len, hostname_len; |
| 9416 | const unsigned char *server_name_list_end; |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9417 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9418 | MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension")); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9419 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9420 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2); |
| 9421 | server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9422 | p += 2; |
| 9423 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9424 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len); |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9425 | server_name_list_end = p + server_name_list_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9426 | while (p < server_name_list_end) { |
| 9427 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3); |
| 9428 | hostname_len = MBEDTLS_GET_UINT16_BE(p, 1); |
| 9429 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, |
| 9430 | hostname_len + 3); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9431 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9432 | if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) { |
XiaokangQian | 75fe8c7 | 2022-06-15 09:42:45 +0000 | [diff] [blame] | 9433 | /* sni_name is intended to be used only during the parsing of the |
| 9434 | * ClientHello message (it is reset to NULL before the end of |
| 9435 | * the message parsing). Thus it is ok to just point to the |
| 9436 | * reception buffer and not make a copy of it. |
| 9437 | */ |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 9438 | ssl->handshake->sni_name = p + 3; |
| 9439 | ssl->handshake->sni_name_len = hostname_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9440 | if (ssl->conf->f_sni == NULL) { |
| 9441 | return 0; |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9442 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9443 | ret = ssl->conf->f_sni(ssl->conf->p_sni, |
| 9444 | ssl, p + 3, hostname_len); |
| 9445 | if (ret != 0) { |
| 9446 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret); |
| 9447 | MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME, |
| 9448 | MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME); |
| 9449 | return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME; |
| 9450 | } |
| 9451 | return 0; |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9452 | } |
| 9453 | |
| 9454 | p += hostname_len + 3; |
| 9455 | } |
| 9456 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9457 | return 0; |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9458 | } |
| 9459 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 9460 | |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9461 | #if defined(MBEDTLS_SSL_ALPN) |
Ronald Cron | ce7d76e | 2022-07-08 18:56:49 +0200 | [diff] [blame] | 9462 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9463 | int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl, |
| 9464 | const unsigned char *buf, |
| 9465 | const unsigned char *end) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9466 | { |
| 9467 | const unsigned char *p = buf; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9468 | size_t protocol_name_list_len; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9469 | const unsigned char *protocol_name_list; |
| 9470 | const unsigned char *protocol_name_list_end; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9471 | size_t protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9472 | |
| 9473 | /* If ALPN not configured, just ignore the extension */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9474 | if (ssl->conf->alpn_list == NULL) { |
| 9475 | return 0; |
| 9476 | } |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9477 | |
| 9478 | /* |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9479 | * RFC7301, section 3.1 |
| 9480 | * opaque ProtocolName<1..2^8-1>; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9481 | * |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9482 | * struct { |
| 9483 | * ProtocolName protocol_name_list<2..2^16-1> |
| 9484 | * } ProtocolNameList; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9485 | */ |
| 9486 | |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9487 | /* |
XiaokangQian | 0b776e2 | 2022-06-24 09:04:59 +0000 | [diff] [blame] | 9488 | * protocol_name_list_len 2 bytes |
| 9489 | * protocol_name_len 1 bytes |
| 9490 | * protocol_name >=1 byte |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9491 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9492 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9493 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9494 | protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9495 | p += 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9496 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9497 | protocol_name_list = p; |
| 9498 | protocol_name_list_end = p + protocol_name_list_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9499 | |
| 9500 | /* Validate peer's list (lengths) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9501 | while (p < protocol_name_list_end) { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9502 | protocol_name_len = *p++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9503 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end, |
| 9504 | protocol_name_len); |
| 9505 | if (protocol_name_len == 0) { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9506 | MBEDTLS_SSL_PEND_FATAL_ALERT( |
| 9507 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9508 | MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER); |
| 9509 | return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9510 | } |
| 9511 | |
| 9512 | p += protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9513 | } |
| 9514 | |
| 9515 | /* Use our order of preference */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9516 | for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) { |
| 9517 | size_t const alpn_len = strlen(*alpn); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9518 | p = protocol_name_list; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9519 | while (p < protocol_name_list_end) { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9520 | protocol_name_len = *p++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9521 | if (protocol_name_len == alpn_len && |
| 9522 | memcmp(p, *alpn, alpn_len) == 0) { |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9523 | ssl->alpn_chosen = *alpn; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9524 | return 0; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9525 | } |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9526 | |
| 9527 | p += protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9528 | } |
| 9529 | } |
| 9530 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9531 | /* If we get here, no match was found */ |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9532 | MBEDTLS_SSL_PEND_FATAL_ALERT( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9533 | MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL, |
| 9534 | MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL); |
| 9535 | return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9536 | } |
| 9537 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9538 | int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl, |
| 9539 | unsigned char *buf, |
| 9540 | unsigned char *end, |
| 9541 | size_t *out_len) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9542 | { |
| 9543 | unsigned char *p = buf; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9544 | size_t protocol_name_len; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9545 | *out_len = 0; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9546 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9547 | if (ssl->alpn_chosen == NULL) { |
| 9548 | return 0; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9549 | } |
| 9550 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9551 | protocol_name_len = strlen(ssl->alpn_chosen); |
| 9552 | MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9553 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9554 | MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension")); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9555 | /* |
| 9556 | * 0 . 1 ext identifier |
| 9557 | * 2 . 3 ext length |
| 9558 | * 4 . 5 protocol list length |
| 9559 | * 6 . 6 protocol name length |
| 9560 | * 7 . 7+n protocol name |
| 9561 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9562 | MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9563 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9564 | *out_len = 7 + protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9565 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9566 | MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2); |
| 9567 | MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4); |
XiaokangQian | 0b776e2 | 2022-06-24 09:04:59 +0000 | [diff] [blame] | 9568 | /* Note: the length of the chosen protocol has been checked to be less |
| 9569 | * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`. |
| 9570 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9571 | p[6] = MBEDTLS_BYTE_0(protocol_name_len); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9572 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9573 | memcpy(p + 7, ssl->alpn_chosen, protocol_name_len); |
Jerry Yu | b95dd36 | 2022-11-08 21:19:34 +0800 | [diff] [blame] | 9574 | |
| 9575 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9576 | 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] | 9577 | #endif |
| 9578 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9579 | return 0; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9580 | } |
| 9581 | #endif /* MBEDTLS_SSL_ALPN */ |
| 9582 | |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9583 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
Xiaokang Qian | 0340929 | 2022-10-12 02:49:52 +0000 | [diff] [blame] | 9584 | defined(MBEDTLS_SSL_SESSION_TICKETS) && \ |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 9585 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \ |
Xiaokang Qian | ed3afcd | 2022-10-12 08:31:11 +0000 | [diff] [blame] | 9586 | defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9587 | int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session, |
| 9588 | const char *hostname) |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9589 | { |
| 9590 | /* Initialize to suppress unnecessary compiler warning */ |
| 9591 | size_t hostname_len = 0; |
| 9592 | |
| 9593 | /* Check if new hostname is valid before |
| 9594 | * making any change to current one */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9595 | if (hostname != NULL) { |
| 9596 | hostname_len = strlen(hostname); |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9597 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9598 | if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) { |
| 9599 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9600 | } |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9601 | } |
| 9602 | |
| 9603 | /* Now it's clear that we will overwrite the old hostname, |
| 9604 | * so we can free it safely */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9605 | if (session->hostname != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 9606 | mbedtls_zeroize_and_free(session->hostname, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9607 | strlen(session->hostname)); |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9608 | } |
| 9609 | |
| 9610 | /* Passing NULL as hostname shall clear the old one */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9611 | if (hostname == NULL) { |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9612 | session->hostname = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9613 | } else { |
| 9614 | session->hostname = mbedtls_calloc(1, hostname_len + 1); |
| 9615 | if (session->hostname == NULL) { |
| 9616 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 9617 | } |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9618 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9619 | memcpy(session->hostname, hostname, hostname_len); |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9620 | } |
| 9621 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9622 | return 0; |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9623 | } |
Xiaokang Qian | 0340929 | 2022-10-12 02:49:52 +0000 | [diff] [blame] | 9624 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 && |
| 9625 | MBEDTLS_SSL_SESSION_TICKETS && |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 9626 | MBEDTLS_SSL_SERVER_NAME_INDICATION && |
Xiaokang Qian | ed3afcd | 2022-10-12 08:31:11 +0000 | [diff] [blame] | 9627 | MBEDTLS_SSL_CLI_C */ |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9628 | |
Waleed Elmelegy | 883f77c | 2024-03-06 19:09:41 +0000 | [diff] [blame] | 9629 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_EARLY_DATA) && \ |
| 9630 | defined(MBEDTLS_SSL_ALPN) |
Waleed Elmelegy | 7dfba34 | 2024-03-12 16:22:39 +0000 | [diff] [blame] | 9631 | int mbedtls_ssl_session_set_ticket_alpn(mbedtls_ssl_session *session, |
| 9632 | const char *alpn) |
Waleed Elmelegy | 883f77c | 2024-03-06 19:09:41 +0000 | [diff] [blame] | 9633 | { |
| 9634 | size_t alpn_len = 0; |
| 9635 | |
| 9636 | if (alpn != NULL) { |
| 9637 | alpn_len = strlen(alpn); |
| 9638 | |
| 9639 | if (alpn_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) { |
| 9640 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9641 | } |
| 9642 | } |
| 9643 | |
| 9644 | if (session->ticket_alpn != NULL) { |
| 9645 | mbedtls_zeroize_and_free(session->ticket_alpn, |
| 9646 | strlen(session->ticket_alpn)); |
Waleed Elmelegy | 7dfba34 | 2024-03-12 16:22:39 +0000 | [diff] [blame] | 9647 | session->ticket_alpn = NULL; |
Waleed Elmelegy | 883f77c | 2024-03-06 19:09:41 +0000 | [diff] [blame] | 9648 | } |
| 9649 | |
Waleed Elmelegy | 7dfba34 | 2024-03-12 16:22:39 +0000 | [diff] [blame] | 9650 | if (alpn != NULL) { |
| 9651 | session->ticket_alpn = mbedtls_calloc(alpn_len + 1, 1); |
Waleed Elmelegy | 883f77c | 2024-03-06 19:09:41 +0000 | [diff] [blame] | 9652 | if (session->ticket_alpn == NULL) { |
| 9653 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 9654 | } |
Waleed Elmelegy | 7dfba34 | 2024-03-12 16:22:39 +0000 | [diff] [blame] | 9655 | memcpy(session->ticket_alpn, alpn, alpn_len); |
Waleed Elmelegy | 883f77c | 2024-03-06 19:09:41 +0000 | [diff] [blame] | 9656 | } |
| 9657 | |
| 9658 | return 0; |
| 9659 | } |
| 9660 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 5398e58 | 2024-08-20 12:14:43 +0200 | [diff] [blame] | 9661 | |
| 9662 | /* |
| 9663 | * The following functions are used by 1.2 and 1.3, client and server. |
| 9664 | */ |
| 9665 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
| 9666 | int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert, |
| 9667 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
| 9668 | int recv_endpoint, |
| 9669 | mbedtls_ssl_protocol_version tls_version, |
| 9670 | uint32_t *flags) |
| 9671 | { |
| 9672 | int ret = 0; |
| 9673 | unsigned int usage = 0; |
| 9674 | const char *ext_oid; |
| 9675 | size_t ext_len; |
| 9676 | |
| 9677 | /* |
| 9678 | * keyUsage |
| 9679 | */ |
| 9680 | |
| 9681 | /* Note: don't guard this with MBEDTLS_SSL_CLI_C because the server wants |
| 9682 | * to check what a compliant client will think while choosing which cert |
| 9683 | * to send to the client. */ |
| 9684 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 9685 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2 && |
| 9686 | recv_endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 9687 | /* TLS 1.2 server part of the key exchange */ |
| 9688 | switch (ciphersuite->key_exchange) { |
| 9689 | case MBEDTLS_KEY_EXCHANGE_RSA: |
Manuel Pégourié-Gonnard | 5398e58 | 2024-08-20 12:14:43 +0200 | [diff] [blame] | 9690 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
| 9691 | break; |
| 9692 | |
| 9693 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 9694 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 9695 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 9696 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
| 9697 | break; |
| 9698 | |
| 9699 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 9700 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
| 9701 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
| 9702 | break; |
| 9703 | |
| 9704 | /* Don't use default: we want warnings when adding new values */ |
| 9705 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 9706 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 9707 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 9708 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
| 9709 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
| 9710 | usage = 0; |
| 9711 | } |
| 9712 | } else |
| 9713 | #endif |
| 9714 | { |
| 9715 | /* This is either TLS 1.3 authentication, which always uses signatures, |
| 9716 | * or 1.2 client auth: rsa_sign and mbedtls_ecdsa_sign are the only |
| 9717 | * options we implement, both using signatures. */ |
| 9718 | (void) tls_version; |
| 9719 | (void) ciphersuite; |
| 9720 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
| 9721 | } |
| 9722 | |
| 9723 | if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) { |
| 9724 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
| 9725 | ret = -1; |
| 9726 | } |
| 9727 | |
| 9728 | /* |
| 9729 | * extKeyUsage |
| 9730 | */ |
| 9731 | |
| 9732 | if (recv_endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 9733 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
| 9734 | ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH); |
| 9735 | } else { |
| 9736 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
| 9737 | ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH); |
| 9738 | } |
| 9739 | |
| 9740 | if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) { |
| 9741 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
| 9742 | ret = -1; |
| 9743 | } |
| 9744 | |
| 9745 | return ret; |
| 9746 | } |
| 9747 | |
| 9748 | int mbedtls_ssl_verify_certificate(mbedtls_ssl_context *ssl, |
| 9749 | int authmode, |
| 9750 | mbedtls_x509_crt *chain, |
| 9751 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info, |
| 9752 | void *rs_ctx) |
| 9753 | { |
| 9754 | if (authmode == MBEDTLS_SSL_VERIFY_NONE) { |
| 9755 | return 0; |
| 9756 | } |
| 9757 | |
| 9758 | /* |
| 9759 | * Primary check: use the appropriate X.509 verification function |
| 9760 | */ |
| 9761 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); |
| 9762 | void *p_vrfy; |
| 9763 | if (ssl->f_vrfy != NULL) { |
| 9764 | MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback")); |
| 9765 | f_vrfy = ssl->f_vrfy; |
| 9766 | p_vrfy = ssl->p_vrfy; |
| 9767 | } else { |
| 9768 | MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback")); |
| 9769 | f_vrfy = ssl->conf->f_vrfy; |
| 9770 | p_vrfy = ssl->conf->p_vrfy; |
| 9771 | } |
| 9772 | |
| 9773 | int ret = 0; |
| 9774 | int have_ca_chain_or_callback = 0; |
| 9775 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 9776 | if (ssl->conf->f_ca_cb != NULL) { |
| 9777 | ((void) rs_ctx); |
| 9778 | have_ca_chain_or_callback = 1; |
| 9779 | |
| 9780 | MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification")); |
| 9781 | ret = mbedtls_x509_crt_verify_with_ca_cb( |
| 9782 | chain, |
| 9783 | ssl->conf->f_ca_cb, |
| 9784 | ssl->conf->p_ca_cb, |
| 9785 | ssl->conf->cert_profile, |
| 9786 | ssl->hostname, |
| 9787 | &ssl->session_negotiate->verify_result, |
| 9788 | f_vrfy, p_vrfy); |
| 9789 | } else |
| 9790 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 9791 | { |
| 9792 | mbedtls_x509_crt *ca_chain; |
| 9793 | mbedtls_x509_crl *ca_crl; |
| 9794 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 9795 | if (ssl->handshake->sni_ca_chain != NULL) { |
| 9796 | ca_chain = ssl->handshake->sni_ca_chain; |
| 9797 | ca_crl = ssl->handshake->sni_ca_crl; |
| 9798 | } else |
| 9799 | #endif |
| 9800 | { |
| 9801 | ca_chain = ssl->conf->ca_chain; |
| 9802 | ca_crl = ssl->conf->ca_crl; |
| 9803 | } |
| 9804 | |
| 9805 | if (ca_chain != NULL) { |
| 9806 | have_ca_chain_or_callback = 1; |
| 9807 | } |
| 9808 | |
| 9809 | ret = mbedtls_x509_crt_verify_restartable( |
| 9810 | chain, |
| 9811 | ca_chain, ca_crl, |
| 9812 | ssl->conf->cert_profile, |
| 9813 | ssl->hostname, |
| 9814 | &ssl->session_negotiate->verify_result, |
| 9815 | f_vrfy, p_vrfy, rs_ctx); |
| 9816 | } |
| 9817 | |
| 9818 | if (ret != 0) { |
| 9819 | MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret); |
| 9820 | } |
| 9821 | |
| 9822 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 9823 | if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) { |
| 9824 | return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; |
| 9825 | } |
| 9826 | #endif |
| 9827 | |
| 9828 | /* |
| 9829 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 9830 | */ |
| 9831 | |
| 9832 | /* With TLS 1.2 and ECC certs, check that the curve used by the |
| 9833 | * certificate is on our list of acceptable curves. |
| 9834 | * |
| 9835 | * With TLS 1.3 this is not needed because the curve is part of the |
| 9836 | * signature algorithm (eg ecdsa_secp256r1_sha256) which is checked when |
| 9837 | * we validate the signature made with the key associated to this cert. |
| 9838 | */ |
| 9839 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 9840 | defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) |
| 9841 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 && |
| 9842 | mbedtls_pk_can_do(&chain->pk, MBEDTLS_PK_ECKEY)) { |
| 9843 | if (mbedtls_ssl_check_curve(ssl, mbedtls_pk_get_ec_group_id(&chain->pk)) != 0) { |
| 9844 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)")); |
| 9845 | ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY; |
| 9846 | if (ret == 0) { |
| 9847 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 9848 | } |
| 9849 | } |
| 9850 | } |
| 9851 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */ |
| 9852 | |
| 9853 | /* Check X.509 usage extensions (keyUsage, extKeyUsage) */ |
| 9854 | if (mbedtls_ssl_check_cert_usage(chain, |
| 9855 | ciphersuite_info, |
| 9856 | ssl->conf->endpoint, |
| 9857 | ssl->tls_version, |
| 9858 | &ssl->session_negotiate->verify_result) != 0) { |
| 9859 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)")); |
| 9860 | if (ret == 0) { |
| 9861 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 9862 | } |
| 9863 | } |
| 9864 | |
| 9865 | /* With authmode optional, we want to keep going if the certificate was |
| 9866 | * unacceptable, but still fail on other errors (out of memory etc), |
| 9867 | * including fatal errors from the f_vrfy callback. |
| 9868 | * |
| 9869 | * The only acceptable errors are: |
| 9870 | * - MBEDTLS_ERR_X509_CERT_VERIFY_FAILED: cert rejected by primary check; |
| 9871 | * - MBEDTLS_ERR_SSL_BAD_CERTIFICATE: cert rejected by secondary checks. |
| 9872 | * Anything else is a fatal error. */ |
| 9873 | if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
| 9874 | (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
| 9875 | ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) { |
| 9876 | ret = 0; |
| 9877 | } |
| 9878 | |
| 9879 | /* Return a specific error as this is a user error: inconsistent |
| 9880 | * configuration - can't verify without trust anchors. */ |
| 9881 | if (have_ca_chain_or_callback == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) { |
| 9882 | MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain")); |
| 9883 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
| 9884 | } |
| 9885 | |
| 9886 | if (ret != 0) { |
| 9887 | uint8_t alert; |
| 9888 | |
| 9889 | /* The certificate may have been rejected for several reasons. |
| 9890 | Pick one and send the corresponding alert. Which alert to send |
| 9891 | may be a subject of debate in some cases. */ |
| 9892 | if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) { |
| 9893 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
| 9894 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) { |
| 9895 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 9896 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) { |
| 9897 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 9898 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) { |
| 9899 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 9900 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) { |
| 9901 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 9902 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) { |
| 9903 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 9904 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) { |
| 9905 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
| 9906 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) { |
| 9907 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
| 9908 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) { |
| 9909 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
| 9910 | } else { |
| 9911 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
| 9912 | } |
| 9913 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 9914 | alert); |
| 9915 | } |
| 9916 | |
| 9917 | #if defined(MBEDTLS_DEBUG_C) |
| 9918 | if (ssl->session_negotiate->verify_result != 0) { |
| 9919 | MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x", |
| 9920 | (unsigned int) ssl->session_negotiate->verify_result)); |
| 9921 | } else { |
| 9922 | MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear")); |
| 9923 | } |
| 9924 | #endif /* MBEDTLS_DEBUG_C */ |
| 9925 | |
| 9926 | return ret; |
| 9927 | } |
| 9928 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
| 9929 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9930 | #endif /* MBEDTLS_SSL_TLS_C */ |