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; |
| 346 | size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0; |
| 347 | size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 348 | if (ssl->in_buf != NULL) { |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 349 | written_in = ssl->in_msg - ssl->in_buf; |
| 350 | iv_offset_in = ssl->in_iv - ssl->in_buf; |
| 351 | len_offset_in = ssl->in_len - ssl->in_buf; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 352 | if (downsizing ? |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 353 | 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] | 354 | ssl->in_buf_len < in_buf_new_len) { |
| 355 | if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != 0) { |
| 356 | MBEDTLS_SSL_DEBUG_MSG(1, ("input buffer resizing failed - out of memory")); |
| 357 | } else { |
| 358 | MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET, |
| 359 | in_buf_new_len)); |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 360 | modified = 1; |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 365 | if (ssl->out_buf != NULL) { |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 366 | written_out = ssl->out_msg - ssl->out_buf; |
| 367 | iv_offset_out = ssl->out_iv - ssl->out_buf; |
| 368 | len_offset_out = ssl->out_len - ssl->out_buf; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 369 | if (downsizing ? |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 370 | 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] | 371 | ssl->out_buf_len < out_buf_new_len) { |
| 372 | if (resize_buffer(&ssl->out_buf, out_buf_new_len, &ssl->out_buf_len) != 0) { |
| 373 | MBEDTLS_SSL_DEBUG_MSG(1, ("output buffer resizing failed - out of memory")); |
| 374 | } else { |
| 375 | MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET, |
| 376 | out_buf_new_len)); |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 377 | modified = 1; |
| 378 | } |
| 379 | } |
| 380 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 381 | if (modified) { |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 382 | /* Update pointers here to avoid doing it twice. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 383 | mbedtls_ssl_reset_in_out_pointers(ssl); |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 384 | /* Fields below might not be properly updated with record |
| 385 | * splitting or with CID, so they are manually updated here. */ |
| 386 | ssl->out_msg = ssl->out_buf + written_out; |
| 387 | ssl->out_len = ssl->out_buf + len_offset_out; |
| 388 | ssl->out_iv = ssl->out_buf + iv_offset_out; |
| 389 | |
| 390 | ssl->in_msg = ssl->in_buf + written_in; |
| 391 | ssl->in_len = ssl->in_buf + len_offset_in; |
| 392 | ssl->in_iv = ssl->in_buf + iv_offset_in; |
| 393 | } |
| 394 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 395 | #endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */ |
| 396 | |
Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 397 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 398 | |
| 399 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 400 | typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen, |
| 401 | const char *label, |
| 402 | const unsigned char *random, size_t rlen, |
| 403 | unsigned char *dstbuf, size_t dlen); |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 404 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 405 | static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id); |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 406 | |
| 407 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
| 408 | |
| 409 | /* Type for the TLS PRF */ |
| 410 | typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *, |
| 411 | const unsigned char *, size_t, |
| 412 | unsigned char *, size_t); |
| 413 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 414 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 415 | static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform, |
| 416 | int ciphersuite, |
| 417 | const unsigned char master[48], |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 418 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 419 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 420 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 421 | ssl_tls_prf_t tls_prf, |
| 422 | const unsigned char randbytes[64], |
| 423 | mbedtls_ssl_protocol_version tls_version, |
| 424 | unsigned endpoint, |
| 425 | const mbedtls_ssl_context *ssl); |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 426 | |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 427 | #if defined(PSA_WANT_ALG_SHA_256) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 428 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 429 | static int tls_prf_sha256(const unsigned char *secret, size_t slen, |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 430 | const char *label, |
| 431 | const unsigned char *random, size_t rlen, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 432 | unsigned char *dstbuf, size_t dlen); |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 433 | static int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *); |
| 434 | 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] | 435 | |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 436 | #endif /* PSA_WANT_ALG_SHA_256*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 437 | |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 438 | #if defined(PSA_WANT_ALG_SHA_384) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 439 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 440 | static int tls_prf_sha384(const unsigned char *secret, size_t slen, |
| 441 | const char *label, |
| 442 | const unsigned char *random, size_t rlen, |
| 443 | unsigned char *dstbuf, size_t dlen); |
| 444 | |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 445 | static int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *); |
| 446 | 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] | 447 | #endif /* PSA_WANT_ALG_SHA_384*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 449 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 450 | static int ssl_tls12_session_load(mbedtls_ssl_session *session, |
| 451 | const unsigned char *buf, |
| 452 | size_t len); |
| 453 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 454 | |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 455 | 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] | 456 | |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 457 | #if defined(PSA_WANT_ALG_SHA_256) |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 458 | 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] | 459 | #endif /* PSA_WANT_ALG_SHA_256*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 460 | |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 461 | #if defined(PSA_WANT_ALG_SHA_384) |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 462 | 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] | 463 | #endif /* PSA_WANT_ALG_SHA_384*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 464 | |
| 465 | int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf, |
| 466 | const unsigned char *secret, size_t slen, |
| 467 | const char *label, |
| 468 | const unsigned char *random, size_t rlen, |
| 469 | unsigned char *dstbuf, size_t dlen) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 470 | { |
| 471 | mbedtls_ssl_tls_prf_cb *tls_prf = NULL; |
| 472 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 473 | switch (prf) { |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 474 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 475 | #if defined(PSA_WANT_ALG_SHA_384) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 476 | case MBEDTLS_SSL_TLS_PRF_SHA384: |
| 477 | tls_prf = tls_prf_sha384; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 478 | break; |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 479 | #endif /* PSA_WANT_ALG_SHA_384*/ |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 480 | #if defined(PSA_WANT_ALG_SHA_256) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 481 | case MBEDTLS_SSL_TLS_PRF_SHA256: |
| 482 | tls_prf = tls_prf_sha256; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 483 | break; |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 484 | #endif /* PSA_WANT_ALG_SHA_256*/ |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 485 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 486 | default: |
| 487 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 488 | } |
| 489 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 490 | return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen); |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 491 | } |
| 492 | |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 493 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 494 | static void ssl_clear_peer_cert(mbedtls_ssl_session *session) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 495 | { |
| 496 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 497 | if (session->peer_cert != NULL) { |
| 498 | mbedtls_x509_crt_free(session->peer_cert); |
| 499 | mbedtls_free(session->peer_cert); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 500 | session->peer_cert = NULL; |
| 501 | } |
| 502 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 503 | if (session->peer_cert_digest != NULL) { |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 504 | /* Zeroization is not necessary. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 505 | mbedtls_free(session->peer_cert_digest); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 506 | session->peer_cert_digest = NULL; |
| 507 | session->peer_cert_digest_type = MBEDTLS_MD_NONE; |
| 508 | session->peer_cert_digest_len = 0; |
| 509 | } |
| 510 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 511 | } |
| 512 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 513 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 514 | uint32_t mbedtls_ssl_get_extension_id(unsigned int extension_type) |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 515 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 516 | switch (extension_type) { |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 517 | case MBEDTLS_TLS_EXT_SERVERNAME: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 518 | return MBEDTLS_SSL_EXT_ID_SERVERNAME; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 519 | |
| 520 | case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 521 | return MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 522 | |
| 523 | case MBEDTLS_TLS_EXT_STATUS_REQUEST: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 524 | return MBEDTLS_SSL_EXT_ID_STATUS_REQUEST; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 525 | |
| 526 | case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 527 | return MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 528 | |
| 529 | case MBEDTLS_TLS_EXT_SIG_ALG: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 530 | return MBEDTLS_SSL_EXT_ID_SIG_ALG; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 531 | |
| 532 | case MBEDTLS_TLS_EXT_USE_SRTP: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 533 | return MBEDTLS_SSL_EXT_ID_USE_SRTP; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 534 | |
| 535 | case MBEDTLS_TLS_EXT_HEARTBEAT: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 536 | return MBEDTLS_SSL_EXT_ID_HEARTBEAT; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 537 | |
| 538 | case MBEDTLS_TLS_EXT_ALPN: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 539 | return MBEDTLS_SSL_EXT_ID_ALPN; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 540 | |
| 541 | case MBEDTLS_TLS_EXT_SCT: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 542 | return MBEDTLS_SSL_EXT_ID_SCT; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 543 | |
| 544 | case MBEDTLS_TLS_EXT_CLI_CERT_TYPE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 545 | return MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 546 | |
| 547 | case MBEDTLS_TLS_EXT_SERV_CERT_TYPE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 548 | return MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 549 | |
| 550 | case MBEDTLS_TLS_EXT_PADDING: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 551 | return MBEDTLS_SSL_EXT_ID_PADDING; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 552 | |
| 553 | case MBEDTLS_TLS_EXT_PRE_SHARED_KEY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 554 | return MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 555 | |
| 556 | case MBEDTLS_TLS_EXT_EARLY_DATA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 557 | return MBEDTLS_SSL_EXT_ID_EARLY_DATA; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 558 | |
| 559 | case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 560 | return MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 561 | |
| 562 | case MBEDTLS_TLS_EXT_COOKIE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 563 | return MBEDTLS_SSL_EXT_ID_COOKIE; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 564 | |
| 565 | case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 566 | return MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 567 | |
| 568 | case MBEDTLS_TLS_EXT_CERT_AUTH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 569 | return MBEDTLS_SSL_EXT_ID_CERT_AUTH; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 570 | |
| 571 | case MBEDTLS_TLS_EXT_OID_FILTERS: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 572 | return MBEDTLS_SSL_EXT_ID_OID_FILTERS; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 573 | |
| 574 | case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 575 | return MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 576 | |
| 577 | case MBEDTLS_TLS_EXT_SIG_ALG_CERT: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 578 | return MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 579 | |
| 580 | case MBEDTLS_TLS_EXT_KEY_SHARE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 581 | return MBEDTLS_SSL_EXT_ID_KEY_SHARE; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 582 | |
| 583 | case MBEDTLS_TLS_EXT_TRUNCATED_HMAC: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 584 | return MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 585 | |
| 586 | case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 587 | return MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 588 | |
| 589 | case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 590 | return MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 591 | |
| 592 | case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 593 | return MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 594 | |
Jan Bruckner | 151f642 | 2023-02-10 12:45:19 +0100 | [diff] [blame] | 595 | case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT: |
| 596 | return MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT; |
| 597 | |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 598 | case MBEDTLS_TLS_EXT_SESSION_TICKET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 599 | return MBEDTLS_SSL_EXT_ID_SESSION_TICKET; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 600 | |
| 601 | } |
| 602 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 603 | return MBEDTLS_SSL_EXT_ID_UNRECOGNIZED; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 604 | } |
| 605 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 606 | uint32_t mbedtls_ssl_get_extension_mask(unsigned int extension_type) |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 607 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 608 | return 1 << mbedtls_ssl_get_extension_id(extension_type); |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 609 | } |
| 610 | |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 611 | #if defined(MBEDTLS_DEBUG_C) |
| 612 | static const char *extension_name_table[] = { |
Jerry Yu | ea52ed9 | 2022-11-08 21:01:17 +0800 | [diff] [blame] | 613 | [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized", |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 614 | [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name", |
| 615 | [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length", |
| 616 | [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request", |
| 617 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups", |
| 618 | [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms", |
| 619 | [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp", |
| 620 | [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat", |
| 621 | [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation", |
| 622 | [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp", |
| 623 | [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type", |
| 624 | [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type", |
| 625 | [MBEDTLS_SSL_EXT_ID_PADDING] = "padding", |
| 626 | [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key", |
| 627 | [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data", |
| 628 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions", |
| 629 | [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie", |
| 630 | [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes", |
| 631 | [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities", |
| 632 | [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters", |
| 633 | [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth", |
| 634 | [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert", |
| 635 | [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share", |
| 636 | [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac", |
| 637 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats", |
| 638 | [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac", |
| 639 | [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret", |
Jan Bruckner | 151f642 | 2023-02-10 12:45:19 +0100 | [diff] [blame] | 640 | [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket", |
| 641 | [MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT] = "record_size_limit" |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 642 | }; |
| 643 | |
Chien Wong | 4e9683e | 2023-12-28 17:07:43 +0800 | [diff] [blame] | 644 | static const unsigned int extension_type_table[] = { |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 645 | [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff, |
| 646 | [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME, |
| 647 | [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, |
| 648 | [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST, |
| 649 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, |
| 650 | [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG, |
| 651 | [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP, |
| 652 | [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT, |
| 653 | [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN, |
| 654 | [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT, |
| 655 | [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE, |
| 656 | [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE, |
| 657 | [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING, |
| 658 | [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY, |
| 659 | [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA, |
| 660 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, |
| 661 | [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE, |
| 662 | [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES, |
| 663 | [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH, |
| 664 | [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS, |
| 665 | [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH, |
| 666 | [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT, |
| 667 | [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE, |
| 668 | [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC, |
| 669 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, |
| 670 | [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, |
| 671 | [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] | 672 | [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET, |
| 673 | [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] | 674 | }; |
| 675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 676 | const char *mbedtls_ssl_get_extension_name(unsigned int extension_type) |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 677 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 678 | return extension_name_table[ |
| 679 | mbedtls_ssl_get_extension_id(extension_type)]; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 680 | } |
| 681 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 682 | 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] | 683 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 684 | switch (hs_msg_type) { |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 685 | case MBEDTLS_SSL_HS_CLIENT_HELLO: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 686 | return "ClientHello"; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 687 | case MBEDTLS_SSL_HS_SERVER_HELLO: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 688 | return "ServerHello"; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 689 | case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 690 | return "HelloRetryRequest"; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 691 | case MBEDTLS_SSL_HS_NEW_SESSION_TICKET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 692 | return "NewSessionTicket"; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 693 | case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 694 | return "EncryptedExtensions"; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 695 | case MBEDTLS_SSL_HS_CERTIFICATE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 696 | return "Certificate"; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 697 | case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 698 | return "CertificateRequest"; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 699 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 700 | return "Unknown"; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 701 | } |
| 702 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 703 | void mbedtls_ssl_print_extension(const mbedtls_ssl_context *ssl, |
| 704 | int level, const char *file, int line, |
| 705 | int hs_msg_type, unsigned int extension_type, |
| 706 | const char *extra_msg0, const char *extra_msg1) |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 707 | { |
| 708 | const char *extra_msg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 709 | if (extra_msg0 && extra_msg1) { |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 710 | mbedtls_debug_print_msg( |
| 711 | ssl, level, file, line, |
| 712 | "%s: %s(%u) extension %s %s.", |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 713 | ssl_tls13_get_hs_msg_name(hs_msg_type), |
| 714 | mbedtls_ssl_get_extension_name(extension_type), |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 715 | extension_type, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 716 | extra_msg0, extra_msg1); |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 717 | return; |
| 718 | } |
| 719 | |
| 720 | extra_msg = extra_msg0 ? extra_msg0 : extra_msg1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 721 | if (extra_msg) { |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 722 | mbedtls_debug_print_msg( |
| 723 | ssl, level, file, line, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 724 | "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name(hs_msg_type), |
| 725 | mbedtls_ssl_get_extension_name(extension_type), extension_type, |
| 726 | extra_msg); |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 727 | return; |
| 728 | } |
| 729 | |
| 730 | mbedtls_debug_print_msg( |
| 731 | ssl, level, file, line, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 732 | "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name(hs_msg_type), |
| 733 | mbedtls_ssl_get_extension_name(extension_type), extension_type); |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 734 | } |
| 735 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 736 | void mbedtls_ssl_print_extensions(const mbedtls_ssl_context *ssl, |
| 737 | int level, const char *file, int line, |
| 738 | int hs_msg_type, uint32_t extensions_mask, |
| 739 | const char *extra) |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 740 | { |
| 741 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 742 | for (unsigned i = 0; |
| 743 | i < sizeof(extension_name_table) / sizeof(extension_name_table[0]); |
| 744 | i++) { |
Jerry Yu | 79aa721 | 2022-11-08 21:30:21 +0800 | [diff] [blame] | 745 | mbedtls_ssl_print_extension( |
Jerry Yu | ea52ed9 | 2022-11-08 21:01:17 +0800 | [diff] [blame] | 746 | ssl, level, file, line, hs_msg_type, extension_type_table[i], |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 747 | extensions_mask & (1 << i) ? "exists" : "does not exist", extra); |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 748 | } |
| 749 | } |
| 750 | |
Pengyu Lv | ee455c0 | 2023-01-12 14:37:24 +0800 | [diff] [blame] | 751 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Pengyu Lv | ee455c0 | 2023-01-12 14:37:24 +0800 | [diff] [blame] | 752 | static const char *ticket_flag_name_table[] = |
| 753 | { |
| 754 | [0] = "ALLOW_PSK_RESUMPTION", |
| 755 | [2] = "ALLOW_PSK_EPHEMERAL_RESUMPTION", |
| 756 | [3] = "ALLOW_EARLY_DATA", |
| 757 | }; |
| 758 | |
Pengyu Lv | 4938a56 | 2023-01-16 11:28:49 +0800 | [diff] [blame] | 759 | void mbedtls_ssl_print_ticket_flags(const mbedtls_ssl_context *ssl, |
| 760 | int level, const char *file, int line, |
| 761 | unsigned int flags) |
Pengyu Lv | ee455c0 | 2023-01-12 14:37:24 +0800 | [diff] [blame] | 762 | { |
| 763 | size_t i; |
| 764 | |
| 765 | mbedtls_debug_print_msg(ssl, level, file, line, |
Pengyu Lv | 4938a56 | 2023-01-16 11:28:49 +0800 | [diff] [blame] | 766 | "print ticket_flags (0x%02x)", flags); |
| 767 | |
| 768 | flags = flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK; |
Pengyu Lv | ee455c0 | 2023-01-12 14:37:24 +0800 | [diff] [blame] | 769 | |
| 770 | for (i = 0; i < ARRAY_LENGTH(ticket_flag_name_table); i++) { |
Pengyu Lv | 4938a56 | 2023-01-16 11:28:49 +0800 | [diff] [blame] | 771 | if ((flags & (1 << i))) { |
Pengyu Lv | ee455c0 | 2023-01-12 14:37:24 +0800 | [diff] [blame] | 772 | mbedtls_debug_print_msg(ssl, level, file, line, "- %s is set.", |
| 773 | ticket_flag_name_table[i]); |
| 774 | } |
| 775 | } |
| 776 | } |
| 777 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */ |
| 778 | |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 779 | #endif /* MBEDTLS_DEBUG_C */ |
| 780 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 781 | void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl, |
| 782 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 783 | { |
| 784 | ((void) ciphersuite_info); |
| 785 | |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 786 | #if defined(PSA_WANT_ALG_SHA_384) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 787 | if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) { |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 788 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 789 | } else |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 790 | #endif |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 791 | #if defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 792 | if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) { |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 793 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 794 | } else |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 795 | #endif |
| 796 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 797 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 798 | return; |
| 799 | } |
| 800 | } |
| 801 | |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 802 | 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] | 803 | unsigned hs_type, |
| 804 | size_t total_hs_len) |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 805 | { |
| 806 | unsigned char hs_hdr[4]; |
| 807 | |
| 808 | /* Build HS header for checksum update. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 809 | hs_hdr[0] = MBEDTLS_BYTE_0(hs_type); |
| 810 | hs_hdr[1] = MBEDTLS_BYTE_2(total_hs_len); |
| 811 | hs_hdr[2] = MBEDTLS_BYTE_1(total_hs_len); |
| 812 | hs_hdr[3] = MBEDTLS_BYTE_0(total_hs_len); |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 813 | |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 814 | return ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr)); |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 815 | } |
| 816 | |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 817 | 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] | 818 | unsigned hs_type, |
| 819 | unsigned char const *msg, |
| 820 | size_t msg_len) |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 821 | { |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 822 | int ret; |
| 823 | 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] | 824 | if (ret != 0) { |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 825 | return ret; |
Manuel Pégourié-Gonnard | 43cc127 | 2023-02-06 11:48:19 +0100 | [diff] [blame] | 826 | } |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 827 | return ssl->handshake->update_checksum(ssl, msg, msg_len); |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 828 | } |
| 829 | |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 830 | int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 831 | { |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 832 | #if defined(PSA_WANT_ALG_SHA_256) || \ |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 833 | defined(PSA_WANT_ALG_SHA_384) |
Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 834 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 835 | psa_status_t status; |
| 836 | #else |
| 837 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 838 | #endif |
Manuel Pégourié-Gonnard | 626aaed | 2023-02-06 22:03:06 +0100 | [diff] [blame] | 839 | #else /* SHA-256 or SHA-384 */ |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 840 | ((void) ssl); |
Manuel Pégourié-Gonnard | 626aaed | 2023-02-06 22:03:06 +0100 | [diff] [blame] | 841 | #endif /* SHA-256 or SHA-384 */ |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 842 | #if defined(PSA_WANT_ALG_SHA_256) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 843 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 844 | status = psa_hash_abort(&ssl->handshake->fin_sha256_psa); |
| 845 | if (status != PSA_SUCCESS) { |
Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 846 | return mbedtls_md_error_from_psa(status); |
Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 847 | } |
| 848 | status = psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256); |
| 849 | if (status != PSA_SUCCESS) { |
Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 850 | return mbedtls_md_error_from_psa(status); |
Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 851 | } |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 852 | #else |
Manuel Pégourié-Gonnard | 947cee1 | 2023-03-06 11:59:59 +0100 | [diff] [blame] | 853 | mbedtls_md_free(&ssl->handshake->fin_sha256); |
| 854 | mbedtls_md_init(&ssl->handshake->fin_sha256); |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 855 | ret = mbedtls_md_setup(&ssl->handshake->fin_sha256, |
| 856 | mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), |
| 857 | 0); |
| 858 | if (ret != 0) { |
| 859 | return ret; |
| 860 | } |
| 861 | ret = mbedtls_md_starts(&ssl->handshake->fin_sha256); |
Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 862 | if (ret != 0) { |
| 863 | return ret; |
| 864 | } |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 865 | #endif |
| 866 | #endif |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 867 | #if defined(PSA_WANT_ALG_SHA_384) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 868 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 869 | status = psa_hash_abort(&ssl->handshake->fin_sha384_psa); |
| 870 | if (status != PSA_SUCCESS) { |
Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 871 | return mbedtls_md_error_from_psa(status); |
Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 872 | } |
| 873 | status = psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384); |
| 874 | if (status != PSA_SUCCESS) { |
Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 875 | return mbedtls_md_error_from_psa(status); |
Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 876 | } |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 877 | #else |
Manuel Pégourié-Gonnard | 947cee1 | 2023-03-06 11:59:59 +0100 | [diff] [blame] | 878 | mbedtls_md_free(&ssl->handshake->fin_sha384); |
| 879 | mbedtls_md_init(&ssl->handshake->fin_sha384); |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 880 | ret = mbedtls_md_setup(&ssl->handshake->fin_sha384, |
| 881 | mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0); |
| 882 | if (ret != 0) { |
| 883 | return ret; |
| 884 | } |
| 885 | ret = mbedtls_md_starts(&ssl->handshake->fin_sha384); |
Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 886 | if (ret != 0) { |
| 887 | return ret; |
| 888 | } |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 889 | #endif |
| 890 | #endif |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 891 | return 0; |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 892 | } |
| 893 | |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 894 | static int ssl_update_checksum_start(mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 43cc127 | 2023-02-06 11:48:19 +0100 | [diff] [blame] | 895 | const unsigned char *buf, size_t len) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 896 | { |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 897 | #if defined(PSA_WANT_ALG_SHA_256) || \ |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 898 | defined(PSA_WANT_ALG_SHA_384) |
Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 899 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 900 | psa_status_t status; |
| 901 | #else |
| 902 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 903 | #endif |
Manuel Pégourié-Gonnard | 626aaed | 2023-02-06 22:03:06 +0100 | [diff] [blame] | 904 | #else /* SHA-256 or SHA-384 */ |
| 905 | ((void) ssl); |
| 906 | (void) buf; |
| 907 | (void) len; |
| 908 | #endif /* SHA-256 or SHA-384 */ |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 909 | #if defined(PSA_WANT_ALG_SHA_256) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 910 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 911 | status = psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len); |
| 912 | if (status != PSA_SUCCESS) { |
Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 913 | return mbedtls_md_error_from_psa(status); |
Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 914 | } |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 915 | #else |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 916 | ret = mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len); |
Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 917 | if (ret != 0) { |
| 918 | return ret; |
| 919 | } |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 920 | #endif |
| 921 | #endif |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 922 | #if defined(PSA_WANT_ALG_SHA_384) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 923 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 924 | status = psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len); |
| 925 | if (status != PSA_SUCCESS) { |
Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 926 | return mbedtls_md_error_from_psa(status); |
Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 927 | } |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 928 | #else |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 929 | ret = mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len); |
Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 930 | if (ret != 0) { |
| 931 | return ret; |
| 932 | } |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 933 | #endif |
| 934 | #endif |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 935 | return 0; |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 936 | } |
| 937 | |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 938 | #if defined(PSA_WANT_ALG_SHA_256) |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 939 | static int ssl_update_checksum_sha256(mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 43cc127 | 2023-02-06 11:48:19 +0100 | [diff] [blame] | 940 | const unsigned char *buf, size_t len) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 941 | { |
| 942 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 943 | return mbedtls_md_error_from_psa(psa_hash_update( |
| 944 | &ssl->handshake->fin_sha256_psa, buf, len)); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 945 | #else |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 946 | return mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 947 | #endif |
| 948 | } |
| 949 | #endif |
| 950 | |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 951 | #if defined(PSA_WANT_ALG_SHA_384) |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 952 | static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 43cc127 | 2023-02-06 11:48:19 +0100 | [diff] [blame] | 953 | const unsigned char *buf, size_t len) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 954 | { |
| 955 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 956 | return mbedtls_md_error_from_psa(psa_hash_update( |
| 957 | &ssl->handshake->fin_sha384_psa, buf, len)); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 958 | #else |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 959 | return mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 960 | #endif |
| 961 | } |
| 962 | #endif |
| 963 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 964 | static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 965 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 966 | memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params)); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 967 | |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 968 | #if defined(PSA_WANT_ALG_SHA_256) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 969 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 970 | handshake->fin_sha256_psa = psa_hash_operation_init(); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 971 | #else |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 972 | mbedtls_md_init(&handshake->fin_sha256); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 973 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 974 | #endif |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 975 | #if defined(PSA_WANT_ALG_SHA_384) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 976 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 977 | handshake->fin_sha384_psa = psa_hash_operation_init(); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 978 | #else |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 979 | mbedtls_md_init(&handshake->fin_sha384); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 980 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 981 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 982 | |
| 983 | handshake->update_checksum = ssl_update_checksum_start; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 984 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 985 | #if defined(MBEDTLS_DHM_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 986 | mbedtls_dhm_init(&handshake->dhm_ctx); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 987 | #endif |
Valerio Setti | 7aeec54 | 2023-07-05 18:57:21 +0200 | [diff] [blame] | 988 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Valerio Setti | 6eb0054 | 2023-07-07 17:04:24 +0200 | [diff] [blame] | 989 | defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 990 | mbedtls_ecdh_init(&handshake->ecdh_ctx); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 991 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 992 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 993 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 994 | handshake->psa_pake_ctx = psa_pake_operation_init(); |
| 995 | handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT; |
| 996 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 997 | mbedtls_ecjpake_init(&handshake->ecjpake_ctx); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 998 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 999 | #if defined(MBEDTLS_SSL_CLI_C) |
| 1000 | handshake->ecjpake_cache = NULL; |
| 1001 | handshake->ecjpake_cache_len = 0; |
| 1002 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 1003 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1004 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1005 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1006 | mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 1007 | #endif |
| 1008 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1009 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 1010 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 1011 | #endif |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 1012 | |
| 1013 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 1014 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1015 | mbedtls_pk_init(&handshake->peer_pubkey); |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 1016 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1017 | } |
| 1018 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1019 | void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1020 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1021 | memset(transform, 0, sizeof(mbedtls_ssl_transform)); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 1022 | |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 1023 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1024 | transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 1025 | transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 1026 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1027 | mbedtls_cipher_init(&transform->cipher_ctx_enc); |
| 1028 | mbedtls_cipher_init(&transform->cipher_ctx_dec); |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 1029 | #endif |
| 1030 | |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 1031 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 39b8e7d | 2022-02-23 09:24:45 +0100 | [diff] [blame] | 1032 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1033 | transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 1034 | transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 1035 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1036 | mbedtls_md_init(&transform->md_ctx_enc); |
| 1037 | mbedtls_md_init(&transform->md_ctx_dec); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1038 | #endif |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 1039 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1040 | } |
| 1041 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1042 | void mbedtls_ssl_session_init(mbedtls_ssl_session *session) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1043 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1044 | memset(session, 0, sizeof(mbedtls_ssl_session)); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1045 | } |
| 1046 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1047 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1048 | static int ssl_handshake_init(mbedtls_ssl_context *ssl) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1049 | { |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 1050 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1051 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1052 | /* Clear old handshake information if present */ |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1053 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1054 | if (ssl->transform_negotiate) { |
| 1055 | mbedtls_ssl_transform_free(ssl->transform_negotiate); |
| 1056 | } |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1057 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1058 | if (ssl->session_negotiate) { |
| 1059 | mbedtls_ssl_session_free(ssl->session_negotiate); |
| 1060 | } |
| 1061 | if (ssl->handshake) { |
| 1062 | mbedtls_ssl_handshake_free(ssl); |
| 1063 | } |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1064 | |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1065 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1066 | /* |
| 1067 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 1068 | * Now allocate missing structures. |
| 1069 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1070 | if (ssl->transform_negotiate == NULL) { |
| 1071 | ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform)); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 1072 | } |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1073 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1074 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1075 | if (ssl->session_negotiate == NULL) { |
| 1076 | ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session)); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 1077 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1078 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1079 | if (ssl->handshake == NULL) { |
| 1080 | ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params)); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 1081 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1082 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1083 | /* If the buffers are too small - reallocate */ |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 1084 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1085 | handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN, |
| 1086 | MBEDTLS_SSL_OUT_BUFFER_LEN); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1087 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1088 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1089 | /* All pointers should exist and can be directly freed without issue */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1090 | if (ssl->handshake == NULL || |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1091 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1092 | ssl->transform_negotiate == NULL || |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1093 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1094 | ssl->session_negotiate == NULL) { |
| 1095 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed")); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1096 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1097 | mbedtls_free(ssl->handshake); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1098 | ssl->handshake = NULL; |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1099 | |
| 1100 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1101 | mbedtls_free(ssl->transform_negotiate); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1102 | ssl->transform_negotiate = NULL; |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1103 | #endif |
| 1104 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1105 | mbedtls_free(ssl->session_negotiate); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1106 | ssl->session_negotiate = NULL; |
| 1107 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1108 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
Jerry Yu | 4caf3ca | 2023-11-15 16:13:47 +0800 | [diff] [blame] | 1111 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 1112 | #if defined(MBEDTLS_SSL_CLI_C) |
Ronald Cron | 05d7cfb | 2024-03-03 15:39:30 +0100 | [diff] [blame] | 1113 | ssl->early_data_state = MBEDTLS_SSL_EARLY_DATA_STATE_IDLE; |
Ronald Cron | 5d0ae90 | 2024-01-05 14:20:35 +0100 | [diff] [blame] | 1114 | #endif |
Jerry Yu | 4caf3ca | 2023-11-15 16:13:47 +0800 | [diff] [blame] | 1115 | #if defined(MBEDTLS_SSL_SRV_C) |
| 1116 | ssl->discard_early_data_record = MBEDTLS_SSL_EARLY_DATA_NO_DISCARD; |
| 1117 | #endif |
Ronald Cron | 19bfe0a | 2024-02-26 16:43:01 +0100 | [diff] [blame] | 1118 | ssl->total_early_data_size = 0; |
Jerry Yu | 4caf3ca | 2023-11-15 16:13:47 +0800 | [diff] [blame] | 1119 | #endif /* MBEDTLS_SSL_EARLY_DATA */ |
Ronald Cron | 5d0ae90 | 2024-01-05 14:20:35 +0100 | [diff] [blame] | 1120 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1121 | /* Initialize structures */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1122 | mbedtls_ssl_session_init(ssl->session_negotiate); |
| 1123 | ssl_handshake_params_init(ssl->handshake); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 1124 | |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1125 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1126 | mbedtls_ssl_transform_init(ssl->transform_negotiate); |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1127 | #endif |
| 1128 | |
Manuel Pégourié-Gonnard | 537f231 | 2023-02-05 10:17:45 +0100 | [diff] [blame] | 1129 | /* Setup handshake checksums */ |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 1130 | ret = mbedtls_ssl_reset_checksum(ssl); |
| 1131 | if (ret != 0) { |
| 1132 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret); |
| 1133 | return ret; |
| 1134 | } |
Manuel Pégourié-Gonnard | 537f231 | 2023-02-05 10:17:45 +0100 | [diff] [blame] | 1135 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 1136 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 1137 | defined(MBEDTLS_SSL_SRV_C) && \ |
| 1138 | defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 1139 | ssl->handshake->new_session_tickets_count = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1140 | ssl->conf->new_session_tickets_count; |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 1141 | #endif |
| 1142 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1143 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1144 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1145 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1146 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1147 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1148 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1149 | } else { |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1150 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1151 | } |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1153 | mbedtls_ssl_set_timer(ssl, 0); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1154 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1155 | #endif |
| 1156 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1157 | /* |
| 1158 | * curve_list is translated to IANA TLS group identifiers here because |
| 1159 | * mbedtls_ssl_conf_curves returns void and so can't return |
| 1160 | * any error codes. |
| 1161 | */ |
Manuel Pégourié-Gonnard | f07ce3b | 2023-09-22 11:53:41 +0200 | [diff] [blame] | 1162 | #if defined(MBEDTLS_ECP_C) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1163 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 1164 | /* Heap allocate and translate curve_list from internal to IANA group ids */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1165 | if (ssl->conf->curve_list != NULL) { |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1166 | size_t length; |
| 1167 | const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list; |
| 1168 | |
Thomas Daubney | 850a079 | 2023-05-22 12:05:03 +0100 | [diff] [blame] | 1169 | for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE); length++) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1170 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1171 | |
| 1172 | /* Leave room for zero termination */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1173 | uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t)); |
| 1174 | if (group_list == NULL) { |
| 1175 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 1176 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1177 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1178 | for (size_t i = 0; i < length; i++) { |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 1179 | 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] | 1180 | curve_list[i]); |
| 1181 | if (tls_id == 0) { |
| 1182 | mbedtls_free(group_list); |
| 1183 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1184 | } |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 1185 | group_list[i] = tls_id; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | group_list[length] = 0; |
| 1189 | |
| 1190 | ssl->handshake->group_list = group_list; |
| 1191 | ssl->handshake->group_list_heap_allocated = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1192 | } else { |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1193 | ssl->handshake->group_list = ssl->conf->group_list; |
| 1194 | ssl->handshake->group_list_heap_allocated = 0; |
| 1195 | } |
| 1196 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Manuel Pégourié-Gonnard | f07ce3b | 2023-09-22 11:53:41 +0200 | [diff] [blame] | 1197 | #endif /* MBEDTLS_ECP_C */ |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1198 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1199 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1200 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 1201 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 1202 | /* Heap allocate and translate sig_hashes from internal hash identifiers to |
| 1203 | signature algorithms IANA identifiers. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1204 | if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) && |
| 1205 | ssl->conf->sig_hashes != NULL) { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1206 | const int *md; |
| 1207 | const int *sig_hashes = ssl->conf->sig_hashes; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1208 | size_t sig_algs_len = 0; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1209 | uint16_t *p; |
| 1210 | |
Tom Cosgrove | 6ef9bb3 | 2023-03-08 14:19:51 +0000 | [diff] [blame] | 1211 | MBEDTLS_STATIC_ASSERT(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN |
| 1212 | <= (SIZE_MAX - (2 * sizeof(uint16_t))), |
| 1213 | "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big"); |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 1214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1215 | for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) { |
| 1216 | if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1217 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1218 | } |
Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 1219 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1220 | sig_algs_len += sizeof(uint16_t); |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1221 | #endif |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 1222 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1223 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1224 | sig_algs_len += sizeof(uint16_t); |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1225 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1226 | if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) { |
| 1227 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
| 1228 | } |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1229 | } |
| 1230 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1231 | if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) { |
| 1232 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
| 1233 | } |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1234 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1235 | ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len + |
| 1236 | sizeof(uint16_t)); |
| 1237 | if (ssl->handshake->sig_algs == NULL) { |
| 1238 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 1239 | } |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1240 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1241 | p = (uint16_t *) ssl->handshake->sig_algs; |
| 1242 | for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) { |
| 1243 | unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md); |
| 1244 | if (hash == MBEDTLS_SSL_HASH_NONE) { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1245 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1246 | } |
Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 1247 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1248 | *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1249 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 1250 | #endif |
| 1251 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1252 | *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1253 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 1254 | #endif |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1255 | } |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 1256 | *p = MBEDTLS_TLS_SIG_NONE; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1257 | ssl->handshake->sig_algs_heap_allocated = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1258 | } else |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 1259 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1260 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1261 | ssl->handshake->sig_algs_heap_allocated = 0; |
| 1262 | } |
Jerry Yu | cc53910 | 2022-06-27 16:27:35 +0800 | [diff] [blame] | 1263 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1264 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1265 | return 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1268 | #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] | 1269 | /* Dummy cookie callbacks for defaults */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1270 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1271 | static int ssl_cookie_write_dummy(void *ctx, |
| 1272 | unsigned char **p, unsigned char *end, |
| 1273 | const unsigned char *cli_id, size_t cli_id_len) |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1274 | { |
| 1275 | ((void) ctx); |
| 1276 | ((void) p); |
| 1277 | ((void) end); |
| 1278 | ((void) cli_id); |
| 1279 | ((void) cli_id_len); |
| 1280 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1281 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1282 | } |
| 1283 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1284 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1285 | static int ssl_cookie_check_dummy(void *ctx, |
| 1286 | const unsigned char *cookie, size_t cookie_len, |
| 1287 | const unsigned char *cli_id, size_t cli_id_len) |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1288 | { |
| 1289 | ((void) ctx); |
| 1290 | ((void) cookie); |
| 1291 | ((void) cookie_len); |
| 1292 | ((void) cli_id); |
| 1293 | ((void) cli_id_len); |
| 1294 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1295 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1296 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1297 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1298 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1299 | /* |
| 1300 | * Initialize an SSL context |
| 1301 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1302 | void mbedtls_ssl_init(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1303 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1304 | memset(ssl, 0, sizeof(mbedtls_ssl_context)); |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1305 | } |
| 1306 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1307 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1308 | static int ssl_conf_version_check(const mbedtls_ssl_context *ssl) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1309 | { |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 1310 | const mbedtls_ssl_config *conf = ssl->conf; |
| 1311 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1312 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1313 | if (mbedtls_ssl_conf_is_tls13_only(conf)) { |
| 1314 | if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 1315 | MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported.")); |
| 1316 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1319 | MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only.")); |
| 1320 | return 0; |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1321 | } |
| 1322 | #endif |
| 1323 | |
| 1324 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1325 | if (mbedtls_ssl_conf_is_tls12_only(conf)) { |
| 1326 | MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only.")); |
| 1327 | return 0; |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1328 | } |
| 1329 | #endif |
| 1330 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1331 | #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] | 1332 | if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) { |
| 1333 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 1334 | MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2")); |
| 1335 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1338 | MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2.")); |
| 1339 | return 0; |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1340 | } |
| 1341 | #endif |
| 1342 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1343 | MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid.")); |
| 1344 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1345 | } |
| 1346 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1347 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1348 | static int ssl_conf_check(const mbedtls_ssl_context *ssl) |
| 1349 | { |
| 1350 | int ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1351 | ret = ssl_conf_version_check(ssl); |
| 1352 | if (ret != 0) { |
| 1353 | return ret; |
| 1354 | } |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1355 | |
Yanray Wang | b422cab | 2023-12-01 16:18:10 +0800 | [diff] [blame] | 1356 | if (ssl->conf->f_rng == NULL) { |
| 1357 | MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided")); |
| 1358 | return MBEDTLS_ERR_SSL_NO_RNG; |
| 1359 | } |
| 1360 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1361 | /* Space for further checks */ |
| 1362 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1363 | return 0; |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1364 | } |
| 1365 | |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1366 | /* |
| 1367 | * Setup an SSL context |
| 1368 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 1369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1370 | int mbedtls_ssl_setup(mbedtls_ssl_context *ssl, |
| 1371 | const mbedtls_ssl_config *conf) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1372 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1373 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1374 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1375 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1376 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 1377 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1378 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1379 | if ((ret = ssl_conf_check(ssl)) != 0) { |
| 1380 | return ret; |
| 1381 | } |
Ronald Cron | 8a12aee | 2023-03-08 15:30:43 +0100 | [diff] [blame] | 1382 | ssl->tls_version = ssl->conf->max_tls_version; |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1383 | |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1384 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 1385 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1386 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 1387 | |
| 1388 | /* Set to NULL in case of an error condition */ |
| 1389 | ssl->out_buf = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1390 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1391 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1392 | ssl->in_buf_len = in_buf_len; |
| 1393 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1394 | ssl->in_buf = mbedtls_calloc(1, in_buf_len); |
| 1395 | if (ssl->in_buf == NULL) { |
| 1396 | 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] | 1397 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1398 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1399 | } |
| 1400 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1401 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1402 | ssl->out_buf_len = out_buf_len; |
| 1403 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1404 | ssl->out_buf = mbedtls_calloc(1, out_buf_len); |
| 1405 | if (ssl->out_buf == NULL) { |
| 1406 | 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] | 1407 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1408 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1409 | } |
| 1410 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1411 | mbedtls_ssl_reset_in_out_pointers(ssl); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 1412 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1413 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1414 | memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info)); |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1415 | #endif |
| 1416 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1417 | if ((ret = ssl_handshake_init(ssl)) != 0) { |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1418 | goto error; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1419 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1420 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1421 | return 0; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1422 | |
| 1423 | error: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1424 | mbedtls_free(ssl->in_buf); |
| 1425 | mbedtls_free(ssl->out_buf); |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1426 | |
| 1427 | ssl->conf = NULL; |
| 1428 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1429 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1430 | ssl->in_buf_len = 0; |
| 1431 | ssl->out_buf_len = 0; |
| 1432 | #endif |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1433 | ssl->in_buf = NULL; |
| 1434 | ssl->out_buf = NULL; |
| 1435 | |
| 1436 | ssl->in_hdr = NULL; |
| 1437 | ssl->in_ctr = NULL; |
| 1438 | ssl->in_len = NULL; |
| 1439 | ssl->in_iv = NULL; |
| 1440 | ssl->in_msg = NULL; |
| 1441 | |
| 1442 | ssl->out_hdr = NULL; |
| 1443 | ssl->out_ctr = NULL; |
| 1444 | ssl->out_len = NULL; |
| 1445 | ssl->out_iv = NULL; |
| 1446 | ssl->out_msg = NULL; |
| 1447 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1448 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1452 | * Reset an initialized and used SSL context for re-use while retaining |
| 1453 | * all application-set variables, function pointers and data. |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1454 | * |
| 1455 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 1456 | * (Use when a DTLS client reconnects from the same port.) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1457 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1458 | void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl, |
| 1459 | int partial) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1460 | { |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1461 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1462 | size_t in_buf_len = ssl->in_buf_len; |
| 1463 | size_t out_buf_len = ssl->out_buf_len; |
| 1464 | #else |
| 1465 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1466 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 1467 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1468 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1469 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C) |
| 1470 | partial = 0; |
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 1471 | #endif |
| 1472 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1473 | /* Cancel any possibly running timer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1474 | mbedtls_ssl_set_timer(ssl, 0); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1475 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1476 | mbedtls_ssl_reset_in_out_pointers(ssl); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1477 | |
| 1478 | /* Reset incoming message parsing */ |
| 1479 | ssl->in_offt = NULL; |
| 1480 | ssl->nb_zero = 0; |
| 1481 | ssl->in_msgtype = 0; |
| 1482 | ssl->in_msglen = 0; |
| 1483 | ssl->in_hslen = 0; |
| 1484 | ssl->keep_current_message = 0; |
| 1485 | ssl->transform_in = NULL; |
| 1486 | |
| 1487 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1488 | ssl->next_record_offset = 0; |
| 1489 | ssl->in_epoch = 0; |
| 1490 | #endif |
| 1491 | |
| 1492 | /* Keep current datagram if partial == 1 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1493 | if (partial == 0) { |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1494 | ssl->in_left = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1495 | memset(ssl->in_buf, 0, in_buf_len); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1496 | } |
| 1497 | |
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 1498 | ssl->send_alert = 0; |
| 1499 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1500 | /* Reset outgoing message writing */ |
| 1501 | ssl->out_msgtype = 0; |
| 1502 | ssl->out_msglen = 0; |
| 1503 | ssl->out_left = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1504 | memset(ssl->out_buf, 0, out_buf_len); |
| 1505 | memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr)); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1506 | ssl->transform_out = NULL; |
| 1507 | |
| 1508 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1509 | mbedtls_ssl_dtls_replay_reset(ssl); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1510 | #endif |
| 1511 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1512 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1513 | if (ssl->transform) { |
| 1514 | mbedtls_ssl_transform_free(ssl->transform); |
| 1515 | mbedtls_free(ssl->transform); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1516 | ssl->transform = NULL; |
| 1517 | } |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1518 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 1519 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1520 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1521 | mbedtls_ssl_transform_free(ssl->transform_application); |
| 1522 | mbedtls_free(ssl->transform_application); |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1523 | ssl->transform_application = NULL; |
| 1524 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1525 | if (ssl->handshake != NULL) { |
Jerry Yu | 3d9b590 | 2022-11-04 14:07:25 +0800 | [diff] [blame] | 1526 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1527 | mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata); |
| 1528 | mbedtls_free(ssl->handshake->transform_earlydata); |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1529 | ssl->handshake->transform_earlydata = NULL; |
Jerry Yu | 3d9b590 | 2022-11-04 14:07:25 +0800 | [diff] [blame] | 1530 | #endif |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1531 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1532 | mbedtls_ssl_transform_free(ssl->handshake->transform_handshake); |
| 1533 | mbedtls_free(ssl->handshake->transform_handshake); |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1534 | ssl->handshake->transform_handshake = NULL; |
| 1535 | } |
| 1536 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1537 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1538 | } |
| 1539 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1540 | int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial) |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1541 | { |
| 1542 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1543 | |
| 1544 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
Ronald Cron | 195c0bc | 2024-02-08 08:51:20 +0100 | [diff] [blame] | 1545 | ssl->tls_version = ssl->conf->max_tls_version; |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1546 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1547 | mbedtls_ssl_session_reset_msg_layer(ssl, partial); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1548 | |
| 1549 | /* Reset renegotiation state */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1550 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1551 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1552 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1553 | |
| 1554 | ssl->verify_data_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1555 | memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN); |
| 1556 | 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] | 1557 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1558 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1559 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1560 | ssl->session_in = NULL; |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 1561 | ssl->session_out = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1562 | if (ssl->session) { |
| 1563 | mbedtls_ssl_session_free(ssl->session); |
| 1564 | mbedtls_free(ssl->session); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1565 | ssl->session = NULL; |
| 1566 | } |
| 1567 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1568 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 1569 | ssl->alpn_chosen = NULL; |
| 1570 | #endif |
| 1571 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1572 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 1573 | int free_cli_id = 1; |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1574 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1575 | free_cli_id = (partial == 0); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1576 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1577 | if (free_cli_id) { |
| 1578 | mbedtls_free(ssl->cli_id); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1579 | ssl->cli_id = NULL; |
| 1580 | ssl->cli_id_len = 0; |
| 1581 | } |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 1582 | #endif |
| 1583 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1584 | if ((ret = ssl_handshake_init(ssl)) != 0) { |
| 1585 | return ret; |
| 1586 | } |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1587 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1588 | return 0; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1589 | } |
| 1590 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 1591 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1592 | * Reset an initialized and used SSL context for re-use while retaining |
| 1593 | * all application-set variables, function pointers and data. |
| 1594 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1595 | int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1596 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1597 | return mbedtls_ssl_session_reset_int(ssl, 0); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1601 | * SSL set accessors |
| 1602 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1603 | void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1604 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1605 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1608 | 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] | 1609 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1610 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 1611 | } |
| 1612 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1613 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1614 | 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] | 1615 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1616 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 1617 | } |
| 1618 | #endif |
| 1619 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1620 | 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] | 1621 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1622 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1623 | } |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1624 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1625 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1626 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1627 | void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl, |
| 1628 | unsigned allow_packing) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1629 | { |
| 1630 | ssl->disable_datagram_packing = !allow_packing; |
| 1631 | } |
| 1632 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1633 | void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf, |
| 1634 | uint32_t min, uint32_t max) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1635 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1636 | conf->hs_timeout_min = min; |
| 1637 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1638 | } |
| 1639 | #endif |
| 1640 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1641 | void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1642 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1643 | conf->authmode = authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1644 | } |
| 1645 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1646 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1647 | void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf, |
| 1648 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 1649 | void *p_vrfy) |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1650 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1651 | conf->f_vrfy = f_vrfy; |
| 1652 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1653 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1654 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1655 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1656 | void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf, |
| 1657 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1658 | void *p_rng) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1659 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1660 | conf->f_rng = f_rng; |
| 1661 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1664 | void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf, |
| 1665 | void (*f_dbg)(void *, int, const char *, int, const char *), |
| 1666 | void *p_dbg) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1667 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1668 | conf->f_dbg = f_dbg; |
| 1669 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1670 | } |
| 1671 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1672 | void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl, |
| 1673 | void *p_bio, |
| 1674 | mbedtls_ssl_send_t *f_send, |
| 1675 | mbedtls_ssl_recv_t *f_recv, |
| 1676 | mbedtls_ssl_recv_timeout_t *f_recv_timeout) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1677 | { |
| 1678 | ssl->p_bio = p_bio; |
| 1679 | ssl->f_send = f_send; |
| 1680 | ssl->f_recv = f_recv; |
| 1681 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 1682 | } |
| 1683 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 1684 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1685 | 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] | 1686 | { |
| 1687 | ssl->mtu = mtu; |
| 1688 | } |
| 1689 | #endif |
| 1690 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1691 | 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] | 1692 | { |
| 1693 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1694 | } |
| 1695 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1696 | void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl, |
| 1697 | void *p_timer, |
| 1698 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 1699 | mbedtls_ssl_get_timer_t *f_get_timer) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1700 | { |
| 1701 | ssl->p_timer = p_timer; |
| 1702 | ssl->f_set_timer = f_set_timer; |
| 1703 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1704 | |
| 1705 | /* Make sure we start with no timer running */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1706 | mbedtls_ssl_set_timer(ssl, 0); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1707 | } |
| 1708 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1709 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1710 | void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf, |
| 1711 | void *p_cache, |
| 1712 | mbedtls_ssl_cache_get_t *f_get_cache, |
| 1713 | mbedtls_ssl_cache_set_t *f_set_cache) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1714 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 1715 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1716 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1717 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1718 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1719 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1720 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1721 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1722 | 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] | 1723 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1724 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1725 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1726 | if (ssl == NULL || |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1727 | session == NULL || |
| 1728 | ssl->session_negotiate == NULL || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1729 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) { |
| 1730 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1731 | } |
| 1732 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1733 | if (ssl->handshake->resume == 1) { |
| 1734 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 1735 | } |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 1736 | |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1737 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1738 | if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Ronald Cron | 233fcaa | 2024-04-04 14:05:21 +0200 | [diff] [blame] | 1739 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1740 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1741 | mbedtls_ssl_ciphersuite_from_id(session->ciphersuite); |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1742 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1743 | if (mbedtls_ssl_validate_ciphersuite( |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1744 | ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1745 | MBEDTLS_SSL_VERSION_TLS1_3) != 0) { |
| 1746 | MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.", |
| 1747 | session->ciphersuite)); |
| 1748 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1749 | } |
Ronald Cron | 233fcaa | 2024-04-04 14:05:21 +0200 | [diff] [blame] | 1750 | #else |
| 1751 | /* |
| 1752 | * If session tickets are not enabled, it is not possible to resume a |
| 1753 | * TLS 1.3 session, thus do not make any change to the SSL context in |
| 1754 | * the first place. |
| 1755 | */ |
| 1756 | return 0; |
| 1757 | #endif |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1758 | } |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1759 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1760 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1761 | if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate, |
| 1762 | session)) != 0) { |
| 1763 | return ret; |
| 1764 | } |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1765 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1766 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1767 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1768 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1769 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1770 | #endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1771 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1772 | void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf, |
| 1773 | const int *ciphersuites) |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1774 | { |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 1775 | conf->ciphersuite_list = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1776 | } |
| 1777 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1778 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1779 | void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf, |
| 1780 | const int kex_modes) |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1781 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1782 | 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] | 1783 | } |
Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame] | 1784 | |
| 1785 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
Yanray Wang | d5ed36f | 2023-11-07 11:40:43 +0800 | [diff] [blame] | 1786 | void mbedtls_ssl_conf_early_data(mbedtls_ssl_config *conf, |
| 1787 | int early_data_enabled) |
Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame] | 1788 | { |
| 1789 | conf->early_data_enabled = early_data_enabled; |
| 1790 | } |
Jerry Yu | cc4e007 | 2022-11-22 17:22:22 +0800 | [diff] [blame] | 1791 | |
| 1792 | #if defined(MBEDTLS_SSL_SRV_C) |
Yanray Wang | 0751761 | 2023-11-07 11:47:36 +0800 | [diff] [blame] | 1793 | void mbedtls_ssl_conf_max_early_data_size( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1794 | mbedtls_ssl_config *conf, uint32_t max_early_data_size) |
Jerry Yu | cc4e007 | 2022-11-22 17:22:22 +0800 | [diff] [blame] | 1795 | { |
Jerry Yu | 39da985 | 2022-12-06 16:58:36 +0800 | [diff] [blame] | 1796 | conf->max_early_data_size = max_early_data_size; |
Jerry Yu | cc4e007 | 2022-11-22 17:22:22 +0800 | [diff] [blame] | 1797 | } |
| 1798 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 1799 | |
Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame] | 1800 | #endif /* MBEDTLS_SSL_EARLY_DATA */ |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1801 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1802 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1803 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1804 | void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf, |
| 1805 | const mbedtls_x509_crt_profile *profile) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1806 | { |
| 1807 | conf->cert_profile = profile; |
| 1808 | } |
| 1809 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1810 | static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert) |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1811 | { |
| 1812 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
| 1813 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1814 | while (cur != NULL) { |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1815 | next = cur->next; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1816 | mbedtls_free(cur); |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1817 | cur = next; |
| 1818 | } |
| 1819 | } |
| 1820 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1821 | /* Append a new keycert entry to a (possibly empty) list */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1822 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1823 | static int ssl_append_key_cert(mbedtls_ssl_key_cert **head, |
| 1824 | mbedtls_x509_crt *cert, |
| 1825 | mbedtls_pk_context *key) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1826 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1827 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1828 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1829 | if (cert == NULL) { |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1830 | /* Free list if cert is null */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1831 | ssl_key_cert_free(*head); |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1832 | *head = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1833 | return 0; |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1834 | } |
| 1835 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1836 | new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert)); |
| 1837 | if (new_cert == NULL) { |
| 1838 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 1839 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1840 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1841 | new_cert->cert = cert; |
| 1842 | new_cert->key = key; |
| 1843 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1844 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1845 | /* Update head if the list was null, else add to the end */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1846 | if (*head == NULL) { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1847 | *head = new_cert; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1848 | } else { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1849 | mbedtls_ssl_key_cert *cur = *head; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1850 | while (cur->next != NULL) { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1851 | cur = cur->next; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1852 | } |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1853 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1854 | } |
| 1855 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1856 | return 0; |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1857 | } |
| 1858 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1859 | int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1860 | mbedtls_x509_crt *own_cert, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1861 | mbedtls_pk_context *pk_key) |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1862 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1863 | 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] | 1864 | } |
| 1865 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1866 | void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1867 | mbedtls_x509_crt *ca_chain, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1868 | mbedtls_x509_crl *ca_crl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1869 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1870 | conf->ca_chain = ca_chain; |
| 1871 | conf->ca_crl = ca_crl; |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1872 | |
| 1873 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1874 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1875 | * cannot be used together. */ |
| 1876 | conf->f_ca_cb = NULL; |
| 1877 | conf->p_ca_cb = NULL; |
| 1878 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1879 | } |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1880 | |
| 1881 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1882 | void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf, |
| 1883 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
| 1884 | void *p_ca_cb) |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1885 | { |
| 1886 | conf->f_ca_cb = f_ca_cb; |
| 1887 | conf->p_ca_cb = p_ca_cb; |
| 1888 | |
| 1889 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1890 | * cannot be used together. */ |
| 1891 | conf->ca_chain = NULL; |
| 1892 | conf->ca_crl = NULL; |
| 1893 | } |
| 1894 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1895 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 1896 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1897 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1898 | const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl, |
| 1899 | size_t *name_len) |
Glenn Strauss | 6989407 | 2022-01-24 12:58:00 -0500 | [diff] [blame] | 1900 | { |
| 1901 | *name_len = ssl->handshake->sni_name_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1902 | return ssl->handshake->sni_name; |
Glenn Strauss | 6989407 | 2022-01-24 12:58:00 -0500 | [diff] [blame] | 1903 | } |
| 1904 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1905 | int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl, |
| 1906 | mbedtls_x509_crt *own_cert, |
| 1907 | mbedtls_pk_context *pk_key) |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1908 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1909 | return ssl_append_key_cert(&ssl->handshake->sni_key_cert, |
| 1910 | own_cert, pk_key); |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1911 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 1912 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1913 | void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl, |
| 1914 | mbedtls_x509_crt *ca_chain, |
| 1915 | mbedtls_x509_crl *ca_crl) |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 1916 | { |
| 1917 | ssl->handshake->sni_ca_chain = ca_chain; |
| 1918 | ssl->handshake->sni_ca_crl = ca_crl; |
| 1919 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1920 | |
Glenn Strauss | 999ef70 | 2022-03-11 01:37:23 -0500 | [diff] [blame] | 1921 | #if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1922 | void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl, |
| 1923 | const mbedtls_x509_crt *crt) |
Glenn Strauss | 999ef70 | 2022-03-11 01:37:23 -0500 | [diff] [blame] | 1924 | { |
| 1925 | ssl->handshake->dn_hints = crt; |
| 1926 | } |
| 1927 | #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ |
| 1928 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1929 | void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl, |
| 1930 | int authmode) |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1931 | { |
| 1932 | ssl->handshake->sni_authmode = authmode; |
| 1933 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1934 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 1935 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1936 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1937 | void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl, |
| 1938 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 1939 | void *p_vrfy) |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1940 | { |
| 1941 | ssl->f_vrfy = f_vrfy; |
| 1942 | ssl->p_vrfy = p_vrfy; |
| 1943 | } |
| 1944 | #endif |
| 1945 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1946 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1947 | |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1948 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemek Stekiel | fde1128 | 2023-03-13 16:06:09 +0100 | [diff] [blame] | 1949 | static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' }; |
| 1950 | static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' }; |
| 1951 | |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1952 | static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1953 | mbedtls_ssl_context *ssl, |
| 1954 | mbedtls_svc_key_id_t pwd) |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1955 | { |
| 1956 | psa_status_t status; |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1957 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
Przemek Stekiel | fde1128 | 2023-03-13 16:06:09 +0100 | [diff] [blame] | 1958 | const uint8_t *user = NULL; |
Przemek Stekiel | 4cd2031 | 2023-03-01 11:11:28 +0100 | [diff] [blame] | 1959 | size_t user_len = 0; |
Przemek Stekiel | fde1128 | 2023-03-13 16:06:09 +0100 | [diff] [blame] | 1960 | const uint8_t *peer = NULL; |
Przemek Stekiel | 4cd2031 | 2023-03-01 11:11:28 +0100 | [diff] [blame] | 1961 | size_t peer_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1962 | psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE); |
| 1963 | psa_pake_cs_set_primitive(&cipher_suite, |
| 1964 | PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, |
| 1965 | PSA_ECC_FAMILY_SECP_R1, |
| 1966 | 256)); |
| 1967 | psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256); |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1968 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1969 | status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite); |
| 1970 | if (status != PSA_SUCCESS) { |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1971 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1972 | } |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1973 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1974 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Przemek Stekiel | fde1128 | 2023-03-13 16:06:09 +0100 | [diff] [blame] | 1975 | user = jpake_server_id; |
| 1976 | user_len = sizeof(jpake_server_id); |
| 1977 | peer = jpake_client_id; |
| 1978 | peer_len = sizeof(jpake_client_id); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1979 | } else { |
Przemek Stekiel | fde1128 | 2023-03-13 16:06:09 +0100 | [diff] [blame] | 1980 | user = jpake_client_id; |
| 1981 | user_len = sizeof(jpake_client_id); |
| 1982 | peer = jpake_server_id; |
| 1983 | peer_len = sizeof(jpake_server_id); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1984 | } |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1985 | |
Przemek Stekiel | 4cd2031 | 2023-03-01 11:11:28 +0100 | [diff] [blame] | 1986 | status = psa_pake_set_user(&ssl->handshake->psa_pake_ctx, user, user_len); |
| 1987 | if (status != PSA_SUCCESS) { |
| 1988 | return status; |
| 1989 | } |
| 1990 | |
| 1991 | 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] | 1992 | if (status != PSA_SUCCESS) { |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1993 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1994 | } |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1995 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1996 | status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd); |
| 1997 | if (status != PSA_SUCCESS) { |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1998 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1999 | } |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 2000 | |
| 2001 | ssl->handshake->psa_pake_ctx_is_ok = 1; |
| 2002 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2003 | return PSA_SUCCESS; |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 2004 | } |
| 2005 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2006 | int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl, |
| 2007 | const unsigned char *pw, |
| 2008 | size_t pw_len) |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 2009 | { |
| 2010 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2011 | psa_status_t status; |
| 2012 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2013 | if (ssl->handshake == NULL || ssl->conf == NULL) { |
| 2014 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 2015 | } |
| 2016 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2017 | /* Empty password is not valid */ |
| 2018 | if ((pw == NULL) || (pw_len == 0)) { |
| 2019 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2020 | } |
| 2021 | |
| 2022 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 2023 | psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE); |
| 2024 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 2025 | |
| 2026 | status = psa_import_key(&attributes, pw, pw_len, |
| 2027 | &ssl->handshake->psa_pake_password); |
| 2028 | if (status != PSA_SUCCESS) { |
| 2029 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 2030 | } |
| 2031 | |
| 2032 | status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, |
| 2033 | ssl->handshake->psa_pake_password); |
| 2034 | if (status != PSA_SUCCESS) { |
| 2035 | psa_destroy_key(ssl->handshake->psa_pake_password); |
| 2036 | psa_pake_abort(&ssl->handshake->psa_pake_ctx); |
| 2037 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 2038 | } |
| 2039 | |
| 2040 | return 0; |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 2041 | } |
Valerio Setti | a9a97dc | 2022-11-28 18:26:16 +0100 | [diff] [blame] | 2042 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2043 | int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl, |
| 2044 | mbedtls_svc_key_id_t pwd) |
Valerio Setti | a9a97dc | 2022-11-28 18:26:16 +0100 | [diff] [blame] | 2045 | { |
Valerio Setti | a9a97dc | 2022-11-28 18:26:16 +0100 | [diff] [blame] | 2046 | psa_status_t status; |
| 2047 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2048 | if (ssl->handshake == NULL || ssl->conf == NULL) { |
| 2049 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 2050 | } |
| 2051 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2052 | if (mbedtls_svc_key_id_is_null(pwd)) { |
| 2053 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2054 | } |
| 2055 | |
| 2056 | status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd); |
| 2057 | if (status != PSA_SUCCESS) { |
| 2058 | psa_pake_abort(&ssl->handshake->psa_pake_ctx); |
| 2059 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 2060 | } |
| 2061 | |
| 2062 | return 0; |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 2063 | } |
| 2064 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2065 | int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl, |
| 2066 | const unsigned char *pw, |
| 2067 | size_t pw_len) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2068 | { |
| 2069 | mbedtls_ecjpake_role role; |
| 2070 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2071 | if (ssl->handshake == NULL || ssl->conf == NULL) { |
| 2072 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2073 | } |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2074 | |
Valerio Setti | c689ed8 | 2022-12-07 14:40:38 +0100 | [diff] [blame] | 2075 | /* Empty password is not valid */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2076 | if ((pw == NULL) || (pw_len == 0)) { |
| 2077 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2078 | } |
Valerio Setti | c689ed8 | 2022-12-07 14:40:38 +0100 | [diff] [blame] | 2079 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2080 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2081 | role = MBEDTLS_ECJPAKE_SERVER; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2082 | } else { |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2083 | role = MBEDTLS_ECJPAKE_CLIENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2084 | } |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2085 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2086 | return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx, |
| 2087 | role, |
| 2088 | MBEDTLS_MD_SHA256, |
| 2089 | MBEDTLS_ECP_DP_SECP256R1, |
| 2090 | pw, pw_len); |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2091 | } |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 2092 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 2093 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2094 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 2095 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2096 | int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2097 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2098 | if (conf->psk_identity == NULL || |
| 2099 | conf->psk_identity_len == 0) { |
| 2100 | return 0; |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 2101 | } |
| 2102 | |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2103 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2104 | if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) { |
| 2105 | return 1; |
| 2106 | } |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2107 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 2108 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2109 | if (conf->psk != NULL && conf->psk_len != 0) { |
| 2110 | return 1; |
| 2111 | } |
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 | return 0; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2114 | } |
| 2115 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2116 | static void ssl_conf_remove_psk(mbedtls_ssl_config *conf) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2117 | { |
| 2118 | /* Remove reference to existing PSK, if any. */ |
| 2119 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2120 | if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) { |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2121 | /* The maintenance of the PSK key slot is the |
| 2122 | * user's responsibility. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2123 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2124 | } |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2125 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2126 | if (conf->psk != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 2127 | mbedtls_zeroize_and_free(conf->psk, conf->psk_len); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2128 | conf->psk = NULL; |
| 2129 | conf->psk_len = 0; |
| 2130 | } |
| 2131 | |
| 2132 | /* Remove reference to PSK identity, if any. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2133 | if (conf->psk_identity != NULL) { |
| 2134 | mbedtls_free(conf->psk_identity); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2135 | conf->psk_identity = NULL; |
| 2136 | conf->psk_identity_len = 0; |
| 2137 | } |
| 2138 | } |
| 2139 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2140 | /* This function assumes that PSK identity in the SSL config is unset. |
| 2141 | * It checks that the provided identity is well-formed and attempts |
| 2142 | * to make a copy of it in the SSL config. |
| 2143 | * On failure, the PSK identity in the config remains unset. */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 2144 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2145 | static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf, |
| 2146 | unsigned char const *psk_identity, |
| 2147 | size_t psk_identity_len) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2148 | { |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 2149 | /* Identity len will be encoded on two bytes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2150 | if (psk_identity == NULL || |
Ronald Cron | 2a87e9b | 2022-10-19 10:55:26 +0200 | [diff] [blame] | 2151 | psk_identity_len == 0 || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2152 | (psk_identity_len >> 16) != 0 || |
| 2153 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) { |
| 2154 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 2155 | } |
| 2156 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2157 | conf->psk_identity = mbedtls_calloc(1, psk_identity_len); |
| 2158 | if (conf->psk_identity == NULL) { |
| 2159 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2160 | } |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 2161 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 2162 | conf->psk_identity_len = psk_identity_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2163 | memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len); |
Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 2164 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2165 | return 0; |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 2166 | } |
| 2167 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2168 | int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf, |
| 2169 | const unsigned char *psk, size_t psk_len, |
| 2170 | const unsigned char *psk_identity, size_t psk_identity_len) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2171 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2172 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2173 | |
| 2174 | /* We currently only support one PSK, raw or opaque. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2175 | if (mbedtls_ssl_conf_has_static_psk(conf)) { |
| 2176 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 2177 | } |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2178 | |
| 2179 | /* Check and set raw PSK */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2180 | if (psk == NULL) { |
| 2181 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2182 | } |
| 2183 | if (psk_len == 0) { |
| 2184 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2185 | } |
| 2186 | if (psk_len > MBEDTLS_PSK_MAX_LEN) { |
| 2187 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2188 | } |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 2189 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2190 | if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) { |
| 2191 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2192 | } |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2193 | conf->psk_len = psk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2194 | memcpy(conf->psk, psk, conf->psk_len); |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2195 | |
| 2196 | /* Check and set PSK Identity */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2197 | ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len); |
| 2198 | if (ret != 0) { |
| 2199 | ssl_conf_remove_psk(conf); |
| 2200 | } |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2201 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2202 | return ret; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2203 | } |
| 2204 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2205 | static void ssl_remove_psk(mbedtls_ssl_context *ssl) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2206 | { |
| 2207 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2208 | if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2209 | /* The maintenance of the external PSK key slot is the |
| 2210 | * user's responsibility. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2211 | if (ssl->handshake->psk_opaque_is_internal) { |
| 2212 | psa_destroy_key(ssl->handshake->psk_opaque); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2213 | ssl->handshake->psk_opaque_is_internal = 0; |
| 2214 | } |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2215 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2216 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 2217 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2218 | if (ssl->handshake->psk != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 2219 | mbedtls_zeroize_and_free(ssl->handshake->psk, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2220 | ssl->handshake->psk_len); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2221 | ssl->handshake->psk_len = 0; |
lhuang04 | 54adeab | 2024-06-10 12:16:29 -0700 | [diff] [blame] | 2222 | ssl->handshake->psk = NULL; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2223 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 2224 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2225 | } |
| 2226 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2227 | int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl, |
| 2228 | const unsigned char *psk, size_t psk_len) |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2229 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2230 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2231 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
Jerry Yu | 5d01c05 | 2022-08-17 10:18:10 +0800 | [diff] [blame] | 2232 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 24b8c81 | 2022-08-20 19:06:56 +0800 | [diff] [blame] | 2233 | psa_algorithm_t alg = PSA_ALG_NONE; |
Jerry Yu | 5d01c05 | 2022-08-17 10:18:10 +0800 | [diff] [blame] | 2234 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2235 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2236 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2237 | if (psk == NULL || ssl->handshake == NULL) { |
| 2238 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2239 | } |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2240 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2241 | if (psk_len > MBEDTLS_PSK_MAX_LEN) { |
| 2242 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2243 | } |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2244 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2245 | ssl_remove_psk(ssl); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2246 | |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2247 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 2248 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2249 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) { |
| 2250 | if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) { |
| 2251 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
| 2252 | } else { |
| 2253 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 2254 | } |
| 2255 | psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE); |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 2256 | } |
| 2257 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2258 | |
Jerry Yu | 568ec25 | 2022-07-22 21:27:34 +0800 | [diff] [blame] | 2259 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2260 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
| 2261 | alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH); |
| 2262 | psa_set_key_usage_flags(&key_attributes, |
| 2263 | PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT); |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 2264 | } |
| 2265 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 2266 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2267 | psa_set_key_algorithm(&key_attributes, alg); |
| 2268 | psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2269 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2270 | status = psa_import_key(&key_attributes, psk, psk_len, &key); |
| 2271 | if (status != PSA_SUCCESS) { |
| 2272 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 2273 | } |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2274 | |
| 2275 | /* Allow calling psa_destroy_key() on psk remove */ |
| 2276 | ssl->handshake->psk_opaque_is_internal = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2277 | return mbedtls_ssl_set_hs_psk_opaque(ssl, key); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2278 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2279 | if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) { |
| 2280 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2281 | } |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2282 | |
| 2283 | ssl->handshake->psk_len = psk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2284 | memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2285 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2286 | return 0; |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2287 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2288 | } |
| 2289 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2290 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2291 | int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf, |
| 2292 | mbedtls_svc_key_id_t psk, |
| 2293 | const unsigned char *psk_identity, |
| 2294 | size_t psk_identity_len) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2295 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2296 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2297 | |
| 2298 | /* We currently only support one PSK, raw or opaque. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2299 | if (mbedtls_ssl_conf_has_static_psk(conf)) { |
| 2300 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 2301 | } |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2302 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2303 | /* Check and set opaque PSK */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2304 | if (mbedtls_svc_key_id_is_null(psk)) { |
| 2305 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2306 | } |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2307 | conf->psk_opaque = psk; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2308 | |
| 2309 | /* Check and set PSK Identity */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2310 | ret = ssl_conf_set_psk_identity(conf, psk_identity, |
| 2311 | psk_identity_len); |
| 2312 | if (ret != 0) { |
| 2313 | ssl_conf_remove_psk(conf); |
| 2314 | } |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2315 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2316 | return ret; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2317 | } |
| 2318 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2319 | int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl, |
| 2320 | mbedtls_svc_key_id_t psk) |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2321 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2322 | if ((mbedtls_svc_key_id_is_null(psk)) || |
| 2323 | (ssl->handshake == NULL)) { |
| 2324 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2325 | } |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2326 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2327 | ssl_remove_psk(ssl); |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2328 | ssl->handshake->psk_opaque = psk; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2329 | return 0; |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2330 | } |
| 2331 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2332 | |
Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 2333 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2334 | void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf, |
| 2335 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
| 2336 | size_t), |
| 2337 | void *p_psk) |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2338 | { |
| 2339 | conf->f_psk = f_psk; |
| 2340 | conf->p_psk = p_psk; |
| 2341 | } |
Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 2342 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 2343 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 2344 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2345 | |
| 2346 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2347 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2348 | psa_algorithm_t alg) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2349 | { |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2350 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2351 | if (alg == PSA_ALG_CBC_NO_PADDING) { |
| 2352 | return MBEDTLS_SSL_MODE_CBC; |
| 2353 | } |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2354 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2355 | if (PSA_ALG_IS_AEAD(alg)) { |
| 2356 | return MBEDTLS_SSL_MODE_AEAD; |
| 2357 | } |
| 2358 | return MBEDTLS_SSL_MODE_STREAM; |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2359 | } |
| 2360 | |
| 2361 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2362 | |
| 2363 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2364 | mbedtls_cipher_mode_t mode) |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2365 | { |
| 2366 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2367 | if (mode == MBEDTLS_MODE_CBC) { |
| 2368 | return MBEDTLS_SSL_MODE_CBC; |
| 2369 | } |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2370 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 2371 | |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2372 | #if defined(MBEDTLS_GCM_C) || \ |
| 2373 | defined(MBEDTLS_CCM_C) || \ |
| 2374 | defined(MBEDTLS_CHACHAPOLY_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2375 | if (mode == MBEDTLS_MODE_GCM || |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2376 | mode == MBEDTLS_MODE_CCM || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2377 | mode == MBEDTLS_MODE_CHACHAPOLY) { |
| 2378 | return MBEDTLS_SSL_MODE_AEAD; |
| 2379 | } |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2380 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2381 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2382 | return MBEDTLS_SSL_MODE_STREAM; |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2383 | } |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2384 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2385 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2386 | static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode( |
| 2387 | mbedtls_ssl_mode_t base_mode, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2388 | int encrypt_then_mac) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2389 | { |
| 2390 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2391 | if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED && |
| 2392 | base_mode == MBEDTLS_SSL_MODE_CBC) { |
| 2393 | return MBEDTLS_SSL_MODE_CBC_ETM; |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2394 | } |
| 2395 | #else |
| 2396 | (void) encrypt_then_mac; |
| 2397 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2398 | return base_mode; |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2399 | } |
| 2400 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 2401 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2402 | const mbedtls_ssl_transform *transform) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2403 | { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2404 | mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode( |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2405 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2406 | transform->psa_alg |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2407 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2408 | mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2409 | #endif |
| 2410 | ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2411 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2412 | int encrypt_then_mac = 0; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2413 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2414 | encrypt_then_mac = transform->encrypt_then_mac; |
| 2415 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2416 | return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2417 | } |
| 2418 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 2419 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2420 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2421 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2422 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2423 | const mbedtls_ssl_ciphersuite_t *suite) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2424 | { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2425 | mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM; |
| 2426 | |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2427 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2428 | psa_status_t status; |
| 2429 | psa_algorithm_t alg; |
| 2430 | psa_key_type_t type; |
| 2431 | size_t size; |
Dave Rodgman | 2eab462 | 2023-10-05 13:30:37 +0100 | [diff] [blame] | 2432 | status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) suite->cipher, |
| 2433 | 0, &alg, &type, &size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2434 | if (status == PSA_SUCCESS) { |
| 2435 | base_mode = mbedtls_ssl_get_base_mode(alg); |
| 2436 | } |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2437 | #else |
| 2438 | const mbedtls_cipher_info_t *cipher = |
Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 2439 | mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) suite->cipher); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2440 | if (cipher != NULL) { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2441 | base_mode = |
| 2442 | mbedtls_ssl_get_base_mode( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2443 | mbedtls_cipher_info_get_mode(cipher)); |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2444 | } |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2445 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2446 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2447 | #if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 2448 | int encrypt_then_mac = 0; |
| 2449 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2450 | return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2451 | } |
| 2452 | |
Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 2453 | #if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2454 | |
Ronald Cron | 8b592d2 | 2024-11-12 18:08:25 +0100 | [diff] [blame] | 2455 | const mbedtls_error_pair_t psa_to_ssl_errors[] = |
| 2456 | { |
| 2457 | { PSA_SUCCESS, 0 }, |
| 2458 | { PSA_ERROR_INSUFFICIENT_MEMORY, MBEDTLS_ERR_SSL_ALLOC_FAILED }, |
| 2459 | { PSA_ERROR_NOT_SUPPORTED, MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE }, |
| 2460 | { PSA_ERROR_INVALID_SIGNATURE, MBEDTLS_ERR_SSL_INVALID_MAC }, |
| 2461 | { PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_SSL_BAD_INPUT_DATA }, |
| 2462 | { PSA_ERROR_BAD_STATE, MBEDTLS_ERR_SSL_INTERNAL_ERROR }, |
| 2463 | { PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL } |
| 2464 | }; |
| 2465 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2466 | psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type, |
| 2467 | size_t taglen, |
| 2468 | psa_algorithm_t *alg, |
| 2469 | psa_key_type_t *key_type, |
| 2470 | size_t *key_size) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2471 | { |
Elena Uziunaite | c256172 | 2024-07-05 11:37:33 +0100 | [diff] [blame] | 2472 | #if !defined(PSA_WANT_ALG_CCM) |
Yanray Wang | 19e4dc8 | 2023-11-16 18:02:05 +0800 | [diff] [blame] | 2473 | (void) taglen; |
| 2474 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2475 | switch (mbedtls_cipher_type) { |
Elena Uziunaite | 74342c7 | 2024-07-05 11:31:29 +0100 | [diff] [blame] | 2476 | #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] | 2477 | case MBEDTLS_CIPHER_AES_128_CBC: |
| 2478 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2479 | *key_type = PSA_KEY_TYPE_AES; |
| 2480 | *key_size = 128; |
| 2481 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2482 | #endif |
Elena Uziunaite | c256172 | 2024-07-05 11:37:33 +0100 | [diff] [blame] | 2483 | #if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2484 | case MBEDTLS_CIPHER_AES_128_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2485 | *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] | 2486 | *key_type = PSA_KEY_TYPE_AES; |
| 2487 | *key_size = 128; |
| 2488 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2489 | #endif |
Elena Uziunaite | 83a0d9d | 2024-07-05 11:41:22 +0100 | [diff] [blame] | 2490 | #if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_GCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2491 | case MBEDTLS_CIPHER_AES_128_GCM: |
| 2492 | *alg = PSA_ALG_GCM; |
| 2493 | *key_type = PSA_KEY_TYPE_AES; |
| 2494 | *key_size = 128; |
| 2495 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2496 | #endif |
Elena Uziunaite | c256172 | 2024-07-05 11:37:33 +0100 | [diff] [blame] | 2497 | #if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2498 | case MBEDTLS_CIPHER_AES_192_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2499 | *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] | 2500 | *key_type = PSA_KEY_TYPE_AES; |
| 2501 | *key_size = 192; |
| 2502 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2503 | #endif |
Elena Uziunaite | 83a0d9d | 2024-07-05 11:41:22 +0100 | [diff] [blame] | 2504 | #if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_GCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2505 | case MBEDTLS_CIPHER_AES_192_GCM: |
| 2506 | *alg = PSA_ALG_GCM; |
| 2507 | *key_type = PSA_KEY_TYPE_AES; |
| 2508 | *key_size = 192; |
| 2509 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2510 | #endif |
Elena Uziunaite | 74342c7 | 2024-07-05 11:31:29 +0100 | [diff] [blame] | 2511 | #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] | 2512 | case MBEDTLS_CIPHER_AES_256_CBC: |
| 2513 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2514 | *key_type = PSA_KEY_TYPE_AES; |
| 2515 | *key_size = 256; |
| 2516 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2517 | #endif |
Elena Uziunaite | c256172 | 2024-07-05 11:37:33 +0100 | [diff] [blame] | 2518 | #if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2519 | case MBEDTLS_CIPHER_AES_256_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2520 | *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] | 2521 | *key_type = PSA_KEY_TYPE_AES; |
| 2522 | *key_size = 256; |
| 2523 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2524 | #endif |
Elena Uziunaite | 83a0d9d | 2024-07-05 11:41:22 +0100 | [diff] [blame] | 2525 | #if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_GCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2526 | case MBEDTLS_CIPHER_AES_256_GCM: |
| 2527 | *alg = PSA_ALG_GCM; |
| 2528 | *key_type = PSA_KEY_TYPE_AES; |
| 2529 | *key_size = 256; |
| 2530 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2531 | #endif |
Elena Uziunaite | 51c85a0 | 2024-07-05 11:20:17 +0100 | [diff] [blame] | 2532 | #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] | 2533 | case MBEDTLS_CIPHER_ARIA_128_CBC: |
| 2534 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2535 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2536 | *key_size = 128; |
| 2537 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2538 | #endif |
Elena Uziunaite | 51c85a0 | 2024-07-05 11:20:17 +0100 | [diff] [blame] | 2539 | #if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2540 | case MBEDTLS_CIPHER_ARIA_128_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2541 | *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] | 2542 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2543 | *key_size = 128; |
| 2544 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2545 | #endif |
Elena Uziunaite | 51c85a0 | 2024-07-05 11:20:17 +0100 | [diff] [blame] | 2546 | #if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_GCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2547 | case MBEDTLS_CIPHER_ARIA_128_GCM: |
| 2548 | *alg = PSA_ALG_GCM; |
| 2549 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2550 | *key_size = 128; |
| 2551 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2552 | #endif |
Elena Uziunaite | 51c85a0 | 2024-07-05 11:20:17 +0100 | [diff] [blame] | 2553 | #if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2554 | case MBEDTLS_CIPHER_ARIA_192_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2555 | *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] | 2556 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2557 | *key_size = 192; |
| 2558 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2559 | #endif |
Elena Uziunaite | 51c85a0 | 2024-07-05 11:20:17 +0100 | [diff] [blame] | 2560 | #if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_GCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2561 | case MBEDTLS_CIPHER_ARIA_192_GCM: |
| 2562 | *alg = PSA_ALG_GCM; |
| 2563 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2564 | *key_size = 192; |
| 2565 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2566 | #endif |
Elena Uziunaite | 51c85a0 | 2024-07-05 11:20:17 +0100 | [diff] [blame] | 2567 | #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] | 2568 | case MBEDTLS_CIPHER_ARIA_256_CBC: |
| 2569 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2570 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2571 | *key_size = 256; |
| 2572 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2573 | #endif |
Elena Uziunaite | 51c85a0 | 2024-07-05 11:20:17 +0100 | [diff] [blame] | 2574 | #if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2575 | case MBEDTLS_CIPHER_ARIA_256_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2576 | *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] | 2577 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2578 | *key_size = 256; |
| 2579 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2580 | #endif |
Elena Uziunaite | 51c85a0 | 2024-07-05 11:20:17 +0100 | [diff] [blame] | 2581 | #if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_GCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2582 | case MBEDTLS_CIPHER_ARIA_256_GCM: |
| 2583 | *alg = PSA_ALG_GCM; |
| 2584 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2585 | *key_size = 256; |
| 2586 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2587 | #endif |
Elena Uziunaite | da41b60 | 2024-07-05 11:27:21 +0100 | [diff] [blame] | 2588 | #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] | 2589 | case MBEDTLS_CIPHER_CAMELLIA_128_CBC: |
| 2590 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2591 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2592 | *key_size = 128; |
| 2593 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2594 | #endif |
Elena Uziunaite | da41b60 | 2024-07-05 11:27:21 +0100 | [diff] [blame] | 2595 | #if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2596 | case MBEDTLS_CIPHER_CAMELLIA_128_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2597 | *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] | 2598 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2599 | *key_size = 128; |
| 2600 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2601 | #endif |
Elena Uziunaite | da41b60 | 2024-07-05 11:27:21 +0100 | [diff] [blame] | 2602 | #if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_GCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2603 | case MBEDTLS_CIPHER_CAMELLIA_128_GCM: |
| 2604 | *alg = PSA_ALG_GCM; |
| 2605 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2606 | *key_size = 128; |
| 2607 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2608 | #endif |
Elena Uziunaite | da41b60 | 2024-07-05 11:27:21 +0100 | [diff] [blame] | 2609 | #if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2610 | case MBEDTLS_CIPHER_CAMELLIA_192_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2611 | *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] | 2612 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2613 | *key_size = 192; |
| 2614 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2615 | #endif |
Elena Uziunaite | da41b60 | 2024-07-05 11:27:21 +0100 | [diff] [blame] | 2616 | #if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_GCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2617 | case MBEDTLS_CIPHER_CAMELLIA_192_GCM: |
| 2618 | *alg = PSA_ALG_GCM; |
| 2619 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2620 | *key_size = 192; |
| 2621 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2622 | #endif |
Elena Uziunaite | da41b60 | 2024-07-05 11:27:21 +0100 | [diff] [blame] | 2623 | #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] | 2624 | case MBEDTLS_CIPHER_CAMELLIA_256_CBC: |
| 2625 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2626 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2627 | *key_size = 256; |
| 2628 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2629 | #endif |
Elena Uziunaite | da41b60 | 2024-07-05 11:27:21 +0100 | [diff] [blame] | 2630 | #if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_CCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2631 | case MBEDTLS_CIPHER_CAMELLIA_256_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2632 | *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] | 2633 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2634 | *key_size = 256; |
| 2635 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2636 | #endif |
Elena Uziunaite | da41b60 | 2024-07-05 11:27:21 +0100 | [diff] [blame] | 2637 | #if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_GCM) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2638 | case MBEDTLS_CIPHER_CAMELLIA_256_GCM: |
| 2639 | *alg = PSA_ALG_GCM; |
| 2640 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2641 | *key_size = 256; |
| 2642 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2643 | #endif |
Elena Uziunaite | 5c70c30 | 2024-07-05 11:44:44 +0100 | [diff] [blame] | 2644 | #if defined(PSA_WANT_ALG_CHACHA20_POLY1305) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2645 | case MBEDTLS_CIPHER_CHACHA20_POLY1305: |
| 2646 | *alg = PSA_ALG_CHACHA20_POLY1305; |
| 2647 | *key_type = PSA_KEY_TYPE_CHACHA20; |
| 2648 | *key_size = 256; |
| 2649 | break; |
Yanray Wang | 1a369d6 | 2023-11-16 15:17:31 +0800 | [diff] [blame] | 2650 | #endif |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2651 | case MBEDTLS_CIPHER_NULL: |
| 2652 | *alg = MBEDTLS_SSL_NULL_CIPHER; |
| 2653 | *key_type = 0; |
| 2654 | *key_size = 0; |
| 2655 | break; |
| 2656 | default: |
| 2657 | return PSA_ERROR_NOT_SUPPORTED; |
| 2658 | } |
| 2659 | |
| 2660 | return PSA_SUCCESS; |
| 2661 | } |
Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 2662 | #endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2663 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2664 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2665 | int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf, |
| 2666 | const unsigned char *dhm_P, size_t P_len, |
| 2667 | const unsigned char *dhm_G, size_t G_len) |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2668 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2669 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2670 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2671 | mbedtls_mpi_free(&conf->dhm_P); |
| 2672 | mbedtls_mpi_free(&conf->dhm_G); |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2673 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2674 | if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 || |
| 2675 | (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) { |
| 2676 | mbedtls_mpi_free(&conf->dhm_P); |
| 2677 | mbedtls_mpi_free(&conf->dhm_G); |
| 2678 | return ret; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2679 | } |
| 2680 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2681 | return 0; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2682 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2683 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2684 | 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] | 2685 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2686 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2687 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2688 | mbedtls_mpi_free(&conf->dhm_P); |
| 2689 | mbedtls_mpi_free(&conf->dhm_G); |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2690 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2691 | if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P, |
| 2692 | &conf->dhm_P)) != 0 || |
| 2693 | (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G, |
| 2694 | &conf->dhm_G)) != 0) { |
| 2695 | mbedtls_mpi_free(&conf->dhm_P); |
| 2696 | mbedtls_mpi_free(&conf->dhm_G); |
| 2697 | return ret; |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 2698 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2699 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2700 | return 0; |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2701 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2702 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2703 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 2704 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 2705 | /* |
| 2706 | * Set the minimum length for Diffie-Hellman parameters |
| 2707 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2708 | void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf, |
| 2709 | unsigned int bitlen) |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 2710 | { |
| 2711 | conf->dhm_min_bitlen = bitlen; |
| 2712 | } |
| 2713 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 2714 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2715 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2716 | #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] | 2717 | /* |
| 2718 | * Set allowed/preferred hashes for handshake signatures |
| 2719 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2720 | void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf, |
| 2721 | const int *hashes) |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 2722 | { |
| 2723 | conf->sig_hashes = hashes; |
| 2724 | } |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2725 | #endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2726 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2727 | /* Configure allowed signature algorithms for handshake */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2728 | void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf, |
| 2729 | const uint16_t *sig_algs) |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2730 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2731 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2732 | conf->sig_hashes = NULL; |
| 2733 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 2734 | conf->sig_algs = sig_algs; |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2735 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2736 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 2737 | |
Manuel Pégourié-Gonnard | f07ce3b | 2023-09-22 11:53:41 +0200 | [diff] [blame] | 2738 | #if defined(MBEDTLS_ECP_C) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2739 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2740 | /* |
| 2741 | * Set the allowed elliptic curves |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2742 | * |
| 2743 | * mbedtls_ssl_setup() takes the provided list |
| 2744 | * and translates it to a list of IANA TLS group identifiers, |
| 2745 | * stored in ssl->handshake->group_list. |
| 2746 | * |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2747 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2748 | void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf, |
| 2749 | const mbedtls_ecp_group_id *curve_list) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2750 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2751 | conf->curve_list = curve_list; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2752 | conf->group_list = NULL; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2753 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2754 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Manuel Pégourié-Gonnard | f07ce3b | 2023-09-22 11:53:41 +0200 | [diff] [blame] | 2755 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2756 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2757 | /* |
| 2758 | * Set the allowed groups |
| 2759 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2760 | void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf, |
| 2761 | const uint16_t *group_list) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2762 | { |
Manuel Pégourié-Gonnard | f07ce3b | 2023-09-22 11:53:41 +0200 | [diff] [blame] | 2763 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2764 | conf->curve_list = NULL; |
| 2765 | #endif |
| 2766 | conf->group_list = group_list; |
| 2767 | } |
| 2768 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2769 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2770 | int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2771 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2772 | /* Initialize to suppress unnecessary compiler warning */ |
| 2773 | size_t hostname_len = 0; |
| 2774 | |
| 2775 | /* Check if new hostname is valid before |
| 2776 | * making any change to current one */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2777 | if (hostname != NULL) { |
| 2778 | hostname_len = strlen(hostname); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2779 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2780 | if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) { |
| 2781 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2782 | } |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2783 | } |
| 2784 | |
| 2785 | /* Now it's clear that we will overwrite the old hostname, |
| 2786 | * so we can free it safely */ |
| 2787 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2788 | if (ssl->hostname != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 2789 | mbedtls_zeroize_and_free(ssl->hostname, strlen(ssl->hostname)); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2790 | } |
| 2791 | |
| 2792 | /* Passing NULL as hostname shall clear the old one */ |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 2793 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2794 | if (hostname == NULL) { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2795 | ssl->hostname = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2796 | } else { |
| 2797 | ssl->hostname = mbedtls_calloc(1, hostname_len + 1); |
| 2798 | if (ssl->hostname == NULL) { |
| 2799 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2800 | } |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2801 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2802 | memcpy(ssl->hostname, hostname, hostname_len); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2803 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2804 | ssl->hostname[hostname_len] = '\0'; |
| 2805 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2806 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2807 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2808 | } |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 2809 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2810 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2811 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2812 | void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf, |
| 2813 | int (*f_sni)(void *, mbedtls_ssl_context *, |
| 2814 | const unsigned char *, size_t), |
| 2815 | void *p_sni) |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2816 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2817 | conf->f_sni = f_sni; |
| 2818 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2819 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2820 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2821 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2822 | #if defined(MBEDTLS_SSL_ALPN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2823 | 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] | 2824 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2825 | size_t cur_len, tot_len; |
| 2826 | const char **p; |
| 2827 | |
| 2828 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 2829 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 2830 | * MUST NOT be truncated." |
| 2831 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2832 | */ |
| 2833 | tot_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2834 | for (p = protos; *p != NULL; p++) { |
| 2835 | cur_len = strlen(*p); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2836 | tot_len += cur_len; |
| 2837 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2838 | if ((cur_len == 0) || |
| 2839 | (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) || |
| 2840 | (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) { |
| 2841 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2842 | } |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2843 | } |
| 2844 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2845 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2846 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2847 | return 0; |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2848 | } |
| 2849 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2850 | 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] | 2851 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2852 | return ssl->alpn_chosen; |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2853 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2854 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2855 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2856 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2857 | void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf, |
| 2858 | int support_mki_value) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2859 | { |
| 2860 | conf->dtls_srtp_mki_support = support_mki_value; |
| 2861 | } |
| 2862 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2863 | int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl, |
| 2864 | unsigned char *mki_value, |
| 2865 | uint16_t mki_len) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2866 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2867 | if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) { |
| 2868 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2869 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2870 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2871 | if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) { |
| 2872 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2873 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2874 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2875 | memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len); |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2876 | ssl->dtls_srtp_info.mki_len = mki_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2877 | return 0; |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2878 | } |
| 2879 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2880 | int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf, |
| 2881 | const mbedtls_ssl_srtp_profile *profiles) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2882 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2883 | const mbedtls_ssl_srtp_profile *p; |
| 2884 | size_t list_size = 0; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2885 | |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2886 | /* check the profiles list: all entry must be valid, |
| 2887 | * 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] | 2888 | for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET && |
| 2889 | list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH; |
| 2890 | p++) { |
| 2891 | if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) { |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 2892 | list_size++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2893 | } else { |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 2894 | /* unsupported value, stop parsing and set the size to an error value */ |
| 2895 | list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2896 | } |
| 2897 | } |
| 2898 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2899 | if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) { |
| 2900 | conf->dtls_srtp_profile_list = NULL; |
| 2901 | conf->dtls_srtp_profile_list_len = 0; |
| 2902 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2903 | } |
| 2904 | |
Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 2905 | conf->dtls_srtp_profile_list = profiles; |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2906 | conf->dtls_srtp_profile_list_len = list_size; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2907 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2908 | return 0; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2909 | } |
| 2910 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2911 | void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl, |
| 2912 | mbedtls_dtls_srtp_info *dtls_srtp_info) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2913 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2914 | dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile; |
| 2915 | /* do not copy the mki value if there is no chosen profile */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2916 | if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2917 | dtls_srtp_info->mki_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2918 | } else { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2919 | dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2920 | memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value, |
| 2921 | ssl->dtls_srtp_info.mki_len); |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2922 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2923 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2924 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 2925 | |
Aditya Patwardhan | 3096f33 | 2022-07-26 14:31:46 +0530 | [diff] [blame] | 2926 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2927 | 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] | 2928 | { |
Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 2929 | conf->max_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor); |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 2930 | } |
| 2931 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2932 | 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] | 2933 | { |
Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 2934 | conf->min_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor); |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2935 | } |
Aditya Patwardhan | 3096f33 | 2022-07-26 14:31:46 +0530 | [diff] [blame] | 2936 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2937 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 2938 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2939 | void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf, |
| 2940 | char cert_req_ca_list) |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 2941 | { |
| 2942 | conf->cert_req_ca_list = cert_req_ca_list; |
| 2943 | } |
| 2944 | #endif |
| 2945 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2946 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2947 | 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] | 2948 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2949 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2950 | } |
| 2951 | #endif |
| 2952 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2953 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2954 | 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] | 2955 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2956 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2957 | } |
| 2958 | #endif |
| 2959 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2960 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2961 | 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] | 2962 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2963 | if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
| 2964 | ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) { |
| 2965 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2966 | } |
| 2967 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 2968 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2969 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2970 | return 0; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2971 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2972 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2973 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2974 | 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] | 2975 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2976 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2977 | } |
| 2978 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2979 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2980 | 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] | 2981 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2982 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2983 | } |
| 2984 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2985 | 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] | 2986 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2987 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 2988 | } |
| 2989 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2990 | void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf, |
| 2991 | const unsigned char period[8]) |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 2992 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2993 | memcpy(conf->renego_period, period, 8); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 2994 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2995 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2996 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2997 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2998 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2999 | 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] | 3000 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 3001 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 3002 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3003 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 3004 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3005 | #if defined(MBEDTLS_SSL_SRV_C) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 3006 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 3007 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3008 | void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf, |
| 3009 | uint16_t num_tickets) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 3010 | { |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 3011 | conf->new_session_tickets_count = num_tickets; |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 3012 | } |
| 3013 | #endif |
| 3014 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3015 | void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf, |
| 3016 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 3017 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 3018 | void *p_ticket) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 3019 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 3020 | conf->f_ticket_write = f_ticket_write; |
| 3021 | conf->f_ticket_parse = f_ticket_parse; |
| 3022 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 3023 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3024 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3025 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 3026 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3027 | void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl, |
| 3028 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 3029 | void *p_export_keys) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 3030 | { |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 3031 | ssl->f_export_keys = f_export_keys; |
| 3032 | ssl->p_export_keys = p_export_keys; |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 3033 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 3034 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3035 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3036 | void mbedtls_ssl_conf_async_private_cb( |
| 3037 | mbedtls_ssl_config *conf, |
| 3038 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 3039 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 3040 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 3041 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3042 | void *async_config_data) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3043 | { |
| 3044 | conf->f_async_sign_start = f_async_sign; |
| 3045 | conf->f_async_decrypt_start = f_async_decrypt; |
| 3046 | conf->f_async_resume = f_async_resume; |
| 3047 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3048 | conf->p_async_config_data = async_config_data; |
| 3049 | } |
| 3050 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3051 | 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] | 3052 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3053 | return conf->p_async_config_data; |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3054 | } |
| 3055 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3056 | void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3057 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3058 | if (ssl->handshake == NULL) { |
| 3059 | return NULL; |
| 3060 | } else { |
| 3061 | return ssl->handshake->user_async_ctx; |
| 3062 | } |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3063 | } |
| 3064 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3065 | void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl, |
| 3066 | void *ctx) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3067 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3068 | if (ssl->handshake != NULL) { |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3069 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3070 | } |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3071 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3072 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3073 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3074 | /* |
| 3075 | * SSL get accessors |
| 3076 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3077 | uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3078 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3079 | if (ssl->session != NULL) { |
| 3080 | return ssl->session->verify_result; |
| 3081 | } |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 3082 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3083 | if (ssl->session_negotiate != NULL) { |
| 3084 | return ssl->session_negotiate->verify_result; |
| 3085 | } |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 3086 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3087 | return 0xFFFFFFFF; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3088 | } |
| 3089 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3090 | 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] | 3091 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3092 | if (ssl == NULL || ssl->session == NULL) { |
| 3093 | return 0; |
| 3094 | } |
Glenn Strauss | 8f52690 | 2022-01-13 00:04:49 -0500 | [diff] [blame] | 3095 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3096 | return ssl->session->ciphersuite; |
Glenn Strauss | 8f52690 | 2022-01-13 00:04:49 -0500 | [diff] [blame] | 3097 | } |
| 3098 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3099 | const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 3100 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3101 | if (ssl == NULL || ssl->session == NULL) { |
| 3102 | return NULL; |
| 3103 | } |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 3104 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3105 | return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 3106 | } |
| 3107 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3108 | const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3109 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3110 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3111 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 3112 | switch (ssl->tls_version) { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3113 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3114 | return "DTLSv1.2"; |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 3115 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3116 | return "unknown (DTLS)"; |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 3117 | } |
| 3118 | } |
| 3119 | #endif |
| 3120 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3121 | switch (ssl->tls_version) { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3122 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3123 | return "TLSv1.2"; |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3124 | case MBEDTLS_SSL_VERSION_TLS1_3: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3125 | return "TLSv1.3"; |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3126 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3127 | return "unknown"; |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3128 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3129 | } |
| 3130 | |
Waleed Elmelegy | 6a971fd | 2023-12-28 17:48:16 +0000 | [diff] [blame] | 3131 | #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) |
| 3132 | |
| 3133 | size_t mbedtls_ssl_get_output_record_size_limit(const mbedtls_ssl_context *ssl) |
| 3134 | { |
| 3135 | const size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 3136 | size_t record_size_limit = max_len; |
| 3137 | |
| 3138 | if (ssl->session != NULL && |
| 3139 | ssl->session->record_size_limit >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN && |
| 3140 | ssl->session->record_size_limit < max_len) { |
| 3141 | record_size_limit = ssl->session->record_size_limit; |
| 3142 | } |
| 3143 | |
| 3144 | // TODO: this is currently untested |
| 3145 | /* During a handshake, use the value being negotiated */ |
| 3146 | if (ssl->session_negotiate != NULL && |
| 3147 | ssl->session_negotiate->record_size_limit >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN && |
| 3148 | ssl->session_negotiate->record_size_limit < max_len) { |
| 3149 | record_size_limit = ssl->session_negotiate->record_size_limit; |
| 3150 | } |
| 3151 | |
| 3152 | return record_size_limit; |
| 3153 | } |
| 3154 | #endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */ |
| 3155 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 3156 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3157 | 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] | 3158 | { |
David Horstmann | 95d516f | 2021-05-04 18:36:56 +0100 | [diff] [blame] | 3159 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3160 | size_t read_mfl; |
| 3161 | |
Jerry Yu | ddda050 | 2022-12-01 19:43:12 +0800 | [diff] [blame] | 3162 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3163 | /* 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] | 3164 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 3165 | ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) { |
| 3166 | return ssl_mfl_code_to_length(ssl->conf->mfl_code); |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3167 | } |
Jerry Yu | ddda050 | 2022-12-01 19:43:12 +0800 | [diff] [blame] | 3168 | #endif |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3169 | |
| 3170 | /* Check if a smaller max length was negotiated */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3171 | if (ssl->session_out != NULL) { |
| 3172 | read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code); |
| 3173 | if (read_mfl < max_len) { |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3174 | max_len = read_mfl; |
| 3175 | } |
| 3176 | } |
| 3177 | |
Jerry Yu | ddda050 | 2022-12-01 19:43:12 +0800 | [diff] [blame] | 3178 | /* During a handshake, use the value being negotiated */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3179 | if (ssl->session_negotiate != NULL) { |
| 3180 | read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code); |
| 3181 | if (read_mfl < max_len) { |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3182 | max_len = read_mfl; |
| 3183 | } |
| 3184 | } |
| 3185 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3186 | return max_len; |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3187 | } |
| 3188 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3189 | 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] | 3190 | { |
| 3191 | size_t max_len; |
| 3192 | |
| 3193 | /* |
| 3194 | * Assume mfl_code is correct since it was checked when set |
| 3195 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3196 | 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] | 3197 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 3198 | /* Check if a smaller max length was negotiated */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3199 | if (ssl->session_out != NULL && |
| 3200 | ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) { |
| 3201 | 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] | 3202 | } |
| 3203 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 3204 | /* During a handshake, use the value being negotiated */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3205 | if (ssl->session_negotiate != NULL && |
| 3206 | ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) { |
| 3207 | 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] | 3208 | } |
| 3209 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3210 | return max_len; |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 3211 | } |
| 3212 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 3213 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3214 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3215 | 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] | 3216 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 3217 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3218 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 3219 | (ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 3220 | ssl->state == MBEDTLS_SSL_SERVER_HELLO)) { |
| 3221 | return 0; |
| 3222 | } |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 3223 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3224 | if (ssl->handshake == NULL || ssl->handshake->mtu == 0) { |
| 3225 | return ssl->mtu; |
| 3226 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3227 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3228 | if (ssl->mtu == 0) { |
| 3229 | return ssl->handshake->mtu; |
| 3230 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3231 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3232 | return ssl->mtu < ssl->handshake->mtu ? |
| 3233 | ssl->mtu : ssl->handshake->mtu; |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3234 | } |
| 3235 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3236 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3237 | 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] | 3238 | { |
| 3239 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 3240 | |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 3241 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
Jan Bruckner | f482dcc | 2023-03-15 09:09:06 +0100 | [diff] [blame] | 3242 | !defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) && \ |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 3243 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3244 | (void) ssl; |
| 3245 | #endif |
| 3246 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3247 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3248 | 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] | 3249 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3250 | if (max_len > mfl) { |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3251 | max_len = mfl; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3252 | } |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3253 | #endif |
| 3254 | |
Jan Bruckner | f482dcc | 2023-03-15 09:09:06 +0100 | [diff] [blame] | 3255 | #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) |
| 3256 | const size_t record_size_limit = mbedtls_ssl_get_output_record_size_limit(ssl); |
| 3257 | |
| 3258 | if (max_len > record_size_limit) { |
| 3259 | max_len = record_size_limit; |
| 3260 | } |
| 3261 | #endif |
| 3262 | |
Waleed Elmelegy | 6a971fd | 2023-12-28 17:48:16 +0000 | [diff] [blame] | 3263 | if (ssl->transform_out != NULL && |
| 3264 | ssl->transform_out->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Waleed Elmelegy | f501790 | 2024-01-09 14:18:34 +0000 | [diff] [blame] | 3265 | /* |
| 3266 | * In TLS 1.3 case, when records are protected, `max_len` as computed |
| 3267 | * above is the maximum length of the TLSInnerPlaintext structure that |
| 3268 | * along the plaintext payload contains the inner content type (one byte) |
| 3269 | * and some zero padding. Given the algorithm used for padding |
| 3270 | * in mbedtls_ssl_encrypt_buf(), compute the maximum length for |
| 3271 | * the plaintext payload. Round down to a multiple of |
| 3272 | * MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY and |
| 3273 | * subtract 1. |
Waleed Elmelegy | 6a971fd | 2023-12-28 17:48:16 +0000 | [diff] [blame] | 3274 | */ |
| 3275 | max_len = ((max_len / MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) * |
| 3276 | MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) - 1; |
| 3277 | } |
| 3278 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3279 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3280 | if (mbedtls_ssl_get_current_mtu(ssl) != 0) { |
| 3281 | const size_t mtu = mbedtls_ssl_get_current_mtu(ssl); |
| 3282 | const int ret = mbedtls_ssl_get_record_expansion(ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3283 | const size_t overhead = (size_t) ret; |
| 3284 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3285 | if (ret < 0) { |
| 3286 | return ret; |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3287 | } |
| 3288 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3289 | if (mtu <= overhead) { |
| 3290 | MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion")); |
| 3291 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 3292 | } |
| 3293 | |
| 3294 | if (max_len > mtu - overhead) { |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3295 | max_len = mtu - overhead; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3296 | } |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3297 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3298 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3299 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 3300 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
Waleed Elmelegy | 049cd30 | 2023-12-20 17:28:31 +0000 | [diff] [blame] | 3301 | !defined(MBEDTLS_SSL_PROTO_DTLS) && \ |
| 3302 | !defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 3303 | ((void) ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3304 | #endif |
| 3305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3306 | return (int) max_len; |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3307 | } |
| 3308 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3309 | 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] | 3310 | { |
| 3311 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
| 3312 | |
| 3313 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 3314 | (void) ssl; |
| 3315 | #endif |
| 3316 | |
| 3317 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3318 | const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl); |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 3319 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3320 | if (max_len > mfl) { |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 3321 | max_len = mfl; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3322 | } |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 3323 | #endif |
| 3324 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3325 | return (int) max_len; |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 3326 | } |
| 3327 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3328 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3329 | 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] | 3330 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3331 | if (ssl == NULL || ssl->session == NULL) { |
| 3332 | return NULL; |
| 3333 | } |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3334 | |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 3335 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3336 | return ssl->session->peer_cert; |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 3337 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3338 | return NULL; |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 3339 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3340 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3341 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3342 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3343 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3344 | int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl, |
| 3345 | mbedtls_ssl_session *dst) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3346 | { |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3347 | int ret; |
| 3348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3349 | if (ssl == NULL || |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3350 | dst == NULL || |
| 3351 | ssl->session == NULL || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3352 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) { |
| 3353 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3354 | } |
| 3355 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3356 | /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer |
| 3357 | * idempotent: Each session can only be exported once. |
| 3358 | * |
| 3359 | * (This is in preparation for TLS 1.3 support where we will |
| 3360 | * need the ability to export multiple sessions (aka tickets), |
| 3361 | * which will be achieved by calling mbedtls_ssl_get_session() |
| 3362 | * multiple times until it fails.) |
| 3363 | * |
| 3364 | * Check whether we have already exported the current session, |
| 3365 | * and fail if so. |
| 3366 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3367 | if (ssl->session->exported == 1) { |
| 3368 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 3369 | } |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3371 | ret = mbedtls_ssl_session_copy(dst, ssl->session); |
| 3372 | if (ret != 0) { |
| 3373 | return ret; |
| 3374 | } |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3375 | |
| 3376 | /* Remember that we've exported the session. */ |
| 3377 | ssl->session->exported = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3378 | return 0; |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3379 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3380 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3381 | |
David Horstmann | cb01b36 | 2024-02-29 17:31:46 +0000 | [diff] [blame] | 3382 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3383 | |
| 3384 | /* Serialization of TLS 1.2 sessions |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3385 | * |
David Horstmann | cb01b36 | 2024-02-29 17:31:46 +0000 | [diff] [blame] | 3386 | * For more detail, see the description of ssl_session_save(). |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3387 | */ |
| 3388 | static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session, |
| 3389 | unsigned char *buf, |
| 3390 | size_t buf_len) |
| 3391 | { |
| 3392 | unsigned char *p = buf; |
| 3393 | size_t used = 0; |
| 3394 | |
| 3395 | #if defined(MBEDTLS_HAVE_TIME) |
| 3396 | uint64_t start; |
| 3397 | #endif |
| 3398 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3399 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3400 | size_t cert_len; |
| 3401 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3402 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 3403 | |
| 3404 | /* |
| 3405 | * Time |
| 3406 | */ |
| 3407 | #if defined(MBEDTLS_HAVE_TIME) |
| 3408 | used += 8; |
| 3409 | |
| 3410 | if (used <= buf_len) { |
| 3411 | start = (uint64_t) session->start; |
| 3412 | |
| 3413 | MBEDTLS_PUT_UINT64_BE(start, p, 0); |
| 3414 | p += 8; |
| 3415 | } |
| 3416 | #endif /* MBEDTLS_HAVE_TIME */ |
| 3417 | |
| 3418 | /* |
| 3419 | * Basic mandatory fields |
| 3420 | */ |
| 3421 | used += 1 /* id_len */ |
| 3422 | + sizeof(session->id) |
| 3423 | + sizeof(session->master) |
| 3424 | + 4; /* verify_result */ |
| 3425 | |
| 3426 | if (used <= buf_len) { |
| 3427 | *p++ = MBEDTLS_BYTE_0(session->id_len); |
| 3428 | memcpy(p, session->id, 32); |
| 3429 | p += 32; |
| 3430 | |
| 3431 | memcpy(p, session->master, 48); |
| 3432 | p += 48; |
| 3433 | |
| 3434 | MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0); |
| 3435 | p += 4; |
| 3436 | } |
| 3437 | |
| 3438 | /* |
| 3439 | * Peer's end-entity certificate |
| 3440 | */ |
| 3441 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3442 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3443 | if (session->peer_cert == NULL) { |
| 3444 | cert_len = 0; |
| 3445 | } else { |
| 3446 | cert_len = session->peer_cert->raw.len; |
| 3447 | } |
| 3448 | |
| 3449 | used += 3 + cert_len; |
| 3450 | |
| 3451 | if (used <= buf_len) { |
| 3452 | *p++ = MBEDTLS_BYTE_2(cert_len); |
| 3453 | *p++ = MBEDTLS_BYTE_1(cert_len); |
| 3454 | *p++ = MBEDTLS_BYTE_0(cert_len); |
| 3455 | |
| 3456 | if (session->peer_cert != NULL) { |
| 3457 | memcpy(p, session->peer_cert->raw.p, cert_len); |
| 3458 | p += cert_len; |
| 3459 | } |
| 3460 | } |
| 3461 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3462 | if (session->peer_cert_digest != NULL) { |
| 3463 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; |
| 3464 | if (used <= buf_len) { |
| 3465 | *p++ = (unsigned char) session->peer_cert_digest_type; |
| 3466 | *p++ = (unsigned char) session->peer_cert_digest_len; |
| 3467 | memcpy(p, session->peer_cert_digest, |
| 3468 | session->peer_cert_digest_len); |
| 3469 | p += session->peer_cert_digest_len; |
| 3470 | } |
| 3471 | } else { |
| 3472 | used += 2; |
| 3473 | if (used <= buf_len) { |
| 3474 | *p++ = (unsigned char) MBEDTLS_MD_NONE; |
| 3475 | *p++ = 0; |
| 3476 | } |
| 3477 | } |
| 3478 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3479 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 3480 | |
| 3481 | /* |
| 3482 | * Session ticket if any, plus associated data |
| 3483 | */ |
| 3484 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 3485 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3486 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 3487 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ |
| 3488 | |
| 3489 | if (used <= buf_len) { |
| 3490 | *p++ = MBEDTLS_BYTE_2(session->ticket_len); |
| 3491 | *p++ = MBEDTLS_BYTE_1(session->ticket_len); |
| 3492 | *p++ = MBEDTLS_BYTE_0(session->ticket_len); |
| 3493 | |
| 3494 | if (session->ticket != NULL) { |
| 3495 | memcpy(p, session->ticket, session->ticket_len); |
| 3496 | p += session->ticket_len; |
| 3497 | } |
| 3498 | |
| 3499 | MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0); |
| 3500 | p += 4; |
| 3501 | } |
| 3502 | } |
| 3503 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 3504 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
| 3505 | if (session->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 3506 | used += 8; |
| 3507 | |
| 3508 | if (used <= buf_len) { |
| 3509 | MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_creation_time, p, 0); |
| 3510 | p += 8; |
| 3511 | } |
| 3512 | } |
| 3513 | #endif /* MBEDTLS_HAVE_TIME && MBEDTLS_SSL_SRV_C */ |
| 3514 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 3515 | |
| 3516 | /* |
| 3517 | * Misc extension-related info |
| 3518 | */ |
| 3519 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 3520 | used += 1; |
| 3521 | |
| 3522 | if (used <= buf_len) { |
| 3523 | *p++ = session->mfl_code; |
| 3524 | } |
| 3525 | #endif |
| 3526 | |
| 3527 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 3528 | used += 1; |
| 3529 | |
| 3530 | if (used <= buf_len) { |
| 3531 | *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac); |
| 3532 | } |
| 3533 | #endif |
| 3534 | |
| 3535 | return used; |
| 3536 | } |
| 3537 | |
| 3538 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 3539 | static int ssl_tls12_session_load(mbedtls_ssl_session *session, |
| 3540 | const unsigned char *buf, |
| 3541 | size_t len) |
| 3542 | { |
| 3543 | #if defined(MBEDTLS_HAVE_TIME) |
| 3544 | uint64_t start; |
| 3545 | #endif |
| 3546 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3547 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3548 | size_t cert_len; |
| 3549 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3550 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 3551 | |
| 3552 | const unsigned char *p = buf; |
| 3553 | const unsigned char * const end = buf + len; |
| 3554 | |
| 3555 | /* |
| 3556 | * Time |
| 3557 | */ |
| 3558 | #if defined(MBEDTLS_HAVE_TIME) |
| 3559 | if (8 > (size_t) (end - p)) { |
| 3560 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3561 | } |
| 3562 | |
| 3563 | start = MBEDTLS_GET_UINT64_BE(p, 0); |
| 3564 | p += 8; |
| 3565 | |
| 3566 | session->start = (time_t) start; |
| 3567 | #endif /* MBEDTLS_HAVE_TIME */ |
| 3568 | |
| 3569 | /* |
| 3570 | * Basic mandatory fields |
| 3571 | */ |
| 3572 | if (1 + 32 + 48 + 4 > (size_t) (end - p)) { |
| 3573 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3574 | } |
| 3575 | |
| 3576 | session->id_len = *p++; |
| 3577 | memcpy(session->id, p, 32); |
| 3578 | p += 32; |
| 3579 | |
| 3580 | memcpy(session->master, p, 48); |
| 3581 | p += 48; |
| 3582 | |
| 3583 | session->verify_result = MBEDTLS_GET_UINT32_BE(p, 0); |
| 3584 | p += 4; |
| 3585 | |
| 3586 | /* Immediately clear invalid pointer values that have been read, in case |
| 3587 | * we exit early before we replaced them with valid ones. */ |
| 3588 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3589 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3590 | session->peer_cert = NULL; |
| 3591 | #else |
| 3592 | session->peer_cert_digest = NULL; |
| 3593 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3594 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 3595 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 3596 | session->ticket = NULL; |
| 3597 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 3598 | |
| 3599 | /* |
| 3600 | * Peer certificate |
| 3601 | */ |
| 3602 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3603 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3604 | /* Deserialize CRT from the end of the ticket. */ |
| 3605 | if (3 > (size_t) (end - p)) { |
| 3606 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3607 | } |
| 3608 | |
| 3609 | cert_len = MBEDTLS_GET_UINT24_BE(p, 0); |
| 3610 | p += 3; |
| 3611 | |
| 3612 | if (cert_len != 0) { |
| 3613 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3614 | |
| 3615 | if (cert_len > (size_t) (end - p)) { |
| 3616 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3617 | } |
| 3618 | |
| 3619 | session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt)); |
| 3620 | |
| 3621 | if (session->peer_cert == NULL) { |
| 3622 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 3623 | } |
| 3624 | |
| 3625 | mbedtls_x509_crt_init(session->peer_cert); |
| 3626 | |
| 3627 | if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert, |
| 3628 | p, cert_len)) != 0) { |
| 3629 | mbedtls_x509_crt_free(session->peer_cert); |
| 3630 | mbedtls_free(session->peer_cert); |
| 3631 | session->peer_cert = NULL; |
| 3632 | return ret; |
| 3633 | } |
| 3634 | |
| 3635 | p += cert_len; |
| 3636 | } |
| 3637 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3638 | /* Deserialize CRT digest from the end of the ticket. */ |
| 3639 | if (2 > (size_t) (end - p)) { |
| 3640 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3641 | } |
| 3642 | |
| 3643 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; |
| 3644 | session->peer_cert_digest_len = (size_t) *p++; |
| 3645 | |
| 3646 | if (session->peer_cert_digest_len != 0) { |
| 3647 | const mbedtls_md_info_t *md_info = |
| 3648 | mbedtls_md_info_from_type(session->peer_cert_digest_type); |
| 3649 | if (md_info == NULL) { |
| 3650 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3651 | } |
| 3652 | if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) { |
| 3653 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3654 | } |
| 3655 | |
| 3656 | if (session->peer_cert_digest_len > (size_t) (end - p)) { |
| 3657 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3658 | } |
| 3659 | |
| 3660 | session->peer_cert_digest = |
| 3661 | mbedtls_calloc(1, session->peer_cert_digest_len); |
| 3662 | if (session->peer_cert_digest == NULL) { |
| 3663 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 3664 | } |
| 3665 | |
| 3666 | memcpy(session->peer_cert_digest, p, |
| 3667 | session->peer_cert_digest_len); |
| 3668 | p += session->peer_cert_digest_len; |
| 3669 | } |
| 3670 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3671 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 3672 | |
| 3673 | /* |
| 3674 | * Session ticket and associated data |
| 3675 | */ |
| 3676 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 3677 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3678 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 3679 | if (3 > (size_t) (end - p)) { |
| 3680 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3681 | } |
| 3682 | |
| 3683 | session->ticket_len = MBEDTLS_GET_UINT24_BE(p, 0); |
| 3684 | p += 3; |
| 3685 | |
| 3686 | if (session->ticket_len != 0) { |
| 3687 | if (session->ticket_len > (size_t) (end - p)) { |
| 3688 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3689 | } |
| 3690 | |
| 3691 | session->ticket = mbedtls_calloc(1, session->ticket_len); |
| 3692 | if (session->ticket == NULL) { |
| 3693 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 3694 | } |
| 3695 | |
| 3696 | memcpy(session->ticket, p, session->ticket_len); |
| 3697 | p += session->ticket_len; |
| 3698 | } |
| 3699 | |
| 3700 | if (4 > (size_t) (end - p)) { |
| 3701 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3702 | } |
| 3703 | |
| 3704 | session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0); |
| 3705 | p += 4; |
| 3706 | } |
| 3707 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 3708 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
| 3709 | if (session->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 3710 | if (8 > (size_t) (end - p)) { |
| 3711 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3712 | } |
| 3713 | session->ticket_creation_time = MBEDTLS_GET_UINT64_BE(p, 0); |
| 3714 | p += 8; |
| 3715 | } |
| 3716 | #endif /* MBEDTLS_HAVE_TIME && MBEDTLS_SSL_SRV_C */ |
| 3717 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 3718 | |
| 3719 | /* |
| 3720 | * Misc extension-related info |
| 3721 | */ |
| 3722 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 3723 | if (1 > (size_t) (end - p)) { |
| 3724 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3725 | } |
| 3726 | |
| 3727 | session->mfl_code = *p++; |
| 3728 | #endif |
| 3729 | |
| 3730 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 3731 | if (1 > (size_t) (end - p)) { |
| 3732 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3733 | } |
| 3734 | |
| 3735 | session->encrypt_then_mac = *p++; |
| 3736 | #endif |
| 3737 | |
| 3738 | /* Done, should have consumed entire buffer */ |
| 3739 | if (p != end) { |
| 3740 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3741 | } |
| 3742 | |
| 3743 | return 0; |
| 3744 | } |
| 3745 | |
| 3746 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3747 | |
| 3748 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3749 | /* Serialization of TLS 1.3 sessions: |
| 3750 | * |
David Horstmann | cb01b36 | 2024-02-29 17:31:46 +0000 | [diff] [blame] | 3751 | * For more detail, see the description of ssl_session_save(). |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3752 | */ |
| 3753 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 3754 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 3755 | static int ssl_tls13_session_save(const mbedtls_ssl_session *session, |
| 3756 | unsigned char *buf, |
| 3757 | size_t buf_len, |
| 3758 | size_t *olen) |
| 3759 | { |
| 3760 | unsigned char *p = buf; |
| 3761 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
| 3762 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 3763 | size_t hostname_len = (session->hostname == NULL) ? |
| 3764 | 0 : strlen(session->hostname) + 1; |
| 3765 | #endif |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3766 | |
| 3767 | #if defined(MBEDTLS_SSL_SRV_C) && \ |
| 3768 | defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) |
Waleed Elmelegy | daa4da7 | 2024-03-13 16:25:34 +0000 | [diff] [blame] | 3769 | const size_t alpn_len = (session->ticket_alpn == NULL) ? |
Waleed Elmelegy | b28ab0a | 2024-03-13 16:48:28 +0000 | [diff] [blame] | 3770 | 0 : strlen(session->ticket_alpn) + 1; |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3771 | #endif |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3772 | size_t needed = 4 /* ticket_age_add */ |
| 3773 | + 1 /* ticket_flags */ |
| 3774 | + 1; /* resumption_key length */ |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3775 | |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3776 | *olen = 0; |
| 3777 | |
| 3778 | if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) { |
| 3779 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3780 | } |
| 3781 | needed += session->resumption_key_len; /* resumption_key */ |
| 3782 | |
| 3783 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 3784 | needed += 4; /* max_early_data_size */ |
| 3785 | #endif |
| 3786 | #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) |
| 3787 | needed += 2; /* record_size_limit */ |
| 3788 | #endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */ |
| 3789 | |
| 3790 | #if defined(MBEDTLS_HAVE_TIME) |
| 3791 | needed += 8; /* ticket_creation_time or ticket_reception_time */ |
| 3792 | #endif |
| 3793 | |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3794 | #if defined(MBEDTLS_SSL_SRV_C) |
| 3795 | if (session->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 3796 | #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) |
Waleed Elmelegy | daa4da7 | 2024-03-13 16:25:34 +0000 | [diff] [blame] | 3797 | needed += 2 /* alpn_len */ |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3798 | + alpn_len; /* alpn */ |
| 3799 | #endif |
| 3800 | } |
| 3801 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 3802 | |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3803 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3804 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 3805 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 3806 | needed += 2 /* hostname_len */ |
| 3807 | + hostname_len; /* hostname */ |
| 3808 | #endif |
| 3809 | |
| 3810 | needed += 4 /* ticket_lifetime */ |
| 3811 | + 2; /* ticket_len */ |
| 3812 | |
| 3813 | /* Check size_t overflow */ |
| 3814 | if (session->ticket_len > SIZE_MAX - needed) { |
| 3815 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3816 | } |
| 3817 | |
| 3818 | needed += session->ticket_len; /* ticket */ |
| 3819 | } |
| 3820 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 3821 | |
| 3822 | *olen = needed; |
| 3823 | if (needed > buf_len) { |
| 3824 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 3825 | } |
| 3826 | |
| 3827 | MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 0); |
| 3828 | p[4] = session->ticket_flags; |
| 3829 | |
| 3830 | /* save resumption_key */ |
| 3831 | p[5] = session->resumption_key_len; |
| 3832 | p += 6; |
| 3833 | memcpy(p, session->resumption_key, session->resumption_key_len); |
| 3834 | p += session->resumption_key_len; |
| 3835 | |
| 3836 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 3837 | MBEDTLS_PUT_UINT32_BE(session->max_early_data_size, p, 0); |
| 3838 | p += 4; |
| 3839 | #endif |
| 3840 | #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) |
| 3841 | MBEDTLS_PUT_UINT16_BE(session->record_size_limit, p, 0); |
| 3842 | p += 2; |
| 3843 | #endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */ |
| 3844 | |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3845 | #if defined(MBEDTLS_SSL_SRV_C) |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3846 | if (session->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3847 | #if defined(MBEDTLS_HAVE_TIME) |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3848 | MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_creation_time, p, 0); |
| 3849 | p += 8; |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3850 | #endif /* MBEDTLS_HAVE_TIME */ |
| 3851 | |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3852 | #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) |
Waleed Elmelegy | daa4da7 | 2024-03-13 16:25:34 +0000 | [diff] [blame] | 3853 | MBEDTLS_PUT_UINT16_BE(alpn_len, p, 0); |
| 3854 | p += 2; |
| 3855 | |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3856 | if (alpn_len > 0) { |
| 3857 | /* save chosen alpn */ |
Waleed Elmelegy | 883f77c | 2024-03-06 19:09:41 +0000 | [diff] [blame] | 3858 | memcpy(p, session->ticket_alpn, alpn_len); |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3859 | p += alpn_len; |
| 3860 | } |
| 3861 | #endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */ |
| 3862 | } |
| 3863 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 3864 | |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3865 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3866 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 3867 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 3868 | MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0); |
| 3869 | p += 2; |
| 3870 | if (hostname_len > 0) { |
| 3871 | /* save host name */ |
| 3872 | memcpy(p, session->hostname, hostname_len); |
| 3873 | p += hostname_len; |
| 3874 | } |
| 3875 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 3876 | |
| 3877 | #if defined(MBEDTLS_HAVE_TIME) |
| 3878 | MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_reception_time, p, 0); |
| 3879 | p += 8; |
| 3880 | #endif |
| 3881 | MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0); |
| 3882 | p += 4; |
| 3883 | |
| 3884 | MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0); |
| 3885 | p += 2; |
| 3886 | |
| 3887 | if (session->ticket != NULL && session->ticket_len > 0) { |
| 3888 | memcpy(p, session->ticket, session->ticket_len); |
| 3889 | p += session->ticket_len; |
| 3890 | } |
| 3891 | } |
| 3892 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 3893 | return 0; |
| 3894 | } |
| 3895 | |
| 3896 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 3897 | static int ssl_tls13_session_load(mbedtls_ssl_session *session, |
| 3898 | const unsigned char *buf, |
| 3899 | size_t len) |
| 3900 | { |
| 3901 | const unsigned char *p = buf; |
| 3902 | const unsigned char *end = buf + len; |
| 3903 | |
| 3904 | if (end - p < 6) { |
| 3905 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3906 | } |
| 3907 | session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 0); |
| 3908 | session->ticket_flags = p[4]; |
| 3909 | |
| 3910 | /* load resumption_key */ |
| 3911 | session->resumption_key_len = p[5]; |
| 3912 | p += 6; |
| 3913 | |
| 3914 | if (end - p < session->resumption_key_len) { |
| 3915 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3916 | } |
| 3917 | |
| 3918 | if (sizeof(session->resumption_key) < session->resumption_key_len) { |
| 3919 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3920 | } |
| 3921 | memcpy(session->resumption_key, p, session->resumption_key_len); |
| 3922 | p += session->resumption_key_len; |
| 3923 | |
| 3924 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 3925 | if (end - p < 4) { |
| 3926 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3927 | } |
| 3928 | session->max_early_data_size = MBEDTLS_GET_UINT32_BE(p, 0); |
| 3929 | p += 4; |
| 3930 | #endif |
| 3931 | #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) |
| 3932 | if (end - p < 2) { |
| 3933 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3934 | } |
| 3935 | session->record_size_limit = MBEDTLS_GET_UINT16_BE(p, 0); |
| 3936 | p += 2; |
| 3937 | #endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */ |
| 3938 | |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3939 | #if defined(MBEDTLS_SSL_SRV_C) |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3940 | if (session->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3941 | #if defined(MBEDTLS_HAVE_TIME) |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3942 | if (end - p < 8) { |
| 3943 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3944 | } |
| 3945 | session->ticket_creation_time = MBEDTLS_GET_UINT64_BE(p, 0); |
| 3946 | p += 8; |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3947 | #endif /* MBEDTLS_HAVE_TIME */ |
| 3948 | |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3949 | #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) |
Waleed Elmelegy | daa4da7 | 2024-03-13 16:25:34 +0000 | [diff] [blame] | 3950 | size_t alpn_len; |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3951 | |
Waleed Elmelegy | daa4da7 | 2024-03-13 16:25:34 +0000 | [diff] [blame] | 3952 | if (end - p < 2) { |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3953 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3954 | } |
Waleed Elmelegy | 883f77c | 2024-03-06 19:09:41 +0000 | [diff] [blame] | 3955 | |
Waleed Elmelegy | daa4da7 | 2024-03-13 16:25:34 +0000 | [diff] [blame] | 3956 | alpn_len = MBEDTLS_GET_UINT16_BE(p, 0); |
| 3957 | p += 2; |
| 3958 | |
| 3959 | if (end - p < (long int) alpn_len) { |
Waleed Elmelegy | 7dfba34 | 2024-03-12 16:22:39 +0000 | [diff] [blame] | 3960 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3961 | } |
| 3962 | |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3963 | if (alpn_len > 0) { |
Waleed Elmelegy | daa4da7 | 2024-03-13 16:25:34 +0000 | [diff] [blame] | 3964 | int ret = mbedtls_ssl_session_set_ticket_alpn(session, (char *) p); |
| 3965 | if (ret != 0) { |
| 3966 | return ret; |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3967 | } |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 3968 | p += alpn_len; |
| 3969 | } |
| 3970 | #endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */ |
| 3971 | } |
| 3972 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 3973 | |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 3974 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3975 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 3976 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 3977 | size_t hostname_len; |
| 3978 | /* load host name */ |
| 3979 | if (end - p < 2) { |
| 3980 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3981 | } |
| 3982 | hostname_len = MBEDTLS_GET_UINT16_BE(p, 0); |
| 3983 | p += 2; |
| 3984 | |
| 3985 | if (end - p < (long int) hostname_len) { |
| 3986 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3987 | } |
| 3988 | if (hostname_len > 0) { |
| 3989 | session->hostname = mbedtls_calloc(1, hostname_len); |
| 3990 | if (session->hostname == NULL) { |
| 3991 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 3992 | } |
| 3993 | memcpy(session->hostname, p, hostname_len); |
| 3994 | p += hostname_len; |
| 3995 | } |
| 3996 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 3997 | |
| 3998 | #if defined(MBEDTLS_HAVE_TIME) |
| 3999 | if (end - p < 8) { |
| 4000 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4001 | } |
| 4002 | session->ticket_reception_time = MBEDTLS_GET_UINT64_BE(p, 0); |
| 4003 | p += 8; |
| 4004 | #endif |
| 4005 | if (end - p < 4) { |
| 4006 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4007 | } |
| 4008 | session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0); |
| 4009 | p += 4; |
| 4010 | |
| 4011 | if (end - p < 2) { |
| 4012 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4013 | } |
| 4014 | session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0); |
| 4015 | p += 2; |
| 4016 | |
| 4017 | if (end - p < (long int) session->ticket_len) { |
| 4018 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4019 | } |
| 4020 | if (session->ticket_len > 0) { |
| 4021 | session->ticket = mbedtls_calloc(1, session->ticket_len); |
| 4022 | if (session->ticket == NULL) { |
| 4023 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 4024 | } |
| 4025 | memcpy(session->ticket, p, session->ticket_len); |
| 4026 | p += session->ticket_len; |
| 4027 | } |
| 4028 | } |
| 4029 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 4030 | |
| 4031 | return 0; |
| 4032 | |
| 4033 | } |
| 4034 | #else /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 4035 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 4036 | static int ssl_tls13_session_save(const mbedtls_ssl_session *session, |
| 4037 | unsigned char *buf, |
| 4038 | size_t buf_len, |
| 4039 | size_t *olen) |
| 4040 | { |
| 4041 | ((void) session); |
| 4042 | ((void) buf); |
| 4043 | ((void) buf_len); |
| 4044 | *olen = 0; |
| 4045 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 4046 | } |
| 4047 | |
| 4048 | static int ssl_tls13_session_load(const mbedtls_ssl_session *session, |
Norbert Fabritius | d36913a | 2023-01-24 17:48:29 +0100 | [diff] [blame] | 4049 | const unsigned char *buf, |
David Horstmann | e59f970 | 2024-02-29 16:08:57 +0000 | [diff] [blame] | 4050 | size_t buf_len) |
| 4051 | { |
| 4052 | ((void) session); |
| 4053 | ((void) buf); |
| 4054 | ((void) buf_len); |
| 4055 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 4056 | } |
| 4057 | #endif /* !MBEDTLS_SSL_SESSION_TICKETS */ |
| 4058 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 4059 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4060 | /* |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 4061 | * Define ticket header determining Mbed TLS version |
| 4062 | * and structure of the ticket. |
| 4063 | */ |
| 4064 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4065 | /* |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 4066 | * Define bitflag determining compile-time settings influencing |
| 4067 | * structure of serialized SSL sessions. |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4068 | */ |
| 4069 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 4070 | #if defined(MBEDTLS_HAVE_TIME) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4071 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 4072 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4073 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4074 | #endif /* MBEDTLS_HAVE_TIME */ |
| 4075 | |
| 4076 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4077 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4078 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4079 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4080 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 4081 | |
David Horstmann | 5c5a32f | 2024-02-13 17:53:35 +0000 | [diff] [blame] | 4082 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
David Horstmann | f686f1d | 2024-03-01 11:20:32 +0000 | [diff] [blame] | 4083 | #define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 1 |
David Horstmann | 5c5a32f | 2024-02-13 17:53:35 +0000 | [diff] [blame] | 4084 | #else |
David Horstmann | f686f1d | 2024-03-01 11:20:32 +0000 | [diff] [blame] | 4085 | #define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 0 |
David Horstmann | 5c5a32f | 2024-02-13 17:53:35 +0000 | [diff] [blame] | 4086 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 4087 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4088 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4089 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4090 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4091 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4092 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ |
| 4093 | |
| 4094 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4095 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4096 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4097 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4098 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 4099 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4100 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4101 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4102 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4103 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4104 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 4105 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4106 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4107 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 |
| 4108 | #else |
| 4109 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 |
| 4110 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 4111 | |
David Horstmann | 92b258b | 2024-02-13 17:23:34 +0000 | [diff] [blame] | 4112 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 4113 | #define SSL_SERIALIZED_SESSION_CONFIG_SNI 1 |
| 4114 | #else |
| 4115 | #define SSL_SERIALIZED_SESSION_CONFIG_SNI 0 |
| 4116 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 4117 | |
| 4118 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 4119 | #define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA 1 |
| 4120 | #else |
| 4121 | #define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA 0 |
| 4122 | #endif /* MBEDTLS_SSL_EARLY_DATA */ |
| 4123 | |
| 4124 | #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) |
| 4125 | #define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE 1 |
| 4126 | #else |
| 4127 | #define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE 0 |
| 4128 | #endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */ |
| 4129 | |
Waleed Elmelegy | 7dfba34 | 2024-03-12 16:22:39 +0000 | [diff] [blame] | 4130 | #if defined(MBEDTLS_SSL_ALPN) && defined(MBEDTLS_SSL_SRV_C) && \ |
| 4131 | defined(MBEDTLS_SSL_EARLY_DATA) |
Waleed Elmelegy | 1102563 | 2024-03-07 15:25:52 +0000 | [diff] [blame] | 4132 | #define SSL_SERIALIZED_SESSION_CONFIG_ALPN 1 |
| 4133 | #else |
| 4134 | #define SSL_SERIALIZED_SESSION_CONFIG_ALPN 0 |
| 4135 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4136 | |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4137 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0 |
| 4138 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1 |
| 4139 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 |
| 4140 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3 |
Hanno Becker | 37bdbe6 | 2021-08-01 05:38:58 +0100 | [diff] [blame] | 4141 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4 |
| 4142 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5 |
David Horstmann | f686f1d | 2024-03-01 11:20:32 +0000 | [diff] [blame] | 4143 | #define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT 6 |
David Horstmann | 92b258b | 2024-02-13 17:23:34 +0000 | [diff] [blame] | 4144 | #define SSL_SERIALIZED_SESSION_CONFIG_SNI_BIT 7 |
| 4145 | #define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA_BIT 8 |
| 4146 | #define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE_BIT 9 |
Waleed Elmelegy | 1102563 | 2024-03-07 15:25:52 +0000 | [diff] [blame] | 4147 | #define SSL_SERIALIZED_SESSION_CONFIG_ALPN_BIT 10 |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4148 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 4149 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4150 | ((uint16_t) ( \ |
| 4151 | (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \ |
| 4152 | (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \ |
| 4153 | (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \ |
| 4154 | SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \ |
| 4155 | (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \ |
| 4156 | (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \ |
David Horstmann | 5c5a32f | 2024-02-13 17:53:35 +0000 | [diff] [blame] | 4157 | (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT) | \ |
David Horstmann | 71fa1a9 | 2024-03-01 12:32:18 +0000 | [diff] [blame] | 4158 | (SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT << \ |
| 4159 | SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT) | \ |
David Horstmann | 92b258b | 2024-02-13 17:23:34 +0000 | [diff] [blame] | 4160 | (SSL_SERIALIZED_SESSION_CONFIG_SNI << SSL_SERIALIZED_SESSION_CONFIG_SNI_BIT) | \ |
| 4161 | (SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA << \ |
| 4162 | SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA_BIT) | \ |
| 4163 | (SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE << \ |
Waleed Elmelegy | 1102563 | 2024-03-07 15:25:52 +0000 | [diff] [blame] | 4164 | SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE_BIT) | \ |
Waleed Elmelegy | 75e33fa | 2024-03-07 16:57:58 +0000 | [diff] [blame] | 4165 | (SSL_SERIALIZED_SESSION_CONFIG_ALPN << \ |
Waleed Elmelegy | 1102563 | 2024-03-07 15:25:52 +0000 | [diff] [blame] | 4166 | SSL_SERIALIZED_SESSION_CONFIG_ALPN_BIT))) |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4167 | |
Chien Wong | 4e9683e | 2023-12-28 17:07:43 +0800 | [diff] [blame] | 4168 | static const unsigned char ssl_serialized_session_header[] = { |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4169 | MBEDTLS_VERSION_MAJOR, |
| 4170 | MBEDTLS_VERSION_MINOR, |
| 4171 | MBEDTLS_VERSION_PATCH, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4172 | MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG), |
| 4173 | MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG), |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 4174 | }; |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 4175 | |
| 4176 | /* |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4177 | * Serialize a session in the following format: |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4178 | * (in the presentation language of TLS, RFC 8446 section 3) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4179 | * |
David Horstmann | cb01b36 | 2024-02-29 17:31:46 +0000 | [diff] [blame] | 4180 | * TLS 1.2 session: |
| 4181 | * |
| 4182 | * struct { |
| 4183 | * #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4184 | * opaque ticket<0..2^24-1>; // length 0 means no ticket |
| 4185 | * uint32 ticket_lifetime; |
| 4186 | * #endif |
| 4187 | * } ClientOnlyData; |
| 4188 | * |
| 4189 | * struct { |
| 4190 | * #if defined(MBEDTLS_HAVE_TIME) |
| 4191 | * uint64 start_time; |
| 4192 | * #endif |
| 4193 | * uint8 session_id_len; // at most 32 |
| 4194 | * opaque session_id[32]; |
| 4195 | * opaque master[48]; // fixed length in the standard |
| 4196 | * uint32 verify_result; |
| 4197 | * #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE |
| 4198 | * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert |
| 4199 | * #else |
David Horstmann | 76ba26a | 2024-03-01 12:03:35 +0000 | [diff] [blame] | 4200 | * uint8 peer_cert_digest_type; |
David Horstmann | cb01b36 | 2024-02-29 17:31:46 +0000 | [diff] [blame] | 4201 | * opaque peer_cert_digest<0..2^8-1> |
| 4202 | * #endif |
| 4203 | * select (endpoint) { |
| 4204 | * case client: ClientOnlyData; |
| 4205 | * case server: uint64 ticket_creation_time; |
| 4206 | * }; |
| 4207 | * #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 4208 | * uint8 mfl_code; // up to 255 according to standard |
| 4209 | * #endif |
| 4210 | * #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 4211 | * uint8 encrypt_then_mac; // 0 or 1 |
| 4212 | * #endif |
| 4213 | * } serialized_session_tls12; |
| 4214 | * |
| 4215 | * |
| 4216 | * TLS 1.3 Session: |
| 4217 | * |
| 4218 | * struct { |
| 4219 | * #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 4220 | * opaque hostname<0..2^16-1>; |
| 4221 | * #endif |
| 4222 | * #if defined(MBEDTLS_HAVE_TIME) |
| 4223 | * uint64 ticket_reception_time; |
| 4224 | * #endif |
| 4225 | * uint32 ticket_lifetime; |
| 4226 | * opaque ticket<1..2^16-1>; |
| 4227 | * } ClientOnlyData; |
| 4228 | * |
| 4229 | * struct { |
| 4230 | * uint32 ticket_age_add; |
| 4231 | * uint8 ticket_flags; |
| 4232 | * opaque resumption_key<0..255>; |
| 4233 | * #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 4234 | * uint32 max_early_data_size; |
| 4235 | * #endif |
| 4236 | * #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) |
| 4237 | * uint16 record_size_limit; |
| 4238 | * #endif |
| 4239 | * select ( endpoint ) { |
| 4240 | * case client: ClientOnlyData; |
Waleed Elmelegy | 7dfba34 | 2024-03-12 16:22:39 +0000 | [diff] [blame] | 4241 | * case server: |
David Horstmann | cb01b36 | 2024-02-29 17:31:46 +0000 | [diff] [blame] | 4242 | * #if defined(MBEDTLS_HAVE_TIME) |
Waleed Elmelegy | 7dfba34 | 2024-03-12 16:22:39 +0000 | [diff] [blame] | 4243 | * uint64 ticket_creation_time; |
David Horstmann | cb01b36 | 2024-02-29 17:31:46 +0000 | [diff] [blame] | 4244 | * #endif |
Waleed Elmelegy | fe9ae08 | 2024-03-07 15:47:31 +0000 | [diff] [blame] | 4245 | * #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) |
Waleed Elmelegy | daa4da7 | 2024-03-13 16:25:34 +0000 | [diff] [blame] | 4246 | * opaque ticket_alpn<0..256>; |
David Horstmann | cb01b36 | 2024-02-29 17:31:46 +0000 | [diff] [blame] | 4247 | * #endif |
| 4248 | * }; |
| 4249 | * } serialized_session_tls13; |
| 4250 | * |
| 4251 | * |
| 4252 | * SSL session: |
| 4253 | * |
| 4254 | * struct { |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 4255 | * |
Hanno Becker | dce5097 | 2021-08-01 05:39:23 +0100 | [diff] [blame] | 4256 | * opaque mbedtls_version[3]; // library version: major, minor, patch |
| 4257 | * opaque session_format[2]; // library-version specific 16-bit field |
| 4258 | * // determining the format of the remaining |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4259 | * // serialized data. |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 4260 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4261 | * Note: When updating the format, remember to keep |
| 4262 | * these version+format bytes. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4263 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4264 | * // In this version, `session_format` determines |
| 4265 | * // the setting of those compile-time |
| 4266 | * // configuration options which influence |
| 4267 | * // the structure of mbedtls_ssl_session. |
| 4268 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 4269 | * uint8_t minor_ver; // Protocol minor version. Possible values: |
Jerry Yu | 0c2a738 | 2022-12-06 13:27:25 +0800 | [diff] [blame] | 4270 | * // - TLS 1.2 (0x0303) |
| 4271 | * // - TLS 1.3 (0x0304) |
David Horstmann | 531aca2 | 2024-02-29 18:14:28 +0000 | [diff] [blame] | 4272 | * uint8_t endpoint; |
| 4273 | * uint16_t ciphersuite; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4274 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 4275 | * select (serialized_session.tls_version) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4276 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 4277 | * case MBEDTLS_SSL_VERSION_TLS1_2: |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4278 | * serialized_session_tls12 data; |
Jerry Yu | 0c2a738 | 2022-12-06 13:27:25 +0800 | [diff] [blame] | 4279 | * case MBEDTLS_SSL_VERSION_TLS1_3: |
Jerry Yu | ddda050 | 2022-12-01 19:43:12 +0800 | [diff] [blame] | 4280 | * serialized_session_tls13 data; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4281 | * |
| 4282 | * }; |
| 4283 | * |
| 4284 | * } serialized_session; |
| 4285 | * |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4286 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4287 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4288 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4289 | static int ssl_session_save(const mbedtls_ssl_session *session, |
| 4290 | unsigned char omit_header, |
| 4291 | unsigned char *buf, |
| 4292 | size_t buf_len, |
| 4293 | size_t *olen) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4294 | { |
| 4295 | unsigned char *p = buf; |
| 4296 | size_t used = 0; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 4297 | size_t remaining_len; |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 4298 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4299 | size_t out_len; |
| 4300 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 4301 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4302 | if (session == NULL) { |
| 4303 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 4304 | } |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 4305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4306 | if (!omit_header) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4307 | /* |
| 4308 | * Add Mbed TLS version identifier |
| 4309 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4310 | used += sizeof(ssl_serialized_session_header); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4311 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4312 | if (used <= buf_len) { |
| 4313 | memcpy(p, ssl_serialized_session_header, |
| 4314 | sizeof(ssl_serialized_session_header)); |
| 4315 | p += sizeof(ssl_serialized_session_header); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4316 | } |
| 4317 | } |
| 4318 | |
| 4319 | /* |
Ronald Cron | 40a4ab0 | 2024-01-15 10:21:30 +0100 | [diff] [blame] | 4320 | * TLS version identifier, endpoint, ciphersuite |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4321 | */ |
Ronald Cron | 40a4ab0 | 2024-01-15 10:21:30 +0100 | [diff] [blame] | 4322 | used += 1 /* TLS version */ |
| 4323 | + 1 /* endpoint */ |
| 4324 | + 2; /* ciphersuite */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4325 | if (used <= buf_len) { |
| 4326 | *p++ = MBEDTLS_BYTE_0(session->tls_version); |
Ronald Cron | 40a4ab0 | 2024-01-15 10:21:30 +0100 | [diff] [blame] | 4327 | *p++ = session->endpoint; |
| 4328 | MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0); |
| 4329 | p += 2; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4330 | } |
| 4331 | |
| 4332 | /* Forward to version-specific serialization routine. */ |
Jerry Yu | fca4d57 | 2022-07-21 10:37:48 +0800 | [diff] [blame] | 4333 | remaining_len = (buf_len >= used) ? buf_len - used : 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4334 | switch (session->tls_version) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4335 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4336 | case MBEDTLS_SSL_VERSION_TLS1_2: |
| 4337 | used += ssl_tls12_session_save(session, p, remaining_len); |
| 4338 | break; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4339 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4340 | |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 4341 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4342 | case MBEDTLS_SSL_VERSION_TLS1_3: |
| 4343 | ret = ssl_tls13_session_save(session, p, remaining_len, &out_len); |
| 4344 | if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) { |
| 4345 | return ret; |
| 4346 | } |
| 4347 | used += out_len; |
| 4348 | break; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 4349 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 4350 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4351 | default: |
| 4352 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4353 | } |
| 4354 | |
| 4355 | *olen = used; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4356 | if (used > buf_len) { |
| 4357 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 4358 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4359 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4360 | return 0; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4361 | } |
| 4362 | |
| 4363 | /* |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4364 | * Public wrapper for ssl_session_save() |
| 4365 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4366 | int mbedtls_ssl_session_save(const mbedtls_ssl_session *session, |
| 4367 | unsigned char *buf, |
| 4368 | size_t buf_len, |
| 4369 | size_t *olen) |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4370 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4371 | return ssl_session_save(session, 0, buf, buf_len, olen); |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4372 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4373 | |
Jerry Yu | f1b23ca | 2022-02-18 11:48:47 +0800 | [diff] [blame] | 4374 | /* |
| 4375 | * Deserialize session, see mbedtls_ssl_session_save() for format. |
| 4376 | * |
| 4377 | * This internal version is wrapped by a public function that cleans up in |
| 4378 | * case of error, and has an extra option omit_header. |
| 4379 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4380 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4381 | static int ssl_session_load(mbedtls_ssl_session *session, |
| 4382 | unsigned char omit_header, |
| 4383 | const unsigned char *buf, |
| 4384 | size_t len) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4385 | { |
| 4386 | const unsigned char *p = buf; |
| 4387 | const unsigned char * const end = buf + len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 4388 | size_t remaining_len; |
| 4389 | |
| 4390 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4391 | if (session == NULL) { |
| 4392 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 4393 | } |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4394 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4395 | if (!omit_header) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4396 | /* |
| 4397 | * Check Mbed TLS version identifier |
| 4398 | */ |
| 4399 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4400 | if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) { |
| 4401 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4402 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4403 | |
| 4404 | if (memcmp(p, ssl_serialized_session_header, |
| 4405 | sizeof(ssl_serialized_session_header)) != 0) { |
| 4406 | return MBEDTLS_ERR_SSL_VERSION_MISMATCH; |
| 4407 | } |
| 4408 | p += sizeof(ssl_serialized_session_header); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4409 | } |
| 4410 | |
| 4411 | /* |
Ronald Cron | 40a4ab0 | 2024-01-15 10:21:30 +0100 | [diff] [blame] | 4412 | * TLS version identifier, endpoint, ciphersuite |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4413 | */ |
Ronald Cron | 40a4ab0 | 2024-01-15 10:21:30 +0100 | [diff] [blame] | 4414 | if (4 > (size_t) (end - p)) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4415 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4416 | } |
Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 4417 | session->tls_version = (mbedtls_ssl_protocol_version) (0x0300 | *p++); |
Ronald Cron | 40a4ab0 | 2024-01-15 10:21:30 +0100 | [diff] [blame] | 4418 | session->endpoint = *p++; |
| 4419 | session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 0); |
| 4420 | p += 2; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4421 | |
| 4422 | /* Dispatch according to TLS version. */ |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 4423 | remaining_len = (size_t) (end - p); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4424 | switch (session->tls_version) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4425 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4426 | case MBEDTLS_SSL_VERSION_TLS1_2: |
| 4427 | return ssl_tls12_session_load(session, p, remaining_len); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4428 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4429 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 4430 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4431 | case MBEDTLS_SSL_VERSION_TLS1_3: |
| 4432 | return ssl_tls13_session_load(session, p, remaining_len); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 4433 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 4434 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4435 | default: |
| 4436 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4437 | } |
| 4438 | } |
| 4439 | |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4440 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 4441 | * Deserialize session: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4442 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4443 | int mbedtls_ssl_session_load(mbedtls_ssl_session *session, |
| 4444 | const unsigned char *buf, |
| 4445 | size_t len) |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4446 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4447 | int ret = ssl_session_load(session, 0, buf, len); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4449 | if (ret != 0) { |
| 4450 | mbedtls_ssl_session_free(session); |
| 4451 | } |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4452 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4453 | return ret; |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4454 | } |
| 4455 | |
| 4456 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4457 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4458 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4459 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4460 | static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl) |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 4461 | { |
| 4462 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 4463 | |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 4464 | /* |
| 4465 | * 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] | 4466 | * that were written into the output buffer by the previous handshake step, |
| 4467 | * if the write to the network callback returned with the |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 4468 | * #MBEDTLS_ERR_SSL_WANT_WRITE error code. |
| 4469 | * We proceed to the next handshake step only when all data from the |
| 4470 | * previous one have been sent to the peer, thus we make sure that this is |
| 4471 | * the case here by calling `mbedtls_ssl_flush_output()`. The function may |
| 4472 | * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case |
| 4473 | * we have to wait before to go ahead. |
| 4474 | * In the case of TLS 1.3, handshake step handlers do not send data to the |
| 4475 | * peer. Data are only sent here and through |
| 4476 | * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an |
Andrzej Kurek | 5c65c57 | 2022-04-13 14:28:52 -0400 | [diff] [blame] | 4477 | * alert occurred. |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 4478 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4479 | if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) { |
| 4480 | return ret; |
| 4481 | } |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 4482 | |
| 4483 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4484 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 4485 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) { |
| 4486 | if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) { |
| 4487 | return ret; |
| 4488 | } |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 4489 | } |
| 4490 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4491 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4492 | return ret; |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 4493 | } |
| 4494 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4495 | int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4496 | { |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 4497 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4498 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4499 | if (ssl == NULL || |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 4500 | ssl->conf == NULL || |
| 4501 | ssl->handshake == NULL || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4502 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 4503 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 4504 | } |
| 4505 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4506 | ret = ssl_prepare_handshake_step(ssl); |
| 4507 | if (ret != 0) { |
| 4508 | return ret; |
| 4509 | } |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 4510 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4511 | ret = mbedtls_ssl_handle_pending_alert(ssl); |
| 4512 | if (ret != 0) { |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 4513 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4514 | } |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 4515 | |
Tom Cosgrove | 2fdc7b3 | 2022-09-21 12:33:17 +0100 | [diff] [blame] | 4516 | /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or |
| 4517 | * MBEDTLS_SSL_IS_SERVER, this is the return code we give */ |
| 4518 | ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4519 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4520 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4521 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 4522 | MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s", |
Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 4523 | mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state))); |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 4524 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4525 | switch (ssl->state) { |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 4526 | case MBEDTLS_SSL_HELLO_REQUEST: |
| 4527 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; |
Tom Cosgrove | 87d9c6c | 2022-09-22 09:27:56 +0100 | [diff] [blame] | 4528 | ret = 0; |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 4529 | break; |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 4530 | |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 4531 | case MBEDTLS_SSL_CLIENT_HELLO: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4532 | ret = mbedtls_ssl_write_client_hello(ssl); |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 4533 | break; |
| 4534 | |
| 4535 | default: |
| 4536 | #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] | 4537 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
| 4538 | ret = mbedtls_ssl_tls13_handshake_client_step(ssl); |
| 4539 | } else { |
| 4540 | ret = mbedtls_ssl_handshake_client_step(ssl); |
| 4541 | } |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 4542 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4543 | ret = mbedtls_ssl_handshake_client_step(ssl); |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 4544 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4545 | ret = mbedtls_ssl_tls13_handshake_client_step(ssl); |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 4546 | #endif |
| 4547 | } |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 4548 | } |
Ronald Cron | 6291b23 | 2023-03-08 15:51:25 +0100 | [diff] [blame] | 4549 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 4550 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4551 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4552 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Ronald Cron | 6291b23 | 2023-03-08 15:51:25 +0100 | [diff] [blame] | 4553 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4554 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4555 | ret = mbedtls_ssl_tls13_handshake_server_step(ssl); |
Ronald Cron | 6291b23 | 2023-03-08 15:51:25 +0100 | [diff] [blame] | 4556 | } else { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4557 | ret = mbedtls_ssl_handshake_server_step(ssl); |
| 4558 | } |
Ronald Cron | 6291b23 | 2023-03-08 15:51:25 +0100 | [diff] [blame] | 4559 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4560 | ret = mbedtls_ssl_handshake_server_step(ssl); |
| 4561 | #else |
| 4562 | ret = mbedtls_ssl_tls13_handshake_server_step(ssl); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4563 | #endif |
Ronald Cron | 6291b23 | 2023-03-08 15:51:25 +0100 | [diff] [blame] | 4564 | } |
| 4565 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4566 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4567 | if (ret != 0) { |
Jerry Yu | bbd5a3f | 2021-09-18 20:50:22 +0800 | [diff] [blame] | 4568 | /* handshake_step return error. And it is same |
| 4569 | * with alert_reason. |
| 4570 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4571 | if (ssl->send_alert) { |
| 4572 | ret = mbedtls_ssl_handle_pending_alert(ssl); |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 4573 | goto cleanup; |
| 4574 | } |
| 4575 | } |
| 4576 | |
| 4577 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4578 | return ret; |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4579 | } |
| 4580 | |
| 4581 | /* |
| 4582 | * Perform the SSL handshake |
| 4583 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4584 | int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4585 | { |
| 4586 | int ret = 0; |
| 4587 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 4588 | /* Sanity checks */ |
| 4589 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4590 | if (ssl == NULL || ssl->conf == NULL) { |
| 4591 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4592 | } |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 4593 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 4594 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4595 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 4596 | (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) { |
| 4597 | MBEDTLS_SSL_DEBUG_MSG(1, ("You must use " |
| 4598 | "mbedtls_ssl_set_timer_cb() for DTLS")); |
| 4599 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 4600 | } |
| 4601 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4602 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4603 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake")); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4604 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 4605 | /* Main handshake loop */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4606 | while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 4607 | ret = mbedtls_ssl_handshake_step(ssl); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4608 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4609 | if (ret != 0) { |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4610 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4611 | } |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4612 | } |
| 4613 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4614 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4615 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4616 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4617 | } |
| 4618 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4619 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 4620 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4621 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4622 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4623 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4624 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4625 | static int ssl_write_hello_request(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4626 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4627 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4628 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4629 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request")); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4630 | |
| 4631 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4632 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 4633 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4634 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4635 | if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { |
| 4636 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret); |
| 4637 | return ret; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4638 | } |
| 4639 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4640 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request")); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4642 | return 0; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4643 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4644 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4645 | |
| 4646 | /* |
| 4647 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4648 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 4649 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 4650 | * - 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] | 4651 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4652 | * 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] | 4653 | * 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] | 4654 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4655 | int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4656 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4657 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4658 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4659 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate")); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4660 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4661 | if ((ret = ssl_handshake_init(ssl)) != 0) { |
| 4662 | return ret; |
| 4663 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4664 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 4665 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 4666 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4667 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4668 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 4669 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) { |
| 4670 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4671 | ssl->handshake->out_msg_seq = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4672 | } else { |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4673 | ssl->handshake->in_msg_seq = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4674 | } |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 4675 | } |
| 4676 | #endif |
| 4677 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4678 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 4679 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4680 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4681 | if ((ret = mbedtls_ssl_handshake(ssl)) != 0) { |
| 4682 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret); |
| 4683 | return ret; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4684 | } |
| 4685 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4686 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate")); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4687 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4688 | return 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4689 | } |
| 4690 | |
| 4691 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4692 | * Renegotiate current connection on client, |
| 4693 | * or request renegotiation on server |
| 4694 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4695 | int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4696 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4697 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4698 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4699 | if (ssl == NULL || ssl->conf == NULL) { |
| 4700 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4701 | } |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 4702 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4703 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4704 | /* On server, just send the request */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4705 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 4706 | if (mbedtls_ssl_is_handshake_over(ssl) == 0) { |
| 4707 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4708 | } |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4709 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4710 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 4711 | |
| 4712 | /* Did we already try/start sending HelloRequest? */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4713 | if (ssl->out_left != 0) { |
| 4714 | return mbedtls_ssl_flush_output(ssl); |
| 4715 | } |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 4716 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4717 | return ssl_write_hello_request(ssl); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4718 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4719 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4720 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4721 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4722 | /* |
| 4723 | * On client, either start the renegotiation process or, |
| 4724 | * if already in progress, continue the handshake |
| 4725 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4726 | if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) { |
| 4727 | if (mbedtls_ssl_is_handshake_over(ssl) == 0) { |
| 4728 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4729 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4730 | |
| 4731 | if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) { |
| 4732 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret); |
| 4733 | return ret; |
| 4734 | } |
| 4735 | } else { |
| 4736 | if ((ret = mbedtls_ssl_handshake(ssl)) != 0) { |
| 4737 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret); |
| 4738 | return ret; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4739 | } |
| 4740 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4741 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4742 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4743 | return ret; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4744 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4745 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4746 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4747 | void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4748 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 4749 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 4750 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4751 | if (handshake == NULL) { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4752 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4753 | } |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4754 | |
Elena Uziunaite | 8dde3b3 | 2024-07-05 12:10:21 +0100 | [diff] [blame] | 4755 | #if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4756 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4757 | if (ssl->handshake->group_list_heap_allocated) { |
| 4758 | mbedtls_free((void *) handshake->group_list); |
| 4759 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4760 | handshake->group_list = NULL; |
| 4761 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Elena Uziunaite | 8dde3b3 | 2024-07-05 12:10:21 +0100 | [diff] [blame] | 4762 | #endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */ |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4763 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4764 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4765 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4766 | if (ssl->handshake->sig_algs_heap_allocated) { |
| 4767 | mbedtls_free((void *) handshake->sig_algs); |
| 4768 | } |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4769 | handshake->sig_algs = NULL; |
| 4770 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Xiaofei Bai | c234ecf | 2022-02-08 09:59:23 +0000 | [diff] [blame] | 4771 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4772 | if (ssl->handshake->certificate_request_context) { |
| 4773 | mbedtls_free((void *) handshake->certificate_request_context); |
Xiaofei Bai | c234ecf | 2022-02-08 09:59:23 +0000 | [diff] [blame] | 4774 | } |
| 4775 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4776 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4777 | |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4778 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4779 | if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) { |
| 4780 | ssl->conf->f_async_cancel(ssl); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4781 | handshake->async_in_progress = 0; |
| 4782 | } |
| 4783 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 4784 | |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 4785 | #if defined(PSA_WANT_ALG_SHA_256) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4786 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4787 | psa_hash_abort(&handshake->fin_sha256_psa); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4788 | #else |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 4789 | mbedtls_md_free(&handshake->fin_sha256); |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 4790 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4791 | #endif |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 4792 | #if defined(PSA_WANT_ALG_SHA_384) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4793 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4794 | psa_hash_abort(&handshake->fin_sha384_psa); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4795 | #else |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 4796 | mbedtls_md_free(&handshake->fin_sha384); |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 4797 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4798 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 4799 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4800 | #if defined(MBEDTLS_DHM_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4801 | mbedtls_dhm_free(&handshake->dhm_ctx); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4802 | #endif |
Valerio Setti | 7aeec54 | 2023-07-05 18:57:21 +0200 | [diff] [blame] | 4803 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Valerio Setti | 6eb0054 | 2023-07-07 17:04:24 +0200 | [diff] [blame] | 4804 | defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4805 | mbedtls_ecdh_free(&handshake->ecdh_ctx); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 4806 | #endif |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 4807 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 4808 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 4809 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4810 | psa_pake_abort(&handshake->psa_pake_ctx); |
Valerio Setti | eb3f788 | 2022-12-08 18:42:58 +0100 | [diff] [blame] | 4811 | /* |
| 4812 | * Opaque keys are not stored in the handshake's data and it's the user |
| 4813 | * responsibility to destroy them. Clear ones, instead, are created by |
| 4814 | * the TLS library and should be destroyed at the same level |
| 4815 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4816 | if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) { |
| 4817 | psa_destroy_key(handshake->psa_pake_password); |
Valerio Setti | eb3f788 | 2022-12-08 18:42:58 +0100 | [diff] [blame] | 4818 | } |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 4819 | handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT; |
| 4820 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4821 | mbedtls_ecjpake_free(&handshake->ecjpake_ctx); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 4822 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 4823 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4824 | mbedtls_free(handshake->ecjpake_cache); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 4825 | handshake->ecjpake_cache = NULL; |
| 4826 | handshake->ecjpake_cache_len = 0; |
| 4827 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 4828 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 4829 | |
Valerio Setti | 7aeec54 | 2023-07-05 18:57:21 +0200 | [diff] [blame] | 4830 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_ANY_ENABLED) || \ |
Valerio Setti | 45d56f3 | 2023-07-13 17:23:20 +0200 | [diff] [blame] | 4831 | defined(MBEDTLS_KEY_EXCHANGE_WITH_ECDSA_ANY_ENABLED) || \ |
Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 4832 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 4833 | /* explicit void pointer cast for buggy MS compiler */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4834 | mbedtls_free((void *) handshake->curves_tls_id); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 4835 | #endif |
| 4836 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 4837 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4838 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4839 | if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4840 | /* The maintenance of the external PSK key slot is the |
| 4841 | * user's responsibility. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4842 | if (ssl->handshake->psk_opaque_is_internal) { |
| 4843 | psa_destroy_key(ssl->handshake->psk_opaque); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4844 | ssl->handshake->psk_opaque_is_internal = 0; |
| 4845 | } |
| 4846 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 4847 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 4848 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4849 | if (handshake->psk != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 4850 | mbedtls_zeroize_and_free(handshake->psk, handshake->psk_len); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 4851 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 4852 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 4853 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 4854 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4855 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 4856 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 4857 | /* |
| 4858 | * Free only the linked list wrapper, not the keys themselves |
| 4859 | * since the belong to the SNI callback |
| 4860 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4861 | ssl_key_cert_free(handshake->sni_key_cert); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4862 | #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] | 4863 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4864 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4865 | mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx); |
| 4866 | if (handshake->ecrs_peer_cert != NULL) { |
| 4867 | mbedtls_x509_crt_free(handshake->ecrs_peer_cert); |
| 4868 | mbedtls_free(handshake->ecrs_peer_cert); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 4869 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 4870 | #endif |
| 4871 | |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 4872 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 4873 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4874 | mbedtls_pk_free(&handshake->peer_pubkey); |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 4875 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4876 | |
XiaokangQian | 9b93c0d | 2022-02-09 06:02:25 +0000 | [diff] [blame] | 4877 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4878 | (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3)) |
| 4879 | mbedtls_free(handshake->cookie); |
XiaokangQian | 9b93c0d | 2022-02-09 06:02:25 +0000 | [diff] [blame] | 4880 | #endif /* MBEDTLS_SSL_CLI_C && |
| 4881 | ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */ |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 4882 | |
| 4883 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4884 | mbedtls_ssl_flight_free(handshake->flight); |
| 4885 | mbedtls_ssl_buffering_free(ssl); |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 4886 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 4887 | |
Valerio Setti | ea59c43 | 2023-07-25 11:14:03 +0200 | [diff] [blame] | 4888 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED) |
Przemek Stekiel | 7ac93be | 2023-07-04 10:02:38 +0200 | [diff] [blame] | 4889 | if (handshake->xxdh_psa_privkey_is_external == 0) { |
| 4890 | psa_destroy_key(handshake->xxdh_psa_privkey); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4891 | } |
Valerio Setti | ea59c43 | 2023-07-25 11:14:03 +0200 | [diff] [blame] | 4892 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED */ |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 4893 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4894 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4895 | mbedtls_ssl_transform_free(handshake->transform_handshake); |
| 4896 | mbedtls_free(handshake->transform_handshake); |
Jerry Yu | 3d9b590 | 2022-11-04 14:07:25 +0800 | [diff] [blame] | 4897 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4898 | mbedtls_ssl_transform_free(handshake->transform_earlydata); |
| 4899 | mbedtls_free(handshake->transform_earlydata); |
Jerry Yu | 3d9b590 | 2022-11-04 14:07:25 +0800 | [diff] [blame] | 4900 | #endif |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4901 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 4902 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4903 | |
| 4904 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4905 | /* If the buffers are too big - reallocate. Because of the way Mbed TLS |
| 4906 | * processes datagrams and the fact that a datagram is allowed to have |
| 4907 | * several records in it, it is possible that the I/O buffers are not |
| 4908 | * empty at this stage */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4909 | handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl), |
| 4910 | mbedtls_ssl_get_output_buflen(ssl)); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4911 | #endif |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 4912 | |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 4913 | /* mbedtls_platform_zeroize MUST be last one in this function */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4914 | mbedtls_platform_zeroize(handshake, |
| 4915 | sizeof(mbedtls_ssl_handshake_params)); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4916 | } |
| 4917 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4918 | void mbedtls_ssl_session_free(mbedtls_ssl_session *session) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4919 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4920 | if (session == NULL) { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4921 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4922 | } |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4923 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4924 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4925 | ssl_clear_peer_cert(session); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4926 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 4927 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4928 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Xiaokang Qian | bc663a0 | 2022-10-09 11:14:39 +0000 | [diff] [blame] | 4929 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 4930 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4931 | mbedtls_free(session->hostname); |
Xiaokang Qian | 281fd1b | 2022-09-20 11:35:41 +0000 | [diff] [blame] | 4932 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4933 | mbedtls_free(session->ticket); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 4934 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 4935 | |
Waleed Elmelegy | 2824a20 | 2024-02-23 17:51:47 +0000 | [diff] [blame] | 4936 | #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) && \ |
| 4937 | defined(MBEDTLS_SSL_SRV_C) |
Waleed Elmelegy | 883f77c | 2024-03-06 19:09:41 +0000 | [diff] [blame] | 4938 | mbedtls_free(session->ticket_alpn); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4939 | #endif |
| 4940 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4941 | mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session)); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4942 | } |
| 4943 | |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 4944 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4945 | |
| 4946 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4947 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u |
| 4948 | #else |
| 4949 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u |
| 4950 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4951 | |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4952 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4953 | |
| 4954 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4955 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u |
| 4956 | #else |
| 4957 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u |
| 4958 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 4959 | |
| 4960 | #if defined(MBEDTLS_SSL_ALPN) |
| 4961 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u |
| 4962 | #else |
| 4963 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u |
| 4964 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4965 | |
| 4966 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0 |
| 4967 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1 |
| 4968 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2 |
| 4969 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3 |
| 4970 | |
| 4971 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4972 | ((uint32_t) ( \ |
| 4973 | (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \ |
| 4974 | SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \ |
| 4975 | (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \ |
| 4976 | SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \ |
| 4977 | (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \ |
| 4978 | SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \ |
| 4979 | (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \ |
| 4980 | 0u)) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4981 | |
Chien Wong | 4e9683e | 2023-12-28 17:07:43 +0800 | [diff] [blame] | 4982 | static const unsigned char ssl_serialized_context_header[] = { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4983 | MBEDTLS_VERSION_MAJOR, |
| 4984 | MBEDTLS_VERSION_MINOR, |
| 4985 | MBEDTLS_VERSION_PATCH, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4986 | MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG), |
| 4987 | MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG), |
| 4988 | MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG), |
| 4989 | MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG), |
| 4990 | MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG), |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4991 | }; |
| 4992 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4993 | /* |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4994 | * Serialize a full SSL context |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 4995 | * |
| 4996 | * The format of the serialized data is: |
| 4997 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 4998 | * |
| 4999 | * // header |
| 5000 | * opaque mbedtls_version[3]; // major, minor, patch |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5001 | * opaque context_format[5]; // version-specific field determining |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 5002 | * // the format of the remaining |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5003 | * // serialized data. |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 5004 | * Note: When updating the format, remember to keep these |
| 5005 | * 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] | 5006 | * |
| 5007 | * // session sub-structure |
| 5008 | * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save() |
| 5009 | * // transform sub-structure |
| 5010 | * uint8 random[64]; // ServerHello.random+ClientHello.random |
| 5011 | * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value |
| 5012 | * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use |
| 5013 | * // fields from ssl_context |
| 5014 | * uint32 badmac_seen; // DTLS: number of records with failing MAC |
| 5015 | * uint64 in_window_top; // DTLS: last validated record seq_num |
| 5016 | * uint64 in_window; // DTLS: bitmask for replay protection |
| 5017 | * uint8 disable_datagram_packing; // DTLS: only one record per datagram |
| 5018 | * uint64 cur_out_ctr; // Record layer: outgoing sequence number |
| 5019 | * uint16 mtu; // DTLS: path mtu (max outgoing fragment size) |
| 5020 | * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol |
| 5021 | * |
| 5022 | * Note that many fields of the ssl_context or sub-structures are not |
| 5023 | * serialized, as they fall in one of the following categories: |
| 5024 | * |
| 5025 | * 1. forced value (eg in_left must be 0) |
| 5026 | * 2. pointer to dynamically-allocated memory (eg session, transform) |
| 5027 | * 3. value can be re-derived from other data (eg session keys from MS) |
| 5028 | * 4. value was temporary (eg content of input buffer) |
| 5029 | * 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] | 5030 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5031 | int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl, |
| 5032 | unsigned char *buf, |
| 5033 | size_t buf_len, |
| 5034 | size_t *olen) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5035 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5036 | unsigned char *p = buf; |
| 5037 | size_t used = 0; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5038 | size_t session_len; |
| 5039 | int ret = 0; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5040 | |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 5041 | /* |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5042 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in |
| 5043 | * this function's documentation. |
| 5044 | * |
| 5045 | * These are due to assumptions/limitations in the implementation. Some of |
| 5046 | * them are likely to stay (no handshake in progress) some might go away |
| 5047 | * (only DTLS) but are currently used to simplify the implementation. |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 5048 | */ |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5049 | /* The initial handshake must be over */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5050 | if (mbedtls_ssl_is_handshake_over(ssl) == 0) { |
| 5051 | MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over")); |
| 5052 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5053 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5054 | if (ssl->handshake != NULL) { |
| 5055 | MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed")); |
| 5056 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5057 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5058 | /* Double-check that sub-structures are indeed ready */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5059 | if (ssl->transform == NULL || ssl->session == NULL) { |
| 5060 | MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready")); |
| 5061 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5062 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5063 | /* There must be no pending incoming or outgoing data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5064 | if (mbedtls_ssl_check_pending(ssl) != 0) { |
| 5065 | MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data")); |
| 5066 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5067 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5068 | if (ssl->out_left != 0) { |
| 5069 | MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data")); |
| 5070 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5071 | } |
Dave Rodgman | 556e8a3 | 2022-12-06 16:31:25 +0000 | [diff] [blame] | 5072 | /* Protocol must be DTLS, not TLS */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5073 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 5074 | MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported")); |
| 5075 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5076 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5077 | /* Version must be 1.2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5078 | if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) { |
| 5079 | MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported")); |
| 5080 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5081 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5082 | /* We must be using an AEAD ciphersuite */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5083 | if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) { |
| 5084 | MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported")); |
| 5085 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5086 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5087 | /* Renegotiation must not be enabled */ |
| 5088 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5089 | if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) { |
| 5090 | MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled")); |
| 5091 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5092 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5093 | #endif |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5094 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5095 | /* |
| 5096 | * Version and format identifier |
| 5097 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5098 | used += sizeof(ssl_serialized_context_header); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5099 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5100 | if (used <= buf_len) { |
| 5101 | memcpy(p, ssl_serialized_context_header, |
| 5102 | sizeof(ssl_serialized_context_header)); |
| 5103 | p += sizeof(ssl_serialized_context_header); |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5104 | } |
| 5105 | |
| 5106 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5107 | * Session (length + data) |
| 5108 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5109 | ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len); |
| 5110 | if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) { |
| 5111 | return ret; |
| 5112 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5113 | |
| 5114 | used += 4 + session_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5115 | if (used <= buf_len) { |
| 5116 | MBEDTLS_PUT_UINT32_BE(session_len, p, 0); |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5117 | p += 4; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5118 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5119 | ret = ssl_session_save(ssl->session, 1, |
| 5120 | p, session_len, &session_len); |
| 5121 | if (ret != 0) { |
| 5122 | return ret; |
| 5123 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5124 | |
| 5125 | p += session_len; |
| 5126 | } |
| 5127 | |
| 5128 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5129 | * Transform |
| 5130 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5131 | used += sizeof(ssl->transform->randbytes); |
| 5132 | if (used <= buf_len) { |
| 5133 | memcpy(p, ssl->transform->randbytes, |
| 5134 | sizeof(ssl->transform->randbytes)); |
| 5135 | p += sizeof(ssl->transform->randbytes); |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5136 | } |
| 5137 | |
| 5138 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Dave Rodgman | c37ad44 | 2023-11-03 23:36:06 +0000 | [diff] [blame] | 5139 | used += 2U + ssl->transform->in_cid_len + ssl->transform->out_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5140 | if (used <= buf_len) { |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5141 | *p++ = ssl->transform->in_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5142 | 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] | 5143 | p += ssl->transform->in_cid_len; |
| 5144 | |
| 5145 | *p++ = ssl->transform->out_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5146 | 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] | 5147 | p += ssl->transform->out_cid_len; |
| 5148 | } |
| 5149 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 5150 | |
| 5151 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5152 | * Saved fields from top-level ssl_context structure |
| 5153 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5154 | used += 4; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5155 | if (used <= buf_len) { |
| 5156 | MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0); |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5157 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5158 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5159 | |
| 5160 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 5161 | used += 16; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5162 | if (used <= buf_len) { |
| 5163 | MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0); |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5164 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5165 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5166 | MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0); |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5167 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5168 | } |
| 5169 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 5170 | |
| 5171 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5172 | used += 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5173 | if (used <= buf_len) { |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5174 | *p++ = ssl->disable_datagram_packing; |
| 5175 | } |
| 5176 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5177 | |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 5178 | used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5179 | if (used <= buf_len) { |
| 5180 | memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN); |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 5181 | p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5182 | } |
| 5183 | |
| 5184 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5185 | used += 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5186 | if (used <= buf_len) { |
| 5187 | MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0); |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5188 | p += 2; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5189 | } |
| 5190 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5191 | |
| 5192 | #if defined(MBEDTLS_SSL_ALPN) |
| 5193 | { |
| 5194 | const uint8_t alpn_len = ssl->alpn_chosen |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5195 | ? (uint8_t) strlen(ssl->alpn_chosen) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5196 | : 0; |
| 5197 | |
| 5198 | used += 1 + alpn_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5199 | if (used <= buf_len) { |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5200 | *p++ = alpn_len; |
| 5201 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5202 | if (ssl->alpn_chosen != NULL) { |
| 5203 | memcpy(p, ssl->alpn_chosen, alpn_len); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5204 | p += alpn_len; |
| 5205 | } |
| 5206 | } |
| 5207 | } |
| 5208 | #endif /* MBEDTLS_SSL_ALPN */ |
| 5209 | |
| 5210 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5211 | * Done |
| 5212 | */ |
| 5213 | *olen = used; |
| 5214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5215 | if (used > buf_len) { |
| 5216 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 5217 | } |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5218 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5219 | MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5221 | return mbedtls_ssl_session_reset_int(ssl, 0); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5222 | } |
| 5223 | |
| 5224 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 5225 | * Deserialize context, see mbedtls_ssl_context_save() for format. |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5226 | * |
| 5227 | * This internal version is wrapped by a public function that cleans up in |
| 5228 | * case of error. |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5229 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5230 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5231 | static int ssl_context_load(mbedtls_ssl_context *ssl, |
| 5232 | const unsigned char *buf, |
| 5233 | size_t len) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5234 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5235 | const unsigned char *p = buf; |
| 5236 | const unsigned char * const end = buf + len; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5237 | size_t session_len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5238 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 5239 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 5240 | tls_prf_fn prf_func = NULL; |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 5241 | #endif |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5242 | |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 5243 | /* |
| 5244 | * The context should have been freshly setup or reset. |
| 5245 | * Give the user an error in case of obvious misuse. |
Manuel Pégourié-Gonnard | 4ca930f | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 5246 | * (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] | 5247 | * renegotiating, or if the user mistakenly loaded a session first.) |
| 5248 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5249 | if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST || |
| 5250 | ssl->session != NULL) { |
| 5251 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 5252 | } |
| 5253 | |
| 5254 | /* |
| 5255 | * We can't check that the config matches the initial one, but we can at |
| 5256 | * least check it matches the requirements for serializing. |
| 5257 | */ |
Dave Rodgman | e99b24d | 2023-09-14 15:45:03 +0100 | [diff] [blame] | 5258 | if ( |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 5259 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 5260 | ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 5261 | #endif |
Dave Rodgman | e99b24d | 2023-09-14 15:45:03 +0100 | [diff] [blame] | 5262 | ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
| 5263 | ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 || |
| 5264 | ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 |
| 5265 | ) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5266 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 5267 | } |
| 5268 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5269 | MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5270 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5271 | /* |
| 5272 | * Check version identifier |
| 5273 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5274 | if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) { |
| 5275 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5276 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5277 | |
| 5278 | if (memcmp(p, ssl_serialized_context_header, |
| 5279 | sizeof(ssl_serialized_context_header)) != 0) { |
| 5280 | return MBEDTLS_ERR_SSL_VERSION_MISMATCH; |
| 5281 | } |
| 5282 | p += sizeof(ssl_serialized_context_header); |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5283 | |
| 5284 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5285 | * Session |
| 5286 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5287 | if ((size_t) (end - p) < 4) { |
| 5288 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5289 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5290 | |
Thomas Daubney | f9f0ba8 | 2023-05-23 17:34:33 +0100 | [diff] [blame] | 5291 | session_len = MBEDTLS_GET_UINT32_BE(p, 0); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5292 | p += 4; |
| 5293 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 5294 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 5295 | * 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] | 5296 | ssl->session = ssl->session_negotiate; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5297 | ssl->session_in = ssl->session; |
| 5298 | ssl->session_out = ssl->session; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 5299 | ssl->session_negotiate = NULL; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5300 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5301 | if ((size_t) (end - p) < session_len) { |
| 5302 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5303 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5304 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5305 | ret = ssl_session_load(ssl->session, 1, p, session_len); |
| 5306 | if (ret != 0) { |
| 5307 | mbedtls_ssl_session_free(ssl->session); |
| 5308 | return ret; |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5309 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5310 | |
| 5311 | p += session_len; |
| 5312 | |
| 5313 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5314 | * Transform |
| 5315 | */ |
| 5316 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 5317 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 5318 | * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */ |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 5319 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 5320 | ssl->transform = ssl->transform_negotiate; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5321 | ssl->transform_in = ssl->transform; |
| 5322 | ssl->transform_out = ssl->transform; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 5323 | ssl->transform_negotiate = NULL; |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 5324 | #endif |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5325 | |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 5326 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5327 | prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite); |
| 5328 | if (prf_func == NULL) { |
| 5329 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5330 | } |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 5331 | |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5332 | /* Read random bytes and populate structure */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5333 | if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) { |
| 5334 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5335 | } |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 5336 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5337 | ret = ssl_tls12_populate_transform(ssl->transform, |
| 5338 | ssl->session->ciphersuite, |
| 5339 | ssl->session->master, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 5340 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5341 | ssl->session->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 5342 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5343 | prf_func, |
| 5344 | p, /* currently pointing to randbytes */ |
| 5345 | MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */ |
| 5346 | ssl->conf->endpoint, |
| 5347 | ssl); |
| 5348 | if (ret != 0) { |
| 5349 | return ret; |
| 5350 | } |
Jerry Yu | 840fbb2 | 2022-02-17 14:59:29 +0800 | [diff] [blame] | 5351 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5352 | p += sizeof(ssl->transform->randbytes); |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5353 | |
| 5354 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 5355 | /* Read connection IDs and store them */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5356 | if ((size_t) (end - p) < 1) { |
| 5357 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5358 | } |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5359 | |
| 5360 | ssl->transform->in_cid_len = *p++; |
| 5361 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5362 | if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) { |
| 5363 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5364 | } |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5365 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5366 | 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] | 5367 | p += ssl->transform->in_cid_len; |
| 5368 | |
| 5369 | ssl->transform->out_cid_len = *p++; |
| 5370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5371 | if ((size_t) (end - p) < ssl->transform->out_cid_len) { |
| 5372 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5373 | } |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5374 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5375 | 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] | 5376 | p += ssl->transform->out_cid_len; |
| 5377 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 5378 | |
| 5379 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5380 | * Saved fields from top-level ssl_context structure |
| 5381 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5382 | if ((size_t) (end - p) < 4) { |
| 5383 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5384 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5385 | |
Thomas Daubney | f9f0ba8 | 2023-05-23 17:34:33 +0100 | [diff] [blame] | 5386 | ssl->badmac_seen = MBEDTLS_GET_UINT32_BE(p, 0); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5387 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5388 | |
| 5389 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5390 | if ((size_t) (end - p) < 16) { |
| 5391 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5392 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5393 | |
Thomas Daubney | f9f0ba8 | 2023-05-23 17:34:33 +0100 | [diff] [blame] | 5394 | ssl->in_window_top = MBEDTLS_GET_UINT64_BE(p, 0); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5395 | p += 8; |
| 5396 | |
Thomas Daubney | f9f0ba8 | 2023-05-23 17:34:33 +0100 | [diff] [blame] | 5397 | ssl->in_window = MBEDTLS_GET_UINT64_BE(p, 0); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5398 | p += 8; |
| 5399 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 5400 | |
| 5401 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5402 | if ((size_t) (end - p) < 1) { |
| 5403 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5404 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5405 | |
| 5406 | ssl->disable_datagram_packing = *p++; |
| 5407 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5408 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5409 | if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) { |
| 5410 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5411 | } |
| 5412 | memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr)); |
| 5413 | p += sizeof(ssl->cur_out_ctr); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5414 | |
| 5415 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5416 | if ((size_t) (end - p) < 2) { |
| 5417 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5418 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5419 | |
Dave Rodgman | a3d0f61 | 2023-11-03 23:34:02 +0000 | [diff] [blame] | 5420 | ssl->mtu = MBEDTLS_GET_UINT16_BE(p, 0); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5421 | p += 2; |
| 5422 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5423 | |
| 5424 | #if defined(MBEDTLS_SSL_ALPN) |
| 5425 | { |
| 5426 | uint8_t alpn_len; |
| 5427 | const char **cur; |
| 5428 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5429 | if ((size_t) (end - p) < 1) { |
| 5430 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5431 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5432 | |
| 5433 | alpn_len = *p++; |
| 5434 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5435 | if (alpn_len != 0 && ssl->conf->alpn_list != NULL) { |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5436 | /* alpn_chosen should point to an item in the configured list */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5437 | for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) { |
| 5438 | if (strlen(*cur) == alpn_len && |
Waleed Elmelegy | 131b2ff | 2024-03-14 01:39:39 +0000 | [diff] [blame] | 5439 | memcmp(p, *cur, alpn_len) == 0) { |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5440 | ssl->alpn_chosen = *cur; |
| 5441 | break; |
| 5442 | } |
| 5443 | } |
| 5444 | } |
| 5445 | |
| 5446 | /* can only happen on conf mismatch */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5447 | if (alpn_len != 0 && ssl->alpn_chosen == NULL) { |
| 5448 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5449 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5450 | |
| 5451 | p += alpn_len; |
| 5452 | } |
| 5453 | #endif /* MBEDTLS_SSL_ALPN */ |
| 5454 | |
| 5455 | /* |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 5456 | * Forced fields from top-level ssl_context structure |
| 5457 | * |
| 5458 | * Most of them already set to the correct value by mbedtls_ssl_init() and |
| 5459 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. |
| 5460 | */ |
| 5461 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 5462 | ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 5463 | |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 5464 | /* Adjust pointers for header fields of outgoing records to |
| 5465 | * the given transform, accounting for explicit IV and CID. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5466 | mbedtls_ssl_update_out_pointers(ssl, ssl->transform); |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 5467 | |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 5468 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5469 | ssl->in_epoch = 1; |
| 5470 | #endif |
| 5471 | |
| 5472 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, |
| 5473 | * 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] | 5474 | * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform() |
| 5475 | * inappropriately. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5476 | if (ssl->handshake != NULL) { |
| 5477 | mbedtls_ssl_handshake_free(ssl); |
| 5478 | mbedtls_free(ssl->handshake); |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 5479 | ssl->handshake = NULL; |
| 5480 | } |
| 5481 | |
| 5482 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5483 | * Done - should have consumed entire buffer |
| 5484 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5485 | if (p != end) { |
| 5486 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5487 | } |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5488 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5489 | return 0; |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5490 | } |
| 5491 | |
| 5492 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 5493 | * Deserialize context: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5494 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5495 | int mbedtls_ssl_context_load(mbedtls_ssl_context *context, |
| 5496 | const unsigned char *buf, |
| 5497 | size_t len) |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5498 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5499 | int ret = ssl_context_load(context, buf, len); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5500 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5501 | if (ret != 0) { |
| 5502 | mbedtls_ssl_free(context); |
| 5503 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5504 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5505 | return ret; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5506 | } |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 5507 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5508 | |
| 5509 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5510 | * Free an SSL context |
| 5511 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5512 | void mbedtls_ssl_free(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5513 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5514 | if (ssl == NULL) { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 5515 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5516 | } |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 5517 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5518 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> free")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5519 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5520 | if (ssl->out_buf != NULL) { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 5521 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 5522 | size_t out_buf_len = ssl->out_buf_len; |
| 5523 | #else |
| 5524 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 5525 | #endif |
| 5526 | |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 5527 | mbedtls_zeroize_and_free(ssl->out_buf, out_buf_len); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5528 | ssl->out_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5529 | } |
| 5530 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5531 | if (ssl->in_buf != NULL) { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 5532 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 5533 | size_t in_buf_len = ssl->in_buf_len; |
| 5534 | #else |
| 5535 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 5536 | #endif |
| 5537 | |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 5538 | mbedtls_zeroize_and_free(ssl->in_buf, in_buf_len); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5539 | ssl->in_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5540 | } |
| 5541 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5542 | if (ssl->transform) { |
| 5543 | mbedtls_ssl_transform_free(ssl->transform); |
| 5544 | mbedtls_free(ssl->transform); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5545 | } |
| 5546 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5547 | if (ssl->handshake) { |
| 5548 | mbedtls_ssl_handshake_free(ssl); |
| 5549 | mbedtls_free(ssl->handshake); |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 5550 | |
| 5551 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5552 | mbedtls_ssl_transform_free(ssl->transform_negotiate); |
| 5553 | mbedtls_free(ssl->transform_negotiate); |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 5554 | #endif |
| 5555 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5556 | mbedtls_ssl_session_free(ssl->session_negotiate); |
| 5557 | mbedtls_free(ssl->session_negotiate); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5558 | } |
| 5559 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5560 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5561 | mbedtls_ssl_transform_free(ssl->transform_application); |
| 5562 | mbedtls_free(ssl->transform_application); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5563 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 5564 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5565 | if (ssl->session) { |
| 5566 | mbedtls_ssl_session_free(ssl->session); |
| 5567 | mbedtls_free(ssl->session); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 5568 | } |
| 5569 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 5570 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5571 | if (ssl->hostname != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 5572 | mbedtls_zeroize_and_free(ssl->hostname, strlen(ssl->hostname)); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5573 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 5574 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5575 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 5576 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5577 | mbedtls_free(ssl->cli_id); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 5578 | #endif |
| 5579 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5580 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= free")); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 5581 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 5582 | /* Actually clear after last debug message */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5583 | mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context)); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5584 | } |
| 5585 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5586 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 5587 | * Initialize mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5588 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5589 | void mbedtls_ssl_config_init(mbedtls_ssl_config *conf) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5590 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5591 | memset(conf, 0, sizeof(mbedtls_ssl_config)); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5592 | } |
| 5593 | |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 5594 | /* The selection should be the same as mbedtls_x509_crt_profile_default in |
| 5595 | * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters: |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 5596 | * curves with a lower resource usage come first. |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 5597 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 5598 | * about this list. |
| 5599 | */ |
Chien Wong | 4e9683e | 2023-12-28 17:07:43 +0800 | [diff] [blame] | 5600 | static const uint16_t ssl_preset_default_groups[] = { |
Elena Uziunaite | 0b5d48e | 2024-07-05 11:48:23 +0100 | [diff] [blame] | 5601 | #if defined(PSA_WANT_ECC_MONTGOMERY_255) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5602 | MBEDTLS_SSL_IANA_TLS_GROUP_X25519, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 5603 | #endif |
Elena Uziunaite | 417d05f | 2024-07-04 17:46:30 +0100 | [diff] [blame] | 5604 | #if defined(PSA_WANT_ECC_SECP_R1_256) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5605 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 5606 | #endif |
Elena Uziunaite | 6b4cd48 | 2024-07-04 17:50:11 +0100 | [diff] [blame] | 5607 | #if defined(PSA_WANT_ECC_SECP_R1_384) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5608 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 5609 | #endif |
Elena Uziunaite | 0b5d48e | 2024-07-05 11:48:23 +0100 | [diff] [blame] | 5610 | #if defined(PSA_WANT_ECC_MONTGOMERY_448) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5611 | MBEDTLS_SSL_IANA_TLS_GROUP_X448, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 5612 | #endif |
Elena Uziunaite | 24e24f2 | 2024-07-04 17:53:40 +0100 | [diff] [blame] | 5613 | #if defined(PSA_WANT_ECC_SECP_R1_521) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5614 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 5615 | #endif |
Elena Uziunaite | b8d1087 | 2024-07-05 10:51:40 +0100 | [diff] [blame] | 5616 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5617 | MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 5618 | #endif |
Elena Uziunaite | b8d1087 | 2024-07-05 10:51:40 +0100 | [diff] [blame] | 5619 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5620 | MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 5621 | #endif |
Elena Uziunaite | b8d1087 | 2024-07-05 10:51:40 +0100 | [diff] [blame] | 5622 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5623 | MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 5624 | #endif |
Przemek Stekiel | 316c19e | 2023-05-31 15:25:11 +0200 | [diff] [blame] | 5625 | #if defined(PSA_WANT_ALG_FFDH) |
Przemek Stekiel | 383f471 | 2022-12-12 14:48:57 +0100 | [diff] [blame] | 5626 | MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048, |
| 5627 | MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072, |
| 5628 | MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096, |
| 5629 | MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144, |
| 5630 | MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192, |
| 5631 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5632 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 5633 | }; |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 5634 | |
Alexey Tsvetkov | 2ca3437 | 2022-06-07 10:21:05 +0300 | [diff] [blame] | 5635 | static const int ssl_preset_suiteb_ciphersuites[] = { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5636 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 5637 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 5638 | 0 |
| 5639 | }; |
| 5640 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5641 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 5642 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5643 | /* NOTICE: |
Jerry Yu | 0b994b8 | 2022-01-25 17:22:12 +0800 | [diff] [blame] | 5644 | * 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] | 5645 | * rules SHOULD be upheld. |
| 5646 | * - No duplicate entries. |
| 5647 | * - 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] | 5648 | * - ssl_tls12_preset* is for TLS 1.2 use only. |
Jerry Yu | 370e146 | 2022-01-25 10:36:53 +0800 | [diff] [blame] | 5649 | * - 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] | 5650 | */ |
Chien Wong | 4e9683e | 2023-12-28 17:07:43 +0800 | [diff] [blame] | 5651 | static const uint16_t ssl_preset_default_sig_algs[] = { |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5652 | |
Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5653 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \ |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 5654 | defined(PSA_WANT_ALG_SHA_256) && \ |
Valerio Setti | cf29c5d | 2023-09-01 09:03:41 +0200 | [diff] [blame] | 5655 | defined(PSA_WANT_ECC_SECP_R1_256) |
Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 5656 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5657 | // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256) |
| 5658 | #endif |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5659 | |
Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5660 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \ |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 5661 | defined(PSA_WANT_ALG_SHA_384) && \ |
Valerio Setti | cf29c5d | 2023-09-01 09:03:41 +0200 | [diff] [blame] | 5662 | defined(PSA_WANT_ECC_SECP_R1_384) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5663 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5664 | // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384) |
| 5665 | #endif |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5666 | |
Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5667 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \ |
Gabor Mezei | c15ef93 | 2024-06-13 12:53:54 +0200 | [diff] [blame] | 5668 | defined(PSA_WANT_ALG_SHA_512) && \ |
Valerio Setti | cf29c5d | 2023-09-01 09:03:41 +0200 | [diff] [blame] | 5669 | defined(PSA_WANT_ECC_SECP_R1_521) |
Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 5670 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512, |
Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5671 | // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512) |
| 5672 | #endif |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5673 | |
Gabor Mezei | c15ef93 | 2024-06-13 12:53:54 +0200 | [diff] [blame] | 5674 | #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] | 5675 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5676 | #endif |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 5677 | |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 5678 | #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] | 5679 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5680 | #endif |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 5681 | |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 5682 | #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] | 5683 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5684 | #endif |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 5685 | |
Gabor Mezei | c15ef93 | 2024-06-13 12:53:54 +0200 | [diff] [blame] | 5686 | #if defined(MBEDTLS_RSA_C) && defined(PSA_WANT_ALG_SHA_512) |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 5687 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512, |
Gabor Mezei | c15ef93 | 2024-06-13 12:53:54 +0200 | [diff] [blame] | 5688 | #endif /* MBEDTLS_RSA_C && PSA_WANT_ALG_SHA_512 */ |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 5689 | |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 5690 | #if defined(MBEDTLS_RSA_C) && defined(PSA_WANT_ALG_SHA_384) |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 5691 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384, |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 5692 | #endif /* MBEDTLS_RSA_C && PSA_WANT_ALG_SHA_384 */ |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 5693 | |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 5694 | #if defined(MBEDTLS_RSA_C) && defined(PSA_WANT_ALG_SHA_256) |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 5695 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 5696 | #endif /* MBEDTLS_RSA_C && PSA_WANT_ALG_SHA_256 */ |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 5697 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5698 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5699 | }; |
| 5700 | |
| 5701 | /* NOTICE: see above */ |
| 5702 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5703 | static uint16_t ssl_tls12_preset_default_sig_algs[] = { |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5704 | |
Gabor Mezei | c15ef93 | 2024-06-13 12:53:54 +0200 | [diff] [blame] | 5705 | #if defined(PSA_WANT_ALG_SHA_512) |
Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 5706 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5707 | 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] | 5708 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 5709 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 5710 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5711 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5712 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5713 | 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] | 5714 | #endif |
Gabor Mezei | c15ef93 | 2024-06-13 12:53:54 +0200 | [diff] [blame] | 5715 | #endif /* PSA_WANT_ALG_SHA_512 */ |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5716 | |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 5717 | #if defined(PSA_WANT_ALG_SHA_384) |
Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 5718 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5719 | 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] | 5720 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 5721 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 5722 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5723 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5724 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5725 | 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] | 5726 | #endif |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 5727 | #endif /* PSA_WANT_ALG_SHA_384 */ |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5728 | |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 5729 | #if defined(PSA_WANT_ALG_SHA_256) |
Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 5730 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5731 | 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] | 5732 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 5733 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 5734 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5735 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5736 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5737 | 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] | 5738 | #endif |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 5739 | #endif /* PSA_WANT_ALG_SHA_256 */ |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5740 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5741 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5742 | }; |
| 5743 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5744 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5745 | /* NOTICE: see above */ |
Chien Wong | 4e9683e | 2023-12-28 17:07:43 +0800 | [diff] [blame] | 5746 | static const uint16_t ssl_preset_suiteb_sig_algs[] = { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5747 | |
Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5748 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \ |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 5749 | defined(PSA_WANT_ALG_SHA_256) && \ |
Elena Uziunaite | 417d05f | 2024-07-04 17:46:30 +0100 | [diff] [blame] | 5750 | defined(PSA_WANT_ECC_SECP_R1_256) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5751 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5752 | // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256) |
| 5753 | #endif |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5754 | |
Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5755 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \ |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 5756 | defined(PSA_WANT_ALG_SHA_384) && \ |
Elena Uziunaite | 6b4cd48 | 2024-07-04 17:50:11 +0100 | [diff] [blame] | 5757 | defined(PSA_WANT_ECC_SECP_R1_384) |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 5758 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
Manuel Pégourié-Gonnard | 275afe1 | 2023-09-18 11:19:20 +0200 | [diff] [blame] | 5759 | // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384) |
| 5760 | #endif |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5761 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5762 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 5763 | }; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 5764 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5765 | /* NOTICE: see above */ |
| 5766 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5767 | static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = { |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5768 | |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 5769 | #if defined(PSA_WANT_ALG_SHA_256) |
Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 5770 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5771 | 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] | 5772 | #endif |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 5773 | #endif /* PSA_WANT_ALG_SHA_256 */ |
Yanray Wang | 69ceb39 | 2023-11-22 16:32:39 +0800 | [diff] [blame] | 5774 | |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 5775 | #if defined(PSA_WANT_ALG_SHA_384) |
Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 5776 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5777 | 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] | 5778 | #endif |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 5779 | #endif /* PSA_WANT_ALG_SHA_384 */ |
Yanray Wang | 1136fad | 2023-11-22 16:54:31 +0800 | [diff] [blame] | 5780 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5781 | MBEDTLS_TLS_SIG_NONE |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 5782 | }; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5783 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5784 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5785 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5786 | |
Chien Wong | 4e9683e | 2023-12-28 17:07:43 +0800 | [diff] [blame] | 5787 | static const uint16_t ssl_preset_suiteb_groups[] = { |
Elena Uziunaite | 417d05f | 2024-07-04 17:46:30 +0100 | [diff] [blame] | 5788 | #if defined(PSA_WANT_ECC_SECP_R1_256) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5789 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 5790 | #endif |
Elena Uziunaite | 6b4cd48 | 2024-07-04 17:50:11 +0100 | [diff] [blame] | 5791 | #if defined(PSA_WANT_ECC_SECP_R1_384) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5792 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 5793 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5794 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5795 | }; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5796 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5797 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5798 | /* 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] | 5799 | * to make sure there are no duplicated signature algorithm entries. */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5800 | MBEDTLS_CHECK_RETURN_CRITICAL |
Chien Wong | 4e9683e | 2023-12-28 17:07:43 +0800 | [diff] [blame] | 5801 | 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] | 5802 | { |
| 5803 | size_t i, j; |
| 5804 | int ret = 0; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5805 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5806 | for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) { |
| 5807 | for (j = 0; j < i; j++) { |
| 5808 | if (sig_algs[i] != sig_algs[j]) { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 5809 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5810 | } |
| 5811 | mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET |
| 5812 | ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n", |
| 5813 | sig_algs[i], j, i); |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 5814 | ret = -1; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5815 | } |
| 5816 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5817 | return ret; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5818 | } |
| 5819 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5820 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5821 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5822 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 5823 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5824 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5825 | int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf, |
| 5826 | int endpoint, int transport, int preset) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5827 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 5828 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5829 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 5830 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5831 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5832 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5833 | if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) { |
| 5834 | mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n"); |
| 5835 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5836 | } |
| 5837 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5838 | if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) { |
| 5839 | mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n"); |
| 5840 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5841 | } |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5842 | |
| 5843 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5844 | if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) { |
| 5845 | mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n"); |
| 5846 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5847 | } |
| 5848 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5849 | if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) { |
| 5850 | mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n"); |
| 5851 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5852 | } |
| 5853 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5854 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5855 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 5856 | /* Use the functions here so that they are covered in tests, |
| 5857 | * but otherwise access member directly for efficiency */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5858 | mbedtls_ssl_conf_endpoint(conf, endpoint); |
| 5859 | mbedtls_ssl_conf_transport(conf, transport); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5860 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5861 | /* |
| 5862 | * Things that are common to all presets |
| 5863 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 5864 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5865 | if (endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 5866 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 5867 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 5868 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
| 5869 | #endif |
| 5870 | } |
| 5871 | #endif |
| 5872 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5873 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 5874 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 5875 | #endif |
| 5876 | |
| 5877 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 5878 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 5879 | #endif |
| 5880 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 5881 | #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] | 5882 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 5883 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 5884 | #endif |
| 5885 | |
| 5886 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 5887 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 5888 | #endif |
| 5889 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 5890 | #if defined(MBEDTLS_SSL_SRV_C) |
| 5891 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
TRodziewicz | 3946f79 | 2021-06-14 12:11:18 +0200 | [diff] [blame] | 5892 | conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER; |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 5893 | #endif |
| 5894 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5895 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5896 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
| 5897 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
| 5898 | #endif |
| 5899 | |
| 5900 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 5901 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5902 | memset(conf->renego_period, 0x00, 2); |
| 5903 | memset(conf->renego_period + 2, 0xFF, 6); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5904 | #endif |
| 5905 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5906 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5907 | if (endpoint == MBEDTLS_SSL_IS_SERVER) { |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 5908 | const unsigned char dhm_p[] = |
| 5909 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 5910 | const unsigned char dhm_g[] = |
| 5911 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 5912 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5913 | if ((ret = mbedtls_ssl_conf_dh_param_bin(conf, |
| 5914 | dhm_p, sizeof(dhm_p), |
| 5915 | dhm_g, sizeof(dhm_g))) != 0) { |
| 5916 | return ret; |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 5917 | } |
| 5918 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 5919 | #endif |
| 5920 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5921 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 6ee56aa | 2022-12-06 17:47:22 +0800 | [diff] [blame] | 5922 | |
| 5923 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
Yanray Wang | d5ed36f | 2023-11-07 11:40:43 +0800 | [diff] [blame] | 5924 | mbedtls_ssl_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED); |
Jerry Yu | 6ee56aa | 2022-12-06 17:47:22 +0800 | [diff] [blame] | 5925 | #if defined(MBEDTLS_SSL_SRV_C) |
Yanray Wang | 0751761 | 2023-11-07 11:47:36 +0800 | [diff] [blame] | 5926 | 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] | 5927 | #endif |
| 5928 | #endif /* MBEDTLS_SSL_EARLY_DATA */ |
| 5929 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 5930 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 5931 | mbedtls_ssl_conf_new_session_tickets( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5932 | conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS); |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 5933 | #endif |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 5934 | /* |
| 5935 | * Allow all TLS 1.3 key exchange modes by default. |
| 5936 | */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 5937 | conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL; |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5938 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 5939 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5940 | if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 5941 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5942 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 5943 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5944 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5945 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 5946 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5947 | } else { |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5948 | #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] | 5949 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5950 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5951 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 5952 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 5953 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 5954 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5955 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5956 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5957 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5958 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5959 | #endif |
| 5960 | } |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 5961 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5962 | /* |
| 5963 | * Preset-specific defaults |
| 5964 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5965 | switch (preset) { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5966 | /* |
| 5967 | * NSA Suite B |
| 5968 | */ |
| 5969 | case MBEDTLS_SSL_PRESET_SUITEB: |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5970 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 5971 | conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5972 | |
| 5973 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5974 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5975 | #endif |
| 5976 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5977 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5978 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5979 | if (mbedtls_ssl_conf_is_tls12_only(conf)) { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5980 | conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5981 | } else |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5982 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5983 | conf->sig_algs = ssl_preset_suiteb_sig_algs; |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5984 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5985 | |
Manuel Pégourié-Gonnard | f07ce3b | 2023-09-22 11:53:41 +0200 | [diff] [blame] | 5986 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5987 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5988 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5989 | conf->group_list = ssl_preset_suiteb_groups; |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 5990 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5991 | |
| 5992 | /* |
| 5993 | * Default |
| 5994 | */ |
| 5995 | default: |
Ronald Cron | f660655 | 2022-03-15 11:23:25 +0100 | [diff] [blame] | 5996 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 5997 | conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites(); |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5998 | |
| 5999 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6000 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 6001 | #endif |
| 6002 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 6003 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6004 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6005 | if (mbedtls_ssl_conf_is_tls12_only(conf)) { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6006 | conf->sig_algs = ssl_tls12_preset_default_sig_algs; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6007 | } else |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6008 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6009 | conf->sig_algs = ssl_preset_default_sig_algs; |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 6010 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6011 | |
Manuel Pégourié-Gonnard | f07ce3b | 2023-09-22 11:53:41 +0200 | [diff] [blame] | 6012 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6013 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6014 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6015 | conf->group_list = ssl_preset_default_groups; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6016 | |
| 6017 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 6018 | conf->dhm_min_bitlen = 1024; |
| 6019 | #endif |
| 6020 | } |
| 6021 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6022 | return 0; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6023 | } |
| 6024 | |
| 6025 | /* |
| 6026 | * Free mbedtls_ssl_config |
| 6027 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6028 | void mbedtls_ssl_config_free(mbedtls_ssl_config *conf) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6029 | { |
Troy-Butler | da73abc | 2024-04-02 13:37:31 -0400 | [diff] [blame] | 6030 | if (conf == NULL) { |
| 6031 | return; |
| 6032 | } |
| 6033 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6034 | #if defined(MBEDTLS_DHM_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6035 | mbedtls_mpi_free(&conf->dhm_P); |
| 6036 | mbedtls_mpi_free(&conf->dhm_G); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6037 | #endif |
| 6038 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 6039 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 6040 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6041 | if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 6042 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 6043 | } |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 6044 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6045 | if (conf->psk != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 6046 | mbedtls_zeroize_and_free(conf->psk, conf->psk_len); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 6047 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6048 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 6049 | } |
| 6050 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6051 | if (conf->psk_identity != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 6052 | mbedtls_zeroize_and_free(conf->psk_identity, conf->psk_identity_len); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 6053 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6054 | conf->psk_identity_len = 0; |
| 6055 | } |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 6056 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6057 | |
| 6058 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6059 | ssl_key_cert_free(conf->key_cert); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6060 | #endif |
| 6061 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6062 | mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config)); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6063 | } |
| 6064 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 6065 | #if defined(MBEDTLS_PK_C) && \ |
Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 6066 | (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] | 6067 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6068 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6069 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6070 | 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] | 6071 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6072 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6073 | if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) { |
| 6074 | return MBEDTLS_SSL_SIG_RSA; |
| 6075 | } |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6076 | #endif |
Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 6077 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6078 | if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) { |
| 6079 | return MBEDTLS_SSL_SIG_ECDSA; |
| 6080 | } |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6081 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6082 | return MBEDTLS_SSL_SIG_ANON; |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6083 | } |
| 6084 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6085 | 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] | 6086 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6087 | switch (type) { |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6088 | case MBEDTLS_PK_RSA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6089 | return MBEDTLS_SSL_SIG_RSA; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6090 | case MBEDTLS_PK_ECDSA: |
| 6091 | case MBEDTLS_PK_ECKEY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6092 | return MBEDTLS_SSL_SIG_ECDSA; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6093 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6094 | return MBEDTLS_SSL_SIG_ANON; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6095 | } |
| 6096 | } |
| 6097 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6098 | 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] | 6099 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6100 | switch (sig) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6101 | #if defined(MBEDTLS_RSA_C) |
| 6102 | case MBEDTLS_SSL_SIG_RSA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6103 | return MBEDTLS_PK_RSA; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6104 | #endif |
Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 6105 | #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] | 6106 | case MBEDTLS_SSL_SIG_ECDSA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6107 | return MBEDTLS_PK_ECDSA; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6108 | #endif |
| 6109 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6110 | return MBEDTLS_PK_NONE; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6111 | } |
| 6112 | } |
Valerio Setti | e9646ec | 2023-08-02 20:02:28 +0200 | [diff] [blame] | 6113 | #endif /* MBEDTLS_PK_C && |
| 6114 | ( 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] | 6115 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 6116 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6117 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 6118 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6119 | 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] | 6120 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6121 | switch (hash) { |
Elena Uziunaite | b66a991 | 2024-05-10 14:25:58 +0100 | [diff] [blame] | 6122 | #if defined(PSA_WANT_ALG_MD5) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6123 | case MBEDTLS_SSL_HASH_MD5: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6124 | return MBEDTLS_MD_MD5; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6125 | #endif |
Elena Uziunaite | 9fc5be0 | 2024-09-04 18:12:59 +0100 | [diff] [blame] | 6126 | #if defined(PSA_WANT_ALG_SHA_1) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6127 | case MBEDTLS_SSL_HASH_SHA1: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6128 | return MBEDTLS_MD_SHA1; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6129 | #endif |
Elena Uziunaite | fcc9afa | 2024-05-23 14:43:22 +0100 | [diff] [blame] | 6130 | #if defined(PSA_WANT_ALG_SHA_224) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6131 | case MBEDTLS_SSL_HASH_SHA224: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6132 | return MBEDTLS_MD_SHA224; |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 6133 | #endif |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 6134 | #if defined(PSA_WANT_ALG_SHA_256) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6135 | case MBEDTLS_SSL_HASH_SHA256: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6136 | return MBEDTLS_MD_SHA256; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6137 | #endif |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 6138 | #if defined(PSA_WANT_ALG_SHA_384) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6139 | case MBEDTLS_SSL_HASH_SHA384: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6140 | return MBEDTLS_MD_SHA384; |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 6141 | #endif |
Gabor Mezei | c15ef93 | 2024-06-13 12:53:54 +0200 | [diff] [blame] | 6142 | #if defined(PSA_WANT_ALG_SHA_512) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6143 | case MBEDTLS_SSL_HASH_SHA512: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6144 | return MBEDTLS_MD_SHA512; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6145 | #endif |
| 6146 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6147 | return MBEDTLS_MD_NONE; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6148 | } |
| 6149 | } |
| 6150 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6151 | /* |
| 6152 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 6153 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6154 | unsigned char mbedtls_ssl_hash_from_md_alg(int md) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6155 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6156 | switch (md) { |
Elena Uziunaite | b66a991 | 2024-05-10 14:25:58 +0100 | [diff] [blame] | 6157 | #if defined(PSA_WANT_ALG_MD5) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6158 | case MBEDTLS_MD_MD5: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6159 | return MBEDTLS_SSL_HASH_MD5; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6160 | #endif |
Elena Uziunaite | 9fc5be0 | 2024-09-04 18:12:59 +0100 | [diff] [blame] | 6161 | #if defined(PSA_WANT_ALG_SHA_1) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6162 | case MBEDTLS_MD_SHA1: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6163 | return MBEDTLS_SSL_HASH_SHA1; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6164 | #endif |
Elena Uziunaite | fcc9afa | 2024-05-23 14:43:22 +0100 | [diff] [blame] | 6165 | #if defined(PSA_WANT_ALG_SHA_224) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6166 | case MBEDTLS_MD_SHA224: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6167 | return MBEDTLS_SSL_HASH_SHA224; |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 6168 | #endif |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 6169 | #if defined(PSA_WANT_ALG_SHA_256) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6170 | case MBEDTLS_MD_SHA256: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6171 | return MBEDTLS_SSL_HASH_SHA256; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6172 | #endif |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 6173 | #if defined(PSA_WANT_ALG_SHA_384) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6174 | case MBEDTLS_MD_SHA384: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6175 | return MBEDTLS_SSL_HASH_SHA384; |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 6176 | #endif |
Gabor Mezei | c15ef93 | 2024-06-13 12:53:54 +0200 | [diff] [blame] | 6177 | #if defined(PSA_WANT_ALG_SHA_512) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6178 | case MBEDTLS_MD_SHA512: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6179 | return MBEDTLS_SSL_HASH_SHA512; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6180 | #endif |
| 6181 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6182 | return MBEDTLS_SSL_HASH_NONE; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6183 | } |
| 6184 | } |
| 6185 | |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 6186 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6187 | * 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] | 6188 | * 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] | 6189 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6190 | 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] | 6191 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6192 | const uint16_t *group_list = mbedtls_ssl_get_groups(ssl); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 6193 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6194 | if (group_list == NULL) { |
| 6195 | return -1; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6196 | } |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 6197 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6198 | for (; *group_list != 0; group_list++) { |
| 6199 | if (*group_list == tls_id) { |
| 6200 | return 0; |
| 6201 | } |
| 6202 | } |
| 6203 | |
| 6204 | return -1; |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 6205 | } |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 6206 | |
Elena Uziunaite | 8dde3b3 | 2024-07-05 12:10:21 +0100 | [diff] [blame] | 6207 | #if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 6208 | /* |
| 6209 | * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id. |
| 6210 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6211 | 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] | 6212 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6213 | 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] | 6214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6215 | if (tls_id == 0) { |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6216 | return -1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6217 | } |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 6218 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6219 | return mbedtls_ssl_check_curve_tls_id(ssl, tls_id); |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 6220 | } |
Elena Uziunaite | 8dde3b3 | 2024-07-05 12:10:21 +0100 | [diff] [blame] | 6221 | #endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */ |
Valerio Setti | 67419f0 | 2023-01-04 16:12:42 +0100 | [diff] [blame] | 6222 | |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6223 | static const struct { |
| 6224 | uint16_t tls_id; |
| 6225 | mbedtls_ecp_group_id ecp_group_id; |
| 6226 | psa_ecc_family_t psa_family; |
| 6227 | uint16_t bits; |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6228 | } tls_id_match_table[] = |
| 6229 | { |
Elena Uziunaite | 24e24f2 | 2024-07-04 17:53:40 +0100 | [diff] [blame] | 6230 | #if defined(PSA_WANT_ECC_SECP_R1_521) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6231 | { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6232 | #endif |
Elena Uziunaite | b8d1087 | 2024-07-05 10:51:40 +0100 | [diff] [blame] | 6233 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6234 | { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6235 | #endif |
Elena Uziunaite | 6b4cd48 | 2024-07-04 17:50:11 +0100 | [diff] [blame] | 6236 | #if defined(PSA_WANT_ECC_SECP_R1_384) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6237 | { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6238 | #endif |
Elena Uziunaite | b8d1087 | 2024-07-05 10:51:40 +0100 | [diff] [blame] | 6239 | #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6240 | { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6241 | #endif |
Elena Uziunaite | 417d05f | 2024-07-04 17:46:30 +0100 | [diff] [blame] | 6242 | #if defined(PSA_WANT_ECC_SECP_R1_256) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6243 | { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6244 | #endif |
Elena Uziunaite | 9e85c9f | 2024-07-04 18:37:34 +0100 | [diff] [blame] | 6245 | #if defined(PSA_WANT_ECC_SECP_K1_256) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6246 | { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256 }, |
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_256) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6249 | { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6250 | #endif |
Elena Uziunaite | eaa0cf0 | 2024-07-04 17:41:25 +0100 | [diff] [blame] | 6251 | #if defined(PSA_WANT_ECC_SECP_R1_224) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6252 | { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6253 | #endif |
Elena Uziunaite | 63cb13e | 2024-09-05 12:43:14 +0100 | [diff] [blame] | 6254 | #if defined(PSA_WANT_ECC_SECP_K1_224) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6255 | { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6256 | #endif |
Elena Uziunaite | a363286 | 2024-07-04 13:52:43 +0100 | [diff] [blame] | 6257 | #if defined(PSA_WANT_ECC_SECP_R1_192) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6258 | { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192 }, |
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_192) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6261 | { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6262 | #endif |
Elena Uziunaite | 0b5d48e | 2024-07-05 11:48:23 +0100 | [diff] [blame] | 6263 | #if defined(PSA_WANT_ECC_MONTGOMERY_255) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6264 | { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6265 | #endif |
Elena Uziunaite | 0b5d48e | 2024-07-05 11:48:23 +0100 | [diff] [blame] | 6266 | #if defined(PSA_WANT_ECC_MONTGOMERY_448) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6267 | { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6268 | #endif |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6269 | { 0, MBEDTLS_ECP_DP_NONE, 0, 0 }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6270 | }; |
| 6271 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6272 | 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] | 6273 | psa_key_type_t *type, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6274 | size_t *bits) |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6275 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6276 | for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) { |
| 6277 | if (tls_id_match_table[i].tls_id == tls_id) { |
Przemek Stekiel | da4fba6 | 2023-06-02 14:52:28 +0200 | [diff] [blame] | 6278 | if (type != NULL) { |
| 6279 | *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] | 6280 | } |
| 6281 | if (bits != NULL) { |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6282 | *bits = tls_id_match_table[i].bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6283 | } |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6284 | return PSA_SUCCESS; |
| 6285 | } |
| 6286 | } |
| 6287 | |
| 6288 | return PSA_ERROR_NOT_SUPPORTED; |
| 6289 | } |
| 6290 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6291 | 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] | 6292 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6293 | for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) { |
| 6294 | if (tls_id_match_table[i].tls_id == tls_id) { |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6295 | return tls_id_match_table[i].ecp_group_id; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6296 | } |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6297 | } |
| 6298 | |
| 6299 | return MBEDTLS_ECP_DP_NONE; |
| 6300 | } |
| 6301 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6302 | 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] | 6303 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6304 | for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE; |
| 6305 | i++) { |
| 6306 | if (tls_id_match_table[i].ecp_group_id == grp_id) { |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6307 | return tls_id_match_table[i].tls_id; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6308 | } |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6309 | } |
| 6310 | |
| 6311 | return 0; |
| 6312 | } |
| 6313 | |
Valerio Setti | 67419f0 | 2023-01-04 16:12:42 +0100 | [diff] [blame] | 6314 | #if defined(MBEDTLS_DEBUG_C) |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6315 | static const struct { |
| 6316 | uint16_t tls_id; |
| 6317 | const char *name; |
| 6318 | } tls_id_curve_name_table[] = |
| 6319 | { |
Valerio Setti | 54e2379 | 2023-07-07 10:49:27 +0200 | [diff] [blame] | 6320 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, "secp521r1" }, |
| 6321 | { MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, "brainpoolP512r1" }, |
| 6322 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, "secp384r1" }, |
| 6323 | { MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, "brainpoolP384r1" }, |
| 6324 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, "secp256r1" }, |
| 6325 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256K1, "secp256k1" }, |
| 6326 | { MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, "brainpoolP256r1" }, |
| 6327 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, "secp224r1" }, |
| 6328 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224K1, "secp224k1" }, |
| 6329 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, "secp192r1" }, |
| 6330 | { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192K1, "secp192k1" }, |
| 6331 | { MBEDTLS_SSL_IANA_TLS_GROUP_X25519, "x25519" }, |
| 6332 | { MBEDTLS_SSL_IANA_TLS_GROUP_X448, "x448" }, |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6333 | { 0, NULL }, |
| 6334 | }; |
| 6335 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6336 | 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] | 6337 | { |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame] | 6338 | for (int i = 0; tls_id_curve_name_table[i].tls_id != 0; i++) { |
| 6339 | if (tls_id_curve_name_table[i].tls_id == tls_id) { |
| 6340 | return tls_id_curve_name_table[i].name; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6341 | } |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6342 | } |
| 6343 | |
| 6344 | return NULL; |
| 6345 | } |
Valerio Setti | 67419f0 | 2023-01-04 16:12:42 +0100 | [diff] [blame] | 6346 | #endif |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 6347 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 6348 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6349 | int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl, |
| 6350 | const mbedtls_md_type_t md, |
| 6351 | unsigned char *dst, |
| 6352 | size_t dst_len, |
| 6353 | size_t *olen) |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 6354 | { |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 6355 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 6356 | psa_hash_operation_t *hash_operation_to_clone; |
| 6357 | psa_hash_operation_t hash_operation = psa_hash_operation_init(); |
| 6358 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 6359 | *olen = 0; |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 6360 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6361 | switch (md) { |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 6362 | #if defined(PSA_WANT_ALG_SHA_384) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6363 | case MBEDTLS_MD_SHA384: |
| 6364 | hash_operation_to_clone = &ssl->handshake->fin_sha384_psa; |
| 6365 | break; |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 6366 | #endif |
| 6367 | |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 6368 | #if defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6369 | case MBEDTLS_MD_SHA256: |
| 6370 | hash_operation_to_clone = &ssl->handshake->fin_sha256_psa; |
| 6371 | break; |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 6372 | #endif |
| 6373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6374 | default: |
| 6375 | goto exit; |
| 6376 | } |
| 6377 | |
| 6378 | status = psa_hash_clone(hash_operation_to_clone, &hash_operation); |
| 6379 | if (status != PSA_SUCCESS) { |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 6380 | goto exit; |
| 6381 | } |
| 6382 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6383 | status = psa_hash_finish(&hash_operation, dst, dst_len, olen); |
| 6384 | if (status != PSA_SUCCESS) { |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 6385 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6386 | } |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 6387 | |
| 6388 | exit: |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 6389 | #if !defined(PSA_WANT_ALG_SHA_384) && \ |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 6390 | !defined(PSA_WANT_ALG_SHA_256) |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 6391 | (void) ssl; |
| 6392 | #endif |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 6393 | return PSA_TO_MBEDTLS_ERR(status); |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 6394 | } |
| 6395 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6396 | |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 6397 | #if defined(PSA_WANT_ALG_SHA_384) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6398 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6399 | static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl, |
| 6400 | unsigned char *dst, |
| 6401 | size_t dst_len, |
| 6402 | size_t *olen) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6403 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6404 | int ret; |
Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame] | 6405 | mbedtls_md_context_t sha384; |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6406 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6407 | if (dst_len < 48) { |
| 6408 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 6409 | } |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6410 | |
Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame] | 6411 | mbedtls_md_init(&sha384); |
| 6412 | 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] | 6413 | if (ret != 0) { |
| 6414 | goto exit; |
| 6415 | } |
Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame] | 6416 | ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384); |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 6417 | if (ret != 0) { |
| 6418 | goto exit; |
| 6419 | } |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6420 | |
Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame] | 6421 | if ((ret = mbedtls_md_finish(&sha384, dst)) != 0) { |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 6422 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6423 | goto exit; |
| 6424 | } |
| 6425 | |
| 6426 | *olen = 48; |
| 6427 | |
| 6428 | exit: |
| 6429 | |
Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame] | 6430 | mbedtls_md_free(&sha384); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6431 | return ret; |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6432 | } |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 6433 | #endif /* PSA_WANT_ALG_SHA_384 */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6434 | |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 6435 | #if defined(PSA_WANT_ALG_SHA_256) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6436 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6437 | static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl, |
| 6438 | unsigned char *dst, |
| 6439 | size_t dst_len, |
| 6440 | size_t *olen) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6441 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6442 | int ret; |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 6443 | mbedtls_md_context_t sha256; |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6444 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6445 | if (dst_len < 32) { |
| 6446 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 6447 | } |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6448 | |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 6449 | mbedtls_md_init(&sha256); |
| 6450 | ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0); |
| 6451 | if (ret != 0) { |
| 6452 | goto exit; |
| 6453 | } |
| 6454 | ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256); |
| 6455 | if (ret != 0) { |
| 6456 | goto exit; |
| 6457 | } |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 6458 | |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 6459 | if ((ret = mbedtls_md_finish(&sha256, dst)) != 0) { |
| 6460 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6461 | goto exit; |
| 6462 | } |
| 6463 | |
| 6464 | *olen = 32; |
| 6465 | |
| 6466 | exit: |
| 6467 | |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 6468 | mbedtls_md_free(&sha256); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6469 | return ret; |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6470 | } |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 6471 | #endif /* PSA_WANT_ALG_SHA_256 */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6472 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6473 | int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl, |
| 6474 | const mbedtls_md_type_t md, |
| 6475 | unsigned char *dst, |
| 6476 | size_t dst_len, |
| 6477 | size_t *olen) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6478 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6479 | switch (md) { |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 6480 | |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 6481 | #if defined(PSA_WANT_ALG_SHA_384) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6482 | case MBEDTLS_MD_SHA384: |
| 6483 | return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen); |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 6484 | #endif /* PSA_WANT_ALG_SHA_384*/ |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 6485 | |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 6486 | #if defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6487 | case MBEDTLS_MD_SHA256: |
| 6488 | return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen); |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 6489 | #endif /* PSA_WANT_ALG_SHA_256*/ |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 6490 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6491 | default: |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 6492 | #if !defined(PSA_WANT_ALG_SHA_384) && \ |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 6493 | !defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6494 | (void) ssl; |
| 6495 | (void) dst; |
| 6496 | (void) dst_len; |
| 6497 | (void) olen; |
Andrzej Kurek | 409248a | 2022-10-24 10:33:21 -0400 | [diff] [blame] | 6498 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6499 | break; |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 6500 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6501 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6502 | } |
XiaokangQian | 647719a | 2021-12-07 09:16:29 +0000 | [diff] [blame] | 6503 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 6504 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 6505 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 6506 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6507 | /* mbedtls_ssl_parse_sig_alg_ext() |
| 6508 | * |
| 6509 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 6510 | * value (TLS 1.3 RFC8446): |
| 6511 | * enum { |
| 6512 | * .... |
| 6513 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 6514 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 6515 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 6516 | * .... |
| 6517 | * } SignatureScheme; |
| 6518 | * |
| 6519 | * struct { |
| 6520 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 6521 | * } SignatureSchemeList; |
| 6522 | * |
| 6523 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 6524 | * value (TLS 1.2 RFC5246): |
| 6525 | * enum { |
| 6526 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 6527 | * sha512(6), (255) |
| 6528 | * } HashAlgorithm; |
| 6529 | * |
| 6530 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 6531 | * SignatureAlgorithm; |
| 6532 | * |
| 6533 | * struct { |
| 6534 | * HashAlgorithm hash; |
| 6535 | * SignatureAlgorithm signature; |
| 6536 | * } SignatureAndHashAlgorithm; |
| 6537 | * |
| 6538 | * SignatureAndHashAlgorithm |
| 6539 | * supported_signature_algorithms<2..2^16-2>; |
| 6540 | * |
| 6541 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 6542 | * generalization of the TLS 1.2 signature algorithm extension. |
| 6543 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 6544 | * `SignatureScheme` field of TLS 1.3 |
| 6545 | * |
| 6546 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6547 | int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl, |
| 6548 | const unsigned char *buf, |
| 6549 | const unsigned char *end) |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6550 | { |
| 6551 | const unsigned char *p = buf; |
| 6552 | size_t supported_sig_algs_len = 0; |
| 6553 | const unsigned char *supported_sig_algs_end; |
| 6554 | uint16_t sig_alg; |
| 6555 | uint32_t common_idx = 0; |
| 6556 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6557 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2); |
| 6558 | supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6559 | p += 2; |
| 6560 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6561 | memset(ssl->handshake->received_sig_algs, 0, |
| 6562 | sizeof(ssl->handshake->received_sig_algs)); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6563 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6564 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6565 | supported_sig_algs_end = p + supported_sig_algs_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6566 | while (p < supported_sig_algs_end) { |
| 6567 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2); |
| 6568 | sig_alg = MBEDTLS_GET_UINT16_BE(p, 0); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6569 | p += 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6570 | MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s", |
| 6571 | sig_alg, |
| 6572 | mbedtls_ssl_sig_alg_to_str(sig_alg))); |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 6573 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6574 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 && |
| 6575 | (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) && |
| 6576 | mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6577 | continue; |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 6578 | } |
| 6579 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6580 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6581 | MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s", |
| 6582 | mbedtls_ssl_sig_alg_to_str(sig_alg))); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6583 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6584 | if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6585 | ssl->handshake->received_sig_algs[common_idx] = sig_alg; |
| 6586 | common_idx += 1; |
| 6587 | } |
| 6588 | } |
| 6589 | /* Check that we consumed all the message. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6590 | if (p != end) { |
| 6591 | MBEDTLS_SSL_DEBUG_MSG(1, |
| 6592 | ("Signature algorithms extension length misaligned")); |
| 6593 | MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, |
| 6594 | MBEDTLS_ERR_SSL_DECODE_ERROR); |
| 6595 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6596 | } |
| 6597 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6598 | if (common_idx == 0) { |
| 6599 | MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common")); |
| 6600 | MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE, |
| 6601 | MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE); |
| 6602 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6603 | } |
| 6604 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 6605 | ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6606 | return 0; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6607 | } |
| 6608 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 6609 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 6610 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6611 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6612 | |
| 6613 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6614 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6615 | static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation, |
| 6616 | mbedtls_svc_key_id_t key, |
| 6617 | psa_algorithm_t alg, |
| 6618 | const unsigned char *raw_psk, size_t raw_psk_length, |
| 6619 | const unsigned char *seed, size_t seed_length, |
| 6620 | const unsigned char *label, size_t label_length, |
| 6621 | const unsigned char *other_secret, |
| 6622 | size_t other_secret_length, |
| 6623 | size_t capacity) |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6624 | { |
| 6625 | psa_status_t status; |
| 6626 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6627 | status = psa_key_derivation_setup(derivation, alg); |
| 6628 | if (status != PSA_SUCCESS) { |
| 6629 | return status; |
| 6630 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6631 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6632 | if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 6633 | status = psa_key_derivation_input_bytes(derivation, |
| 6634 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 6635 | seed, seed_length); |
| 6636 | if (status != PSA_SUCCESS) { |
| 6637 | return status; |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 6638 | } |
| 6639 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6640 | if (other_secret != NULL) { |
| 6641 | status = psa_key_derivation_input_bytes(derivation, |
| 6642 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 6643 | other_secret, other_secret_length); |
| 6644 | if (status != PSA_SUCCESS) { |
| 6645 | return status; |
| 6646 | } |
| 6647 | } |
| 6648 | |
| 6649 | if (mbedtls_svc_key_id_is_null(key)) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6650 | status = psa_key_derivation_input_bytes( |
| 6651 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6652 | raw_psk, raw_psk_length); |
| 6653 | } else { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6654 | status = psa_key_derivation_input_key( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6655 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6656 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6657 | if (status != PSA_SUCCESS) { |
| 6658 | return status; |
| 6659 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6660 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6661 | status = psa_key_derivation_input_bytes(derivation, |
| 6662 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 6663 | label, label_length); |
| 6664 | if (status != PSA_SUCCESS) { |
| 6665 | return status; |
| 6666 | } |
| 6667 | } else { |
| 6668 | return PSA_ERROR_NOT_SUPPORTED; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6669 | } |
| 6670 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6671 | status = psa_key_derivation_set_capacity(derivation, capacity); |
| 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 | return PSA_SUCCESS; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6677 | } |
| 6678 | |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 6679 | #if defined(PSA_WANT_ALG_SHA_384) || \ |
| 6680 | defined(PSA_WANT_ALG_SHA_256) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6681 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6682 | static int tls_prf_generic(mbedtls_md_type_t md_type, |
| 6683 | const unsigned char *secret, size_t slen, |
| 6684 | const char *label, |
| 6685 | const unsigned char *random, size_t rlen, |
| 6686 | unsigned char *dstbuf, size_t dlen) |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6687 | { |
| 6688 | psa_status_t status; |
| 6689 | psa_algorithm_t alg; |
| 6690 | mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6691 | psa_key_derivation_operation_t derivation = |
| 6692 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 6693 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6694 | if (md_type == MBEDTLS_MD_SHA384) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6695 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6696 | } else { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6697 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6698 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6699 | |
| 6700 | /* Normally a "secret" should be long enough to be impossible to |
| 6701 | * find by brute force, and in particular should not be empty. But |
| 6702 | * this PRF is also used to derive an IV, in particular in EAP-TLS, |
| 6703 | * and for this use case it makes sense to have a 0-length "secret". |
| 6704 | * Since the key API doesn't allow importing a key of length 0, |
| 6705 | * keep master_key=0, which setup_psa_key_derivation() understands |
| 6706 | * to mean a 0-length "secret" input. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6707 | if (slen != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6708 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6709 | psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE); |
| 6710 | psa_set_key_algorithm(&key_attributes, alg); |
| 6711 | psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6712 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6713 | status = psa_import_key(&key_attributes, secret, slen, &master_key); |
| 6714 | if (status != PSA_SUCCESS) { |
| 6715 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 6716 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6717 | } |
| 6718 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6719 | status = setup_psa_key_derivation(&derivation, |
| 6720 | master_key, alg, |
| 6721 | NULL, 0, |
| 6722 | random, rlen, |
| 6723 | (unsigned char const *) label, |
| 6724 | (size_t) strlen(label), |
| 6725 | NULL, 0, |
| 6726 | dlen); |
| 6727 | if (status != PSA_SUCCESS) { |
| 6728 | psa_key_derivation_abort(&derivation); |
| 6729 | psa_destroy_key(master_key); |
| 6730 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6731 | } |
| 6732 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6733 | status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen); |
| 6734 | if (status != PSA_SUCCESS) { |
| 6735 | psa_key_derivation_abort(&derivation); |
| 6736 | psa_destroy_key(master_key); |
| 6737 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6738 | } |
| 6739 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6740 | status = psa_key_derivation_abort(&derivation); |
| 6741 | if (status != PSA_SUCCESS) { |
| 6742 | psa_destroy_key(master_key); |
| 6743 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6744 | } |
| 6745 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6746 | if (!mbedtls_svc_key_id_is_null(master_key)) { |
| 6747 | status = psa_destroy_key(master_key); |
| 6748 | } |
| 6749 | if (status != PSA_SUCCESS) { |
| 6750 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 6751 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6752 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6753 | return 0; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6754 | } |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 6755 | #endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6756 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6757 | |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 6758 | #if defined(MBEDTLS_MD_C) && \ |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 6759 | (defined(PSA_WANT_ALG_SHA_256) || \ |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 6760 | defined(PSA_WANT_ALG_SHA_384)) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6761 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6762 | static int tls_prf_generic(mbedtls_md_type_t md_type, |
| 6763 | const unsigned char *secret, size_t slen, |
| 6764 | const char *label, |
| 6765 | const unsigned char *random, size_t rlen, |
| 6766 | unsigned char *dstbuf, size_t dlen) |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6767 | { |
| 6768 | size_t nb; |
| 6769 | size_t i, j, k, md_len; |
| 6770 | unsigned char *tmp; |
| 6771 | size_t tmp_len = 0; |
| 6772 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 6773 | const mbedtls_md_info_t *md_info; |
| 6774 | mbedtls_md_context_t md_ctx; |
| 6775 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6776 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6777 | mbedtls_md_init(&md_ctx); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6778 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6779 | if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) { |
| 6780 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 6781 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6782 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6783 | md_len = mbedtls_md_get_size(md_info); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6784 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6785 | tmp_len = md_len + strlen(label) + rlen; |
| 6786 | tmp = mbedtls_calloc(1, tmp_len); |
| 6787 | if (tmp == NULL) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6788 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 6789 | goto exit; |
| 6790 | } |
| 6791 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6792 | nb = strlen(label); |
| 6793 | memcpy(tmp + md_len, label, nb); |
| 6794 | memcpy(tmp + md_len + nb, random, rlen); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6795 | nb += rlen; |
| 6796 | |
| 6797 | /* |
| 6798 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 6799 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6800 | if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6801 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6802 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6803 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6804 | ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen); |
| 6805 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6806 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6807 | } |
| 6808 | ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb); |
| 6809 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6810 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6811 | } |
| 6812 | ret = mbedtls_md_hmac_finish(&md_ctx, tmp); |
| 6813 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6814 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6815 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6816 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6817 | for (i = 0; i < dlen; i += md_len) { |
| 6818 | ret = mbedtls_md_hmac_reset(&md_ctx); |
| 6819 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6820 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6821 | } |
| 6822 | ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb); |
| 6823 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6824 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6825 | } |
| 6826 | ret = mbedtls_md_hmac_finish(&md_ctx, h_i); |
| 6827 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6828 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6829 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6830 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6831 | ret = mbedtls_md_hmac_reset(&md_ctx); |
| 6832 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6833 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6834 | } |
| 6835 | ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len); |
| 6836 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6837 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6838 | } |
| 6839 | ret = mbedtls_md_hmac_finish(&md_ctx, tmp); |
| 6840 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6841 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6842 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6843 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6844 | k = (i + md_len > dlen) ? dlen % md_len : md_len; |
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 | for (j = 0; j < k; j++) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6847 | dstbuf[i + j] = h_i[j]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6848 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6849 | } |
| 6850 | |
| 6851 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6852 | mbedtls_md_free(&md_ctx); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6853 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6854 | if (tmp != NULL) { |
| 6855 | mbedtls_platform_zeroize(tmp, tmp_len); |
| 6856 | } |
Dave Rodgman | 29b9b2b | 2022-11-01 16:08:14 +0000 | [diff] [blame] | 6857 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6858 | mbedtls_platform_zeroize(h_i, sizeof(h_i)); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6859 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6860 | mbedtls_free(tmp); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6861 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6862 | return ret; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6863 | } |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 6864 | #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] | 6865 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6866 | |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 6867 | #if defined(PSA_WANT_ALG_SHA_256) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6868 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6869 | static int tls_prf_sha256(const unsigned char *secret, size_t slen, |
| 6870 | const char *label, |
| 6871 | const unsigned char *random, size_t rlen, |
| 6872 | unsigned char *dstbuf, size_t dlen) |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6873 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6874 | return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen, |
| 6875 | label, random, rlen, dstbuf, dlen); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6876 | } |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 6877 | #endif /* PSA_WANT_ALG_SHA_256*/ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6878 | |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 6879 | #if defined(PSA_WANT_ALG_SHA_384) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6880 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6881 | static int tls_prf_sha384(const unsigned char *secret, size_t slen, |
| 6882 | const char *label, |
| 6883 | const unsigned char *random, size_t rlen, |
| 6884 | unsigned char *dstbuf, size_t dlen) |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6885 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6886 | return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen, |
| 6887 | label, random, rlen, dstbuf, dlen); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6888 | } |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 6889 | #endif /* PSA_WANT_ALG_SHA_384*/ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6890 | |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6891 | /* |
| 6892 | * Set appropriate PRF function and other SSL / TLS1.2 functions |
| 6893 | * |
| 6894 | * Inputs: |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6895 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
| 6896 | * |
| 6897 | * Outputs: |
| 6898 | * - the tls_prf, calc_verify and calc_finished members of handshake structure |
| 6899 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6900 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6901 | static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake, |
| 6902 | mbedtls_md_type_t hash) |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6903 | { |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 6904 | #if defined(PSA_WANT_ALG_SHA_384) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6905 | if (hash == MBEDTLS_MD_SHA384) { |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6906 | handshake->tls_prf = tls_prf_sha384; |
| 6907 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 6908 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6909 | } else |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6910 | #endif |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 6911 | #if defined(PSA_WANT_ALG_SHA_256) |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6912 | { |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 6913 | (void) hash; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6914 | handshake->tls_prf = tls_prf_sha256; |
| 6915 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 6916 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
| 6917 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 6918 | #else |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6919 | { |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6920 | (void) handshake; |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 6921 | (void) hash; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6922 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6923 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 6924 | #endif |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6925 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6926 | return 0; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6927 | } |
Jerry Yu | d6ab235 | 2022-02-17 14:03:43 +0800 | [diff] [blame] | 6928 | |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6929 | /* |
| 6930 | * Compute master secret if needed |
| 6931 | * |
| 6932 | * Parameters: |
| 6933 | * [in/out] handshake |
| 6934 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf |
| 6935 | * (PSA-PSK) ciphersuite_info, psk_opaque |
| 6936 | * [out] premaster (cleared) |
| 6937 | * [out] master |
| 6938 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK |
| 6939 | * debug: conf->f_dbg, conf->p_dbg |
| 6940 | * EMS: passed to calc_verify (debug + session_negotiate) |
Ronald Cron | a25cf58 | 2022-03-07 11:10:36 +0100 | [diff] [blame] | 6941 | * PSA-PSA: conf |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6942 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6943 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6944 | static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake, |
| 6945 | unsigned char *master, |
| 6946 | const mbedtls_ssl_context *ssl) |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6947 | { |
| 6948 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6949 | |
| 6950 | /* cf. RFC 5246, Section 8.1: |
| 6951 | * "The master secret is always exactly 48 bytes in length." */ |
| 6952 | size_t const master_secret_len = 48; |
| 6953 | |
| 6954 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 6955 | unsigned char session_hash[48]; |
| 6956 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 6957 | |
| 6958 | /* The label for the KDF used for key expansion. |
| 6959 | * This is either "master secret" or "extended master secret" |
| 6960 | * depending on whether the Extended Master Secret extension |
| 6961 | * is used. */ |
| 6962 | char const *lbl = "master secret"; |
| 6963 | |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 6964 | /* The seed for the KDF used for key expansion. |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6965 | * - If the Extended Master Secret extension is not used, |
| 6966 | * this is ClientHello.Random + ServerHello.Random |
| 6967 | * (see Sect. 8.1 in RFC 5246). |
| 6968 | * - If the Extended Master Secret extension is used, |
| 6969 | * this is the transcript of the handshake so far. |
| 6970 | * (see Sect. 4 in RFC 7627). */ |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 6971 | unsigned char const *seed = handshake->randbytes; |
| 6972 | size_t seed_len = 64; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6973 | |
| 6974 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 6975 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ |
| 6976 | !(defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6977 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6978 | ssl = NULL; /* make sure we don't use it except for those cases */ |
| 6979 | (void) ssl; |
| 6980 | #endif |
| 6981 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6982 | if (handshake->resume != 0) { |
| 6983 | MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)")); |
| 6984 | return 0; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6985 | } |
| 6986 | |
| 6987 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6988 | if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) { |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6989 | lbl = "extended master secret"; |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 6990 | seed = session_hash; |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 6991 | ret = handshake->calc_verify(ssl, session_hash, &seed_len); |
| 6992 | if (ret != 0) { |
| 6993 | MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret); |
| 6994 | } |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6995 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6996 | MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret", |
| 6997 | session_hash, seed_len); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6998 | } |
Przemek Stekiel | 169bf0b | 2022-04-29 07:53:29 +0200 | [diff] [blame] | 6999 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7000 | |
Przemek Stekiel | 99114f3 | 2022-04-22 11:20:09 +0200 | [diff] [blame] | 7001 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Przemek Stekiel | 8a4b7fd | 2022-04-28 09:22:22 +0200 | [diff] [blame] | 7002 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7003 | if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) { |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7004 | /* Perform PSK-to-MS expansion in a single step. */ |
| 7005 | psa_status_t status; |
| 7006 | psa_algorithm_t alg; |
| 7007 | mbedtls_svc_key_id_t psk; |
| 7008 | psa_key_derivation_operation_t derivation = |
| 7009 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Dave Rodgman | 2eab462 | 2023-10-05 13:30:37 +0100 | [diff] [blame] | 7010 | 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] | 7011 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7012 | MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion")); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7013 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7014 | psk = mbedtls_ssl_get_opaque_psk(ssl); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7015 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7016 | if (hash_alg == MBEDTLS_MD_SHA384) { |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7017 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7018 | } else { |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7019 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7020 | } |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7021 | |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 7022 | size_t other_secret_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7023 | unsigned char *other_secret = NULL; |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 7024 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7025 | switch (handshake->ciphersuite_info->key_exchange) { |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 7026 | /* Provide other secret. |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 7027 | * Other secret is stored in premaster, where first 2 bytes hold the |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 7028 | * length of the other key. |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 7029 | */ |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 7030 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 7031 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 7032 | other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0); |
| 7033 | other_secret = handshake->premaster + 2; |
| 7034 | break; |
| 7035 | default: |
| 7036 | break; |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 7037 | } |
| 7038 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7039 | status = setup_psa_key_derivation(&derivation, psk, alg, |
| 7040 | ssl->conf->psk, ssl->conf->psk_len, |
| 7041 | seed, seed_len, |
| 7042 | (unsigned char const *) lbl, |
| 7043 | (size_t) strlen(lbl), |
| 7044 | other_secret, other_secret_len, |
| 7045 | master_secret_len); |
| 7046 | if (status != PSA_SUCCESS) { |
| 7047 | psa_key_derivation_abort(&derivation); |
| 7048 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7049 | } |
| 7050 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7051 | status = psa_key_derivation_output_bytes(&derivation, |
| 7052 | master, |
| 7053 | master_secret_len); |
| 7054 | if (status != PSA_SUCCESS) { |
| 7055 | psa_key_derivation_abort(&derivation); |
| 7056 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7057 | } |
| 7058 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7059 | status = psa_key_derivation_abort(&derivation); |
| 7060 | if (status != PSA_SUCCESS) { |
| 7061 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 7062 | } |
| 7063 | } else |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7064 | #endif |
| 7065 | { |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 7066 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7067 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 7068 | if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) { |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 7069 | psa_status_t status; |
| 7070 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 7071 | psa_key_derivation_operation_t derivation = |
| 7072 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7073 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7074 | MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE")); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 7075 | |
| 7076 | handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE; |
| 7077 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7078 | status = psa_key_derivation_setup(&derivation, alg); |
| 7079 | if (status != PSA_SUCCESS) { |
| 7080 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 7081 | } |
| 7082 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7083 | status = psa_key_derivation_set_capacity(&derivation, |
| 7084 | PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE); |
| 7085 | if (status != PSA_SUCCESS) { |
| 7086 | psa_key_derivation_abort(&derivation); |
| 7087 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 7088 | } |
| 7089 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7090 | status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx, |
| 7091 | &derivation); |
| 7092 | if (status != PSA_SUCCESS) { |
| 7093 | psa_key_derivation_abort(&derivation); |
| 7094 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 7095 | } |
| 7096 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7097 | status = psa_key_derivation_output_bytes(&derivation, |
| 7098 | handshake->premaster, |
| 7099 | handshake->pmslen); |
| 7100 | if (status != PSA_SUCCESS) { |
| 7101 | psa_key_derivation_abort(&derivation); |
| 7102 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 7103 | } |
| 7104 | |
| 7105 | status = psa_key_derivation_abort(&derivation); |
| 7106 | if (status != PSA_SUCCESS) { |
| 7107 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 7108 | } |
| 7109 | } |
| 7110 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7111 | ret = handshake->tls_prf(handshake->premaster, handshake->pmslen, |
| 7112 | lbl, seed, seed_len, |
| 7113 | master, |
| 7114 | master_secret_len); |
| 7115 | if (ret != 0) { |
| 7116 | MBEDTLS_SSL_DEBUG_RET(1, "prf", ret); |
| 7117 | return ret; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7118 | } |
| 7119 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7120 | MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret", |
| 7121 | handshake->premaster, |
| 7122 | handshake->pmslen); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7123 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7124 | mbedtls_platform_zeroize(handshake->premaster, |
| 7125 | sizeof(handshake->premaster)); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7126 | } |
| 7127 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7128 | return 0; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 7129 | } |
| 7130 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7131 | int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl) |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 7132 | { |
| 7133 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7134 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 7135 | ssl->handshake->ciphersuite_info; |
| 7136 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7137 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys")); |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 7138 | |
| 7139 | /* Set PRF, calc_verify and calc_finished function pointers */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7140 | ret = ssl_set_handshake_prfs(ssl->handshake, |
Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 7141 | (mbedtls_md_type_t) ciphersuite_info->mac); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7142 | if (ret != 0) { |
| 7143 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret); |
| 7144 | return ret; |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 7145 | } |
| 7146 | |
| 7147 | /* Compute master secret if needed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7148 | ret = ssl_compute_master(ssl->handshake, |
| 7149 | ssl->session_negotiate->master, |
| 7150 | ssl); |
| 7151 | if (ret != 0) { |
| 7152 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret); |
| 7153 | return ret; |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 7154 | } |
| 7155 | |
| 7156 | /* Swap the client and server random values: |
| 7157 | * - MS derivation wanted client+server (RFC 5246 8.1) |
| 7158 | * - key derivation wants server+client (RFC 5246 6.3) */ |
| 7159 | { |
| 7160 | unsigned char tmp[64]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7161 | memcpy(tmp, ssl->handshake->randbytes, 64); |
| 7162 | memcpy(ssl->handshake->randbytes, tmp + 32, 32); |
| 7163 | memcpy(ssl->handshake->randbytes + 32, tmp, 32); |
| 7164 | mbedtls_platform_zeroize(tmp, sizeof(tmp)); |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 7165 | } |
| 7166 | |
| 7167 | /* Populate transform structure */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7168 | ret = ssl_tls12_populate_transform(ssl->transform_negotiate, |
| 7169 | ssl->session_negotiate->ciphersuite, |
| 7170 | ssl->session_negotiate->master, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7171 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7172 | ssl->session_negotiate->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7173 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7174 | ssl->handshake->tls_prf, |
| 7175 | ssl->handshake->randbytes, |
| 7176 | ssl->tls_version, |
| 7177 | ssl->conf->endpoint, |
| 7178 | ssl); |
| 7179 | if (ret != 0) { |
| 7180 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret); |
| 7181 | return ret; |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 7182 | } |
| 7183 | |
| 7184 | /* We no longer need Server/ClientHello.random values */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7185 | mbedtls_platform_zeroize(ssl->handshake->randbytes, |
| 7186 | sizeof(ssl->handshake->randbytes)); |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 7187 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7188 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys")); |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 7189 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7190 | return 0; |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 7191 | } |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 7192 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7193 | 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] | 7194 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7195 | switch (md) { |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 7196 | #if defined(PSA_WANT_ALG_SHA_384) |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 7197 | case MBEDTLS_SSL_HASH_SHA384: |
| 7198 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 7199 | break; |
| 7200 | #endif |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 7201 | #if defined(PSA_WANT_ALG_SHA_256) |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 7202 | case MBEDTLS_SSL_HASH_SHA256: |
| 7203 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 7204 | break; |
| 7205 | #endif |
| 7206 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7207 | return -1; |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 7208 | } |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 7209 | #if !defined(PSA_WANT_ALG_SHA_384) && \ |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 7210 | !defined(PSA_WANT_ALG_SHA_256) |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 7211 | (void) ssl; |
| 7212 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7213 | return 0; |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 7214 | } |
| 7215 | |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7216 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7217 | static int ssl_calc_verify_tls_psa(const mbedtls_ssl_context *ssl, |
| 7218 | const psa_hash_operation_t *hs_op, |
| 7219 | size_t buffer_size, |
| 7220 | unsigned char *hash, |
| 7221 | size_t *hlen) |
| 7222 | { |
| 7223 | psa_status_t status; |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7224 | psa_hash_operation_t cloned_op = psa_hash_operation_init(); |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7225 | |
| 7226 | #if !defined(MBEDTLS_DEBUG_C) |
| 7227 | (void) ssl; |
| 7228 | #endif |
| 7229 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify")); |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7230 | status = psa_hash_clone(hs_op, &cloned_op); |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7231 | if (status != PSA_SUCCESS) { |
| 7232 | goto exit; |
| 7233 | } |
| 7234 | |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7235 | status = psa_hash_finish(&cloned_op, hash, buffer_size, hlen); |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7236 | if (status != PSA_SUCCESS) { |
| 7237 | goto exit; |
| 7238 | } |
| 7239 | |
| 7240 | MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen); |
| 7241 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify")); |
| 7242 | |
| 7243 | exit: |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7244 | psa_hash_abort(&cloned_op); |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7245 | return mbedtls_md_error_from_psa(status); |
| 7246 | } |
| 7247 | #else |
| 7248 | static int ssl_calc_verify_tls_legacy(const mbedtls_ssl_context *ssl, |
| 7249 | const mbedtls_md_context_t *hs_ctx, |
| 7250 | unsigned char *hash, |
| 7251 | size_t *hlen) |
| 7252 | { |
| 7253 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7254 | mbedtls_md_context_t cloned_ctx; |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7255 | |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7256 | mbedtls_md_init(&cloned_ctx); |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7257 | |
| 7258 | #if !defined(MBEDTLS_DEBUG_C) |
| 7259 | (void) ssl; |
| 7260 | #endif |
| 7261 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify")); |
| 7262 | |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7263 | 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] | 7264 | if (ret != 0) { |
| 7265 | goto exit; |
| 7266 | } |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7267 | ret = mbedtls_md_clone(&cloned_ctx, hs_ctx); |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7268 | if (ret != 0) { |
| 7269 | goto exit; |
| 7270 | } |
| 7271 | |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7272 | ret = mbedtls_md_finish(&cloned_ctx, hash); |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7273 | if (ret != 0) { |
| 7274 | goto exit; |
| 7275 | } |
| 7276 | |
| 7277 | *hlen = mbedtls_md_get_size(mbedtls_md_info_from_ctx(hs_ctx)); |
| 7278 | |
| 7279 | MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen); |
| 7280 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify")); |
| 7281 | |
| 7282 | exit: |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 7283 | mbedtls_md_free(&cloned_ctx); |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7284 | return ret; |
| 7285 | } |
| 7286 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7287 | |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 7288 | #if defined(PSA_WANT_ALG_SHA_256) |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 7289 | int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl, |
| 7290 | unsigned char *hash, |
| 7291 | size_t *hlen) |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 7292 | { |
| 7293 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7294 | return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha256_psa, 32, |
| 7295 | hash, hlen); |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 7296 | #else |
Manuel Pégourié-Gonnard | 7497066 | 2023-06-24 09:43:26 +0200 | [diff] [blame] | 7297 | return ssl_calc_verify_tls_legacy(ssl, &ssl->handshake->fin_sha256, |
| 7298 | hash, hlen); |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 7299 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 7300 | } |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 7301 | #endif /* PSA_WANT_ALG_SHA_256 */ |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 7302 | |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 7303 | #if defined(PSA_WANT_ALG_SHA_384) |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 7304 | int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl, |
| 7305 | unsigned char *hash, |
| 7306 | size_t *hlen) |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +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_sha384_psa, 48, |
| 7310 | hash, hlen); |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +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_sha384, |
| 7313 | hash, hlen); |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 7314 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 7315 | } |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 7316 | #endif /* PSA_WANT_ALG_SHA_384 */ |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 7317 | |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 7318 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 7319 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7320 | 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] | 7321 | { |
| 7322 | unsigned char *p = ssl->handshake->premaster; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7323 | unsigned char *end = p + sizeof(ssl->handshake->premaster); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7324 | const unsigned char *psk = NULL; |
| 7325 | size_t psk_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7326 | int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7327 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7328 | if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) { |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7329 | /* |
| 7330 | * This should never happen because the existence of a PSK is always |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 7331 | * checked before calling this function. |
| 7332 | * |
| 7333 | * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with |
Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 7334 | * the shared secret without PSK. |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7335 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7336 | if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) { |
| 7337 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 7338 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 7339 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7340 | } |
| 7341 | |
| 7342 | /* |
| 7343 | * PMS = struct { |
| 7344 | * opaque other_secret<0..2^16-1>; |
| 7345 | * opaque psk<0..2^16-1>; |
| 7346 | * }; |
| 7347 | * with "other_secret" depending on the particular key exchange |
| 7348 | */ |
| 7349 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7350 | if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) { |
| 7351 | if (end - p < 2) { |
| 7352 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 7353 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7354 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7355 | MBEDTLS_PUT_UINT16_BE(psk_len, p, 0); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7356 | p += 2; |
| 7357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7358 | if (end < p || (size_t) (end - p) < psk_len) { |
| 7359 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 7360 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7361 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7362 | memset(p, 0, psk_len); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7363 | p += psk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7364 | } else |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7365 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7366 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7367 | if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) { |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7368 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7369 | size_t len; |
| 7370 | |
| 7371 | /* Write length only when we know the actual value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7372 | if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx, |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 7373 | p + 2, (size_t) (end - (p + 2)), &len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7374 | ssl->conf->f_rng, ssl->conf->p_rng)) != 0) { |
| 7375 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret); |
| 7376 | return ret; |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7377 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7378 | MBEDTLS_PUT_UINT16_BE(len, p, 0); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7379 | p += 2 + len; |
| 7380 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7381 | MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K); |
| 7382 | } else |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7383 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 7384 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7385 | if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) { |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7386 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7387 | size_t zlen; |
| 7388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7389 | if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen, |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 7390 | p + 2, (size_t) (end - (p + 2)), |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7391 | ssl->conf->f_rng, ssl->conf->p_rng)) != 0) { |
| 7392 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret); |
| 7393 | return ret; |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7394 | } |
| 7395 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7396 | MBEDTLS_PUT_UINT16_BE(zlen, p, 0); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7397 | p += 2 + zlen; |
| 7398 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7399 | MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx, |
| 7400 | MBEDTLS_DEBUG_ECDH_Z); |
| 7401 | } else |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 7402 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7403 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7404 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 7405 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7406 | } |
| 7407 | |
| 7408 | /* opaque psk<0..2^16-1>; */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7409 | if (end - p < 2) { |
| 7410 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 7411 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7412 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7413 | MBEDTLS_PUT_UINT16_BE(psk_len, p, 0); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7414 | p += 2; |
| 7415 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7416 | if (end < p || (size_t) (end - p) < psk_len) { |
| 7417 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 7418 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7419 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7420 | memcpy(p, psk, psk_len); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7421 | p += psk_len; |
| 7422 | |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 7423 | ssl->handshake->pmslen = (size_t) (p - ssl->handshake->premaster); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7424 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7425 | return 0; |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 7426 | } |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 7427 | #endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 7428 | |
| 7429 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7430 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7431 | static int ssl_write_hello_request(mbedtls_ssl_context *ssl); |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 7432 | |
| 7433 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7434 | int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl) |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 7435 | { |
| 7436 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 7437 | * timeout if we were using the usual handshake doubling scheme */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7438 | if (ssl->conf->renego_max_records < 0) { |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 7439 | uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1; |
| 7440 | unsigned char doublings = 1; |
| 7441 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7442 | while (ratio != 0) { |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 7443 | ++doublings; |
| 7444 | ratio >>= 1; |
| 7445 | } |
| 7446 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7447 | if (++ssl->renego_records_seen > doublings) { |
| 7448 | MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request")); |
| 7449 | return 0; |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 7450 | } |
| 7451 | } |
| 7452 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7453 | return ssl_write_hello_request(ssl); |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 7454 | } |
| 7455 | #endif |
| 7456 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7457 | |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7458 | /* |
| 7459 | * Handshake functions |
| 7460 | */ |
| 7461 | #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 7462 | /* No certificate support -> dummy functions */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7463 | int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7464 | { |
| 7465 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 7466 | ssl->handshake->ciphersuite_info; |
| 7467 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7468 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7469 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7470 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { |
| 7471 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7472 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7473 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7474 | } |
| 7475 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7476 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 7477 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7478 | } |
| 7479 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7480 | int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7481 | { |
| 7482 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 7483 | ssl->handshake->ciphersuite_info; |
| 7484 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7485 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7486 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7487 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { |
| 7488 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7489 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7490 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7491 | } |
| 7492 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7493 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 7494 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7495 | } |
| 7496 | |
| 7497 | #else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 7498 | /* Some certificate support -> implement write and parse */ |
| 7499 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7500 | int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7501 | { |
| 7502 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 7503 | size_t i, n; |
| 7504 | const mbedtls_x509_crt *crt; |
| 7505 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 7506 | ssl->handshake->ciphersuite_info; |
| 7507 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7508 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7509 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7510 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { |
| 7511 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7512 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7513 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7514 | } |
| 7515 | |
| 7516 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7517 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 7518 | if (ssl->handshake->client_auth == 0) { |
| 7519 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7520 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7521 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7522 | } |
| 7523 | } |
| 7524 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 7525 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7526 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 7527 | if (mbedtls_ssl_own_cert(ssl) == NULL) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7528 | /* Should never happen because we shouldn't have picked the |
| 7529 | * ciphersuite if we don't have a certificate. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7530 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7531 | } |
| 7532 | } |
| 7533 | #endif |
| 7534 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7535 | MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl)); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7536 | |
| 7537 | /* |
| 7538 | * 0 . 0 handshake type |
| 7539 | * 1 . 3 handshake length |
| 7540 | * 4 . 6 length of all certs |
| 7541 | * 7 . 9 length of cert. 1 |
| 7542 | * 10 . n-1 peer certificate |
| 7543 | * n . n+2 length of cert. 2 |
| 7544 | * n+3 . ... upper level cert, etc. |
| 7545 | */ |
| 7546 | i = 7; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7547 | crt = mbedtls_ssl_own_cert(ssl); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7548 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7549 | while (crt != NULL) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7550 | n = crt->raw.len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7551 | if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) { |
| 7552 | MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET |
| 7553 | " > %" MBEDTLS_PRINTF_SIZET, |
| 7554 | i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN)); |
| 7555 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7556 | } |
| 7557 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7558 | ssl->out_msg[i] = MBEDTLS_BYTE_2(n); |
| 7559 | ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n); |
| 7560 | ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7562 | i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7563 | i += n; crt = crt->next; |
| 7564 | } |
| 7565 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7566 | ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7); |
| 7567 | ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7); |
| 7568 | ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7569 | |
| 7570 | ssl->out_msglen = i; |
| 7571 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 7572 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
| 7573 | |
| 7574 | ssl->state++; |
| 7575 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7576 | if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { |
| 7577 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret); |
| 7578 | return ret; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7579 | } |
| 7580 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7581 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7582 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7583 | return ret; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7584 | } |
| 7585 | |
| 7586 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 7587 | |
| 7588 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7589 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7590 | static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl, |
| 7591 | unsigned char *crt_buf, |
| 7592 | size_t crt_buf_len) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7593 | { |
| 7594 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; |
| 7595 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7596 | if (peer_crt == NULL) { |
| 7597 | return -1; |
| 7598 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7599 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7600 | if (peer_crt->raw.len != crt_buf_len) { |
| 7601 | return -1; |
| 7602 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7603 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7604 | return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7605 | } |
| 7606 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7607 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7608 | static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl, |
| 7609 | unsigned char *crt_buf, |
| 7610 | size_t crt_buf_len) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7611 | { |
| 7612 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7613 | unsigned char const * const peer_cert_digest = |
| 7614 | ssl->session->peer_cert_digest; |
| 7615 | mbedtls_md_type_t const peer_cert_digest_type = |
| 7616 | ssl->session->peer_cert_digest_type; |
| 7617 | mbedtls_md_info_t const * const digest_info = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7618 | mbedtls_md_info_from_type(peer_cert_digest_type); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7619 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; |
| 7620 | size_t digest_len; |
| 7621 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7622 | if (peer_cert_digest == NULL || digest_info == NULL) { |
| 7623 | return -1; |
| 7624 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7625 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7626 | digest_len = mbedtls_md_get_size(digest_info); |
| 7627 | if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) { |
| 7628 | return -1; |
| 7629 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7630 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7631 | ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest); |
| 7632 | if (ret != 0) { |
| 7633 | return -1; |
| 7634 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7635 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7636 | return memcmp(tmp_digest, peer_cert_digest, digest_len); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7637 | } |
| 7638 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7639 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 7640 | |
| 7641 | /* |
| 7642 | * Once the certificate message is read, parse it into a cert chain and |
| 7643 | * perform basic checks, but leave actual verification to the caller |
| 7644 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7645 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7646 | static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl, |
| 7647 | mbedtls_x509_crt *chain) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7648 | { |
| 7649 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7650 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7651 | int crt_cnt = 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7652 | #endif |
| 7653 | size_t i, n; |
| 7654 | uint8_t alert; |
| 7655 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7656 | if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) { |
| 7657 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7658 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7659 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
| 7660 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7661 | } |
| 7662 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7663 | if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) { |
| 7664 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7665 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
| 7666 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7667 | } |
| 7668 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7669 | if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) { |
| 7670 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7671 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7672 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
| 7673 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7674 | } |
| 7675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7676 | i = mbedtls_ssl_hs_hdr_len(ssl); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7677 | |
| 7678 | /* |
| 7679 | * Same message structure as in mbedtls_ssl_write_certificate() |
| 7680 | */ |
Dave Rodgman | a3d0f61 | 2023-11-03 23:34:02 +0000 | [diff] [blame] | 7681 | n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7682 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7683 | if (ssl->in_msg[i] != 0 || |
| 7684 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) { |
| 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 | |
| 7691 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 7692 | i += 3; |
| 7693 | |
| 7694 | /* Iterate through and parse the CRTs in the provided chain. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7695 | while (i < ssl->in_hslen) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7696 | /* Check that there's room for the next CRT's length fields. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7697 | if (i + 3 > ssl->in_hslen) { |
| 7698 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7699 | mbedtls_ssl_send_alert_message(ssl, |
| 7700 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7701 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
| 7702 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7703 | } |
| 7704 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 7705 | * anything beyond 2**16 ~ 64K. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7706 | if (ssl->in_msg[i] != 0) { |
| 7707 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7708 | mbedtls_ssl_send_alert_message(ssl, |
| 7709 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7710 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT); |
| 7711 | return MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7712 | } |
| 7713 | |
| 7714 | /* Read length of the next CRT in the chain. */ |
Dave Rodgman | a3d0f61 | 2023-11-03 23:34:02 +0000 | [diff] [blame] | 7715 | n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7716 | i += 3; |
| 7717 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7718 | if (n < 128 || i + n > ssl->in_hslen) { |
| 7719 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7720 | mbedtls_ssl_send_alert_message(ssl, |
| 7721 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7722 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
| 7723 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7724 | } |
| 7725 | |
| 7726 | /* Check if we're handling the first CRT in the chain. */ |
| 7727 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7728 | if (crt_cnt++ == 0 && |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7729 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7730 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7731 | /* During client-side renegotiation, check that the server's |
| 7732 | * end-CRTs hasn't changed compared to the initial handshake, |
| 7733 | * mitigating the triple handshake attack. On success, reuse |
| 7734 | * the original end-CRT instead of parsing it again. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7735 | MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation")); |
| 7736 | if (ssl_check_peer_crt_unchanged(ssl, |
| 7737 | &ssl->in_msg[i], |
| 7738 | n) != 0) { |
| 7739 | MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation")); |
| 7740 | mbedtls_ssl_send_alert_message(ssl, |
| 7741 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7742 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED); |
| 7743 | return MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7744 | } |
| 7745 | |
| 7746 | /* Now we can safely free the original chain. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7747 | ssl_clear_peer_cert(ssl->session); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7748 | } |
| 7749 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 7750 | |
| 7751 | /* Parse the next certificate in the chain. */ |
| 7752 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7753 | ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7754 | #else |
| 7755 | /* If we don't need to store the CRT chain permanently, parse |
| 7756 | * it in-place from the input buffer instead of making a copy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7757 | 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] | 7758 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7759 | switch (ret) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7760 | case 0: /*ok*/ |
| 7761 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 7762 | /* Ignore certificate with an unknown algorithm: maybe a |
| 7763 | prior certificate was already trusted. */ |
| 7764 | break; |
| 7765 | |
| 7766 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 7767 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 7768 | goto crt_parse_der_failed; |
| 7769 | |
| 7770 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 7771 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7772 | goto crt_parse_der_failed; |
| 7773 | |
| 7774 | default: |
| 7775 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7776 | crt_parse_der_failed: |
| 7777 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert); |
| 7778 | MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret); |
| 7779 | return ret; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7780 | } |
| 7781 | |
| 7782 | i += n; |
| 7783 | } |
| 7784 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7785 | MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain); |
| 7786 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7787 | } |
| 7788 | |
| 7789 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7790 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7791 | 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] | 7792 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7793 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 7794 | return -1; |
| 7795 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7796 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7797 | if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) && |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7798 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 7799 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7800 | memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) { |
| 7801 | MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate")); |
| 7802 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7803 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7804 | return -1; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7805 | } |
| 7806 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7807 | |
| 7808 | /* Check if a certificate message is expected. |
| 7809 | * Return either |
| 7810 | * - SSL_CERTIFICATE_EXPECTED, or |
| 7811 | * - SSL_CERTIFICATE_SKIP |
| 7812 | * indicating whether a Certificate message is expected or not. |
| 7813 | */ |
| 7814 | #define SSL_CERTIFICATE_EXPECTED 0 |
| 7815 | #define SSL_CERTIFICATE_SKIP 1 |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7816 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7817 | static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl, |
| 7818 | int authmode) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7819 | { |
| 7820 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 7821 | ssl->handshake->ciphersuite_info; |
| 7822 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7823 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { |
| 7824 | return SSL_CERTIFICATE_SKIP; |
| 7825 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7826 | |
| 7827 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7828 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7829 | if (authmode == MBEDTLS_SSL_VERIFY_NONE) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7830 | ssl->session_negotiate->verify_result = |
| 7831 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7832 | return SSL_CERTIFICATE_SKIP; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7833 | } |
| 7834 | } |
| 7835 | #else |
| 7836 | ((void) authmode); |
| 7837 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7838 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7839 | return SSL_CERTIFICATE_EXPECTED; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7840 | } |
| 7841 | |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7842 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7843 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7844 | static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl, |
| 7845 | unsigned char *start, size_t len) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7846 | { |
| 7847 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7848 | /* Remember digest of the peer's end-CRT. */ |
| 7849 | ssl->session_negotiate->peer_cert_digest = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7850 | mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN); |
| 7851 | if (ssl->session_negotiate->peer_cert_digest == NULL) { |
| 7852 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed", |
| 7853 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN)); |
| 7854 | mbedtls_ssl_send_alert_message(ssl, |
| 7855 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7856 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7857 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7858 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7859 | } |
| 7860 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7861 | ret = mbedtls_md(mbedtls_md_info_from_type( |
| 7862 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE), |
| 7863 | start, len, |
| 7864 | ssl->session_negotiate->peer_cert_digest); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7865 | |
| 7866 | ssl->session_negotiate->peer_cert_digest_type = |
| 7867 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 7868 | ssl->session_negotiate->peer_cert_digest_len = |
| 7869 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 7870 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7871 | return ret; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7872 | } |
| 7873 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7874 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7875 | static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl, |
| 7876 | unsigned char *start, size_t len) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7877 | { |
| 7878 | unsigned char *end = start + len; |
| 7879 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7880 | |
| 7881 | /* Make a copy of the peer's raw public key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7882 | mbedtls_pk_init(&ssl->handshake->peer_pubkey); |
| 7883 | ret = mbedtls_pk_parse_subpubkey(&start, end, |
| 7884 | &ssl->handshake->peer_pubkey); |
| 7885 | if (ret != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7886 | /* We should have parsed the public key before. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7887 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7888 | } |
| 7889 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7890 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7891 | } |
| 7892 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7893 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7894 | int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7895 | { |
| 7896 | int ret = 0; |
| 7897 | int crt_expected; |
Manuel Pégourié-Gonnard | 58ab9ba | 2024-08-14 09:47:38 +0200 | [diff] [blame] | 7898 | /* Authmode: precedence order is SNI if used else configuration */ |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7899 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 7900 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 7901 | ? ssl->handshake->sni_authmode |
| 7902 | : ssl->conf->authmode; |
| 7903 | #else |
| 7904 | const int authmode = ssl->conf->authmode; |
| 7905 | #endif |
| 7906 | void *rs_ctx = NULL; |
| 7907 | mbedtls_x509_crt *chain = NULL; |
| 7908 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7909 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7910 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7911 | crt_expected = ssl_parse_certificate_coordinate(ssl, authmode); |
| 7912 | if (crt_expected == SSL_CERTIFICATE_SKIP) { |
| 7913 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7914 | goto exit; |
| 7915 | } |
| 7916 | |
| 7917 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7918 | if (ssl->handshake->ecrs_enabled && |
| 7919 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7920 | chain = ssl->handshake->ecrs_peer_cert; |
| 7921 | ssl->handshake->ecrs_peer_cert = NULL; |
| 7922 | goto crt_verify; |
| 7923 | } |
| 7924 | #endif |
| 7925 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7926 | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7927 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 7928 | let it decide whether to alert. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7929 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7930 | goto exit; |
| 7931 | } |
| 7932 | |
| 7933 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7934 | if (ssl_srv_check_client_no_crt_notification(ssl) == 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7935 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
| 7936 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7937 | if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7938 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7939 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7940 | |
| 7941 | goto exit; |
| 7942 | } |
| 7943 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7944 | |
| 7945 | /* Clear existing peer CRT structure in case we tried to |
| 7946 | * reuse a session but it failed, and allocate a new one. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7947 | ssl_clear_peer_cert(ssl->session_negotiate); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7948 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7949 | chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt)); |
| 7950 | if (chain == NULL) { |
| 7951 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", |
| 7952 | sizeof(mbedtls_x509_crt))); |
| 7953 | mbedtls_ssl_send_alert_message(ssl, |
| 7954 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7955 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7956 | |
| 7957 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 7958 | goto exit; |
| 7959 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7960 | mbedtls_x509_crt_init(chain); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7961 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7962 | ret = ssl_parse_certificate_chain(ssl, chain); |
| 7963 | if (ret != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7964 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7965 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7966 | |
| 7967 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7968 | if (ssl->handshake->ecrs_enabled) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7969 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7970 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7971 | |
| 7972 | crt_verify: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7973 | if (ssl->handshake->ecrs_enabled) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7974 | rs_ctx = &ssl->handshake->ecrs_ctx; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7975 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7976 | #endif |
| 7977 | |
Manuel Pégourié-Gonnard | 19dd9f5 | 2024-08-16 11:03:42 +0200 | [diff] [blame] | 7978 | ret = mbedtls_ssl_verify_certificate(ssl, authmode, chain, |
| 7979 | ssl->handshake->ciphersuite_info, |
| 7980 | rs_ctx); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7981 | if (ret != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7982 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7983 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7984 | |
| 7985 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7986 | { |
| 7987 | unsigned char *crt_start, *pk_start; |
| 7988 | size_t crt_len, pk_len; |
| 7989 | |
| 7990 | /* We parse the CRT chain without copying, so |
| 7991 | * these pointers point into the input buffer, |
| 7992 | * and are hence still valid after freeing the |
| 7993 | * CRT chain. */ |
| 7994 | |
| 7995 | crt_start = chain->raw.p; |
| 7996 | crt_len = chain->raw.len; |
| 7997 | |
| 7998 | pk_start = chain->pk_raw.p; |
| 7999 | pk_len = chain->pk_raw.len; |
| 8000 | |
| 8001 | /* Free the CRT structures before computing |
| 8002 | * digest and copying the peer's public key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8003 | mbedtls_x509_crt_free(chain); |
| 8004 | mbedtls_free(chain); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8005 | chain = NULL; |
| 8006 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8007 | ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len); |
| 8008 | if (ret != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8009 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8010 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8011 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8012 | ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len); |
| 8013 | if (ret != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8014 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8015 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8016 | } |
| 8017 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8018 | /* Pass ownership to session structure. */ |
| 8019 | ssl->session_negotiate->peer_cert = chain; |
| 8020 | chain = NULL; |
| 8021 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8022 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8023 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8024 | |
| 8025 | exit: |
| 8026 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8027 | if (ret == 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8028 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8029 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8030 | |
| 8031 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8032 | if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8033 | ssl->handshake->ecrs_peer_cert = chain; |
| 8034 | chain = NULL; |
| 8035 | } |
| 8036 | #endif |
| 8037 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8038 | if (chain != NULL) { |
| 8039 | mbedtls_x509_crt_free(chain); |
| 8040 | mbedtls_free(chain); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8041 | } |
| 8042 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8043 | return ret; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 8044 | } |
| 8045 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 8046 | |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8047 | static int ssl_calc_finished_tls_generic(mbedtls_ssl_context *ssl, void *ctx, |
| 8048 | unsigned char *padbuf, size_t hlen, |
| 8049 | unsigned char *buf, int from) |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8050 | { |
Dave Rodgman | c37ad44 | 2023-11-03 23:36:06 +0000 | [diff] [blame] | 8051 | unsigned int len = 12; |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8052 | const char *sender; |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8053 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8054 | psa_status_t status; |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8055 | psa_hash_operation_t *hs_op = ctx; |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 8056 | psa_hash_operation_t cloned_op = PSA_HASH_OPERATION_INIT; |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8057 | size_t hash_size; |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8058 | #else |
Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 8059 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8060 | mbedtls_md_context_t *hs_ctx = ctx; |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 8061 | mbedtls_md_context_t cloned_ctx; |
| 8062 | mbedtls_md_init(&cloned_ctx); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8063 | #endif |
| 8064 | |
| 8065 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8066 | if (!session) { |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8067 | session = ssl->session; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8068 | } |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8069 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8070 | sender = (from == MBEDTLS_SSL_IS_CLIENT) |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8071 | ? "client finished" |
| 8072 | : "server finished"; |
| 8073 | |
| 8074 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8075 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls")); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8076 | |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 8077 | status = psa_hash_clone(hs_op, &cloned_op); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8078 | if (status != PSA_SUCCESS) { |
Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 8079 | goto exit; |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8080 | } |
| 8081 | |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 8082 | status = psa_hash_finish(&cloned_op, padbuf, hlen, &hash_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8083 | if (status != PSA_SUCCESS) { |
Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 8084 | goto exit; |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8085 | } |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8086 | MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, hlen); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8087 | #else |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8088 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls")); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8089 | |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 8090 | 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] | 8091 | if (ret != 0) { |
| 8092 | goto exit; |
| 8093 | } |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 8094 | ret = mbedtls_md_clone(&cloned_ctx, hs_ctx); |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 8095 | if (ret != 0) { |
| 8096 | goto exit; |
| 8097 | } |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8098 | |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 8099 | ret = mbedtls_md_finish(&cloned_ctx, padbuf); |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8100 | if (ret != 0) { |
| 8101 | goto exit; |
| 8102 | } |
| 8103 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8104 | |
| 8105 | MBEDTLS_SSL_DEBUG_BUF(4, "finished output", padbuf, hlen); |
| 8106 | |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8107 | /* |
| 8108 | * TLSv1.2: |
| 8109 | * hash = PRF( master, finished_label, |
| 8110 | * Hash( handshake ) )[0.11] |
| 8111 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8112 | ssl->handshake->tls_prf(session->master, 48, sender, |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8113 | padbuf, hlen, buf, len); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8114 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8115 | MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8116 | |
Paul Elliott | ecb95be | 2023-08-11 16:41:04 +0100 | [diff] [blame] | 8117 | mbedtls_platform_zeroize(padbuf, hlen); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8118 | |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8119 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished")); |
Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 8120 | |
| 8121 | exit: |
| 8122 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 8123 | psa_hash_abort(&cloned_op); |
Manuel Pégourié-Gonnard | 3761e9e | 2023-03-28 12:54:56 +0200 | [diff] [blame] | 8124 | return mbedtls_md_error_from_psa(status); |
Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 8125 | #else |
Manuel Pégourié-Gonnard | aaad2b6 | 2023-07-04 11:35:16 +0200 | [diff] [blame] | 8126 | mbedtls_md_free(&cloned_ctx); |
Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 8127 | return ret; |
| 8128 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8129 | } |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8130 | |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 8131 | #if defined(PSA_WANT_ALG_SHA_256) |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8132 | static int ssl_calc_finished_tls_sha256( |
| 8133 | mbedtls_ssl_context *ssl, unsigned char *buf, int from) |
| 8134 | { |
| 8135 | unsigned char padbuf[32]; |
| 8136 | return ssl_calc_finished_tls_generic(ssl, |
| 8137 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8138 | &ssl->handshake->fin_sha256_psa, |
| 8139 | #else |
| 8140 | &ssl->handshake->fin_sha256, |
| 8141 | #endif |
| 8142 | padbuf, sizeof(padbuf), |
| 8143 | buf, from); |
| 8144 | } |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 8145 | #endif /* PSA_WANT_ALG_SHA_256*/ |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 8146 | |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 8147 | |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 8148 | #if defined(PSA_WANT_ALG_SHA_384) |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 8149 | static int ssl_calc_finished_tls_sha384( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8150 | mbedtls_ssl_context *ssl, unsigned char *buf, int from) |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 8151 | { |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 8152 | unsigned char padbuf[48]; |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8153 | return ssl_calc_finished_tls_generic(ssl, |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 8154 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8155 | &ssl->handshake->fin_sha384_psa, |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 8156 | #else |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8157 | &ssl->handshake->fin_sha384, |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 8158 | #endif |
Manuel Pégourié-Gonnard | de33278 | 2023-06-24 10:13:41 +0200 | [diff] [blame] | 8159 | padbuf, sizeof(padbuf), |
| 8160 | buf, from); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 8161 | } |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 8162 | #endif /* PSA_WANT_ALG_SHA_384*/ |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 8163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8164 | void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl) |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 8165 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8166 | MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free")); |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 8167 | |
| 8168 | /* |
| 8169 | * Free our handshake params |
| 8170 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8171 | mbedtls_ssl_handshake_free(ssl); |
| 8172 | mbedtls_free(ssl->handshake); |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 8173 | ssl->handshake = NULL; |
| 8174 | |
| 8175 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 8176 | * Free the previous transform and switch in the current one |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 8177 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8178 | if (ssl->transform) { |
| 8179 | mbedtls_ssl_transform_free(ssl->transform); |
| 8180 | mbedtls_free(ssl->transform); |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 8181 | } |
| 8182 | ssl->transform = ssl->transform_negotiate; |
| 8183 | ssl->transform_negotiate = NULL; |
| 8184 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8185 | MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free")); |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 8186 | } |
| 8187 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8188 | void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl) |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8189 | { |
| 8190 | int resume = ssl->handshake->resume; |
| 8191 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8192 | MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup")); |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8193 | |
| 8194 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8195 | if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) { |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8196 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
| 8197 | ssl->renego_records_seen = 0; |
| 8198 | } |
| 8199 | #endif |
| 8200 | |
| 8201 | /* |
| 8202 | * Free the previous session and switch in the current one |
| 8203 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8204 | if (ssl->session) { |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8205 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 8206 | /* RFC 7366 3.1: keep the EtM state */ |
| 8207 | ssl->session_negotiate->encrypt_then_mac = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8208 | ssl->session->encrypt_then_mac; |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8209 | #endif |
| 8210 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8211 | mbedtls_ssl_session_free(ssl->session); |
| 8212 | mbedtls_free(ssl->session); |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8213 | } |
| 8214 | ssl->session = ssl->session_negotiate; |
| 8215 | ssl->session_negotiate = NULL; |
| 8216 | |
| 8217 | /* |
| 8218 | * Add cache entry |
| 8219 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8220 | if (ssl->conf->f_set_cache != NULL && |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8221 | ssl->session->id_len != 0 && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8222 | resume == 0) { |
| 8223 | if (ssl->conf->f_set_cache(ssl->conf->p_cache, |
| 8224 | ssl->session->id, |
| 8225 | ssl->session->id_len, |
| 8226 | ssl->session) != 0) { |
| 8227 | MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session")); |
| 8228 | } |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8229 | } |
| 8230 | |
| 8231 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8232 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 8233 | ssl->handshake->flight != NULL) { |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8234 | /* Cancel handshake timer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8235 | mbedtls_ssl_set_timer(ssl, 0); |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8236 | |
| 8237 | /* Keep last flight around in case we need to resend it: |
| 8238 | * we need the handshake and transform structures for that */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8239 | MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform")); |
| 8240 | } else |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8241 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8242 | mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl); |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8243 | |
Jerry Yu | 5ed73ff | 2022-10-27 13:08:42 +0800 | [diff] [blame] | 8244 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8245 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8246 | MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup")); |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 8247 | } |
| 8248 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8249 | int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl) |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8250 | { |
Dave Rodgman | c37ad44 | 2023-11-03 23:36:06 +0000 | [diff] [blame] | 8251 | int ret; |
| 8252 | unsigned int hash_len; |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8253 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8254 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished")); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8256 | mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8257 | |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 8258 | ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint); |
| 8259 | if (ret != 0) { |
| 8260 | MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret); |
| 8261 | } |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8262 | |
| 8263 | /* |
| 8264 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 8265 | * may define some other value. Currently (early 2016), no defined |
| 8266 | * ciphersuite does this (and this is unlikely to change as activity has |
| 8267 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 8268 | */ |
| 8269 | hash_len = 12; |
| 8270 | |
| 8271 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 8272 | ssl->verify_data_len = hash_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8273 | memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8274 | #endif |
| 8275 | |
| 8276 | ssl->out_msglen = 4 + hash_len; |
| 8277 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 8278 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
| 8279 | |
| 8280 | /* |
| 8281 | * In case of session resuming, invert the client and server |
| 8282 | * ChangeCipherSpec messages order. |
| 8283 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8284 | if (ssl->handshake->resume != 0) { |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8285 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8286 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8287 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8288 | } |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8289 | #endif |
| 8290 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8291 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8292 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8293 | } |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8294 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8295 | } else { |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8296 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8297 | } |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8298 | |
| 8299 | /* |
| 8300 | * Switch to our negotiated transform and session parameters for outbound |
| 8301 | * data. |
| 8302 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8303 | 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] | 8304 | |
| 8305 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8306 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8307 | unsigned char i; |
| 8308 | |
| 8309 | /* Remember current epoch settings for resending */ |
| 8310 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8311 | memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, |
| 8312 | sizeof(ssl->handshake->alt_out_ctr)); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8313 | |
| 8314 | /* Set sequence_number to zero */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8315 | 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] | 8316 | |
| 8317 | |
| 8318 | /* Increment epoch */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8319 | for (i = 2; i > 0; i--) { |
| 8320 | if (++ssl->cur_out_ctr[i - 1] != 0) { |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8321 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8322 | } |
| 8323 | } |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8324 | |
| 8325 | /* The loop goes to its end iff the counter is wrapping */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8326 | if (i == 0) { |
| 8327 | MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap")); |
| 8328 | return MBEDTLS_ERR_SSL_COUNTER_WRAPPING; |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8329 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8330 | } else |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8331 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8332 | memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr)); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8333 | |
| 8334 | ssl->transform_out = ssl->transform_negotiate; |
| 8335 | ssl->session_out = ssl->session_negotiate; |
| 8336 | |
| 8337 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8338 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 8339 | mbedtls_ssl_send_flight_completed(ssl); |
| 8340 | } |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8341 | #endif |
| 8342 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8343 | if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { |
| 8344 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret); |
| 8345 | return ret; |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8346 | } |
| 8347 | |
| 8348 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8349 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 8350 | (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) { |
| 8351 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret); |
| 8352 | return ret; |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8353 | } |
| 8354 | #endif |
| 8355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8356 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished")); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8358 | return 0; |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8359 | } |
| 8360 | |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8361 | #define SSL_MAX_HASH_LEN 12 |
| 8362 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8363 | int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl) |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8364 | { |
| 8365 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 8366 | unsigned int hash_len = 12; |
| 8367 | unsigned char buf[SSL_MAX_HASH_LEN]; |
| 8368 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8369 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished")); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8370 | |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 8371 | ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1); |
| 8372 | if (ret != 0) { |
| 8373 | MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret); |
| 8374 | } |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8375 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8376 | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { |
| 8377 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8378 | goto exit; |
| 8379 | } |
| 8380 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8381 | if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) { |
| 8382 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message")); |
| 8383 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8384 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8385 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 8386 | goto exit; |
| 8387 | } |
| 8388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8389 | if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) { |
| 8390 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8391 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8392 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 8393 | goto exit; |
| 8394 | } |
| 8395 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8396 | if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) { |
| 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_DECODE_ERROR); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8400 | ret = MBEDTLS_ERR_SSL_DECODE_ERROR; |
| 8401 | goto exit; |
| 8402 | } |
| 8403 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8404 | if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), |
| 8405 | buf, hash_len) != 0) { |
| 8406 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message")); |
| 8407 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8408 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8409 | ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
| 8410 | goto exit; |
| 8411 | } |
| 8412 | |
| 8413 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 8414 | ssl->verify_data_len = hash_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8415 | memcpy(ssl->peer_verify_data, buf, hash_len); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8416 | #endif |
| 8417 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8418 | if (ssl->handshake->resume != 0) { |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8419 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8420 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8421 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8422 | } |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8423 | #endif |
| 8424 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8425 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8426 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8427 | } |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8428 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8429 | } else { |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8430 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8431 | } |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8432 | |
| 8433 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8434 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 8435 | mbedtls_ssl_recv_flight_completed(ssl); |
| 8436 | } |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8437 | #endif |
| 8438 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8439 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished")); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8440 | |
| 8441 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8442 | mbedtls_platform_zeroize(buf, hash_len); |
| 8443 | return ret; |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8444 | } |
| 8445 | |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 8446 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 8447 | /* |
| 8448 | * Helper to get TLS 1.2 PRF from ciphersuite |
| 8449 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) |
| 8450 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8451 | static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id) |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 8452 | { |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 8453 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8454 | mbedtls_ssl_ciphersuite_from_id(ciphersuite_id); |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 8455 | #if defined(PSA_WANT_ALG_SHA_384) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8456 | if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) { |
| 8457 | return tls_prf_sha384; |
| 8458 | } else |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 8459 | #endif |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 8460 | #if defined(PSA_WANT_ALG_SHA_256) |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 8461 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8462 | if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) { |
| 8463 | return tls_prf_sha256; |
| 8464 | } |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 8465 | } |
| 8466 | #endif |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 8467 | #if !defined(PSA_WANT_ALG_SHA_384) && \ |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 8468 | !defined(PSA_WANT_ALG_SHA_256) |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 8469 | (void) ciphersuite_info; |
| 8470 | #endif |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 8471 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8472 | return NULL; |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 8473 | } |
| 8474 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 8475 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8476 | 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] | 8477 | { |
| 8478 | ((void) tls_prf); |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 8479 | #if defined(PSA_WANT_ALG_SHA_384) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8480 | if (tls_prf == tls_prf_sha384) { |
| 8481 | return MBEDTLS_SSL_TLS_PRF_SHA384; |
| 8482 | } else |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 8483 | #endif |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 8484 | #if defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8485 | if (tls_prf == tls_prf_sha256) { |
| 8486 | return MBEDTLS_SSL_TLS_PRF_SHA256; |
| 8487 | } else |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 8488 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8489 | return MBEDTLS_SSL_TLS_PRF_NONE; |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 8490 | } |
| 8491 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8492 | /* |
| 8493 | * Populate a transform structure with session keys and all the other |
| 8494 | * necessary information. |
| 8495 | * |
| 8496 | * Parameters: |
| 8497 | * - [in/out]: transform: structure to populate |
| 8498 | * [in] must be just initialised with mbedtls_ssl_transform_init() |
| 8499 | * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf() |
| 8500 | * - [in] ciphersuite |
| 8501 | * - [in] master |
| 8502 | * - [in] encrypt_then_mac |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8503 | * - [in] tls_prf: pointer to PRF to use for key derivation |
| 8504 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 8505 | * - [in] tls_version: TLS version |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8506 | * - [in] endpoint: client or server |
| 8507 | * - [in] ssl: used for: |
| 8508 | * - ssl->conf->{f,p}_export_keys |
| 8509 | * [in] optionally used for: |
| 8510 | * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg |
| 8511 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 8512 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8513 | static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform, |
| 8514 | int ciphersuite, |
| 8515 | const unsigned char master[48], |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8516 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8517 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8518 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8519 | ssl_tls_prf_t tls_prf, |
| 8520 | const unsigned char randbytes[64], |
| 8521 | mbedtls_ssl_protocol_version tls_version, |
| 8522 | unsigned endpoint, |
| 8523 | const mbedtls_ssl_context *ssl) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8524 | { |
| 8525 | int ret = 0; |
| 8526 | unsigned char keyblk[256]; |
| 8527 | unsigned char *key1; |
| 8528 | unsigned char *key2; |
| 8529 | unsigned char *mac_enc; |
| 8530 | unsigned char *mac_dec; |
| 8531 | size_t mac_key_len = 0; |
| 8532 | size_t iv_copy_len; |
| 8533 | size_t keylen; |
| 8534 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8535 | mbedtls_ssl_mode_t ssl_mode; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8536 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8537 | const mbedtls_cipher_info_t *cipher_info; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8538 | const mbedtls_md_info_t *md_info; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8539 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8540 | |
| 8541 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8542 | psa_key_type_t key_type; |
| 8543 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8544 | psa_algorithm_t alg; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8545 | psa_algorithm_t mac_alg = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8546 | size_t key_bits; |
| 8547 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 8548 | #endif |
| 8549 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8550 | /* |
| 8551 | * Some data just needs copying into the structure |
| 8552 | */ |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8553 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8554 | transform->encrypt_then_mac = encrypt_then_mac; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8555 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 8556 | transform->tls_version = tls_version; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8557 | |
| 8558 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8559 | memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes)); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8560 | #endif |
| 8561 | |
| 8562 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8563 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8564 | /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform |
| 8565 | * generation separate. This should never happen. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8566 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8567 | } |
| 8568 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 8569 | |
| 8570 | /* |
| 8571 | * Get various info structures |
| 8572 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8573 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite); |
| 8574 | if (ciphersuite_info == NULL) { |
| 8575 | MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found", |
| 8576 | ciphersuite)); |
| 8577 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8578 | } |
| 8579 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 8580 | ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8581 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8582 | encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8583 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8584 | ciphersuite_info); |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8585 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8586 | if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) { |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8587 | transform->taglen = |
| 8588 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8589 | } |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8590 | |
| 8591 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Dave Rodgman | 2eab462 | 2023-10-05 13:30:37 +0100 | [diff] [blame] | 8592 | 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] | 8593 | transform->taglen, |
| 8594 | &alg, |
| 8595 | &key_type, |
| 8596 | &key_bits)) != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 8597 | ret = PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8598 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8599 | goto end; |
| 8600 | } |
| 8601 | #else |
Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 8602 | 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] | 8603 | if (cipher_info == NULL) { |
| 8604 | MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found", |
| 8605 | ciphersuite_info->cipher)); |
| 8606 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8607 | } |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8608 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8609 | |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8610 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Dave Rodgman | 2eab462 | 2023-10-05 13:30:37 +0100 | [diff] [blame] | 8611 | 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] | 8612 | if (mac_alg == 0) { |
Manuel Pégourié-Gonnard | 2d6d993 | 2023-03-28 11:38:08 +0200 | [diff] [blame] | 8613 | 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] | 8614 | (unsigned) ciphersuite_info->mac)); |
| 8615 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8616 | } |
| 8617 | #else |
Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 8618 | 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] | 8619 | if (md_info == NULL) { |
| 8620 | MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found", |
| 8621 | (unsigned) ciphersuite_info->mac)); |
| 8622 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8623 | } |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8624 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8625 | |
| 8626 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 8627 | /* Copy own and peer's CID if the use of the CID |
| 8628 | * extension has been negotiated. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8629 | if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) { |
| 8630 | MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform")); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8631 | |
| 8632 | transform->in_cid_len = ssl->own_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8633 | memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len); |
| 8634 | MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid, |
| 8635 | transform->in_cid_len); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8636 | |
| 8637 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8638 | memcpy(transform->out_cid, ssl->handshake->peer_cid, |
| 8639 | ssl->handshake->peer_cid_len); |
| 8640 | MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid, |
| 8641 | transform->out_cid_len); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8642 | } |
| 8643 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 8644 | |
| 8645 | /* |
| 8646 | * Compute key block using the PRF |
| 8647 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8648 | ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256); |
| 8649 | if (ret != 0) { |
| 8650 | MBEDTLS_SSL_DEBUG_RET(1, "prf", ret); |
| 8651 | return ret; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8652 | } |
| 8653 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8654 | MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s", |
| 8655 | mbedtls_ssl_get_ciphersuite_name(ciphersuite))); |
| 8656 | MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48); |
| 8657 | MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64); |
| 8658 | MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8659 | |
| 8660 | /* |
| 8661 | * Determine the appropriate key, IV and MAC length. |
| 8662 | */ |
| 8663 | |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8664 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8665 | keylen = PSA_BITS_TO_BYTES(key_bits); |
| 8666 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8667 | keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8668 | #endif |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8669 | |
Valerio Setti | e570704 | 2023-10-11 11:54:42 +0200 | [diff] [blame] | 8670 | #if defined(MBEDTLS_SSL_HAVE_AEAD) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8671 | if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8672 | size_t explicit_ivlen; |
| 8673 | |
| 8674 | transform->maclen = 0; |
| 8675 | mac_key_len = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8676 | |
| 8677 | /* All modes haves 96-bit IVs, but the length of the static parts vary |
| 8678 | * with mode and version: |
| 8679 | * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes |
| 8680 | * (to be concatenated with a dynamically chosen IV of 8 Bytes) |
| 8681 | * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's |
| 8682 | * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record |
| 8683 | * sequence number). |
| 8684 | */ |
| 8685 | transform->ivlen = 12; |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 8686 | |
| 8687 | int is_chachapoly = 0; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8688 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8689 | is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8690 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8691 | is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info) |
| 8692 | == MBEDTLS_MODE_CHACHAPOLY); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8693 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 8694 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8695 | if (is_chachapoly) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8696 | transform->fixed_ivlen = 12; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8697 | } else { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8698 | transform->fixed_ivlen = 4; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8699 | } |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8700 | |
| 8701 | /* Minimum length of encrypted record */ |
| 8702 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
| 8703 | transform->minlen = explicit_ivlen + transform->taglen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8704 | } else |
Valerio Setti | e570704 | 2023-10-11 11:54:42 +0200 | [diff] [blame] | 8705 | #endif /* MBEDTLS_SSL_HAVE_AEAD */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8706 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8707 | if (ssl_mode == MBEDTLS_SSL_MODE_STREAM || |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8708 | ssl_mode == MBEDTLS_SSL_MODE_CBC || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8709 | ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) { |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8710 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8711 | size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8712 | #else |
Dave Rodgman | 85a8813 | 2023-06-24 11:41:50 +0100 | [diff] [blame] | 8713 | size_t block_size = mbedtls_cipher_info_get_block_size(cipher_info); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8714 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8715 | |
| 8716 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8717 | /* Get MAC length */ |
| 8718 | mac_key_len = PSA_HASH_LENGTH(mac_alg); |
| 8719 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8720 | /* Initialize HMAC contexts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8721 | if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 || |
| 8722 | (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) { |
| 8723 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8724 | goto end; |
| 8725 | } |
| 8726 | |
| 8727 | /* Get MAC length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8728 | mac_key_len = mbedtls_md_get_size(md_info); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8729 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8730 | transform->maclen = mac_key_len; |
| 8731 | |
| 8732 | /* IV length */ |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8733 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8734 | transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8735 | #else |
Dave Rodgman | bb521fd | 2023-06-24 11:21:25 +0100 | [diff] [blame] | 8736 | transform->ivlen = mbedtls_cipher_info_get_iv_size(cipher_info); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8737 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8738 | |
| 8739 | /* Minimum length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8740 | if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8741 | transform->minlen = transform->maclen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8742 | } else { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8743 | /* |
| 8744 | * GenericBlockCipher: |
| 8745 | * 1. if EtM is in use: one block plus MAC |
| 8746 | * otherwise: * first multiple of blocklen greater than maclen |
| 8747 | * 2. IV |
| 8748 | */ |
| 8749 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8750 | if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8751 | transform->minlen = transform->maclen |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8752 | + block_size; |
| 8753 | } else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8754 | #endif |
| 8755 | { |
| 8756 | transform->minlen = transform->maclen |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8757 | + block_size |
| 8758 | - transform->maclen % block_size; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8759 | } |
| 8760 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8761 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8762 | transform->minlen += transform->ivlen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8763 | } else { |
| 8764 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8765 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 8766 | goto end; |
| 8767 | } |
| 8768 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8769 | } else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8770 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 8771 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8772 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 8773 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
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 | MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 8777 | (unsigned) keylen, |
| 8778 | (unsigned) transform->minlen, |
| 8779 | (unsigned) transform->ivlen, |
| 8780 | (unsigned) transform->maclen)); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8781 | |
| 8782 | /* |
| 8783 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 8784 | */ |
| 8785 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8786 | if (endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8787 | key1 = keyblk + mac_key_len * 2; |
| 8788 | key2 = keyblk + mac_key_len * 2 + keylen; |
| 8789 | |
| 8790 | mac_enc = keyblk; |
| 8791 | mac_dec = keyblk + mac_key_len; |
| 8792 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8793 | iv_copy_len = (transform->fixed_ivlen) ? |
| 8794 | transform->fixed_ivlen : transform->ivlen; |
| 8795 | memcpy(transform->iv_enc, key2 + keylen, iv_copy_len); |
| 8796 | memcpy(transform->iv_dec, key2 + keylen + iv_copy_len, |
| 8797 | iv_copy_len); |
| 8798 | } else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8799 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 8800 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8801 | if (endpoint == MBEDTLS_SSL_IS_SERVER) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8802 | key1 = keyblk + mac_key_len * 2 + keylen; |
| 8803 | key2 = keyblk + mac_key_len * 2; |
| 8804 | |
| 8805 | mac_enc = keyblk + mac_key_len; |
| 8806 | mac_dec = keyblk; |
| 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_dec, key1 + keylen, iv_copy_len); |
| 8811 | memcpy(transform->iv_enc, key1 + 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_SRV_C */ |
| 8815 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8816 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8817 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 8818 | goto end; |
| 8819 | } |
| 8820 | |
Paul Elliott | 4fb1955 | 2023-10-18 12:15:30 +0100 | [diff] [blame] | 8821 | if (ssl->f_export_keys != NULL) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8822 | ssl->f_export_keys(ssl->p_export_keys, |
| 8823 | MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET, |
| 8824 | master, 48, |
| 8825 | randbytes + 32, |
| 8826 | randbytes, |
| 8827 | tls_prf_get_type(tls_prf)); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8828 | } |
| 8829 | |
| 8830 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8831 | transform->psa_alg = alg; |
| 8832 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8833 | if (alg != MBEDTLS_SSL_NULL_CIPHER) { |
| 8834 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 8835 | psa_set_key_algorithm(&attributes, alg); |
| 8836 | psa_set_key_type(&attributes, key_type); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8837 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8838 | if ((status = psa_import_key(&attributes, |
| 8839 | key1, |
| 8840 | PSA_BITS_TO_BYTES(key_bits), |
| 8841 | &transform->psa_key_enc)) != PSA_SUCCESS) { |
| 8842 | MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status); |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 8843 | ret = PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8844 | MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8845 | goto end; |
| 8846 | } |
| 8847 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8848 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8849 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8850 | if ((status = psa_import_key(&attributes, |
| 8851 | key2, |
| 8852 | PSA_BITS_TO_BYTES(key_bits), |
| 8853 | &transform->psa_key_dec)) != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 8854 | ret = PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8855 | MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8856 | goto end; |
| 8857 | } |
| 8858 | } |
| 8859 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8860 | if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc, |
| 8861 | cipher_info)) != 0) { |
| 8862 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8863 | goto end; |
| 8864 | } |
| 8865 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8866 | if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec, |
| 8867 | cipher_info)) != 0) { |
| 8868 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8869 | goto end; |
| 8870 | } |
| 8871 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8872 | if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1, |
| 8873 | (int) mbedtls_cipher_info_get_key_bitlen(cipher_info), |
| 8874 | MBEDTLS_ENCRYPT)) != 0) { |
| 8875 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8876 | goto end; |
| 8877 | } |
| 8878 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8879 | if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2, |
| 8880 | (int) mbedtls_cipher_info_get_key_bitlen(cipher_info), |
| 8881 | MBEDTLS_DECRYPT)) != 0) { |
| 8882 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8883 | goto end; |
| 8884 | } |
| 8885 | |
| 8886 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8887 | if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) { |
| 8888 | if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc, |
| 8889 | MBEDTLS_PADDING_NONE)) != 0) { |
| 8890 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", 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_set_padding_mode(&transform->cipher_ctx_dec, |
| 8895 | MBEDTLS_PADDING_NONE)) != 0) { |
| 8896 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8897 | goto end; |
| 8898 | } |
| 8899 | } |
| 8900 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 8901 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8902 | |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8903 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 8904 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 8905 | For AEAD-based ciphersuites, there is nothing to do here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8906 | if (mac_key_len != 0) { |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8907 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8908 | transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8909 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8910 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 8911 | psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg)); |
| 8912 | psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8913 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8914 | if ((status = psa_import_key(&attributes, |
| 8915 | mac_enc, mac_key_len, |
| 8916 | &transform->psa_mac_enc)) != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 8917 | ret = PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8918 | MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8919 | goto end; |
| 8920 | } |
| 8921 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8922 | if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) || |
| 8923 | ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING) |
Andrzej Kurek | 8c95ac4 | 2022-08-17 16:17:00 -0400 | [diff] [blame] | 8924 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8925 | && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED) |
Andrzej Kurek | 8c95ac4 | 2022-08-17 16:17:00 -0400 | [diff] [blame] | 8926 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8927 | )) { |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8928 | /* mbedtls_ct_hmac() requires the key to be exportable */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8929 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT | |
| 8930 | PSA_KEY_USAGE_VERIFY_HASH); |
| 8931 | } else { |
| 8932 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 8933 | } |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8934 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8935 | if ((status = psa_import_key(&attributes, |
| 8936 | mac_dec, mac_key_len, |
| 8937 | &transform->psa_mac_dec)) != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 8938 | ret = PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8939 | MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8940 | goto end; |
| 8941 | } |
| 8942 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8943 | ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len); |
| 8944 | if (ret != 0) { |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8945 | goto end; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8946 | } |
| 8947 | ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len); |
| 8948 | if (ret != 0) { |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8949 | goto end; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8950 | } |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8951 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8952 | } |
| 8953 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 8954 | |
| 8955 | ((void) mac_dec); |
| 8956 | ((void) mac_enc); |
| 8957 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8958 | end: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8959 | mbedtls_platform_zeroize(keyblk, sizeof(keyblk)); |
| 8960 | return ret; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8961 | } |
| 8962 | |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8963 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \ |
| 8964 | defined(MBEDTLS_USE_PSA_CRYPTO) |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8965 | int mbedtls_psa_ecjpake_read_round( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8966 | psa_pake_operation_t *pake_ctx, |
| 8967 | const unsigned char *buf, |
| 8968 | size_t len, mbedtls_ecjpake_rounds_t round) |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8969 | { |
| 8970 | psa_status_t status; |
| 8971 | size_t input_offset = 0; |
Valerio Setti | 819de86 | 2022-11-17 18:05:19 +0100 | [diff] [blame] | 8972 | /* |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8973 | * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice |
| 8974 | * At round two perform a single cycle |
| 8975 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8976 | unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8977 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8978 | for (; remaining_steps > 0; remaining_steps--) { |
| 8979 | for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8980 | step <= PSA_PAKE_STEP_ZK_PROOF; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8981 | ++step) { |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8982 | /* Length is stored at the first byte */ |
| 8983 | size_t length = buf[input_offset]; |
| 8984 | input_offset += 1; |
| 8985 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8986 | if (input_offset + length > len) { |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8987 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
| 8988 | } |
| 8989 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8990 | status = psa_pake_input(pake_ctx, step, |
| 8991 | buf + input_offset, length); |
| 8992 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 8993 | return PSA_TO_MBEDTLS_ERR(status); |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8994 | } |
| 8995 | |
| 8996 | input_offset += length; |
| 8997 | } |
| 8998 | } |
| 8999 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9000 | if (input_offset != len) { |
Valerio Setti | 61ea17d | 2022-11-18 12:11:00 +0100 | [diff] [blame] | 9001 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9002 | } |
Valerio Setti | 30ebe11 | 2022-11-17 16:23:34 +0100 | [diff] [blame] | 9003 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9004 | return 0; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 9005 | } |
| 9006 | |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 9007 | int mbedtls_psa_ecjpake_write_round( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9008 | psa_pake_operation_t *pake_ctx, |
| 9009 | unsigned char *buf, |
| 9010 | size_t len, size_t *olen, |
| 9011 | mbedtls_ecjpake_rounds_t round) |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 9012 | { |
| 9013 | psa_status_t status; |
| 9014 | size_t output_offset = 0; |
| 9015 | size_t output_len; |
Valerio Setti | 819de86 | 2022-11-17 18:05:19 +0100 | [diff] [blame] | 9016 | /* |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 9017 | * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice |
| 9018 | * At round two perform a single cycle |
| 9019 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9020 | unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 9021 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9022 | for (; remaining_steps > 0; remaining_steps--) { |
| 9023 | for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE; |
| 9024 | step <= PSA_PAKE_STEP_ZK_PROOF; |
| 9025 | ++step) { |
Valerio Setti | 79f6b6b | 2022-11-21 14:17:03 +0100 | [diff] [blame] | 9026 | /* |
Valerio Setti | d4a9b1a | 2022-11-22 11:11:10 +0100 | [diff] [blame] | 9027 | * For each step, prepend 1 byte with the length of the data as |
| 9028 | * given by psa_pake_output(). |
Valerio Setti | 79f6b6b | 2022-11-21 14:17:03 +0100 | [diff] [blame] | 9029 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9030 | status = psa_pake_output(pake_ctx, step, |
| 9031 | buf + output_offset + 1, |
| 9032 | len - output_offset - 1, |
| 9033 | &output_len); |
| 9034 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 9035 | return PSA_TO_MBEDTLS_ERR(status); |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 9036 | } |
| 9037 | |
Valerio Setti | 99d88c1 | 2022-11-22 16:03:43 +0100 | [diff] [blame] | 9038 | *(buf + output_offset) = (uint8_t) output_len; |
Valerio Setti | 79f6b6b | 2022-11-21 14:17:03 +0100 | [diff] [blame] | 9039 | |
| 9040 | output_offset += output_len + 1; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 9041 | } |
| 9042 | } |
| 9043 | |
| 9044 | *olen = output_offset; |
| 9045 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9046 | return 0; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 9047 | } |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 9048 | #endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO |
| 9049 | |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9050 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9051 | int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl, |
| 9052 | unsigned char *hash, size_t *hashlen, |
| 9053 | unsigned char *data, size_t data_len, |
| 9054 | mbedtls_md_type_t md_alg) |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9055 | { |
| 9056 | psa_status_t status; |
| 9057 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; |
Manuel Pégourié-Gonnard | 2d6d993 | 2023-03-28 11:38:08 +0200 | [diff] [blame] | 9058 | 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] | 9059 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9060 | 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] | 9061 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9062 | if ((status = psa_hash_setup(&hash_operation, |
| 9063 | hash_alg)) != PSA_SUCCESS) { |
| 9064 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9065 | goto exit; |
| 9066 | } |
| 9067 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9068 | if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes, |
| 9069 | 64)) != PSA_SUCCESS) { |
| 9070 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9071 | goto exit; |
| 9072 | } |
| 9073 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9074 | if ((status = psa_hash_update(&hash_operation, |
| 9075 | data, data_len)) != PSA_SUCCESS) { |
| 9076 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9077 | goto exit; |
| 9078 | } |
| 9079 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9080 | if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE, |
| 9081 | hashlen)) != PSA_SUCCESS) { |
| 9082 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status); |
| 9083 | goto exit; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9084 | } |
| 9085 | |
| 9086 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9087 | if (status != PSA_SUCCESS) { |
| 9088 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 9089 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
| 9090 | switch (status) { |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9091 | case PSA_ERROR_NOT_SUPPORTED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9092 | return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9093 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ |
| 9094 | case PSA_ERROR_BUFFER_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9095 | return MBEDTLS_ERR_MD_BAD_INPUT_DATA; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9096 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9097 | return MBEDTLS_ERR_MD_ALLOC_FAILED; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9098 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9099 | return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9100 | } |
| 9101 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9102 | return 0; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9103 | } |
| 9104 | |
| 9105 | #else |
| 9106 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9107 | int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl, |
| 9108 | unsigned char *hash, size_t *hashlen, |
| 9109 | unsigned char *data, size_t data_len, |
| 9110 | mbedtls_md_type_t md_alg) |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9111 | { |
| 9112 | int ret = 0; |
| 9113 | mbedtls_md_context_t ctx; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9114 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg); |
| 9115 | *hashlen = mbedtls_md_get_size(md_info); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9116 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9117 | 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] | 9118 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9119 | mbedtls_md_init(&ctx); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9120 | |
| 9121 | /* |
| 9122 | * digitally-signed struct { |
| 9123 | * opaque client_random[32]; |
| 9124 | * opaque server_random[32]; |
| 9125 | * ServerDHParams params; |
| 9126 | * }; |
| 9127 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9128 | if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) { |
| 9129 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9130 | goto exit; |
| 9131 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9132 | if ((ret = mbedtls_md_starts(&ctx)) != 0) { |
| 9133 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9134 | goto exit; |
| 9135 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9136 | if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) { |
| 9137 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9138 | goto exit; |
| 9139 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9140 | if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) { |
| 9141 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9142 | goto exit; |
| 9143 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9144 | if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) { |
| 9145 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9146 | goto exit; |
| 9147 | } |
| 9148 | |
| 9149 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9150 | mbedtls_md_free(&ctx); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9151 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9152 | if (ret != 0) { |
| 9153 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 9154 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
| 9155 | } |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9156 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9157 | return ret; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 9158 | } |
| 9159 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 9160 | |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 9161 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 9162 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 9163 | /* Find the preferred hash for a given signature algorithm. */ |
| 9164 | unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9165 | mbedtls_ssl_context *ssl, |
| 9166 | unsigned int sig_alg) |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 9167 | { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 9168 | unsigned int i; |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 9169 | uint16_t *received_sig_algs = ssl->handshake->received_sig_algs; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 9170 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9171 | if (sig_alg == MBEDTLS_SSL_SIG_ANON) { |
| 9172 | return MBEDTLS_SSL_HASH_NONE; |
| 9173 | } |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 9174 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9175 | for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) { |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 9176 | unsigned int hash_alg_received = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9177 | MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG( |
| 9178 | received_sig_algs[i]); |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 9179 | unsigned int sig_alg_received = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9180 | MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG( |
| 9181 | received_sig_algs[i]); |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 9182 | |
Manuel Pégourié-Gonnard | 47bb380 | 2023-06-05 12:40:32 +0200 | [diff] [blame] | 9183 | mbedtls_md_type_t md_alg = |
| 9184 | mbedtls_ssl_md_alg_from_hash((unsigned char) hash_alg_received); |
| 9185 | if (md_alg == MBEDTLS_MD_NONE) { |
| 9186 | continue; |
| 9187 | } |
| 9188 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9189 | if (sig_alg == sig_alg_received) { |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 9190 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9191 | if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) { |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 9192 | psa_algorithm_t psa_hash_alg = |
Manuel Pégourié-Gonnard | 47bb380 | 2023-06-05 12:40:32 +0200 | [diff] [blame] | 9193 | mbedtls_md_psa_alg_from_type(md_alg); |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 9194 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9195 | if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA && |
| 9196 | !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key, |
| 9197 | PSA_ALG_ECDSA(psa_hash_alg), |
| 9198 | PSA_KEY_USAGE_SIGN_HASH)) { |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 9199 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9200 | } |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 9201 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9202 | if (sig_alg_received == MBEDTLS_SSL_SIG_RSA && |
| 9203 | !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key, |
| 9204 | PSA_ALG_RSA_PKCS1V15_SIGN( |
| 9205 | psa_hash_alg), |
| 9206 | PSA_KEY_USAGE_SIGN_HASH)) { |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 9207 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9208 | } |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 9209 | } |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 9210 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 9211 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9212 | return hash_alg_received; |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 9213 | } |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 9214 | } |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 9215 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9216 | return MBEDTLS_SSL_HASH_NONE; |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 9217 | } |
| 9218 | |
| 9219 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 9220 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 9221 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 9222 | |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9223 | int mbedtls_ssl_validate_ciphersuite( |
| 9224 | const mbedtls_ssl_context *ssl, |
| 9225 | const mbedtls_ssl_ciphersuite_t *suite_info, |
| 9226 | mbedtls_ssl_protocol_version min_tls_version, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9227 | mbedtls_ssl_protocol_version max_tls_version) |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9228 | { |
| 9229 | (void) ssl; |
| 9230 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9231 | if (suite_info == NULL) { |
| 9232 | return -1; |
| 9233 | } |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9234 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9235 | if ((suite_info->min_tls_version > max_tls_version) || |
| 9236 | (suite_info->max_tls_version < min_tls_version)) { |
| 9237 | return -1; |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9238 | } |
| 9239 | |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 9240 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C) |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9241 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 9242 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9243 | if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
| 9244 | ssl->handshake->psa_pake_ctx_is_ok != 1) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 9245 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9246 | if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
| 9247 | mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 9248 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
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 | return -1; |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9251 | } |
| 9252 | #endif |
| 9253 | |
| 9254 | /* Don't suggest PSK-based ciphersuite if no PSK is available. */ |
| 9255 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9256 | if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) && |
| 9257 | mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) { |
| 9258 | return -1; |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9259 | } |
| 9260 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
| 9261 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 9262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9263 | return 0; |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9264 | } |
| 9265 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 9266 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9267 | /* |
| 9268 | * Function for writing a signature algorithm extension. |
| 9269 | * |
| 9270 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 9271 | * value (TLS 1.3 RFC8446): |
| 9272 | * enum { |
| 9273 | * .... |
| 9274 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 9275 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 9276 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 9277 | * .... |
| 9278 | * } SignatureScheme; |
| 9279 | * |
| 9280 | * struct { |
| 9281 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 9282 | * } SignatureSchemeList; |
| 9283 | * |
| 9284 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 9285 | * value (TLS 1.2 RFC5246): |
| 9286 | * enum { |
| 9287 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 9288 | * sha512(6), (255) |
| 9289 | * } HashAlgorithm; |
| 9290 | * |
| 9291 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 9292 | * SignatureAlgorithm; |
| 9293 | * |
| 9294 | * struct { |
| 9295 | * HashAlgorithm hash; |
| 9296 | * SignatureAlgorithm signature; |
| 9297 | * } SignatureAndHashAlgorithm; |
| 9298 | * |
| 9299 | * SignatureAndHashAlgorithm |
| 9300 | * supported_signature_algorithms<2..2^16-2>; |
| 9301 | * |
| 9302 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 9303 | * generalization of the TLS 1.2 signature algorithm extension. |
| 9304 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 9305 | * `SignatureScheme` field of TLS 1.3 |
| 9306 | * |
| 9307 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9308 | int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf, |
| 9309 | const unsigned char *end, size_t *out_len) |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9310 | { |
| 9311 | unsigned char *p = buf; |
| 9312 | unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */ |
| 9313 | size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */ |
| 9314 | |
| 9315 | *out_len = 0; |
| 9316 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9317 | MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension")); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9318 | |
| 9319 | /* Check if we have space for header and length field: |
| 9320 | * - extension_type (2 bytes) |
| 9321 | * - extension_data_length (2 bytes) |
| 9322 | * - supported_signature_algorithms_length (2 bytes) |
| 9323 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9324 | MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9325 | p += 6; |
| 9326 | |
| 9327 | /* |
| 9328 | * Write supported_signature_algorithms |
| 9329 | */ |
| 9330 | supported_sig_alg = p; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9331 | const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl); |
| 9332 | if (sig_alg == NULL) { |
| 9333 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
| 9334 | } |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9335 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9336 | for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) { |
| 9337 | MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s", |
| 9338 | *sig_alg, |
| 9339 | mbedtls_ssl_sig_alg_to_str(*sig_alg))); |
| 9340 | if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) { |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9341 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9342 | } |
| 9343 | MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2); |
| 9344 | MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9345 | p += 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9346 | MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s", |
| 9347 | *sig_alg, |
| 9348 | mbedtls_ssl_sig_alg_to_str(*sig_alg))); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9349 | } |
| 9350 | |
| 9351 | /* Length of supported_signature_algorithms */ |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 9352 | supported_sig_alg_len = (size_t) (p - supported_sig_alg); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9353 | if (supported_sig_alg_len == 0) { |
| 9354 | MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined.")); |
| 9355 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9356 | } |
| 9357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9358 | MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0); |
| 9359 | MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2); |
| 9360 | MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9361 | |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 9362 | *out_len = (size_t) (p - buf); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9363 | |
| 9364 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9365 | 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] | 9366 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | 0c354a2 | 2022-08-29 15:25:36 +0800 | [diff] [blame] | 9367 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9368 | return 0; |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9369 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 9370 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9371 | |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9372 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9373 | /* |
| 9374 | * mbedtls_ssl_parse_server_name_ext |
| 9375 | * |
| 9376 | * Structure of server_name extension: |
| 9377 | * |
| 9378 | * enum { |
| 9379 | * host_name(0), (255) |
| 9380 | * } NameType; |
| 9381 | * opaque HostName<1..2^16-1>; |
| 9382 | * |
| 9383 | * struct { |
| 9384 | * NameType name_type; |
| 9385 | * select (name_type) { |
| 9386 | * case host_name: HostName; |
| 9387 | * } name; |
| 9388 | * } ServerName; |
| 9389 | * struct { |
| 9390 | * ServerName server_name_list<1..2^16-1> |
| 9391 | * } ServerNameList; |
| 9392 | */ |
Ronald Cron | ce7d76e | 2022-07-08 18:56:49 +0200 | [diff] [blame] | 9393 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9394 | int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl, |
| 9395 | const unsigned char *buf, |
| 9396 | const unsigned char *end) |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9397 | { |
| 9398 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 9399 | const unsigned char *p = buf; |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9400 | size_t server_name_list_len, hostname_len; |
| 9401 | const unsigned char *server_name_list_end; |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9402 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9403 | MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension")); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9404 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9405 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2); |
| 9406 | server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9407 | p += 2; |
| 9408 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9409 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len); |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9410 | server_name_list_end = p + server_name_list_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9411 | while (p < server_name_list_end) { |
| 9412 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3); |
| 9413 | hostname_len = MBEDTLS_GET_UINT16_BE(p, 1); |
| 9414 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, |
| 9415 | hostname_len + 3); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9416 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9417 | if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) { |
XiaokangQian | 75fe8c7 | 2022-06-15 09:42:45 +0000 | [diff] [blame] | 9418 | /* sni_name is intended to be used only during the parsing of the |
| 9419 | * ClientHello message (it is reset to NULL before the end of |
| 9420 | * the message parsing). Thus it is ok to just point to the |
| 9421 | * reception buffer and not make a copy of it. |
| 9422 | */ |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 9423 | ssl->handshake->sni_name = p + 3; |
| 9424 | ssl->handshake->sni_name_len = hostname_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9425 | if (ssl->conf->f_sni == NULL) { |
| 9426 | return 0; |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9427 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9428 | ret = ssl->conf->f_sni(ssl->conf->p_sni, |
| 9429 | ssl, p + 3, hostname_len); |
| 9430 | if (ret != 0) { |
| 9431 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret); |
| 9432 | MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME, |
| 9433 | MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME); |
| 9434 | return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME; |
| 9435 | } |
| 9436 | return 0; |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9437 | } |
| 9438 | |
| 9439 | p += hostname_len + 3; |
| 9440 | } |
| 9441 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9442 | return 0; |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9443 | } |
| 9444 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 9445 | |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9446 | #if defined(MBEDTLS_SSL_ALPN) |
Ronald Cron | ce7d76e | 2022-07-08 18:56:49 +0200 | [diff] [blame] | 9447 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9448 | int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl, |
| 9449 | const unsigned char *buf, |
| 9450 | const unsigned char *end) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9451 | { |
| 9452 | const unsigned char *p = buf; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9453 | size_t protocol_name_list_len; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9454 | const unsigned char *protocol_name_list; |
| 9455 | const unsigned char *protocol_name_list_end; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9456 | size_t protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9457 | |
| 9458 | /* If ALPN not configured, just ignore the extension */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9459 | if (ssl->conf->alpn_list == NULL) { |
| 9460 | return 0; |
| 9461 | } |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9462 | |
| 9463 | /* |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9464 | * RFC7301, section 3.1 |
| 9465 | * opaque ProtocolName<1..2^8-1>; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9466 | * |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9467 | * struct { |
| 9468 | * ProtocolName protocol_name_list<2..2^16-1> |
| 9469 | * } ProtocolNameList; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9470 | */ |
| 9471 | |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9472 | /* |
XiaokangQian | 0b776e2 | 2022-06-24 09:04:59 +0000 | [diff] [blame] | 9473 | * protocol_name_list_len 2 bytes |
| 9474 | * protocol_name_len 1 bytes |
| 9475 | * protocol_name >=1 byte |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9476 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9477 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9478 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9479 | protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9480 | p += 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9481 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9482 | protocol_name_list = p; |
| 9483 | protocol_name_list_end = p + protocol_name_list_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9484 | |
| 9485 | /* Validate peer's list (lengths) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9486 | while (p < protocol_name_list_end) { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9487 | protocol_name_len = *p++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9488 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end, |
| 9489 | protocol_name_len); |
| 9490 | if (protocol_name_len == 0) { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9491 | MBEDTLS_SSL_PEND_FATAL_ALERT( |
| 9492 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9493 | MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER); |
| 9494 | return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9495 | } |
| 9496 | |
| 9497 | p += protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9498 | } |
| 9499 | |
| 9500 | /* Use our order of preference */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9501 | for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) { |
| 9502 | size_t const alpn_len = strlen(*alpn); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9503 | p = protocol_name_list; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9504 | while (p < protocol_name_list_end) { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9505 | protocol_name_len = *p++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9506 | if (protocol_name_len == alpn_len && |
| 9507 | memcmp(p, *alpn, alpn_len) == 0) { |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9508 | ssl->alpn_chosen = *alpn; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9509 | return 0; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9510 | } |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9511 | |
| 9512 | p += protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9513 | } |
| 9514 | } |
| 9515 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9516 | /* If we get here, no match was found */ |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9517 | MBEDTLS_SSL_PEND_FATAL_ALERT( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9518 | MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL, |
| 9519 | MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL); |
| 9520 | return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9521 | } |
| 9522 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9523 | int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl, |
| 9524 | unsigned char *buf, |
| 9525 | unsigned char *end, |
| 9526 | size_t *out_len) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9527 | { |
| 9528 | unsigned char *p = buf; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9529 | size_t protocol_name_len; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9530 | *out_len = 0; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9531 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9532 | if (ssl->alpn_chosen == NULL) { |
| 9533 | return 0; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9534 | } |
| 9535 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9536 | protocol_name_len = strlen(ssl->alpn_chosen); |
| 9537 | MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9538 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9539 | MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension")); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9540 | /* |
| 9541 | * 0 . 1 ext identifier |
| 9542 | * 2 . 3 ext length |
| 9543 | * 4 . 5 protocol list length |
| 9544 | * 6 . 6 protocol name length |
| 9545 | * 7 . 7+n protocol name |
| 9546 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9547 | MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9548 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9549 | *out_len = 7 + protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9550 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9551 | MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2); |
| 9552 | MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4); |
XiaokangQian | 0b776e2 | 2022-06-24 09:04:59 +0000 | [diff] [blame] | 9553 | /* Note: the length of the chosen protocol has been checked to be less |
| 9554 | * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`. |
| 9555 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9556 | p[6] = MBEDTLS_BYTE_0(protocol_name_len); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9557 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9558 | memcpy(p + 7, ssl->alpn_chosen, protocol_name_len); |
Jerry Yu | b95dd36 | 2022-11-08 21:19:34 +0800 | [diff] [blame] | 9559 | |
| 9560 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9561 | 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] | 9562 | #endif |
| 9563 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9564 | return 0; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9565 | } |
| 9566 | #endif /* MBEDTLS_SSL_ALPN */ |
| 9567 | |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9568 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
Xiaokang Qian | 0340929 | 2022-10-12 02:49:52 +0000 | [diff] [blame] | 9569 | defined(MBEDTLS_SSL_SESSION_TICKETS) && \ |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 9570 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \ |
Xiaokang Qian | ed3afcd | 2022-10-12 08:31:11 +0000 | [diff] [blame] | 9571 | defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9572 | int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session, |
| 9573 | const char *hostname) |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9574 | { |
| 9575 | /* Initialize to suppress unnecessary compiler warning */ |
| 9576 | size_t hostname_len = 0; |
| 9577 | |
| 9578 | /* Check if new hostname is valid before |
| 9579 | * making any change to current one */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9580 | if (hostname != NULL) { |
| 9581 | hostname_len = strlen(hostname); |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9582 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9583 | if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) { |
| 9584 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9585 | } |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9586 | } |
| 9587 | |
| 9588 | /* Now it's clear that we will overwrite the old hostname, |
| 9589 | * so we can free it safely */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9590 | if (session->hostname != NULL) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 9591 | mbedtls_zeroize_and_free(session->hostname, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9592 | strlen(session->hostname)); |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9593 | } |
| 9594 | |
| 9595 | /* Passing NULL as hostname shall clear the old one */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9596 | if (hostname == NULL) { |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9597 | session->hostname = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9598 | } else { |
| 9599 | session->hostname = mbedtls_calloc(1, hostname_len + 1); |
| 9600 | if (session->hostname == NULL) { |
| 9601 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 9602 | } |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9603 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9604 | memcpy(session->hostname, hostname, hostname_len); |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9605 | } |
| 9606 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9607 | return 0; |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9608 | } |
Xiaokang Qian | 0340929 | 2022-10-12 02:49:52 +0000 | [diff] [blame] | 9609 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 && |
| 9610 | MBEDTLS_SSL_SESSION_TICKETS && |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 9611 | MBEDTLS_SSL_SERVER_NAME_INDICATION && |
Xiaokang Qian | ed3afcd | 2022-10-12 08:31:11 +0000 | [diff] [blame] | 9612 | MBEDTLS_SSL_CLI_C */ |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9613 | |
Waleed Elmelegy | 883f77c | 2024-03-06 19:09:41 +0000 | [diff] [blame] | 9614 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_EARLY_DATA) && \ |
| 9615 | defined(MBEDTLS_SSL_ALPN) |
Waleed Elmelegy | 7dfba34 | 2024-03-12 16:22:39 +0000 | [diff] [blame] | 9616 | int mbedtls_ssl_session_set_ticket_alpn(mbedtls_ssl_session *session, |
| 9617 | const char *alpn) |
Waleed Elmelegy | 883f77c | 2024-03-06 19:09:41 +0000 | [diff] [blame] | 9618 | { |
| 9619 | size_t alpn_len = 0; |
| 9620 | |
| 9621 | if (alpn != NULL) { |
| 9622 | alpn_len = strlen(alpn); |
| 9623 | |
| 9624 | if (alpn_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) { |
| 9625 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9626 | } |
| 9627 | } |
| 9628 | |
| 9629 | if (session->ticket_alpn != NULL) { |
| 9630 | mbedtls_zeroize_and_free(session->ticket_alpn, |
| 9631 | strlen(session->ticket_alpn)); |
Waleed Elmelegy | 7dfba34 | 2024-03-12 16:22:39 +0000 | [diff] [blame] | 9632 | session->ticket_alpn = NULL; |
Waleed Elmelegy | 883f77c | 2024-03-06 19:09:41 +0000 | [diff] [blame] | 9633 | } |
| 9634 | |
Waleed Elmelegy | 7dfba34 | 2024-03-12 16:22:39 +0000 | [diff] [blame] | 9635 | if (alpn != NULL) { |
| 9636 | session->ticket_alpn = mbedtls_calloc(alpn_len + 1, 1); |
Waleed Elmelegy | 883f77c | 2024-03-06 19:09:41 +0000 | [diff] [blame] | 9637 | if (session->ticket_alpn == NULL) { |
| 9638 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 9639 | } |
Waleed Elmelegy | 7dfba34 | 2024-03-12 16:22:39 +0000 | [diff] [blame] | 9640 | memcpy(session->ticket_alpn, alpn, alpn_len); |
Waleed Elmelegy | 883f77c | 2024-03-06 19:09:41 +0000 | [diff] [blame] | 9641 | } |
| 9642 | |
| 9643 | return 0; |
| 9644 | } |
| 9645 | #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] | 9646 | |
| 9647 | /* |
| 9648 | * The following functions are used by 1.2 and 1.3, client and server. |
| 9649 | */ |
| 9650 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
| 9651 | int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert, |
| 9652 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
| 9653 | int recv_endpoint, |
| 9654 | mbedtls_ssl_protocol_version tls_version, |
| 9655 | uint32_t *flags) |
| 9656 | { |
| 9657 | int ret = 0; |
| 9658 | unsigned int usage = 0; |
| 9659 | const char *ext_oid; |
| 9660 | size_t ext_len; |
| 9661 | |
| 9662 | /* |
| 9663 | * keyUsage |
| 9664 | */ |
| 9665 | |
| 9666 | /* Note: don't guard this with MBEDTLS_SSL_CLI_C because the server wants |
| 9667 | * to check what a compliant client will think while choosing which cert |
| 9668 | * to send to the client. */ |
| 9669 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 9670 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2 && |
| 9671 | recv_endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 9672 | /* TLS 1.2 server part of the key exchange */ |
| 9673 | switch (ciphersuite->key_exchange) { |
| 9674 | case MBEDTLS_KEY_EXCHANGE_RSA: |
Manuel Pégourié-Gonnard | 5398e58 | 2024-08-20 12:14:43 +0200 | [diff] [blame] | 9675 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
| 9676 | break; |
| 9677 | |
| 9678 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 9679 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 9680 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 9681 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
| 9682 | break; |
| 9683 | |
| 9684 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 9685 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
| 9686 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
| 9687 | break; |
| 9688 | |
| 9689 | /* Don't use default: we want warnings when adding new values */ |
| 9690 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 9691 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 9692 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 9693 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
| 9694 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
| 9695 | usage = 0; |
| 9696 | } |
| 9697 | } else |
| 9698 | #endif |
| 9699 | { |
| 9700 | /* This is either TLS 1.3 authentication, which always uses signatures, |
| 9701 | * or 1.2 client auth: rsa_sign and mbedtls_ecdsa_sign are the only |
| 9702 | * options we implement, both using signatures. */ |
| 9703 | (void) tls_version; |
| 9704 | (void) ciphersuite; |
| 9705 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
| 9706 | } |
| 9707 | |
| 9708 | if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) { |
| 9709 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
| 9710 | ret = -1; |
| 9711 | } |
| 9712 | |
| 9713 | /* |
| 9714 | * extKeyUsage |
| 9715 | */ |
| 9716 | |
| 9717 | if (recv_endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 9718 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
| 9719 | ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH); |
| 9720 | } else { |
| 9721 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
| 9722 | ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH); |
| 9723 | } |
| 9724 | |
| 9725 | if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) { |
| 9726 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
| 9727 | ret = -1; |
| 9728 | } |
| 9729 | |
| 9730 | return ret; |
| 9731 | } |
| 9732 | |
| 9733 | int mbedtls_ssl_verify_certificate(mbedtls_ssl_context *ssl, |
| 9734 | int authmode, |
| 9735 | mbedtls_x509_crt *chain, |
| 9736 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info, |
| 9737 | void *rs_ctx) |
| 9738 | { |
| 9739 | if (authmode == MBEDTLS_SSL_VERIFY_NONE) { |
| 9740 | return 0; |
| 9741 | } |
| 9742 | |
| 9743 | /* |
| 9744 | * Primary check: use the appropriate X.509 verification function |
| 9745 | */ |
| 9746 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); |
| 9747 | void *p_vrfy; |
| 9748 | if (ssl->f_vrfy != NULL) { |
| 9749 | MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback")); |
| 9750 | f_vrfy = ssl->f_vrfy; |
| 9751 | p_vrfy = ssl->p_vrfy; |
| 9752 | } else { |
| 9753 | MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback")); |
| 9754 | f_vrfy = ssl->conf->f_vrfy; |
| 9755 | p_vrfy = ssl->conf->p_vrfy; |
| 9756 | } |
| 9757 | |
| 9758 | int ret = 0; |
| 9759 | int have_ca_chain_or_callback = 0; |
| 9760 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 9761 | if (ssl->conf->f_ca_cb != NULL) { |
| 9762 | ((void) rs_ctx); |
| 9763 | have_ca_chain_or_callback = 1; |
| 9764 | |
| 9765 | MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification")); |
| 9766 | ret = mbedtls_x509_crt_verify_with_ca_cb( |
| 9767 | chain, |
| 9768 | ssl->conf->f_ca_cb, |
| 9769 | ssl->conf->p_ca_cb, |
| 9770 | ssl->conf->cert_profile, |
| 9771 | ssl->hostname, |
| 9772 | &ssl->session_negotiate->verify_result, |
| 9773 | f_vrfy, p_vrfy); |
| 9774 | } else |
| 9775 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 9776 | { |
| 9777 | mbedtls_x509_crt *ca_chain; |
| 9778 | mbedtls_x509_crl *ca_crl; |
| 9779 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 9780 | if (ssl->handshake->sni_ca_chain != NULL) { |
| 9781 | ca_chain = ssl->handshake->sni_ca_chain; |
| 9782 | ca_crl = ssl->handshake->sni_ca_crl; |
| 9783 | } else |
| 9784 | #endif |
| 9785 | { |
| 9786 | ca_chain = ssl->conf->ca_chain; |
| 9787 | ca_crl = ssl->conf->ca_crl; |
| 9788 | } |
| 9789 | |
| 9790 | if (ca_chain != NULL) { |
| 9791 | have_ca_chain_or_callback = 1; |
| 9792 | } |
| 9793 | |
| 9794 | ret = mbedtls_x509_crt_verify_restartable( |
| 9795 | chain, |
| 9796 | ca_chain, ca_crl, |
| 9797 | ssl->conf->cert_profile, |
| 9798 | ssl->hostname, |
| 9799 | &ssl->session_negotiate->verify_result, |
| 9800 | f_vrfy, p_vrfy, rs_ctx); |
| 9801 | } |
| 9802 | |
| 9803 | if (ret != 0) { |
| 9804 | MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret); |
| 9805 | } |
| 9806 | |
| 9807 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 9808 | if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) { |
| 9809 | return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; |
| 9810 | } |
| 9811 | #endif |
| 9812 | |
| 9813 | /* |
| 9814 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 9815 | */ |
| 9816 | |
| 9817 | /* With TLS 1.2 and ECC certs, check that the curve used by the |
| 9818 | * certificate is on our list of acceptable curves. |
| 9819 | * |
| 9820 | * With TLS 1.3 this is not needed because the curve is part of the |
| 9821 | * signature algorithm (eg ecdsa_secp256r1_sha256) which is checked when |
| 9822 | * we validate the signature made with the key associated to this cert. |
| 9823 | */ |
| 9824 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 9825 | defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) |
| 9826 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 && |
| 9827 | mbedtls_pk_can_do(&chain->pk, MBEDTLS_PK_ECKEY)) { |
| 9828 | if (mbedtls_ssl_check_curve(ssl, mbedtls_pk_get_ec_group_id(&chain->pk)) != 0) { |
| 9829 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)")); |
| 9830 | ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY; |
| 9831 | if (ret == 0) { |
| 9832 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 9833 | } |
| 9834 | } |
| 9835 | } |
| 9836 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */ |
| 9837 | |
| 9838 | /* Check X.509 usage extensions (keyUsage, extKeyUsage) */ |
| 9839 | if (mbedtls_ssl_check_cert_usage(chain, |
| 9840 | ciphersuite_info, |
| 9841 | ssl->conf->endpoint, |
| 9842 | ssl->tls_version, |
| 9843 | &ssl->session_negotiate->verify_result) != 0) { |
| 9844 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)")); |
| 9845 | if (ret == 0) { |
| 9846 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 9847 | } |
| 9848 | } |
| 9849 | |
| 9850 | /* With authmode optional, we want to keep going if the certificate was |
| 9851 | * unacceptable, but still fail on other errors (out of memory etc), |
| 9852 | * including fatal errors from the f_vrfy callback. |
| 9853 | * |
| 9854 | * The only acceptable errors are: |
| 9855 | * - MBEDTLS_ERR_X509_CERT_VERIFY_FAILED: cert rejected by primary check; |
| 9856 | * - MBEDTLS_ERR_SSL_BAD_CERTIFICATE: cert rejected by secondary checks. |
| 9857 | * Anything else is a fatal error. */ |
| 9858 | if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
| 9859 | (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
| 9860 | ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) { |
| 9861 | ret = 0; |
| 9862 | } |
| 9863 | |
| 9864 | /* Return a specific error as this is a user error: inconsistent |
| 9865 | * configuration - can't verify without trust anchors. */ |
| 9866 | if (have_ca_chain_or_callback == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) { |
| 9867 | MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain")); |
| 9868 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
| 9869 | } |
| 9870 | |
| 9871 | if (ret != 0) { |
| 9872 | uint8_t alert; |
| 9873 | |
| 9874 | /* The certificate may have been rejected for several reasons. |
| 9875 | Pick one and send the corresponding alert. Which alert to send |
| 9876 | may be a subject of debate in some cases. */ |
| 9877 | if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) { |
| 9878 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
| 9879 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) { |
| 9880 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 9881 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) { |
| 9882 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 9883 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) { |
| 9884 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 9885 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) { |
| 9886 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 9887 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) { |
| 9888 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 9889 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) { |
| 9890 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
| 9891 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) { |
| 9892 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
| 9893 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) { |
| 9894 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
| 9895 | } else { |
| 9896 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
| 9897 | } |
| 9898 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 9899 | alert); |
| 9900 | } |
| 9901 | |
| 9902 | #if defined(MBEDTLS_DEBUG_C) |
| 9903 | if (ssl->session_negotiate->verify_result != 0) { |
| 9904 | MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x", |
| 9905 | (unsigned int) ssl->session_negotiate->verify_result)); |
| 9906 | } else { |
| 9907 | MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear")); |
| 9908 | } |
| 9909 | #endif /* MBEDTLS_DEBUG_C */ |
| 9910 | |
| 9911 | return ret; |
| 9912 | } |
| 9913 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
| 9914 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9915 | #endif /* MBEDTLS_SSL_TLS_C */ |