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 |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 18 | */ |
| 19 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 20 | * http://www.ietf.org/rfc/rfc2246.txt |
| 21 | * http://www.ietf.org/rfc/rfc4346.txt |
| 22 | */ |
| 23 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 24 | #include "common.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 25 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #if defined(MBEDTLS_SSL_TLS_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 27 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 28 | #include <assert.h> |
| 29 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 30 | #include "mbedtls/platform.h" |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 31 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 32 | #include "mbedtls/ssl.h" |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 33 | #include "ssl_client.h" |
Ronald Cron | 27c85e7 | 2022-03-08 11:37:55 +0100 | [diff] [blame] | 34 | #include "ssl_debug_helpers.h" |
Chris Jones | 84a773f | 2021-03-05 18:38:47 +0000 | [diff] [blame] | 35 | #include "ssl_misc.h" |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 36 | |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 37 | #include "mbedtls/debug.h" |
| 38 | #include "mbedtls/error.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 39 | #include "mbedtls/platform_util.h" |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 40 | #include "mbedtls/version.h" |
Gabor Mezei | 765862c | 2021-10-19 12:22:25 +0200 | [diff] [blame] | 41 | #include "mbedtls/constant_time.h" |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 42 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 43 | #include <string.h> |
| 44 | |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 45 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 46 | #include "mbedtls/psa_util.h" |
| 47 | #include "psa/crypto.h" |
| 48 | #endif |
Manuel Pégourié-Gonnard | 07018f9 | 2022-09-15 11:29:35 +0200 | [diff] [blame] | 49 | #include "mbedtls/legacy_or_psa.h" |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 50 | |
Janos Follath | 23bdca0 | 2016-10-07 14:47:14 +0100 | [diff] [blame] | 51 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 52 | #include "mbedtls/oid.h" |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 53 | #endif |
| 54 | |
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 55 | #if defined(MBEDTLS_TEST_HOOKS) |
| 56 | static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args; |
| 57 | |
| 58 | void mbedtls_ssl_set_chk_buf_ptr_fail_args( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 59 | const uint8_t *cur, const uint8_t *end, size_t need) |
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 60 | { |
| 61 | chk_buf_ptr_fail_args.cur = cur; |
| 62 | chk_buf_ptr_fail_args.end = end; |
| 63 | chk_buf_ptr_fail_args.need = need; |
| 64 | } |
| 65 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 66 | void mbedtls_ssl_reset_chk_buf_ptr_fail_args(void) |
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 67 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 68 | memset(&chk_buf_ptr_fail_args, 0, sizeof(chk_buf_ptr_fail_args)); |
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 69 | } |
| 70 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 71 | int mbedtls_ssl_cmp_chk_buf_ptr_fail_args(mbedtls_ssl_chk_buf_ptr_args *args) |
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 72 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 73 | return (chk_buf_ptr_fail_args.cur != args->cur) || |
| 74 | (chk_buf_ptr_fail_args.end != args->end) || |
| 75 | (chk_buf_ptr_fail_args.need != args->need); |
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 76 | } |
| 77 | #endif /* MBEDTLS_TEST_HOOKS */ |
| 78 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 79 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 80 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 81 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 82 | /* Top-level Connection ID API */ |
| 83 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 84 | int mbedtls_ssl_conf_cid(mbedtls_ssl_config *conf, |
| 85 | size_t len, |
| 86 | int ignore_other_cid) |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 87 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 88 | if (len > MBEDTLS_SSL_CID_IN_LEN_MAX) { |
| 89 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 90 | } |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 91 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 92 | if (ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL && |
| 93 | ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) { |
| 94 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | 611ac77 | 2019-05-14 11:45:26 +0100 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | conf->ignore_unexpected_cid = ignore_other_cid; |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 98 | conf->cid_len = len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 99 | return 0; |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 100 | } |
| 101 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 102 | int mbedtls_ssl_set_cid(mbedtls_ssl_context *ssl, |
| 103 | int enable, |
| 104 | unsigned char const *own_cid, |
| 105 | size_t own_cid_len) |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 106 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 107 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 108 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 109 | } |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 110 | |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 111 | ssl->negotiate_cid = enable; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 112 | if (enable == MBEDTLS_SSL_CID_DISABLED) { |
| 113 | MBEDTLS_SSL_DEBUG_MSG(3, ("Disable use of CID extension.")); |
| 114 | return 0; |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 115 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 116 | MBEDTLS_SSL_DEBUG_MSG(3, ("Enable use of CID extension.")); |
| 117 | MBEDTLS_SSL_DEBUG_BUF(3, "Own CID", own_cid, own_cid_len); |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 118 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 119 | if (own_cid_len != ssl->conf->cid_len) { |
| 120 | MBEDTLS_SSL_DEBUG_MSG(3, ("CID length %u does not match CID length %u in config", |
| 121 | (unsigned) own_cid_len, |
| 122 | (unsigned) ssl->conf->cid_len)); |
| 123 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 124 | } |
| 125 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 126 | memcpy(ssl->own_cid, own_cid, own_cid_len); |
Hanno Becker | b7ee0cf | 2019-04-30 14:07:31 +0100 | [diff] [blame] | 127 | /* Truncation is not an issue here because |
| 128 | * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */ |
| 129 | ssl->own_cid_len = (uint8_t) own_cid_len; |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 130 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 131 | return 0; |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 132 | } |
| 133 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 134 | int mbedtls_ssl_get_own_cid(mbedtls_ssl_context *ssl, |
| 135 | int *enabled, |
| 136 | unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX], |
| 137 | size_t *own_cid_len) |
Paul Elliott | 0113cf1 | 2022-03-11 20:26:47 +0000 | [diff] [blame] | 138 | { |
| 139 | *enabled = MBEDTLS_SSL_CID_DISABLED; |
| 140 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 141 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 142 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 143 | } |
Paul Elliott | 0113cf1 | 2022-03-11 20:26:47 +0000 | [diff] [blame] | 144 | |
| 145 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is |
| 146 | * zero as this is indistinguishable from not requesting to use |
| 147 | * the CID extension. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 148 | if (ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) { |
| 149 | return 0; |
| 150 | } |
Paul Elliott | 0113cf1 | 2022-03-11 20:26:47 +0000 | [diff] [blame] | 151 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 152 | if (own_cid_len != NULL) { |
Paul Elliott | 0113cf1 | 2022-03-11 20:26:47 +0000 | [diff] [blame] | 153 | *own_cid_len = ssl->own_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 154 | if (own_cid != NULL) { |
| 155 | memcpy(own_cid, ssl->own_cid, ssl->own_cid_len); |
| 156 | } |
Paul Elliott | 0113cf1 | 2022-03-11 20:26:47 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | *enabled = MBEDTLS_SSL_CID_ENABLED; |
| 160 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 161 | return 0; |
Paul Elliott | 0113cf1 | 2022-03-11 20:26:47 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 164 | int mbedtls_ssl_get_peer_cid(mbedtls_ssl_context *ssl, |
| 165 | int *enabled, |
| 166 | unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX], |
| 167 | size_t *peer_cid_len) |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 168 | { |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 169 | *enabled = MBEDTLS_SSL_CID_DISABLED; |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 170 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 171 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
| 172 | mbedtls_ssl_is_handshake_over(ssl) == 0) { |
| 173 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 174 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 175 | |
Hanno Becker | c5f2422 | 2019-05-03 12:54:52 +0100 | [diff] [blame] | 176 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions |
| 177 | * were used, but client and server requested the empty CID. |
| 178 | * This is indistinguishable from not using the CID extension |
| 179 | * in the first place. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 180 | if (ssl->transform_in->in_cid_len == 0 && |
| 181 | ssl->transform_in->out_cid_len == 0) { |
| 182 | return 0; |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 183 | } |
| 184 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 185 | if (peer_cid_len != NULL) { |
Hanno Becker | 615ef17 | 2019-05-22 16:50:35 +0100 | [diff] [blame] | 186 | *peer_cid_len = ssl->transform_in->out_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 187 | if (peer_cid != NULL) { |
| 188 | memcpy(peer_cid, ssl->transform_in->out_cid, |
| 189 | ssl->transform_in->out_cid_len); |
Hanno Becker | 615ef17 | 2019-05-22 16:50:35 +0100 | [diff] [blame] | 190 | } |
| 191 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 192 | |
| 193 | *enabled = MBEDTLS_SSL_CID_ENABLED; |
| 194 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 195 | return 0; |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 196 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 197 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 198 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 199 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 200 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 201 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 202 | /* |
| 203 | * Convert max_fragment_length codes to length. |
| 204 | * RFC 6066 says: |
| 205 | * enum{ |
| 206 | * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255) |
| 207 | * } MaxFragmentLength; |
| 208 | * and we add 0 -> extension unused |
| 209 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 210 | static unsigned int ssl_mfl_code_to_length(int mfl) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 211 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 212 | switch (mfl) { |
| 213 | case MBEDTLS_SSL_MAX_FRAG_LEN_NONE: |
| 214 | return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN; |
| 215 | case MBEDTLS_SSL_MAX_FRAG_LEN_512: |
| 216 | return 512; |
| 217 | case MBEDTLS_SSL_MAX_FRAG_LEN_1024: |
| 218 | return 1024; |
| 219 | case MBEDTLS_SSL_MAX_FRAG_LEN_2048: |
| 220 | return 2048; |
| 221 | case MBEDTLS_SSL_MAX_FRAG_LEN_4096: |
| 222 | return 4096; |
| 223 | default: |
| 224 | return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 225 | } |
| 226 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 227 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 228 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 229 | int mbedtls_ssl_session_copy(mbedtls_ssl_session *dst, |
| 230 | const mbedtls_ssl_session *src) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 231 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 232 | mbedtls_ssl_session_free(dst); |
| 233 | memcpy(dst, src, sizeof(mbedtls_ssl_session)); |
吴敬辉 | 0b71611 | 2021-11-29 10:46:35 +0800 | [diff] [blame] | 234 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 235 | dst->ticket = NULL; |
Xiaokang Qian | 8730644 | 2022-10-12 09:47:38 +0000 | [diff] [blame] | 236 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 237 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 238 | dst->hostname = NULL; |
吴敬辉 | 0b71611 | 2021-11-29 10:46:35 +0800 | [diff] [blame] | 239 | #endif |
Xiaokang Qian | 8730644 | 2022-10-12 09:47:38 +0000 | [diff] [blame] | 240 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
吴敬辉 | 0b71611 | 2021-11-29 10:46:35 +0800 | [diff] [blame] | 241 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 242 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 243 | |
| 244 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 245 | if (src->peer_cert != NULL) { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 246 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 247 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 248 | dst->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt)); |
| 249 | if (dst->peer_cert == NULL) { |
| 250 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 251 | } |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 252 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 253 | mbedtls_x509_crt_init(dst->peer_cert); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 254 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 255 | if ((ret = mbedtls_x509_crt_parse_der(dst->peer_cert, src->peer_cert->raw.p, |
| 256 | src->peer_cert->raw.len)) != 0) { |
| 257 | mbedtls_free(dst->peer_cert); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 258 | dst->peer_cert = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 259 | return ret; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 260 | } |
| 261 | } |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 262 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 263 | if (src->peer_cert_digest != NULL) { |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 264 | dst->peer_cert_digest = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 265 | mbedtls_calloc(1, src->peer_cert_digest_len); |
| 266 | if (dst->peer_cert_digest == NULL) { |
| 267 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 268 | } |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 269 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 270 | memcpy(dst->peer_cert_digest, src->peer_cert_digest, |
| 271 | src->peer_cert_digest_len); |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 272 | dst->peer_cert_digest_type = src->peer_cert_digest_type; |
Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 273 | dst->peer_cert_digest_len = src->peer_cert_digest_len; |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 274 | } |
| 275 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 276 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 277 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 278 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 279 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 280 | if (src->ticket != NULL) { |
| 281 | dst->ticket = mbedtls_calloc(1, src->ticket_len); |
| 282 | if (dst->ticket == NULL) { |
| 283 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 284 | } |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 285 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 286 | memcpy(dst->ticket, src->ticket, src->ticket_len); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 287 | } |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 288 | |
| 289 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 290 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 291 | if (src->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 292 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 293 | ret = mbedtls_ssl_session_set_hostname(dst, src->hostname); |
| 294 | if (ret != 0) { |
| 295 | return ret; |
| 296 | } |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 297 | } |
| 298 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 && |
| 299 | MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 300 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 301 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 302 | return 0; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 303 | } |
| 304 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 305 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 306 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 307 | 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] | 308 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 309 | unsigned char *resized_buffer = mbedtls_calloc(1, len_new); |
| 310 | if (resized_buffer == NULL) { |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 311 | return -1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 312 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 313 | |
| 314 | /* We want to copy len_new bytes when downsizing the buffer, and |
| 315 | * len_old bytes when upsizing, so we choose the smaller of two sizes, |
| 316 | * to fit one buffer into another. Size checks, ensuring that no data is |
| 317 | * lost, are done outside of this function. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 318 | memcpy(resized_buffer, *buffer, |
| 319 | (len_new < *len_old) ? len_new : *len_old); |
| 320 | mbedtls_platform_zeroize(*buffer, *len_old); |
| 321 | mbedtls_free(*buffer); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 322 | |
| 323 | *buffer = resized_buffer; |
| 324 | *len_old = len_new; |
| 325 | |
| 326 | return 0; |
| 327 | } |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 328 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 329 | static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing, |
| 330 | size_t in_buf_new_len, |
| 331 | size_t out_buf_new_len) |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 332 | { |
| 333 | int modified = 0; |
| 334 | size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0; |
| 335 | 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] | 336 | if (ssl->in_buf != NULL) { |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 337 | written_in = ssl->in_msg - ssl->in_buf; |
| 338 | iv_offset_in = ssl->in_iv - ssl->in_buf; |
| 339 | len_offset_in = ssl->in_len - ssl->in_buf; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 340 | if (downsizing ? |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 341 | 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] | 342 | ssl->in_buf_len < in_buf_new_len) { |
| 343 | if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != 0) { |
| 344 | MBEDTLS_SSL_DEBUG_MSG(1, ("input buffer resizing failed - out of memory")); |
| 345 | } else { |
| 346 | MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET, |
| 347 | in_buf_new_len)); |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 348 | modified = 1; |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 353 | if (ssl->out_buf != NULL) { |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 354 | written_out = ssl->out_msg - ssl->out_buf; |
| 355 | iv_offset_out = ssl->out_iv - ssl->out_buf; |
| 356 | len_offset_out = ssl->out_len - ssl->out_buf; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 357 | if (downsizing ? |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 358 | ssl->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] | 359 | ssl->out_buf_len < out_buf_new_len) { |
| 360 | if (resize_buffer(&ssl->out_buf, out_buf_new_len, &ssl->out_buf_len) != 0) { |
| 361 | MBEDTLS_SSL_DEBUG_MSG(1, ("output buffer resizing failed - out of memory")); |
| 362 | } else { |
| 363 | MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET, |
| 364 | out_buf_new_len)); |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 365 | modified = 1; |
| 366 | } |
| 367 | } |
| 368 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 369 | if (modified) { |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 370 | /* Update pointers here to avoid doing it twice. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 371 | mbedtls_ssl_reset_in_out_pointers(ssl); |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 372 | /* Fields below might not be properly updated with record |
| 373 | * splitting or with CID, so they are manually updated here. */ |
| 374 | ssl->out_msg = ssl->out_buf + written_out; |
| 375 | ssl->out_len = ssl->out_buf + len_offset_out; |
| 376 | ssl->out_iv = ssl->out_buf + iv_offset_out; |
| 377 | |
| 378 | ssl->in_msg = ssl->in_buf + written_in; |
| 379 | ssl->in_len = ssl->in_buf + len_offset_in; |
| 380 | ssl->in_iv = ssl->in_buf + iv_offset_in; |
| 381 | } |
| 382 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 383 | #endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */ |
| 384 | |
Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 385 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 386 | |
| 387 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 388 | typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen, |
| 389 | const char *label, |
| 390 | const unsigned char *random, size_t rlen, |
| 391 | unsigned char *dstbuf, size_t dlen); |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 392 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 393 | static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id); |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 394 | |
| 395 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
| 396 | |
| 397 | /* Type for the TLS PRF */ |
| 398 | typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *, |
| 399 | const unsigned char *, size_t, |
| 400 | unsigned char *, size_t); |
| 401 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 402 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 403 | static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform, |
| 404 | int ciphersuite, |
| 405 | const unsigned char master[48], |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 406 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 407 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 408 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 409 | ssl_tls_prf_t tls_prf, |
| 410 | const unsigned char randbytes[64], |
| 411 | mbedtls_ssl_protocol_version tls_version, |
| 412 | unsigned endpoint, |
| 413 | const mbedtls_ssl_context *ssl); |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 414 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 415 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 416 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 417 | static int tls_prf_sha256(const unsigned char *secret, size_t slen, |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 418 | const char *label, |
| 419 | const unsigned char *random, size_t rlen, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 420 | unsigned char *dstbuf, size_t dlen); |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 421 | static int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *); |
| 422 | 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] | 423 | |
| 424 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
| 425 | |
| 426 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 427 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 428 | static int tls_prf_sha384(const unsigned char *secret, size_t slen, |
| 429 | const char *label, |
| 430 | const unsigned char *random, size_t rlen, |
| 431 | unsigned char *dstbuf, size_t dlen); |
| 432 | |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 433 | static int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *); |
| 434 | static int ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 435 | #endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
| 436 | |
| 437 | static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session, |
| 438 | unsigned char *buf, |
| 439 | size_t buf_len); |
| 440 | |
| 441 | MBEDTLS_CHECK_RETURN_CRITICAL |
| 442 | static int ssl_tls12_session_load(mbedtls_ssl_session *session, |
| 443 | const unsigned char *buf, |
| 444 | size_t len); |
| 445 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 446 | |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 447 | 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] | 448 | |
| 449 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 450 | static int ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 451 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
| 452 | |
| 453 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 454 | static int ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 455 | #endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
| 456 | |
| 457 | int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf, |
| 458 | const unsigned char *secret, size_t slen, |
| 459 | const char *label, |
| 460 | const unsigned char *random, size_t rlen, |
| 461 | unsigned char *dstbuf, size_t dlen) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 462 | { |
| 463 | mbedtls_ssl_tls_prf_cb *tls_prf = NULL; |
| 464 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 465 | switch (prf) { |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 466 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 467 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 468 | case MBEDTLS_SSL_TLS_PRF_SHA384: |
| 469 | tls_prf = tls_prf_sha384; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 470 | break; |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 471 | #endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 472 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 473 | case MBEDTLS_SSL_TLS_PRF_SHA256: |
| 474 | tls_prf = tls_prf_sha256; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 475 | break; |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 476 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 477 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 478 | default: |
| 479 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 480 | } |
| 481 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 482 | return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen); |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 483 | } |
| 484 | |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 485 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 486 | static void ssl_clear_peer_cert(mbedtls_ssl_session *session) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 487 | { |
| 488 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 489 | if (session->peer_cert != NULL) { |
| 490 | mbedtls_x509_crt_free(session->peer_cert); |
| 491 | mbedtls_free(session->peer_cert); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 492 | session->peer_cert = NULL; |
| 493 | } |
| 494 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 495 | if (session->peer_cert_digest != NULL) { |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 496 | /* Zeroization is not necessary. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 497 | mbedtls_free(session->peer_cert_digest); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 498 | session->peer_cert_digest = NULL; |
| 499 | session->peer_cert_digest_type = MBEDTLS_MD_NONE; |
| 500 | session->peer_cert_digest_len = 0; |
| 501 | } |
| 502 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 503 | } |
| 504 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 505 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 506 | uint32_t mbedtls_ssl_get_extension_id(unsigned int extension_type) |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 507 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 508 | switch (extension_type) { |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 509 | case MBEDTLS_TLS_EXT_SERVERNAME: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 510 | return MBEDTLS_SSL_EXT_ID_SERVERNAME; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 511 | |
| 512 | case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 513 | return MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 514 | |
| 515 | case MBEDTLS_TLS_EXT_STATUS_REQUEST: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 516 | return MBEDTLS_SSL_EXT_ID_STATUS_REQUEST; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 517 | |
| 518 | case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 519 | return MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 520 | |
| 521 | case MBEDTLS_TLS_EXT_SIG_ALG: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 522 | return MBEDTLS_SSL_EXT_ID_SIG_ALG; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 523 | |
| 524 | case MBEDTLS_TLS_EXT_USE_SRTP: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 525 | return MBEDTLS_SSL_EXT_ID_USE_SRTP; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 526 | |
| 527 | case MBEDTLS_TLS_EXT_HEARTBEAT: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 528 | return MBEDTLS_SSL_EXT_ID_HEARTBEAT; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 529 | |
| 530 | case MBEDTLS_TLS_EXT_ALPN: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 531 | return MBEDTLS_SSL_EXT_ID_ALPN; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 532 | |
| 533 | case MBEDTLS_TLS_EXT_SCT: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 534 | return MBEDTLS_SSL_EXT_ID_SCT; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 535 | |
| 536 | case MBEDTLS_TLS_EXT_CLI_CERT_TYPE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 537 | return MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 538 | |
| 539 | case MBEDTLS_TLS_EXT_SERV_CERT_TYPE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 540 | return MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 541 | |
| 542 | case MBEDTLS_TLS_EXT_PADDING: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 543 | return MBEDTLS_SSL_EXT_ID_PADDING; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 544 | |
| 545 | case MBEDTLS_TLS_EXT_PRE_SHARED_KEY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 546 | return MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 547 | |
| 548 | case MBEDTLS_TLS_EXT_EARLY_DATA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 549 | return MBEDTLS_SSL_EXT_ID_EARLY_DATA; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 550 | |
| 551 | case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 552 | return MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 553 | |
| 554 | case MBEDTLS_TLS_EXT_COOKIE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 555 | return MBEDTLS_SSL_EXT_ID_COOKIE; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 556 | |
| 557 | case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 558 | return MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 559 | |
| 560 | case MBEDTLS_TLS_EXT_CERT_AUTH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 561 | return MBEDTLS_SSL_EXT_ID_CERT_AUTH; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 562 | |
| 563 | case MBEDTLS_TLS_EXT_OID_FILTERS: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 564 | return MBEDTLS_SSL_EXT_ID_OID_FILTERS; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 565 | |
| 566 | case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 567 | return MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 568 | |
| 569 | case MBEDTLS_TLS_EXT_SIG_ALG_CERT: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 570 | return MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 571 | |
| 572 | case MBEDTLS_TLS_EXT_KEY_SHARE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 573 | return MBEDTLS_SSL_EXT_ID_KEY_SHARE; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 574 | |
| 575 | case MBEDTLS_TLS_EXT_TRUNCATED_HMAC: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 576 | return MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 577 | |
| 578 | case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 579 | return MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 580 | |
| 581 | case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 582 | return MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 583 | |
| 584 | case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 585 | return MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 586 | |
| 587 | case MBEDTLS_TLS_EXT_SESSION_TICKET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 588 | return MBEDTLS_SSL_EXT_ID_SESSION_TICKET; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 589 | |
| 590 | } |
| 591 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 592 | return MBEDTLS_SSL_EXT_ID_UNRECOGNIZED; |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 593 | } |
| 594 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 595 | uint32_t mbedtls_ssl_get_extension_mask(unsigned int extension_type) |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 596 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 597 | return 1 << mbedtls_ssl_get_extension_id(extension_type); |
Jerry Yu | 7a485c1 | 2022-10-31 13:08:18 +0800 | [diff] [blame] | 598 | } |
| 599 | |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 600 | #if defined(MBEDTLS_DEBUG_C) |
| 601 | static const char *extension_name_table[] = { |
Jerry Yu | ea52ed9 | 2022-11-08 21:01:17 +0800 | [diff] [blame] | 602 | [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized", |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 603 | [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name", |
| 604 | [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length", |
| 605 | [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request", |
| 606 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups", |
| 607 | [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms", |
| 608 | [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp", |
| 609 | [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat", |
| 610 | [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation", |
| 611 | [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp", |
| 612 | [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type", |
| 613 | [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type", |
| 614 | [MBEDTLS_SSL_EXT_ID_PADDING] = "padding", |
| 615 | [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key", |
| 616 | [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data", |
| 617 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions", |
| 618 | [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie", |
| 619 | [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes", |
| 620 | [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities", |
| 621 | [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters", |
| 622 | [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth", |
| 623 | [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert", |
| 624 | [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share", |
| 625 | [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac", |
| 626 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats", |
| 627 | [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac", |
| 628 | [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret", |
| 629 | [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket" |
| 630 | }; |
| 631 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 632 | static unsigned int extension_type_table[] = { |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 633 | [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff, |
| 634 | [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME, |
| 635 | [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, |
| 636 | [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST, |
| 637 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, |
| 638 | [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG, |
| 639 | [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP, |
| 640 | [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT, |
| 641 | [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN, |
| 642 | [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT, |
| 643 | [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE, |
| 644 | [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE, |
| 645 | [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING, |
| 646 | [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY, |
| 647 | [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA, |
| 648 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, |
| 649 | [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE, |
| 650 | [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES, |
| 651 | [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH, |
| 652 | [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS, |
| 653 | [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH, |
| 654 | [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT, |
| 655 | [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE, |
| 656 | [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC, |
| 657 | [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, |
| 658 | [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, |
| 659 | [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, |
| 660 | [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET |
| 661 | }; |
| 662 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 663 | const char *mbedtls_ssl_get_extension_name(unsigned int extension_type) |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 664 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 665 | return extension_name_table[ |
| 666 | mbedtls_ssl_get_extension_id(extension_type)]; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 667 | } |
| 668 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 669 | 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] | 670 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 671 | switch (hs_msg_type) { |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 672 | case MBEDTLS_SSL_HS_CLIENT_HELLO: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 673 | return "ClientHello"; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 674 | case MBEDTLS_SSL_HS_SERVER_HELLO: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 675 | return "ServerHello"; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 676 | case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 677 | return "HelloRetryRequest"; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 678 | case MBEDTLS_SSL_HS_NEW_SESSION_TICKET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 679 | return "NewSessionTicket"; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 680 | case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 681 | return "EncryptedExtensions"; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 682 | case MBEDTLS_SSL_HS_CERTIFICATE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 683 | return "Certificate"; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 684 | case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 685 | return "CertificateRequest"; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 686 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 687 | return "Unknown"; |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 688 | } |
| 689 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 690 | void mbedtls_ssl_print_extension(const mbedtls_ssl_context *ssl, |
| 691 | int level, const char *file, int line, |
| 692 | int hs_msg_type, unsigned int extension_type, |
| 693 | const char *extra_msg0, const char *extra_msg1) |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 694 | { |
| 695 | const char *extra_msg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 696 | if (extra_msg0 && extra_msg1) { |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 697 | mbedtls_debug_print_msg( |
| 698 | ssl, level, file, line, |
| 699 | "%s: %s(%u) extension %s %s.", |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 700 | ssl_tls13_get_hs_msg_name(hs_msg_type), |
| 701 | mbedtls_ssl_get_extension_name(extension_type), |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 702 | extension_type, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 703 | extra_msg0, extra_msg1); |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 704 | return; |
| 705 | } |
| 706 | |
| 707 | extra_msg = extra_msg0 ? extra_msg0 : extra_msg1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 708 | if (extra_msg) { |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 709 | mbedtls_debug_print_msg( |
| 710 | ssl, level, file, line, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 711 | "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name(hs_msg_type), |
| 712 | mbedtls_ssl_get_extension_name(extension_type), extension_type, |
| 713 | extra_msg); |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 714 | return; |
| 715 | } |
| 716 | |
| 717 | mbedtls_debug_print_msg( |
| 718 | ssl, level, file, line, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 719 | "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name(hs_msg_type), |
| 720 | mbedtls_ssl_get_extension_name(extension_type), extension_type); |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 721 | } |
| 722 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 723 | void mbedtls_ssl_print_extensions(const mbedtls_ssl_context *ssl, |
| 724 | int level, const char *file, int line, |
| 725 | int hs_msg_type, uint32_t extensions_mask, |
| 726 | const char *extra) |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 727 | { |
| 728 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 729 | for (unsigned i = 0; |
| 730 | i < sizeof(extension_name_table) / sizeof(extension_name_table[0]); |
| 731 | i++) { |
Jerry Yu | 79aa721 | 2022-11-08 21:30:21 +0800 | [diff] [blame] | 732 | mbedtls_ssl_print_extension( |
Jerry Yu | ea52ed9 | 2022-11-08 21:01:17 +0800 | [diff] [blame] | 733 | ssl, level, file, line, hs_msg_type, extension_type_table[i], |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 734 | extensions_mask & (1 << i) ? "exists" : "does not exist", extra); |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 735 | } |
| 736 | } |
| 737 | |
Pengyu Lv | ee455c0 | 2023-01-12 14:37:24 +0800 | [diff] [blame] | 738 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 739 | #define ARRAY_LENGTH(a) (sizeof(a) / sizeof(*(a))) |
| 740 | |
| 741 | static const char *ticket_flag_name_table[] = |
| 742 | { |
| 743 | [0] = "ALLOW_PSK_RESUMPTION", |
| 744 | [2] = "ALLOW_PSK_EPHEMERAL_RESUMPTION", |
| 745 | [3] = "ALLOW_EARLY_DATA", |
| 746 | }; |
| 747 | |
Pengyu Lv | 4938a56 | 2023-01-16 11:28:49 +0800 | [diff] [blame] | 748 | void mbedtls_ssl_print_ticket_flags(const mbedtls_ssl_context *ssl, |
| 749 | int level, const char *file, int line, |
| 750 | unsigned int flags) |
Pengyu Lv | ee455c0 | 2023-01-12 14:37:24 +0800 | [diff] [blame] | 751 | { |
| 752 | size_t i; |
| 753 | |
| 754 | mbedtls_debug_print_msg(ssl, level, file, line, |
Pengyu Lv | 4938a56 | 2023-01-16 11:28:49 +0800 | [diff] [blame] | 755 | "print ticket_flags (0x%02x)", flags); |
| 756 | |
| 757 | flags = flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK; |
Pengyu Lv | ee455c0 | 2023-01-12 14:37:24 +0800 | [diff] [blame] | 758 | |
| 759 | for (i = 0; i < ARRAY_LENGTH(ticket_flag_name_table); i++) { |
Pengyu Lv | 4938a56 | 2023-01-16 11:28:49 +0800 | [diff] [blame] | 760 | if ((flags & (1 << i))) { |
Pengyu Lv | ee455c0 | 2023-01-12 14:37:24 +0800 | [diff] [blame] | 761 | mbedtls_debug_print_msg(ssl, level, file, line, "- %s is set.", |
| 762 | ticket_flag_name_table[i]); |
| 763 | } |
| 764 | } |
| 765 | } |
| 766 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */ |
| 767 | |
Jerry Yu | d25cab0 | 2022-10-31 12:48:30 +0800 | [diff] [blame] | 768 | #endif /* MBEDTLS_DEBUG_C */ |
| 769 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 770 | void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl, |
| 771 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 772 | { |
| 773 | ((void) ciphersuite_info); |
| 774 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 775 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 776 | if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) { |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 777 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 778 | } else |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 779 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 780 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 781 | if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) { |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 782 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 783 | } else |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 784 | #endif |
| 785 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 786 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 787 | return; |
| 788 | } |
| 789 | } |
| 790 | |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 791 | int mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 43cc127 | 2023-02-06 11:48:19 +0100 | [diff] [blame] | 792 | unsigned hs_type, |
| 793 | size_t total_hs_len) |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 794 | { |
| 795 | unsigned char hs_hdr[4]; |
| 796 | |
| 797 | /* Build HS header for checksum update. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 798 | hs_hdr[0] = MBEDTLS_BYTE_0(hs_type); |
| 799 | hs_hdr[1] = MBEDTLS_BYTE_2(total_hs_len); |
| 800 | hs_hdr[2] = MBEDTLS_BYTE_1(total_hs_len); |
| 801 | hs_hdr[3] = MBEDTLS_BYTE_0(total_hs_len); |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 802 | |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 803 | return ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr)); |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 804 | } |
| 805 | |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 806 | int mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 43cc127 | 2023-02-06 11:48:19 +0100 | [diff] [blame] | 807 | unsigned hs_type, |
| 808 | unsigned char const *msg, |
| 809 | size_t msg_len) |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 810 | { |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 811 | int ret; |
| 812 | ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl, hs_type, msg_len); |
Manuel Pégourié-Gonnard | 43cc127 | 2023-02-06 11:48:19 +0100 | [diff] [blame] | 813 | if (ret != 0) { |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 814 | return ret; |
Manuel Pégourié-Gonnard | 43cc127 | 2023-02-06 11:48:19 +0100 | [diff] [blame] | 815 | } |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 816 | return ssl->handshake->update_checksum(ssl, msg, msg_len); |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 817 | } |
| 818 | |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 819 | int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 820 | { |
Manuel Pégourié-Gonnard | 626aaed | 2023-02-06 22:03:06 +0100 | [diff] [blame] | 821 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) || \ |
| 822 | defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 823 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 824 | psa_status_t status; |
| 825 | #else |
| 826 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 827 | #endif |
Manuel Pégourié-Gonnard | 626aaed | 2023-02-06 22:03:06 +0100 | [diff] [blame] | 828 | #else /* SHA-256 or SHA-384 */ |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 829 | ((void) ssl); |
Manuel Pégourié-Gonnard | 626aaed | 2023-02-06 22:03:06 +0100 | [diff] [blame] | 830 | #endif /* SHA-256 or SHA-384 */ |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 831 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 832 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 833 | status = psa_hash_abort(&ssl->handshake->fin_sha256_psa); |
| 834 | if (status != PSA_SUCCESS) { |
| 835 | return mbedtls_md_error_from_psa(status); |
| 836 | } |
| 837 | status = psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256); |
| 838 | if (status != PSA_SUCCESS) { |
| 839 | return mbedtls_md_error_from_psa(status); |
| 840 | } |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 841 | #else |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 842 | ret = mbedtls_md_setup(&ssl->handshake->fin_sha256, |
| 843 | mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), |
| 844 | 0); |
| 845 | if (ret != 0) { |
| 846 | return ret; |
| 847 | } |
| 848 | ret = mbedtls_md_starts(&ssl->handshake->fin_sha256); |
Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 849 | if (ret != 0) { |
| 850 | return ret; |
| 851 | } |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 852 | #endif |
| 853 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 854 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 855 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 856 | status = psa_hash_abort(&ssl->handshake->fin_sha384_psa); |
| 857 | if (status != PSA_SUCCESS) { |
| 858 | return mbedtls_md_error_from_psa(status); |
| 859 | } |
| 860 | status = psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384); |
| 861 | if (status != PSA_SUCCESS) { |
| 862 | return mbedtls_md_error_from_psa(status); |
| 863 | } |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 864 | #else |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 865 | ret = mbedtls_md_setup(&ssl->handshake->fin_sha384, |
| 866 | mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0); |
| 867 | if (ret != 0) { |
| 868 | return ret; |
| 869 | } |
| 870 | ret = mbedtls_md_starts(&ssl->handshake->fin_sha384); |
Manuel Pégourié-Gonnard | b72ff49 | 2023-02-06 09:54:49 +0100 | [diff] [blame] | 871 | if (ret != 0) { |
| 872 | return ret; |
| 873 | } |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 874 | #endif |
| 875 | #endif |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 876 | return 0; |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 877 | } |
| 878 | |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 879 | static int ssl_update_checksum_start(mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 43cc127 | 2023-02-06 11:48:19 +0100 | [diff] [blame] | 880 | const unsigned char *buf, size_t len) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 881 | { |
Manuel Pégourié-Gonnard | 626aaed | 2023-02-06 22:03:06 +0100 | [diff] [blame] | 882 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) || \ |
| 883 | defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 884 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 885 | psa_status_t status; |
| 886 | #else |
| 887 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 888 | #endif |
Manuel Pégourié-Gonnard | 626aaed | 2023-02-06 22:03:06 +0100 | [diff] [blame] | 889 | #else /* SHA-256 or SHA-384 */ |
| 890 | ((void) ssl); |
| 891 | (void) buf; |
| 892 | (void) len; |
| 893 | #endif /* SHA-256 or SHA-384 */ |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 894 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 895 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 896 | status = psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len); |
| 897 | if (status != PSA_SUCCESS) { |
| 898 | return mbedtls_md_error_from_psa(status); |
| 899 | } |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 900 | #else |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 901 | ret = mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len); |
Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 902 | if (ret != 0) { |
| 903 | return ret; |
| 904 | } |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 905 | #endif |
| 906 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 907 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 908 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 909 | status = psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len); |
| 910 | if (status != PSA_SUCCESS) { |
| 911 | return mbedtls_md_error_from_psa(status); |
| 912 | } |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 913 | #else |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 914 | ret = mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len); |
Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 915 | if (ret != 0) { |
| 916 | return ret; |
| 917 | } |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 918 | #endif |
| 919 | #endif |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 920 | return 0; |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 921 | } |
| 922 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 923 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 924 | static int ssl_update_checksum_sha256(mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 43cc127 | 2023-02-06 11:48:19 +0100 | [diff] [blame] | 925 | const unsigned char *buf, size_t len) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 926 | { |
| 927 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 928 | return mbedtls_md_error_from_psa(psa_hash_update( |
Manuel Pégourié-Gonnard | 43cc127 | 2023-02-06 11:48:19 +0100 | [diff] [blame] | 929 | &ssl->handshake->fin_sha256_psa, buf, len)); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 930 | #else |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 931 | return mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 932 | #endif |
| 933 | } |
| 934 | #endif |
| 935 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 936 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 937 | static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 43cc127 | 2023-02-06 11:48:19 +0100 | [diff] [blame] | 938 | const unsigned char *buf, size_t len) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 939 | { |
| 940 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | df94901 | 2023-02-06 10:00:52 +0100 | [diff] [blame] | 941 | return mbedtls_md_error_from_psa(psa_hash_update( |
Manuel Pégourié-Gonnard | 43cc127 | 2023-02-06 11:48:19 +0100 | [diff] [blame] | 942 | &ssl->handshake->fin_sha384_psa, buf, len)); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 943 | #else |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 944 | return mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 945 | #endif |
| 946 | } |
| 947 | #endif |
| 948 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 949 | static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 950 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 951 | memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params)); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 952 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 953 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 954 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 955 | handshake->fin_sha256_psa = psa_hash_operation_init(); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 956 | #else |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 957 | mbedtls_md_init(&handshake->fin_sha256); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 958 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 959 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 960 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 961 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 962 | handshake->fin_sha384_psa = psa_hash_operation_init(); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 963 | #else |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 964 | mbedtls_md_init(&handshake->fin_sha384); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 965 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 966 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 967 | |
| 968 | handshake->update_checksum = ssl_update_checksum_start; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 969 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 970 | #if defined(MBEDTLS_DHM_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 971 | mbedtls_dhm_init(&handshake->dhm_ctx); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 972 | #endif |
Neil Armstrong | f3f4641 | 2022-04-12 14:43:39 +0200 | [diff] [blame] | 973 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 974 | mbedtls_ecdh_init(&handshake->ecdh_ctx); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 975 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 976 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 977 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 978 | handshake->psa_pake_ctx = psa_pake_operation_init(); |
| 979 | handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT; |
| 980 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 981 | mbedtls_ecjpake_init(&handshake->ecjpake_ctx); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 982 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 983 | #if defined(MBEDTLS_SSL_CLI_C) |
| 984 | handshake->ecjpake_cache = NULL; |
| 985 | handshake->ecjpake_cache_len = 0; |
| 986 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 987 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 988 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 989 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 990 | mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 991 | #endif |
| 992 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 993 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 994 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 995 | #endif |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 996 | |
| 997 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 998 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 999 | mbedtls_pk_init(&handshake->peer_pubkey); |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 1000 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1001 | } |
| 1002 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1003 | void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1004 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1005 | memset(transform, 0, sizeof(mbedtls_ssl_transform)); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 1006 | |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 1007 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1008 | transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 1009 | transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 1010 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1011 | mbedtls_cipher_init(&transform->cipher_ctx_enc); |
| 1012 | mbedtls_cipher_init(&transform->cipher_ctx_dec); |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 1013 | #endif |
| 1014 | |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 1015 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 39b8e7d | 2022-02-23 09:24:45 +0100 | [diff] [blame] | 1016 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1017 | transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 1018 | transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 1019 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1020 | mbedtls_md_init(&transform->md_ctx_enc); |
| 1021 | mbedtls_md_init(&transform->md_ctx_dec); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1022 | #endif |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 1023 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1024 | } |
| 1025 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1026 | void mbedtls_ssl_session_init(mbedtls_ssl_session *session) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1027 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1028 | memset(session, 0, sizeof(mbedtls_ssl_session)); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1029 | } |
| 1030 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1031 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1032 | static int ssl_handshake_init(mbedtls_ssl_context *ssl) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1033 | { |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 1034 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1035 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1036 | /* Clear old handshake information if present */ |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1037 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1038 | if (ssl->transform_negotiate) { |
| 1039 | mbedtls_ssl_transform_free(ssl->transform_negotiate); |
| 1040 | } |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1041 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1042 | if (ssl->session_negotiate) { |
| 1043 | mbedtls_ssl_session_free(ssl->session_negotiate); |
| 1044 | } |
| 1045 | if (ssl->handshake) { |
| 1046 | mbedtls_ssl_handshake_free(ssl); |
| 1047 | } |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1048 | |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1049 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1050 | /* |
| 1051 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 1052 | * Now allocate missing structures. |
| 1053 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1054 | if (ssl->transform_negotiate == NULL) { |
| 1055 | ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform)); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 1056 | } |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1057 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1058 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1059 | if (ssl->session_negotiate == NULL) { |
| 1060 | ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session)); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 1061 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1062 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1063 | if (ssl->handshake == NULL) { |
| 1064 | ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params)); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 1065 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1066 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1067 | /* If the buffers are too small - reallocate */ |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 1068 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1069 | handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN, |
| 1070 | MBEDTLS_SSL_OUT_BUFFER_LEN); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1071 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1072 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1073 | /* All pointers should exist and can be directly freed without issue */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1074 | if (ssl->handshake == NULL || |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1075 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1076 | ssl->transform_negotiate == NULL || |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1077 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1078 | ssl->session_negotiate == NULL) { |
| 1079 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed")); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1080 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1081 | mbedtls_free(ssl->handshake); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1082 | ssl->handshake = NULL; |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1083 | |
| 1084 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1085 | mbedtls_free(ssl->transform_negotiate); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1086 | ssl->transform_negotiate = NULL; |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1087 | #endif |
| 1088 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1089 | mbedtls_free(ssl->session_negotiate); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1090 | ssl->session_negotiate = NULL; |
| 1091 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1092 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1095 | /* Initialize structures */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1096 | mbedtls_ssl_session_init(ssl->session_negotiate); |
| 1097 | ssl_handshake_params_init(ssl->handshake); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 1098 | |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1099 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1100 | mbedtls_ssl_transform_init(ssl->transform_negotiate); |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1101 | #endif |
| 1102 | |
Manuel Pégourié-Gonnard | 537f231 | 2023-02-05 10:17:45 +0100 | [diff] [blame] | 1103 | /* Setup handshake checksums */ |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 1104 | ret = mbedtls_ssl_reset_checksum(ssl); |
| 1105 | if (ret != 0) { |
| 1106 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret); |
| 1107 | return ret; |
| 1108 | } |
Manuel Pégourié-Gonnard | 537f231 | 2023-02-05 10:17:45 +0100 | [diff] [blame] | 1109 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 1110 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 1111 | defined(MBEDTLS_SSL_SRV_C) && \ |
| 1112 | defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 1113 | ssl->handshake->new_session_tickets_count = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1114 | ssl->conf->new_session_tickets_count; |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 1115 | #endif |
| 1116 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1117 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1118 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1119 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1120 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1121 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1122 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1123 | } else { |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1124 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1125 | } |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1126 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1127 | mbedtls_ssl_set_timer(ssl, 0); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1128 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1129 | #endif |
| 1130 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1131 | /* |
| 1132 | * curve_list is translated to IANA TLS group identifiers here because |
| 1133 | * mbedtls_ssl_conf_curves returns void and so can't return |
| 1134 | * any error codes. |
| 1135 | */ |
| 1136 | #if defined(MBEDTLS_ECP_C) |
| 1137 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 1138 | /* Heap allocate and translate curve_list from internal to IANA group ids */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1139 | if (ssl->conf->curve_list != NULL) { |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1140 | size_t length; |
| 1141 | const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list; |
| 1142 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1143 | for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE) && |
| 1144 | (length < MBEDTLS_ECP_DP_MAX); length++) { |
| 1145 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1146 | |
| 1147 | /* Leave room for zero termination */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1148 | uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t)); |
| 1149 | if (group_list == NULL) { |
| 1150 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 1151 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1153 | for (size_t i = 0; i < length; i++) { |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 1154 | 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] | 1155 | curve_list[i]); |
| 1156 | if (tls_id == 0) { |
| 1157 | mbedtls_free(group_list); |
| 1158 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1159 | } |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 1160 | group_list[i] = tls_id; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | group_list[length] = 0; |
| 1164 | |
| 1165 | ssl->handshake->group_list = group_list; |
| 1166 | ssl->handshake->group_list_heap_allocated = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1167 | } else { |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1168 | ssl->handshake->group_list = ssl->conf->group_list; |
| 1169 | ssl->handshake->group_list_heap_allocated = 0; |
| 1170 | } |
| 1171 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 1172 | #endif /* MBEDTLS_ECP_C */ |
| 1173 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1174 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1175 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 1176 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 1177 | /* Heap allocate and translate sig_hashes from internal hash identifiers to |
| 1178 | signature algorithms IANA identifiers. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1179 | if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) && |
| 1180 | ssl->conf->sig_hashes != NULL) { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1181 | const int *md; |
| 1182 | const int *sig_hashes = ssl->conf->sig_hashes; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1183 | size_t sig_algs_len = 0; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1184 | uint16_t *p; |
| 1185 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1186 | #if defined(static_assert) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1187 | static_assert(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN |
| 1188 | <= (SIZE_MAX - (2 * sizeof(uint16_t))), |
| 1189 | "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big"); |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 1190 | #endif |
| 1191 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1192 | for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) { |
| 1193 | if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1194 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1195 | } |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1196 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1197 | sig_algs_len += sizeof(uint16_t); |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1198 | #endif |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 1199 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1200 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1201 | sig_algs_len += sizeof(uint16_t); |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1202 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1203 | if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) { |
| 1204 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
| 1205 | } |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1206 | } |
| 1207 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1208 | if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) { |
| 1209 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
| 1210 | } |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1211 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1212 | ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len + |
| 1213 | sizeof(uint16_t)); |
| 1214 | if (ssl->handshake->sig_algs == NULL) { |
| 1215 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 1216 | } |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1217 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1218 | p = (uint16_t *) ssl->handshake->sig_algs; |
| 1219 | for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) { |
| 1220 | unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md); |
| 1221 | if (hash == MBEDTLS_SSL_HASH_NONE) { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1222 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1223 | } |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 1224 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1225 | *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1226 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 1227 | #endif |
| 1228 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1229 | *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1230 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 1231 | #endif |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1232 | } |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 1233 | *p = MBEDTLS_TLS_SIG_NONE; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1234 | ssl->handshake->sig_algs_heap_allocated = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1235 | } else |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 1236 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1237 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1238 | ssl->handshake->sig_algs_heap_allocated = 0; |
| 1239 | } |
Jerry Yu | cc53910 | 2022-06-27 16:27:35 +0800 | [diff] [blame] | 1240 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1241 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1242 | return 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1245 | #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] | 1246 | /* Dummy cookie callbacks for defaults */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1247 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1248 | static int ssl_cookie_write_dummy(void *ctx, |
| 1249 | unsigned char **p, unsigned char *end, |
| 1250 | const unsigned char *cli_id, size_t cli_id_len) |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1251 | { |
| 1252 | ((void) ctx); |
| 1253 | ((void) p); |
| 1254 | ((void) end); |
| 1255 | ((void) cli_id); |
| 1256 | ((void) cli_id_len); |
| 1257 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1258 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1259 | } |
| 1260 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1261 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1262 | static int ssl_cookie_check_dummy(void *ctx, |
| 1263 | const unsigned char *cookie, size_t cookie_len, |
| 1264 | const unsigned char *cli_id, size_t cli_id_len) |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1265 | { |
| 1266 | ((void) ctx); |
| 1267 | ((void) cookie); |
| 1268 | ((void) cookie_len); |
| 1269 | ((void) cli_id); |
| 1270 | ((void) cli_id_len); |
| 1271 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1272 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1273 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1274 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1275 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1276 | /* |
| 1277 | * Initialize an SSL context |
| 1278 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1279 | void mbedtls_ssl_init(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1280 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1281 | memset(ssl, 0, sizeof(mbedtls_ssl_context)); |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +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_conf_version_check(const mbedtls_ssl_context *ssl) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1286 | { |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 1287 | const mbedtls_ssl_config *conf = ssl->conf; |
| 1288 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1289 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1290 | if (mbedtls_ssl_conf_is_tls13_only(conf)) { |
| 1291 | if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 1292 | MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported.")); |
| 1293 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1296 | MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only.")); |
| 1297 | return 0; |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1298 | } |
| 1299 | #endif |
| 1300 | |
| 1301 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1302 | if (mbedtls_ssl_conf_is_tls12_only(conf)) { |
| 1303 | MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only.")); |
| 1304 | return 0; |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1305 | } |
| 1306 | #endif |
| 1307 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1308 | #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] | 1309 | if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) { |
| 1310 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 1311 | MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2")); |
| 1312 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 1313 | } |
| 1314 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1315 | if (conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 1316 | MBEDTLS_SSL_DEBUG_MSG(1, ("TLS 1.3 server is not supported yet.")); |
| 1317 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
XiaokangQian | 8f9dfe4 | 2022-04-15 02:52:39 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1321 | MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2.")); |
| 1322 | return 0; |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1323 | } |
| 1324 | #endif |
| 1325 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1326 | MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid.")); |
| 1327 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1328 | } |
| 1329 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1330 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1331 | static int ssl_conf_check(const mbedtls_ssl_context *ssl) |
| 1332 | { |
| 1333 | int ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1334 | ret = ssl_conf_version_check(ssl); |
| 1335 | if (ret != 0) { |
| 1336 | return ret; |
| 1337 | } |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1338 | |
Jerry Yu | def7ae4 | 2022-10-30 14:13:19 +0800 | [diff] [blame] | 1339 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 1340 | /* RFC 8446 section 4.4.3 |
| 1341 | * |
| 1342 | * If the verification fails, the receiver MUST terminate the handshake with |
| 1343 | * a "decrypt_error" alert. |
| 1344 | * |
| 1345 | * If the client is configured as TLS 1.3 only with optional verify, return |
| 1346 | * bad config. |
| 1347 | * |
| 1348 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1349 | if (mbedtls_ssl_conf_tls13_ephemeral_enabled( |
| 1350 | (mbedtls_ssl_context *) ssl) && |
Jerry Yu | def7ae4 | 2022-10-30 14:13:19 +0800 | [diff] [blame] | 1351 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 1352 | ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 && |
| 1353 | ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1354 | ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) { |
Jerry Yu | def7ae4 | 2022-10-30 14:13:19 +0800 | [diff] [blame] | 1355 | MBEDTLS_SSL_DEBUG_MSG( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1356 | 1, ("Optional verify auth mode " |
| 1357 | "is not available for TLS 1.3 client")); |
| 1358 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
Jerry Yu | def7ae4 | 2022-10-30 14:13:19 +0800 | [diff] [blame] | 1359 | } |
| 1360 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 1361 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1362 | /* Space for further checks */ |
| 1363 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1364 | return 0; |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1365 | } |
| 1366 | |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1367 | /* |
| 1368 | * Setup an SSL context |
| 1369 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 1370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1371 | int mbedtls_ssl_setup(mbedtls_ssl_context *ssl, |
| 1372 | const mbedtls_ssl_config *conf) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1373 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1374 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1375 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1376 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1377 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 1378 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1379 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1380 | if ((ret = ssl_conf_check(ssl)) != 0) { |
| 1381 | return ret; |
| 1382 | } |
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; |
| 1545 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1546 | mbedtls_ssl_session_reset_msg_layer(ssl, partial); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1547 | |
| 1548 | /* Reset renegotiation state */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1549 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1550 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1551 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1552 | |
| 1553 | ssl->verify_data_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1554 | memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN); |
| 1555 | 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] | 1556 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1557 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1558 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1559 | ssl->session_in = NULL; |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 1560 | ssl->session_out = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1561 | if (ssl->session) { |
| 1562 | mbedtls_ssl_session_free(ssl->session); |
| 1563 | mbedtls_free(ssl->session); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1564 | ssl->session = NULL; |
| 1565 | } |
| 1566 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1567 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 1568 | ssl->alpn_chosen = NULL; |
| 1569 | #endif |
| 1570 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1571 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 1572 | int free_cli_id = 1; |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1573 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1574 | free_cli_id = (partial == 0); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1575 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1576 | if (free_cli_id) { |
| 1577 | mbedtls_free(ssl->cli_id); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1578 | ssl->cli_id = NULL; |
| 1579 | ssl->cli_id_len = 0; |
| 1580 | } |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 1581 | #endif |
| 1582 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1583 | if ((ret = ssl_handshake_init(ssl)) != 0) { |
| 1584 | return ret; |
| 1585 | } |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1586 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1587 | return 0; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 1590 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1591 | * Reset an initialized and used SSL context for re-use while retaining |
| 1592 | * all application-set variables, function pointers and data. |
| 1593 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1594 | int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1595 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1596 | return mbedtls_ssl_session_reset_int(ssl, 0); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1597 | } |
| 1598 | |
| 1599 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1600 | * SSL set accessors |
| 1601 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1602 | void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1603 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1604 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1605 | } |
| 1606 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1607 | 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] | 1608 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1609 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 1610 | } |
| 1611 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1612 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1613 | 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] | 1614 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1615 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 1616 | } |
| 1617 | #endif |
| 1618 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1619 | 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] | 1620 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1621 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1622 | } |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1623 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1624 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1625 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1626 | void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl, |
| 1627 | unsigned allow_packing) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1628 | { |
| 1629 | ssl->disable_datagram_packing = !allow_packing; |
| 1630 | } |
| 1631 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1632 | void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf, |
| 1633 | uint32_t min, uint32_t max) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1634 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1635 | conf->hs_timeout_min = min; |
| 1636 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1637 | } |
| 1638 | #endif |
| 1639 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1640 | void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1641 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1642 | conf->authmode = authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1645 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1646 | void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf, |
| 1647 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 1648 | void *p_vrfy) |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1649 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1650 | conf->f_vrfy = f_vrfy; |
| 1651 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1652 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1653 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1654 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1655 | void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf, |
| 1656 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1657 | void *p_rng) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1658 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1659 | conf->f_rng = f_rng; |
| 1660 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1663 | void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf, |
| 1664 | void (*f_dbg)(void *, int, const char *, int, const char *), |
| 1665 | void *p_dbg) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1666 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1667 | conf->f_dbg = f_dbg; |
| 1668 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1669 | } |
| 1670 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1671 | void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl, |
| 1672 | void *p_bio, |
| 1673 | mbedtls_ssl_send_t *f_send, |
| 1674 | mbedtls_ssl_recv_t *f_recv, |
| 1675 | mbedtls_ssl_recv_timeout_t *f_recv_timeout) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1676 | { |
| 1677 | ssl->p_bio = p_bio; |
| 1678 | ssl->f_send = f_send; |
| 1679 | ssl->f_recv = f_recv; |
| 1680 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 1681 | } |
| 1682 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 1683 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1684 | 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] | 1685 | { |
| 1686 | ssl->mtu = mtu; |
| 1687 | } |
| 1688 | #endif |
| 1689 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1690 | 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] | 1691 | { |
| 1692 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1693 | } |
| 1694 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1695 | void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl, |
| 1696 | void *p_timer, |
| 1697 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 1698 | mbedtls_ssl_get_timer_t *f_get_timer) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1699 | { |
| 1700 | ssl->p_timer = p_timer; |
| 1701 | ssl->f_set_timer = f_set_timer; |
| 1702 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1703 | |
| 1704 | /* Make sure we start with no timer running */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1705 | mbedtls_ssl_set_timer(ssl, 0); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1706 | } |
| 1707 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1708 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1709 | void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf, |
| 1710 | void *p_cache, |
| 1711 | mbedtls_ssl_cache_get_t *f_get_cache, |
| 1712 | mbedtls_ssl_cache_set_t *f_set_cache) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1713 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 1714 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1715 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1716 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1717 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1718 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1719 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1720 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1721 | 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] | 1722 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1723 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1724 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1725 | if (ssl == NULL || |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1726 | session == NULL || |
| 1727 | ssl->session_negotiate == NULL || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1728 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) { |
| 1729 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1730 | } |
| 1731 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1732 | if (ssl->handshake->resume == 1) { |
| 1733 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 1734 | } |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 1735 | |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1736 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1737 | if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1738 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1739 | mbedtls_ssl_ciphersuite_from_id(session->ciphersuite); |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1740 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1741 | if (mbedtls_ssl_validate_ciphersuite( |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1742 | ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1743 | MBEDTLS_SSL_VERSION_TLS1_3) != 0) { |
| 1744 | MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.", |
| 1745 | session->ciphersuite)); |
| 1746 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1747 | } |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1748 | } |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1749 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1750 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1751 | if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate, |
| 1752 | session)) != 0) { |
| 1753 | return ret; |
| 1754 | } |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1755 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1756 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1757 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1758 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1759 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1760 | #endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1761 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1762 | void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf, |
| 1763 | const int *ciphersuites) |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1764 | { |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 1765 | conf->ciphersuite_list = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1768 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1769 | void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf, |
| 1770 | const int kex_modes) |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1771 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1772 | 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] | 1773 | } |
Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame] | 1774 | |
| 1775 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1776 | void mbedtls_ssl_tls13_conf_early_data(mbedtls_ssl_config *conf, |
| 1777 | int early_data_enabled) |
Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame] | 1778 | { |
| 1779 | conf->early_data_enabled = early_data_enabled; |
| 1780 | } |
Jerry Yu | cc4e007 | 2022-11-22 17:22:22 +0800 | [diff] [blame] | 1781 | |
| 1782 | #if defined(MBEDTLS_SSL_SRV_C) |
| 1783 | void mbedtls_ssl_tls13_conf_max_early_data_size( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1784 | mbedtls_ssl_config *conf, uint32_t max_early_data_size) |
Jerry Yu | cc4e007 | 2022-11-22 17:22:22 +0800 | [diff] [blame] | 1785 | { |
Jerry Yu | 39da985 | 2022-12-06 16:58:36 +0800 | [diff] [blame] | 1786 | conf->max_early_data_size = max_early_data_size; |
Jerry Yu | cc4e007 | 2022-11-22 17:22:22 +0800 | [diff] [blame] | 1787 | } |
| 1788 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 1789 | |
Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame] | 1790 | #endif /* MBEDTLS_SSL_EARLY_DATA */ |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1791 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1792 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1793 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1794 | void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf, |
| 1795 | const mbedtls_x509_crt_profile *profile) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1796 | { |
| 1797 | conf->cert_profile = profile; |
| 1798 | } |
| 1799 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1800 | static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert) |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1801 | { |
| 1802 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
| 1803 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1804 | while (cur != NULL) { |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1805 | next = cur->next; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1806 | mbedtls_free(cur); |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1807 | cur = next; |
| 1808 | } |
| 1809 | } |
| 1810 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1811 | /* Append a new keycert entry to a (possibly empty) list */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1812 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1813 | static int ssl_append_key_cert(mbedtls_ssl_key_cert **head, |
| 1814 | mbedtls_x509_crt *cert, |
| 1815 | mbedtls_pk_context *key) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1816 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1817 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1818 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1819 | if (cert == NULL) { |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1820 | /* Free list if cert is null */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1821 | ssl_key_cert_free(*head); |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1822 | *head = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1823 | return 0; |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1824 | } |
| 1825 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1826 | new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert)); |
| 1827 | if (new_cert == NULL) { |
| 1828 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 1829 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1830 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1831 | new_cert->cert = cert; |
| 1832 | new_cert->key = key; |
| 1833 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1834 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1835 | /* Update head if the list was null, else add to the end */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1836 | if (*head == NULL) { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1837 | *head = new_cert; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1838 | } else { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1839 | mbedtls_ssl_key_cert *cur = *head; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1840 | while (cur->next != NULL) { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1841 | cur = cur->next; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1842 | } |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1843 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1844 | } |
| 1845 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1846 | return 0; |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1847 | } |
| 1848 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1849 | int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1850 | mbedtls_x509_crt *own_cert, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1851 | mbedtls_pk_context *pk_key) |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1852 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1853 | 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] | 1854 | } |
| 1855 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1856 | void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1857 | mbedtls_x509_crt *ca_chain, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1858 | mbedtls_x509_crl *ca_crl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1859 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1860 | conf->ca_chain = ca_chain; |
| 1861 | conf->ca_crl = ca_crl; |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1862 | |
| 1863 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1864 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1865 | * cannot be used together. */ |
| 1866 | conf->f_ca_cb = NULL; |
| 1867 | conf->p_ca_cb = NULL; |
| 1868 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1869 | } |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1870 | |
| 1871 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1872 | void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf, |
| 1873 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
| 1874 | void *p_ca_cb) |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1875 | { |
| 1876 | conf->f_ca_cb = f_ca_cb; |
| 1877 | conf->p_ca_cb = p_ca_cb; |
| 1878 | |
| 1879 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1880 | * cannot be used together. */ |
| 1881 | conf->ca_chain = NULL; |
| 1882 | conf->ca_crl = NULL; |
| 1883 | } |
| 1884 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1885 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 1886 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1887 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1888 | const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl, |
| 1889 | size_t *name_len) |
Glenn Strauss | 6989407 | 2022-01-24 12:58:00 -0500 | [diff] [blame] | 1890 | { |
| 1891 | *name_len = ssl->handshake->sni_name_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1892 | return ssl->handshake->sni_name; |
Glenn Strauss | 6989407 | 2022-01-24 12:58:00 -0500 | [diff] [blame] | 1893 | } |
| 1894 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1895 | int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl, |
| 1896 | mbedtls_x509_crt *own_cert, |
| 1897 | mbedtls_pk_context *pk_key) |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1898 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1899 | return ssl_append_key_cert(&ssl->handshake->sni_key_cert, |
| 1900 | own_cert, pk_key); |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1901 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 1902 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1903 | void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl, |
| 1904 | mbedtls_x509_crt *ca_chain, |
| 1905 | mbedtls_x509_crl *ca_crl) |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 1906 | { |
| 1907 | ssl->handshake->sni_ca_chain = ca_chain; |
| 1908 | ssl->handshake->sni_ca_crl = ca_crl; |
| 1909 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1910 | |
Glenn Strauss | 999ef70 | 2022-03-11 01:37:23 -0500 | [diff] [blame] | 1911 | #if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1912 | void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl, |
| 1913 | const mbedtls_x509_crt *crt) |
Glenn Strauss | 999ef70 | 2022-03-11 01:37:23 -0500 | [diff] [blame] | 1914 | { |
| 1915 | ssl->handshake->dn_hints = crt; |
| 1916 | } |
| 1917 | #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ |
| 1918 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1919 | void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl, |
| 1920 | int authmode) |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1921 | { |
| 1922 | ssl->handshake->sni_authmode = authmode; |
| 1923 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1924 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 1925 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1926 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1927 | void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl, |
| 1928 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 1929 | void *p_vrfy) |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1930 | { |
| 1931 | ssl->f_vrfy = f_vrfy; |
| 1932 | ssl->p_vrfy = p_vrfy; |
| 1933 | } |
| 1934 | #endif |
| 1935 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1936 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1937 | |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1938 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1939 | static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1940 | mbedtls_ssl_context *ssl, |
| 1941 | mbedtls_svc_key_id_t pwd) |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1942 | { |
| 1943 | psa_status_t status; |
| 1944 | psa_pake_role_t psa_role; |
| 1945 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 1946 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1947 | psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE); |
| 1948 | psa_pake_cs_set_primitive(&cipher_suite, |
| 1949 | PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, |
| 1950 | PSA_ECC_FAMILY_SECP_R1, |
| 1951 | 256)); |
| 1952 | psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256); |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1953 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1954 | status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite); |
| 1955 | if (status != PSA_SUCCESS) { |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1956 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1957 | } |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1958 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1959 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1960 | psa_role = PSA_PAKE_ROLE_SERVER; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1961 | } else { |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1962 | psa_role = PSA_PAKE_ROLE_CLIENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1963 | } |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1964 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1965 | status = psa_pake_set_role(&ssl->handshake->psa_pake_ctx, psa_role); |
| 1966 | if (status != PSA_SUCCESS) { |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1967 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1968 | } |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1969 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1970 | status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd); |
| 1971 | if (status != PSA_SUCCESS) { |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1972 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1973 | } |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1974 | |
| 1975 | ssl->handshake->psa_pake_ctx_is_ok = 1; |
| 1976 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1977 | return PSA_SUCCESS; |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1978 | } |
| 1979 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1980 | int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl, |
| 1981 | const unsigned char *pw, |
| 1982 | size_t pw_len) |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1983 | { |
| 1984 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1985 | psa_status_t status; |
| 1986 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1987 | if (ssl->handshake == NULL || ssl->conf == NULL) { |
| 1988 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1989 | } |
| 1990 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1991 | /* Empty password is not valid */ |
| 1992 | if ((pw == NULL) || (pw_len == 0)) { |
| 1993 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 1994 | } |
| 1995 | |
| 1996 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 1997 | psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE); |
| 1998 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 1999 | |
| 2000 | status = psa_import_key(&attributes, pw, pw_len, |
| 2001 | &ssl->handshake->psa_pake_password); |
| 2002 | if (status != PSA_SUCCESS) { |
| 2003 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 2004 | } |
| 2005 | |
| 2006 | status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, |
| 2007 | ssl->handshake->psa_pake_password); |
| 2008 | if (status != PSA_SUCCESS) { |
| 2009 | psa_destroy_key(ssl->handshake->psa_pake_password); |
| 2010 | psa_pake_abort(&ssl->handshake->psa_pake_ctx); |
| 2011 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 2012 | } |
| 2013 | |
| 2014 | return 0; |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 2015 | } |
Valerio Setti | a9a97dc | 2022-11-28 18:26:16 +0100 | [diff] [blame] | 2016 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2017 | int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl, |
| 2018 | mbedtls_svc_key_id_t pwd) |
Valerio Setti | a9a97dc | 2022-11-28 18:26:16 +0100 | [diff] [blame] | 2019 | { |
Valerio Setti | a9a97dc | 2022-11-28 18:26:16 +0100 | [diff] [blame] | 2020 | psa_status_t status; |
| 2021 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2022 | if (ssl->handshake == NULL || ssl->conf == NULL) { |
| 2023 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 2024 | } |
| 2025 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2026 | if (mbedtls_svc_key_id_is_null(pwd)) { |
| 2027 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2028 | } |
| 2029 | |
| 2030 | status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd); |
| 2031 | if (status != PSA_SUCCESS) { |
| 2032 | psa_pake_abort(&ssl->handshake->psa_pake_ctx); |
| 2033 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 2034 | } |
| 2035 | |
| 2036 | return 0; |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 2037 | } |
| 2038 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2039 | int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl, |
| 2040 | const unsigned char *pw, |
| 2041 | size_t pw_len) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2042 | { |
| 2043 | mbedtls_ecjpake_role role; |
| 2044 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2045 | if (ssl->handshake == NULL || ssl->conf == NULL) { |
| 2046 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2047 | } |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2048 | |
Valerio Setti | c689ed8 | 2022-12-07 14:40:38 +0100 | [diff] [blame] | 2049 | /* Empty password is not valid */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2050 | if ((pw == NULL) || (pw_len == 0)) { |
| 2051 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2052 | } |
Valerio Setti | c689ed8 | 2022-12-07 14:40:38 +0100 | [diff] [blame] | 2053 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2054 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2055 | role = MBEDTLS_ECJPAKE_SERVER; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2056 | } else { |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2057 | role = MBEDTLS_ECJPAKE_CLIENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2058 | } |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2059 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2060 | return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx, |
| 2061 | role, |
| 2062 | MBEDTLS_MD_SHA256, |
| 2063 | MBEDTLS_ECP_DP_SECP256R1, |
| 2064 | pw, pw_len); |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2065 | } |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 2066 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 2067 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2068 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 2069 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2070 | int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2071 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2072 | if (conf->psk_identity == NULL || |
| 2073 | conf->psk_identity_len == 0) { |
| 2074 | return 0; |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 2075 | } |
| 2076 | |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2077 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2078 | if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) { |
| 2079 | return 1; |
| 2080 | } |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2081 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 2082 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2083 | if (conf->psk != NULL && conf->psk_len != 0) { |
| 2084 | return 1; |
| 2085 | } |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2086 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2087 | return 0; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2088 | } |
| 2089 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2090 | static void ssl_conf_remove_psk(mbedtls_ssl_config *conf) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2091 | { |
| 2092 | /* Remove reference to existing PSK, if any. */ |
| 2093 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2094 | if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) { |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2095 | /* The maintenance of the PSK key slot is the |
| 2096 | * user's responsibility. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2097 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2098 | } |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2099 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2100 | if (conf->psk != NULL) { |
| 2101 | mbedtls_platform_zeroize(conf->psk, conf->psk_len); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2102 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2103 | mbedtls_free(conf->psk); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2104 | conf->psk = NULL; |
| 2105 | conf->psk_len = 0; |
| 2106 | } |
| 2107 | |
| 2108 | /* Remove reference to PSK identity, if any. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2109 | if (conf->psk_identity != NULL) { |
| 2110 | mbedtls_free(conf->psk_identity); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2111 | conf->psk_identity = NULL; |
| 2112 | conf->psk_identity_len = 0; |
| 2113 | } |
| 2114 | } |
| 2115 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2116 | /* This function assumes that PSK identity in the SSL config is unset. |
| 2117 | * It checks that the provided identity is well-formed and attempts |
| 2118 | * to make a copy of it in the SSL config. |
| 2119 | * On failure, the PSK identity in the config remains unset. */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 2120 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2121 | static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf, |
| 2122 | unsigned char const *psk_identity, |
| 2123 | size_t psk_identity_len) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2124 | { |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 2125 | /* Identity len will be encoded on two bytes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2126 | if (psk_identity == NULL || |
Ronald Cron | 2a87e9b | 2022-10-19 10:55:26 +0200 | [diff] [blame] | 2127 | psk_identity_len == 0 || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2128 | (psk_identity_len >> 16) != 0 || |
| 2129 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) { |
| 2130 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 2131 | } |
| 2132 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2133 | conf->psk_identity = mbedtls_calloc(1, psk_identity_len); |
| 2134 | if (conf->psk_identity == NULL) { |
| 2135 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2136 | } |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 2137 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 2138 | conf->psk_identity_len = psk_identity_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2139 | memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len); |
Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 2140 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2141 | return 0; |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 2142 | } |
| 2143 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2144 | int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf, |
| 2145 | const unsigned char *psk, size_t psk_len, |
| 2146 | const unsigned char *psk_identity, size_t psk_identity_len) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2147 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2148 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2149 | |
| 2150 | /* We currently only support one PSK, raw or opaque. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2151 | if (mbedtls_ssl_conf_has_static_psk(conf)) { |
| 2152 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 2153 | } |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2154 | |
| 2155 | /* Check and set raw PSK */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2156 | if (psk == NULL) { |
| 2157 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2158 | } |
| 2159 | if (psk_len == 0) { |
| 2160 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2161 | } |
| 2162 | if (psk_len > MBEDTLS_PSK_MAX_LEN) { |
| 2163 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2164 | } |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 2165 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2166 | if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) { |
| 2167 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2168 | } |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2169 | conf->psk_len = psk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2170 | memcpy(conf->psk, psk, conf->psk_len); |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2171 | |
| 2172 | /* Check and set PSK Identity */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2173 | ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len); |
| 2174 | if (ret != 0) { |
| 2175 | ssl_conf_remove_psk(conf); |
| 2176 | } |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2177 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2178 | return ret; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2179 | } |
| 2180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2181 | static void ssl_remove_psk(mbedtls_ssl_context *ssl) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2182 | { |
| 2183 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2184 | if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2185 | /* The maintenance of the external PSK key slot is the |
| 2186 | * user's responsibility. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2187 | if (ssl->handshake->psk_opaque_is_internal) { |
| 2188 | psa_destroy_key(ssl->handshake->psk_opaque); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2189 | ssl->handshake->psk_opaque_is_internal = 0; |
| 2190 | } |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2191 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2192 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 2193 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2194 | if (ssl->handshake->psk != NULL) { |
| 2195 | mbedtls_platform_zeroize(ssl->handshake->psk, |
| 2196 | ssl->handshake->psk_len); |
| 2197 | mbedtls_free(ssl->handshake->psk); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2198 | ssl->handshake->psk_len = 0; |
| 2199 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 2200 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2201 | } |
| 2202 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2203 | int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl, |
| 2204 | const unsigned char *psk, size_t psk_len) |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2205 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2206 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2207 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
Jerry Yu | 5d01c05 | 2022-08-17 10:18:10 +0800 | [diff] [blame] | 2208 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 24b8c81 | 2022-08-20 19:06:56 +0800 | [diff] [blame] | 2209 | psa_algorithm_t alg = PSA_ALG_NONE; |
Jerry Yu | 5d01c05 | 2022-08-17 10:18:10 +0800 | [diff] [blame] | 2210 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2211 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2212 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2213 | if (psk == NULL || ssl->handshake == NULL) { |
| 2214 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2215 | } |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2216 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2217 | if (psk_len > MBEDTLS_PSK_MAX_LEN) { |
| 2218 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2219 | } |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2221 | ssl_remove_psk(ssl); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2222 | |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2223 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 2224 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2225 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) { |
| 2226 | if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) { |
| 2227 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
| 2228 | } else { |
| 2229 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 2230 | } |
| 2231 | psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE); |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 2232 | } |
| 2233 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2234 | |
Jerry Yu | 568ec25 | 2022-07-22 21:27:34 +0800 | [diff] [blame] | 2235 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2236 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
| 2237 | alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH); |
| 2238 | psa_set_key_usage_flags(&key_attributes, |
| 2239 | PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT); |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 2240 | } |
| 2241 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 2242 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2243 | psa_set_key_algorithm(&key_attributes, alg); |
| 2244 | psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2245 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2246 | status = psa_import_key(&key_attributes, psk, psk_len, &key); |
| 2247 | if (status != PSA_SUCCESS) { |
| 2248 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 2249 | } |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2250 | |
| 2251 | /* Allow calling psa_destroy_key() on psk remove */ |
| 2252 | ssl->handshake->psk_opaque_is_internal = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2253 | return mbedtls_ssl_set_hs_psk_opaque(ssl, key); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2254 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2255 | if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) { |
| 2256 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2257 | } |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2258 | |
| 2259 | ssl->handshake->psk_len = psk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2260 | memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2261 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2262 | return 0; |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2263 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2264 | } |
| 2265 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2266 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2267 | int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf, |
| 2268 | mbedtls_svc_key_id_t psk, |
| 2269 | const unsigned char *psk_identity, |
| 2270 | size_t psk_identity_len) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2271 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2272 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2273 | |
| 2274 | /* We currently only support one PSK, raw or opaque. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2275 | if (mbedtls_ssl_conf_has_static_psk(conf)) { |
| 2276 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 2277 | } |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2278 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2279 | /* Check and set opaque PSK */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2280 | if (mbedtls_svc_key_id_is_null(psk)) { |
| 2281 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2282 | } |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2283 | conf->psk_opaque = psk; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2284 | |
| 2285 | /* Check and set PSK Identity */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2286 | ret = ssl_conf_set_psk_identity(conf, psk_identity, |
| 2287 | psk_identity_len); |
| 2288 | if (ret != 0) { |
| 2289 | ssl_conf_remove_psk(conf); |
| 2290 | } |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2291 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2292 | return ret; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2293 | } |
| 2294 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2295 | int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl, |
| 2296 | mbedtls_svc_key_id_t psk) |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2297 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2298 | if ((mbedtls_svc_key_id_is_null(psk)) || |
| 2299 | (ssl->handshake == NULL)) { |
| 2300 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2301 | } |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2302 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2303 | ssl_remove_psk(ssl); |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2304 | ssl->handshake->psk_opaque = psk; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2305 | return 0; |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2306 | } |
| 2307 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2308 | |
Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 2309 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2310 | void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf, |
| 2311 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
| 2312 | size_t), |
| 2313 | void *p_psk) |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2314 | { |
| 2315 | conf->f_psk = f_psk; |
| 2316 | conf->p_psk = p_psk; |
| 2317 | } |
Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 2318 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 2319 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 2320 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2321 | |
| 2322 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2323 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2324 | psa_algorithm_t alg) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2325 | { |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2326 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2327 | if (alg == PSA_ALG_CBC_NO_PADDING) { |
| 2328 | return MBEDTLS_SSL_MODE_CBC; |
| 2329 | } |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2330 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2331 | if (PSA_ALG_IS_AEAD(alg)) { |
| 2332 | return MBEDTLS_SSL_MODE_AEAD; |
| 2333 | } |
| 2334 | return MBEDTLS_SSL_MODE_STREAM; |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2335 | } |
| 2336 | |
| 2337 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2338 | |
| 2339 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2340 | mbedtls_cipher_mode_t mode) |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2341 | { |
| 2342 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2343 | if (mode == MBEDTLS_MODE_CBC) { |
| 2344 | return MBEDTLS_SSL_MODE_CBC; |
| 2345 | } |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2346 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 2347 | |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2348 | #if defined(MBEDTLS_GCM_C) || \ |
| 2349 | defined(MBEDTLS_CCM_C) || \ |
| 2350 | defined(MBEDTLS_CHACHAPOLY_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2351 | if (mode == MBEDTLS_MODE_GCM || |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2352 | mode == MBEDTLS_MODE_CCM || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2353 | mode == MBEDTLS_MODE_CHACHAPOLY) { |
| 2354 | return MBEDTLS_SSL_MODE_AEAD; |
| 2355 | } |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2356 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2358 | return MBEDTLS_SSL_MODE_STREAM; |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2359 | } |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2360 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2361 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2362 | static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode( |
| 2363 | mbedtls_ssl_mode_t base_mode, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2364 | int encrypt_then_mac) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2365 | { |
| 2366 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2367 | if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED && |
| 2368 | base_mode == MBEDTLS_SSL_MODE_CBC) { |
| 2369 | return MBEDTLS_SSL_MODE_CBC_ETM; |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2370 | } |
| 2371 | #else |
| 2372 | (void) encrypt_then_mac; |
| 2373 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2374 | return base_mode; |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2375 | } |
| 2376 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 2377 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2378 | const mbedtls_ssl_transform *transform) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2379 | { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2380 | mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode( |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2381 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2382 | transform->psa_alg |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2383 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2384 | mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2385 | #endif |
| 2386 | ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2387 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2388 | int encrypt_then_mac = 0; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2389 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2390 | encrypt_then_mac = transform->encrypt_then_mac; |
| 2391 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2392 | return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2393 | } |
| 2394 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 2395 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2396 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2397 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2398 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2399 | const mbedtls_ssl_ciphersuite_t *suite) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2400 | { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2401 | mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM; |
| 2402 | |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2403 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2404 | psa_status_t status; |
| 2405 | psa_algorithm_t alg; |
| 2406 | psa_key_type_t type; |
| 2407 | size_t size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2408 | status = mbedtls_ssl_cipher_to_psa(suite->cipher, 0, &alg, &type, &size); |
| 2409 | if (status == PSA_SUCCESS) { |
| 2410 | base_mode = mbedtls_ssl_get_base_mode(alg); |
| 2411 | } |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2412 | #else |
| 2413 | const mbedtls_cipher_info_t *cipher = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2414 | mbedtls_cipher_info_from_type(suite->cipher); |
| 2415 | if (cipher != NULL) { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2416 | base_mode = |
| 2417 | mbedtls_ssl_get_base_mode( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2418 | mbedtls_cipher_info_get_mode(cipher)); |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2419 | } |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2420 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2421 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2422 | #if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 2423 | int encrypt_then_mac = 0; |
| 2424 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2425 | return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2426 | } |
| 2427 | |
Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 2428 | #if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2429 | |
| 2430 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2431 | /* Serialization of TLS 1.3 sessions: |
| 2432 | * |
| 2433 | * struct { |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 2434 | * opaque hostname<0..2^16-1>; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2435 | * uint64 ticket_received; |
| 2436 | * uint32 ticket_lifetime; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2437 | * opaque ticket<1..2^16-1>; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2438 | * } ClientOnlyData; |
| 2439 | * |
| 2440 | * struct { |
| 2441 | * uint8 endpoint; |
| 2442 | * uint8 ciphersuite[2]; |
| 2443 | * uint32 ticket_age_add; |
| 2444 | * uint8 ticket_flags; |
| 2445 | * opaque resumption_key<0..255>; |
| 2446 | * select ( endpoint ) { |
| 2447 | * case client: ClientOnlyData; |
| 2448 | * case server: uint64 start_time; |
| 2449 | * }; |
| 2450 | * } serialized_session_tls13; |
| 2451 | * |
| 2452 | */ |
| 2453 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2454 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2455 | static int ssl_tls13_session_save(const mbedtls_ssl_session *session, |
| 2456 | unsigned char *buf, |
| 2457 | size_t buf_len, |
| 2458 | size_t *olen) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2459 | { |
| 2460 | unsigned char *p = buf; |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 2461 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
| 2462 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2463 | size_t hostname_len = (session->hostname == NULL) ? |
| 2464 | 0 : strlen(session->hostname) + 1; |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 2465 | #endif |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2466 | size_t needed = 1 /* endpoint */ |
| 2467 | + 2 /* ciphersuite */ |
| 2468 | + 4 /* ticket_age_add */ |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2469 | + 1 /* ticket_flags */ |
| 2470 | + 1; /* resumption_key length */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2471 | *olen = 0; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2472 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2473 | if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) { |
| 2474 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2475 | } |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2476 | needed += session->resumption_key_len; /* resumption_key */ |
| 2477 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2478 | #if defined(MBEDTLS_HAVE_TIME) |
| 2479 | needed += 8; /* start_time or ticket_received */ |
| 2480 | #endif |
| 2481 | |
| 2482 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2483 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 2484 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Xiaokang Qian | bc663a0 | 2022-10-09 11:14:39 +0000 | [diff] [blame] | 2485 | needed += 2 /* hostname_len */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2486 | + hostname_len; /* hostname */ |
Xiaokang Qian | 281fd1b | 2022-09-20 11:35:41 +0000 | [diff] [blame] | 2487 | #endif |
| 2488 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2489 | needed += 4 /* ticket_lifetime */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2490 | + 2; /* ticket_len */ |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2491 | |
| 2492 | /* Check size_t overflow */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2493 | if (session->ticket_len > SIZE_MAX - needed) { |
| 2494 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2495 | } |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2496 | |
Jerry Yu | e28d974 | 2022-08-18 15:44:03 +0800 | [diff] [blame] | 2497 | needed += session->ticket_len; /* ticket */ |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2498 | } |
| 2499 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 2500 | |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2501 | *olen = needed; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2502 | if (needed > buf_len) { |
| 2503 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 2504 | } |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2505 | |
| 2506 | p[0] = session->endpoint; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2507 | MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 1); |
| 2508 | MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 3); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2509 | p[7] = session->ticket_flags; |
| 2510 | |
| 2511 | /* save resumption_key */ |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2512 | p[8] = session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2513 | p += 9; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2514 | memcpy(p, session->resumption_key, session->resumption_key_len); |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2515 | p += session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2516 | |
| 2517 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2518 | if (session->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 2519 | MBEDTLS_PUT_UINT64_BE((uint64_t) session->start, p, 0); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2520 | p += 8; |
| 2521 | } |
| 2522 | #endif /* MBEDTLS_HAVE_TIME */ |
| 2523 | |
| 2524 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2525 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2526 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2527 | MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0); |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2528 | p += 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2529 | if (hostname_len > 0) { |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2530 | /* save host name */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2531 | memcpy(p, session->hostname, hostname_len); |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2532 | p += hostname_len; |
| 2533 | } |
| 2534 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 2535 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2536 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2537 | MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_received, p, 0); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2538 | p += 8; |
| 2539 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2540 | MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2541 | p += 4; |
| 2542 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2543 | MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2544 | p += 2; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2545 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2546 | if (session->ticket != NULL && session->ticket_len > 0) { |
| 2547 | memcpy(p, session->ticket, session->ticket_len); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2548 | p += session->ticket_len; |
| 2549 | } |
| 2550 | } |
| 2551 | #endif /* MBEDTLS_SSL_CLI_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2552 | return 0; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2553 | } |
| 2554 | |
| 2555 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2556 | static int ssl_tls13_session_load(mbedtls_ssl_session *session, |
| 2557 | const unsigned char *buf, |
| 2558 | size_t len) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2559 | { |
| 2560 | const unsigned char *p = buf; |
| 2561 | const unsigned char *end = buf + len; |
| 2562 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2563 | if (end - p < 9) { |
| 2564 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2565 | } |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2566 | session->endpoint = p[0]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2567 | session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 1); |
| 2568 | session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 3); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2569 | session->ticket_flags = p[7]; |
| 2570 | |
| 2571 | /* load resumption_key */ |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2572 | session->resumption_key_len = p[8]; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2573 | p += 9; |
| 2574 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2575 | if (end - p < session->resumption_key_len) { |
| 2576 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2577 | } |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2578 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2579 | if (sizeof(session->resumption_key) < session->resumption_key_len) { |
| 2580 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2581 | } |
| 2582 | memcpy(session->resumption_key, p, session->resumption_key_len); |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2583 | p += session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2584 | |
| 2585 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2586 | if (session->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 2587 | if (end - p < 8) { |
| 2588 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2589 | } |
| 2590 | session->start = MBEDTLS_GET_UINT64_BE(p, 0); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2591 | p += 8; |
| 2592 | } |
| 2593 | #endif /* MBEDTLS_HAVE_TIME */ |
| 2594 | |
| 2595 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2596 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2597 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2598 | defined(MBEDTLS_SSL_SESSION_TICKETS) |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2599 | size_t hostname_len; |
| 2600 | /* load host name */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2601 | if (end - p < 2) { |
| 2602 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2603 | } |
| 2604 | hostname_len = MBEDTLS_GET_UINT16_BE(p, 0); |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2605 | p += 2; |
| 2606 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2607 | if (end - p < (long int) hostname_len) { |
| 2608 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2609 | } |
| 2610 | if (hostname_len > 0) { |
| 2611 | session->hostname = mbedtls_calloc(1, hostname_len); |
| 2612 | if (session->hostname == NULL) { |
| 2613 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2614 | } |
| 2615 | memcpy(session->hostname, p, hostname_len); |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2616 | p += hostname_len; |
| 2617 | } |
| 2618 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION && |
| 2619 | MBEDTLS_SSL_SESSION_TICKETS */ |
| 2620 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2621 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2622 | if (end - p < 8) { |
| 2623 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2624 | } |
| 2625 | session->ticket_received = MBEDTLS_GET_UINT64_BE(p, 0); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2626 | p += 8; |
| 2627 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2628 | if (end - p < 4) { |
| 2629 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2630 | } |
| 2631 | session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2632 | p += 4; |
| 2633 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2634 | if (end - p < 2) { |
| 2635 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2636 | } |
| 2637 | session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2638 | p += 2; |
| 2639 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2640 | if (end - p < (long int) session->ticket_len) { |
| 2641 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2642 | } |
| 2643 | if (session->ticket_len > 0) { |
| 2644 | session->ticket = mbedtls_calloc(1, session->ticket_len); |
| 2645 | if (session->ticket == NULL) { |
| 2646 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2647 | } |
| 2648 | memcpy(session->ticket, p, session->ticket_len); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2649 | p += session->ticket_len; |
| 2650 | } |
| 2651 | } |
| 2652 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 2653 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2654 | return 0; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2655 | |
| 2656 | } |
| 2657 | #else /* MBEDTLS_SSL_SESSION_TICKETS */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2658 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2659 | static int ssl_tls13_session_save(const mbedtls_ssl_session *session, |
| 2660 | unsigned char *buf, |
| 2661 | size_t buf_len, |
| 2662 | size_t *olen) |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2663 | { |
| 2664 | ((void) session); |
| 2665 | ((void) buf); |
| 2666 | ((void) buf_len); |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2667 | *olen = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2668 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2669 | } |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2670 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2671 | static int ssl_tls13_session_load(const mbedtls_ssl_session *session, |
| 2672 | unsigned char *buf, |
| 2673 | size_t buf_len) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2674 | { |
| 2675 | ((void) session); |
| 2676 | ((void) buf); |
| 2677 | ((void) buf_len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2678 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2679 | } |
| 2680 | #endif /* !MBEDTLS_SSL_SESSION_TICKETS */ |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2681 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 2682 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2683 | psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type, |
| 2684 | size_t taglen, |
| 2685 | psa_algorithm_t *alg, |
| 2686 | psa_key_type_t *key_type, |
| 2687 | size_t *key_size) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2688 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2689 | switch (mbedtls_cipher_type) { |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2690 | case MBEDTLS_CIPHER_AES_128_CBC: |
| 2691 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2692 | *key_type = PSA_KEY_TYPE_AES; |
| 2693 | *key_size = 128; |
| 2694 | break; |
| 2695 | case MBEDTLS_CIPHER_AES_128_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2696 | *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] | 2697 | *key_type = PSA_KEY_TYPE_AES; |
| 2698 | *key_size = 128; |
| 2699 | break; |
| 2700 | case MBEDTLS_CIPHER_AES_128_GCM: |
| 2701 | *alg = PSA_ALG_GCM; |
| 2702 | *key_type = PSA_KEY_TYPE_AES; |
| 2703 | *key_size = 128; |
| 2704 | break; |
| 2705 | case MBEDTLS_CIPHER_AES_192_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2706 | *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] | 2707 | *key_type = PSA_KEY_TYPE_AES; |
| 2708 | *key_size = 192; |
| 2709 | break; |
| 2710 | case MBEDTLS_CIPHER_AES_192_GCM: |
| 2711 | *alg = PSA_ALG_GCM; |
| 2712 | *key_type = PSA_KEY_TYPE_AES; |
| 2713 | *key_size = 192; |
| 2714 | break; |
| 2715 | case MBEDTLS_CIPHER_AES_256_CBC: |
| 2716 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2717 | *key_type = PSA_KEY_TYPE_AES; |
| 2718 | *key_size = 256; |
| 2719 | break; |
| 2720 | case MBEDTLS_CIPHER_AES_256_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2721 | *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] | 2722 | *key_type = PSA_KEY_TYPE_AES; |
| 2723 | *key_size = 256; |
| 2724 | break; |
| 2725 | case MBEDTLS_CIPHER_AES_256_GCM: |
| 2726 | *alg = PSA_ALG_GCM; |
| 2727 | *key_type = PSA_KEY_TYPE_AES; |
| 2728 | *key_size = 256; |
| 2729 | break; |
| 2730 | case MBEDTLS_CIPHER_ARIA_128_CBC: |
| 2731 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2732 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2733 | *key_size = 128; |
| 2734 | break; |
| 2735 | case MBEDTLS_CIPHER_ARIA_128_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2736 | *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] | 2737 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2738 | *key_size = 128; |
| 2739 | break; |
| 2740 | case MBEDTLS_CIPHER_ARIA_128_GCM: |
| 2741 | *alg = PSA_ALG_GCM; |
| 2742 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2743 | *key_size = 128; |
| 2744 | break; |
| 2745 | case MBEDTLS_CIPHER_ARIA_192_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2746 | *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] | 2747 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2748 | *key_size = 192; |
| 2749 | break; |
| 2750 | case MBEDTLS_CIPHER_ARIA_192_GCM: |
| 2751 | *alg = PSA_ALG_GCM; |
| 2752 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2753 | *key_size = 192; |
| 2754 | break; |
| 2755 | case MBEDTLS_CIPHER_ARIA_256_CBC: |
| 2756 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2757 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2758 | *key_size = 256; |
| 2759 | break; |
| 2760 | case MBEDTLS_CIPHER_ARIA_256_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2761 | *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] | 2762 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2763 | *key_size = 256; |
| 2764 | break; |
| 2765 | case MBEDTLS_CIPHER_ARIA_256_GCM: |
| 2766 | *alg = PSA_ALG_GCM; |
| 2767 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2768 | *key_size = 256; |
| 2769 | break; |
| 2770 | case MBEDTLS_CIPHER_CAMELLIA_128_CBC: |
| 2771 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2772 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2773 | *key_size = 128; |
| 2774 | break; |
| 2775 | case MBEDTLS_CIPHER_CAMELLIA_128_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2776 | *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] | 2777 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2778 | *key_size = 128; |
| 2779 | break; |
| 2780 | case MBEDTLS_CIPHER_CAMELLIA_128_GCM: |
| 2781 | *alg = PSA_ALG_GCM; |
| 2782 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2783 | *key_size = 128; |
| 2784 | break; |
| 2785 | case MBEDTLS_CIPHER_CAMELLIA_192_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2786 | *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] | 2787 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2788 | *key_size = 192; |
| 2789 | break; |
| 2790 | case MBEDTLS_CIPHER_CAMELLIA_192_GCM: |
| 2791 | *alg = PSA_ALG_GCM; |
| 2792 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2793 | *key_size = 192; |
| 2794 | break; |
| 2795 | case MBEDTLS_CIPHER_CAMELLIA_256_CBC: |
| 2796 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2797 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2798 | *key_size = 256; |
| 2799 | break; |
| 2800 | case MBEDTLS_CIPHER_CAMELLIA_256_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2801 | *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] | 2802 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2803 | *key_size = 256; |
| 2804 | break; |
| 2805 | case MBEDTLS_CIPHER_CAMELLIA_256_GCM: |
| 2806 | *alg = PSA_ALG_GCM; |
| 2807 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2808 | *key_size = 256; |
| 2809 | break; |
| 2810 | case MBEDTLS_CIPHER_CHACHA20_POLY1305: |
| 2811 | *alg = PSA_ALG_CHACHA20_POLY1305; |
| 2812 | *key_type = PSA_KEY_TYPE_CHACHA20; |
| 2813 | *key_size = 256; |
| 2814 | break; |
| 2815 | case MBEDTLS_CIPHER_NULL: |
| 2816 | *alg = MBEDTLS_SSL_NULL_CIPHER; |
| 2817 | *key_type = 0; |
| 2818 | *key_size = 0; |
| 2819 | break; |
| 2820 | default: |
| 2821 | return PSA_ERROR_NOT_SUPPORTED; |
| 2822 | } |
| 2823 | |
| 2824 | return PSA_SUCCESS; |
| 2825 | } |
Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 2826 | #endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2827 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2828 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2829 | int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf, |
| 2830 | const unsigned char *dhm_P, size_t P_len, |
| 2831 | const unsigned char *dhm_G, size_t G_len) |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2832 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2833 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2834 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2835 | mbedtls_mpi_free(&conf->dhm_P); |
| 2836 | mbedtls_mpi_free(&conf->dhm_G); |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2837 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2838 | if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 || |
| 2839 | (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) { |
| 2840 | mbedtls_mpi_free(&conf->dhm_P); |
| 2841 | mbedtls_mpi_free(&conf->dhm_G); |
| 2842 | return ret; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2843 | } |
| 2844 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2845 | return 0; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2846 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2847 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2848 | 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] | 2849 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2850 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2851 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2852 | mbedtls_mpi_free(&conf->dhm_P); |
| 2853 | mbedtls_mpi_free(&conf->dhm_G); |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2854 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2855 | if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P, |
| 2856 | &conf->dhm_P)) != 0 || |
| 2857 | (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G, |
| 2858 | &conf->dhm_G)) != 0) { |
| 2859 | mbedtls_mpi_free(&conf->dhm_P); |
| 2860 | mbedtls_mpi_free(&conf->dhm_G); |
| 2861 | return ret; |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 2862 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2863 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2864 | return 0; |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2865 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2866 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2867 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 2868 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 2869 | /* |
| 2870 | * Set the minimum length for Diffie-Hellman parameters |
| 2871 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2872 | void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf, |
| 2873 | unsigned int bitlen) |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 2874 | { |
| 2875 | conf->dhm_min_bitlen = bitlen; |
| 2876 | } |
| 2877 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 2878 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2879 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2880 | #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] | 2881 | /* |
| 2882 | * Set allowed/preferred hashes for handshake signatures |
| 2883 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2884 | void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf, |
| 2885 | const int *hashes) |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 2886 | { |
| 2887 | conf->sig_hashes = hashes; |
| 2888 | } |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2889 | #endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2890 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2891 | /* Configure allowed signature algorithms for handshake */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2892 | void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf, |
| 2893 | const uint16_t *sig_algs) |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2894 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2895 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2896 | conf->sig_hashes = NULL; |
| 2897 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 2898 | conf->sig_algs = sig_algs; |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2899 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2900 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 2901 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 2902 | #if defined(MBEDTLS_ECP_C) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2903 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2904 | /* |
| 2905 | * Set the allowed elliptic curves |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2906 | * |
| 2907 | * mbedtls_ssl_setup() takes the provided list |
| 2908 | * and translates it to a list of IANA TLS group identifiers, |
| 2909 | * stored in ssl->handshake->group_list. |
| 2910 | * |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2911 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2912 | void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf, |
| 2913 | const mbedtls_ecp_group_id *curve_list) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2914 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2915 | conf->curve_list = curve_list; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2916 | conf->group_list = NULL; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2917 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2918 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2919 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2920 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2921 | /* |
| 2922 | * Set the allowed groups |
| 2923 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2924 | void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf, |
| 2925 | const uint16_t *group_list) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2926 | { |
| 2927 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2928 | conf->curve_list = NULL; |
| 2929 | #endif |
| 2930 | conf->group_list = group_list; |
| 2931 | } |
| 2932 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2933 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2934 | int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2935 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2936 | /* Initialize to suppress unnecessary compiler warning */ |
| 2937 | size_t hostname_len = 0; |
| 2938 | |
| 2939 | /* Check if new hostname is valid before |
| 2940 | * making any change to current one */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2941 | if (hostname != NULL) { |
| 2942 | hostname_len = strlen(hostname); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2943 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2944 | if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) { |
| 2945 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2946 | } |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2947 | } |
| 2948 | |
| 2949 | /* Now it's clear that we will overwrite the old hostname, |
| 2950 | * so we can free it safely */ |
| 2951 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2952 | if (ssl->hostname != NULL) { |
| 2953 | mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname)); |
| 2954 | mbedtls_free(ssl->hostname); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2955 | } |
| 2956 | |
| 2957 | /* Passing NULL as hostname shall clear the old one */ |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 2958 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2959 | if (hostname == NULL) { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2960 | ssl->hostname = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2961 | } else { |
| 2962 | ssl->hostname = mbedtls_calloc(1, hostname_len + 1); |
| 2963 | if (ssl->hostname == NULL) { |
| 2964 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2965 | } |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2966 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2967 | memcpy(ssl->hostname, hostname, hostname_len); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2968 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2969 | ssl->hostname[hostname_len] = '\0'; |
| 2970 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2971 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2972 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2973 | } |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 2974 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2975 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2976 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2977 | void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf, |
| 2978 | int (*f_sni)(void *, mbedtls_ssl_context *, |
| 2979 | const unsigned char *, size_t), |
| 2980 | void *p_sni) |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2981 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2982 | conf->f_sni = f_sni; |
| 2983 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2984 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2985 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2986 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2987 | #if defined(MBEDTLS_SSL_ALPN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2988 | 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] | 2989 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2990 | size_t cur_len, tot_len; |
| 2991 | const char **p; |
| 2992 | |
| 2993 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 2994 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 2995 | * MUST NOT be truncated." |
| 2996 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2997 | */ |
| 2998 | tot_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2999 | for (p = protos; *p != NULL; p++) { |
| 3000 | cur_len = strlen(*p); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 3001 | tot_len += cur_len; |
| 3002 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3003 | if ((cur_len == 0) || |
| 3004 | (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) || |
| 3005 | (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) { |
| 3006 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3007 | } |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 3008 | } |
| 3009 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3010 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 3011 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3012 | return 0; |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 3013 | } |
| 3014 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3015 | 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] | 3016 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3017 | return ssl->alpn_chosen; |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 3018 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3019 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 3020 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3021 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3022 | void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf, |
| 3023 | int support_mki_value) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 3024 | { |
| 3025 | conf->dtls_srtp_mki_support = support_mki_value; |
| 3026 | } |
| 3027 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3028 | int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl, |
| 3029 | unsigned char *mki_value, |
| 3030 | uint16_t mki_len) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 3031 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3032 | if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) { |
| 3033 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 3034 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 3035 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3036 | if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) { |
| 3037 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 3038 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 3039 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3040 | memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len); |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 3041 | ssl->dtls_srtp_info.mki_len = mki_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3042 | return 0; |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 3043 | } |
| 3044 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3045 | int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf, |
| 3046 | const mbedtls_ssl_srtp_profile *profiles) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3047 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 3048 | const mbedtls_ssl_srtp_profile *p; |
| 3049 | size_t list_size = 0; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3050 | |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 3051 | /* check the profiles list: all entry must be valid, |
| 3052 | * 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] | 3053 | for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET && |
| 3054 | list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH; |
| 3055 | p++) { |
| 3056 | if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) { |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 3057 | list_size++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3058 | } else { |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 3059 | /* unsupported value, stop parsing and set the size to an error value */ |
| 3060 | list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3061 | } |
| 3062 | } |
| 3063 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3064 | if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) { |
| 3065 | conf->dtls_srtp_profile_list = NULL; |
| 3066 | conf->dtls_srtp_profile_list_len = 0; |
| 3067 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 3068 | } |
| 3069 | |
Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 3070 | conf->dtls_srtp_profile_list = profiles; |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 3071 | conf->dtls_srtp_profile_list_len = list_size; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3072 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3073 | return 0; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3074 | } |
| 3075 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3076 | void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl, |
| 3077 | mbedtls_dtls_srtp_info *dtls_srtp_info) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3078 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 3079 | dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile; |
| 3080 | /* do not copy the mki value if there is no chosen profile */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3081 | if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 3082 | dtls_srtp_info->mki_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3083 | } else { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 3084 | dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3085 | memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value, |
| 3086 | ssl->dtls_srtp_info.mki_len); |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 3087 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3088 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3089 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 3090 | |
Aditya Patwardhan | 3096f33 | 2022-07-26 14:31:46 +0530 | [diff] [blame] | 3091 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3092 | 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] | 3093 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 3094 | conf->max_tls_version = (major << 8) | minor; |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 3095 | } |
| 3096 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3097 | 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] | 3098 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 3099 | conf->min_tls_version = (major << 8) | minor; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 3100 | } |
Aditya Patwardhan | 3096f33 | 2022-07-26 14:31:46 +0530 | [diff] [blame] | 3101 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 3102 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3103 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3104 | void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf, |
| 3105 | char cert_req_ca_list) |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3106 | { |
| 3107 | conf->cert_req_ca_list = cert_req_ca_list; |
| 3108 | } |
| 3109 | #endif |
| 3110 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3111 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3112 | 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] | 3113 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3114 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 3115 | } |
| 3116 | #endif |
| 3117 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3118 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3119 | 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] | 3120 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3121 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 3122 | } |
| 3123 | #endif |
| 3124 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3125 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3126 | 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] | 3127 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3128 | if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
| 3129 | ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) { |
| 3130 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 3131 | } |
| 3132 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 3133 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 3134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3135 | return 0; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 3136 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3137 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 3138 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3139 | 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] | 3140 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3141 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3142 | } |
| 3143 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3144 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3145 | 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] | 3146 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3147 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 3148 | } |
| 3149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3150 | 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] | 3151 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3152 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 3153 | } |
| 3154 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3155 | void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf, |
| 3156 | const unsigned char period[8]) |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 3157 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3158 | memcpy(conf->renego_period, period, 8); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 3159 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3160 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3161 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3162 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3163 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3164 | 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] | 3165 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 3166 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 3167 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3168 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 3169 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3170 | #if defined(MBEDTLS_SSL_SRV_C) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 3171 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 3172 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3173 | void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf, |
| 3174 | uint16_t num_tickets) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 3175 | { |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 3176 | conf->new_session_tickets_count = num_tickets; |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 3177 | } |
| 3178 | #endif |
| 3179 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3180 | void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf, |
| 3181 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 3182 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 3183 | void *p_ticket) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 3184 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 3185 | conf->f_ticket_write = f_ticket_write; |
| 3186 | conf->f_ticket_parse = f_ticket_parse; |
| 3187 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 3188 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3189 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3190 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 3191 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3192 | void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl, |
| 3193 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 3194 | void *p_export_keys) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 3195 | { |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 3196 | ssl->f_export_keys = f_export_keys; |
| 3197 | ssl->p_export_keys = p_export_keys; |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 3198 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 3199 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3200 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3201 | void mbedtls_ssl_conf_async_private_cb( |
| 3202 | mbedtls_ssl_config *conf, |
| 3203 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 3204 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 3205 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 3206 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3207 | void *async_config_data) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3208 | { |
| 3209 | conf->f_async_sign_start = f_async_sign; |
| 3210 | conf->f_async_decrypt_start = f_async_decrypt; |
| 3211 | conf->f_async_resume = f_async_resume; |
| 3212 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3213 | conf->p_async_config_data = async_config_data; |
| 3214 | } |
| 3215 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3216 | 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] | 3217 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3218 | return conf->p_async_config_data; |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3219 | } |
| 3220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3221 | void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3222 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3223 | if (ssl->handshake == NULL) { |
| 3224 | return NULL; |
| 3225 | } else { |
| 3226 | return ssl->handshake->user_async_ctx; |
| 3227 | } |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3228 | } |
| 3229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3230 | void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl, |
| 3231 | void *ctx) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3232 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3233 | if (ssl->handshake != NULL) { |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3234 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3235 | } |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3236 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3237 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3238 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3239 | /* |
| 3240 | * SSL get accessors |
| 3241 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3242 | uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3243 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3244 | if (ssl->session != NULL) { |
| 3245 | return ssl->session->verify_result; |
| 3246 | } |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 3247 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3248 | if (ssl->session_negotiate != NULL) { |
| 3249 | return ssl->session_negotiate->verify_result; |
| 3250 | } |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 3251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3252 | return 0xFFFFFFFF; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3253 | } |
| 3254 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3255 | 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] | 3256 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3257 | if (ssl == NULL || ssl->session == NULL) { |
| 3258 | return 0; |
| 3259 | } |
Glenn Strauss | 8f52690 | 2022-01-13 00:04:49 -0500 | [diff] [blame] | 3260 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3261 | return ssl->session->ciphersuite; |
Glenn Strauss | 8f52690 | 2022-01-13 00:04:49 -0500 | [diff] [blame] | 3262 | } |
| 3263 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3264 | const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 3265 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3266 | if (ssl == NULL || ssl->session == NULL) { |
| 3267 | return NULL; |
| 3268 | } |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 3269 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3270 | return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 3271 | } |
| 3272 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3273 | const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3274 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3275 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3276 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 3277 | switch (ssl->tls_version) { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3278 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3279 | return "DTLSv1.2"; |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 3280 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3281 | return "unknown (DTLS)"; |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 3282 | } |
| 3283 | } |
| 3284 | #endif |
| 3285 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3286 | switch (ssl->tls_version) { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3287 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3288 | return "TLSv1.2"; |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3289 | case MBEDTLS_SSL_VERSION_TLS1_3: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3290 | return "TLSv1.3"; |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3291 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3292 | return "unknown"; |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3293 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3294 | } |
| 3295 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 3296 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3297 | 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] | 3298 | { |
David Horstmann | 95d516f | 2021-05-04 18:36:56 +0100 | [diff] [blame] | 3299 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3300 | size_t read_mfl; |
| 3301 | |
Jerry Yu | ddda050 | 2022-12-01 19:43:12 +0800 | [diff] [blame] | 3302 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3303 | /* 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] | 3304 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 3305 | ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) { |
| 3306 | return ssl_mfl_code_to_length(ssl->conf->mfl_code); |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3307 | } |
Jerry Yu | ddda050 | 2022-12-01 19:43:12 +0800 | [diff] [blame] | 3308 | #endif |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3309 | |
| 3310 | /* Check if a smaller max length was negotiated */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3311 | if (ssl->session_out != NULL) { |
| 3312 | read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code); |
| 3313 | if (read_mfl < max_len) { |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3314 | max_len = read_mfl; |
| 3315 | } |
| 3316 | } |
| 3317 | |
Jerry Yu | ddda050 | 2022-12-01 19:43:12 +0800 | [diff] [blame] | 3318 | /* During a handshake, use the value being negotiated */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3319 | if (ssl->session_negotiate != NULL) { |
| 3320 | read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code); |
| 3321 | if (read_mfl < max_len) { |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3322 | max_len = read_mfl; |
| 3323 | } |
| 3324 | } |
| 3325 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3326 | return max_len; |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3327 | } |
| 3328 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3329 | 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] | 3330 | { |
| 3331 | size_t max_len; |
| 3332 | |
| 3333 | /* |
| 3334 | * Assume mfl_code is correct since it was checked when set |
| 3335 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3336 | 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] | 3337 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 3338 | /* Check if a smaller max length was negotiated */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3339 | if (ssl->session_out != NULL && |
| 3340 | ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) { |
| 3341 | 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] | 3342 | } |
| 3343 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 3344 | /* During a handshake, use the value being negotiated */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3345 | if (ssl->session_negotiate != NULL && |
| 3346 | ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) { |
| 3347 | 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] | 3348 | } |
| 3349 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3350 | return max_len; |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 3351 | } |
| 3352 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 3353 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3354 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3355 | 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] | 3356 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 3357 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3358 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 3359 | (ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 3360 | ssl->state == MBEDTLS_SSL_SERVER_HELLO)) { |
| 3361 | return 0; |
| 3362 | } |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 3363 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3364 | if (ssl->handshake == NULL || ssl->handshake->mtu == 0) { |
| 3365 | return ssl->mtu; |
| 3366 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3367 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3368 | if (ssl->mtu == 0) { |
| 3369 | return ssl->handshake->mtu; |
| 3370 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3371 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3372 | return ssl->mtu < ssl->handshake->mtu ? |
| 3373 | ssl->mtu : ssl->handshake->mtu; |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3374 | } |
| 3375 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3377 | 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] | 3378 | { |
| 3379 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 3380 | |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 3381 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 3382 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3383 | (void) ssl; |
| 3384 | #endif |
| 3385 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3386 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3387 | 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] | 3388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3389 | if (max_len > mfl) { |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3390 | max_len = mfl; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3391 | } |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3392 | #endif |
| 3393 | |
| 3394 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3395 | if (mbedtls_ssl_get_current_mtu(ssl) != 0) { |
| 3396 | const size_t mtu = mbedtls_ssl_get_current_mtu(ssl); |
| 3397 | const int ret = mbedtls_ssl_get_record_expansion(ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3398 | const size_t overhead = (size_t) ret; |
| 3399 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3400 | if (ret < 0) { |
| 3401 | return ret; |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3402 | } |
| 3403 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3404 | if (mtu <= overhead) { |
| 3405 | MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion")); |
| 3406 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 3407 | } |
| 3408 | |
| 3409 | if (max_len > mtu - overhead) { |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3410 | max_len = mtu - overhead; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3411 | } |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3412 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3413 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3414 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 3415 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 3416 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3417 | ((void) ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3418 | #endif |
| 3419 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3420 | return (int) max_len; |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3421 | } |
| 3422 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3423 | 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] | 3424 | { |
| 3425 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
| 3426 | |
| 3427 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 3428 | (void) ssl; |
| 3429 | #endif |
| 3430 | |
| 3431 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3432 | const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl); |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 3433 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3434 | if (max_len > mfl) { |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 3435 | max_len = mfl; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3436 | } |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 3437 | #endif |
| 3438 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3439 | return (int) max_len; |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 3440 | } |
| 3441 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3442 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3443 | 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] | 3444 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3445 | if (ssl == NULL || ssl->session == NULL) { |
| 3446 | return NULL; |
| 3447 | } |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3448 | |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 3449 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3450 | return ssl->session->peer_cert; |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 3451 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3452 | return NULL; |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 3453 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3454 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3455 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3456 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3457 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3458 | int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl, |
| 3459 | mbedtls_ssl_session *dst) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3460 | { |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3461 | int ret; |
| 3462 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3463 | if (ssl == NULL || |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3464 | dst == NULL || |
| 3465 | ssl->session == NULL || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3466 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) { |
| 3467 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3468 | } |
| 3469 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3470 | /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer |
| 3471 | * idempotent: Each session can only be exported once. |
| 3472 | * |
| 3473 | * (This is in preparation for TLS 1.3 support where we will |
| 3474 | * need the ability to export multiple sessions (aka tickets), |
| 3475 | * which will be achieved by calling mbedtls_ssl_get_session() |
| 3476 | * multiple times until it fails.) |
| 3477 | * |
| 3478 | * Check whether we have already exported the current session, |
| 3479 | * and fail if so. |
| 3480 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3481 | if (ssl->session->exported == 1) { |
| 3482 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 3483 | } |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3484 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3485 | ret = mbedtls_ssl_session_copy(dst, ssl->session); |
| 3486 | if (ret != 0) { |
| 3487 | return ret; |
| 3488 | } |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3489 | |
| 3490 | /* Remember that we've exported the session. */ |
| 3491 | ssl->session->exported = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3492 | return 0; |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3493 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3494 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3495 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3496 | /* |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 3497 | * Define ticket header determining Mbed TLS version |
| 3498 | * and structure of the ticket. |
| 3499 | */ |
| 3500 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3501 | /* |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3502 | * Define bitflag determining compile-time settings influencing |
| 3503 | * structure of serialized SSL sessions. |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3504 | */ |
| 3505 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3506 | #if defined(MBEDTLS_HAVE_TIME) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3507 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3508 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3509 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3510 | #endif /* MBEDTLS_HAVE_TIME */ |
| 3511 | |
| 3512 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3513 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3514 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3515 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3516 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 3517 | |
| 3518 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3519 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3520 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3521 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3522 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ |
| 3523 | |
| 3524 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3525 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3526 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3527 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3528 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 3529 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3530 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3531 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3532 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3533 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3534 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 3535 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3536 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 3537 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 |
| 3538 | #else |
| 3539 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 |
| 3540 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 3541 | |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3542 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0 |
| 3543 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1 |
| 3544 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 |
| 3545 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3 |
Hanno Becker | 37bdbe6 | 2021-08-01 05:38:58 +0100 | [diff] [blame] | 3546 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4 |
| 3547 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5 |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3548 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3549 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3550 | ((uint16_t) ( \ |
| 3551 | (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \ |
| 3552 | (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \ |
| 3553 | (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \ |
| 3554 | SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \ |
| 3555 | (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \ |
| 3556 | (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \ |
| 3557 | (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT))) |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3558 | |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 3559 | static unsigned char ssl_serialized_session_header[] = { |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3560 | MBEDTLS_VERSION_MAJOR, |
| 3561 | MBEDTLS_VERSION_MINOR, |
| 3562 | MBEDTLS_VERSION_PATCH, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3563 | MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG), |
| 3564 | MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG), |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 3565 | }; |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 3566 | |
| 3567 | /* |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3568 | * Serialize a session in the following format: |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 3569 | * (in the presentation language of TLS, RFC 8446 section 3) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3570 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3571 | * struct { |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 3572 | * |
Hanno Becker | dce5097 | 2021-08-01 05:39:23 +0100 | [diff] [blame] | 3573 | * opaque mbedtls_version[3]; // library version: major, minor, patch |
| 3574 | * opaque session_format[2]; // library-version specific 16-bit field |
| 3575 | * // determining the format of the remaining |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3576 | * // serialized data. |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 3577 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3578 | * Note: When updating the format, remember to keep |
| 3579 | * these version+format bytes. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3580 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3581 | * // In this version, `session_format` determines |
| 3582 | * // the setting of those compile-time |
| 3583 | * // configuration options which influence |
| 3584 | * // the structure of mbedtls_ssl_session. |
| 3585 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3586 | * uint8_t minor_ver; // Protocol minor version. Possible values: |
Jerry Yu | 0c2a738 | 2022-12-06 13:27:25 +0800 | [diff] [blame] | 3587 | * // - TLS 1.2 (0x0303) |
| 3588 | * // - TLS 1.3 (0x0304) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3589 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3590 | * select (serialized_session.tls_version) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3591 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3592 | * case MBEDTLS_SSL_VERSION_TLS1_2: |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3593 | * serialized_session_tls12 data; |
Jerry Yu | 0c2a738 | 2022-12-06 13:27:25 +0800 | [diff] [blame] | 3594 | * case MBEDTLS_SSL_VERSION_TLS1_3: |
Jerry Yu | ddda050 | 2022-12-01 19:43:12 +0800 | [diff] [blame] | 3595 | * serialized_session_tls13 data; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3596 | * |
| 3597 | * }; |
| 3598 | * |
| 3599 | * } serialized_session; |
| 3600 | * |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3601 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3602 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3603 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3604 | static int ssl_session_save(const mbedtls_ssl_session *session, |
| 3605 | unsigned char omit_header, |
| 3606 | unsigned char *buf, |
| 3607 | size_t buf_len, |
| 3608 | size_t *olen) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3609 | { |
| 3610 | unsigned char *p = buf; |
| 3611 | size_t used = 0; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3612 | size_t remaining_len; |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3613 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3614 | size_t out_len; |
| 3615 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3616 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3617 | if (session == NULL) { |
| 3618 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 3619 | } |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3620 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3621 | if (!omit_header) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3622 | /* |
| 3623 | * Add Mbed TLS version identifier |
| 3624 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3625 | used += sizeof(ssl_serialized_session_header); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3626 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3627 | if (used <= buf_len) { |
| 3628 | memcpy(p, ssl_serialized_session_header, |
| 3629 | sizeof(ssl_serialized_session_header)); |
| 3630 | p += sizeof(ssl_serialized_session_header); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3631 | } |
| 3632 | } |
| 3633 | |
| 3634 | /* |
| 3635 | * TLS version identifier |
| 3636 | */ |
| 3637 | used += 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3638 | if (used <= buf_len) { |
| 3639 | *p++ = MBEDTLS_BYTE_0(session->tls_version); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3640 | } |
| 3641 | |
| 3642 | /* Forward to version-specific serialization routine. */ |
Jerry Yu | fca4d57 | 2022-07-21 10:37:48 +0800 | [diff] [blame] | 3643 | remaining_len = (buf_len >= used) ? buf_len - used : 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3644 | switch (session->tls_version) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3645 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3646 | case MBEDTLS_SSL_VERSION_TLS1_2: |
| 3647 | used += ssl_tls12_session_save(session, p, remaining_len); |
| 3648 | break; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3649 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3650 | |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3651 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3652 | case MBEDTLS_SSL_VERSION_TLS1_3: |
| 3653 | ret = ssl_tls13_session_save(session, p, remaining_len, &out_len); |
| 3654 | if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) { |
| 3655 | return ret; |
| 3656 | } |
| 3657 | used += out_len; |
| 3658 | break; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3659 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 3660 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3661 | default: |
| 3662 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3663 | } |
| 3664 | |
| 3665 | *olen = used; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3666 | if (used > buf_len) { |
| 3667 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 3668 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3669 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3670 | return 0; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3671 | } |
| 3672 | |
| 3673 | /* |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3674 | * Public wrapper for ssl_session_save() |
| 3675 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3676 | int mbedtls_ssl_session_save(const mbedtls_ssl_session *session, |
| 3677 | unsigned char *buf, |
| 3678 | size_t buf_len, |
| 3679 | size_t *olen) |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3680 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3681 | return ssl_session_save(session, 0, buf, buf_len, olen); |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3682 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3683 | |
Jerry Yu | f1b23ca | 2022-02-18 11:48:47 +0800 | [diff] [blame] | 3684 | /* |
| 3685 | * Deserialize session, see mbedtls_ssl_session_save() for format. |
| 3686 | * |
| 3687 | * This internal version is wrapped by a public function that cleans up in |
| 3688 | * case of error, and has an extra option omit_header. |
| 3689 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3690 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3691 | static int ssl_session_load(mbedtls_ssl_session *session, |
| 3692 | unsigned char omit_header, |
| 3693 | const unsigned char *buf, |
| 3694 | size_t len) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3695 | { |
| 3696 | const unsigned char *p = buf; |
| 3697 | const unsigned char * const end = buf + len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3698 | size_t remaining_len; |
| 3699 | |
| 3700 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3701 | if (session == NULL) { |
| 3702 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 3703 | } |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3704 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3705 | if (!omit_header) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3706 | /* |
| 3707 | * Check Mbed TLS version identifier |
| 3708 | */ |
| 3709 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3710 | if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) { |
| 3711 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3712 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3713 | |
| 3714 | if (memcmp(p, ssl_serialized_session_header, |
| 3715 | sizeof(ssl_serialized_session_header)) != 0) { |
| 3716 | return MBEDTLS_ERR_SSL_VERSION_MISMATCH; |
| 3717 | } |
| 3718 | p += sizeof(ssl_serialized_session_header); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3719 | } |
| 3720 | |
| 3721 | /* |
| 3722 | * TLS version identifier |
| 3723 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3724 | if (1 > (size_t) (end - p)) { |
| 3725 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3726 | } |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3727 | session->tls_version = 0x0300 | *p++; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3728 | |
| 3729 | /* Dispatch according to TLS version. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3730 | remaining_len = (end - p); |
| 3731 | switch (session->tls_version) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3732 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3733 | case MBEDTLS_SSL_VERSION_TLS1_2: |
| 3734 | return ssl_tls12_session_load(session, p, remaining_len); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3735 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3736 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3737 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3738 | case MBEDTLS_SSL_VERSION_TLS1_3: |
| 3739 | return ssl_tls13_session_load(session, p, remaining_len); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3740 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 3741 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3742 | default: |
| 3743 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3744 | } |
| 3745 | } |
| 3746 | |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3747 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 3748 | * Deserialize session: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3749 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3750 | int mbedtls_ssl_session_load(mbedtls_ssl_session *session, |
| 3751 | const unsigned char *buf, |
| 3752 | size_t len) |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3753 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3754 | int ret = ssl_session_load(session, 0, buf, len); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3755 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3756 | if (ret != 0) { |
| 3757 | mbedtls_ssl_session_free(session); |
| 3758 | } |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3759 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3760 | return ret; |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3761 | } |
| 3762 | |
| 3763 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3764 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3765 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3766 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3767 | static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl) |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3768 | { |
| 3769 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3770 | |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3771 | /* |
| 3772 | * 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] | 3773 | * that were written into the output buffer by the previous handshake step, |
| 3774 | * if the write to the network callback returned with the |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3775 | * #MBEDTLS_ERR_SSL_WANT_WRITE error code. |
| 3776 | * We proceed to the next handshake step only when all data from the |
| 3777 | * previous one have been sent to the peer, thus we make sure that this is |
| 3778 | * the case here by calling `mbedtls_ssl_flush_output()`. The function may |
| 3779 | * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case |
| 3780 | * we have to wait before to go ahead. |
| 3781 | * In the case of TLS 1.3, handshake step handlers do not send data to the |
| 3782 | * peer. Data are only sent here and through |
| 3783 | * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an |
Andrzej Kurek | 5c65c57 | 2022-04-13 14:28:52 -0400 | [diff] [blame] | 3784 | * alert occurred. |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3785 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3786 | if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) { |
| 3787 | return ret; |
| 3788 | } |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3789 | |
| 3790 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3791 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3792 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) { |
| 3793 | if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) { |
| 3794 | return ret; |
| 3795 | } |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3796 | } |
| 3797 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3798 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3799 | return ret; |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3800 | } |
| 3801 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3802 | int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3803 | { |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3804 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3805 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3806 | if (ssl == NULL || |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3807 | ssl->conf == NULL || |
| 3808 | ssl->handshake == NULL || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3809 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 3810 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3811 | } |
| 3812 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3813 | ret = ssl_prepare_handshake_step(ssl); |
| 3814 | if (ret != 0) { |
| 3815 | return ret; |
| 3816 | } |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3817 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3818 | ret = mbedtls_ssl_handle_pending_alert(ssl); |
| 3819 | if (ret != 0) { |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3820 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3821 | } |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3822 | |
Tom Cosgrove | 2fdc7b3 | 2022-09-21 12:33:17 +0100 | [diff] [blame] | 3823 | /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or |
| 3824 | * MBEDTLS_SSL_IS_SERVER, this is the return code we give */ |
| 3825 | ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3826 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3827 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3828 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 3829 | MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s", |
| 3830 | mbedtls_ssl_states_str(ssl->state))); |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3831 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3832 | switch (ssl->state) { |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3833 | case MBEDTLS_SSL_HELLO_REQUEST: |
| 3834 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; |
Tom Cosgrove | 87d9c6c | 2022-09-22 09:27:56 +0100 | [diff] [blame] | 3835 | ret = 0; |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3836 | break; |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3837 | |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3838 | case MBEDTLS_SSL_CLIENT_HELLO: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3839 | ret = mbedtls_ssl_write_client_hello(ssl); |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3840 | break; |
| 3841 | |
| 3842 | default: |
| 3843 | #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] | 3844 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
| 3845 | ret = mbedtls_ssl_tls13_handshake_client_step(ssl); |
| 3846 | } else { |
| 3847 | ret = mbedtls_ssl_handshake_client_step(ssl); |
| 3848 | } |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3849 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3850 | ret = mbedtls_ssl_handshake_client_step(ssl); |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3851 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3852 | ret = mbedtls_ssl_tls13_handshake_client_step(ssl); |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3853 | #endif |
| 3854 | } |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3855 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3856 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3857 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3858 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3859 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3860 | if (mbedtls_ssl_conf_is_tls13_only(ssl->conf)) { |
| 3861 | ret = mbedtls_ssl_tls13_handshake_server_step(ssl); |
| 3862 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3863 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3864 | |
| 3865 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3866 | if (mbedtls_ssl_conf_is_tls12_only(ssl->conf)) { |
| 3867 | ret = mbedtls_ssl_handshake_server_step(ssl); |
| 3868 | } |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3869 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3870 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3871 | #endif |
| 3872 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3873 | if (ret != 0) { |
Jerry Yu | bbd5a3f | 2021-09-18 20:50:22 +0800 | [diff] [blame] | 3874 | /* handshake_step return error. And it is same |
| 3875 | * with alert_reason. |
| 3876 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3877 | if (ssl->send_alert) { |
| 3878 | ret = mbedtls_ssl_handle_pending_alert(ssl); |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3879 | goto cleanup; |
| 3880 | } |
| 3881 | } |
| 3882 | |
| 3883 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3884 | return ret; |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3885 | } |
| 3886 | |
| 3887 | /* |
| 3888 | * Perform the SSL handshake |
| 3889 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3890 | int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3891 | { |
| 3892 | int ret = 0; |
| 3893 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3894 | /* Sanity checks */ |
| 3895 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3896 | if (ssl == NULL || ssl->conf == NULL) { |
| 3897 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3898 | } |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3899 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3900 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3901 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3902 | (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) { |
| 3903 | MBEDTLS_SSL_DEBUG_MSG(1, ("You must use " |
| 3904 | "mbedtls_ssl_set_timer_cb() for DTLS")); |
| 3905 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3906 | } |
| 3907 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3908 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3909 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake")); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3910 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3911 | /* Main handshake loop */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3912 | while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 3913 | ret = mbedtls_ssl_handshake_step(ssl); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3914 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3915 | if (ret != 0) { |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3916 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3917 | } |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3918 | } |
| 3919 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3920 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3921 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3922 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3923 | } |
| 3924 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3925 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3926 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3927 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3928 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3929 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3930 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3931 | static int ssl_write_hello_request(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3932 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3933 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3934 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3935 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request")); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3936 | |
| 3937 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3938 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3939 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3940 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3941 | if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { |
| 3942 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret); |
| 3943 | return ret; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3944 | } |
| 3945 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3946 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request")); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3947 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3948 | return 0; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3949 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3950 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3951 | |
| 3952 | /* |
| 3953 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3954 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 3955 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 3956 | * - 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] | 3957 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3958 | * 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] | 3959 | * 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] | 3960 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3961 | int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3962 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3963 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3964 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3965 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate")); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3966 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3967 | if ((ret = ssl_handshake_init(ssl)) != 0) { |
| 3968 | return ret; |
| 3969 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3970 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3971 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 3972 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3973 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3974 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3975 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) { |
| 3976 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3977 | ssl->handshake->out_msg_seq = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3978 | } else { |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3979 | ssl->handshake->in_msg_seq = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3980 | } |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3981 | } |
| 3982 | #endif |
| 3983 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3984 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 3985 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3986 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3987 | if ((ret = mbedtls_ssl_handshake(ssl)) != 0) { |
| 3988 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret); |
| 3989 | return ret; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3990 | } |
| 3991 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3992 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate")); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3993 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3994 | return 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3995 | } |
| 3996 | |
| 3997 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3998 | * Renegotiate current connection on client, |
| 3999 | * or request renegotiation on server |
| 4000 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4001 | int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4002 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4003 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4004 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4005 | if (ssl == NULL || ssl->conf == NULL) { |
| 4006 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4007 | } |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 4008 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4009 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4010 | /* On server, just send the request */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4011 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 4012 | if (mbedtls_ssl_is_handshake_over(ssl) == 0) { |
| 4013 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4014 | } |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4015 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4016 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 4017 | |
| 4018 | /* Did we already try/start sending HelloRequest? */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4019 | if (ssl->out_left != 0) { |
| 4020 | return mbedtls_ssl_flush_output(ssl); |
| 4021 | } |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 4022 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4023 | return ssl_write_hello_request(ssl); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4024 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4025 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4026 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4027 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4028 | /* |
| 4029 | * On client, either start the renegotiation process or, |
| 4030 | * if already in progress, continue the handshake |
| 4031 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4032 | if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) { |
| 4033 | if (mbedtls_ssl_is_handshake_over(ssl) == 0) { |
| 4034 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4035 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4036 | |
| 4037 | if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) { |
| 4038 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret); |
| 4039 | return ret; |
| 4040 | } |
| 4041 | } else { |
| 4042 | if ((ret = mbedtls_ssl_handshake(ssl)) != 0) { |
| 4043 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret); |
| 4044 | return ret; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4045 | } |
| 4046 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4047 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4048 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4049 | return ret; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4050 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4051 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4052 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4053 | void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4054 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 4055 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 4056 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4057 | if (handshake == NULL) { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4058 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4059 | } |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4060 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4061 | #if defined(MBEDTLS_ECP_C) |
| 4062 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4063 | if (ssl->handshake->group_list_heap_allocated) { |
| 4064 | mbedtls_free((void *) handshake->group_list); |
| 4065 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4066 | handshake->group_list = NULL; |
| 4067 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 4068 | #endif /* MBEDTLS_ECP_C */ |
| 4069 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4070 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4071 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4072 | if (ssl->handshake->sig_algs_heap_allocated) { |
| 4073 | mbedtls_free((void *) handshake->sig_algs); |
| 4074 | } |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4075 | handshake->sig_algs = NULL; |
| 4076 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Xiaofei Bai | c234ecf | 2022-02-08 09:59:23 +0000 | [diff] [blame] | 4077 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4078 | if (ssl->handshake->certificate_request_context) { |
| 4079 | mbedtls_free((void *) handshake->certificate_request_context); |
Xiaofei Bai | c234ecf | 2022-02-08 09:59:23 +0000 | [diff] [blame] | 4080 | } |
| 4081 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4082 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4083 | |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4084 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4085 | if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) { |
| 4086 | ssl->conf->f_async_cancel(ssl); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4087 | handshake->async_in_progress = 0; |
| 4088 | } |
| 4089 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 4090 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4091 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4092 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4093 | psa_hash_abort(&handshake->fin_sha256_psa); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4094 | #else |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 4095 | mbedtls_md_free(&handshake->fin_sha256); |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 4096 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4097 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4098 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4099 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4100 | psa_hash_abort(&handshake->fin_sha384_psa); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4101 | #else |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 4102 | mbedtls_md_free(&handshake->fin_sha384); |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 4103 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4104 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 4105 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4106 | #if defined(MBEDTLS_DHM_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4107 | mbedtls_dhm_free(&handshake->dhm_ctx); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4108 | #endif |
Neil Armstrong | f3f4641 | 2022-04-12 14:43:39 +0200 | [diff] [blame] | 4109 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4110 | mbedtls_ecdh_free(&handshake->ecdh_ctx); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 4111 | #endif |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 4112 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 4113 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 4114 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4115 | psa_pake_abort(&handshake->psa_pake_ctx); |
Valerio Setti | eb3f788 | 2022-12-08 18:42:58 +0100 | [diff] [blame] | 4116 | /* |
| 4117 | * Opaque keys are not stored in the handshake's data and it's the user |
| 4118 | * responsibility to destroy them. Clear ones, instead, are created by |
| 4119 | * the TLS library and should be destroyed at the same level |
| 4120 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4121 | if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) { |
| 4122 | psa_destroy_key(handshake->psa_pake_password); |
Valerio Setti | eb3f788 | 2022-12-08 18:42:58 +0100 | [diff] [blame] | 4123 | } |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 4124 | handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT; |
| 4125 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4126 | mbedtls_ecjpake_free(&handshake->ecjpake_ctx); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 4127 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 4128 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4129 | mbedtls_free(handshake->ecjpake_cache); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 4130 | handshake->ecjpake_cache = NULL; |
| 4131 | handshake->ecjpake_cache_len = 0; |
| 4132 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 4133 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 4134 | |
Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 4135 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 4136 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 4137 | /* explicit void pointer cast for buggy MS compiler */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4138 | mbedtls_free((void *) handshake->curves_tls_id); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 4139 | #endif |
| 4140 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 4141 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4142 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4143 | if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4144 | /* The maintenance of the external PSK key slot is the |
| 4145 | * user's responsibility. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4146 | if (ssl->handshake->psk_opaque_is_internal) { |
| 4147 | psa_destroy_key(ssl->handshake->psk_opaque); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4148 | ssl->handshake->psk_opaque_is_internal = 0; |
| 4149 | } |
| 4150 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 4151 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 4152 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4153 | if (handshake->psk != NULL) { |
| 4154 | mbedtls_platform_zeroize(handshake->psk, handshake->psk_len); |
| 4155 | mbedtls_free(handshake->psk); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 4156 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 4157 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 4158 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 4159 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4160 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 4161 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 4162 | /* |
| 4163 | * Free only the linked list wrapper, not the keys themselves |
| 4164 | * since the belong to the SNI callback |
| 4165 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4166 | ssl_key_cert_free(handshake->sni_key_cert); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4167 | #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] | 4168 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4169 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4170 | mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx); |
| 4171 | if (handshake->ecrs_peer_cert != NULL) { |
| 4172 | mbedtls_x509_crt_free(handshake->ecrs_peer_cert); |
| 4173 | mbedtls_free(handshake->ecrs_peer_cert); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 4174 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 4175 | #endif |
| 4176 | |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 4177 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 4178 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4179 | mbedtls_pk_free(&handshake->peer_pubkey); |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 4180 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4181 | |
XiaokangQian | 9b93c0d | 2022-02-09 06:02:25 +0000 | [diff] [blame] | 4182 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4183 | (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3)) |
| 4184 | mbedtls_free(handshake->cookie); |
XiaokangQian | 9b93c0d | 2022-02-09 06:02:25 +0000 | [diff] [blame] | 4185 | #endif /* MBEDTLS_SSL_CLI_C && |
| 4186 | ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */ |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 4187 | |
| 4188 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4189 | mbedtls_ssl_flight_free(handshake->flight); |
| 4190 | mbedtls_ssl_buffering_free(ssl); |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 4191 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 4192 | |
Ronald Cron | f12b81d | 2022-03-15 10:42:41 +0100 | [diff] [blame] | 4193 | #if defined(MBEDTLS_ECDH_C) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4194 | (defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)) |
| 4195 | if (handshake->ecdh_psa_privkey_is_external == 0) { |
| 4196 | psa_destroy_key(handshake->ecdh_psa_privkey); |
| 4197 | } |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 4198 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */ |
| 4199 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4200 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4201 | mbedtls_ssl_transform_free(handshake->transform_handshake); |
| 4202 | mbedtls_free(handshake->transform_handshake); |
Jerry Yu | 3d9b590 | 2022-11-04 14:07:25 +0800 | [diff] [blame] | 4203 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4204 | mbedtls_ssl_transform_free(handshake->transform_earlydata); |
| 4205 | mbedtls_free(handshake->transform_earlydata); |
Jerry Yu | 3d9b590 | 2022-11-04 14:07:25 +0800 | [diff] [blame] | 4206 | #endif |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4207 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 4208 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4209 | |
| 4210 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4211 | /* If the buffers are too big - reallocate. Because of the way Mbed TLS |
| 4212 | * processes datagrams and the fact that a datagram is allowed to have |
| 4213 | * several records in it, it is possible that the I/O buffers are not |
| 4214 | * empty at this stage */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4215 | handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl), |
| 4216 | mbedtls_ssl_get_output_buflen(ssl)); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4217 | #endif |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 4218 | |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 4219 | /* mbedtls_platform_zeroize MUST be last one in this function */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4220 | mbedtls_platform_zeroize(handshake, |
| 4221 | sizeof(mbedtls_ssl_handshake_params)); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4222 | } |
| 4223 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4224 | void mbedtls_ssl_session_free(mbedtls_ssl_session *session) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4225 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4226 | if (session == NULL) { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4227 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4228 | } |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4229 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4230 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4231 | ssl_clear_peer_cert(session); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4232 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 4233 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4234 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Xiaokang Qian | bc663a0 | 2022-10-09 11:14:39 +0000 | [diff] [blame] | 4235 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 4236 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4237 | mbedtls_free(session->hostname); |
Xiaokang Qian | 281fd1b | 2022-09-20 11:35:41 +0000 | [diff] [blame] | 4238 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4239 | mbedtls_free(session->ticket); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 4240 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 4241 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4242 | mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session)); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4243 | } |
| 4244 | |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 4245 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4246 | |
| 4247 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4248 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u |
| 4249 | #else |
| 4250 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u |
| 4251 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4252 | |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4253 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4254 | |
| 4255 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4256 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u |
| 4257 | #else |
| 4258 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u |
| 4259 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 4260 | |
| 4261 | #if defined(MBEDTLS_SSL_ALPN) |
| 4262 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u |
| 4263 | #else |
| 4264 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u |
| 4265 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4266 | |
| 4267 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0 |
| 4268 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1 |
| 4269 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2 |
| 4270 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3 |
| 4271 | |
| 4272 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4273 | ((uint32_t) ( \ |
| 4274 | (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \ |
| 4275 | SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \ |
| 4276 | (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \ |
| 4277 | SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \ |
| 4278 | (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \ |
| 4279 | SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \ |
| 4280 | (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \ |
| 4281 | 0u)) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4282 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4283 | static unsigned char ssl_serialized_context_header[] = { |
| 4284 | MBEDTLS_VERSION_MAJOR, |
| 4285 | MBEDTLS_VERSION_MINOR, |
| 4286 | MBEDTLS_VERSION_PATCH, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4287 | MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG), |
| 4288 | MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG), |
| 4289 | MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG), |
| 4290 | MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG), |
| 4291 | MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG), |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4292 | }; |
| 4293 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4294 | /* |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4295 | * Serialize a full SSL context |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 4296 | * |
| 4297 | * The format of the serialized data is: |
| 4298 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 4299 | * |
| 4300 | * // header |
| 4301 | * opaque mbedtls_version[3]; // major, minor, patch |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4302 | * opaque context_format[5]; // version-specific field determining |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 4303 | * // the format of the remaining |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4304 | * // serialized data. |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4305 | * Note: When updating the format, remember to keep these |
| 4306 | * 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] | 4307 | * |
| 4308 | * // session sub-structure |
| 4309 | * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save() |
| 4310 | * // transform sub-structure |
| 4311 | * uint8 random[64]; // ServerHello.random+ClientHello.random |
| 4312 | * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value |
| 4313 | * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use |
| 4314 | * // fields from ssl_context |
| 4315 | * uint32 badmac_seen; // DTLS: number of records with failing MAC |
| 4316 | * uint64 in_window_top; // DTLS: last validated record seq_num |
| 4317 | * uint64 in_window; // DTLS: bitmask for replay protection |
| 4318 | * uint8 disable_datagram_packing; // DTLS: only one record per datagram |
| 4319 | * uint64 cur_out_ctr; // Record layer: outgoing sequence number |
| 4320 | * uint16 mtu; // DTLS: path mtu (max outgoing fragment size) |
| 4321 | * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol |
| 4322 | * |
| 4323 | * Note that many fields of the ssl_context or sub-structures are not |
| 4324 | * serialized, as they fall in one of the following categories: |
| 4325 | * |
| 4326 | * 1. forced value (eg in_left must be 0) |
| 4327 | * 2. pointer to dynamically-allocated memory (eg session, transform) |
| 4328 | * 3. value can be re-derived from other data (eg session keys from MS) |
| 4329 | * 4. value was temporary (eg content of input buffer) |
| 4330 | * 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] | 4331 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4332 | int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl, |
| 4333 | unsigned char *buf, |
| 4334 | size_t buf_len, |
| 4335 | size_t *olen) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4336 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4337 | unsigned char *p = buf; |
| 4338 | size_t used = 0; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4339 | size_t session_len; |
| 4340 | int ret = 0; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4341 | |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 4342 | /* |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4343 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in |
| 4344 | * this function's documentation. |
| 4345 | * |
| 4346 | * These are due to assumptions/limitations in the implementation. Some of |
| 4347 | * them are likely to stay (no handshake in progress) some might go away |
| 4348 | * (only DTLS) but are currently used to simplify the implementation. |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 4349 | */ |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4350 | /* The initial handshake must be over */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4351 | if (mbedtls_ssl_is_handshake_over(ssl) == 0) { |
| 4352 | MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over")); |
| 4353 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4354 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4355 | if (ssl->handshake != NULL) { |
| 4356 | MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed")); |
| 4357 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4358 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4359 | /* Double-check that sub-structures are indeed ready */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4360 | if (ssl->transform == NULL || ssl->session == NULL) { |
| 4361 | MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready")); |
| 4362 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4363 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4364 | /* There must be no pending incoming or outgoing data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4365 | if (mbedtls_ssl_check_pending(ssl) != 0) { |
| 4366 | MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data")); |
| 4367 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4368 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4369 | if (ssl->out_left != 0) { |
| 4370 | MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data")); |
| 4371 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4372 | } |
Dave Rodgman | 556e8a3 | 2022-12-06 16:31:25 +0000 | [diff] [blame] | 4373 | /* Protocol must be DTLS, not TLS */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4374 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 4375 | MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported")); |
| 4376 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4377 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4378 | /* Version must be 1.2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4379 | if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) { |
| 4380 | MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported")); |
| 4381 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4382 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4383 | /* We must be using an AEAD ciphersuite */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4384 | if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) { |
| 4385 | MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported")); |
| 4386 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4387 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4388 | /* Renegotiation must not be enabled */ |
| 4389 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4390 | if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) { |
| 4391 | MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled")); |
| 4392 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4393 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4394 | #endif |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4395 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4396 | /* |
| 4397 | * Version and format identifier |
| 4398 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4399 | used += sizeof(ssl_serialized_context_header); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4400 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4401 | if (used <= buf_len) { |
| 4402 | memcpy(p, ssl_serialized_context_header, |
| 4403 | sizeof(ssl_serialized_context_header)); |
| 4404 | p += sizeof(ssl_serialized_context_header); |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4405 | } |
| 4406 | |
| 4407 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4408 | * Session (length + data) |
| 4409 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4410 | ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len); |
| 4411 | if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) { |
| 4412 | return ret; |
| 4413 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4414 | |
| 4415 | used += 4 + session_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4416 | if (used <= buf_len) { |
| 4417 | MBEDTLS_PUT_UINT32_BE(session_len, p, 0); |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4418 | p += 4; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4419 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4420 | ret = ssl_session_save(ssl->session, 1, |
| 4421 | p, session_len, &session_len); |
| 4422 | if (ret != 0) { |
| 4423 | return ret; |
| 4424 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4425 | |
| 4426 | p += session_len; |
| 4427 | } |
| 4428 | |
| 4429 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4430 | * Transform |
| 4431 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4432 | used += sizeof(ssl->transform->randbytes); |
| 4433 | if (used <= buf_len) { |
| 4434 | memcpy(p, ssl->transform->randbytes, |
| 4435 | sizeof(ssl->transform->randbytes)); |
| 4436 | p += sizeof(ssl->transform->randbytes); |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4437 | } |
| 4438 | |
| 4439 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4440 | used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4441 | if (used <= buf_len) { |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4442 | *p++ = ssl->transform->in_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4443 | 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] | 4444 | p += ssl->transform->in_cid_len; |
| 4445 | |
| 4446 | *p++ = ssl->transform->out_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4447 | 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] | 4448 | p += ssl->transform->out_cid_len; |
| 4449 | } |
| 4450 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4451 | |
| 4452 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4453 | * Saved fields from top-level ssl_context structure |
| 4454 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4455 | used += 4; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4456 | if (used <= buf_len) { |
| 4457 | MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0); |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4458 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4459 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4460 | |
| 4461 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4462 | used += 16; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4463 | if (used <= buf_len) { |
| 4464 | MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0); |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4465 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4466 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4467 | MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0); |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4468 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4469 | } |
| 4470 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 4471 | |
| 4472 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4473 | used += 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4474 | if (used <= buf_len) { |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4475 | *p++ = ssl->disable_datagram_packing; |
| 4476 | } |
| 4477 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4478 | |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 4479 | used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4480 | if (used <= buf_len) { |
| 4481 | memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN); |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 4482 | p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4483 | } |
| 4484 | |
| 4485 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4486 | used += 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4487 | if (used <= buf_len) { |
| 4488 | MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0); |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4489 | p += 2; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4490 | } |
| 4491 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4492 | |
| 4493 | #if defined(MBEDTLS_SSL_ALPN) |
| 4494 | { |
| 4495 | const uint8_t alpn_len = ssl->alpn_chosen |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4496 | ? (uint8_t) strlen(ssl->alpn_chosen) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4497 | : 0; |
| 4498 | |
| 4499 | used += 1 + alpn_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4500 | if (used <= buf_len) { |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4501 | *p++ = alpn_len; |
| 4502 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4503 | if (ssl->alpn_chosen != NULL) { |
| 4504 | memcpy(p, ssl->alpn_chosen, alpn_len); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4505 | p += alpn_len; |
| 4506 | } |
| 4507 | } |
| 4508 | } |
| 4509 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4510 | |
| 4511 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4512 | * Done |
| 4513 | */ |
| 4514 | *olen = used; |
| 4515 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4516 | if (used > buf_len) { |
| 4517 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 4518 | } |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4519 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4520 | MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4521 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4522 | return mbedtls_ssl_session_reset_int(ssl, 0); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4523 | } |
| 4524 | |
| 4525 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 4526 | * Deserialize context, see mbedtls_ssl_context_save() for format. |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4527 | * |
| 4528 | * This internal version is wrapped by a public function that cleans up in |
| 4529 | * case of error. |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4530 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4531 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4532 | static int ssl_context_load(mbedtls_ssl_context *ssl, |
| 4533 | const unsigned char *buf, |
| 4534 | size_t len) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4535 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4536 | const unsigned char *p = buf; |
| 4537 | const unsigned char * const end = buf + len; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4538 | size_t session_len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4539 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4540 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 4541 | tls_prf_fn prf_func = NULL; |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4542 | #endif |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4543 | |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4544 | /* |
| 4545 | * The context should have been freshly setup or reset. |
| 4546 | * Give the user an error in case of obvious misuse. |
Manuel Pégourié-Gonnard | 4ca930f | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 4547 | * (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] | 4548 | * renegotiating, or if the user mistakenly loaded a session first.) |
| 4549 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4550 | if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST || |
| 4551 | ssl->session != NULL) { |
| 4552 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4553 | } |
| 4554 | |
| 4555 | /* |
| 4556 | * We can't check that the config matches the initial one, but we can at |
| 4557 | * least check it matches the requirements for serializing. |
| 4558 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4559 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 4560 | ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 || |
| 4561 | ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4562 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 4563 | ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4564 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4565 | 0) { |
| 4566 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4567 | } |
| 4568 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4569 | MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4570 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4571 | /* |
| 4572 | * Check version identifier |
| 4573 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4574 | if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) { |
| 4575 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4576 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4577 | |
| 4578 | if (memcmp(p, ssl_serialized_context_header, |
| 4579 | sizeof(ssl_serialized_context_header)) != 0) { |
| 4580 | return MBEDTLS_ERR_SSL_VERSION_MISMATCH; |
| 4581 | } |
| 4582 | p += sizeof(ssl_serialized_context_header); |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4583 | |
| 4584 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4585 | * Session |
| 4586 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4587 | if ((size_t) (end - p) < 4) { |
| 4588 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4589 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4590 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4591 | session_len = ((size_t) p[0] << 24) | |
| 4592 | ((size_t) p[1] << 16) | |
| 4593 | ((size_t) p[2] << 8) | |
| 4594 | ((size_t) p[3]); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4595 | p += 4; |
| 4596 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4597 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4598 | * 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] | 4599 | ssl->session = ssl->session_negotiate; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4600 | ssl->session_in = ssl->session; |
| 4601 | ssl->session_out = ssl->session; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4602 | ssl->session_negotiate = NULL; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4603 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4604 | if ((size_t) (end - p) < session_len) { |
| 4605 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4606 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4607 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4608 | ret = ssl_session_load(ssl->session, 1, p, session_len); |
| 4609 | if (ret != 0) { |
| 4610 | mbedtls_ssl_session_free(ssl->session); |
| 4611 | return ret; |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4612 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4613 | |
| 4614 | p += session_len; |
| 4615 | |
| 4616 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4617 | * Transform |
| 4618 | */ |
| 4619 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4620 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4621 | * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */ |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 4622 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4623 | ssl->transform = ssl->transform_negotiate; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4624 | ssl->transform_in = ssl->transform; |
| 4625 | ssl->transform_out = ssl->transform; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4626 | ssl->transform_negotiate = NULL; |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 4627 | #endif |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4628 | |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4629 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4630 | prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite); |
| 4631 | if (prf_func == NULL) { |
| 4632 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4633 | } |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 4634 | |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4635 | /* Read random bytes and populate structure */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4636 | if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) { |
| 4637 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4638 | } |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4639 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4640 | ret = ssl_tls12_populate_transform(ssl->transform, |
| 4641 | ssl->session->ciphersuite, |
| 4642 | ssl->session->master, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 4643 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4644 | ssl->session->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 4645 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4646 | prf_func, |
| 4647 | p, /* currently pointing to randbytes */ |
| 4648 | MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */ |
| 4649 | ssl->conf->endpoint, |
| 4650 | ssl); |
| 4651 | if (ret != 0) { |
| 4652 | return ret; |
| 4653 | } |
Jerry Yu | 840fbb2 | 2022-02-17 14:59:29 +0800 | [diff] [blame] | 4654 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4655 | p += sizeof(ssl->transform->randbytes); |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4656 | |
| 4657 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4658 | /* Read connection IDs and store them */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4659 | if ((size_t) (end - p) < 1) { |
| 4660 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4661 | } |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4662 | |
| 4663 | ssl->transform->in_cid_len = *p++; |
| 4664 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4665 | if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) { |
| 4666 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4667 | } |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4668 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4669 | 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] | 4670 | p += ssl->transform->in_cid_len; |
| 4671 | |
| 4672 | ssl->transform->out_cid_len = *p++; |
| 4673 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4674 | if ((size_t) (end - p) < ssl->transform->out_cid_len) { |
| 4675 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4676 | } |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4677 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4678 | 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] | 4679 | p += ssl->transform->out_cid_len; |
| 4680 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4681 | |
| 4682 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4683 | * Saved fields from top-level ssl_context structure |
| 4684 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4685 | if ((size_t) (end - p) < 4) { |
| 4686 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4687 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4688 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4689 | ssl->badmac_seen = ((uint32_t) p[0] << 24) | |
| 4690 | ((uint32_t) p[1] << 16) | |
| 4691 | ((uint32_t) p[2] << 8) | |
| 4692 | ((uint32_t) p[3]); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4693 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4694 | |
| 4695 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4696 | if ((size_t) (end - p) < 16) { |
| 4697 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4698 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4699 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4700 | ssl->in_window_top = ((uint64_t) p[0] << 56) | |
| 4701 | ((uint64_t) p[1] << 48) | |
| 4702 | ((uint64_t) p[2] << 40) | |
| 4703 | ((uint64_t) p[3] << 32) | |
| 4704 | ((uint64_t) p[4] << 24) | |
| 4705 | ((uint64_t) p[5] << 16) | |
| 4706 | ((uint64_t) p[6] << 8) | |
| 4707 | ((uint64_t) p[7]); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4708 | p += 8; |
| 4709 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4710 | ssl->in_window = ((uint64_t) p[0] << 56) | |
| 4711 | ((uint64_t) p[1] << 48) | |
| 4712 | ((uint64_t) p[2] << 40) | |
| 4713 | ((uint64_t) p[3] << 32) | |
| 4714 | ((uint64_t) p[4] << 24) | |
| 4715 | ((uint64_t) p[5] << 16) | |
| 4716 | ((uint64_t) p[6] << 8) | |
| 4717 | ((uint64_t) p[7]); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4718 | p += 8; |
| 4719 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 4720 | |
| 4721 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4722 | if ((size_t) (end - p) < 1) { |
| 4723 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4724 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4725 | |
| 4726 | ssl->disable_datagram_packing = *p++; |
| 4727 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4728 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4729 | if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) { |
| 4730 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4731 | } |
| 4732 | memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr)); |
| 4733 | p += sizeof(ssl->cur_out_ctr); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4734 | |
| 4735 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4736 | if ((size_t) (end - p) < 2) { |
| 4737 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4738 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4739 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4740 | ssl->mtu = (p[0] << 8) | p[1]; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4741 | p += 2; |
| 4742 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4743 | |
| 4744 | #if defined(MBEDTLS_SSL_ALPN) |
| 4745 | { |
| 4746 | uint8_t alpn_len; |
| 4747 | const char **cur; |
| 4748 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4749 | if ((size_t) (end - p) < 1) { |
| 4750 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4751 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4752 | |
| 4753 | alpn_len = *p++; |
| 4754 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4755 | if (alpn_len != 0 && ssl->conf->alpn_list != NULL) { |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4756 | /* alpn_chosen should point to an item in the configured list */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4757 | for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) { |
| 4758 | if (strlen(*cur) == alpn_len && |
| 4759 | memcmp(p, cur, alpn_len) == 0) { |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4760 | ssl->alpn_chosen = *cur; |
| 4761 | break; |
| 4762 | } |
| 4763 | } |
| 4764 | } |
| 4765 | |
| 4766 | /* can only happen on conf mismatch */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4767 | if (alpn_len != 0 && ssl->alpn_chosen == NULL) { |
| 4768 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4769 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4770 | |
| 4771 | p += alpn_len; |
| 4772 | } |
| 4773 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4774 | |
| 4775 | /* |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4776 | * Forced fields from top-level ssl_context structure |
| 4777 | * |
| 4778 | * Most of them already set to the correct value by mbedtls_ssl_init() and |
| 4779 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. |
| 4780 | */ |
| 4781 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 4782 | ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4783 | |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 4784 | /* Adjust pointers for header fields of outgoing records to |
| 4785 | * the given transform, accounting for explicit IV and CID. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4786 | mbedtls_ssl_update_out_pointers(ssl, ssl->transform); |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 4787 | |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4788 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4789 | ssl->in_epoch = 1; |
| 4790 | #endif |
| 4791 | |
| 4792 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, |
| 4793 | * 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] | 4794 | * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform() |
| 4795 | * inappropriately. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4796 | if (ssl->handshake != NULL) { |
| 4797 | mbedtls_ssl_handshake_free(ssl); |
| 4798 | mbedtls_free(ssl->handshake); |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4799 | ssl->handshake = NULL; |
| 4800 | } |
| 4801 | |
| 4802 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4803 | * Done - should have consumed entire buffer |
| 4804 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4805 | if (p != end) { |
| 4806 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4807 | } |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4808 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4809 | return 0; |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4810 | } |
| 4811 | |
| 4812 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 4813 | * Deserialize context: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4814 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4815 | int mbedtls_ssl_context_load(mbedtls_ssl_context *context, |
| 4816 | const unsigned char *buf, |
| 4817 | size_t len) |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4818 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4819 | int ret = ssl_context_load(context, buf, len); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4820 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4821 | if (ret != 0) { |
| 4822 | mbedtls_ssl_free(context); |
| 4823 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4824 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4825 | return ret; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4826 | } |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 4827 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4828 | |
| 4829 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4830 | * Free an SSL context |
| 4831 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4832 | void mbedtls_ssl_free(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4833 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4834 | if (ssl == NULL) { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4835 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4836 | } |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4837 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4838 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> free")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4839 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4840 | if (ssl->out_buf != NULL) { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 4841 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4842 | size_t out_buf_len = ssl->out_buf_len; |
| 4843 | #else |
| 4844 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 4845 | #endif |
| 4846 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4847 | mbedtls_platform_zeroize(ssl->out_buf, out_buf_len); |
| 4848 | mbedtls_free(ssl->out_buf); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4849 | ssl->out_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4850 | } |
| 4851 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4852 | if (ssl->in_buf != NULL) { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 4853 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4854 | size_t in_buf_len = ssl->in_buf_len; |
| 4855 | #else |
| 4856 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 4857 | #endif |
| 4858 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4859 | mbedtls_platform_zeroize(ssl->in_buf, in_buf_len); |
| 4860 | mbedtls_free(ssl->in_buf); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4861 | ssl->in_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4862 | } |
| 4863 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4864 | if (ssl->transform) { |
| 4865 | mbedtls_ssl_transform_free(ssl->transform); |
| 4866 | mbedtls_free(ssl->transform); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4867 | } |
| 4868 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4869 | if (ssl->handshake) { |
| 4870 | mbedtls_ssl_handshake_free(ssl); |
| 4871 | mbedtls_free(ssl->handshake); |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 4872 | |
| 4873 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4874 | mbedtls_ssl_transform_free(ssl->transform_negotiate); |
| 4875 | mbedtls_free(ssl->transform_negotiate); |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 4876 | #endif |
| 4877 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4878 | mbedtls_ssl_session_free(ssl->session_negotiate); |
| 4879 | mbedtls_free(ssl->session_negotiate); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4880 | } |
| 4881 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4882 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4883 | mbedtls_ssl_transform_free(ssl->transform_application); |
| 4884 | mbedtls_free(ssl->transform_application); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4885 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 4886 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4887 | if (ssl->session) { |
| 4888 | mbedtls_ssl_session_free(ssl->session); |
| 4889 | mbedtls_free(ssl->session); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 4890 | } |
| 4891 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 4892 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4893 | if (ssl->hostname != NULL) { |
| 4894 | mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname)); |
| 4895 | mbedtls_free(ssl->hostname); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4896 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 4897 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4898 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 4899 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4900 | mbedtls_free(ssl->cli_id); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 4901 | #endif |
| 4902 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4903 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= free")); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 4904 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 4905 | /* Actually clear after last debug message */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4906 | mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context)); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4907 | } |
| 4908 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4909 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 4910 | * Initialize mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4911 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4912 | void mbedtls_ssl_config_init(mbedtls_ssl_config *conf) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4913 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4914 | memset(conf, 0, sizeof(mbedtls_ssl_config)); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4915 | } |
| 4916 | |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4917 | /* The selection should be the same as mbedtls_x509_crt_profile_default in |
| 4918 | * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters: |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4919 | * curves with a lower resource usage come first. |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4920 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4921 | * about this list. |
| 4922 | */ |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4923 | static uint16_t ssl_preset_default_groups[] = { |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4924 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4925 | MBEDTLS_SSL_IANA_TLS_GROUP_X25519, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4926 | #endif |
| 4927 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4928 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4929 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4930 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4931 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4932 | #endif |
| 4933 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4934 | MBEDTLS_SSL_IANA_TLS_GROUP_X448, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4935 | #endif |
| 4936 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4937 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4938 | #endif |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4939 | #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4940 | MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4941 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4942 | #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4943 | MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4944 | #endif |
| 4945 | #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4946 | MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4947 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4948 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4949 | }; |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4950 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4951 | static int ssl_preset_suiteb_ciphersuites[] = { |
| 4952 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 4953 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 4954 | 0 |
| 4955 | }; |
| 4956 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4957 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4958 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4959 | /* NOTICE: |
Jerry Yu | 0b994b8 | 2022-01-25 17:22:12 +0800 | [diff] [blame] | 4960 | * 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] | 4961 | * rules SHOULD be upheld. |
| 4962 | * - No duplicate entries. |
| 4963 | * - 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] | 4964 | * - ssl_tls12_preset* is for TLS 1.2 use only. |
Jerry Yu | 370e146 | 2022-01-25 10:36:53 +0800 | [diff] [blame] | 4965 | * - 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] | 4966 | */ |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4967 | static uint16_t ssl_preset_default_sig_algs[] = { |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4968 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4969 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 4970 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 4971 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4972 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA && |
Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 4973 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4974 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4975 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4976 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 4977 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4978 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&& |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4979 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
| 4980 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4981 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 4982 | defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
| 4983 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4984 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&& |
Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 4985 | MBEDTLS_ECP_DP_SECP521R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4986 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4987 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \ |
| 4988 | defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 4989 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4990 | #endif \ |
| 4991 | /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA */ |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 4992 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4993 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \ |
| 4994 | defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 4995 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4996 | #endif \ |
| 4997 | /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */ |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 4998 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4999 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \ |
| 5000 | defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 5001 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5002 | #endif \ |
| 5003 | /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */ |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 5004 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5005 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 5006 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512, |
| 5007 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */ |
| 5008 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5009 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 5010 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384, |
| 5011 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */ |
| 5012 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5013 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 693a47a | 2022-06-23 14:02:28 +0800 | [diff] [blame] | 5014 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
| 5015 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */ |
| 5016 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5017 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5018 | }; |
| 5019 | |
| 5020 | /* NOTICE: see above */ |
| 5021 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5022 | static uint16_t ssl_tls12_preset_default_sig_algs[] = { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5023 | #if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5024 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5025 | 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] | 5026 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 5027 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 5028 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, |
| 5029 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5030 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5031 | 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] | 5032 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5033 | #endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5034 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5035 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5036 | 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] | 5037 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 5038 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 5039 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, |
| 5040 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5041 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5042 | 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] | 5043 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5044 | #endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5045 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5046 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5047 | 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] | 5048 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 5049 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 5050 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
| 5051 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5052 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5053 | 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] | 5054 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5055 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5056 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5057 | }; |
| 5058 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5059 | /* NOTICE: see above */ |
| 5060 | static uint16_t ssl_preset_suiteb_sig_algs[] = { |
| 5061 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5062 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5063 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 5064 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5065 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&& |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5066 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
| 5067 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5068 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 5069 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 5070 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5071 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&& |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 5072 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5073 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5074 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \ |
| 5075 | defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5076 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5077 | #endif \ |
| 5078 | /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5079 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5080 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 5081 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5082 | #endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 5083 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5084 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 5085 | }; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 5086 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5087 | /* NOTICE: see above */ |
| 5088 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5089 | static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5090 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5091 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5092 | 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] | 5093 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5094 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5095 | 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] | 5096 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5097 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5098 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5099 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5100 | 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] | 5101 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5102 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5103 | 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] | 5104 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5105 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5106 | MBEDTLS_TLS_SIG_NONE |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 5107 | }; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5108 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5109 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5110 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5111 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5112 | static uint16_t ssl_preset_suiteb_groups[] = { |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 5113 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5114 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 5115 | #endif |
| 5116 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5117 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 5118 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5119 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5120 | }; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5121 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5122 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5123 | /* 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] | 5124 | * to make sure there are no duplicated signature algorithm entries. */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5125 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5126 | static int ssl_check_no_sig_alg_duplication(uint16_t *sig_algs) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5127 | { |
| 5128 | size_t i, j; |
| 5129 | int ret = 0; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5130 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5131 | for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) { |
| 5132 | for (j = 0; j < i; j++) { |
| 5133 | if (sig_algs[i] != sig_algs[j]) { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 5134 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5135 | } |
| 5136 | mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET |
| 5137 | ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n", |
| 5138 | sig_algs[i], j, i); |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 5139 | ret = -1; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5140 | } |
| 5141 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5142 | return ret; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5143 | } |
| 5144 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5145 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5146 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5147 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 5148 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5149 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5150 | int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf, |
| 5151 | int endpoint, int transport, int preset) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5152 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 5153 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5154 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 5155 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5156 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5157 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5158 | if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) { |
| 5159 | mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n"); |
| 5160 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5161 | } |
| 5162 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5163 | if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) { |
| 5164 | mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n"); |
| 5165 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5166 | } |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5167 | |
| 5168 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5169 | if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) { |
| 5170 | mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n"); |
| 5171 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5172 | } |
| 5173 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5174 | if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) { |
| 5175 | mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n"); |
| 5176 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5177 | } |
| 5178 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5179 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5180 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 5181 | /* Use the functions here so that they are covered in tests, |
| 5182 | * but otherwise access member directly for efficiency */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5183 | mbedtls_ssl_conf_endpoint(conf, endpoint); |
| 5184 | mbedtls_ssl_conf_transport(conf, transport); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5185 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5186 | /* |
| 5187 | * Things that are common to all presets |
| 5188 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 5189 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5190 | if (endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 5191 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 5192 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 5193 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
| 5194 | #endif |
| 5195 | } |
| 5196 | #endif |
| 5197 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5198 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 5199 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 5200 | #endif |
| 5201 | |
| 5202 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 5203 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 5204 | #endif |
| 5205 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 5206 | #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] | 5207 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 5208 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 5209 | #endif |
| 5210 | |
| 5211 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 5212 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 5213 | #endif |
| 5214 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 5215 | #if defined(MBEDTLS_SSL_SRV_C) |
| 5216 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
TRodziewicz | 3946f79 | 2021-06-14 12:11:18 +0200 | [diff] [blame] | 5217 | conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER; |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 5218 | #endif |
| 5219 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5220 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5221 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
| 5222 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
| 5223 | #endif |
| 5224 | |
| 5225 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 5226 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5227 | memset(conf->renego_period, 0x00, 2); |
| 5228 | memset(conf->renego_period + 2, 0xFF, 6); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5229 | #endif |
| 5230 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5231 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5232 | if (endpoint == MBEDTLS_SSL_IS_SERVER) { |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 5233 | const unsigned char dhm_p[] = |
| 5234 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 5235 | const unsigned char dhm_g[] = |
| 5236 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 5237 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5238 | if ((ret = mbedtls_ssl_conf_dh_param_bin(conf, |
| 5239 | dhm_p, sizeof(dhm_p), |
| 5240 | dhm_g, sizeof(dhm_g))) != 0) { |
| 5241 | return ret; |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 5242 | } |
| 5243 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 5244 | #endif |
| 5245 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5246 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 6ee56aa | 2022-12-06 17:47:22 +0800 | [diff] [blame] | 5247 | |
| 5248 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5249 | mbedtls_ssl_tls13_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED); |
Jerry Yu | 6ee56aa | 2022-12-06 17:47:22 +0800 | [diff] [blame] | 5250 | #if defined(MBEDTLS_SSL_SRV_C) |
| 5251 | mbedtls_ssl_tls13_conf_max_early_data_size( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5252 | conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE); |
Jerry Yu | 6ee56aa | 2022-12-06 17:47:22 +0800 | [diff] [blame] | 5253 | #endif |
| 5254 | #endif /* MBEDTLS_SSL_EARLY_DATA */ |
| 5255 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 5256 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 5257 | mbedtls_ssl_conf_new_session_tickets( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5258 | conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS); |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 5259 | #endif |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 5260 | /* |
| 5261 | * Allow all TLS 1.3 key exchange modes by default. |
| 5262 | */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 5263 | conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL; |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5264 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 5265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5266 | if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 5267 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5268 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 5269 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5270 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5271 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 5272 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5273 | } else { |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5274 | #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] | 5275 | if (endpoint == MBEDTLS_SSL_IS_CLIENT) { |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5276 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5277 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5278 | } else { |
| 5279 | /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */ |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5280 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5281 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5282 | } |
| 5283 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 5284 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 5285 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 5286 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5287 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5288 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5289 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5290 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5291 | #endif |
| 5292 | } |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 5293 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5294 | /* |
| 5295 | * Preset-specific defaults |
| 5296 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5297 | switch (preset) { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5298 | /* |
| 5299 | * NSA Suite B |
| 5300 | */ |
| 5301 | case MBEDTLS_SSL_PRESET_SUITEB: |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5302 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 5303 | conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5304 | |
| 5305 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5306 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5307 | #endif |
| 5308 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5309 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5310 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5311 | if (mbedtls_ssl_conf_is_tls12_only(conf)) { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5312 | conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5313 | } else |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5314 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5315 | conf->sig_algs = ssl_preset_suiteb_sig_algs; |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5316 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5317 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5318 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 5319 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5320 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5321 | conf->group_list = ssl_preset_suiteb_groups; |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 5322 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5323 | |
| 5324 | /* |
| 5325 | * Default |
| 5326 | */ |
| 5327 | default: |
Ronald Cron | f660655 | 2022-03-15 11:23:25 +0100 | [diff] [blame] | 5328 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 5329 | conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites(); |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5330 | |
| 5331 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5332 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 5333 | #endif |
| 5334 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5335 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5336 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5337 | if (mbedtls_ssl_conf_is_tls12_only(conf)) { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5338 | conf->sig_algs = ssl_tls12_preset_default_sig_algs; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5339 | } else |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5340 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5341 | conf->sig_algs = ssl_preset_default_sig_algs; |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5342 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5343 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5344 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 5345 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5346 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5347 | conf->group_list = ssl_preset_default_groups; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5348 | |
| 5349 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 5350 | conf->dhm_min_bitlen = 1024; |
| 5351 | #endif |
| 5352 | } |
| 5353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5354 | return 0; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5355 | } |
| 5356 | |
| 5357 | /* |
| 5358 | * Free mbedtls_ssl_config |
| 5359 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5360 | void mbedtls_ssl_config_free(mbedtls_ssl_config *conf) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5361 | { |
| 5362 | #if defined(MBEDTLS_DHM_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5363 | mbedtls_mpi_free(&conf->dhm_P); |
| 5364 | mbedtls_mpi_free(&conf->dhm_G); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5365 | #endif |
| 5366 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 5367 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 5368 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5369 | if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 5370 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 5371 | } |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5372 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5373 | if (conf->psk != NULL) { |
| 5374 | mbedtls_platform_zeroize(conf->psk, conf->psk_len); |
| 5375 | mbedtls_free(conf->psk); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 5376 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5377 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 5378 | } |
| 5379 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5380 | if (conf->psk_identity != NULL) { |
| 5381 | mbedtls_platform_zeroize(conf->psk_identity, conf->psk_identity_len); |
| 5382 | mbedtls_free(conf->psk_identity); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 5383 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5384 | conf->psk_identity_len = 0; |
| 5385 | } |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 5386 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5387 | |
| 5388 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5389 | ssl_key_cert_free(conf->key_cert); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5390 | #endif |
| 5391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5392 | mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config)); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5393 | } |
| 5394 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 5395 | #if defined(MBEDTLS_PK_C) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5396 | (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C)) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5397 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5398 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5399 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5400 | 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] | 5401 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5402 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5403 | if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) { |
| 5404 | return MBEDTLS_SSL_SIG_RSA; |
| 5405 | } |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5406 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5407 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5408 | if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) { |
| 5409 | return MBEDTLS_SSL_SIG_ECDSA; |
| 5410 | } |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5411 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5412 | return MBEDTLS_SSL_SIG_ANON; |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5413 | } |
| 5414 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5415 | 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] | 5416 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5417 | switch (type) { |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 5418 | case MBEDTLS_PK_RSA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5419 | return MBEDTLS_SSL_SIG_RSA; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 5420 | case MBEDTLS_PK_ECDSA: |
| 5421 | case MBEDTLS_PK_ECKEY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5422 | return MBEDTLS_SSL_SIG_ECDSA; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 5423 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5424 | return MBEDTLS_SSL_SIG_ANON; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 5425 | } |
| 5426 | } |
| 5427 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5428 | 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] | 5429 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5430 | switch (sig) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5431 | #if defined(MBEDTLS_RSA_C) |
| 5432 | case MBEDTLS_SSL_SIG_RSA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5433 | return MBEDTLS_PK_RSA; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5434 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5435 | #if defined(MBEDTLS_ECDSA_C) |
| 5436 | case MBEDTLS_SSL_SIG_ECDSA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5437 | return MBEDTLS_PK_ECDSA; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5438 | #endif |
| 5439 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5440 | return MBEDTLS_PK_NONE; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5441 | } |
| 5442 | } |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 5443 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5444 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 5445 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5446 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 5447 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5448 | 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] | 5449 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5450 | switch (hash) { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5451 | #if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5452 | case MBEDTLS_SSL_HASH_MD5: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5453 | return MBEDTLS_MD_MD5; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5454 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5455 | #if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5456 | case MBEDTLS_SSL_HASH_SHA1: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5457 | return MBEDTLS_MD_SHA1; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5458 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5459 | #if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5460 | case MBEDTLS_SSL_HASH_SHA224: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5461 | return MBEDTLS_MD_SHA224; |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 5462 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5463 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5464 | case MBEDTLS_SSL_HASH_SHA256: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5465 | return MBEDTLS_MD_SHA256; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5466 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5467 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5468 | case MBEDTLS_SSL_HASH_SHA384: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5469 | return MBEDTLS_MD_SHA384; |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 5470 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5471 | #if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5472 | case MBEDTLS_SSL_HASH_SHA512: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5473 | return MBEDTLS_MD_SHA512; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5474 | #endif |
| 5475 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5476 | return MBEDTLS_MD_NONE; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5477 | } |
| 5478 | } |
| 5479 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5480 | /* |
| 5481 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 5482 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5483 | unsigned char mbedtls_ssl_hash_from_md_alg(int md) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5484 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5485 | switch (md) { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5486 | #if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5487 | case MBEDTLS_MD_MD5: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5488 | return MBEDTLS_SSL_HASH_MD5; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5489 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5490 | #if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5491 | case MBEDTLS_MD_SHA1: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5492 | return MBEDTLS_SSL_HASH_SHA1; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5493 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5494 | #if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5495 | case MBEDTLS_MD_SHA224: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5496 | return MBEDTLS_SSL_HASH_SHA224; |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 5497 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5498 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5499 | case MBEDTLS_MD_SHA256: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5500 | return MBEDTLS_SSL_HASH_SHA256; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5501 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5502 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5503 | case MBEDTLS_MD_SHA384: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5504 | return MBEDTLS_SSL_HASH_SHA384; |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 5505 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5506 | #if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5507 | case MBEDTLS_MD_SHA512: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5508 | return MBEDTLS_SSL_HASH_SHA512; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5509 | #endif |
| 5510 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5511 | return MBEDTLS_SSL_HASH_NONE; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5512 | } |
| 5513 | } |
| 5514 | |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5515 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5516 | * 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] | 5517 | * 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] | 5518 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5519 | 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] | 5520 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5521 | const uint16_t *group_list = mbedtls_ssl_get_groups(ssl); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5522 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5523 | if (group_list == NULL) { |
| 5524 | return -1; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5525 | } |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5526 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5527 | for (; *group_list != 0; group_list++) { |
| 5528 | if (*group_list == tls_id) { |
| 5529 | return 0; |
| 5530 | } |
| 5531 | } |
| 5532 | |
| 5533 | return -1; |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5534 | } |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 5535 | |
| 5536 | #if defined(MBEDTLS_ECP_C) |
| 5537 | /* |
| 5538 | * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id. |
| 5539 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5540 | 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] | 5541 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5542 | 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] | 5543 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5544 | if (tls_id == 0) { |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 5545 | return -1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5546 | } |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 5547 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5548 | return mbedtls_ssl_check_curve_tls_id(ssl, tls_id); |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 5549 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 5550 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5551 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5552 | #if defined(MBEDTLS_DEBUG_C) |
Valerio Setti | 67419f0 | 2023-01-04 16:12:42 +0100 | [diff] [blame] | 5553 | #define EC_NAME(_name_) _name_ |
| 5554 | #else |
| 5555 | #define EC_NAME(_name_) NULL |
| 5556 | #endif |
| 5557 | |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5558 | static const struct { |
| 5559 | uint16_t tls_id; |
| 5560 | mbedtls_ecp_group_id ecp_group_id; |
| 5561 | psa_ecc_family_t psa_family; |
| 5562 | uint16_t bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5563 | const char *name; |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5564 | } tls_id_match_table[] = |
| 5565 | { |
| 5566 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_521) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5567 | { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521, EC_NAME("secp521r1") }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5568 | #endif |
| 5569 | #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5570 | { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512, EC_NAME("brainpoolP512r1") }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5571 | #endif |
Valerio Setti | 67419f0 | 2023-01-04 16:12:42 +0100 | [diff] [blame] | 5572 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_384) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5573 | { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384, EC_NAME("secp384r1") }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5574 | #endif |
Valerio Setti | 67419f0 | 2023-01-04 16:12:42 +0100 | [diff] [blame] | 5575 | #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5576 | { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384, EC_NAME("brainpoolP384r1") }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5577 | #endif |
Valerio Setti | 67419f0 | 2023-01-04 16:12:42 +0100 | [diff] [blame] | 5578 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5579 | { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256, EC_NAME("secp256r1") }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5580 | #endif |
| 5581 | #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5582 | { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256, EC_NAME("secp256k1") }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5583 | #endif |
Valerio Setti | 67419f0 | 2023-01-04 16:12:42 +0100 | [diff] [blame] | 5584 | #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5585 | { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256, EC_NAME("brainpoolP256r1") }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5586 | #endif |
| 5587 | #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_224) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5588 | { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224, EC_NAME("secp224r1") }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5589 | #endif |
| 5590 | #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_224) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5591 | { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224, EC_NAME("secp224k1") }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5592 | #endif |
| 5593 | #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_192) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5594 | { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192, EC_NAME("secp192r1") }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5595 | #endif |
| 5596 | #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_192) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5597 | { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192, EC_NAME("secp192k1") }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5598 | #endif |
| 5599 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_255) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5600 | { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255, EC_NAME("x25519") }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5601 | #endif |
| 5602 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_448) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5603 | { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448, EC_NAME("x448") }, |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5604 | #endif |
| 5605 | { 0, MBEDTLS_ECP_DP_NONE, 0, 0, NULL }, |
| 5606 | }; |
| 5607 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5608 | int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id, |
| 5609 | psa_ecc_family_t *family, |
| 5610 | size_t *bits) |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5611 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5612 | for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) { |
| 5613 | if (tls_id_match_table[i].tls_id == tls_id) { |
| 5614 | if (family != NULL) { |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5615 | *family = tls_id_match_table[i].psa_family; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5616 | } |
| 5617 | if (bits != NULL) { |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5618 | *bits = tls_id_match_table[i].bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5619 | } |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5620 | return PSA_SUCCESS; |
| 5621 | } |
| 5622 | } |
| 5623 | |
| 5624 | return PSA_ERROR_NOT_SUPPORTED; |
| 5625 | } |
| 5626 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5627 | 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] | 5628 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5629 | for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) { |
| 5630 | if (tls_id_match_table[i].tls_id == tls_id) { |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5631 | return tls_id_match_table[i].ecp_group_id; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5632 | } |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5633 | } |
| 5634 | |
| 5635 | return MBEDTLS_ECP_DP_NONE; |
| 5636 | } |
| 5637 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5638 | 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] | 5639 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5640 | for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE; |
| 5641 | i++) { |
| 5642 | if (tls_id_match_table[i].ecp_group_id == grp_id) { |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5643 | return tls_id_match_table[i].tls_id; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5644 | } |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5645 | } |
| 5646 | |
| 5647 | return 0; |
| 5648 | } |
| 5649 | |
Valerio Setti | 67419f0 | 2023-01-04 16:12:42 +0100 | [diff] [blame] | 5650 | #if defined(MBEDTLS_DEBUG_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5651 | 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] | 5652 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5653 | for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) { |
| 5654 | if (tls_id_match_table[i].tls_id == tls_id) { |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5655 | return tls_id_match_table[i].name; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5656 | } |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5657 | } |
| 5658 | |
| 5659 | return NULL; |
| 5660 | } |
Valerio Setti | 67419f0 | 2023-01-04 16:12:42 +0100 | [diff] [blame] | 5661 | #endif |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5662 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5663 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5664 | int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert, |
| 5665 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
| 5666 | int cert_endpoint, |
| 5667 | uint32_t *flags) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5668 | { |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5669 | int ret = 0; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5670 | int usage = 0; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5671 | const char *ext_oid; |
| 5672 | size_t ext_len; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5673 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5674 | if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) { |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5675 | /* Server part of the key exchange */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5676 | switch (ciphersuite->key_exchange) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5677 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 5678 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5679 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5680 | break; |
| 5681 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5682 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 5683 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 5684 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 5685 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5686 | break; |
| 5687 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5688 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 5689 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5690 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5691 | break; |
| 5692 | |
| 5693 | /* Don't use default: we want warnings when adding new values */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5694 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 5695 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 5696 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 5697 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 5698 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5699 | usage = 0; |
| 5700 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5701 | } else { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5702 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ |
| 5703 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5704 | } |
| 5705 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5706 | if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5707 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5708 | ret = -1; |
| 5709 | } |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5710 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5711 | if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5712 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5713 | ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH); |
| 5714 | } else { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5715 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5716 | ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5717 | } |
| 5718 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5719 | if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5720 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5721 | ret = -1; |
| 5722 | } |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5723 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5724 | return ret; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5725 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5726 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 5727 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5728 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5729 | int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl, |
| 5730 | const mbedtls_md_type_t md, |
| 5731 | unsigned char *dst, |
| 5732 | size_t dst_len, |
| 5733 | size_t *olen) |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5734 | { |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5735 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5736 | psa_hash_operation_t *hash_operation_to_clone; |
| 5737 | psa_hash_operation_t hash_operation = psa_hash_operation_init(); |
| 5738 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5739 | *olen = 0; |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5740 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5741 | switch (md) { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5742 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5743 | case MBEDTLS_MD_SHA384: |
| 5744 | hash_operation_to_clone = &ssl->handshake->fin_sha384_psa; |
| 5745 | break; |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5746 | #endif |
| 5747 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5748 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5749 | case MBEDTLS_MD_SHA256: |
| 5750 | hash_operation_to_clone = &ssl->handshake->fin_sha256_psa; |
| 5751 | break; |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5752 | #endif |
| 5753 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5754 | default: |
| 5755 | goto exit; |
| 5756 | } |
| 5757 | |
| 5758 | status = psa_hash_clone(hash_operation_to_clone, &hash_operation); |
| 5759 | if (status != PSA_SUCCESS) { |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5760 | goto exit; |
| 5761 | } |
| 5762 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5763 | status = psa_hash_finish(&hash_operation, dst, dst_len, olen); |
| 5764 | if (status != PSA_SUCCESS) { |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5765 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5766 | } |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5767 | |
| 5768 | exit: |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 5769 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 5770 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 5771 | (void) ssl; |
| 5772 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5773 | return psa_ssl_status_to_mbedtls(status); |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5774 | } |
| 5775 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5776 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5777 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5778 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5779 | static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl, |
| 5780 | unsigned char *dst, |
| 5781 | size_t dst_len, |
| 5782 | size_t *olen) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5783 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5784 | int ret; |
Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame^] | 5785 | mbedtls_md_context_t sha384; |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5786 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5787 | if (dst_len < 48) { |
| 5788 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 5789 | } |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5790 | |
Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame^] | 5791 | mbedtls_md_init(&sha384); |
| 5792 | 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] | 5793 | if (ret != 0) { |
| 5794 | goto exit; |
| 5795 | } |
Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame^] | 5796 | ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384); |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 5797 | if (ret != 0) { |
| 5798 | goto exit; |
| 5799 | } |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5800 | |
Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame^] | 5801 | if ((ret = mbedtls_md_finish(&sha384, dst)) != 0) { |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 5802 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5803 | goto exit; |
| 5804 | } |
| 5805 | |
| 5806 | *olen = 48; |
| 5807 | |
| 5808 | exit: |
| 5809 | |
Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame^] | 5810 | mbedtls_md_free(&sha384); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5811 | return ret; |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5812 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5813 | #endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5814 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5815 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5816 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5817 | static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl, |
| 5818 | unsigned char *dst, |
| 5819 | size_t dst_len, |
| 5820 | size_t *olen) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5821 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5822 | int ret; |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 5823 | mbedtls_md_context_t sha256; |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5824 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5825 | if (dst_len < 32) { |
| 5826 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 5827 | } |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5828 | |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 5829 | mbedtls_md_init(&sha256); |
| 5830 | ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0); |
| 5831 | if (ret != 0) { |
| 5832 | goto exit; |
| 5833 | } |
| 5834 | ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256); |
| 5835 | if (ret != 0) { |
| 5836 | goto exit; |
| 5837 | } |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5838 | |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 5839 | if ((ret = mbedtls_md_finish(&sha256, dst)) != 0) { |
| 5840 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5841 | goto exit; |
| 5842 | } |
| 5843 | |
| 5844 | *olen = 32; |
| 5845 | |
| 5846 | exit: |
| 5847 | |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 5848 | mbedtls_md_free(&sha256); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5849 | return ret; |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5850 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5851 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5852 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5853 | int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl, |
| 5854 | const mbedtls_md_type_t md, |
| 5855 | unsigned char *dst, |
| 5856 | size_t dst_len, |
| 5857 | size_t *olen) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5858 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5859 | switch (md) { |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5860 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5861 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5862 | case MBEDTLS_MD_SHA384: |
| 5863 | return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5864 | #endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5865 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5866 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5867 | case MBEDTLS_MD_SHA256: |
| 5868 | return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5869 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5870 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5871 | default: |
Andrzej Kurek | 409248a | 2022-10-24 10:33:21 -0400 | [diff] [blame] | 5872 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5873 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 5874 | (void) ssl; |
| 5875 | (void) dst; |
| 5876 | (void) dst_len; |
| 5877 | (void) olen; |
Andrzej Kurek | 409248a | 2022-10-24 10:33:21 -0400 | [diff] [blame] | 5878 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5879 | break; |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5880 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5881 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5882 | } |
XiaokangQian | 647719a | 2021-12-07 09:16:29 +0000 | [diff] [blame] | 5883 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5884 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5885 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5886 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5887 | /* mbedtls_ssl_parse_sig_alg_ext() |
| 5888 | * |
| 5889 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 5890 | * value (TLS 1.3 RFC8446): |
| 5891 | * enum { |
| 5892 | * .... |
| 5893 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 5894 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 5895 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 5896 | * .... |
| 5897 | * } SignatureScheme; |
| 5898 | * |
| 5899 | * struct { |
| 5900 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 5901 | * } SignatureSchemeList; |
| 5902 | * |
| 5903 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 5904 | * value (TLS 1.2 RFC5246): |
| 5905 | * enum { |
| 5906 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 5907 | * sha512(6), (255) |
| 5908 | * } HashAlgorithm; |
| 5909 | * |
| 5910 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 5911 | * SignatureAlgorithm; |
| 5912 | * |
| 5913 | * struct { |
| 5914 | * HashAlgorithm hash; |
| 5915 | * SignatureAlgorithm signature; |
| 5916 | * } SignatureAndHashAlgorithm; |
| 5917 | * |
| 5918 | * SignatureAndHashAlgorithm |
| 5919 | * supported_signature_algorithms<2..2^16-2>; |
| 5920 | * |
| 5921 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 5922 | * generalization of the TLS 1.2 signature algorithm extension. |
| 5923 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 5924 | * `SignatureScheme` field of TLS 1.3 |
| 5925 | * |
| 5926 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5927 | int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl, |
| 5928 | const unsigned char *buf, |
| 5929 | const unsigned char *end) |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5930 | { |
| 5931 | const unsigned char *p = buf; |
| 5932 | size_t supported_sig_algs_len = 0; |
| 5933 | const unsigned char *supported_sig_algs_end; |
| 5934 | uint16_t sig_alg; |
| 5935 | uint32_t common_idx = 0; |
| 5936 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5937 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2); |
| 5938 | supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5939 | p += 2; |
| 5940 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5941 | memset(ssl->handshake->received_sig_algs, 0, |
| 5942 | sizeof(ssl->handshake->received_sig_algs)); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5943 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5944 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5945 | supported_sig_algs_end = p + supported_sig_algs_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5946 | while (p < supported_sig_algs_end) { |
| 5947 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2); |
| 5948 | sig_alg = MBEDTLS_GET_UINT16_BE(p, 0); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5949 | p += 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5950 | MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s", |
| 5951 | sig_alg, |
| 5952 | mbedtls_ssl_sig_alg_to_str(sig_alg))); |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5953 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5954 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 && |
| 5955 | (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) && |
| 5956 | mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5957 | continue; |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5958 | } |
| 5959 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5960 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5961 | MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s", |
| 5962 | mbedtls_ssl_sig_alg_to_str(sig_alg))); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5963 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5964 | if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5965 | ssl->handshake->received_sig_algs[common_idx] = sig_alg; |
| 5966 | common_idx += 1; |
| 5967 | } |
| 5968 | } |
| 5969 | /* Check that we consumed all the message. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5970 | if (p != end) { |
| 5971 | MBEDTLS_SSL_DEBUG_MSG(1, |
| 5972 | ("Signature algorithms extension length misaligned")); |
| 5973 | MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, |
| 5974 | MBEDTLS_ERR_SSL_DECODE_ERROR); |
| 5975 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5976 | } |
| 5977 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5978 | if (common_idx == 0) { |
| 5979 | MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common")); |
| 5980 | MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE, |
| 5981 | MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE); |
| 5982 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5983 | } |
| 5984 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5985 | ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5986 | return 0; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5987 | } |
| 5988 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5989 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5990 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5991 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5992 | |
| 5993 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5994 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5995 | static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation, |
| 5996 | mbedtls_svc_key_id_t key, |
| 5997 | psa_algorithm_t alg, |
| 5998 | const unsigned char *raw_psk, size_t raw_psk_length, |
| 5999 | const unsigned char *seed, size_t seed_length, |
| 6000 | const unsigned char *label, size_t label_length, |
| 6001 | const unsigned char *other_secret, |
| 6002 | size_t other_secret_length, |
| 6003 | size_t capacity) |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6004 | { |
| 6005 | psa_status_t status; |
| 6006 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6007 | status = psa_key_derivation_setup(derivation, alg); |
| 6008 | if (status != PSA_SUCCESS) { |
| 6009 | return status; |
| 6010 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6011 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6012 | if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 6013 | status = psa_key_derivation_input_bytes(derivation, |
| 6014 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 6015 | seed, seed_length); |
| 6016 | if (status != PSA_SUCCESS) { |
| 6017 | return status; |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 6018 | } |
| 6019 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6020 | if (other_secret != NULL) { |
| 6021 | status = psa_key_derivation_input_bytes(derivation, |
| 6022 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 6023 | other_secret, other_secret_length); |
| 6024 | if (status != PSA_SUCCESS) { |
| 6025 | return status; |
| 6026 | } |
| 6027 | } |
| 6028 | |
| 6029 | if (mbedtls_svc_key_id_is_null(key)) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6030 | status = psa_key_derivation_input_bytes( |
| 6031 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6032 | raw_psk, raw_psk_length); |
| 6033 | } else { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6034 | status = psa_key_derivation_input_key( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6035 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6036 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6037 | if (status != PSA_SUCCESS) { |
| 6038 | return status; |
| 6039 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6040 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6041 | status = psa_key_derivation_input_bytes(derivation, |
| 6042 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 6043 | label, label_length); |
| 6044 | if (status != PSA_SUCCESS) { |
| 6045 | return status; |
| 6046 | } |
| 6047 | } else { |
| 6048 | return PSA_ERROR_NOT_SUPPORTED; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6049 | } |
| 6050 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6051 | status = psa_key_derivation_set_capacity(derivation, capacity); |
| 6052 | if (status != PSA_SUCCESS) { |
| 6053 | return status; |
| 6054 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6055 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6056 | return PSA_SUCCESS; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6057 | } |
| 6058 | |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 6059 | #if defined(PSA_WANT_ALG_SHA_384) || \ |
| 6060 | defined(PSA_WANT_ALG_SHA_256) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6061 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6062 | static int tls_prf_generic(mbedtls_md_type_t md_type, |
| 6063 | const unsigned char *secret, size_t slen, |
| 6064 | const char *label, |
| 6065 | const unsigned char *random, size_t rlen, |
| 6066 | unsigned char *dstbuf, size_t dlen) |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6067 | { |
| 6068 | psa_status_t status; |
| 6069 | psa_algorithm_t alg; |
| 6070 | mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6071 | psa_key_derivation_operation_t derivation = |
| 6072 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 6073 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6074 | if (md_type == MBEDTLS_MD_SHA384) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6075 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6076 | } else { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6077 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6078 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6079 | |
| 6080 | /* Normally a "secret" should be long enough to be impossible to |
| 6081 | * find by brute force, and in particular should not be empty. But |
| 6082 | * this PRF is also used to derive an IV, in particular in EAP-TLS, |
| 6083 | * and for this use case it makes sense to have a 0-length "secret". |
| 6084 | * Since the key API doesn't allow importing a key of length 0, |
| 6085 | * keep master_key=0, which setup_psa_key_derivation() understands |
| 6086 | * to mean a 0-length "secret" input. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6087 | if (slen != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6088 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6089 | psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE); |
| 6090 | psa_set_key_algorithm(&key_attributes, alg); |
| 6091 | psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6092 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6093 | status = psa_import_key(&key_attributes, secret, slen, &master_key); |
| 6094 | if (status != PSA_SUCCESS) { |
| 6095 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 6096 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6097 | } |
| 6098 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6099 | status = setup_psa_key_derivation(&derivation, |
| 6100 | master_key, alg, |
| 6101 | NULL, 0, |
| 6102 | random, rlen, |
| 6103 | (unsigned char const *) label, |
| 6104 | (size_t) strlen(label), |
| 6105 | NULL, 0, |
| 6106 | dlen); |
| 6107 | if (status != PSA_SUCCESS) { |
| 6108 | psa_key_derivation_abort(&derivation); |
| 6109 | psa_destroy_key(master_key); |
| 6110 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6111 | } |
| 6112 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6113 | status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen); |
| 6114 | if (status != PSA_SUCCESS) { |
| 6115 | psa_key_derivation_abort(&derivation); |
| 6116 | psa_destroy_key(master_key); |
| 6117 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6118 | } |
| 6119 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6120 | status = psa_key_derivation_abort(&derivation); |
| 6121 | if (status != PSA_SUCCESS) { |
| 6122 | psa_destroy_key(master_key); |
| 6123 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6124 | } |
| 6125 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6126 | if (!mbedtls_svc_key_id_is_null(master_key)) { |
| 6127 | status = psa_destroy_key(master_key); |
| 6128 | } |
| 6129 | if (status != PSA_SUCCESS) { |
| 6130 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 6131 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6132 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6133 | return 0; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6134 | } |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 6135 | #endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6136 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6137 | |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 6138 | #if defined(MBEDTLS_MD_C) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6139 | (defined(MBEDTLS_SHA256_C) || \ |
| 6140 | defined(MBEDTLS_SHA384_C)) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6141 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6142 | static int tls_prf_generic(mbedtls_md_type_t md_type, |
| 6143 | const unsigned char *secret, size_t slen, |
| 6144 | const char *label, |
| 6145 | const unsigned char *random, size_t rlen, |
| 6146 | unsigned char *dstbuf, size_t dlen) |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6147 | { |
| 6148 | size_t nb; |
| 6149 | size_t i, j, k, md_len; |
| 6150 | unsigned char *tmp; |
| 6151 | size_t tmp_len = 0; |
| 6152 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 6153 | const mbedtls_md_info_t *md_info; |
| 6154 | mbedtls_md_context_t md_ctx; |
| 6155 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6156 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6157 | mbedtls_md_init(&md_ctx); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6159 | if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) { |
| 6160 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 6161 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6162 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6163 | md_len = mbedtls_md_get_size(md_info); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6164 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6165 | tmp_len = md_len + strlen(label) + rlen; |
| 6166 | tmp = mbedtls_calloc(1, tmp_len); |
| 6167 | if (tmp == NULL) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6168 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 6169 | goto exit; |
| 6170 | } |
| 6171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6172 | nb = strlen(label); |
| 6173 | memcpy(tmp + md_len, label, nb); |
| 6174 | memcpy(tmp + md_len + nb, random, rlen); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6175 | nb += rlen; |
| 6176 | |
| 6177 | /* |
| 6178 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 6179 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6180 | if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6181 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6182 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6183 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6184 | ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen); |
| 6185 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6186 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6187 | } |
| 6188 | ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb); |
| 6189 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6190 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6191 | } |
| 6192 | ret = mbedtls_md_hmac_finish(&md_ctx, tmp); |
| 6193 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6194 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6195 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6196 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6197 | for (i = 0; i < dlen; i += md_len) { |
| 6198 | ret = mbedtls_md_hmac_reset(&md_ctx); |
| 6199 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6200 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6201 | } |
| 6202 | ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb); |
| 6203 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6204 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6205 | } |
| 6206 | ret = mbedtls_md_hmac_finish(&md_ctx, h_i); |
| 6207 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6208 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6209 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6210 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6211 | ret = mbedtls_md_hmac_reset(&md_ctx); |
| 6212 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6213 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6214 | } |
| 6215 | ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len); |
| 6216 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6217 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6218 | } |
| 6219 | ret = mbedtls_md_hmac_finish(&md_ctx, tmp); |
| 6220 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6221 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6222 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6223 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6224 | k = (i + md_len > dlen) ? dlen % md_len : md_len; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6225 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6226 | for (j = 0; j < k; j++) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6227 | dstbuf[i + j] = h_i[j]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6228 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6229 | } |
| 6230 | |
| 6231 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6232 | mbedtls_md_free(&md_ctx); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6233 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6234 | if (tmp != NULL) { |
| 6235 | mbedtls_platform_zeroize(tmp, tmp_len); |
| 6236 | } |
Dave Rodgman | 29b9b2b | 2022-11-01 16:08:14 +0000 | [diff] [blame] | 6237 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6238 | mbedtls_platform_zeroize(h_i, sizeof(h_i)); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6239 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6240 | mbedtls_free(tmp); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6241 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6242 | return ret; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6243 | } |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 6244 | #endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6245 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6246 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6247 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6248 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6249 | static int tls_prf_sha256(const unsigned char *secret, size_t slen, |
| 6250 | const char *label, |
| 6251 | const unsigned char *random, size_t rlen, |
| 6252 | unsigned char *dstbuf, size_t dlen) |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6253 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6254 | return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen, |
| 6255 | label, random, rlen, dstbuf, dlen); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6256 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 6257 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6258 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6259 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6260 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6261 | static int tls_prf_sha384(const unsigned char *secret, size_t slen, |
| 6262 | const char *label, |
| 6263 | const unsigned char *random, size_t rlen, |
| 6264 | unsigned char *dstbuf, size_t dlen) |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6265 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6266 | return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen, |
| 6267 | label, random, rlen, dstbuf, dlen); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6268 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 6269 | #endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6270 | |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6271 | /* |
| 6272 | * Set appropriate PRF function and other SSL / TLS1.2 functions |
| 6273 | * |
| 6274 | * Inputs: |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6275 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
| 6276 | * |
| 6277 | * Outputs: |
| 6278 | * - the tls_prf, calc_verify and calc_finished members of handshake structure |
| 6279 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6280 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6281 | static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake, |
| 6282 | mbedtls_md_type_t hash) |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6283 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6284 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6285 | if (hash == MBEDTLS_MD_SHA384) { |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6286 | handshake->tls_prf = tls_prf_sha384; |
| 6287 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 6288 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6289 | } else |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6290 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6291 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6292 | { |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 6293 | (void) hash; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6294 | handshake->tls_prf = tls_prf_sha256; |
| 6295 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 6296 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
| 6297 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 6298 | #else |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6299 | { |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6300 | (void) handshake; |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 6301 | (void) hash; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6302 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6303 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 6304 | #endif |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6306 | return 0; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6307 | } |
Jerry Yu | d6ab235 | 2022-02-17 14:03:43 +0800 | [diff] [blame] | 6308 | |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6309 | /* |
| 6310 | * Compute master secret if needed |
| 6311 | * |
| 6312 | * Parameters: |
| 6313 | * [in/out] handshake |
| 6314 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf |
| 6315 | * (PSA-PSK) ciphersuite_info, psk_opaque |
| 6316 | * [out] premaster (cleared) |
| 6317 | * [out] master |
| 6318 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK |
| 6319 | * debug: conf->f_dbg, conf->p_dbg |
| 6320 | * EMS: passed to calc_verify (debug + session_negotiate) |
Ronald Cron | a25cf58 | 2022-03-07 11:10:36 +0100 | [diff] [blame] | 6321 | * PSA-PSA: conf |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6322 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6323 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6324 | static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake, |
| 6325 | unsigned char *master, |
| 6326 | const mbedtls_ssl_context *ssl) |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6327 | { |
| 6328 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6329 | |
| 6330 | /* cf. RFC 5246, Section 8.1: |
| 6331 | * "The master secret is always exactly 48 bytes in length." */ |
| 6332 | size_t const master_secret_len = 48; |
| 6333 | |
| 6334 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 6335 | unsigned char session_hash[48]; |
| 6336 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 6337 | |
| 6338 | /* The label for the KDF used for key expansion. |
| 6339 | * This is either "master secret" or "extended master secret" |
| 6340 | * depending on whether the Extended Master Secret extension |
| 6341 | * is used. */ |
| 6342 | char const *lbl = "master secret"; |
| 6343 | |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 6344 | /* The seed for the KDF used for key expansion. |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6345 | * - If the Extended Master Secret extension is not used, |
| 6346 | * this is ClientHello.Random + ServerHello.Random |
| 6347 | * (see Sect. 8.1 in RFC 5246). |
| 6348 | * - If the Extended Master Secret extension is used, |
| 6349 | * this is the transcript of the handshake so far. |
| 6350 | * (see Sect. 4 in RFC 7627). */ |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 6351 | unsigned char const *seed = handshake->randbytes; |
| 6352 | size_t seed_len = 64; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6353 | |
| 6354 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 6355 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ |
| 6356 | !(defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6357 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6358 | ssl = NULL; /* make sure we don't use it except for those cases */ |
| 6359 | (void) ssl; |
| 6360 | #endif |
| 6361 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6362 | if (handshake->resume != 0) { |
| 6363 | MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)")); |
| 6364 | return 0; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6365 | } |
| 6366 | |
| 6367 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6368 | if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) { |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6369 | lbl = "extended master secret"; |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 6370 | seed = session_hash; |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 6371 | ret = handshake->calc_verify(ssl, session_hash, &seed_len); |
| 6372 | if (ret != 0) { |
| 6373 | MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret); |
| 6374 | } |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6375 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6376 | MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret", |
| 6377 | session_hash, seed_len); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6378 | } |
Przemek Stekiel | 169bf0b | 2022-04-29 07:53:29 +0200 | [diff] [blame] | 6379 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6380 | |
Przemek Stekiel | 99114f3 | 2022-04-22 11:20:09 +0200 | [diff] [blame] | 6381 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Przemek Stekiel | 8a4b7fd | 2022-04-28 09:22:22 +0200 | [diff] [blame] | 6382 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6383 | if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) { |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6384 | /* Perform PSK-to-MS expansion in a single step. */ |
| 6385 | psa_status_t status; |
| 6386 | psa_algorithm_t alg; |
| 6387 | mbedtls_svc_key_id_t psk; |
| 6388 | psa_key_derivation_operation_t derivation = |
| 6389 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 6390 | mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac; |
| 6391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6392 | MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion")); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6393 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6394 | psk = mbedtls_ssl_get_opaque_psk(ssl); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6395 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6396 | if (hash_alg == MBEDTLS_MD_SHA384) { |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6397 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6398 | } else { |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6399 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6400 | } |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6401 | |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 6402 | size_t other_secret_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6403 | unsigned char *other_secret = NULL; |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 6404 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6405 | switch (handshake->ciphersuite_info->key_exchange) { |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6406 | /* Provide other secret. |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 6407 | * Other secret is stored in premaster, where first 2 bytes hold the |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6408 | * length of the other key. |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 6409 | */ |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6410 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 6411 | /* For RSA-PSK other key length is always 48 bytes. */ |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6412 | other_secret_len = 48; |
| 6413 | other_secret = handshake->premaster + 2; |
| 6414 | break; |
| 6415 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6416 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6417 | other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0); |
| 6418 | other_secret = handshake->premaster + 2; |
| 6419 | break; |
| 6420 | default: |
| 6421 | break; |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 6422 | } |
| 6423 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6424 | status = setup_psa_key_derivation(&derivation, psk, alg, |
| 6425 | ssl->conf->psk, ssl->conf->psk_len, |
| 6426 | seed, seed_len, |
| 6427 | (unsigned char const *) lbl, |
| 6428 | (size_t) strlen(lbl), |
| 6429 | other_secret, other_secret_len, |
| 6430 | master_secret_len); |
| 6431 | if (status != PSA_SUCCESS) { |
| 6432 | psa_key_derivation_abort(&derivation); |
| 6433 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6434 | } |
| 6435 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6436 | status = psa_key_derivation_output_bytes(&derivation, |
| 6437 | master, |
| 6438 | master_secret_len); |
| 6439 | if (status != PSA_SUCCESS) { |
| 6440 | psa_key_derivation_abort(&derivation); |
| 6441 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6442 | } |
| 6443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6444 | status = psa_key_derivation_abort(&derivation); |
| 6445 | if (status != PSA_SUCCESS) { |
| 6446 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 6447 | } |
| 6448 | } else |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6449 | #endif |
| 6450 | { |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 6451 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6452 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 6453 | if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) { |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 6454 | psa_status_t status; |
| 6455 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 6456 | psa_key_derivation_operation_t derivation = |
| 6457 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 6458 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6459 | MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE")); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 6460 | |
| 6461 | handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE; |
| 6462 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6463 | status = psa_key_derivation_setup(&derivation, alg); |
| 6464 | if (status != PSA_SUCCESS) { |
| 6465 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 6466 | } |
| 6467 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6468 | status = psa_key_derivation_set_capacity(&derivation, |
| 6469 | PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE); |
| 6470 | if (status != PSA_SUCCESS) { |
| 6471 | psa_key_derivation_abort(&derivation); |
| 6472 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 6473 | } |
| 6474 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6475 | status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx, |
| 6476 | &derivation); |
| 6477 | if (status != PSA_SUCCESS) { |
| 6478 | psa_key_derivation_abort(&derivation); |
| 6479 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 6480 | } |
| 6481 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6482 | status = psa_key_derivation_output_bytes(&derivation, |
| 6483 | handshake->premaster, |
| 6484 | handshake->pmslen); |
| 6485 | if (status != PSA_SUCCESS) { |
| 6486 | psa_key_derivation_abort(&derivation); |
| 6487 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 6488 | } |
| 6489 | |
| 6490 | status = psa_key_derivation_abort(&derivation); |
| 6491 | if (status != PSA_SUCCESS) { |
| 6492 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 6493 | } |
| 6494 | } |
| 6495 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6496 | ret = handshake->tls_prf(handshake->premaster, handshake->pmslen, |
| 6497 | lbl, seed, seed_len, |
| 6498 | master, |
| 6499 | master_secret_len); |
| 6500 | if (ret != 0) { |
| 6501 | MBEDTLS_SSL_DEBUG_RET(1, "prf", ret); |
| 6502 | return ret; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6503 | } |
| 6504 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6505 | MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret", |
| 6506 | handshake->premaster, |
| 6507 | handshake->pmslen); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6508 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6509 | mbedtls_platform_zeroize(handshake->premaster, |
| 6510 | sizeof(handshake->premaster)); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6511 | } |
| 6512 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6513 | return 0; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6514 | } |
| 6515 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6516 | int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl) |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6517 | { |
| 6518 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6519 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 6520 | ssl->handshake->ciphersuite_info; |
| 6521 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6522 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys")); |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6523 | |
| 6524 | /* Set PRF, calc_verify and calc_finished function pointers */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6525 | ret = ssl_set_handshake_prfs(ssl->handshake, |
| 6526 | ciphersuite_info->mac); |
| 6527 | if (ret != 0) { |
| 6528 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret); |
| 6529 | return ret; |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6530 | } |
| 6531 | |
| 6532 | /* Compute master secret if needed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6533 | ret = ssl_compute_master(ssl->handshake, |
| 6534 | ssl->session_negotiate->master, |
| 6535 | ssl); |
| 6536 | if (ret != 0) { |
| 6537 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret); |
| 6538 | return ret; |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6539 | } |
| 6540 | |
| 6541 | /* Swap the client and server random values: |
| 6542 | * - MS derivation wanted client+server (RFC 5246 8.1) |
| 6543 | * - key derivation wants server+client (RFC 5246 6.3) */ |
| 6544 | { |
| 6545 | unsigned char tmp[64]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6546 | memcpy(tmp, ssl->handshake->randbytes, 64); |
| 6547 | memcpy(ssl->handshake->randbytes, tmp + 32, 32); |
| 6548 | memcpy(ssl->handshake->randbytes + 32, tmp, 32); |
| 6549 | mbedtls_platform_zeroize(tmp, sizeof(tmp)); |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6550 | } |
| 6551 | |
| 6552 | /* Populate transform structure */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6553 | ret = ssl_tls12_populate_transform(ssl->transform_negotiate, |
| 6554 | ssl->session_negotiate->ciphersuite, |
| 6555 | ssl->session_negotiate->master, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 6556 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6557 | ssl->session_negotiate->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 6558 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6559 | ssl->handshake->tls_prf, |
| 6560 | ssl->handshake->randbytes, |
| 6561 | ssl->tls_version, |
| 6562 | ssl->conf->endpoint, |
| 6563 | ssl); |
| 6564 | if (ret != 0) { |
| 6565 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret); |
| 6566 | return ret; |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6567 | } |
| 6568 | |
| 6569 | /* We no longer need Server/ClientHello.random values */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6570 | mbedtls_platform_zeroize(ssl->handshake->randbytes, |
| 6571 | sizeof(ssl->handshake->randbytes)); |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6572 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6573 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys")); |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6574 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6575 | return 0; |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6576 | } |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6577 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6578 | 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] | 6579 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6580 | switch (md) { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6581 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 6582 | case MBEDTLS_SSL_HASH_SHA384: |
| 6583 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 6584 | break; |
| 6585 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6586 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 6587 | case MBEDTLS_SSL_HASH_SHA256: |
| 6588 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 6589 | break; |
| 6590 | #endif |
| 6591 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6592 | return -1; |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 6593 | } |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 6594 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 6595 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 6596 | (void) ssl; |
| 6597 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6598 | return 0; |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 6599 | } |
| 6600 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6601 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 6602 | int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl, |
| 6603 | unsigned char *hash, |
| 6604 | size_t *hlen) |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6605 | { |
| 6606 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6607 | size_t hash_size; |
| 6608 | psa_status_t status; |
| 6609 | psa_hash_operation_t sha256_psa = psa_hash_operation_init(); |
| 6610 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6611 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha256")); |
| 6612 | status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa); |
| 6613 | if (status != PSA_SUCCESS) { |
Manuel Pégourié-Gonnard | b9b564e | 2023-02-06 10:06:04 +0100 | [diff] [blame] | 6614 | goto exit; |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6615 | } |
| 6616 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6617 | status = psa_hash_finish(&sha256_psa, hash, 32, &hash_size); |
| 6618 | if (status != PSA_SUCCESS) { |
Manuel Pégourié-Gonnard | b9b564e | 2023-02-06 10:06:04 +0100 | [diff] [blame] | 6619 | goto exit; |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6620 | } |
| 6621 | |
| 6622 | *hlen = 32; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6623 | MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen); |
| 6624 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify")); |
Manuel Pégourié-Gonnard | b9b564e | 2023-02-06 10:06:04 +0100 | [diff] [blame] | 6625 | |
| 6626 | exit: |
| 6627 | psa_hash_abort(&sha256_psa); |
| 6628 | return mbedtls_md_error_from_psa(status); |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6629 | #else |
Manuel Pégourié-Gonnard | b9b564e | 2023-02-06 10:06:04 +0100 | [diff] [blame] | 6630 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 6631 | mbedtls_md_context_t sha256; |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6632 | |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 6633 | mbedtls_md_init(&sha256); |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6634 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6635 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha256")); |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6636 | |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 6637 | ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0); |
| 6638 | if (ret != 0) { |
| 6639 | goto exit; |
| 6640 | } |
| 6641 | ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256); |
| 6642 | if (ret != 0) { |
| 6643 | goto exit; |
| 6644 | } |
Manuel Pégourié-Gonnard | b9b564e | 2023-02-06 10:06:04 +0100 | [diff] [blame] | 6645 | |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 6646 | ret = mbedtls_md_finish(&sha256, hash); |
Manuel Pégourié-Gonnard | b9b564e | 2023-02-06 10:06:04 +0100 | [diff] [blame] | 6647 | if (ret != 0) { |
| 6648 | goto exit; |
| 6649 | } |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6650 | |
| 6651 | *hlen = 32; |
| 6652 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6653 | MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen); |
| 6654 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify")); |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6655 | |
Manuel Pégourié-Gonnard | b9b564e | 2023-02-06 10:06:04 +0100 | [diff] [blame] | 6656 | exit: |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 6657 | mbedtls_md_free(&sha256); |
Manuel Pégourié-Gonnard | 8e176f7 | 2023-02-09 10:33:54 +0100 | [diff] [blame] | 6658 | return ret; |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6659 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6660 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6661 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */ |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6662 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6663 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 6664 | int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl, |
| 6665 | unsigned char *hash, |
| 6666 | size_t *hlen) |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6667 | { |
| 6668 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6669 | size_t hash_size; |
| 6670 | psa_status_t status; |
| 6671 | psa_hash_operation_t sha384_psa = psa_hash_operation_init(); |
| 6672 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6673 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha384")); |
| 6674 | status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa); |
| 6675 | if (status != PSA_SUCCESS) { |
Manuel Pégourié-Gonnard | b9b564e | 2023-02-06 10:06:04 +0100 | [diff] [blame] | 6676 | goto exit; |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6677 | } |
| 6678 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6679 | status = psa_hash_finish(&sha384_psa, hash, 48, &hash_size); |
| 6680 | if (status != PSA_SUCCESS) { |
Manuel Pégourié-Gonnard | b9b564e | 2023-02-06 10:06:04 +0100 | [diff] [blame] | 6681 | goto exit; |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6682 | } |
| 6683 | |
| 6684 | *hlen = 48; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6685 | MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen); |
| 6686 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify")); |
Manuel Pégourié-Gonnard | b9b564e | 2023-02-06 10:06:04 +0100 | [diff] [blame] | 6687 | |
| 6688 | exit: |
| 6689 | psa_hash_abort(&sha384_psa); |
| 6690 | return mbedtls_md_error_from_psa(status); |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6691 | #else |
Manuel Pégourié-Gonnard | b9b564e | 2023-02-06 10:06:04 +0100 | [diff] [blame] | 6692 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame^] | 6693 | mbedtls_md_context_t sha384; |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6694 | |
Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame^] | 6695 | mbedtls_md_init(&sha384); |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6696 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6697 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha384")); |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6698 | |
Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame^] | 6699 | 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] | 6700 | if (ret != 0) { |
| 6701 | goto exit; |
| 6702 | } |
Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame^] | 6703 | ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384); |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 6704 | if (ret != 0) { |
| 6705 | goto exit; |
| 6706 | } |
Manuel Pégourié-Gonnard | b9b564e | 2023-02-06 10:06:04 +0100 | [diff] [blame] | 6707 | |
Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame^] | 6708 | ret = mbedtls_md_finish(&sha384, hash); |
Manuel Pégourié-Gonnard | b9b564e | 2023-02-06 10:06:04 +0100 | [diff] [blame] | 6709 | if (ret != 0) { |
| 6710 | goto exit; |
| 6711 | } |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6712 | |
| 6713 | *hlen = 48; |
| 6714 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6715 | MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen); |
| 6716 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify")); |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6717 | |
Manuel Pégourié-Gonnard | b9b564e | 2023-02-06 10:06:04 +0100 | [diff] [blame] | 6718 | exit: |
Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame^] | 6719 | mbedtls_md_free(&sha384); |
Manuel Pégourié-Gonnard | b9b564e | 2023-02-06 10:06:04 +0100 | [diff] [blame] | 6720 | return ret; |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6721 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6722 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6723 | #endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */ |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6724 | |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6725 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 6726 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6727 | 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] | 6728 | { |
| 6729 | unsigned char *p = ssl->handshake->premaster; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6730 | unsigned char *end = p + sizeof(ssl->handshake->premaster); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6731 | const unsigned char *psk = NULL; |
| 6732 | size_t psk_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6733 | int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6734 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6735 | if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) { |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6736 | /* |
| 6737 | * This should never happen because the existence of a PSK is always |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6738 | * checked before calling this function. |
| 6739 | * |
| 6740 | * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with |
Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 6741 | * the shared secret without PSK. |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6742 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6743 | if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) { |
| 6744 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 6745 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6746 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6747 | } |
| 6748 | |
| 6749 | /* |
| 6750 | * PMS = struct { |
| 6751 | * opaque other_secret<0..2^16-1>; |
| 6752 | * opaque psk<0..2^16-1>; |
| 6753 | * }; |
| 6754 | * with "other_secret" depending on the particular key exchange |
| 6755 | */ |
| 6756 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6757 | if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) { |
| 6758 | if (end - p < 2) { |
| 6759 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 6760 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6761 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6762 | MBEDTLS_PUT_UINT16_BE(psk_len, p, 0); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6763 | p += 2; |
| 6764 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6765 | if (end < p || (size_t) (end - p) < psk_len) { |
| 6766 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 6767 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6768 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6769 | memset(p, 0, psk_len); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6770 | p += psk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6771 | } else |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6772 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 6773 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6774 | if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) { |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6775 | /* |
| 6776 | * other_secret already set by the ClientKeyExchange message, |
| 6777 | * and is 48 bytes long |
| 6778 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6779 | if (end - p < 2) { |
| 6780 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 6781 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6782 | |
| 6783 | *p++ = 0; |
| 6784 | *p++ = 48; |
| 6785 | p += 48; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6786 | } else |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6787 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 6788 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6789 | if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) { |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6790 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6791 | size_t len; |
| 6792 | |
| 6793 | /* Write length only when we know the actual value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6794 | if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx, |
| 6795 | p + 2, end - (p + 2), &len, |
| 6796 | ssl->conf->f_rng, ssl->conf->p_rng)) != 0) { |
| 6797 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret); |
| 6798 | return ret; |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6799 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6800 | MBEDTLS_PUT_UINT16_BE(len, p, 0); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6801 | p += 2 + len; |
| 6802 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6803 | MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K); |
| 6804 | } else |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6805 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6806 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6807 | if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) { |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6808 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6809 | size_t zlen; |
| 6810 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6811 | if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen, |
| 6812 | p + 2, end - (p + 2), |
| 6813 | ssl->conf->f_rng, ssl->conf->p_rng)) != 0) { |
| 6814 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret); |
| 6815 | return ret; |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6816 | } |
| 6817 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6818 | MBEDTLS_PUT_UINT16_BE(zlen, p, 0); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6819 | p += 2 + zlen; |
| 6820 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6821 | MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx, |
| 6822 | MBEDTLS_DEBUG_ECDH_Z); |
| 6823 | } else |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6824 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6825 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6826 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 6827 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6828 | } |
| 6829 | |
| 6830 | /* opaque psk<0..2^16-1>; */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6831 | if (end - p < 2) { |
| 6832 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 6833 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6834 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6835 | MBEDTLS_PUT_UINT16_BE(psk_len, p, 0); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6836 | p += 2; |
| 6837 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6838 | if (end < p || (size_t) (end - p) < psk_len) { |
| 6839 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 6840 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6841 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6842 | memcpy(p, psk, psk_len); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6843 | p += psk_len; |
| 6844 | |
| 6845 | ssl->handshake->pmslen = p - ssl->handshake->premaster; |
| 6846 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6847 | return 0; |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6848 | } |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6849 | #endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6850 | |
| 6851 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6852 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6853 | static int ssl_write_hello_request(mbedtls_ssl_context *ssl); |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6854 | |
| 6855 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6856 | int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl) |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6857 | { |
| 6858 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 6859 | * timeout if we were using the usual handshake doubling scheme */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6860 | if (ssl->conf->renego_max_records < 0) { |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6861 | uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1; |
| 6862 | unsigned char doublings = 1; |
| 6863 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6864 | while (ratio != 0) { |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6865 | ++doublings; |
| 6866 | ratio >>= 1; |
| 6867 | } |
| 6868 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6869 | if (++ssl->renego_records_seen > doublings) { |
| 6870 | MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request")); |
| 6871 | return 0; |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6872 | } |
| 6873 | } |
| 6874 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6875 | return ssl_write_hello_request(ssl); |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6876 | } |
| 6877 | #endif |
| 6878 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6879 | |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6880 | /* |
| 6881 | * Handshake functions |
| 6882 | */ |
| 6883 | #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 6884 | /* No certificate support -> dummy functions */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6885 | int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6886 | { |
| 6887 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6888 | ssl->handshake->ciphersuite_info; |
| 6889 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6890 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6891 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6892 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { |
| 6893 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6894 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6895 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6896 | } |
| 6897 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6898 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 6899 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6900 | } |
| 6901 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6902 | int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6903 | { |
| 6904 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6905 | ssl->handshake->ciphersuite_info; |
| 6906 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6907 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6908 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6909 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { |
| 6910 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6911 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6912 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6913 | } |
| 6914 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6915 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 6916 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6917 | } |
| 6918 | |
| 6919 | #else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 6920 | /* Some certificate support -> implement write and parse */ |
| 6921 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6922 | int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6923 | { |
| 6924 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 6925 | size_t i, n; |
| 6926 | const mbedtls_x509_crt *crt; |
| 6927 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6928 | ssl->handshake->ciphersuite_info; |
| 6929 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6930 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6931 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6932 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { |
| 6933 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6934 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6935 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6936 | } |
| 6937 | |
| 6938 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6939 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 6940 | if (ssl->handshake->client_auth == 0) { |
| 6941 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6942 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6943 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6944 | } |
| 6945 | } |
| 6946 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 6947 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6948 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 6949 | if (mbedtls_ssl_own_cert(ssl) == NULL) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6950 | /* Should never happen because we shouldn't have picked the |
| 6951 | * ciphersuite if we don't have a certificate. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6952 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6953 | } |
| 6954 | } |
| 6955 | #endif |
| 6956 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6957 | MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl)); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6958 | |
| 6959 | /* |
| 6960 | * 0 . 0 handshake type |
| 6961 | * 1 . 3 handshake length |
| 6962 | * 4 . 6 length of all certs |
| 6963 | * 7 . 9 length of cert. 1 |
| 6964 | * 10 . n-1 peer certificate |
| 6965 | * n . n+2 length of cert. 2 |
| 6966 | * n+3 . ... upper level cert, etc. |
| 6967 | */ |
| 6968 | i = 7; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6969 | crt = mbedtls_ssl_own_cert(ssl); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6970 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6971 | while (crt != NULL) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6972 | n = crt->raw.len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6973 | if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) { |
| 6974 | MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET |
| 6975 | " > %" MBEDTLS_PRINTF_SIZET, |
| 6976 | i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN)); |
| 6977 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6978 | } |
| 6979 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6980 | ssl->out_msg[i] = MBEDTLS_BYTE_2(n); |
| 6981 | ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n); |
| 6982 | ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6983 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6984 | i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6985 | i += n; crt = crt->next; |
| 6986 | } |
| 6987 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6988 | ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7); |
| 6989 | ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7); |
| 6990 | ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6991 | |
| 6992 | ssl->out_msglen = i; |
| 6993 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 6994 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
| 6995 | |
| 6996 | ssl->state++; |
| 6997 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6998 | if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { |
| 6999 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret); |
| 7000 | return ret; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7001 | } |
| 7002 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7003 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7004 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7005 | return ret; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7006 | } |
| 7007 | |
| 7008 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 7009 | |
| 7010 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7011 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7012 | static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl, |
| 7013 | unsigned char *crt_buf, |
| 7014 | size_t crt_buf_len) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7015 | { |
| 7016 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; |
| 7017 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7018 | if (peer_crt == NULL) { |
| 7019 | return -1; |
| 7020 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7021 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7022 | if (peer_crt->raw.len != crt_buf_len) { |
| 7023 | return -1; |
| 7024 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7025 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7026 | return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7027 | } |
| 7028 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7029 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7030 | static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl, |
| 7031 | unsigned char *crt_buf, |
| 7032 | size_t crt_buf_len) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7033 | { |
| 7034 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7035 | unsigned char const * const peer_cert_digest = |
| 7036 | ssl->session->peer_cert_digest; |
| 7037 | mbedtls_md_type_t const peer_cert_digest_type = |
| 7038 | ssl->session->peer_cert_digest_type; |
| 7039 | mbedtls_md_info_t const * const digest_info = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7040 | mbedtls_md_info_from_type(peer_cert_digest_type); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7041 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; |
| 7042 | size_t digest_len; |
| 7043 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7044 | if (peer_cert_digest == NULL || digest_info == NULL) { |
| 7045 | return -1; |
| 7046 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7047 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7048 | digest_len = mbedtls_md_get_size(digest_info); |
| 7049 | if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) { |
| 7050 | return -1; |
| 7051 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7052 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7053 | ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest); |
| 7054 | if (ret != 0) { |
| 7055 | return -1; |
| 7056 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7057 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7058 | return memcmp(tmp_digest, peer_cert_digest, digest_len); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7059 | } |
| 7060 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7061 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 7062 | |
| 7063 | /* |
| 7064 | * Once the certificate message is read, parse it into a cert chain and |
| 7065 | * perform basic checks, but leave actual verification to the caller |
| 7066 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7067 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7068 | static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl, |
| 7069 | mbedtls_x509_crt *chain) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7070 | { |
| 7071 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7072 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7073 | int crt_cnt = 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7074 | #endif |
| 7075 | size_t i, n; |
| 7076 | uint8_t alert; |
| 7077 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7078 | if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) { |
| 7079 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7080 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7081 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
| 7082 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7083 | } |
| 7084 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7085 | if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) { |
| 7086 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7087 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
| 7088 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7089 | } |
| 7090 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7091 | if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) { |
| 7092 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7093 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7094 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
| 7095 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7096 | } |
| 7097 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7098 | i = mbedtls_ssl_hs_hdr_len(ssl); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7099 | |
| 7100 | /* |
| 7101 | * Same message structure as in mbedtls_ssl_write_certificate() |
| 7102 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7103 | n = (ssl->in_msg[i+1] << 8) | ssl->in_msg[i+2]; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7104 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7105 | if (ssl->in_msg[i] != 0 || |
| 7106 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) { |
| 7107 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7108 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7109 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
| 7110 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7111 | } |
| 7112 | |
| 7113 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 7114 | i += 3; |
| 7115 | |
| 7116 | /* Iterate through and parse the CRTs in the provided chain. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7117 | while (i < ssl->in_hslen) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7118 | /* Check that there's room for the next CRT's length fields. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7119 | if (i + 3 > ssl->in_hslen) { |
| 7120 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7121 | mbedtls_ssl_send_alert_message(ssl, |
| 7122 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7123 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
| 7124 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7125 | } |
| 7126 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 7127 | * anything beyond 2**16 ~ 64K. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7128 | if (ssl->in_msg[i] != 0) { |
| 7129 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7130 | mbedtls_ssl_send_alert_message(ssl, |
| 7131 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7132 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT); |
| 7133 | return MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7134 | } |
| 7135 | |
| 7136 | /* Read length of the next CRT in the chain. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7137 | n = ((unsigned int) ssl->in_msg[i + 1] << 8) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7138 | | (unsigned int) ssl->in_msg[i + 2]; |
| 7139 | i += 3; |
| 7140 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7141 | if (n < 128 || i + n > ssl->in_hslen) { |
| 7142 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7143 | mbedtls_ssl_send_alert_message(ssl, |
| 7144 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7145 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
| 7146 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7147 | } |
| 7148 | |
| 7149 | /* Check if we're handling the first CRT in the chain. */ |
| 7150 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7151 | if (crt_cnt++ == 0 && |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7152 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7153 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7154 | /* During client-side renegotiation, check that the server's |
| 7155 | * end-CRTs hasn't changed compared to the initial handshake, |
| 7156 | * mitigating the triple handshake attack. On success, reuse |
| 7157 | * the original end-CRT instead of parsing it again. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7158 | MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation")); |
| 7159 | if (ssl_check_peer_crt_unchanged(ssl, |
| 7160 | &ssl->in_msg[i], |
| 7161 | n) != 0) { |
| 7162 | MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation")); |
| 7163 | mbedtls_ssl_send_alert_message(ssl, |
| 7164 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7165 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED); |
| 7166 | return MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7167 | } |
| 7168 | |
| 7169 | /* Now we can safely free the original chain. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7170 | ssl_clear_peer_cert(ssl->session); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7171 | } |
| 7172 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 7173 | |
| 7174 | /* Parse the next certificate in the chain. */ |
| 7175 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7176 | ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7177 | #else |
| 7178 | /* If we don't need to store the CRT chain permanently, parse |
| 7179 | * it in-place from the input buffer instead of making a copy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7180 | 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] | 7181 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7182 | switch (ret) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7183 | case 0: /*ok*/ |
| 7184 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 7185 | /* Ignore certificate with an unknown algorithm: maybe a |
| 7186 | prior certificate was already trusted. */ |
| 7187 | break; |
| 7188 | |
| 7189 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 7190 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 7191 | goto crt_parse_der_failed; |
| 7192 | |
| 7193 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 7194 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7195 | goto crt_parse_der_failed; |
| 7196 | |
| 7197 | default: |
| 7198 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7199 | crt_parse_der_failed: |
| 7200 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert); |
| 7201 | MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret); |
| 7202 | return ret; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7203 | } |
| 7204 | |
| 7205 | i += n; |
| 7206 | } |
| 7207 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7208 | MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain); |
| 7209 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7210 | } |
| 7211 | |
| 7212 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7213 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7214 | 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] | 7215 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7216 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 7217 | return -1; |
| 7218 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7219 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7220 | if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) && |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7221 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 7222 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7223 | memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) { |
| 7224 | MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate")); |
| 7225 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7226 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7227 | return -1; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7228 | } |
| 7229 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7230 | |
| 7231 | /* Check if a certificate message is expected. |
| 7232 | * Return either |
| 7233 | * - SSL_CERTIFICATE_EXPECTED, or |
| 7234 | * - SSL_CERTIFICATE_SKIP |
| 7235 | * indicating whether a Certificate message is expected or not. |
| 7236 | */ |
| 7237 | #define SSL_CERTIFICATE_EXPECTED 0 |
| 7238 | #define SSL_CERTIFICATE_SKIP 1 |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7239 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7240 | static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl, |
| 7241 | int authmode) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7242 | { |
| 7243 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 7244 | ssl->handshake->ciphersuite_info; |
| 7245 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7246 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { |
| 7247 | return SSL_CERTIFICATE_SKIP; |
| 7248 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7249 | |
| 7250 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7251 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 7252 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) { |
| 7253 | return SSL_CERTIFICATE_SKIP; |
| 7254 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7256 | if (authmode == MBEDTLS_SSL_VERIFY_NONE) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7257 | ssl->session_negotiate->verify_result = |
| 7258 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7259 | return SSL_CERTIFICATE_SKIP; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7260 | } |
| 7261 | } |
| 7262 | #else |
| 7263 | ((void) authmode); |
| 7264 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7266 | return SSL_CERTIFICATE_EXPECTED; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7267 | } |
| 7268 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7269 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7270 | static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl, |
| 7271 | int authmode, |
| 7272 | mbedtls_x509_crt *chain, |
| 7273 | void *rs_ctx) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7274 | { |
| 7275 | int ret = 0; |
| 7276 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 7277 | ssl->handshake->ciphersuite_info; |
| 7278 | int have_ca_chain = 0; |
| 7279 | |
| 7280 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); |
| 7281 | void *p_vrfy; |
| 7282 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7283 | if (authmode == MBEDTLS_SSL_VERIFY_NONE) { |
| 7284 | return 0; |
| 7285 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7286 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7287 | if (ssl->f_vrfy != NULL) { |
| 7288 | MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7289 | f_vrfy = ssl->f_vrfy; |
| 7290 | p_vrfy = ssl->p_vrfy; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7291 | } else { |
| 7292 | MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7293 | f_vrfy = ssl->conf->f_vrfy; |
| 7294 | p_vrfy = ssl->conf->p_vrfy; |
| 7295 | } |
| 7296 | |
| 7297 | /* |
| 7298 | * Main check: verify certificate |
| 7299 | */ |
| 7300 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7301 | if (ssl->conf->f_ca_cb != NULL) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7302 | ((void) rs_ctx); |
| 7303 | have_ca_chain = 1; |
| 7304 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7305 | MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7306 | ret = mbedtls_x509_crt_verify_with_ca_cb( |
| 7307 | chain, |
| 7308 | ssl->conf->f_ca_cb, |
| 7309 | ssl->conf->p_ca_cb, |
| 7310 | ssl->conf->cert_profile, |
| 7311 | ssl->hostname, |
| 7312 | &ssl->session_negotiate->verify_result, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7313 | f_vrfy, p_vrfy); |
| 7314 | } else |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7315 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 7316 | { |
| 7317 | mbedtls_x509_crt *ca_chain; |
| 7318 | mbedtls_x509_crl *ca_crl; |
| 7319 | |
| 7320 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7321 | if (ssl->handshake->sni_ca_chain != NULL) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7322 | ca_chain = ssl->handshake->sni_ca_chain; |
| 7323 | ca_crl = ssl->handshake->sni_ca_crl; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7324 | } else |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7325 | #endif |
| 7326 | { |
| 7327 | ca_chain = ssl->conf->ca_chain; |
| 7328 | ca_crl = ssl->conf->ca_crl; |
| 7329 | } |
| 7330 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7331 | if (ca_chain != NULL) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7332 | have_ca_chain = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7333 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7334 | |
| 7335 | ret = mbedtls_x509_crt_verify_restartable( |
| 7336 | chain, |
| 7337 | ca_chain, ca_crl, |
| 7338 | ssl->conf->cert_profile, |
| 7339 | ssl->hostname, |
| 7340 | &ssl->session_negotiate->verify_result, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7341 | f_vrfy, p_vrfy, rs_ctx); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7342 | } |
| 7343 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7344 | if (ret != 0) { |
| 7345 | MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7346 | } |
| 7347 | |
| 7348 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7349 | if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) { |
| 7350 | return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; |
| 7351 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7352 | #endif |
| 7353 | |
| 7354 | /* |
| 7355 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 7356 | */ |
| 7357 | |
| 7358 | #if defined(MBEDTLS_ECP_C) |
| 7359 | { |
| 7360 | const mbedtls_pk_context *pk = &chain->pk; |
| 7361 | |
Manuel Pégourié-Gonnard | 66b0d61 | 2022-06-17 10:49:29 +0200 | [diff] [blame] | 7362 | /* If certificate uses an EC key, make sure the curve is OK. |
| 7363 | * This is a public key, so it can't be opaque, so can_do() is a good |
| 7364 | * enough check to ensure pk_ec() is safe to use here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7365 | if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY)) { |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 7366 | /* and in the unlikely case the above assumption no longer holds |
| 7367 | * we are making sure that pk_ec() here does not return a NULL |
| 7368 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7369 | const mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*pk); |
| 7370 | if (ec == NULL) { |
| 7371 | MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_pk_ec() returned NULL")); |
| 7372 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 7373 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7374 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7375 | if (mbedtls_ssl_check_curve(ssl, ec->grp.id) != 0) { |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 7376 | ssl->session_negotiate->verify_result |= |
| 7377 | MBEDTLS_X509_BADCERT_BAD_KEY; |
| 7378 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7379 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)")); |
| 7380 | if (ret == 0) { |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 7381 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7382 | } |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 7383 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7384 | } |
| 7385 | } |
| 7386 | #endif /* MBEDTLS_ECP_C */ |
| 7387 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7388 | if (mbedtls_ssl_check_cert_usage(chain, |
| 7389 | ciphersuite_info, |
| 7390 | !ssl->conf->endpoint, |
| 7391 | &ssl->session_negotiate->verify_result) != 0) { |
| 7392 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)")); |
| 7393 | if (ret == 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7394 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7395 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7396 | } |
| 7397 | |
| 7398 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a |
| 7399 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, |
| 7400 | * with details encoded in the verification flags. All other kinds |
| 7401 | * of error codes, including those from the user provided f_vrfy |
| 7402 | * functions, are treated as fatal and lead to a failure of |
| 7403 | * ssl_parse_certificate even if verification was optional. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7404 | if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
| 7405 | (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
| 7406 | ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7407 | ret = 0; |
| 7408 | } |
| 7409 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7410 | if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) { |
| 7411 | MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7412 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
| 7413 | } |
| 7414 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7415 | if (ret != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7416 | uint8_t alert; |
| 7417 | |
| 7418 | /* The certificate may have been rejected for several reasons. |
| 7419 | Pick one and send the corresponding alert. Which alert to send |
| 7420 | may be a subject of debate in some cases. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7421 | if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7422 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7423 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7424 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7425 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7426 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7427 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7428 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7429 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7430 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7431 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7432 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7433 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7434 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7435 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7436 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7437 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7438 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7439 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7440 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7441 | } else { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7442 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7443 | } |
| 7444 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7445 | alert); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7446 | } |
| 7447 | |
| 7448 | #if defined(MBEDTLS_DEBUG_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7449 | if (ssl->session_negotiate->verify_result != 0) { |
| 7450 | MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x", |
| 7451 | (unsigned int) ssl->session_negotiate->verify_result)); |
| 7452 | } else { |
| 7453 | MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7454 | } |
| 7455 | #endif /* MBEDTLS_DEBUG_C */ |
| 7456 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7457 | return ret; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7458 | } |
| 7459 | |
| 7460 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7461 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7462 | static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl, |
| 7463 | unsigned char *start, size_t len) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7464 | { |
| 7465 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7466 | /* Remember digest of the peer's end-CRT. */ |
| 7467 | ssl->session_negotiate->peer_cert_digest = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7468 | mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN); |
| 7469 | if (ssl->session_negotiate->peer_cert_digest == NULL) { |
| 7470 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed", |
| 7471 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN)); |
| 7472 | mbedtls_ssl_send_alert_message(ssl, |
| 7473 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7474 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7475 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7476 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7477 | } |
| 7478 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7479 | ret = mbedtls_md(mbedtls_md_info_from_type( |
| 7480 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE), |
| 7481 | start, len, |
| 7482 | ssl->session_negotiate->peer_cert_digest); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7483 | |
| 7484 | ssl->session_negotiate->peer_cert_digest_type = |
| 7485 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 7486 | ssl->session_negotiate->peer_cert_digest_len = |
| 7487 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 7488 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7489 | return ret; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7490 | } |
| 7491 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7492 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7493 | static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl, |
| 7494 | unsigned char *start, size_t len) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7495 | { |
| 7496 | unsigned char *end = start + len; |
| 7497 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7498 | |
| 7499 | /* Make a copy of the peer's raw public key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7500 | mbedtls_pk_init(&ssl->handshake->peer_pubkey); |
| 7501 | ret = mbedtls_pk_parse_subpubkey(&start, end, |
| 7502 | &ssl->handshake->peer_pubkey); |
| 7503 | if (ret != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7504 | /* We should have parsed the public key before. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7505 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7506 | } |
| 7507 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7508 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7509 | } |
| 7510 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7511 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7512 | int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7513 | { |
| 7514 | int ret = 0; |
| 7515 | int crt_expected; |
| 7516 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 7517 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 7518 | ? ssl->handshake->sni_authmode |
| 7519 | : ssl->conf->authmode; |
| 7520 | #else |
| 7521 | const int authmode = ssl->conf->authmode; |
| 7522 | #endif |
| 7523 | void *rs_ctx = NULL; |
| 7524 | mbedtls_x509_crt *chain = NULL; |
| 7525 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7526 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7527 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7528 | crt_expected = ssl_parse_certificate_coordinate(ssl, authmode); |
| 7529 | if (crt_expected == SSL_CERTIFICATE_SKIP) { |
| 7530 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7531 | goto exit; |
| 7532 | } |
| 7533 | |
| 7534 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7535 | if (ssl->handshake->ecrs_enabled && |
| 7536 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7537 | chain = ssl->handshake->ecrs_peer_cert; |
| 7538 | ssl->handshake->ecrs_peer_cert = NULL; |
| 7539 | goto crt_verify; |
| 7540 | } |
| 7541 | #endif |
| 7542 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7543 | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7544 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 7545 | let it decide whether to alert. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7546 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7547 | goto exit; |
| 7548 | } |
| 7549 | |
| 7550 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7551 | if (ssl_srv_check_client_no_crt_notification(ssl) == 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7552 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
| 7553 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7554 | if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7555 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7556 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7557 | |
| 7558 | goto exit; |
| 7559 | } |
| 7560 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7561 | |
| 7562 | /* Clear existing peer CRT structure in case we tried to |
| 7563 | * reuse a session but it failed, and allocate a new one. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7564 | ssl_clear_peer_cert(ssl->session_negotiate); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7565 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7566 | chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt)); |
| 7567 | if (chain == NULL) { |
| 7568 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", |
| 7569 | sizeof(mbedtls_x509_crt))); |
| 7570 | mbedtls_ssl_send_alert_message(ssl, |
| 7571 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7572 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7573 | |
| 7574 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 7575 | goto exit; |
| 7576 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7577 | mbedtls_x509_crt_init(chain); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7578 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7579 | ret = ssl_parse_certificate_chain(ssl, chain); |
| 7580 | if (ret != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7581 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7582 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7583 | |
| 7584 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7585 | if (ssl->handshake->ecrs_enabled) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7586 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7587 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7588 | |
| 7589 | crt_verify: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7590 | if (ssl->handshake->ecrs_enabled) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7591 | rs_ctx = &ssl->handshake->ecrs_ctx; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7592 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7593 | #endif |
| 7594 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7595 | ret = ssl_parse_certificate_verify(ssl, authmode, |
| 7596 | chain, rs_ctx); |
| 7597 | if (ret != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7598 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7599 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7600 | |
| 7601 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7602 | { |
| 7603 | unsigned char *crt_start, *pk_start; |
| 7604 | size_t crt_len, pk_len; |
| 7605 | |
| 7606 | /* We parse the CRT chain without copying, so |
| 7607 | * these pointers point into the input buffer, |
| 7608 | * and are hence still valid after freeing the |
| 7609 | * CRT chain. */ |
| 7610 | |
| 7611 | crt_start = chain->raw.p; |
| 7612 | crt_len = chain->raw.len; |
| 7613 | |
| 7614 | pk_start = chain->pk_raw.p; |
| 7615 | pk_len = chain->pk_raw.len; |
| 7616 | |
| 7617 | /* Free the CRT structures before computing |
| 7618 | * digest and copying the peer's public key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7619 | mbedtls_x509_crt_free(chain); |
| 7620 | mbedtls_free(chain); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7621 | chain = NULL; |
| 7622 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7623 | ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len); |
| 7624 | if (ret != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7625 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7626 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7627 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7628 | ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len); |
| 7629 | if (ret != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7630 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7631 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7632 | } |
| 7633 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7634 | /* Pass ownership to session structure. */ |
| 7635 | ssl->session_negotiate->peer_cert = chain; |
| 7636 | chain = NULL; |
| 7637 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7638 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7639 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7640 | |
| 7641 | exit: |
| 7642 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7643 | if (ret == 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7644 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7645 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7646 | |
| 7647 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7648 | if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7649 | ssl->handshake->ecrs_peer_cert = chain; |
| 7650 | chain = NULL; |
| 7651 | } |
| 7652 | #endif |
| 7653 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7654 | if (chain != NULL) { |
| 7655 | mbedtls_x509_crt_free(chain); |
| 7656 | mbedtls_free(chain); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7657 | } |
| 7658 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7659 | return ret; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7660 | } |
| 7661 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 7662 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7663 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 7664 | static int ssl_calc_finished_tls_sha256( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7665 | mbedtls_ssl_context *ssl, unsigned char *buf, int from) |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7666 | { |
| 7667 | int len = 12; |
| 7668 | const char *sender; |
| 7669 | unsigned char padbuf[32]; |
| 7670 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7671 | size_t hash_size; |
| 7672 | psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT; |
| 7673 | psa_status_t status; |
| 7674 | #else |
Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 7675 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 7676 | mbedtls_md_context_t sha256; |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7677 | #endif |
| 7678 | |
| 7679 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7680 | if (!session) { |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7681 | session = ssl->session; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7682 | } |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7683 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7684 | sender = (from == MBEDTLS_SSL_IS_CLIENT) |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7685 | ? "client finished" |
| 7686 | : "server finished"; |
| 7687 | |
| 7688 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7689 | sha256_psa = psa_hash_operation_init(); |
| 7690 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7691 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha256")); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7692 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7693 | status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa); |
| 7694 | if (status != PSA_SUCCESS) { |
Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 7695 | goto exit; |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7696 | } |
| 7697 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7698 | status = psa_hash_finish(&sha256_psa, padbuf, sizeof(padbuf), &hash_size); |
| 7699 | if (status != PSA_SUCCESS) { |
Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 7700 | goto exit; |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7701 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7702 | MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 32); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7703 | #else |
| 7704 | |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 7705 | mbedtls_md_init(&sha256); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7706 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7707 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha256")); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7708 | |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 7709 | ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0); |
| 7710 | if (ret != 0) { |
| 7711 | goto exit; |
| 7712 | } |
| 7713 | ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256); |
| 7714 | if (ret != 0) { |
| 7715 | goto exit; |
| 7716 | } |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7717 | |
| 7718 | /* |
| 7719 | * TLSv1.2: |
| 7720 | * hash = PRF( master, finished_label, |
| 7721 | * Hash( handshake ) )[0.11] |
| 7722 | */ |
| 7723 | |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 7724 | ret = mbedtls_md_finish(&sha256, padbuf); |
Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 7725 | if (ret != 0) { |
| 7726 | goto exit; |
| 7727 | } |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7728 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7729 | |
Manuel Pégourié-Gonnard | 0ac71c0 | 2023-02-24 12:13:55 +0100 | [diff] [blame] | 7730 | MBEDTLS_SSL_DEBUG_BUF(4, "finished sha256 output", padbuf, 32); |
| 7731 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7732 | ssl->handshake->tls_prf(session->master, 48, sender, |
| 7733 | padbuf, 32, buf, len); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7734 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7735 | MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7736 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7737 | mbedtls_platform_zeroize(padbuf, sizeof(padbuf)); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7738 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7739 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished")); |
Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 7740 | |
| 7741 | exit: |
| 7742 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7743 | psa_hash_abort(&sha256_psa); |
| 7744 | return mbedtls_md_error_from_psa(status); |
| 7745 | #else |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 7746 | mbedtls_md_free(&sha256); |
Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 7747 | return ret; |
| 7748 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7749 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 7750 | #endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7751 | |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7752 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7753 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame] | 7754 | static int ssl_calc_finished_tls_sha384( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7755 | mbedtls_ssl_context *ssl, unsigned char *buf, int from) |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7756 | { |
| 7757 | int len = 12; |
| 7758 | const char *sender; |
| 7759 | unsigned char padbuf[48]; |
| 7760 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7761 | size_t hash_size; |
| 7762 | psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT; |
| 7763 | psa_status_t status; |
| 7764 | #else |
Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 7765 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame^] | 7766 | mbedtls_md_context_t sha384; |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7767 | #endif |
| 7768 | |
| 7769 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7770 | if (!session) { |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7771 | session = ssl->session; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7772 | } |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7773 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7774 | sender = (from == MBEDTLS_SSL_IS_CLIENT) |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7775 | ? "client finished" |
| 7776 | : "server finished"; |
| 7777 | |
| 7778 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7779 | sha384_psa = psa_hash_operation_init(); |
| 7780 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7781 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha384")); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7782 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7783 | status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa); |
| 7784 | if (status != PSA_SUCCESS) { |
Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 7785 | goto exit; |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7786 | } |
| 7787 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7788 | status = psa_hash_finish(&sha384_psa, padbuf, sizeof(padbuf), &hash_size); |
| 7789 | if (status != PSA_SUCCESS) { |
Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 7790 | goto exit; |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7791 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7792 | MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 48); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7793 | #else |
Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame^] | 7794 | mbedtls_md_init(&sha384); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7795 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7796 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha384")); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7797 | |
Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame^] | 7798 | 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] | 7799 | if (ret != 0) { |
| 7800 | goto exit; |
| 7801 | } |
Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame^] | 7802 | ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384); |
Manuel Pégourié-Gonnard | f057ecf | 2023-02-24 13:19:17 +0100 | [diff] [blame] | 7803 | if (ret != 0) { |
| 7804 | goto exit; |
| 7805 | } |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7806 | |
| 7807 | /* |
| 7808 | * TLSv1.2: |
| 7809 | * hash = PRF( master, finished_label, |
| 7810 | * Hash( handshake ) )[0.11] |
| 7811 | */ |
| 7812 | |
Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame^] | 7813 | ret = mbedtls_md_finish(&sha384, padbuf); |
Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 7814 | if (ret != 0) { |
| 7815 | goto exit; |
| 7816 | } |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7817 | #endif |
| 7818 | |
Manuel Pégourié-Gonnard | 0ac71c0 | 2023-02-24 12:13:55 +0100 | [diff] [blame] | 7819 | MBEDTLS_SSL_DEBUG_BUF(4, "finished sha384 output", padbuf, 48); |
| 7820 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7821 | ssl->handshake->tls_prf(session->master, 48, sender, |
| 7822 | padbuf, 48, buf, len); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7823 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7824 | MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7825 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7826 | mbedtls_platform_zeroize(padbuf, sizeof(padbuf)); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7827 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7828 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished")); |
Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 7829 | |
| 7830 | exit: |
| 7831 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7832 | psa_hash_abort(&sha384_psa); |
| 7833 | return mbedtls_md_error_from_psa(status); |
| 7834 | #else |
Manuel Pégourié-Gonnard | 02d55d5 | 2023-02-24 13:21:16 +0100 | [diff] [blame^] | 7835 | mbedtls_md_free(&sha384); |
Manuel Pégourié-Gonnard | e1a4caa | 2023-02-06 10:14:25 +0100 | [diff] [blame] | 7836 | return ret; |
| 7837 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7838 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 7839 | #endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/ |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7840 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7841 | void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl) |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7842 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7843 | MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free")); |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7844 | |
| 7845 | /* |
| 7846 | * Free our handshake params |
| 7847 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7848 | mbedtls_ssl_handshake_free(ssl); |
| 7849 | mbedtls_free(ssl->handshake); |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7850 | ssl->handshake = NULL; |
| 7851 | |
| 7852 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 7853 | * Free the previous transform and switch in the current one |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7854 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7855 | if (ssl->transform) { |
| 7856 | mbedtls_ssl_transform_free(ssl->transform); |
| 7857 | mbedtls_free(ssl->transform); |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7858 | } |
| 7859 | ssl->transform = ssl->transform_negotiate; |
| 7860 | ssl->transform_negotiate = NULL; |
| 7861 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7862 | MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free")); |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7863 | } |
| 7864 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7865 | void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl) |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7866 | { |
| 7867 | int resume = ssl->handshake->resume; |
| 7868 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7869 | MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup")); |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7870 | |
| 7871 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7872 | if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) { |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7873 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
| 7874 | ssl->renego_records_seen = 0; |
| 7875 | } |
| 7876 | #endif |
| 7877 | |
| 7878 | /* |
| 7879 | * Free the previous session and switch in the current one |
| 7880 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7881 | if (ssl->session) { |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7882 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 7883 | /* RFC 7366 3.1: keep the EtM state */ |
| 7884 | ssl->session_negotiate->encrypt_then_mac = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7885 | ssl->session->encrypt_then_mac; |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7886 | #endif |
| 7887 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7888 | mbedtls_ssl_session_free(ssl->session); |
| 7889 | mbedtls_free(ssl->session); |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7890 | } |
| 7891 | ssl->session = ssl->session_negotiate; |
| 7892 | ssl->session_negotiate = NULL; |
| 7893 | |
| 7894 | /* |
| 7895 | * Add cache entry |
| 7896 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7897 | if (ssl->conf->f_set_cache != NULL && |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7898 | ssl->session->id_len != 0 && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7899 | resume == 0) { |
| 7900 | if (ssl->conf->f_set_cache(ssl->conf->p_cache, |
| 7901 | ssl->session->id, |
| 7902 | ssl->session->id_len, |
| 7903 | ssl->session) != 0) { |
| 7904 | MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session")); |
| 7905 | } |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7906 | } |
| 7907 | |
| 7908 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7909 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 7910 | ssl->handshake->flight != NULL) { |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7911 | /* Cancel handshake timer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7912 | mbedtls_ssl_set_timer(ssl, 0); |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7913 | |
| 7914 | /* Keep last flight around in case we need to resend it: |
| 7915 | * we need the handshake and transform structures for that */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7916 | MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform")); |
| 7917 | } else |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7918 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7919 | mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl); |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7920 | |
Jerry Yu | 5ed73ff | 2022-10-27 13:08:42 +0800 | [diff] [blame] | 7921 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7922 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7923 | MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup")); |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7924 | } |
| 7925 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7926 | int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl) |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7927 | { |
| 7928 | int ret, hash_len; |
| 7929 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7930 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished")); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7931 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7932 | mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7933 | |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 7934 | ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint); |
| 7935 | if (ret != 0) { |
| 7936 | MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret); |
| 7937 | } |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7938 | |
| 7939 | /* |
| 7940 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 7941 | * may define some other value. Currently (early 2016), no defined |
| 7942 | * ciphersuite does this (and this is unlikely to change as activity has |
| 7943 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 7944 | */ |
| 7945 | hash_len = 12; |
| 7946 | |
| 7947 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7948 | ssl->verify_data_len = hash_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7949 | memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7950 | #endif |
| 7951 | |
| 7952 | ssl->out_msglen = 4 + hash_len; |
| 7953 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 7954 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
| 7955 | |
| 7956 | /* |
| 7957 | * In case of session resuming, invert the client and server |
| 7958 | * ChangeCipherSpec messages order. |
| 7959 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7960 | if (ssl->handshake->resume != 0) { |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7961 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7962 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7963 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7964 | } |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7965 | #endif |
| 7966 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7967 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7968 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7969 | } |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7970 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7971 | } else { |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7972 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7973 | } |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7974 | |
| 7975 | /* |
| 7976 | * Switch to our negotiated transform and session parameters for outbound |
| 7977 | * data. |
| 7978 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7979 | 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] | 7980 | |
| 7981 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7982 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7983 | unsigned char i; |
| 7984 | |
| 7985 | /* Remember current epoch settings for resending */ |
| 7986 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7987 | memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, |
| 7988 | sizeof(ssl->handshake->alt_out_ctr)); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7989 | |
| 7990 | /* Set sequence_number to zero */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7991 | 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] | 7992 | |
| 7993 | |
| 7994 | /* Increment epoch */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7995 | for (i = 2; i > 0; i--) { |
| 7996 | if (++ssl->cur_out_ctr[i - 1] != 0) { |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7997 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7998 | } |
| 7999 | } |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8000 | |
| 8001 | /* The loop goes to its end iff the counter is wrapping */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8002 | if (i == 0) { |
| 8003 | MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap")); |
| 8004 | return MBEDTLS_ERR_SSL_COUNTER_WRAPPING; |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8005 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8006 | } else |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8007 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8008 | memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr)); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8009 | |
| 8010 | ssl->transform_out = ssl->transform_negotiate; |
| 8011 | ssl->session_out = ssl->session_negotiate; |
| 8012 | |
| 8013 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8014 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 8015 | mbedtls_ssl_send_flight_completed(ssl); |
| 8016 | } |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8017 | #endif |
| 8018 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8019 | if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { |
| 8020 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret); |
| 8021 | return ret; |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8022 | } |
| 8023 | |
| 8024 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8025 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 8026 | (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) { |
| 8027 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret); |
| 8028 | return ret; |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8029 | } |
| 8030 | #endif |
| 8031 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8032 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished")); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8033 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8034 | return 0; |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 8035 | } |
| 8036 | |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8037 | #define SSL_MAX_HASH_LEN 12 |
| 8038 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8039 | int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl) |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8040 | { |
| 8041 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 8042 | unsigned int hash_len = 12; |
| 8043 | unsigned char buf[SSL_MAX_HASH_LEN]; |
| 8044 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8045 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished")); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8046 | |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 8047 | ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1); |
| 8048 | if (ret != 0) { |
| 8049 | MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret); |
| 8050 | } |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8051 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8052 | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { |
| 8053 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8054 | goto exit; |
| 8055 | } |
| 8056 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8057 | if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) { |
| 8058 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message")); |
| 8059 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8060 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8061 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 8062 | goto exit; |
| 8063 | } |
| 8064 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8065 | if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) { |
| 8066 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8067 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8068 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 8069 | goto exit; |
| 8070 | } |
| 8071 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8072 | if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) { |
| 8073 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message")); |
| 8074 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8075 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8076 | ret = MBEDTLS_ERR_SSL_DECODE_ERROR; |
| 8077 | goto exit; |
| 8078 | } |
| 8079 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8080 | if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), |
| 8081 | buf, hash_len) != 0) { |
| 8082 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message")); |
| 8083 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8084 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8085 | ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
| 8086 | goto exit; |
| 8087 | } |
| 8088 | |
| 8089 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 8090 | ssl->verify_data_len = hash_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8091 | memcpy(ssl->peer_verify_data, buf, hash_len); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8092 | #endif |
| 8093 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8094 | if (ssl->handshake->resume != 0) { |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8095 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8096 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8097 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8098 | } |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8099 | #endif |
| 8100 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8101 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8102 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8103 | } |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8104 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8105 | } else { |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8106 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8107 | } |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8108 | |
| 8109 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8110 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 8111 | mbedtls_ssl_recv_flight_completed(ssl); |
| 8112 | } |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8113 | #endif |
| 8114 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8115 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished")); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8116 | |
| 8117 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8118 | mbedtls_platform_zeroize(buf, hash_len); |
| 8119 | return ret; |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 8120 | } |
| 8121 | |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 8122 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 8123 | /* |
| 8124 | * Helper to get TLS 1.2 PRF from ciphersuite |
| 8125 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) |
| 8126 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8127 | static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id) |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 8128 | { |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 8129 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8130 | mbedtls_ssl_ciphersuite_from_id(ciphersuite_id); |
Andrzej Kurek | 6832774 | 2022-10-03 06:18:18 -0400 | [diff] [blame] | 8131 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8132 | if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) { |
| 8133 | return tls_prf_sha384; |
| 8134 | } else |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 8135 | #endif |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 8136 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 8137 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8138 | if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) { |
| 8139 | return tls_prf_sha256; |
| 8140 | } |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 8141 | } |
| 8142 | #endif |
| 8143 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 8144 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 8145 | (void) ciphersuite_info; |
| 8146 | #endif |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 8147 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8148 | return NULL; |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 8149 | } |
| 8150 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 8151 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8152 | 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] | 8153 | { |
| 8154 | ((void) tls_prf); |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 8155 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8156 | if (tls_prf == tls_prf_sha384) { |
| 8157 | return MBEDTLS_SSL_TLS_PRF_SHA384; |
| 8158 | } else |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 8159 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 8160 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8161 | if (tls_prf == tls_prf_sha256) { |
| 8162 | return MBEDTLS_SSL_TLS_PRF_SHA256; |
| 8163 | } else |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 8164 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8165 | return MBEDTLS_SSL_TLS_PRF_NONE; |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 8166 | } |
| 8167 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8168 | /* |
| 8169 | * Populate a transform structure with session keys and all the other |
| 8170 | * necessary information. |
| 8171 | * |
| 8172 | * Parameters: |
| 8173 | * - [in/out]: transform: structure to populate |
| 8174 | * [in] must be just initialised with mbedtls_ssl_transform_init() |
| 8175 | * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf() |
| 8176 | * - [in] ciphersuite |
| 8177 | * - [in] master |
| 8178 | * - [in] encrypt_then_mac |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8179 | * - [in] tls_prf: pointer to PRF to use for key derivation |
| 8180 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 8181 | * - [in] tls_version: TLS version |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8182 | * - [in] endpoint: client or server |
| 8183 | * - [in] ssl: used for: |
| 8184 | * - ssl->conf->{f,p}_export_keys |
| 8185 | * [in] optionally used for: |
| 8186 | * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg |
| 8187 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 8188 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8189 | static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform, |
| 8190 | int ciphersuite, |
| 8191 | const unsigned char master[48], |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8192 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8193 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8194 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8195 | ssl_tls_prf_t tls_prf, |
| 8196 | const unsigned char randbytes[64], |
| 8197 | mbedtls_ssl_protocol_version tls_version, |
| 8198 | unsigned endpoint, |
| 8199 | const mbedtls_ssl_context *ssl) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8200 | { |
| 8201 | int ret = 0; |
| 8202 | unsigned char keyblk[256]; |
| 8203 | unsigned char *key1; |
| 8204 | unsigned char *key2; |
| 8205 | unsigned char *mac_enc; |
| 8206 | unsigned char *mac_dec; |
| 8207 | size_t mac_key_len = 0; |
| 8208 | size_t iv_copy_len; |
| 8209 | size_t keylen; |
| 8210 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8211 | mbedtls_ssl_mode_t ssl_mode; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8212 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8213 | const mbedtls_cipher_info_t *cipher_info; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8214 | const mbedtls_md_info_t *md_info; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8215 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8216 | |
| 8217 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8218 | psa_key_type_t key_type; |
| 8219 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8220 | psa_algorithm_t alg; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8221 | psa_algorithm_t mac_alg = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8222 | size_t key_bits; |
| 8223 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 8224 | #endif |
| 8225 | |
| 8226 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 8227 | !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8228 | if (ssl->f_export_keys == NULL) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8229 | ssl = NULL; /* make sure we don't use it except for these cases */ |
| 8230 | (void) ssl; |
| 8231 | } |
| 8232 | #endif |
| 8233 | |
| 8234 | /* |
| 8235 | * Some data just needs copying into the structure |
| 8236 | */ |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8237 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8238 | transform->encrypt_then_mac = encrypt_then_mac; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8239 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 8240 | transform->tls_version = tls_version; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8241 | |
| 8242 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8243 | memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes)); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8244 | #endif |
| 8245 | |
| 8246 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8247 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8248 | /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform |
| 8249 | * generation separate. This should never happen. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8250 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8251 | } |
| 8252 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 8253 | |
| 8254 | /* |
| 8255 | * Get various info structures |
| 8256 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8257 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite); |
| 8258 | if (ciphersuite_info == NULL) { |
| 8259 | MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found", |
| 8260 | ciphersuite)); |
| 8261 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8262 | } |
| 8263 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 8264 | ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8265 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8266 | encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8267 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8268 | ciphersuite_info); |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8269 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8270 | if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) { |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8271 | transform->taglen = |
| 8272 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8273 | } |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8274 | |
| 8275 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8276 | if ((status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher, |
| 8277 | transform->taglen, |
| 8278 | &alg, |
| 8279 | &key_type, |
| 8280 | &key_bits)) != PSA_SUCCESS) { |
| 8281 | ret = psa_ssl_status_to_mbedtls(status); |
| 8282 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8283 | goto end; |
| 8284 | } |
| 8285 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8286 | cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher); |
| 8287 | if (cipher_info == NULL) { |
| 8288 | MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found", |
| 8289 | ciphersuite_info->cipher)); |
| 8290 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8291 | } |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8292 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8293 | |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8294 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8295 | mac_alg = mbedtls_hash_info_psa_from_md(ciphersuite_info->mac); |
| 8296 | if (mac_alg == 0) { |
| 8297 | MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_hash_info_psa_from_md for %u not found", |
| 8298 | (unsigned) ciphersuite_info->mac)); |
| 8299 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8300 | } |
| 8301 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8302 | md_info = mbedtls_md_info_from_type(ciphersuite_info->mac); |
| 8303 | if (md_info == NULL) { |
| 8304 | MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found", |
| 8305 | (unsigned) ciphersuite_info->mac)); |
| 8306 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8307 | } |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8308 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8309 | |
| 8310 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 8311 | /* Copy own and peer's CID if the use of the CID |
| 8312 | * extension has been negotiated. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8313 | if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) { |
| 8314 | MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform")); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8315 | |
| 8316 | transform->in_cid_len = ssl->own_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8317 | memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len); |
| 8318 | MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid, |
| 8319 | transform->in_cid_len); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8320 | |
| 8321 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8322 | memcpy(transform->out_cid, ssl->handshake->peer_cid, |
| 8323 | ssl->handshake->peer_cid_len); |
| 8324 | MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid, |
| 8325 | transform->out_cid_len); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8326 | } |
| 8327 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 8328 | |
| 8329 | /* |
| 8330 | * Compute key block using the PRF |
| 8331 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8332 | ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256); |
| 8333 | if (ret != 0) { |
| 8334 | MBEDTLS_SSL_DEBUG_RET(1, "prf", ret); |
| 8335 | return ret; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8336 | } |
| 8337 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8338 | MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s", |
| 8339 | mbedtls_ssl_get_ciphersuite_name(ciphersuite))); |
| 8340 | MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48); |
| 8341 | MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64); |
| 8342 | MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8343 | |
| 8344 | /* |
| 8345 | * Determine the appropriate key, IV and MAC length. |
| 8346 | */ |
| 8347 | |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8348 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8349 | keylen = PSA_BITS_TO_BYTES(key_bits); |
| 8350 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8351 | keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8352 | #endif |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8353 | |
| 8354 | #if defined(MBEDTLS_GCM_C) || \ |
| 8355 | defined(MBEDTLS_CCM_C) || \ |
| 8356 | defined(MBEDTLS_CHACHAPOLY_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8357 | if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8358 | size_t explicit_ivlen; |
| 8359 | |
| 8360 | transform->maclen = 0; |
| 8361 | mac_key_len = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8362 | |
| 8363 | /* All modes haves 96-bit IVs, but the length of the static parts vary |
| 8364 | * with mode and version: |
| 8365 | * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes |
| 8366 | * (to be concatenated with a dynamically chosen IV of 8 Bytes) |
| 8367 | * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's |
| 8368 | * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record |
| 8369 | * sequence number). |
| 8370 | */ |
| 8371 | transform->ivlen = 12; |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 8372 | |
| 8373 | int is_chachapoly = 0; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8374 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8375 | is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8376 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8377 | is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info) |
| 8378 | == MBEDTLS_MODE_CHACHAPOLY); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8379 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 8380 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8381 | if (is_chachapoly) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8382 | transform->fixed_ivlen = 12; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8383 | } else { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8384 | transform->fixed_ivlen = 4; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8385 | } |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8386 | |
| 8387 | /* Minimum length of encrypted record */ |
| 8388 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
| 8389 | transform->minlen = explicit_ivlen + transform->taglen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8390 | } else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8391 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
| 8392 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8393 | if (ssl_mode == MBEDTLS_SSL_MODE_STREAM || |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8394 | ssl_mode == MBEDTLS_SSL_MODE_CBC || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8395 | ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) { |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8396 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8397 | size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8398 | #else |
| 8399 | size_t block_size = cipher_info->block_size; |
| 8400 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8401 | |
| 8402 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8403 | /* Get MAC length */ |
| 8404 | mac_key_len = PSA_HASH_LENGTH(mac_alg); |
| 8405 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8406 | /* Initialize HMAC contexts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8407 | if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 || |
| 8408 | (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) { |
| 8409 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8410 | goto end; |
| 8411 | } |
| 8412 | |
| 8413 | /* Get MAC length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8414 | mac_key_len = mbedtls_md_get_size(md_info); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8415 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8416 | transform->maclen = mac_key_len; |
| 8417 | |
| 8418 | /* IV length */ |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8419 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8420 | transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8421 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8422 | transform->ivlen = cipher_info->iv_size; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8423 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8424 | |
| 8425 | /* Minimum length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8426 | if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8427 | transform->minlen = transform->maclen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8428 | } else { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8429 | /* |
| 8430 | * GenericBlockCipher: |
| 8431 | * 1. if EtM is in use: one block plus MAC |
| 8432 | * otherwise: * first multiple of blocklen greater than maclen |
| 8433 | * 2. IV |
| 8434 | */ |
| 8435 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8436 | if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8437 | transform->minlen = transform->maclen |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8438 | + block_size; |
| 8439 | } else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8440 | #endif |
| 8441 | { |
| 8442 | transform->minlen = transform->maclen |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8443 | + block_size |
| 8444 | - transform->maclen % block_size; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8445 | } |
| 8446 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8447 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8448 | transform->minlen += transform->ivlen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8449 | } else { |
| 8450 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8451 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 8452 | goto end; |
| 8453 | } |
| 8454 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8455 | } else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8456 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 8457 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8458 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 8459 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8460 | } |
| 8461 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8462 | MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 8463 | (unsigned) keylen, |
| 8464 | (unsigned) transform->minlen, |
| 8465 | (unsigned) transform->ivlen, |
| 8466 | (unsigned) transform->maclen)); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8467 | |
| 8468 | /* |
| 8469 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 8470 | */ |
| 8471 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8472 | if (endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8473 | key1 = keyblk + mac_key_len * 2; |
| 8474 | key2 = keyblk + mac_key_len * 2 + keylen; |
| 8475 | |
| 8476 | mac_enc = keyblk; |
| 8477 | mac_dec = keyblk + mac_key_len; |
| 8478 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8479 | iv_copy_len = (transform->fixed_ivlen) ? |
| 8480 | transform->fixed_ivlen : transform->ivlen; |
| 8481 | memcpy(transform->iv_enc, key2 + keylen, iv_copy_len); |
| 8482 | memcpy(transform->iv_dec, key2 + keylen + iv_copy_len, |
| 8483 | iv_copy_len); |
| 8484 | } else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8485 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 8486 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8487 | if (endpoint == MBEDTLS_SSL_IS_SERVER) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8488 | key1 = keyblk + mac_key_len * 2 + keylen; |
| 8489 | key2 = keyblk + mac_key_len * 2; |
| 8490 | |
| 8491 | mac_enc = keyblk + mac_key_len; |
| 8492 | mac_dec = keyblk; |
| 8493 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8494 | iv_copy_len = (transform->fixed_ivlen) ? |
| 8495 | transform->fixed_ivlen : transform->ivlen; |
| 8496 | memcpy(transform->iv_dec, key1 + keylen, iv_copy_len); |
| 8497 | memcpy(transform->iv_enc, key1 + keylen + iv_copy_len, |
| 8498 | iv_copy_len); |
| 8499 | } else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8500 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 8501 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8502 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8503 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 8504 | goto end; |
| 8505 | } |
| 8506 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8507 | if (ssl != NULL && ssl->f_export_keys != NULL) { |
| 8508 | ssl->f_export_keys(ssl->p_export_keys, |
| 8509 | MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET, |
| 8510 | master, 48, |
| 8511 | randbytes + 32, |
| 8512 | randbytes, |
| 8513 | tls_prf_get_type(tls_prf)); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8514 | } |
| 8515 | |
| 8516 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8517 | transform->psa_alg = alg; |
| 8518 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8519 | if (alg != MBEDTLS_SSL_NULL_CIPHER) { |
| 8520 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 8521 | psa_set_key_algorithm(&attributes, alg); |
| 8522 | psa_set_key_type(&attributes, key_type); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8523 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8524 | if ((status = psa_import_key(&attributes, |
| 8525 | key1, |
| 8526 | PSA_BITS_TO_BYTES(key_bits), |
| 8527 | &transform->psa_key_enc)) != PSA_SUCCESS) { |
| 8528 | MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status); |
| 8529 | ret = psa_ssl_status_to_mbedtls(status); |
| 8530 | MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8531 | goto end; |
| 8532 | } |
| 8533 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8534 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8535 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8536 | if ((status = psa_import_key(&attributes, |
| 8537 | key2, |
| 8538 | PSA_BITS_TO_BYTES(key_bits), |
| 8539 | &transform->psa_key_dec)) != PSA_SUCCESS) { |
| 8540 | ret = psa_ssl_status_to_mbedtls(status); |
| 8541 | MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8542 | goto end; |
| 8543 | } |
| 8544 | } |
| 8545 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8546 | if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc, |
| 8547 | cipher_info)) != 0) { |
| 8548 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8549 | goto end; |
| 8550 | } |
| 8551 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8552 | if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec, |
| 8553 | cipher_info)) != 0) { |
| 8554 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8555 | goto end; |
| 8556 | } |
| 8557 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8558 | if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1, |
| 8559 | (int) mbedtls_cipher_info_get_key_bitlen(cipher_info), |
| 8560 | MBEDTLS_ENCRYPT)) != 0) { |
| 8561 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8562 | goto end; |
| 8563 | } |
| 8564 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8565 | if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2, |
| 8566 | (int) mbedtls_cipher_info_get_key_bitlen(cipher_info), |
| 8567 | MBEDTLS_DECRYPT)) != 0) { |
| 8568 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8569 | goto end; |
| 8570 | } |
| 8571 | |
| 8572 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8573 | if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) { |
| 8574 | if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc, |
| 8575 | MBEDTLS_PADDING_NONE)) != 0) { |
| 8576 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8577 | goto end; |
| 8578 | } |
| 8579 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8580 | if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec, |
| 8581 | MBEDTLS_PADDING_NONE)) != 0) { |
| 8582 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8583 | goto end; |
| 8584 | } |
| 8585 | } |
| 8586 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 8587 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8588 | |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8589 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 8590 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 8591 | For AEAD-based ciphersuites, there is nothing to do here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8592 | if (mac_key_len != 0) { |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8593 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8594 | transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8595 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8596 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 8597 | psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg)); |
| 8598 | psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8599 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8600 | if ((status = psa_import_key(&attributes, |
| 8601 | mac_enc, mac_key_len, |
| 8602 | &transform->psa_mac_enc)) != PSA_SUCCESS) { |
| 8603 | ret = psa_ssl_status_to_mbedtls(status); |
| 8604 | MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8605 | goto end; |
| 8606 | } |
| 8607 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8608 | if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) || |
| 8609 | ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING) |
Andrzej Kurek | 8c95ac4 | 2022-08-17 16:17:00 -0400 | [diff] [blame] | 8610 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8611 | && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED) |
Andrzej Kurek | 8c95ac4 | 2022-08-17 16:17:00 -0400 | [diff] [blame] | 8612 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8613 | )) { |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8614 | /* mbedtls_ct_hmac() requires the key to be exportable */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8615 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT | |
| 8616 | PSA_KEY_USAGE_VERIFY_HASH); |
| 8617 | } else { |
| 8618 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 8619 | } |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8620 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8621 | if ((status = psa_import_key(&attributes, |
| 8622 | mac_dec, mac_key_len, |
| 8623 | &transform->psa_mac_dec)) != PSA_SUCCESS) { |
| 8624 | ret = psa_ssl_status_to_mbedtls(status); |
| 8625 | MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8626 | goto end; |
| 8627 | } |
| 8628 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8629 | ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len); |
| 8630 | if (ret != 0) { |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8631 | goto end; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8632 | } |
| 8633 | ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len); |
| 8634 | if (ret != 0) { |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8635 | goto end; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8636 | } |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8637 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8638 | } |
| 8639 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 8640 | |
| 8641 | ((void) mac_dec); |
| 8642 | ((void) mac_enc); |
| 8643 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8644 | end: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8645 | mbedtls_platform_zeroize(keyblk, sizeof(keyblk)); |
| 8646 | return ret; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8647 | } |
| 8648 | |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8649 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \ |
| 8650 | defined(MBEDTLS_USE_PSA_CRYPTO) |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8651 | int mbedtls_psa_ecjpake_read_round( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8652 | psa_pake_operation_t *pake_ctx, |
| 8653 | const unsigned char *buf, |
| 8654 | size_t len, mbedtls_ecjpake_rounds_t round) |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8655 | { |
| 8656 | psa_status_t status; |
| 8657 | size_t input_offset = 0; |
Valerio Setti | 819de86 | 2022-11-17 18:05:19 +0100 | [diff] [blame] | 8658 | /* |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8659 | * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice |
| 8660 | * At round two perform a single cycle |
| 8661 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8662 | unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8663 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8664 | for (; remaining_steps > 0; remaining_steps--) { |
| 8665 | for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8666 | step <= PSA_PAKE_STEP_ZK_PROOF; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8667 | ++step) { |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8668 | /* Length is stored at the first byte */ |
| 8669 | size_t length = buf[input_offset]; |
| 8670 | input_offset += 1; |
| 8671 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8672 | if (input_offset + length > len) { |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8673 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
| 8674 | } |
| 8675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8676 | status = psa_pake_input(pake_ctx, step, |
| 8677 | buf + input_offset, length); |
| 8678 | if (status != PSA_SUCCESS) { |
| 8679 | return psa_ssl_status_to_mbedtls(status); |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8680 | } |
| 8681 | |
| 8682 | input_offset += length; |
| 8683 | } |
| 8684 | } |
| 8685 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8686 | if (input_offset != len) { |
Valerio Setti | 61ea17d | 2022-11-18 12:11:00 +0100 | [diff] [blame] | 8687 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8688 | } |
Valerio Setti | 30ebe11 | 2022-11-17 16:23:34 +0100 | [diff] [blame] | 8689 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8690 | return 0; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8691 | } |
| 8692 | |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8693 | int mbedtls_psa_ecjpake_write_round( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8694 | psa_pake_operation_t *pake_ctx, |
| 8695 | unsigned char *buf, |
| 8696 | size_t len, size_t *olen, |
| 8697 | mbedtls_ecjpake_rounds_t round) |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8698 | { |
| 8699 | psa_status_t status; |
| 8700 | size_t output_offset = 0; |
| 8701 | size_t output_len; |
Valerio Setti | 819de86 | 2022-11-17 18:05:19 +0100 | [diff] [blame] | 8702 | /* |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8703 | * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice |
| 8704 | * At round two perform a single cycle |
| 8705 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8706 | unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8707 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8708 | for (; remaining_steps > 0; remaining_steps--) { |
| 8709 | for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE; |
| 8710 | step <= PSA_PAKE_STEP_ZK_PROOF; |
| 8711 | ++step) { |
Valerio Setti | 79f6b6b | 2022-11-21 14:17:03 +0100 | [diff] [blame] | 8712 | /* |
Valerio Setti | d4a9b1a | 2022-11-22 11:11:10 +0100 | [diff] [blame] | 8713 | * For each step, prepend 1 byte with the length of the data as |
| 8714 | * given by psa_pake_output(). |
Valerio Setti | 79f6b6b | 2022-11-21 14:17:03 +0100 | [diff] [blame] | 8715 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8716 | status = psa_pake_output(pake_ctx, step, |
| 8717 | buf + output_offset + 1, |
| 8718 | len - output_offset - 1, |
| 8719 | &output_len); |
| 8720 | if (status != PSA_SUCCESS) { |
| 8721 | return psa_ssl_status_to_mbedtls(status); |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8722 | } |
| 8723 | |
Valerio Setti | 99d88c1 | 2022-11-22 16:03:43 +0100 | [diff] [blame] | 8724 | *(buf + output_offset) = (uint8_t) output_len; |
Valerio Setti | 79f6b6b | 2022-11-21 14:17:03 +0100 | [diff] [blame] | 8725 | |
| 8726 | output_offset += output_len + 1; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8727 | } |
| 8728 | } |
| 8729 | |
| 8730 | *olen = output_offset; |
| 8731 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8732 | return 0; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8733 | } |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8734 | #endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO |
| 8735 | |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8736 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8737 | int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl, |
| 8738 | unsigned char *hash, size_t *hashlen, |
| 8739 | unsigned char *data, size_t data_len, |
| 8740 | mbedtls_md_type_t md_alg) |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8741 | { |
| 8742 | psa_status_t status; |
| 8743 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8744 | psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(md_alg); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8745 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8746 | 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] | 8747 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8748 | if ((status = psa_hash_setup(&hash_operation, |
| 8749 | hash_alg)) != PSA_SUCCESS) { |
| 8750 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8751 | goto exit; |
| 8752 | } |
| 8753 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8754 | if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes, |
| 8755 | 64)) != PSA_SUCCESS) { |
| 8756 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8757 | goto exit; |
| 8758 | } |
| 8759 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8760 | if ((status = psa_hash_update(&hash_operation, |
| 8761 | data, data_len)) != PSA_SUCCESS) { |
| 8762 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8763 | goto exit; |
| 8764 | } |
| 8765 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8766 | if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE, |
| 8767 | hashlen)) != PSA_SUCCESS) { |
| 8768 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status); |
| 8769 | goto exit; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8770 | } |
| 8771 | |
| 8772 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8773 | if (status != PSA_SUCCESS) { |
| 8774 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8775 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
| 8776 | switch (status) { |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8777 | case PSA_ERROR_NOT_SUPPORTED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8778 | return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8779 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ |
| 8780 | case PSA_ERROR_BUFFER_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8781 | return MBEDTLS_ERR_MD_BAD_INPUT_DATA; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8782 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8783 | return MBEDTLS_ERR_MD_ALLOC_FAILED; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8784 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8785 | return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8786 | } |
| 8787 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8788 | return 0; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8789 | } |
| 8790 | |
| 8791 | #else |
| 8792 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8793 | int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl, |
| 8794 | unsigned char *hash, size_t *hashlen, |
| 8795 | unsigned char *data, size_t data_len, |
| 8796 | mbedtls_md_type_t md_alg) |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8797 | { |
| 8798 | int ret = 0; |
| 8799 | mbedtls_md_context_t ctx; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8800 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg); |
| 8801 | *hashlen = mbedtls_md_get_size(md_info); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8802 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8803 | 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] | 8804 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8805 | mbedtls_md_init(&ctx); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8806 | |
| 8807 | /* |
| 8808 | * digitally-signed struct { |
| 8809 | * opaque client_random[32]; |
| 8810 | * opaque server_random[32]; |
| 8811 | * ServerDHParams params; |
| 8812 | * }; |
| 8813 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8814 | if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) { |
| 8815 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8816 | goto exit; |
| 8817 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8818 | if ((ret = mbedtls_md_starts(&ctx)) != 0) { |
| 8819 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8820 | goto exit; |
| 8821 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8822 | if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) { |
| 8823 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8824 | goto exit; |
| 8825 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8826 | if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) { |
| 8827 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8828 | goto exit; |
| 8829 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8830 | if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) { |
| 8831 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8832 | goto exit; |
| 8833 | } |
| 8834 | |
| 8835 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8836 | mbedtls_md_free(&ctx); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8837 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8838 | if (ret != 0) { |
| 8839 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8840 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
| 8841 | } |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8842 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8843 | return ret; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8844 | } |
| 8845 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8846 | |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8847 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 8848 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8849 | /* Find the preferred hash for a given signature algorithm. */ |
| 8850 | unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8851 | mbedtls_ssl_context *ssl, |
| 8852 | unsigned int sig_alg) |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8853 | { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8854 | unsigned int i; |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8855 | uint16_t *received_sig_algs = ssl->handshake->received_sig_algs; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8856 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8857 | if (sig_alg == MBEDTLS_SSL_SIG_ANON) { |
| 8858 | return MBEDTLS_SSL_HASH_NONE; |
| 8859 | } |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8860 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8861 | for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) { |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8862 | unsigned int hash_alg_received = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8863 | MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG( |
| 8864 | received_sig_algs[i]); |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8865 | unsigned int sig_alg_received = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8866 | MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG( |
| 8867 | received_sig_algs[i]); |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8868 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8869 | if (sig_alg == sig_alg_received) { |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8870 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8871 | if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) { |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8872 | psa_algorithm_t psa_hash_alg = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8873 | mbedtls_hash_info_psa_from_md(hash_alg_received); |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8874 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8875 | if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA && |
| 8876 | !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key, |
| 8877 | PSA_ALG_ECDSA(psa_hash_alg), |
| 8878 | PSA_KEY_USAGE_SIGN_HASH)) { |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8879 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8880 | } |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8881 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8882 | if (sig_alg_received == MBEDTLS_SSL_SIG_RSA && |
| 8883 | !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key, |
| 8884 | PSA_ALG_RSA_PKCS1V15_SIGN( |
| 8885 | psa_hash_alg), |
| 8886 | PSA_KEY_USAGE_SIGN_HASH)) { |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8887 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8888 | } |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8889 | } |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8890 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8891 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8892 | return hash_alg_received; |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8893 | } |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8894 | } |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8895 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8896 | return MBEDTLS_SSL_HASH_NONE; |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8897 | } |
| 8898 | |
| 8899 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 8900 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8901 | /* Serialization of TLS 1.2 sessions: |
| 8902 | * |
| 8903 | * struct { |
| 8904 | * uint64 start_time; |
| 8905 | * uint8 ciphersuite[2]; // defined by the standard |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8906 | * uint8 session_id_len; // at most 32 |
| 8907 | * opaque session_id[32]; |
| 8908 | * opaque master[48]; // fixed length in the standard |
| 8909 | * uint32 verify_result; |
| 8910 | * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert |
| 8911 | * opaque ticket<0..2^24-1>; // length 0 means no ticket |
| 8912 | * uint32 ticket_lifetime; |
| 8913 | * uint8 mfl_code; // up to 255 according to standard |
| 8914 | * uint8 encrypt_then_mac; // 0 or 1 |
| 8915 | * } serialized_session_tls12; |
| 8916 | * |
| 8917 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8918 | static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session, |
| 8919 | unsigned char *buf, |
| 8920 | size_t buf_len) |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8921 | { |
| 8922 | unsigned char *p = buf; |
| 8923 | size_t used = 0; |
| 8924 | |
| 8925 | #if defined(MBEDTLS_HAVE_TIME) |
| 8926 | uint64_t start; |
| 8927 | #endif |
| 8928 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8929 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8930 | size_t cert_len; |
| 8931 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8932 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8933 | |
| 8934 | /* |
| 8935 | * Time |
| 8936 | */ |
| 8937 | #if defined(MBEDTLS_HAVE_TIME) |
| 8938 | used += 8; |
| 8939 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8940 | if (used <= buf_len) { |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8941 | start = (uint64_t) session->start; |
| 8942 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8943 | MBEDTLS_PUT_UINT64_BE(start, p, 0); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8944 | p += 8; |
| 8945 | } |
| 8946 | #endif /* MBEDTLS_HAVE_TIME */ |
| 8947 | |
| 8948 | /* |
| 8949 | * Basic mandatory fields |
| 8950 | */ |
| 8951 | used += 2 /* ciphersuite */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8952 | + 1 /* id_len */ |
| 8953 | + sizeof(session->id) |
| 8954 | + sizeof(session->master) |
| 8955 | + 4; /* verify_result */ |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8956 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8957 | if (used <= buf_len) { |
| 8958 | MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8959 | p += 2; |
| 8960 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8961 | *p++ = MBEDTLS_BYTE_0(session->id_len); |
| 8962 | memcpy(p, session->id, 32); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8963 | p += 32; |
| 8964 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8965 | memcpy(p, session->master, 48); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8966 | p += 48; |
| 8967 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8968 | MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8969 | p += 4; |
| 8970 | } |
| 8971 | |
| 8972 | /* |
| 8973 | * Peer's end-entity certificate |
| 8974 | */ |
| 8975 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8976 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8977 | if (session->peer_cert == NULL) { |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8978 | cert_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8979 | } else { |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8980 | cert_len = session->peer_cert->raw.len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8981 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8982 | |
| 8983 | used += 3 + cert_len; |
| 8984 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8985 | if (used <= buf_len) { |
| 8986 | *p++ = MBEDTLS_BYTE_2(cert_len); |
| 8987 | *p++ = MBEDTLS_BYTE_1(cert_len); |
| 8988 | *p++ = MBEDTLS_BYTE_0(cert_len); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8989 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8990 | if (session->peer_cert != NULL) { |
| 8991 | memcpy(p, session->peer_cert->raw.p, cert_len); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8992 | p += cert_len; |
| 8993 | } |
| 8994 | } |
| 8995 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8996 | if (session->peer_cert_digest != NULL) { |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8997 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8998 | if (used <= buf_len) { |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8999 | *p++ = (unsigned char) session->peer_cert_digest_type; |
| 9000 | *p++ = (unsigned char) session->peer_cert_digest_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9001 | memcpy(p, session->peer_cert_digest, |
| 9002 | session->peer_cert_digest_len); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9003 | p += session->peer_cert_digest_len; |
| 9004 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9005 | } else { |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9006 | used += 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9007 | if (used <= buf_len) { |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9008 | *p++ = (unsigned char) MBEDTLS_MD_NONE; |
| 9009 | *p++ = 0; |
| 9010 | } |
| 9011 | } |
| 9012 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 9013 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 9014 | |
| 9015 | /* |
| 9016 | * Session ticket if any, plus associated data |
| 9017 | */ |
| 9018 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 9019 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ |
| 9020 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9021 | if (used <= buf_len) { |
| 9022 | *p++ = MBEDTLS_BYTE_2(session->ticket_len); |
| 9023 | *p++ = MBEDTLS_BYTE_1(session->ticket_len); |
| 9024 | *p++ = MBEDTLS_BYTE_0(session->ticket_len); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9025 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9026 | if (session->ticket != NULL) { |
| 9027 | memcpy(p, session->ticket, session->ticket_len); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9028 | p += session->ticket_len; |
| 9029 | } |
| 9030 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9031 | MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9032 | p += 4; |
| 9033 | } |
| 9034 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 9035 | |
| 9036 | /* |
| 9037 | * Misc extension-related info |
| 9038 | */ |
| 9039 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 9040 | used += 1; |
| 9041 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9042 | if (used <= buf_len) { |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9043 | *p++ = session->mfl_code; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9044 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9045 | #endif |
| 9046 | |
| 9047 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 9048 | used += 1; |
| 9049 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9050 | if (used <= buf_len) { |
| 9051 | *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac); |
| 9052 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9053 | #endif |
| 9054 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9055 | return used; |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9056 | } |
| 9057 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 9058 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9059 | static int ssl_tls12_session_load(mbedtls_ssl_session *session, |
| 9060 | const unsigned char *buf, |
| 9061 | size_t len) |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9062 | { |
| 9063 | #if defined(MBEDTLS_HAVE_TIME) |
| 9064 | uint64_t start; |
| 9065 | #endif |
| 9066 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 9067 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 9068 | size_t cert_len; |
| 9069 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 9070 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 9071 | |
| 9072 | const unsigned char *p = buf; |
| 9073 | const unsigned char * const end = buf + len; |
| 9074 | |
| 9075 | /* |
| 9076 | * Time |
| 9077 | */ |
| 9078 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9079 | if (8 > (size_t) (end - p)) { |
| 9080 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9081 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9082 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9083 | start = ((uint64_t) p[0] << 56) | |
| 9084 | ((uint64_t) p[1] << 48) | |
| 9085 | ((uint64_t) p[2] << 40) | |
| 9086 | ((uint64_t) p[3] << 32) | |
| 9087 | ((uint64_t) p[4] << 24) | |
| 9088 | ((uint64_t) p[5] << 16) | |
| 9089 | ((uint64_t) p[6] << 8) | |
| 9090 | ((uint64_t) p[7]); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9091 | p += 8; |
| 9092 | |
| 9093 | session->start = (time_t) start; |
| 9094 | #endif /* MBEDTLS_HAVE_TIME */ |
| 9095 | |
| 9096 | /* |
| 9097 | * Basic mandatory fields |
| 9098 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9099 | if (2 + 1 + 32 + 48 + 4 > (size_t) (end - p)) { |
| 9100 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9101 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9102 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9103 | session->ciphersuite = (p[0] << 8) | p[1]; |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9104 | p += 2; |
| 9105 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9106 | session->id_len = *p++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9107 | memcpy(session->id, p, 32); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9108 | p += 32; |
| 9109 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9110 | memcpy(session->master, p, 48); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9111 | p += 48; |
| 9112 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9113 | session->verify_result = ((uint32_t) p[0] << 24) | |
| 9114 | ((uint32_t) p[1] << 16) | |
| 9115 | ((uint32_t) p[2] << 8) | |
| 9116 | ((uint32_t) p[3]); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9117 | p += 4; |
| 9118 | |
| 9119 | /* Immediately clear invalid pointer values that have been read, in case |
| 9120 | * we exit early before we replaced them with valid ones. */ |
| 9121 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 9122 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 9123 | session->peer_cert = NULL; |
| 9124 | #else |
| 9125 | session->peer_cert_digest = NULL; |
| 9126 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 9127 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 9128 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 9129 | session->ticket = NULL; |
| 9130 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 9131 | |
| 9132 | /* |
| 9133 | * Peer certificate |
| 9134 | */ |
| 9135 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 9136 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 9137 | /* Deserialize CRT from the end of the ticket. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9138 | if (3 > (size_t) (end - p)) { |
| 9139 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9140 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9141 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9142 | cert_len = (p[0] << 16) | (p[1] << 8) | p[2]; |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9143 | p += 3; |
| 9144 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9145 | if (cert_len != 0) { |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9146 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 9147 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9148 | if (cert_len > (size_t) (end - p)) { |
| 9149 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9150 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9151 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9152 | session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt)); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9153 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9154 | if (session->peer_cert == NULL) { |
| 9155 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 9156 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9157 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9158 | mbedtls_x509_crt_init(session->peer_cert); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9159 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9160 | if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert, |
| 9161 | p, cert_len)) != 0) { |
| 9162 | mbedtls_x509_crt_free(session->peer_cert); |
| 9163 | mbedtls_free(session->peer_cert); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9164 | session->peer_cert = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9165 | return ret; |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9166 | } |
| 9167 | |
| 9168 | p += cert_len; |
| 9169 | } |
| 9170 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 9171 | /* Deserialize CRT digest from the end of the ticket. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9172 | if (2 > (size_t) (end - p)) { |
| 9173 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9174 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9175 | |
| 9176 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; |
| 9177 | session->peer_cert_digest_len = (size_t) *p++; |
| 9178 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9179 | if (session->peer_cert_digest_len != 0) { |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9180 | const mbedtls_md_info_t *md_info = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9181 | mbedtls_md_info_from_type(session->peer_cert_digest_type); |
| 9182 | if (md_info == NULL) { |
| 9183 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9184 | } |
| 9185 | if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) { |
| 9186 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9187 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9188 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9189 | if (session->peer_cert_digest_len > (size_t) (end - p)) { |
| 9190 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9191 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9192 | |
| 9193 | session->peer_cert_digest = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9194 | mbedtls_calloc(1, session->peer_cert_digest_len); |
| 9195 | if (session->peer_cert_digest == NULL) { |
| 9196 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 9197 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9198 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9199 | memcpy(session->peer_cert_digest, p, |
| 9200 | session->peer_cert_digest_len); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9201 | p += session->peer_cert_digest_len; |
| 9202 | } |
| 9203 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 9204 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 9205 | |
| 9206 | /* |
| 9207 | * Session ticket and associated data |
| 9208 | */ |
| 9209 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9210 | if (3 > (size_t) (end - p)) { |
| 9211 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9212 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9213 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9214 | session->ticket_len = (p[0] << 16) | (p[1] << 8) | p[2]; |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9215 | p += 3; |
| 9216 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9217 | if (session->ticket_len != 0) { |
| 9218 | if (session->ticket_len > (size_t) (end - p)) { |
| 9219 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9220 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9221 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9222 | session->ticket = mbedtls_calloc(1, session->ticket_len); |
| 9223 | if (session->ticket == NULL) { |
| 9224 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 9225 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9226 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9227 | memcpy(session->ticket, p, session->ticket_len); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9228 | p += session->ticket_len; |
| 9229 | } |
| 9230 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9231 | if (4 > (size_t) (end - p)) { |
| 9232 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9233 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9234 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9235 | session->ticket_lifetime = ((uint32_t) p[0] << 24) | |
| 9236 | ((uint32_t) p[1] << 16) | |
| 9237 | ((uint32_t) p[2] << 8) | |
| 9238 | ((uint32_t) p[3]); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9239 | p += 4; |
| 9240 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 9241 | |
| 9242 | /* |
| 9243 | * Misc extension-related info |
| 9244 | */ |
| 9245 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9246 | if (1 > (size_t) (end - p)) { |
| 9247 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9248 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9249 | |
| 9250 | session->mfl_code = *p++; |
| 9251 | #endif |
| 9252 | |
| 9253 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9254 | if (1 > (size_t) (end - p)) { |
| 9255 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9256 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9257 | |
| 9258 | session->encrypt_then_mac = *p++; |
| 9259 | #endif |
| 9260 | |
| 9261 | /* Done, should have consumed entire buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9262 | if (p != end) { |
| 9263 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9264 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9266 | return 0; |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9267 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 9268 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 9269 | |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9270 | int mbedtls_ssl_validate_ciphersuite( |
| 9271 | const mbedtls_ssl_context *ssl, |
| 9272 | const mbedtls_ssl_ciphersuite_t *suite_info, |
| 9273 | mbedtls_ssl_protocol_version min_tls_version, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9274 | mbedtls_ssl_protocol_version max_tls_version) |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9275 | { |
| 9276 | (void) ssl; |
| 9277 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9278 | if (suite_info == NULL) { |
| 9279 | return -1; |
| 9280 | } |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9282 | if ((suite_info->min_tls_version > max_tls_version) || |
| 9283 | (suite_info->max_tls_version < min_tls_version)) { |
| 9284 | return -1; |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9285 | } |
| 9286 | |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 9287 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C) |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9288 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 9289 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9290 | if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
| 9291 | ssl->handshake->psa_pake_ctx_is_ok != 1) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 9292 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9293 | if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
| 9294 | mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 9295 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9296 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9297 | return -1; |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9298 | } |
| 9299 | #endif |
| 9300 | |
| 9301 | /* Don't suggest PSK-based ciphersuite if no PSK is available. */ |
| 9302 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9303 | if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) && |
| 9304 | mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) { |
| 9305 | return -1; |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9306 | } |
| 9307 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
| 9308 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 9309 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9310 | return 0; |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9311 | } |
| 9312 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 9313 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9314 | /* |
| 9315 | * Function for writing a signature algorithm extension. |
| 9316 | * |
| 9317 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 9318 | * value (TLS 1.3 RFC8446): |
| 9319 | * enum { |
| 9320 | * .... |
| 9321 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 9322 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 9323 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 9324 | * .... |
| 9325 | * } SignatureScheme; |
| 9326 | * |
| 9327 | * struct { |
| 9328 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 9329 | * } SignatureSchemeList; |
| 9330 | * |
| 9331 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 9332 | * value (TLS 1.2 RFC5246): |
| 9333 | * enum { |
| 9334 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 9335 | * sha512(6), (255) |
| 9336 | * } HashAlgorithm; |
| 9337 | * |
| 9338 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 9339 | * SignatureAlgorithm; |
| 9340 | * |
| 9341 | * struct { |
| 9342 | * HashAlgorithm hash; |
| 9343 | * SignatureAlgorithm signature; |
| 9344 | * } SignatureAndHashAlgorithm; |
| 9345 | * |
| 9346 | * SignatureAndHashAlgorithm |
| 9347 | * supported_signature_algorithms<2..2^16-2>; |
| 9348 | * |
| 9349 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 9350 | * generalization of the TLS 1.2 signature algorithm extension. |
| 9351 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 9352 | * `SignatureScheme` field of TLS 1.3 |
| 9353 | * |
| 9354 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9355 | int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf, |
| 9356 | const unsigned char *end, size_t *out_len) |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9357 | { |
| 9358 | unsigned char *p = buf; |
| 9359 | unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */ |
| 9360 | size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */ |
| 9361 | |
| 9362 | *out_len = 0; |
| 9363 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9364 | MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension")); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9365 | |
| 9366 | /* Check if we have space for header and length field: |
| 9367 | * - extension_type (2 bytes) |
| 9368 | * - extension_data_length (2 bytes) |
| 9369 | * - supported_signature_algorithms_length (2 bytes) |
| 9370 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9371 | MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9372 | p += 6; |
| 9373 | |
| 9374 | /* |
| 9375 | * Write supported_signature_algorithms |
| 9376 | */ |
| 9377 | supported_sig_alg = p; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9378 | const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl); |
| 9379 | if (sig_alg == NULL) { |
| 9380 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
| 9381 | } |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9382 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9383 | for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) { |
| 9384 | MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s", |
| 9385 | *sig_alg, |
| 9386 | mbedtls_ssl_sig_alg_to_str(*sig_alg))); |
| 9387 | if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) { |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9388 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9389 | } |
| 9390 | MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2); |
| 9391 | MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9392 | p += 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9393 | MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s", |
| 9394 | *sig_alg, |
| 9395 | mbedtls_ssl_sig_alg_to_str(*sig_alg))); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9396 | } |
| 9397 | |
| 9398 | /* Length of supported_signature_algorithms */ |
| 9399 | supported_sig_alg_len = p - supported_sig_alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9400 | if (supported_sig_alg_len == 0) { |
| 9401 | MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined.")); |
| 9402 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9403 | } |
| 9404 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9405 | MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0); |
| 9406 | MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2); |
| 9407 | MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9408 | |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9409 | *out_len = p - buf; |
| 9410 | |
| 9411 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9412 | 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] | 9413 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | 0c354a2 | 2022-08-29 15:25:36 +0800 | [diff] [blame] | 9414 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9415 | return 0; |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9416 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 9417 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9418 | |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9419 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9420 | /* |
| 9421 | * mbedtls_ssl_parse_server_name_ext |
| 9422 | * |
| 9423 | * Structure of server_name extension: |
| 9424 | * |
| 9425 | * enum { |
| 9426 | * host_name(0), (255) |
| 9427 | * } NameType; |
| 9428 | * opaque HostName<1..2^16-1>; |
| 9429 | * |
| 9430 | * struct { |
| 9431 | * NameType name_type; |
| 9432 | * select (name_type) { |
| 9433 | * case host_name: HostName; |
| 9434 | * } name; |
| 9435 | * } ServerName; |
| 9436 | * struct { |
| 9437 | * ServerName server_name_list<1..2^16-1> |
| 9438 | * } ServerNameList; |
| 9439 | */ |
Ronald Cron | ce7d76e | 2022-07-08 18:56:49 +0200 | [diff] [blame] | 9440 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9441 | int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl, |
| 9442 | const unsigned char *buf, |
| 9443 | const unsigned char *end) |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9444 | { |
| 9445 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 9446 | const unsigned char *p = buf; |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9447 | size_t server_name_list_len, hostname_len; |
| 9448 | const unsigned char *server_name_list_end; |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9449 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9450 | MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension")); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9451 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9452 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2); |
| 9453 | server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9454 | p += 2; |
| 9455 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9456 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len); |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9457 | server_name_list_end = p + server_name_list_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9458 | while (p < server_name_list_end) { |
| 9459 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3); |
| 9460 | hostname_len = MBEDTLS_GET_UINT16_BE(p, 1); |
| 9461 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, |
| 9462 | hostname_len + 3); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9463 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9464 | if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) { |
XiaokangQian | 75fe8c7 | 2022-06-15 09:42:45 +0000 | [diff] [blame] | 9465 | /* sni_name is intended to be used only during the parsing of the |
| 9466 | * ClientHello message (it is reset to NULL before the end of |
| 9467 | * the message parsing). Thus it is ok to just point to the |
| 9468 | * reception buffer and not make a copy of it. |
| 9469 | */ |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 9470 | ssl->handshake->sni_name = p + 3; |
| 9471 | ssl->handshake->sni_name_len = hostname_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9472 | if (ssl->conf->f_sni == NULL) { |
| 9473 | return 0; |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9474 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9475 | ret = ssl->conf->f_sni(ssl->conf->p_sni, |
| 9476 | ssl, p + 3, hostname_len); |
| 9477 | if (ret != 0) { |
| 9478 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret); |
| 9479 | MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME, |
| 9480 | MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME); |
| 9481 | return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME; |
| 9482 | } |
| 9483 | return 0; |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9484 | } |
| 9485 | |
| 9486 | p += hostname_len + 3; |
| 9487 | } |
| 9488 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9489 | return 0; |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9490 | } |
| 9491 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 9492 | |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9493 | #if defined(MBEDTLS_SSL_ALPN) |
Ronald Cron | ce7d76e | 2022-07-08 18:56:49 +0200 | [diff] [blame] | 9494 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9495 | int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl, |
| 9496 | const unsigned char *buf, |
| 9497 | const unsigned char *end) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9498 | { |
| 9499 | const unsigned char *p = buf; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9500 | size_t protocol_name_list_len; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9501 | const unsigned char *protocol_name_list; |
| 9502 | const unsigned char *protocol_name_list_end; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9503 | size_t protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9504 | |
| 9505 | /* If ALPN not configured, just ignore the extension */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9506 | if (ssl->conf->alpn_list == NULL) { |
| 9507 | return 0; |
| 9508 | } |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9509 | |
| 9510 | /* |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9511 | * RFC7301, section 3.1 |
| 9512 | * opaque ProtocolName<1..2^8-1>; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9513 | * |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9514 | * struct { |
| 9515 | * ProtocolName protocol_name_list<2..2^16-1> |
| 9516 | * } ProtocolNameList; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9517 | */ |
| 9518 | |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9519 | /* |
XiaokangQian | 0b776e2 | 2022-06-24 09:04:59 +0000 | [diff] [blame] | 9520 | * protocol_name_list_len 2 bytes |
| 9521 | * protocol_name_len 1 bytes |
| 9522 | * protocol_name >=1 byte |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9523 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9524 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9525 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9526 | protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9527 | p += 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9528 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9529 | protocol_name_list = p; |
| 9530 | protocol_name_list_end = p + protocol_name_list_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9531 | |
| 9532 | /* Validate peer's list (lengths) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9533 | while (p < protocol_name_list_end) { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9534 | protocol_name_len = *p++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9535 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end, |
| 9536 | protocol_name_len); |
| 9537 | if (protocol_name_len == 0) { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9538 | MBEDTLS_SSL_PEND_FATAL_ALERT( |
| 9539 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9540 | MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER); |
| 9541 | return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9542 | } |
| 9543 | |
| 9544 | p += protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9545 | } |
| 9546 | |
| 9547 | /* Use our order of preference */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9548 | for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) { |
| 9549 | size_t const alpn_len = strlen(*alpn); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9550 | p = protocol_name_list; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9551 | while (p < protocol_name_list_end) { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9552 | protocol_name_len = *p++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9553 | if (protocol_name_len == alpn_len && |
| 9554 | memcmp(p, *alpn, alpn_len) == 0) { |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9555 | ssl->alpn_chosen = *alpn; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9556 | return 0; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9557 | } |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9558 | |
| 9559 | p += protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9560 | } |
| 9561 | } |
| 9562 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9563 | /* If we get here, no match was found */ |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9564 | MBEDTLS_SSL_PEND_FATAL_ALERT( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9565 | MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL, |
| 9566 | MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL); |
| 9567 | return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9568 | } |
| 9569 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9570 | int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl, |
| 9571 | unsigned char *buf, |
| 9572 | unsigned char *end, |
| 9573 | size_t *out_len) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9574 | { |
| 9575 | unsigned char *p = buf; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9576 | size_t protocol_name_len; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9577 | *out_len = 0; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9578 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9579 | if (ssl->alpn_chosen == NULL) { |
| 9580 | return 0; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9581 | } |
| 9582 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9583 | protocol_name_len = strlen(ssl->alpn_chosen); |
| 9584 | MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9585 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9586 | MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension")); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9587 | /* |
| 9588 | * 0 . 1 ext identifier |
| 9589 | * 2 . 3 ext length |
| 9590 | * 4 . 5 protocol list length |
| 9591 | * 6 . 6 protocol name length |
| 9592 | * 7 . 7+n protocol name |
| 9593 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9594 | MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9595 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9596 | *out_len = 7 + protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9597 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9598 | MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2); |
| 9599 | MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4); |
XiaokangQian | 0b776e2 | 2022-06-24 09:04:59 +0000 | [diff] [blame] | 9600 | /* Note: the length of the chosen protocol has been checked to be less |
| 9601 | * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`. |
| 9602 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9603 | p[6] = MBEDTLS_BYTE_0(protocol_name_len); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9604 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9605 | memcpy(p + 7, ssl->alpn_chosen, protocol_name_len); |
Jerry Yu | b95dd36 | 2022-11-08 21:19:34 +0800 | [diff] [blame] | 9606 | |
| 9607 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9608 | 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] | 9609 | #endif |
| 9610 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9611 | return 0; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9612 | } |
| 9613 | #endif /* MBEDTLS_SSL_ALPN */ |
| 9614 | |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9615 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
Xiaokang Qian | 0340929 | 2022-10-12 02:49:52 +0000 | [diff] [blame] | 9616 | defined(MBEDTLS_SSL_SESSION_TICKETS) && \ |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 9617 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \ |
Xiaokang Qian | ed3afcd | 2022-10-12 08:31:11 +0000 | [diff] [blame] | 9618 | defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9619 | int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session, |
| 9620 | const char *hostname) |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9621 | { |
| 9622 | /* Initialize to suppress unnecessary compiler warning */ |
| 9623 | size_t hostname_len = 0; |
| 9624 | |
| 9625 | /* Check if new hostname is valid before |
| 9626 | * making any change to current one */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9627 | if (hostname != NULL) { |
| 9628 | hostname_len = strlen(hostname); |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9629 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9630 | if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) { |
| 9631 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9632 | } |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9633 | } |
| 9634 | |
| 9635 | /* Now it's clear that we will overwrite the old hostname, |
| 9636 | * so we can free it safely */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9637 | if (session->hostname != NULL) { |
| 9638 | mbedtls_platform_zeroize(session->hostname, |
| 9639 | strlen(session->hostname)); |
| 9640 | mbedtls_free(session->hostname); |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9641 | } |
| 9642 | |
| 9643 | /* Passing NULL as hostname shall clear the old one */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9644 | if (hostname == NULL) { |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9645 | session->hostname = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9646 | } else { |
| 9647 | session->hostname = mbedtls_calloc(1, hostname_len + 1); |
| 9648 | if (session->hostname == NULL) { |
| 9649 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 9650 | } |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9651 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9652 | memcpy(session->hostname, hostname, hostname_len); |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9653 | } |
| 9654 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9655 | return 0; |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9656 | } |
Xiaokang Qian | 0340929 | 2022-10-12 02:49:52 +0000 | [diff] [blame] | 9657 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 && |
| 9658 | MBEDTLS_SSL_SESSION_TICKETS && |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 9659 | MBEDTLS_SSL_SERVER_NAME_INDICATION && |
Xiaokang Qian | ed3afcd | 2022-10-12 08:31:11 +0000 | [diff] [blame] | 9660 | MBEDTLS_SSL_CLI_C */ |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9661 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9662 | #endif /* MBEDTLS_SSL_TLS_C */ |