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 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 791 | void mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl, |
| 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 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 803 | ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr)); |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 804 | } |
| 805 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 806 | void mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl, |
| 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 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 811 | mbedtls_ssl_add_hs_hdr_to_checksum(ssl, hs_type, msg_len); |
| 812 | ssl->handshake->update_checksum(ssl, msg, msg_len); |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 813 | } |
| 814 | |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame^] | 815 | int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 816 | { |
| 817 | ((void) ssl); |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 818 | #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] | 819 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 820 | psa_hash_abort(&ssl->handshake->fin_sha256_psa); |
| 821 | psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 822 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 823 | mbedtls_sha256_starts(&ssl->handshake->fin_sha256, 0); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 824 | #endif |
| 825 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 826 | #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] | 827 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 828 | psa_hash_abort(&ssl->handshake->fin_sha384_psa); |
| 829 | psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 830 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 831 | mbedtls_sha512_starts(&ssl->handshake->fin_sha384, 1); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 832 | #endif |
| 833 | #endif |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame^] | 834 | return 0; |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 835 | } |
| 836 | |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame^] | 837 | static int ssl_update_checksum_start(mbedtls_ssl_context *ssl, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 838 | const unsigned char *buf, size_t len) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 839 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 840 | #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] | 841 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 842 | psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 843 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 844 | mbedtls_sha256_update(&ssl->handshake->fin_sha256, buf, len); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 845 | #endif |
| 846 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 847 | #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] | 848 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 849 | psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 850 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 851 | mbedtls_sha512_update(&ssl->handshake->fin_sha384, buf, len); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 852 | #endif |
| 853 | #endif |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 854 | #if !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 855 | !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 856 | (void) ssl; |
| 857 | (void) buf; |
| 858 | (void) len; |
| 859 | #endif |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame^] | 860 | return 0; |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 861 | } |
| 862 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 863 | #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^] | 864 | static int ssl_update_checksum_sha256(mbedtls_ssl_context *ssl, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 865 | const unsigned char *buf, size_t len) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 866 | { |
| 867 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 868 | psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 869 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 870 | mbedtls_sha256_update(&ssl->handshake->fin_sha256, buf, len); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 871 | #endif |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame^] | 872 | return 0; |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 873 | } |
| 874 | #endif |
| 875 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 876 | #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^] | 877 | static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 878 | const unsigned char *buf, size_t len) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 879 | { |
| 880 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 881 | psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 882 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 883 | mbedtls_sha512_update(&ssl->handshake->fin_sha384, buf, len); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 884 | #endif |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame^] | 885 | return 0; |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 886 | } |
| 887 | #endif |
| 888 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 889 | static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 890 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 891 | memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params)); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 892 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 893 | #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] | 894 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 895 | handshake->fin_sha256_psa = psa_hash_operation_init(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 896 | psa_hash_setup(&handshake->fin_sha256_psa, PSA_ALG_SHA_256); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 897 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 898 | mbedtls_sha256_init(&handshake->fin_sha256); |
| 899 | mbedtls_sha256_starts(&handshake->fin_sha256, 0); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 900 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 901 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 902 | #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] | 903 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 904 | handshake->fin_sha384_psa = psa_hash_operation_init(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 905 | psa_hash_setup(&handshake->fin_sha384_psa, PSA_ALG_SHA_384); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 906 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 907 | mbedtls_sha512_init(&handshake->fin_sha384); |
| 908 | mbedtls_sha512_starts(&handshake->fin_sha384, 1); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 909 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 910 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 911 | |
| 912 | handshake->update_checksum = ssl_update_checksum_start; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 913 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 914 | #if defined(MBEDTLS_DHM_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 915 | mbedtls_dhm_init(&handshake->dhm_ctx); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 916 | #endif |
Neil Armstrong | f3f4641 | 2022-04-12 14:43:39 +0200 | [diff] [blame] | 917 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 918 | mbedtls_ecdh_init(&handshake->ecdh_ctx); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 919 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 920 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 921 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 922 | handshake->psa_pake_ctx = psa_pake_operation_init(); |
| 923 | handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT; |
| 924 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 925 | mbedtls_ecjpake_init(&handshake->ecjpake_ctx); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 926 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 927 | #if defined(MBEDTLS_SSL_CLI_C) |
| 928 | handshake->ecjpake_cache = NULL; |
| 929 | handshake->ecjpake_cache_len = 0; |
| 930 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 931 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 932 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 933 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 934 | mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 935 | #endif |
| 936 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 937 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 938 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 939 | #endif |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 940 | |
| 941 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 942 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 943 | mbedtls_pk_init(&handshake->peer_pubkey); |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 944 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 945 | } |
| 946 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 947 | void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 948 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 949 | memset(transform, 0, sizeof(mbedtls_ssl_transform)); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 950 | |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 951 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 952 | transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 953 | transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 954 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 955 | mbedtls_cipher_init(&transform->cipher_ctx_enc); |
| 956 | mbedtls_cipher_init(&transform->cipher_ctx_dec); |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 957 | #endif |
| 958 | |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 959 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 39b8e7d | 2022-02-23 09:24:45 +0100 | [diff] [blame] | 960 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 961 | transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 962 | transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 963 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 964 | mbedtls_md_init(&transform->md_ctx_enc); |
| 965 | mbedtls_md_init(&transform->md_ctx_dec); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 966 | #endif |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 967 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 968 | } |
| 969 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 970 | void mbedtls_ssl_session_init(mbedtls_ssl_session *session) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 971 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 972 | memset(session, 0, sizeof(mbedtls_ssl_session)); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 973 | } |
| 974 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 975 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 976 | static int ssl_handshake_init(mbedtls_ssl_context *ssl) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 977 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 978 | /* Clear old handshake information if present */ |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 979 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 980 | if (ssl->transform_negotiate) { |
| 981 | mbedtls_ssl_transform_free(ssl->transform_negotiate); |
| 982 | } |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 983 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 984 | if (ssl->session_negotiate) { |
| 985 | mbedtls_ssl_session_free(ssl->session_negotiate); |
| 986 | } |
| 987 | if (ssl->handshake) { |
| 988 | mbedtls_ssl_handshake_free(ssl); |
| 989 | } |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 990 | |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 991 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 992 | /* |
| 993 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 994 | * Now allocate missing structures. |
| 995 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 996 | if (ssl->transform_negotiate == NULL) { |
| 997 | ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform)); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 998 | } |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 999 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1000 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1001 | if (ssl->session_negotiate == NULL) { |
| 1002 | ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session)); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 1003 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1004 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1005 | if (ssl->handshake == NULL) { |
| 1006 | ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params)); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 1007 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1008 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1009 | /* If the buffers are too small - reallocate */ |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 1010 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1011 | handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN, |
| 1012 | MBEDTLS_SSL_OUT_BUFFER_LEN); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1013 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1014 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1015 | /* All pointers should exist and can be directly freed without issue */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1016 | if (ssl->handshake == NULL || |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1017 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1018 | ssl->transform_negotiate == NULL || |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1019 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1020 | ssl->session_negotiate == NULL) { |
| 1021 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed")); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1022 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1023 | mbedtls_free(ssl->handshake); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1024 | ssl->handshake = NULL; |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1025 | |
| 1026 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1027 | mbedtls_free(ssl->transform_negotiate); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1028 | ssl->transform_negotiate = NULL; |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1029 | #endif |
| 1030 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1031 | mbedtls_free(ssl->session_negotiate); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1032 | ssl->session_negotiate = NULL; |
| 1033 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1034 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1037 | /* Initialize structures */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1038 | mbedtls_ssl_session_init(ssl->session_negotiate); |
| 1039 | ssl_handshake_params_init(ssl->handshake); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 1040 | |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1041 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1042 | mbedtls_ssl_transform_init(ssl->transform_negotiate); |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1043 | #endif |
| 1044 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 1045 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 1046 | defined(MBEDTLS_SSL_SRV_C) && \ |
| 1047 | defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 1048 | ssl->handshake->new_session_tickets_count = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1049 | ssl->conf->new_session_tickets_count; |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 1050 | #endif |
| 1051 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1052 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1053 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1054 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1055 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1056 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1057 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1058 | } else { |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1059 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1060 | } |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1061 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1062 | mbedtls_ssl_set_timer(ssl, 0); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1063 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1064 | #endif |
| 1065 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1066 | /* |
| 1067 | * curve_list is translated to IANA TLS group identifiers here because |
| 1068 | * mbedtls_ssl_conf_curves returns void and so can't return |
| 1069 | * any error codes. |
| 1070 | */ |
| 1071 | #if defined(MBEDTLS_ECP_C) |
| 1072 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 1073 | /* Heap allocate and translate curve_list from internal to IANA group ids */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1074 | if (ssl->conf->curve_list != NULL) { |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1075 | size_t length; |
| 1076 | const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list; |
| 1077 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1078 | for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE) && |
| 1079 | (length < MBEDTLS_ECP_DP_MAX); length++) { |
| 1080 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1081 | |
| 1082 | /* Leave room for zero termination */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1083 | uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t)); |
| 1084 | if (group_list == NULL) { |
| 1085 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 1086 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1087 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1088 | for (size_t i = 0; i < length; i++) { |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 1089 | 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] | 1090 | curve_list[i]); |
| 1091 | if (tls_id == 0) { |
| 1092 | mbedtls_free(group_list); |
| 1093 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1094 | } |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 1095 | group_list[i] = tls_id; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | group_list[length] = 0; |
| 1099 | |
| 1100 | ssl->handshake->group_list = group_list; |
| 1101 | ssl->handshake->group_list_heap_allocated = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1102 | } else { |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1103 | ssl->handshake->group_list = ssl->conf->group_list; |
| 1104 | ssl->handshake->group_list_heap_allocated = 0; |
| 1105 | } |
| 1106 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 1107 | #endif /* MBEDTLS_ECP_C */ |
| 1108 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1109 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1110 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 1111 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 1112 | /* Heap allocate and translate sig_hashes from internal hash identifiers to |
| 1113 | signature algorithms IANA identifiers. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1114 | if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) && |
| 1115 | ssl->conf->sig_hashes != NULL) { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1116 | const int *md; |
| 1117 | const int *sig_hashes = ssl->conf->sig_hashes; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1118 | size_t sig_algs_len = 0; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1119 | uint16_t *p; |
| 1120 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1121 | #if defined(static_assert) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1122 | static_assert(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN |
| 1123 | <= (SIZE_MAX - (2 * sizeof(uint16_t))), |
| 1124 | "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big"); |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 1125 | #endif |
| 1126 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1127 | for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) { |
| 1128 | if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1129 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1130 | } |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1131 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1132 | sig_algs_len += sizeof(uint16_t); |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1133 | #endif |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 1134 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1135 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1136 | sig_algs_len += sizeof(uint16_t); |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1137 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1138 | if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) { |
| 1139 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
| 1140 | } |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1141 | } |
| 1142 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1143 | if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) { |
| 1144 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
| 1145 | } |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1146 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1147 | ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len + |
| 1148 | sizeof(uint16_t)); |
| 1149 | if (ssl->handshake->sig_algs == NULL) { |
| 1150 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 1151 | } |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1153 | p = (uint16_t *) ssl->handshake->sig_algs; |
| 1154 | for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) { |
| 1155 | unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md); |
| 1156 | if (hash == MBEDTLS_SSL_HASH_NONE) { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1157 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1158 | } |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 1159 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1160 | *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1161 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 1162 | #endif |
| 1163 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1164 | *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1165 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 1166 | #endif |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1167 | } |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 1168 | *p = MBEDTLS_TLS_SIG_NONE; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1169 | ssl->handshake->sig_algs_heap_allocated = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1170 | } else |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 1171 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1172 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1173 | ssl->handshake->sig_algs_heap_allocated = 0; |
| 1174 | } |
Jerry Yu | cc53910 | 2022-06-27 16:27:35 +0800 | [diff] [blame] | 1175 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1176 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1177 | return 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1180 | #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] | 1181 | /* Dummy cookie callbacks for defaults */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1182 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1183 | static int ssl_cookie_write_dummy(void *ctx, |
| 1184 | unsigned char **p, unsigned char *end, |
| 1185 | const unsigned char *cli_id, size_t cli_id_len) |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1186 | { |
| 1187 | ((void) ctx); |
| 1188 | ((void) p); |
| 1189 | ((void) end); |
| 1190 | ((void) cli_id); |
| 1191 | ((void) cli_id_len); |
| 1192 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1193 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1194 | } |
| 1195 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1196 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1197 | static int ssl_cookie_check_dummy(void *ctx, |
| 1198 | const unsigned char *cookie, size_t cookie_len, |
| 1199 | const unsigned char *cli_id, size_t cli_id_len) |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1200 | { |
| 1201 | ((void) ctx); |
| 1202 | ((void) cookie); |
| 1203 | ((void) cookie_len); |
| 1204 | ((void) cli_id); |
| 1205 | ((void) cli_id_len); |
| 1206 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1207 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1208 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1209 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1210 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1211 | /* |
| 1212 | * Initialize an SSL context |
| 1213 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1214 | void mbedtls_ssl_init(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1215 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1216 | memset(ssl, 0, sizeof(mbedtls_ssl_context)); |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1217 | } |
| 1218 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1219 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1220 | static int ssl_conf_version_check(const mbedtls_ssl_context *ssl) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1221 | { |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 1222 | const mbedtls_ssl_config *conf = ssl->conf; |
| 1223 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1224 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1225 | if (mbedtls_ssl_conf_is_tls13_only(conf)) { |
| 1226 | if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 1227 | MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported.")); |
| 1228 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1231 | MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only.")); |
| 1232 | return 0; |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1233 | } |
| 1234 | #endif |
| 1235 | |
| 1236 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1237 | if (mbedtls_ssl_conf_is_tls12_only(conf)) { |
| 1238 | MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only.")); |
| 1239 | return 0; |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1240 | } |
| 1241 | #endif |
| 1242 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1243 | #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] | 1244 | if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) { |
| 1245 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 1246 | MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2")); |
| 1247 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1250 | if (conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 1251 | MBEDTLS_SSL_DEBUG_MSG(1, ("TLS 1.3 server is not supported yet.")); |
| 1252 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
XiaokangQian | 8f9dfe4 | 2022-04-15 02:52:39 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1256 | MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2.")); |
| 1257 | return 0; |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1258 | } |
| 1259 | #endif |
| 1260 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1261 | MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid.")); |
| 1262 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1263 | } |
| 1264 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1265 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1266 | static int ssl_conf_check(const mbedtls_ssl_context *ssl) |
| 1267 | { |
| 1268 | int ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1269 | ret = ssl_conf_version_check(ssl); |
| 1270 | if (ret != 0) { |
| 1271 | return ret; |
| 1272 | } |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1273 | |
Jerry Yu | def7ae4 | 2022-10-30 14:13:19 +0800 | [diff] [blame] | 1274 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 1275 | /* RFC 8446 section 4.4.3 |
| 1276 | * |
| 1277 | * If the verification fails, the receiver MUST terminate the handshake with |
| 1278 | * a "decrypt_error" alert. |
| 1279 | * |
| 1280 | * If the client is configured as TLS 1.3 only with optional verify, return |
| 1281 | * bad config. |
| 1282 | * |
| 1283 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1284 | if (mbedtls_ssl_conf_tls13_ephemeral_enabled( |
| 1285 | (mbedtls_ssl_context *) ssl) && |
Jerry Yu | def7ae4 | 2022-10-30 14:13:19 +0800 | [diff] [blame] | 1286 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 1287 | ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 && |
| 1288 | ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1289 | ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) { |
Jerry Yu | def7ae4 | 2022-10-30 14:13:19 +0800 | [diff] [blame] | 1290 | MBEDTLS_SSL_DEBUG_MSG( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1291 | 1, ("Optional verify auth mode " |
| 1292 | "is not available for TLS 1.3 client")); |
| 1293 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
Jerry Yu | def7ae4 | 2022-10-30 14:13:19 +0800 | [diff] [blame] | 1294 | } |
| 1295 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 1296 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1297 | /* Space for further checks */ |
| 1298 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1299 | return 0; |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1300 | } |
| 1301 | |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1302 | /* |
| 1303 | * Setup an SSL context |
| 1304 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 1305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1306 | int mbedtls_ssl_setup(mbedtls_ssl_context *ssl, |
| 1307 | const mbedtls_ssl_config *conf) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1308 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1309 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1310 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1311 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1312 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 1313 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1314 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1315 | if ((ret = ssl_conf_check(ssl)) != 0) { |
| 1316 | return ret; |
| 1317 | } |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1318 | |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1319 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 1320 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1321 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 1322 | |
| 1323 | /* Set to NULL in case of an error condition */ |
| 1324 | ssl->out_buf = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1325 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1326 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1327 | ssl->in_buf_len = in_buf_len; |
| 1328 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1329 | ssl->in_buf = mbedtls_calloc(1, in_buf_len); |
| 1330 | if (ssl->in_buf == NULL) { |
| 1331 | 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] | 1332 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1333 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1334 | } |
| 1335 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1336 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1337 | ssl->out_buf_len = out_buf_len; |
| 1338 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1339 | ssl->out_buf = mbedtls_calloc(1, out_buf_len); |
| 1340 | if (ssl->out_buf == NULL) { |
| 1341 | 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] | 1342 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1343 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1346 | mbedtls_ssl_reset_in_out_pointers(ssl); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 1347 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1348 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1349 | memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info)); |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1350 | #endif |
| 1351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1352 | if ((ret = ssl_handshake_init(ssl)) != 0) { |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1353 | goto error; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1354 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1356 | return 0; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1357 | |
| 1358 | error: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1359 | mbedtls_free(ssl->in_buf); |
| 1360 | mbedtls_free(ssl->out_buf); |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1361 | |
| 1362 | ssl->conf = NULL; |
| 1363 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1364 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1365 | ssl->in_buf_len = 0; |
| 1366 | ssl->out_buf_len = 0; |
| 1367 | #endif |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1368 | ssl->in_buf = NULL; |
| 1369 | ssl->out_buf = NULL; |
| 1370 | |
| 1371 | ssl->in_hdr = NULL; |
| 1372 | ssl->in_ctr = NULL; |
| 1373 | ssl->in_len = NULL; |
| 1374 | ssl->in_iv = NULL; |
| 1375 | ssl->in_msg = NULL; |
| 1376 | |
| 1377 | ssl->out_hdr = NULL; |
| 1378 | ssl->out_ctr = NULL; |
| 1379 | ssl->out_len = NULL; |
| 1380 | ssl->out_iv = NULL; |
| 1381 | ssl->out_msg = NULL; |
| 1382 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1383 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1387 | * Reset an initialized and used SSL context for re-use while retaining |
| 1388 | * all application-set variables, function pointers and data. |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1389 | * |
| 1390 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 1391 | * (Use when a DTLS client reconnects from the same port.) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1392 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1393 | void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl, |
| 1394 | int partial) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1395 | { |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1396 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1397 | size_t in_buf_len = ssl->in_buf_len; |
| 1398 | size_t out_buf_len = ssl->out_buf_len; |
| 1399 | #else |
| 1400 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1401 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 1402 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1403 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1404 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C) |
| 1405 | partial = 0; |
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 1406 | #endif |
| 1407 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1408 | /* Cancel any possibly running timer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1409 | mbedtls_ssl_set_timer(ssl, 0); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1410 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1411 | mbedtls_ssl_reset_in_out_pointers(ssl); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1412 | |
| 1413 | /* Reset incoming message parsing */ |
| 1414 | ssl->in_offt = NULL; |
| 1415 | ssl->nb_zero = 0; |
| 1416 | ssl->in_msgtype = 0; |
| 1417 | ssl->in_msglen = 0; |
| 1418 | ssl->in_hslen = 0; |
| 1419 | ssl->keep_current_message = 0; |
| 1420 | ssl->transform_in = NULL; |
| 1421 | |
| 1422 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1423 | ssl->next_record_offset = 0; |
| 1424 | ssl->in_epoch = 0; |
| 1425 | #endif |
| 1426 | |
| 1427 | /* Keep current datagram if partial == 1 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1428 | if (partial == 0) { |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1429 | ssl->in_left = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1430 | memset(ssl->in_buf, 0, in_buf_len); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1431 | } |
| 1432 | |
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 1433 | ssl->send_alert = 0; |
| 1434 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1435 | /* Reset outgoing message writing */ |
| 1436 | ssl->out_msgtype = 0; |
| 1437 | ssl->out_msglen = 0; |
| 1438 | ssl->out_left = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1439 | memset(ssl->out_buf, 0, out_buf_len); |
| 1440 | memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr)); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1441 | ssl->transform_out = NULL; |
| 1442 | |
| 1443 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1444 | mbedtls_ssl_dtls_replay_reset(ssl); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1445 | #endif |
| 1446 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1447 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1448 | if (ssl->transform) { |
| 1449 | mbedtls_ssl_transform_free(ssl->transform); |
| 1450 | mbedtls_free(ssl->transform); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1451 | ssl->transform = NULL; |
| 1452 | } |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1453 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 1454 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1455 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1456 | mbedtls_ssl_transform_free(ssl->transform_application); |
| 1457 | mbedtls_free(ssl->transform_application); |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1458 | ssl->transform_application = NULL; |
| 1459 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1460 | if (ssl->handshake != NULL) { |
Jerry Yu | 3d9b590 | 2022-11-04 14:07:25 +0800 | [diff] [blame] | 1461 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1462 | mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata); |
| 1463 | mbedtls_free(ssl->handshake->transform_earlydata); |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1464 | ssl->handshake->transform_earlydata = NULL; |
Jerry Yu | 3d9b590 | 2022-11-04 14:07:25 +0800 | [diff] [blame] | 1465 | #endif |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1466 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1467 | mbedtls_ssl_transform_free(ssl->handshake->transform_handshake); |
| 1468 | mbedtls_free(ssl->handshake->transform_handshake); |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1469 | ssl->handshake->transform_handshake = NULL; |
| 1470 | } |
| 1471 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1472 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1473 | } |
| 1474 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1475 | int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial) |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1476 | { |
| 1477 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1478 | |
| 1479 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 1480 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1481 | mbedtls_ssl_session_reset_msg_layer(ssl, partial); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1482 | |
| 1483 | /* Reset renegotiation state */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1484 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1485 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1486 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1487 | |
| 1488 | ssl->verify_data_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1489 | memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN); |
| 1490 | 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] | 1491 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1492 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1493 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1494 | ssl->session_in = NULL; |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 1495 | ssl->session_out = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1496 | if (ssl->session) { |
| 1497 | mbedtls_ssl_session_free(ssl->session); |
| 1498 | mbedtls_free(ssl->session); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1499 | ssl->session = NULL; |
| 1500 | } |
| 1501 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1502 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 1503 | ssl->alpn_chosen = NULL; |
| 1504 | #endif |
| 1505 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1506 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 1507 | int free_cli_id = 1; |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1508 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1509 | free_cli_id = (partial == 0); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1510 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1511 | if (free_cli_id) { |
| 1512 | mbedtls_free(ssl->cli_id); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1513 | ssl->cli_id = NULL; |
| 1514 | ssl->cli_id_len = 0; |
| 1515 | } |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 1516 | #endif |
| 1517 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1518 | if ((ret = ssl_handshake_init(ssl)) != 0) { |
| 1519 | return ret; |
| 1520 | } |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1521 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1522 | return 0; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1523 | } |
| 1524 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 1525 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1526 | * Reset an initialized and used SSL context for re-use while retaining |
| 1527 | * all application-set variables, function pointers and data. |
| 1528 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1529 | int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1530 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1531 | return mbedtls_ssl_session_reset_int(ssl, 0); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1535 | * SSL set accessors |
| 1536 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1537 | void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1538 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1539 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1542 | 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] | 1543 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1544 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 1545 | } |
| 1546 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1547 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1548 | 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] | 1549 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1550 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 1551 | } |
| 1552 | #endif |
| 1553 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1554 | 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] | 1555 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1556 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1557 | } |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1558 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1559 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1560 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1561 | void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl, |
| 1562 | unsigned allow_packing) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1563 | { |
| 1564 | ssl->disable_datagram_packing = !allow_packing; |
| 1565 | } |
| 1566 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1567 | void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf, |
| 1568 | uint32_t min, uint32_t max) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1569 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1570 | conf->hs_timeout_min = min; |
| 1571 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1572 | } |
| 1573 | #endif |
| 1574 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1575 | void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1576 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1577 | conf->authmode = authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1580 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1581 | void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf, |
| 1582 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 1583 | void *p_vrfy) |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1584 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1585 | conf->f_vrfy = f_vrfy; |
| 1586 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1587 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1588 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1589 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1590 | void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf, |
| 1591 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1592 | void *p_rng) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1593 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1594 | conf->f_rng = f_rng; |
| 1595 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1598 | void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf, |
| 1599 | void (*f_dbg)(void *, int, const char *, int, const char *), |
| 1600 | void *p_dbg) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1601 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1602 | conf->f_dbg = f_dbg; |
| 1603 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1606 | void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl, |
| 1607 | void *p_bio, |
| 1608 | mbedtls_ssl_send_t *f_send, |
| 1609 | mbedtls_ssl_recv_t *f_recv, |
| 1610 | mbedtls_ssl_recv_timeout_t *f_recv_timeout) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1611 | { |
| 1612 | ssl->p_bio = p_bio; |
| 1613 | ssl->f_send = f_send; |
| 1614 | ssl->f_recv = f_recv; |
| 1615 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 1616 | } |
| 1617 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 1618 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1619 | 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] | 1620 | { |
| 1621 | ssl->mtu = mtu; |
| 1622 | } |
| 1623 | #endif |
| 1624 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1625 | 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] | 1626 | { |
| 1627 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1628 | } |
| 1629 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1630 | void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl, |
| 1631 | void *p_timer, |
| 1632 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 1633 | mbedtls_ssl_get_timer_t *f_get_timer) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1634 | { |
| 1635 | ssl->p_timer = p_timer; |
| 1636 | ssl->f_set_timer = f_set_timer; |
| 1637 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1638 | |
| 1639 | /* Make sure we start with no timer running */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1640 | mbedtls_ssl_set_timer(ssl, 0); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1641 | } |
| 1642 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1643 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1644 | void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf, |
| 1645 | void *p_cache, |
| 1646 | mbedtls_ssl_cache_get_t *f_get_cache, |
| 1647 | mbedtls_ssl_cache_set_t *f_set_cache) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1648 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 1649 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1650 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1651 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1652 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1653 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1654 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1655 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1656 | 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] | 1657 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1658 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1659 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1660 | if (ssl == NULL || |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1661 | session == NULL || |
| 1662 | ssl->session_negotiate == NULL || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1663 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) { |
| 1664 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1665 | } |
| 1666 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1667 | if (ssl->handshake->resume == 1) { |
| 1668 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 1669 | } |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 1670 | |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1671 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1672 | if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1673 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1674 | mbedtls_ssl_ciphersuite_from_id(session->ciphersuite); |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1676 | if (mbedtls_ssl_validate_ciphersuite( |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1677 | ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1678 | MBEDTLS_SSL_VERSION_TLS1_3) != 0) { |
| 1679 | MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.", |
| 1680 | session->ciphersuite)); |
| 1681 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1682 | } |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1683 | } |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1684 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1685 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1686 | if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate, |
| 1687 | session)) != 0) { |
| 1688 | return ret; |
| 1689 | } |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1690 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1691 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1692 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1693 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1694 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1695 | #endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1696 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1697 | void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf, |
| 1698 | const int *ciphersuites) |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1699 | { |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 1700 | conf->ciphersuite_list = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1701 | } |
| 1702 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1703 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1704 | void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf, |
| 1705 | const int kex_modes) |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1706 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1707 | 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] | 1708 | } |
Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame] | 1709 | |
| 1710 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1711 | void mbedtls_ssl_tls13_conf_early_data(mbedtls_ssl_config *conf, |
| 1712 | int early_data_enabled) |
Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame] | 1713 | { |
| 1714 | conf->early_data_enabled = early_data_enabled; |
| 1715 | } |
Jerry Yu | cc4e007 | 2022-11-22 17:22:22 +0800 | [diff] [blame] | 1716 | |
| 1717 | #if defined(MBEDTLS_SSL_SRV_C) |
| 1718 | void mbedtls_ssl_tls13_conf_max_early_data_size( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1719 | mbedtls_ssl_config *conf, uint32_t max_early_data_size) |
Jerry Yu | cc4e007 | 2022-11-22 17:22:22 +0800 | [diff] [blame] | 1720 | { |
Jerry Yu | 39da985 | 2022-12-06 16:58:36 +0800 | [diff] [blame] | 1721 | conf->max_early_data_size = max_early_data_size; |
Jerry Yu | cc4e007 | 2022-11-22 17:22:22 +0800 | [diff] [blame] | 1722 | } |
| 1723 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 1724 | |
Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame] | 1725 | #endif /* MBEDTLS_SSL_EARLY_DATA */ |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1726 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1727 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1728 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1729 | void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf, |
| 1730 | const mbedtls_x509_crt_profile *profile) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1731 | { |
| 1732 | conf->cert_profile = profile; |
| 1733 | } |
| 1734 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1735 | static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert) |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1736 | { |
| 1737 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
| 1738 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1739 | while (cur != NULL) { |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1740 | next = cur->next; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1741 | mbedtls_free(cur); |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1742 | cur = next; |
| 1743 | } |
| 1744 | } |
| 1745 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1746 | /* Append a new keycert entry to a (possibly empty) list */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1747 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1748 | static int ssl_append_key_cert(mbedtls_ssl_key_cert **head, |
| 1749 | mbedtls_x509_crt *cert, |
| 1750 | mbedtls_pk_context *key) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1751 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1752 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1753 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1754 | if (cert == NULL) { |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1755 | /* Free list if cert is null */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1756 | ssl_key_cert_free(*head); |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1757 | *head = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1758 | return 0; |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1759 | } |
| 1760 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1761 | new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert)); |
| 1762 | if (new_cert == NULL) { |
| 1763 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 1764 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1765 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1766 | new_cert->cert = cert; |
| 1767 | new_cert->key = key; |
| 1768 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1769 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1770 | /* Update head if the list was null, else add to the end */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1771 | if (*head == NULL) { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1772 | *head = new_cert; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1773 | } else { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1774 | mbedtls_ssl_key_cert *cur = *head; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1775 | while (cur->next != NULL) { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1776 | cur = cur->next; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1777 | } |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1778 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1779 | } |
| 1780 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1781 | return 0; |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1782 | } |
| 1783 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1784 | int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1785 | mbedtls_x509_crt *own_cert, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1786 | mbedtls_pk_context *pk_key) |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1787 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1788 | 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] | 1789 | } |
| 1790 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1791 | void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1792 | mbedtls_x509_crt *ca_chain, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1793 | mbedtls_x509_crl *ca_crl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1794 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1795 | conf->ca_chain = ca_chain; |
| 1796 | conf->ca_crl = ca_crl; |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1797 | |
| 1798 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1799 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1800 | * cannot be used together. */ |
| 1801 | conf->f_ca_cb = NULL; |
| 1802 | conf->p_ca_cb = NULL; |
| 1803 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1804 | } |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1805 | |
| 1806 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1807 | void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf, |
| 1808 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
| 1809 | void *p_ca_cb) |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1810 | { |
| 1811 | conf->f_ca_cb = f_ca_cb; |
| 1812 | conf->p_ca_cb = p_ca_cb; |
| 1813 | |
| 1814 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1815 | * cannot be used together. */ |
| 1816 | conf->ca_chain = NULL; |
| 1817 | conf->ca_crl = NULL; |
| 1818 | } |
| 1819 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1820 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 1821 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1822 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1823 | const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl, |
| 1824 | size_t *name_len) |
Glenn Strauss | 6989407 | 2022-01-24 12:58:00 -0500 | [diff] [blame] | 1825 | { |
| 1826 | *name_len = ssl->handshake->sni_name_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1827 | return ssl->handshake->sni_name; |
Glenn Strauss | 6989407 | 2022-01-24 12:58:00 -0500 | [diff] [blame] | 1828 | } |
| 1829 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1830 | int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl, |
| 1831 | mbedtls_x509_crt *own_cert, |
| 1832 | mbedtls_pk_context *pk_key) |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1833 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1834 | return ssl_append_key_cert(&ssl->handshake->sni_key_cert, |
| 1835 | own_cert, pk_key); |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1836 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 1837 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1838 | void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl, |
| 1839 | mbedtls_x509_crt *ca_chain, |
| 1840 | mbedtls_x509_crl *ca_crl) |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 1841 | { |
| 1842 | ssl->handshake->sni_ca_chain = ca_chain; |
| 1843 | ssl->handshake->sni_ca_crl = ca_crl; |
| 1844 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1845 | |
Glenn Strauss | 999ef70 | 2022-03-11 01:37:23 -0500 | [diff] [blame] | 1846 | #if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1847 | void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl, |
| 1848 | const mbedtls_x509_crt *crt) |
Glenn Strauss | 999ef70 | 2022-03-11 01:37:23 -0500 | [diff] [blame] | 1849 | { |
| 1850 | ssl->handshake->dn_hints = crt; |
| 1851 | } |
| 1852 | #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ |
| 1853 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1854 | void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl, |
| 1855 | int authmode) |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1856 | { |
| 1857 | ssl->handshake->sni_authmode = authmode; |
| 1858 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1859 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 1860 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1861 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1862 | void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl, |
| 1863 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 1864 | void *p_vrfy) |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1865 | { |
| 1866 | ssl->f_vrfy = f_vrfy; |
| 1867 | ssl->p_vrfy = p_vrfy; |
| 1868 | } |
| 1869 | #endif |
| 1870 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1871 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1872 | |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1873 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1874 | static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1875 | mbedtls_ssl_context *ssl, |
| 1876 | mbedtls_svc_key_id_t pwd) |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1877 | { |
| 1878 | psa_status_t status; |
| 1879 | psa_pake_role_t psa_role; |
| 1880 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 1881 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1882 | psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE); |
| 1883 | psa_pake_cs_set_primitive(&cipher_suite, |
| 1884 | PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, |
| 1885 | PSA_ECC_FAMILY_SECP_R1, |
| 1886 | 256)); |
| 1887 | psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256); |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1888 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1889 | status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite); |
| 1890 | if (status != PSA_SUCCESS) { |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1891 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1892 | } |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1893 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1894 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1895 | psa_role = PSA_PAKE_ROLE_SERVER; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1896 | } else { |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1897 | psa_role = PSA_PAKE_ROLE_CLIENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1898 | } |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1899 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1900 | status = psa_pake_set_role(&ssl->handshake->psa_pake_ctx, psa_role); |
| 1901 | if (status != PSA_SUCCESS) { |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1902 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1903 | } |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1904 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1905 | status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd); |
| 1906 | if (status != PSA_SUCCESS) { |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1907 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1908 | } |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1909 | |
| 1910 | ssl->handshake->psa_pake_ctx_is_ok = 1; |
| 1911 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1912 | return PSA_SUCCESS; |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1913 | } |
| 1914 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1915 | int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl, |
| 1916 | const unsigned char *pw, |
| 1917 | size_t pw_len) |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1918 | { |
| 1919 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1920 | psa_status_t status; |
| 1921 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1922 | if (ssl->handshake == NULL || ssl->conf == NULL) { |
| 1923 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1924 | } |
| 1925 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1926 | /* Empty password is not valid */ |
| 1927 | if ((pw == NULL) || (pw_len == 0)) { |
| 1928 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 1929 | } |
| 1930 | |
| 1931 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 1932 | psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE); |
| 1933 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 1934 | |
| 1935 | status = psa_import_key(&attributes, pw, pw_len, |
| 1936 | &ssl->handshake->psa_pake_password); |
| 1937 | if (status != PSA_SUCCESS) { |
| 1938 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 1939 | } |
| 1940 | |
| 1941 | status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, |
| 1942 | ssl->handshake->psa_pake_password); |
| 1943 | if (status != PSA_SUCCESS) { |
| 1944 | psa_destroy_key(ssl->handshake->psa_pake_password); |
| 1945 | psa_pake_abort(&ssl->handshake->psa_pake_ctx); |
| 1946 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 1947 | } |
| 1948 | |
| 1949 | return 0; |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 1950 | } |
Valerio Setti | a9a97dc | 2022-11-28 18:26:16 +0100 | [diff] [blame] | 1951 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1952 | int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl, |
| 1953 | mbedtls_svc_key_id_t pwd) |
Valerio Setti | a9a97dc | 2022-11-28 18:26:16 +0100 | [diff] [blame] | 1954 | { |
Valerio Setti | a9a97dc | 2022-11-28 18:26:16 +0100 | [diff] [blame] | 1955 | psa_status_t status; |
| 1956 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1957 | if (ssl->handshake == NULL || ssl->conf == NULL) { |
| 1958 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1959 | } |
| 1960 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1961 | if (mbedtls_svc_key_id_is_null(pwd)) { |
| 1962 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 1963 | } |
| 1964 | |
| 1965 | status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd); |
| 1966 | if (status != PSA_SUCCESS) { |
| 1967 | psa_pake_abort(&ssl->handshake->psa_pake_ctx); |
| 1968 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 1969 | } |
| 1970 | |
| 1971 | return 0; |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 1972 | } |
| 1973 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1974 | int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl, |
| 1975 | const unsigned char *pw, |
| 1976 | size_t pw_len) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1977 | { |
| 1978 | mbedtls_ecjpake_role role; |
| 1979 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1980 | if (ssl->handshake == NULL || ssl->conf == NULL) { |
| 1981 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 1982 | } |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1983 | |
Valerio Setti | c689ed8 | 2022-12-07 14:40:38 +0100 | [diff] [blame] | 1984 | /* Empty password is not valid */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1985 | if ((pw == NULL) || (pw_len == 0)) { |
| 1986 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 1987 | } |
Valerio Setti | c689ed8 | 2022-12-07 14:40:38 +0100 | [diff] [blame] | 1988 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1989 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1990 | role = MBEDTLS_ECJPAKE_SERVER; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1991 | } else { |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1992 | role = MBEDTLS_ECJPAKE_CLIENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1993 | } |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1994 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1995 | return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx, |
| 1996 | role, |
| 1997 | MBEDTLS_MD_SHA256, |
| 1998 | MBEDTLS_ECP_DP_SECP256R1, |
| 1999 | pw, pw_len); |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2000 | } |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 2001 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 2002 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 2003 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 2004 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2005 | int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2006 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2007 | if (conf->psk_identity == NULL || |
| 2008 | conf->psk_identity_len == 0) { |
| 2009 | return 0; |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 2010 | } |
| 2011 | |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2012 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2013 | if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) { |
| 2014 | return 1; |
| 2015 | } |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2016 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 2017 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2018 | if (conf->psk != NULL && conf->psk_len != 0) { |
| 2019 | return 1; |
| 2020 | } |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2021 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2022 | return 0; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2023 | } |
| 2024 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2025 | static void ssl_conf_remove_psk(mbedtls_ssl_config *conf) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2026 | { |
| 2027 | /* Remove reference to existing PSK, if any. */ |
| 2028 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2029 | if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) { |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2030 | /* The maintenance of the PSK key slot is the |
| 2031 | * user's responsibility. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2032 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2033 | } |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2034 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2035 | if (conf->psk != NULL) { |
| 2036 | mbedtls_platform_zeroize(conf->psk, conf->psk_len); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2037 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2038 | mbedtls_free(conf->psk); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2039 | conf->psk = NULL; |
| 2040 | conf->psk_len = 0; |
| 2041 | } |
| 2042 | |
| 2043 | /* Remove reference to PSK identity, if any. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2044 | if (conf->psk_identity != NULL) { |
| 2045 | mbedtls_free(conf->psk_identity); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2046 | conf->psk_identity = NULL; |
| 2047 | conf->psk_identity_len = 0; |
| 2048 | } |
| 2049 | } |
| 2050 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2051 | /* This function assumes that PSK identity in the SSL config is unset. |
| 2052 | * It checks that the provided identity is well-formed and attempts |
| 2053 | * to make a copy of it in the SSL config. |
| 2054 | * On failure, the PSK identity in the config remains unset. */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 2055 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2056 | static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf, |
| 2057 | unsigned char const *psk_identity, |
| 2058 | size_t psk_identity_len) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2059 | { |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 2060 | /* Identity len will be encoded on two bytes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2061 | if (psk_identity == NULL || |
Ronald Cron | 2a87e9b | 2022-10-19 10:55:26 +0200 | [diff] [blame] | 2062 | psk_identity_len == 0 || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2063 | (psk_identity_len >> 16) != 0 || |
| 2064 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) { |
| 2065 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 2066 | } |
| 2067 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2068 | conf->psk_identity = mbedtls_calloc(1, psk_identity_len); |
| 2069 | if (conf->psk_identity == NULL) { |
| 2070 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2071 | } |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 2072 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 2073 | conf->psk_identity_len = psk_identity_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2074 | memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len); |
Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 2075 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2076 | return 0; |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 2077 | } |
| 2078 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2079 | int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf, |
| 2080 | const unsigned char *psk, size_t psk_len, |
| 2081 | const unsigned char *psk_identity, size_t psk_identity_len) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2082 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2083 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2084 | |
| 2085 | /* We currently only support one PSK, raw or opaque. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2086 | if (mbedtls_ssl_conf_has_static_psk(conf)) { |
| 2087 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 2088 | } |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2089 | |
| 2090 | /* Check and set raw PSK */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2091 | if (psk == NULL) { |
| 2092 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2093 | } |
| 2094 | if (psk_len == 0) { |
| 2095 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2096 | } |
| 2097 | if (psk_len > MBEDTLS_PSK_MAX_LEN) { |
| 2098 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2099 | } |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 2100 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2101 | if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) { |
| 2102 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2103 | } |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2104 | conf->psk_len = psk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2105 | memcpy(conf->psk, psk, conf->psk_len); |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2106 | |
| 2107 | /* Check and set PSK Identity */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2108 | ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len); |
| 2109 | if (ret != 0) { |
| 2110 | ssl_conf_remove_psk(conf); |
| 2111 | } |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2112 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2113 | return ret; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2114 | } |
| 2115 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2116 | static void ssl_remove_psk(mbedtls_ssl_context *ssl) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2117 | { |
| 2118 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2119 | if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2120 | /* The maintenance of the external PSK key slot is the |
| 2121 | * user's responsibility. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2122 | if (ssl->handshake->psk_opaque_is_internal) { |
| 2123 | psa_destroy_key(ssl->handshake->psk_opaque); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2124 | ssl->handshake->psk_opaque_is_internal = 0; |
| 2125 | } |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2126 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2127 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 2128 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2129 | if (ssl->handshake->psk != NULL) { |
| 2130 | mbedtls_platform_zeroize(ssl->handshake->psk, |
| 2131 | ssl->handshake->psk_len); |
| 2132 | mbedtls_free(ssl->handshake->psk); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2133 | ssl->handshake->psk_len = 0; |
| 2134 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 2135 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2136 | } |
| 2137 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2138 | int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl, |
| 2139 | const unsigned char *psk, size_t psk_len) |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2140 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2141 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2142 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
Jerry Yu | 5d01c05 | 2022-08-17 10:18:10 +0800 | [diff] [blame] | 2143 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 24b8c81 | 2022-08-20 19:06:56 +0800 | [diff] [blame] | 2144 | psa_algorithm_t alg = PSA_ALG_NONE; |
Jerry Yu | 5d01c05 | 2022-08-17 10:18:10 +0800 | [diff] [blame] | 2145 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2146 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2147 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2148 | if (psk == NULL || ssl->handshake == NULL) { |
| 2149 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2150 | } |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2151 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2152 | if (psk_len > MBEDTLS_PSK_MAX_LEN) { |
| 2153 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2154 | } |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2155 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2156 | ssl_remove_psk(ssl); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2157 | |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2158 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 2159 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2160 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) { |
| 2161 | if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) { |
| 2162 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
| 2163 | } else { |
| 2164 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 2165 | } |
| 2166 | psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE); |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 2167 | } |
| 2168 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2169 | |
Jerry Yu | 568ec25 | 2022-07-22 21:27:34 +0800 | [diff] [blame] | 2170 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2171 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
| 2172 | alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH); |
| 2173 | psa_set_key_usage_flags(&key_attributes, |
| 2174 | PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT); |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 2175 | } |
| 2176 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 2177 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2178 | psa_set_key_algorithm(&key_attributes, alg); |
| 2179 | psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2181 | status = psa_import_key(&key_attributes, psk, psk_len, &key); |
| 2182 | if (status != PSA_SUCCESS) { |
| 2183 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 2184 | } |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2185 | |
| 2186 | /* Allow calling psa_destroy_key() on psk remove */ |
| 2187 | ssl->handshake->psk_opaque_is_internal = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2188 | return mbedtls_ssl_set_hs_psk_opaque(ssl, key); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2189 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2190 | if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) { |
| 2191 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2192 | } |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2193 | |
| 2194 | ssl->handshake->psk_len = psk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2195 | memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2196 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2197 | return 0; |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2198 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2199 | } |
| 2200 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2201 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2202 | int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf, |
| 2203 | mbedtls_svc_key_id_t psk, |
| 2204 | const unsigned char *psk_identity, |
| 2205 | size_t psk_identity_len) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2206 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2207 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2208 | |
| 2209 | /* We currently only support one PSK, raw or opaque. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2210 | if (mbedtls_ssl_conf_has_static_psk(conf)) { |
| 2211 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 2212 | } |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2213 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2214 | /* Check and set opaque PSK */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2215 | if (mbedtls_svc_key_id_is_null(psk)) { |
| 2216 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2217 | } |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2218 | conf->psk_opaque = psk; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2219 | |
| 2220 | /* Check and set PSK Identity */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2221 | ret = ssl_conf_set_psk_identity(conf, psk_identity, |
| 2222 | psk_identity_len); |
| 2223 | if (ret != 0) { |
| 2224 | ssl_conf_remove_psk(conf); |
| 2225 | } |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2226 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2227 | return ret; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2228 | } |
| 2229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2230 | int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl, |
| 2231 | mbedtls_svc_key_id_t psk) |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2232 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2233 | if ((mbedtls_svc_key_id_is_null(psk)) || |
| 2234 | (ssl->handshake == NULL)) { |
| 2235 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2236 | } |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2237 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2238 | ssl_remove_psk(ssl); |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2239 | ssl->handshake->psk_opaque = psk; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2240 | return 0; |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2241 | } |
| 2242 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2243 | |
Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 2244 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2245 | void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf, |
| 2246 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
| 2247 | size_t), |
| 2248 | void *p_psk) |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2249 | { |
| 2250 | conf->f_psk = f_psk; |
| 2251 | conf->p_psk = p_psk; |
| 2252 | } |
Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 2253 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 2254 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 2255 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2256 | |
| 2257 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2258 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2259 | psa_algorithm_t alg) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2260 | { |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2261 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2262 | if (alg == PSA_ALG_CBC_NO_PADDING) { |
| 2263 | return MBEDTLS_SSL_MODE_CBC; |
| 2264 | } |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2265 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2266 | if (PSA_ALG_IS_AEAD(alg)) { |
| 2267 | return MBEDTLS_SSL_MODE_AEAD; |
| 2268 | } |
| 2269 | return MBEDTLS_SSL_MODE_STREAM; |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2270 | } |
| 2271 | |
| 2272 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2273 | |
| 2274 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2275 | mbedtls_cipher_mode_t mode) |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2276 | { |
| 2277 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2278 | if (mode == MBEDTLS_MODE_CBC) { |
| 2279 | return MBEDTLS_SSL_MODE_CBC; |
| 2280 | } |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2281 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 2282 | |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2283 | #if defined(MBEDTLS_GCM_C) || \ |
| 2284 | defined(MBEDTLS_CCM_C) || \ |
| 2285 | defined(MBEDTLS_CHACHAPOLY_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2286 | if (mode == MBEDTLS_MODE_GCM || |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2287 | mode == MBEDTLS_MODE_CCM || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2288 | mode == MBEDTLS_MODE_CHACHAPOLY) { |
| 2289 | return MBEDTLS_SSL_MODE_AEAD; |
| 2290 | } |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2291 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2292 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2293 | return MBEDTLS_SSL_MODE_STREAM; |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2294 | } |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2295 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2296 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2297 | static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode( |
| 2298 | mbedtls_ssl_mode_t base_mode, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2299 | int encrypt_then_mac) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2300 | { |
| 2301 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2302 | if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED && |
| 2303 | base_mode == MBEDTLS_SSL_MODE_CBC) { |
| 2304 | return MBEDTLS_SSL_MODE_CBC_ETM; |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2305 | } |
| 2306 | #else |
| 2307 | (void) encrypt_then_mac; |
| 2308 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2309 | return base_mode; |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2310 | } |
| 2311 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 2312 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2313 | const mbedtls_ssl_transform *transform) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2314 | { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2315 | mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode( |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2316 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2317 | transform->psa_alg |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2318 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2319 | mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2320 | #endif |
| 2321 | ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2322 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2323 | int encrypt_then_mac = 0; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2324 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2325 | encrypt_then_mac = transform->encrypt_then_mac; |
| 2326 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2327 | return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2328 | } |
| 2329 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 2330 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2331 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2332 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2333 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2334 | const mbedtls_ssl_ciphersuite_t *suite) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2335 | { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2336 | mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM; |
| 2337 | |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2338 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2339 | psa_status_t status; |
| 2340 | psa_algorithm_t alg; |
| 2341 | psa_key_type_t type; |
| 2342 | size_t size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2343 | status = mbedtls_ssl_cipher_to_psa(suite->cipher, 0, &alg, &type, &size); |
| 2344 | if (status == PSA_SUCCESS) { |
| 2345 | base_mode = mbedtls_ssl_get_base_mode(alg); |
| 2346 | } |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2347 | #else |
| 2348 | const mbedtls_cipher_info_t *cipher = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2349 | mbedtls_cipher_info_from_type(suite->cipher); |
| 2350 | if (cipher != NULL) { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2351 | base_mode = |
| 2352 | mbedtls_ssl_get_base_mode( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2353 | mbedtls_cipher_info_get_mode(cipher)); |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2354 | } |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2355 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2356 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2357 | #if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 2358 | int encrypt_then_mac = 0; |
| 2359 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2360 | return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2361 | } |
| 2362 | |
Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 2363 | #if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2364 | |
| 2365 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2366 | /* Serialization of TLS 1.3 sessions: |
| 2367 | * |
| 2368 | * struct { |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 2369 | * opaque hostname<0..2^16-1>; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2370 | * uint64 ticket_received; |
| 2371 | * uint32 ticket_lifetime; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2372 | * opaque ticket<1..2^16-1>; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2373 | * } ClientOnlyData; |
| 2374 | * |
| 2375 | * struct { |
| 2376 | * uint8 endpoint; |
| 2377 | * uint8 ciphersuite[2]; |
| 2378 | * uint32 ticket_age_add; |
| 2379 | * uint8 ticket_flags; |
| 2380 | * opaque resumption_key<0..255>; |
| 2381 | * select ( endpoint ) { |
| 2382 | * case client: ClientOnlyData; |
| 2383 | * case server: uint64 start_time; |
| 2384 | * }; |
| 2385 | * } serialized_session_tls13; |
| 2386 | * |
| 2387 | */ |
| 2388 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2389 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2390 | static int ssl_tls13_session_save(const mbedtls_ssl_session *session, |
| 2391 | unsigned char *buf, |
| 2392 | size_t buf_len, |
| 2393 | size_t *olen) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2394 | { |
| 2395 | unsigned char *p = buf; |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 2396 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
| 2397 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2398 | size_t hostname_len = (session->hostname == NULL) ? |
| 2399 | 0 : strlen(session->hostname) + 1; |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 2400 | #endif |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2401 | size_t needed = 1 /* endpoint */ |
| 2402 | + 2 /* ciphersuite */ |
| 2403 | + 4 /* ticket_age_add */ |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2404 | + 1 /* ticket_flags */ |
| 2405 | + 1; /* resumption_key length */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2406 | *olen = 0; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2407 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2408 | if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) { |
| 2409 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2410 | } |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2411 | needed += session->resumption_key_len; /* resumption_key */ |
| 2412 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2413 | #if defined(MBEDTLS_HAVE_TIME) |
| 2414 | needed += 8; /* start_time or ticket_received */ |
| 2415 | #endif |
| 2416 | |
| 2417 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2418 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 2419 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Xiaokang Qian | bc663a0 | 2022-10-09 11:14:39 +0000 | [diff] [blame] | 2420 | needed += 2 /* hostname_len */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2421 | + hostname_len; /* hostname */ |
Xiaokang Qian | 281fd1b | 2022-09-20 11:35:41 +0000 | [diff] [blame] | 2422 | #endif |
| 2423 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2424 | needed += 4 /* ticket_lifetime */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2425 | + 2; /* ticket_len */ |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2426 | |
| 2427 | /* Check size_t overflow */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2428 | if (session->ticket_len > SIZE_MAX - needed) { |
| 2429 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2430 | } |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2431 | |
Jerry Yu | e28d974 | 2022-08-18 15:44:03 +0800 | [diff] [blame] | 2432 | needed += session->ticket_len; /* ticket */ |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2433 | } |
| 2434 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 2435 | |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2436 | *olen = needed; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2437 | if (needed > buf_len) { |
| 2438 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 2439 | } |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2440 | |
| 2441 | p[0] = session->endpoint; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2442 | MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 1); |
| 2443 | MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 3); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2444 | p[7] = session->ticket_flags; |
| 2445 | |
| 2446 | /* save resumption_key */ |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2447 | p[8] = session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2448 | p += 9; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2449 | memcpy(p, session->resumption_key, session->resumption_key_len); |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2450 | p += session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2451 | |
| 2452 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2453 | if (session->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 2454 | MBEDTLS_PUT_UINT64_BE((uint64_t) session->start, p, 0); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2455 | p += 8; |
| 2456 | } |
| 2457 | #endif /* MBEDTLS_HAVE_TIME */ |
| 2458 | |
| 2459 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2460 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2461 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2462 | MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0); |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2463 | p += 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2464 | if (hostname_len > 0) { |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2465 | /* save host name */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2466 | memcpy(p, session->hostname, hostname_len); |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2467 | p += hostname_len; |
| 2468 | } |
| 2469 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 2470 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2471 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2472 | MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_received, p, 0); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2473 | p += 8; |
| 2474 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2475 | MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2476 | p += 4; |
| 2477 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2478 | MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2479 | p += 2; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2480 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2481 | if (session->ticket != NULL && session->ticket_len > 0) { |
| 2482 | memcpy(p, session->ticket, session->ticket_len); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2483 | p += session->ticket_len; |
| 2484 | } |
| 2485 | } |
| 2486 | #endif /* MBEDTLS_SSL_CLI_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2487 | return 0; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2488 | } |
| 2489 | |
| 2490 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2491 | static int ssl_tls13_session_load(mbedtls_ssl_session *session, |
| 2492 | const unsigned char *buf, |
| 2493 | size_t len) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2494 | { |
| 2495 | const unsigned char *p = buf; |
| 2496 | const unsigned char *end = buf + len; |
| 2497 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2498 | if (end - p < 9) { |
| 2499 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2500 | } |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2501 | session->endpoint = p[0]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2502 | session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 1); |
| 2503 | session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 3); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2504 | session->ticket_flags = p[7]; |
| 2505 | |
| 2506 | /* load resumption_key */ |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2507 | session->resumption_key_len = p[8]; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2508 | p += 9; |
| 2509 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2510 | if (end - p < session->resumption_key_len) { |
| 2511 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2512 | } |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2513 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2514 | if (sizeof(session->resumption_key) < session->resumption_key_len) { |
| 2515 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2516 | } |
| 2517 | memcpy(session->resumption_key, p, session->resumption_key_len); |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2518 | p += session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2519 | |
| 2520 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2521 | if (session->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 2522 | if (end - p < 8) { |
| 2523 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2524 | } |
| 2525 | session->start = MBEDTLS_GET_UINT64_BE(p, 0); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2526 | p += 8; |
| 2527 | } |
| 2528 | #endif /* MBEDTLS_HAVE_TIME */ |
| 2529 | |
| 2530 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2531 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2532 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2533 | defined(MBEDTLS_SSL_SESSION_TICKETS) |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2534 | size_t hostname_len; |
| 2535 | /* load host name */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2536 | if (end - p < 2) { |
| 2537 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2538 | } |
| 2539 | hostname_len = MBEDTLS_GET_UINT16_BE(p, 0); |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2540 | p += 2; |
| 2541 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2542 | if (end - p < (long int) hostname_len) { |
| 2543 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2544 | } |
| 2545 | if (hostname_len > 0) { |
| 2546 | session->hostname = mbedtls_calloc(1, hostname_len); |
| 2547 | if (session->hostname == NULL) { |
| 2548 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2549 | } |
| 2550 | memcpy(session->hostname, p, hostname_len); |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2551 | p += hostname_len; |
| 2552 | } |
| 2553 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION && |
| 2554 | MBEDTLS_SSL_SESSION_TICKETS */ |
| 2555 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2556 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2557 | if (end - p < 8) { |
| 2558 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2559 | } |
| 2560 | session->ticket_received = MBEDTLS_GET_UINT64_BE(p, 0); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2561 | p += 8; |
| 2562 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2563 | if (end - p < 4) { |
| 2564 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2565 | } |
| 2566 | session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2567 | p += 4; |
| 2568 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2569 | if (end - p < 2) { |
| 2570 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2571 | } |
| 2572 | session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2573 | p += 2; |
| 2574 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2575 | if (end - p < (long int) session->ticket_len) { |
| 2576 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2577 | } |
| 2578 | if (session->ticket_len > 0) { |
| 2579 | session->ticket = mbedtls_calloc(1, session->ticket_len); |
| 2580 | if (session->ticket == NULL) { |
| 2581 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2582 | } |
| 2583 | memcpy(session->ticket, p, session->ticket_len); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2584 | p += session->ticket_len; |
| 2585 | } |
| 2586 | } |
| 2587 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 2588 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2589 | return 0; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2590 | |
| 2591 | } |
| 2592 | #else /* MBEDTLS_SSL_SESSION_TICKETS */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2593 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2594 | static int ssl_tls13_session_save(const mbedtls_ssl_session *session, |
| 2595 | unsigned char *buf, |
| 2596 | size_t buf_len, |
| 2597 | size_t *olen) |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2598 | { |
| 2599 | ((void) session); |
| 2600 | ((void) buf); |
| 2601 | ((void) buf_len); |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2602 | *olen = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2603 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2604 | } |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2605 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2606 | static int ssl_tls13_session_load(const mbedtls_ssl_session *session, |
| 2607 | unsigned char *buf, |
| 2608 | size_t buf_len) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2609 | { |
| 2610 | ((void) session); |
| 2611 | ((void) buf); |
| 2612 | ((void) buf_len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2613 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2614 | } |
| 2615 | #endif /* !MBEDTLS_SSL_SESSION_TICKETS */ |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2616 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 2617 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2618 | psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type, |
| 2619 | size_t taglen, |
| 2620 | psa_algorithm_t *alg, |
| 2621 | psa_key_type_t *key_type, |
| 2622 | size_t *key_size) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2623 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2624 | switch (mbedtls_cipher_type) { |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2625 | case MBEDTLS_CIPHER_AES_128_CBC: |
| 2626 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2627 | *key_type = PSA_KEY_TYPE_AES; |
| 2628 | *key_size = 128; |
| 2629 | break; |
| 2630 | case MBEDTLS_CIPHER_AES_128_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2631 | *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] | 2632 | *key_type = PSA_KEY_TYPE_AES; |
| 2633 | *key_size = 128; |
| 2634 | break; |
| 2635 | case MBEDTLS_CIPHER_AES_128_GCM: |
| 2636 | *alg = PSA_ALG_GCM; |
| 2637 | *key_type = PSA_KEY_TYPE_AES; |
| 2638 | *key_size = 128; |
| 2639 | break; |
| 2640 | case MBEDTLS_CIPHER_AES_192_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2641 | *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] | 2642 | *key_type = PSA_KEY_TYPE_AES; |
| 2643 | *key_size = 192; |
| 2644 | break; |
| 2645 | case MBEDTLS_CIPHER_AES_192_GCM: |
| 2646 | *alg = PSA_ALG_GCM; |
| 2647 | *key_type = PSA_KEY_TYPE_AES; |
| 2648 | *key_size = 192; |
| 2649 | break; |
| 2650 | case MBEDTLS_CIPHER_AES_256_CBC: |
| 2651 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2652 | *key_type = PSA_KEY_TYPE_AES; |
| 2653 | *key_size = 256; |
| 2654 | break; |
| 2655 | case MBEDTLS_CIPHER_AES_256_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2656 | *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] | 2657 | *key_type = PSA_KEY_TYPE_AES; |
| 2658 | *key_size = 256; |
| 2659 | break; |
| 2660 | case MBEDTLS_CIPHER_AES_256_GCM: |
| 2661 | *alg = PSA_ALG_GCM; |
| 2662 | *key_type = PSA_KEY_TYPE_AES; |
| 2663 | *key_size = 256; |
| 2664 | break; |
| 2665 | case MBEDTLS_CIPHER_ARIA_128_CBC: |
| 2666 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2667 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2668 | *key_size = 128; |
| 2669 | break; |
| 2670 | case MBEDTLS_CIPHER_ARIA_128_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2671 | *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] | 2672 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2673 | *key_size = 128; |
| 2674 | break; |
| 2675 | case MBEDTLS_CIPHER_ARIA_128_GCM: |
| 2676 | *alg = PSA_ALG_GCM; |
| 2677 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2678 | *key_size = 128; |
| 2679 | break; |
| 2680 | case MBEDTLS_CIPHER_ARIA_192_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2681 | *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] | 2682 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2683 | *key_size = 192; |
| 2684 | break; |
| 2685 | case MBEDTLS_CIPHER_ARIA_192_GCM: |
| 2686 | *alg = PSA_ALG_GCM; |
| 2687 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2688 | *key_size = 192; |
| 2689 | break; |
| 2690 | case MBEDTLS_CIPHER_ARIA_256_CBC: |
| 2691 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2692 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2693 | *key_size = 256; |
| 2694 | break; |
| 2695 | case MBEDTLS_CIPHER_ARIA_256_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_ARIA; |
| 2698 | *key_size = 256; |
| 2699 | break; |
| 2700 | case MBEDTLS_CIPHER_ARIA_256_GCM: |
| 2701 | *alg = PSA_ALG_GCM; |
| 2702 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2703 | *key_size = 256; |
| 2704 | break; |
| 2705 | case MBEDTLS_CIPHER_CAMELLIA_128_CBC: |
| 2706 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2707 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2708 | *key_size = 128; |
| 2709 | break; |
| 2710 | case MBEDTLS_CIPHER_CAMELLIA_128_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2711 | *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] | 2712 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2713 | *key_size = 128; |
| 2714 | break; |
| 2715 | case MBEDTLS_CIPHER_CAMELLIA_128_GCM: |
| 2716 | *alg = PSA_ALG_GCM; |
| 2717 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2718 | *key_size = 128; |
| 2719 | break; |
| 2720 | case MBEDTLS_CIPHER_CAMELLIA_192_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_CAMELLIA; |
| 2723 | *key_size = 192; |
| 2724 | break; |
| 2725 | case MBEDTLS_CIPHER_CAMELLIA_192_GCM: |
| 2726 | *alg = PSA_ALG_GCM; |
| 2727 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2728 | *key_size = 192; |
| 2729 | break; |
| 2730 | case MBEDTLS_CIPHER_CAMELLIA_256_CBC: |
| 2731 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2732 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2733 | *key_size = 256; |
| 2734 | break; |
| 2735 | case MBEDTLS_CIPHER_CAMELLIA_256_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_CAMELLIA; |
| 2738 | *key_size = 256; |
| 2739 | break; |
| 2740 | case MBEDTLS_CIPHER_CAMELLIA_256_GCM: |
| 2741 | *alg = PSA_ALG_GCM; |
| 2742 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2743 | *key_size = 256; |
| 2744 | break; |
| 2745 | case MBEDTLS_CIPHER_CHACHA20_POLY1305: |
| 2746 | *alg = PSA_ALG_CHACHA20_POLY1305; |
| 2747 | *key_type = PSA_KEY_TYPE_CHACHA20; |
| 2748 | *key_size = 256; |
| 2749 | break; |
| 2750 | case MBEDTLS_CIPHER_NULL: |
| 2751 | *alg = MBEDTLS_SSL_NULL_CIPHER; |
| 2752 | *key_type = 0; |
| 2753 | *key_size = 0; |
| 2754 | break; |
| 2755 | default: |
| 2756 | return PSA_ERROR_NOT_SUPPORTED; |
| 2757 | } |
| 2758 | |
| 2759 | return PSA_SUCCESS; |
| 2760 | } |
Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 2761 | #endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2762 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2763 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2764 | int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf, |
| 2765 | const unsigned char *dhm_P, size_t P_len, |
| 2766 | const unsigned char *dhm_G, size_t G_len) |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2767 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2768 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2769 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2770 | mbedtls_mpi_free(&conf->dhm_P); |
| 2771 | mbedtls_mpi_free(&conf->dhm_G); |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2772 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2773 | if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 || |
| 2774 | (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) { |
| 2775 | mbedtls_mpi_free(&conf->dhm_P); |
| 2776 | mbedtls_mpi_free(&conf->dhm_G); |
| 2777 | return ret; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2778 | } |
| 2779 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2780 | return 0; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2781 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2782 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2783 | 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] | 2784 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2785 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2786 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2787 | mbedtls_mpi_free(&conf->dhm_P); |
| 2788 | mbedtls_mpi_free(&conf->dhm_G); |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2789 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2790 | if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P, |
| 2791 | &conf->dhm_P)) != 0 || |
| 2792 | (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G, |
| 2793 | &conf->dhm_G)) != 0) { |
| 2794 | mbedtls_mpi_free(&conf->dhm_P); |
| 2795 | mbedtls_mpi_free(&conf->dhm_G); |
| 2796 | return ret; |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 2797 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2798 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2799 | return 0; |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2800 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2801 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2802 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 2803 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 2804 | /* |
| 2805 | * Set the minimum length for Diffie-Hellman parameters |
| 2806 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2807 | void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf, |
| 2808 | unsigned int bitlen) |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 2809 | { |
| 2810 | conf->dhm_min_bitlen = bitlen; |
| 2811 | } |
| 2812 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 2813 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2814 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2815 | #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] | 2816 | /* |
| 2817 | * Set allowed/preferred hashes for handshake signatures |
| 2818 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2819 | void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf, |
| 2820 | const int *hashes) |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 2821 | { |
| 2822 | conf->sig_hashes = hashes; |
| 2823 | } |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2824 | #endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2825 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2826 | /* Configure allowed signature algorithms for handshake */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2827 | void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf, |
| 2828 | const uint16_t *sig_algs) |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2829 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2830 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2831 | conf->sig_hashes = NULL; |
| 2832 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 2833 | conf->sig_algs = sig_algs; |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2834 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2835 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 2836 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 2837 | #if defined(MBEDTLS_ECP_C) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2838 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2839 | /* |
| 2840 | * Set the allowed elliptic curves |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2841 | * |
| 2842 | * mbedtls_ssl_setup() takes the provided list |
| 2843 | * and translates it to a list of IANA TLS group identifiers, |
| 2844 | * stored in ssl->handshake->group_list. |
| 2845 | * |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2846 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2847 | void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf, |
| 2848 | const mbedtls_ecp_group_id *curve_list) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2849 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2850 | conf->curve_list = curve_list; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2851 | conf->group_list = NULL; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2852 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2853 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2854 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2855 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2856 | /* |
| 2857 | * Set the allowed groups |
| 2858 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2859 | void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf, |
| 2860 | const uint16_t *group_list) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2861 | { |
| 2862 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2863 | conf->curve_list = NULL; |
| 2864 | #endif |
| 2865 | conf->group_list = group_list; |
| 2866 | } |
| 2867 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2868 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2869 | int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2870 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2871 | /* Initialize to suppress unnecessary compiler warning */ |
| 2872 | size_t hostname_len = 0; |
| 2873 | |
| 2874 | /* Check if new hostname is valid before |
| 2875 | * making any change to current one */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2876 | if (hostname != NULL) { |
| 2877 | hostname_len = strlen(hostname); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2878 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2879 | if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) { |
| 2880 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2881 | } |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2882 | } |
| 2883 | |
| 2884 | /* Now it's clear that we will overwrite the old hostname, |
| 2885 | * so we can free it safely */ |
| 2886 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2887 | if (ssl->hostname != NULL) { |
| 2888 | mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname)); |
| 2889 | mbedtls_free(ssl->hostname); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2890 | } |
| 2891 | |
| 2892 | /* Passing NULL as hostname shall clear the old one */ |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 2893 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2894 | if (hostname == NULL) { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2895 | ssl->hostname = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2896 | } else { |
| 2897 | ssl->hostname = mbedtls_calloc(1, hostname_len + 1); |
| 2898 | if (ssl->hostname == NULL) { |
| 2899 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2900 | } |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2901 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2902 | memcpy(ssl->hostname, hostname, hostname_len); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2903 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2904 | ssl->hostname[hostname_len] = '\0'; |
| 2905 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2906 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2907 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2908 | } |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 2909 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2910 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2911 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2912 | void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf, |
| 2913 | int (*f_sni)(void *, mbedtls_ssl_context *, |
| 2914 | const unsigned char *, size_t), |
| 2915 | void *p_sni) |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2916 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2917 | conf->f_sni = f_sni; |
| 2918 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2919 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2920 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2921 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2922 | #if defined(MBEDTLS_SSL_ALPN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2923 | 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] | 2924 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2925 | size_t cur_len, tot_len; |
| 2926 | const char **p; |
| 2927 | |
| 2928 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 2929 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 2930 | * MUST NOT be truncated." |
| 2931 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2932 | */ |
| 2933 | tot_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2934 | for (p = protos; *p != NULL; p++) { |
| 2935 | cur_len = strlen(*p); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2936 | tot_len += cur_len; |
| 2937 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2938 | if ((cur_len == 0) || |
| 2939 | (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) || |
| 2940 | (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) { |
| 2941 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2942 | } |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2943 | } |
| 2944 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2945 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2946 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2947 | return 0; |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2948 | } |
| 2949 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2950 | 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] | 2951 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2952 | return ssl->alpn_chosen; |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2953 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2954 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2955 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2956 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2957 | void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf, |
| 2958 | int support_mki_value) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2959 | { |
| 2960 | conf->dtls_srtp_mki_support = support_mki_value; |
| 2961 | } |
| 2962 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2963 | int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl, |
| 2964 | unsigned char *mki_value, |
| 2965 | uint16_t mki_len) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2966 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2967 | if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) { |
| 2968 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2969 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2970 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2971 | if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) { |
| 2972 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2973 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2974 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2975 | memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len); |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2976 | ssl->dtls_srtp_info.mki_len = mki_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2977 | return 0; |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2978 | } |
| 2979 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2980 | int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf, |
| 2981 | const mbedtls_ssl_srtp_profile *profiles) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2982 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2983 | const mbedtls_ssl_srtp_profile *p; |
| 2984 | size_t list_size = 0; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2985 | |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2986 | /* check the profiles list: all entry must be valid, |
| 2987 | * 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] | 2988 | for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET && |
| 2989 | list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH; |
| 2990 | p++) { |
| 2991 | if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) { |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 2992 | list_size++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2993 | } else { |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 2994 | /* unsupported value, stop parsing and set the size to an error value */ |
| 2995 | list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2996 | } |
| 2997 | } |
| 2998 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2999 | if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) { |
| 3000 | conf->dtls_srtp_profile_list = NULL; |
| 3001 | conf->dtls_srtp_profile_list_len = 0; |
| 3002 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 3003 | } |
| 3004 | |
Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 3005 | conf->dtls_srtp_profile_list = profiles; |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 3006 | conf->dtls_srtp_profile_list_len = list_size; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3007 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3008 | return 0; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3009 | } |
| 3010 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3011 | void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl, |
| 3012 | mbedtls_dtls_srtp_info *dtls_srtp_info) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3013 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 3014 | dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile; |
| 3015 | /* do not copy the mki value if there is no chosen profile */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3016 | if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 3017 | dtls_srtp_info->mki_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3018 | } else { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 3019 | dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3020 | memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value, |
| 3021 | ssl->dtls_srtp_info.mki_len); |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 3022 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3023 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3024 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 3025 | |
Aditya Patwardhan | 3096f33 | 2022-07-26 14:31:46 +0530 | [diff] [blame] | 3026 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3027 | 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] | 3028 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 3029 | conf->max_tls_version = (major << 8) | minor; |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 3030 | } |
| 3031 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3032 | 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] | 3033 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 3034 | conf->min_tls_version = (major << 8) | minor; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 3035 | } |
Aditya Patwardhan | 3096f33 | 2022-07-26 14:31:46 +0530 | [diff] [blame] | 3036 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 3037 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3038 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3039 | void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf, |
| 3040 | char cert_req_ca_list) |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3041 | { |
| 3042 | conf->cert_req_ca_list = cert_req_ca_list; |
| 3043 | } |
| 3044 | #endif |
| 3045 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3046 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3047 | 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] | 3048 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3049 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 3050 | } |
| 3051 | #endif |
| 3052 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3053 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3054 | 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] | 3055 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3056 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 3057 | } |
| 3058 | #endif |
| 3059 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3060 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3061 | 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] | 3062 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3063 | if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
| 3064 | ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) { |
| 3065 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 3066 | } |
| 3067 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 3068 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 3069 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3070 | return 0; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 3071 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3072 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 3073 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3074 | 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] | 3075 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3076 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3077 | } |
| 3078 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3079 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3080 | 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] | 3081 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3082 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 3083 | } |
| 3084 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3085 | 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] | 3086 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3087 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 3088 | } |
| 3089 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3090 | void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf, |
| 3091 | const unsigned char period[8]) |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 3092 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3093 | memcpy(conf->renego_period, period, 8); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 3094 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3095 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3096 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3097 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3098 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3099 | 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] | 3100 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 3101 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 3102 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3103 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 3104 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3105 | #if defined(MBEDTLS_SSL_SRV_C) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 3106 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 3107 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3108 | void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf, |
| 3109 | uint16_t num_tickets) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 3110 | { |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 3111 | conf->new_session_tickets_count = num_tickets; |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 3112 | } |
| 3113 | #endif |
| 3114 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3115 | void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf, |
| 3116 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 3117 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 3118 | void *p_ticket) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 3119 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 3120 | conf->f_ticket_write = f_ticket_write; |
| 3121 | conf->f_ticket_parse = f_ticket_parse; |
| 3122 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 3123 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3124 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3125 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 3126 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3127 | void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl, |
| 3128 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 3129 | void *p_export_keys) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 3130 | { |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 3131 | ssl->f_export_keys = f_export_keys; |
| 3132 | ssl->p_export_keys = p_export_keys; |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 3133 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 3134 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3135 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3136 | void mbedtls_ssl_conf_async_private_cb( |
| 3137 | mbedtls_ssl_config *conf, |
| 3138 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 3139 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 3140 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 3141 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3142 | void *async_config_data) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3143 | { |
| 3144 | conf->f_async_sign_start = f_async_sign; |
| 3145 | conf->f_async_decrypt_start = f_async_decrypt; |
| 3146 | conf->f_async_resume = f_async_resume; |
| 3147 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3148 | conf->p_async_config_data = async_config_data; |
| 3149 | } |
| 3150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3151 | 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] | 3152 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3153 | return conf->p_async_config_data; |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3154 | } |
| 3155 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3156 | void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3157 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3158 | if (ssl->handshake == NULL) { |
| 3159 | return NULL; |
| 3160 | } else { |
| 3161 | return ssl->handshake->user_async_ctx; |
| 3162 | } |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3163 | } |
| 3164 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3165 | void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl, |
| 3166 | void *ctx) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3167 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3168 | if (ssl->handshake != NULL) { |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3169 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3170 | } |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3171 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3172 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3173 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3174 | /* |
| 3175 | * SSL get accessors |
| 3176 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3177 | uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3178 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3179 | if (ssl->session != NULL) { |
| 3180 | return ssl->session->verify_result; |
| 3181 | } |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 3182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3183 | if (ssl->session_negotiate != NULL) { |
| 3184 | return ssl->session_negotiate->verify_result; |
| 3185 | } |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 3186 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3187 | return 0xFFFFFFFF; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3188 | } |
| 3189 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3190 | 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] | 3191 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3192 | if (ssl == NULL || ssl->session == NULL) { |
| 3193 | return 0; |
| 3194 | } |
Glenn Strauss | 8f52690 | 2022-01-13 00:04:49 -0500 | [diff] [blame] | 3195 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3196 | return ssl->session->ciphersuite; |
Glenn Strauss | 8f52690 | 2022-01-13 00:04:49 -0500 | [diff] [blame] | 3197 | } |
| 3198 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3199 | const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 3200 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3201 | if (ssl == NULL || ssl->session == NULL) { |
| 3202 | return NULL; |
| 3203 | } |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 3204 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3205 | return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 3206 | } |
| 3207 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3208 | const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3209 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3210 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3211 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 3212 | switch (ssl->tls_version) { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3213 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3214 | return "DTLSv1.2"; |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 3215 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3216 | return "unknown (DTLS)"; |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 3217 | } |
| 3218 | } |
| 3219 | #endif |
| 3220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3221 | switch (ssl->tls_version) { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3222 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3223 | return "TLSv1.2"; |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3224 | case MBEDTLS_SSL_VERSION_TLS1_3: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3225 | return "TLSv1.3"; |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3226 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3227 | return "unknown"; |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3228 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3229 | } |
| 3230 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 3231 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3232 | 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] | 3233 | { |
David Horstmann | 95d516f | 2021-05-04 18:36:56 +0100 | [diff] [blame] | 3234 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3235 | size_t read_mfl; |
| 3236 | |
Jerry Yu | ddda050 | 2022-12-01 19:43:12 +0800 | [diff] [blame] | 3237 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3238 | /* 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] | 3239 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 3240 | ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) { |
| 3241 | return ssl_mfl_code_to_length(ssl->conf->mfl_code); |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3242 | } |
Jerry Yu | ddda050 | 2022-12-01 19:43:12 +0800 | [diff] [blame] | 3243 | #endif |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3244 | |
| 3245 | /* Check if a smaller max length was negotiated */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3246 | if (ssl->session_out != NULL) { |
| 3247 | read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code); |
| 3248 | if (read_mfl < max_len) { |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3249 | max_len = read_mfl; |
| 3250 | } |
| 3251 | } |
| 3252 | |
Jerry Yu | ddda050 | 2022-12-01 19:43:12 +0800 | [diff] [blame] | 3253 | /* During a handshake, use the value being negotiated */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3254 | if (ssl->session_negotiate != NULL) { |
| 3255 | read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code); |
| 3256 | if (read_mfl < max_len) { |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3257 | max_len = read_mfl; |
| 3258 | } |
| 3259 | } |
| 3260 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3261 | return max_len; |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3262 | } |
| 3263 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3264 | 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] | 3265 | { |
| 3266 | size_t max_len; |
| 3267 | |
| 3268 | /* |
| 3269 | * Assume mfl_code is correct since it was checked when set |
| 3270 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3271 | 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] | 3272 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 3273 | /* Check if a smaller max length was negotiated */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3274 | if (ssl->session_out != NULL && |
| 3275 | ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) { |
| 3276 | 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] | 3277 | } |
| 3278 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 3279 | /* During a handshake, use the value being negotiated */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3280 | if (ssl->session_negotiate != NULL && |
| 3281 | ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) { |
| 3282 | 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] | 3283 | } |
| 3284 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3285 | return max_len; |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 3286 | } |
| 3287 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 3288 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3289 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3290 | 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] | 3291 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 3292 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3293 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 3294 | (ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 3295 | ssl->state == MBEDTLS_SSL_SERVER_HELLO)) { |
| 3296 | return 0; |
| 3297 | } |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 3298 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3299 | if (ssl->handshake == NULL || ssl->handshake->mtu == 0) { |
| 3300 | return ssl->mtu; |
| 3301 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3302 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3303 | if (ssl->mtu == 0) { |
| 3304 | return ssl->handshake->mtu; |
| 3305 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3306 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3307 | return ssl->mtu < ssl->handshake->mtu ? |
| 3308 | ssl->mtu : ssl->handshake->mtu; |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3309 | } |
| 3310 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3311 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3312 | 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] | 3313 | { |
| 3314 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 3315 | |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 3316 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 3317 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3318 | (void) ssl; |
| 3319 | #endif |
| 3320 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3321 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3322 | 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] | 3323 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3324 | if (max_len > mfl) { |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3325 | max_len = mfl; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3326 | } |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3327 | #endif |
| 3328 | |
| 3329 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3330 | if (mbedtls_ssl_get_current_mtu(ssl) != 0) { |
| 3331 | const size_t mtu = mbedtls_ssl_get_current_mtu(ssl); |
| 3332 | const int ret = mbedtls_ssl_get_record_expansion(ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3333 | const size_t overhead = (size_t) ret; |
| 3334 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3335 | if (ret < 0) { |
| 3336 | return ret; |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3337 | } |
| 3338 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3339 | if (mtu <= overhead) { |
| 3340 | MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion")); |
| 3341 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 3342 | } |
| 3343 | |
| 3344 | if (max_len > mtu - overhead) { |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3345 | max_len = mtu - overhead; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3346 | } |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3347 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3348 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3349 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 3350 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 3351 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3352 | ((void) ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3353 | #endif |
| 3354 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3355 | return (int) max_len; |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3356 | } |
| 3357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3358 | 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] | 3359 | { |
| 3360 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
| 3361 | |
| 3362 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 3363 | (void) ssl; |
| 3364 | #endif |
| 3365 | |
| 3366 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3367 | const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl); |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 3368 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3369 | if (max_len > mfl) { |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 3370 | max_len = mfl; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3371 | } |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 3372 | #endif |
| 3373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3374 | return (int) max_len; |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 3375 | } |
| 3376 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3377 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3378 | 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] | 3379 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3380 | if (ssl == NULL || ssl->session == NULL) { |
| 3381 | return NULL; |
| 3382 | } |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3383 | |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 3384 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3385 | return ssl->session->peer_cert; |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 3386 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3387 | return NULL; |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 3388 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3389 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3390 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3391 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3392 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3393 | int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl, |
| 3394 | mbedtls_ssl_session *dst) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3395 | { |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3396 | int ret; |
| 3397 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3398 | if (ssl == NULL || |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3399 | dst == NULL || |
| 3400 | ssl->session == NULL || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3401 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) { |
| 3402 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3403 | } |
| 3404 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3405 | /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer |
| 3406 | * idempotent: Each session can only be exported once. |
| 3407 | * |
| 3408 | * (This is in preparation for TLS 1.3 support where we will |
| 3409 | * need the ability to export multiple sessions (aka tickets), |
| 3410 | * which will be achieved by calling mbedtls_ssl_get_session() |
| 3411 | * multiple times until it fails.) |
| 3412 | * |
| 3413 | * Check whether we have already exported the current session, |
| 3414 | * and fail if so. |
| 3415 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3416 | if (ssl->session->exported == 1) { |
| 3417 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 3418 | } |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3419 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3420 | ret = mbedtls_ssl_session_copy(dst, ssl->session); |
| 3421 | if (ret != 0) { |
| 3422 | return ret; |
| 3423 | } |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3424 | |
| 3425 | /* Remember that we've exported the session. */ |
| 3426 | ssl->session->exported = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3427 | return 0; |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3428 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3429 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3430 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3431 | /* |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 3432 | * Define ticket header determining Mbed TLS version |
| 3433 | * and structure of the ticket. |
| 3434 | */ |
| 3435 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3436 | /* |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3437 | * Define bitflag determining compile-time settings influencing |
| 3438 | * structure of serialized SSL sessions. |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3439 | */ |
| 3440 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3441 | #if defined(MBEDTLS_HAVE_TIME) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3442 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3443 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3444 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3445 | #endif /* MBEDTLS_HAVE_TIME */ |
| 3446 | |
| 3447 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3448 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3449 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3450 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3451 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 3452 | |
| 3453 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3454 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3455 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3456 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3457 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ |
| 3458 | |
| 3459 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3460 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3461 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3462 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3463 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 3464 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3465 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3466 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3467 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3468 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3469 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 3470 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3471 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 3472 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 |
| 3473 | #else |
| 3474 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 |
| 3475 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 3476 | |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3477 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0 |
| 3478 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1 |
| 3479 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 |
| 3480 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3 |
Hanno Becker | 37bdbe6 | 2021-08-01 05:38:58 +0100 | [diff] [blame] | 3481 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4 |
| 3482 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5 |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3483 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3484 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3485 | ((uint16_t) ( \ |
| 3486 | (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \ |
| 3487 | (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \ |
| 3488 | (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \ |
| 3489 | SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \ |
| 3490 | (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \ |
| 3491 | (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \ |
| 3492 | (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT))) |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3493 | |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 3494 | static unsigned char ssl_serialized_session_header[] = { |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3495 | MBEDTLS_VERSION_MAJOR, |
| 3496 | MBEDTLS_VERSION_MINOR, |
| 3497 | MBEDTLS_VERSION_PATCH, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3498 | MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG), |
| 3499 | MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG), |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 3500 | }; |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 3501 | |
| 3502 | /* |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3503 | * Serialize a session in the following format: |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 3504 | * (in the presentation language of TLS, RFC 8446 section 3) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3505 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3506 | * struct { |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 3507 | * |
Hanno Becker | dce5097 | 2021-08-01 05:39:23 +0100 | [diff] [blame] | 3508 | * opaque mbedtls_version[3]; // library version: major, minor, patch |
| 3509 | * opaque session_format[2]; // library-version specific 16-bit field |
| 3510 | * // determining the format of the remaining |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3511 | * // serialized data. |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 3512 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3513 | * Note: When updating the format, remember to keep |
| 3514 | * these version+format bytes. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3515 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3516 | * // In this version, `session_format` determines |
| 3517 | * // the setting of those compile-time |
| 3518 | * // configuration options which influence |
| 3519 | * // the structure of mbedtls_ssl_session. |
| 3520 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3521 | * uint8_t minor_ver; // Protocol minor version. Possible values: |
Jerry Yu | 0c2a738 | 2022-12-06 13:27:25 +0800 | [diff] [blame] | 3522 | * // - TLS 1.2 (0x0303) |
| 3523 | * // - TLS 1.3 (0x0304) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3524 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3525 | * select (serialized_session.tls_version) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3526 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3527 | * case MBEDTLS_SSL_VERSION_TLS1_2: |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3528 | * serialized_session_tls12 data; |
Jerry Yu | 0c2a738 | 2022-12-06 13:27:25 +0800 | [diff] [blame] | 3529 | * case MBEDTLS_SSL_VERSION_TLS1_3: |
Jerry Yu | ddda050 | 2022-12-01 19:43:12 +0800 | [diff] [blame] | 3530 | * serialized_session_tls13 data; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3531 | * |
| 3532 | * }; |
| 3533 | * |
| 3534 | * } serialized_session; |
| 3535 | * |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3536 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3537 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3538 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3539 | static int ssl_session_save(const mbedtls_ssl_session *session, |
| 3540 | unsigned char omit_header, |
| 3541 | unsigned char *buf, |
| 3542 | size_t buf_len, |
| 3543 | size_t *olen) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3544 | { |
| 3545 | unsigned char *p = buf; |
| 3546 | size_t used = 0; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3547 | size_t remaining_len; |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3548 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3549 | size_t out_len; |
| 3550 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3551 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3552 | if (session == NULL) { |
| 3553 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 3554 | } |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3555 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3556 | if (!omit_header) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3557 | /* |
| 3558 | * Add Mbed TLS version identifier |
| 3559 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3560 | used += sizeof(ssl_serialized_session_header); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3562 | if (used <= buf_len) { |
| 3563 | memcpy(p, ssl_serialized_session_header, |
| 3564 | sizeof(ssl_serialized_session_header)); |
| 3565 | p += sizeof(ssl_serialized_session_header); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3566 | } |
| 3567 | } |
| 3568 | |
| 3569 | /* |
| 3570 | * TLS version identifier |
| 3571 | */ |
| 3572 | used += 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3573 | if (used <= buf_len) { |
| 3574 | *p++ = MBEDTLS_BYTE_0(session->tls_version); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3575 | } |
| 3576 | |
| 3577 | /* Forward to version-specific serialization routine. */ |
Jerry Yu | fca4d57 | 2022-07-21 10:37:48 +0800 | [diff] [blame] | 3578 | remaining_len = (buf_len >= used) ? buf_len - used : 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3579 | switch (session->tls_version) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3580 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3581 | case MBEDTLS_SSL_VERSION_TLS1_2: |
| 3582 | used += ssl_tls12_session_save(session, p, remaining_len); |
| 3583 | break; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3584 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3585 | |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3586 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3587 | case MBEDTLS_SSL_VERSION_TLS1_3: |
| 3588 | ret = ssl_tls13_session_save(session, p, remaining_len, &out_len); |
| 3589 | if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) { |
| 3590 | return ret; |
| 3591 | } |
| 3592 | used += out_len; |
| 3593 | break; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3594 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 3595 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3596 | default: |
| 3597 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3598 | } |
| 3599 | |
| 3600 | *olen = used; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3601 | if (used > buf_len) { |
| 3602 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 3603 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3604 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3605 | return 0; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3606 | } |
| 3607 | |
| 3608 | /* |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3609 | * Public wrapper for ssl_session_save() |
| 3610 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3611 | int mbedtls_ssl_session_save(const mbedtls_ssl_session *session, |
| 3612 | unsigned char *buf, |
| 3613 | size_t buf_len, |
| 3614 | size_t *olen) |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3615 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3616 | return ssl_session_save(session, 0, buf, buf_len, olen); |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3617 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3618 | |
Jerry Yu | f1b23ca | 2022-02-18 11:48:47 +0800 | [diff] [blame] | 3619 | /* |
| 3620 | * Deserialize session, see mbedtls_ssl_session_save() for format. |
| 3621 | * |
| 3622 | * This internal version is wrapped by a public function that cleans up in |
| 3623 | * case of error, and has an extra option omit_header. |
| 3624 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3625 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3626 | static int ssl_session_load(mbedtls_ssl_session *session, |
| 3627 | unsigned char omit_header, |
| 3628 | const unsigned char *buf, |
| 3629 | size_t len) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3630 | { |
| 3631 | const unsigned char *p = buf; |
| 3632 | const unsigned char * const end = buf + len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3633 | size_t remaining_len; |
| 3634 | |
| 3635 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3636 | if (session == NULL) { |
| 3637 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 3638 | } |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3639 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3640 | if (!omit_header) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3641 | /* |
| 3642 | * Check Mbed TLS version identifier |
| 3643 | */ |
| 3644 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3645 | if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) { |
| 3646 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3647 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3648 | |
| 3649 | if (memcmp(p, ssl_serialized_session_header, |
| 3650 | sizeof(ssl_serialized_session_header)) != 0) { |
| 3651 | return MBEDTLS_ERR_SSL_VERSION_MISMATCH; |
| 3652 | } |
| 3653 | p += sizeof(ssl_serialized_session_header); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3654 | } |
| 3655 | |
| 3656 | /* |
| 3657 | * TLS version identifier |
| 3658 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3659 | if (1 > (size_t) (end - p)) { |
| 3660 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3661 | } |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3662 | session->tls_version = 0x0300 | *p++; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3663 | |
| 3664 | /* Dispatch according to TLS version. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3665 | remaining_len = (end - p); |
| 3666 | switch (session->tls_version) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3667 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3668 | case MBEDTLS_SSL_VERSION_TLS1_2: |
| 3669 | return ssl_tls12_session_load(session, p, remaining_len); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3670 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3671 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3672 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3673 | case MBEDTLS_SSL_VERSION_TLS1_3: |
| 3674 | return ssl_tls13_session_load(session, p, remaining_len); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3675 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 3676 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3677 | default: |
| 3678 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3679 | } |
| 3680 | } |
| 3681 | |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3682 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 3683 | * Deserialize session: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3684 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3685 | int mbedtls_ssl_session_load(mbedtls_ssl_session *session, |
| 3686 | const unsigned char *buf, |
| 3687 | size_t len) |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3688 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3689 | int ret = ssl_session_load(session, 0, buf, len); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3690 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3691 | if (ret != 0) { |
| 3692 | mbedtls_ssl_session_free(session); |
| 3693 | } |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3694 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3695 | return ret; |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3696 | } |
| 3697 | |
| 3698 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3699 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3700 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3701 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3702 | static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl) |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3703 | { |
| 3704 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3705 | |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3706 | /* |
| 3707 | * 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] | 3708 | * that were written into the output buffer by the previous handshake step, |
| 3709 | * if the write to the network callback returned with the |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3710 | * #MBEDTLS_ERR_SSL_WANT_WRITE error code. |
| 3711 | * We proceed to the next handshake step only when all data from the |
| 3712 | * previous one have been sent to the peer, thus we make sure that this is |
| 3713 | * the case here by calling `mbedtls_ssl_flush_output()`. The function may |
| 3714 | * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case |
| 3715 | * we have to wait before to go ahead. |
| 3716 | * In the case of TLS 1.3, handshake step handlers do not send data to the |
| 3717 | * peer. Data are only sent here and through |
| 3718 | * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an |
Andrzej Kurek | 5c65c57 | 2022-04-13 14:28:52 -0400 | [diff] [blame] | 3719 | * alert occurred. |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3720 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3721 | if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) { |
| 3722 | return ret; |
| 3723 | } |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3724 | |
| 3725 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3726 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3727 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) { |
| 3728 | if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) { |
| 3729 | return ret; |
| 3730 | } |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3731 | } |
| 3732 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3733 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3734 | return ret; |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3735 | } |
| 3736 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3737 | int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3738 | { |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3739 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3740 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3741 | if (ssl == NULL || |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3742 | ssl->conf == NULL || |
| 3743 | ssl->handshake == NULL || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3744 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 3745 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3746 | } |
| 3747 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3748 | ret = ssl_prepare_handshake_step(ssl); |
| 3749 | if (ret != 0) { |
| 3750 | return ret; |
| 3751 | } |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3752 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3753 | ret = mbedtls_ssl_handle_pending_alert(ssl); |
| 3754 | if (ret != 0) { |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3755 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3756 | } |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3757 | |
Tom Cosgrove | 2fdc7b3 | 2022-09-21 12:33:17 +0100 | [diff] [blame] | 3758 | /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or |
| 3759 | * MBEDTLS_SSL_IS_SERVER, this is the return code we give */ |
| 3760 | ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3761 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3762 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3763 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 3764 | MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s", |
| 3765 | mbedtls_ssl_states_str(ssl->state))); |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3766 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3767 | switch (ssl->state) { |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3768 | case MBEDTLS_SSL_HELLO_REQUEST: |
| 3769 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; |
Tom Cosgrove | 87d9c6c | 2022-09-22 09:27:56 +0100 | [diff] [blame] | 3770 | ret = 0; |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3771 | break; |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3772 | |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3773 | case MBEDTLS_SSL_CLIENT_HELLO: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3774 | ret = mbedtls_ssl_write_client_hello(ssl); |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3775 | break; |
| 3776 | |
| 3777 | default: |
| 3778 | #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] | 3779 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
| 3780 | ret = mbedtls_ssl_tls13_handshake_client_step(ssl); |
| 3781 | } else { |
| 3782 | ret = mbedtls_ssl_handshake_client_step(ssl); |
| 3783 | } |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3784 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3785 | ret = mbedtls_ssl_handshake_client_step(ssl); |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3786 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3787 | ret = mbedtls_ssl_tls13_handshake_client_step(ssl); |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3788 | #endif |
| 3789 | } |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3790 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3791 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3792 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3793 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3794 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3795 | if (mbedtls_ssl_conf_is_tls13_only(ssl->conf)) { |
| 3796 | ret = mbedtls_ssl_tls13_handshake_server_step(ssl); |
| 3797 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3798 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3799 | |
| 3800 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3801 | if (mbedtls_ssl_conf_is_tls12_only(ssl->conf)) { |
| 3802 | ret = mbedtls_ssl_handshake_server_step(ssl); |
| 3803 | } |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3804 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3805 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3806 | #endif |
| 3807 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3808 | if (ret != 0) { |
Jerry Yu | bbd5a3f | 2021-09-18 20:50:22 +0800 | [diff] [blame] | 3809 | /* handshake_step return error. And it is same |
| 3810 | * with alert_reason. |
| 3811 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3812 | if (ssl->send_alert) { |
| 3813 | ret = mbedtls_ssl_handle_pending_alert(ssl); |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3814 | goto cleanup; |
| 3815 | } |
| 3816 | } |
| 3817 | |
| 3818 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3819 | return ret; |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3820 | } |
| 3821 | |
| 3822 | /* |
| 3823 | * Perform the SSL handshake |
| 3824 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3825 | int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3826 | { |
| 3827 | int ret = 0; |
| 3828 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3829 | /* Sanity checks */ |
| 3830 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3831 | if (ssl == NULL || ssl->conf == NULL) { |
| 3832 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3833 | } |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3834 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3835 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3836 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3837 | (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) { |
| 3838 | MBEDTLS_SSL_DEBUG_MSG(1, ("You must use " |
| 3839 | "mbedtls_ssl_set_timer_cb() for DTLS")); |
| 3840 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3841 | } |
| 3842 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3843 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3844 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake")); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3845 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3846 | /* Main handshake loop */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3847 | while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 3848 | ret = mbedtls_ssl_handshake_step(ssl); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3849 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3850 | if (ret != 0) { |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3851 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3852 | } |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3853 | } |
| 3854 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3855 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3856 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3857 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3858 | } |
| 3859 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3860 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3861 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3862 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3863 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3864 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3865 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3866 | static int ssl_write_hello_request(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3867 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3868 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3869 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3870 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request")); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3871 | |
| 3872 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3873 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3874 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3875 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3876 | if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { |
| 3877 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret); |
| 3878 | return ret; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3879 | } |
| 3880 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3881 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request")); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3882 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3883 | return 0; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3884 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3885 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3886 | |
| 3887 | /* |
| 3888 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3889 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 3890 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 3891 | * - 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] | 3892 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3893 | * 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] | 3894 | * 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] | 3895 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3896 | int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3897 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3898 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3899 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3900 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate")); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3901 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3902 | if ((ret = ssl_handshake_init(ssl)) != 0) { |
| 3903 | return ret; |
| 3904 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3905 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3906 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 3907 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3908 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3909 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3910 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) { |
| 3911 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3912 | ssl->handshake->out_msg_seq = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3913 | } else { |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3914 | ssl->handshake->in_msg_seq = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3915 | } |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3916 | } |
| 3917 | #endif |
| 3918 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3919 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 3920 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3921 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3922 | if ((ret = mbedtls_ssl_handshake(ssl)) != 0) { |
| 3923 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret); |
| 3924 | return ret; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3925 | } |
| 3926 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3927 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate")); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3928 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3929 | return 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3930 | } |
| 3931 | |
| 3932 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3933 | * Renegotiate current connection on client, |
| 3934 | * or request renegotiation on server |
| 3935 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3936 | int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3937 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3938 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3939 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3940 | if (ssl == NULL || ssl->conf == NULL) { |
| 3941 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3942 | } |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3943 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3944 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3945 | /* On server, just send the request */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3946 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 3947 | if (mbedtls_ssl_is_handshake_over(ssl) == 0) { |
| 3948 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3949 | } |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3950 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3951 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 3952 | |
| 3953 | /* Did we already try/start sending HelloRequest? */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3954 | if (ssl->out_left != 0) { |
| 3955 | return mbedtls_ssl_flush_output(ssl); |
| 3956 | } |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 3957 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3958 | return ssl_write_hello_request(ssl); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3959 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3960 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3961 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3962 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3963 | /* |
| 3964 | * On client, either start the renegotiation process or, |
| 3965 | * if already in progress, continue the handshake |
| 3966 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3967 | if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) { |
| 3968 | if (mbedtls_ssl_is_handshake_over(ssl) == 0) { |
| 3969 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3970 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3971 | |
| 3972 | if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) { |
| 3973 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret); |
| 3974 | return ret; |
| 3975 | } |
| 3976 | } else { |
| 3977 | if ((ret = mbedtls_ssl_handshake(ssl)) != 0) { |
| 3978 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret); |
| 3979 | return ret; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3980 | } |
| 3981 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3982 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3983 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3984 | return ret; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3985 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3986 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3987 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3988 | void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3989 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3990 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 3991 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3992 | if (handshake == NULL) { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3993 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3994 | } |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3995 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3996 | #if defined(MBEDTLS_ECP_C) |
| 3997 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3998 | if (ssl->handshake->group_list_heap_allocated) { |
| 3999 | mbedtls_free((void *) handshake->group_list); |
| 4000 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4001 | handshake->group_list = NULL; |
| 4002 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 4003 | #endif /* MBEDTLS_ECP_C */ |
| 4004 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4005 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4006 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4007 | if (ssl->handshake->sig_algs_heap_allocated) { |
| 4008 | mbedtls_free((void *) handshake->sig_algs); |
| 4009 | } |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4010 | handshake->sig_algs = NULL; |
| 4011 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Xiaofei Bai | c234ecf | 2022-02-08 09:59:23 +0000 | [diff] [blame] | 4012 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4013 | if (ssl->handshake->certificate_request_context) { |
| 4014 | mbedtls_free((void *) handshake->certificate_request_context); |
Xiaofei Bai | c234ecf | 2022-02-08 09:59:23 +0000 | [diff] [blame] | 4015 | } |
| 4016 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4017 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4018 | |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4019 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4020 | if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) { |
| 4021 | ssl->conf->f_async_cancel(ssl); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4022 | handshake->async_in_progress = 0; |
| 4023 | } |
| 4024 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 4025 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4026 | #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] | 4027 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4028 | psa_hash_abort(&handshake->fin_sha256_psa); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4029 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4030 | mbedtls_sha256_free(&handshake->fin_sha256); |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 4031 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4032 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4033 | #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] | 4034 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4035 | psa_hash_abort(&handshake->fin_sha384_psa); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4036 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4037 | mbedtls_sha512_free(&handshake->fin_sha384); |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 4038 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4039 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 4040 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4041 | #if defined(MBEDTLS_DHM_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4042 | mbedtls_dhm_free(&handshake->dhm_ctx); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4043 | #endif |
Neil Armstrong | f3f4641 | 2022-04-12 14:43:39 +0200 | [diff] [blame] | 4044 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4045 | mbedtls_ecdh_free(&handshake->ecdh_ctx); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 4046 | #endif |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 4047 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 4048 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 4049 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4050 | psa_pake_abort(&handshake->psa_pake_ctx); |
Valerio Setti | eb3f788 | 2022-12-08 18:42:58 +0100 | [diff] [blame] | 4051 | /* |
| 4052 | * Opaque keys are not stored in the handshake's data and it's the user |
| 4053 | * responsibility to destroy them. Clear ones, instead, are created by |
| 4054 | * the TLS library and should be destroyed at the same level |
| 4055 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4056 | if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) { |
| 4057 | psa_destroy_key(handshake->psa_pake_password); |
Valerio Setti | eb3f788 | 2022-12-08 18:42:58 +0100 | [diff] [blame] | 4058 | } |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 4059 | handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT; |
| 4060 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4061 | mbedtls_ecjpake_free(&handshake->ecjpake_ctx); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 4062 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 4063 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4064 | mbedtls_free(handshake->ecjpake_cache); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 4065 | handshake->ecjpake_cache = NULL; |
| 4066 | handshake->ecjpake_cache_len = 0; |
| 4067 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 4068 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 4069 | |
Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 4070 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 4071 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 4072 | /* explicit void pointer cast for buggy MS compiler */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4073 | mbedtls_free((void *) handshake->curves_tls_id); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 4074 | #endif |
| 4075 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 4076 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4077 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4078 | if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4079 | /* The maintenance of the external PSK key slot is the |
| 4080 | * user's responsibility. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4081 | if (ssl->handshake->psk_opaque_is_internal) { |
| 4082 | psa_destroy_key(ssl->handshake->psk_opaque); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4083 | ssl->handshake->psk_opaque_is_internal = 0; |
| 4084 | } |
| 4085 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 4086 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 4087 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4088 | if (handshake->psk != NULL) { |
| 4089 | mbedtls_platform_zeroize(handshake->psk, handshake->psk_len); |
| 4090 | mbedtls_free(handshake->psk); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 4091 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 4092 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 4093 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 4094 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4095 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 4096 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 4097 | /* |
| 4098 | * Free only the linked list wrapper, not the keys themselves |
| 4099 | * since the belong to the SNI callback |
| 4100 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4101 | ssl_key_cert_free(handshake->sni_key_cert); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4102 | #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] | 4103 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4104 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4105 | mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx); |
| 4106 | if (handshake->ecrs_peer_cert != NULL) { |
| 4107 | mbedtls_x509_crt_free(handshake->ecrs_peer_cert); |
| 4108 | mbedtls_free(handshake->ecrs_peer_cert); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 4109 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 4110 | #endif |
| 4111 | |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 4112 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 4113 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4114 | mbedtls_pk_free(&handshake->peer_pubkey); |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 4115 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4116 | |
XiaokangQian | 9b93c0d | 2022-02-09 06:02:25 +0000 | [diff] [blame] | 4117 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4118 | (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3)) |
| 4119 | mbedtls_free(handshake->cookie); |
XiaokangQian | 9b93c0d | 2022-02-09 06:02:25 +0000 | [diff] [blame] | 4120 | #endif /* MBEDTLS_SSL_CLI_C && |
| 4121 | ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */ |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 4122 | |
| 4123 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4124 | mbedtls_ssl_flight_free(handshake->flight); |
| 4125 | mbedtls_ssl_buffering_free(ssl); |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 4126 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 4127 | |
Ronald Cron | f12b81d | 2022-03-15 10:42:41 +0100 | [diff] [blame] | 4128 | #if defined(MBEDTLS_ECDH_C) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4129 | (defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)) |
| 4130 | if (handshake->ecdh_psa_privkey_is_external == 0) { |
| 4131 | psa_destroy_key(handshake->ecdh_psa_privkey); |
| 4132 | } |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 4133 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */ |
| 4134 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4135 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4136 | mbedtls_ssl_transform_free(handshake->transform_handshake); |
| 4137 | mbedtls_free(handshake->transform_handshake); |
Jerry Yu | 3d9b590 | 2022-11-04 14:07:25 +0800 | [diff] [blame] | 4138 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4139 | mbedtls_ssl_transform_free(handshake->transform_earlydata); |
| 4140 | mbedtls_free(handshake->transform_earlydata); |
Jerry Yu | 3d9b590 | 2022-11-04 14:07:25 +0800 | [diff] [blame] | 4141 | #endif |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4142 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 4143 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4144 | |
| 4145 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4146 | /* If the buffers are too big - reallocate. Because of the way Mbed TLS |
| 4147 | * processes datagrams and the fact that a datagram is allowed to have |
| 4148 | * several records in it, it is possible that the I/O buffers are not |
| 4149 | * empty at this stage */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4150 | handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl), |
| 4151 | mbedtls_ssl_get_output_buflen(ssl)); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4152 | #endif |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 4153 | |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 4154 | /* mbedtls_platform_zeroize MUST be last one in this function */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4155 | mbedtls_platform_zeroize(handshake, |
| 4156 | sizeof(mbedtls_ssl_handshake_params)); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4157 | } |
| 4158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4159 | void mbedtls_ssl_session_free(mbedtls_ssl_session *session) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4160 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4161 | if (session == NULL) { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4162 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4163 | } |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4164 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4165 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4166 | ssl_clear_peer_cert(session); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4167 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 4168 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4169 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Xiaokang Qian | bc663a0 | 2022-10-09 11:14:39 +0000 | [diff] [blame] | 4170 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 4171 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4172 | mbedtls_free(session->hostname); |
Xiaokang Qian | 281fd1b | 2022-09-20 11:35:41 +0000 | [diff] [blame] | 4173 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4174 | mbedtls_free(session->ticket); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 4175 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 4176 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4177 | mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session)); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4178 | } |
| 4179 | |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 4180 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4181 | |
| 4182 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4183 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u |
| 4184 | #else |
| 4185 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u |
| 4186 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4187 | |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4188 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4189 | |
| 4190 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4191 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u |
| 4192 | #else |
| 4193 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u |
| 4194 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 4195 | |
| 4196 | #if defined(MBEDTLS_SSL_ALPN) |
| 4197 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u |
| 4198 | #else |
| 4199 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u |
| 4200 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4201 | |
| 4202 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0 |
| 4203 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1 |
| 4204 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2 |
| 4205 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3 |
| 4206 | |
| 4207 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4208 | ((uint32_t) ( \ |
| 4209 | (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \ |
| 4210 | SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \ |
| 4211 | (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \ |
| 4212 | SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \ |
| 4213 | (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \ |
| 4214 | SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \ |
| 4215 | (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \ |
| 4216 | 0u)) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4217 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4218 | static unsigned char ssl_serialized_context_header[] = { |
| 4219 | MBEDTLS_VERSION_MAJOR, |
| 4220 | MBEDTLS_VERSION_MINOR, |
| 4221 | MBEDTLS_VERSION_PATCH, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4222 | MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG), |
| 4223 | MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG), |
| 4224 | MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG), |
| 4225 | MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG), |
| 4226 | MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG), |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4227 | }; |
| 4228 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4229 | /* |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4230 | * Serialize a full SSL context |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 4231 | * |
| 4232 | * The format of the serialized data is: |
| 4233 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 4234 | * |
| 4235 | * // header |
| 4236 | * opaque mbedtls_version[3]; // major, minor, patch |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4237 | * opaque context_format[5]; // version-specific field determining |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 4238 | * // the format of the remaining |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4239 | * // serialized data. |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4240 | * Note: When updating the format, remember to keep these |
| 4241 | * 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] | 4242 | * |
| 4243 | * // session sub-structure |
| 4244 | * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save() |
| 4245 | * // transform sub-structure |
| 4246 | * uint8 random[64]; // ServerHello.random+ClientHello.random |
| 4247 | * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value |
| 4248 | * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use |
| 4249 | * // fields from ssl_context |
| 4250 | * uint32 badmac_seen; // DTLS: number of records with failing MAC |
| 4251 | * uint64 in_window_top; // DTLS: last validated record seq_num |
| 4252 | * uint64 in_window; // DTLS: bitmask for replay protection |
| 4253 | * uint8 disable_datagram_packing; // DTLS: only one record per datagram |
| 4254 | * uint64 cur_out_ctr; // Record layer: outgoing sequence number |
| 4255 | * uint16 mtu; // DTLS: path mtu (max outgoing fragment size) |
| 4256 | * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol |
| 4257 | * |
| 4258 | * Note that many fields of the ssl_context or sub-structures are not |
| 4259 | * serialized, as they fall in one of the following categories: |
| 4260 | * |
| 4261 | * 1. forced value (eg in_left must be 0) |
| 4262 | * 2. pointer to dynamically-allocated memory (eg session, transform) |
| 4263 | * 3. value can be re-derived from other data (eg session keys from MS) |
| 4264 | * 4. value was temporary (eg content of input buffer) |
| 4265 | * 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] | 4266 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4267 | int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl, |
| 4268 | unsigned char *buf, |
| 4269 | size_t buf_len, |
| 4270 | size_t *olen) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4271 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4272 | unsigned char *p = buf; |
| 4273 | size_t used = 0; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4274 | size_t session_len; |
| 4275 | int ret = 0; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4276 | |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 4277 | /* |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4278 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in |
| 4279 | * this function's documentation. |
| 4280 | * |
| 4281 | * These are due to assumptions/limitations in the implementation. Some of |
| 4282 | * them are likely to stay (no handshake in progress) some might go away |
| 4283 | * (only DTLS) but are currently used to simplify the implementation. |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 4284 | */ |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4285 | /* The initial handshake must be over */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4286 | if (mbedtls_ssl_is_handshake_over(ssl) == 0) { |
| 4287 | MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over")); |
| 4288 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4289 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4290 | if (ssl->handshake != NULL) { |
| 4291 | MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed")); |
| 4292 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4293 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4294 | /* Double-check that sub-structures are indeed ready */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4295 | if (ssl->transform == NULL || ssl->session == NULL) { |
| 4296 | MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready")); |
| 4297 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4298 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4299 | /* There must be no pending incoming or outgoing data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4300 | if (mbedtls_ssl_check_pending(ssl) != 0) { |
| 4301 | MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data")); |
| 4302 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4303 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4304 | if (ssl->out_left != 0) { |
| 4305 | MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data")); |
| 4306 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4307 | } |
Dave Rodgman | 556e8a3 | 2022-12-06 16:31:25 +0000 | [diff] [blame] | 4308 | /* Protocol must be DTLS, not TLS */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4309 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 4310 | MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported")); |
| 4311 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4312 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4313 | /* Version must be 1.2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4314 | if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) { |
| 4315 | MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported")); |
| 4316 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4317 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4318 | /* We must be using an AEAD ciphersuite */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4319 | if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) { |
| 4320 | MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported")); |
| 4321 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4322 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4323 | /* Renegotiation must not be enabled */ |
| 4324 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4325 | if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) { |
| 4326 | MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled")); |
| 4327 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4328 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4329 | #endif |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4330 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4331 | /* |
| 4332 | * Version and format identifier |
| 4333 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4334 | used += sizeof(ssl_serialized_context_header); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4335 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4336 | if (used <= buf_len) { |
| 4337 | memcpy(p, ssl_serialized_context_header, |
| 4338 | sizeof(ssl_serialized_context_header)); |
| 4339 | p += sizeof(ssl_serialized_context_header); |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4340 | } |
| 4341 | |
| 4342 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4343 | * Session (length + data) |
| 4344 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4345 | ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len); |
| 4346 | if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) { |
| 4347 | return ret; |
| 4348 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4349 | |
| 4350 | used += 4 + session_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4351 | if (used <= buf_len) { |
| 4352 | MBEDTLS_PUT_UINT32_BE(session_len, p, 0); |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4353 | p += 4; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4354 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4355 | ret = ssl_session_save(ssl->session, 1, |
| 4356 | p, session_len, &session_len); |
| 4357 | if (ret != 0) { |
| 4358 | return ret; |
| 4359 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4360 | |
| 4361 | p += session_len; |
| 4362 | } |
| 4363 | |
| 4364 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4365 | * Transform |
| 4366 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4367 | used += sizeof(ssl->transform->randbytes); |
| 4368 | if (used <= buf_len) { |
| 4369 | memcpy(p, ssl->transform->randbytes, |
| 4370 | sizeof(ssl->transform->randbytes)); |
| 4371 | p += sizeof(ssl->transform->randbytes); |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4372 | } |
| 4373 | |
| 4374 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4375 | used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4376 | if (used <= buf_len) { |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4377 | *p++ = ssl->transform->in_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4378 | 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] | 4379 | p += ssl->transform->in_cid_len; |
| 4380 | |
| 4381 | *p++ = ssl->transform->out_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4382 | 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] | 4383 | p += ssl->transform->out_cid_len; |
| 4384 | } |
| 4385 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4386 | |
| 4387 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4388 | * Saved fields from top-level ssl_context structure |
| 4389 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4390 | used += 4; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4391 | if (used <= buf_len) { |
| 4392 | MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0); |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4393 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4394 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4395 | |
| 4396 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4397 | used += 16; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4398 | if (used <= buf_len) { |
| 4399 | MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0); |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4400 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4401 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4402 | MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0); |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4403 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4404 | } |
| 4405 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 4406 | |
| 4407 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4408 | used += 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4409 | if (used <= buf_len) { |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4410 | *p++ = ssl->disable_datagram_packing; |
| 4411 | } |
| 4412 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4413 | |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 4414 | used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4415 | if (used <= buf_len) { |
| 4416 | memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN); |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 4417 | p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4418 | } |
| 4419 | |
| 4420 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4421 | used += 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4422 | if (used <= buf_len) { |
| 4423 | MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0); |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4424 | p += 2; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4425 | } |
| 4426 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4427 | |
| 4428 | #if defined(MBEDTLS_SSL_ALPN) |
| 4429 | { |
| 4430 | const uint8_t alpn_len = ssl->alpn_chosen |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4431 | ? (uint8_t) strlen(ssl->alpn_chosen) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4432 | : 0; |
| 4433 | |
| 4434 | used += 1 + alpn_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4435 | if (used <= buf_len) { |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4436 | *p++ = alpn_len; |
| 4437 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4438 | if (ssl->alpn_chosen != NULL) { |
| 4439 | memcpy(p, ssl->alpn_chosen, alpn_len); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4440 | p += alpn_len; |
| 4441 | } |
| 4442 | } |
| 4443 | } |
| 4444 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4445 | |
| 4446 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4447 | * Done |
| 4448 | */ |
| 4449 | *olen = used; |
| 4450 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4451 | if (used > buf_len) { |
| 4452 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 4453 | } |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4454 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4455 | MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4456 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4457 | return mbedtls_ssl_session_reset_int(ssl, 0); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4458 | } |
| 4459 | |
| 4460 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 4461 | * Deserialize context, see mbedtls_ssl_context_save() for format. |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4462 | * |
| 4463 | * This internal version is wrapped by a public function that cleans up in |
| 4464 | * case of error. |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4465 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4466 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4467 | static int ssl_context_load(mbedtls_ssl_context *ssl, |
| 4468 | const unsigned char *buf, |
| 4469 | size_t len) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4470 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4471 | const unsigned char *p = buf; |
| 4472 | const unsigned char * const end = buf + len; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4473 | size_t session_len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4474 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4475 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 4476 | tls_prf_fn prf_func = NULL; |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4477 | #endif |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4478 | |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4479 | /* |
| 4480 | * The context should have been freshly setup or reset. |
| 4481 | * Give the user an error in case of obvious misuse. |
Manuel Pégourié-Gonnard | 4ca930f | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 4482 | * (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] | 4483 | * renegotiating, or if the user mistakenly loaded a session first.) |
| 4484 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4485 | if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST || |
| 4486 | ssl->session != NULL) { |
| 4487 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4488 | } |
| 4489 | |
| 4490 | /* |
| 4491 | * We can't check that the config matches the initial one, but we can at |
| 4492 | * least check it matches the requirements for serializing. |
| 4493 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4494 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 4495 | ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 || |
| 4496 | ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4497 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 4498 | ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4499 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4500 | 0) { |
| 4501 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4502 | } |
| 4503 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4504 | MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4505 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4506 | /* |
| 4507 | * Check version identifier |
| 4508 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4509 | if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) { |
| 4510 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4511 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4512 | |
| 4513 | if (memcmp(p, ssl_serialized_context_header, |
| 4514 | sizeof(ssl_serialized_context_header)) != 0) { |
| 4515 | return MBEDTLS_ERR_SSL_VERSION_MISMATCH; |
| 4516 | } |
| 4517 | p += sizeof(ssl_serialized_context_header); |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4518 | |
| 4519 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4520 | * Session |
| 4521 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4522 | if ((size_t) (end - p) < 4) { |
| 4523 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4524 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4525 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4526 | session_len = ((size_t) p[0] << 24) | |
| 4527 | ((size_t) p[1] << 16) | |
| 4528 | ((size_t) p[2] << 8) | |
| 4529 | ((size_t) p[3]); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4530 | p += 4; |
| 4531 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4532 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4533 | * 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] | 4534 | ssl->session = ssl->session_negotiate; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4535 | ssl->session_in = ssl->session; |
| 4536 | ssl->session_out = ssl->session; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4537 | ssl->session_negotiate = NULL; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4538 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4539 | if ((size_t) (end - p) < session_len) { |
| 4540 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4541 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4542 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4543 | ret = ssl_session_load(ssl->session, 1, p, session_len); |
| 4544 | if (ret != 0) { |
| 4545 | mbedtls_ssl_session_free(ssl->session); |
| 4546 | return ret; |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4547 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4548 | |
| 4549 | p += session_len; |
| 4550 | |
| 4551 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4552 | * Transform |
| 4553 | */ |
| 4554 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4555 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4556 | * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */ |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 4557 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4558 | ssl->transform = ssl->transform_negotiate; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4559 | ssl->transform_in = ssl->transform; |
| 4560 | ssl->transform_out = ssl->transform; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4561 | ssl->transform_negotiate = NULL; |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 4562 | #endif |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4563 | |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4564 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4565 | prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite); |
| 4566 | if (prf_func == NULL) { |
| 4567 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4568 | } |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 4569 | |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4570 | /* Read random bytes and populate structure */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4571 | if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) { |
| 4572 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4573 | } |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4574 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4575 | ret = ssl_tls12_populate_transform(ssl->transform, |
| 4576 | ssl->session->ciphersuite, |
| 4577 | ssl->session->master, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 4578 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4579 | ssl->session->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 4580 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4581 | prf_func, |
| 4582 | p, /* currently pointing to randbytes */ |
| 4583 | MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */ |
| 4584 | ssl->conf->endpoint, |
| 4585 | ssl); |
| 4586 | if (ret != 0) { |
| 4587 | return ret; |
| 4588 | } |
Jerry Yu | 840fbb2 | 2022-02-17 14:59:29 +0800 | [diff] [blame] | 4589 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4590 | p += sizeof(ssl->transform->randbytes); |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4591 | |
| 4592 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4593 | /* Read connection IDs and store them */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4594 | if ((size_t) (end - p) < 1) { |
| 4595 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4596 | } |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4597 | |
| 4598 | ssl->transform->in_cid_len = *p++; |
| 4599 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4600 | if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) { |
| 4601 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4602 | } |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4603 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4604 | 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] | 4605 | p += ssl->transform->in_cid_len; |
| 4606 | |
| 4607 | ssl->transform->out_cid_len = *p++; |
| 4608 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4609 | if ((size_t) (end - p) < ssl->transform->out_cid_len) { |
| 4610 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4611 | } |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4612 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4613 | 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] | 4614 | p += ssl->transform->out_cid_len; |
| 4615 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4616 | |
| 4617 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4618 | * Saved fields from top-level ssl_context structure |
| 4619 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4620 | if ((size_t) (end - p) < 4) { |
| 4621 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4622 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4623 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4624 | ssl->badmac_seen = ((uint32_t) p[0] << 24) | |
| 4625 | ((uint32_t) p[1] << 16) | |
| 4626 | ((uint32_t) p[2] << 8) | |
| 4627 | ((uint32_t) p[3]); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4628 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4629 | |
| 4630 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4631 | if ((size_t) (end - p) < 16) { |
| 4632 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4633 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4634 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4635 | ssl->in_window_top = ((uint64_t) p[0] << 56) | |
| 4636 | ((uint64_t) p[1] << 48) | |
| 4637 | ((uint64_t) p[2] << 40) | |
| 4638 | ((uint64_t) p[3] << 32) | |
| 4639 | ((uint64_t) p[4] << 24) | |
| 4640 | ((uint64_t) p[5] << 16) | |
| 4641 | ((uint64_t) p[6] << 8) | |
| 4642 | ((uint64_t) p[7]); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4643 | p += 8; |
| 4644 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4645 | ssl->in_window = ((uint64_t) p[0] << 56) | |
| 4646 | ((uint64_t) p[1] << 48) | |
| 4647 | ((uint64_t) p[2] << 40) | |
| 4648 | ((uint64_t) p[3] << 32) | |
| 4649 | ((uint64_t) p[4] << 24) | |
| 4650 | ((uint64_t) p[5] << 16) | |
| 4651 | ((uint64_t) p[6] << 8) | |
| 4652 | ((uint64_t) p[7]); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4653 | p += 8; |
| 4654 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 4655 | |
| 4656 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4657 | if ((size_t) (end - p) < 1) { |
| 4658 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4659 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4660 | |
| 4661 | ssl->disable_datagram_packing = *p++; |
| 4662 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4663 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4664 | if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) { |
| 4665 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4666 | } |
| 4667 | memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr)); |
| 4668 | p += sizeof(ssl->cur_out_ctr); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4669 | |
| 4670 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4671 | if ((size_t) (end - p) < 2) { |
| 4672 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4673 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4674 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4675 | ssl->mtu = (p[0] << 8) | p[1]; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4676 | p += 2; |
| 4677 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4678 | |
| 4679 | #if defined(MBEDTLS_SSL_ALPN) |
| 4680 | { |
| 4681 | uint8_t alpn_len; |
| 4682 | const char **cur; |
| 4683 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4684 | if ((size_t) (end - p) < 1) { |
| 4685 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4686 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4687 | |
| 4688 | alpn_len = *p++; |
| 4689 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4690 | if (alpn_len != 0 && ssl->conf->alpn_list != NULL) { |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4691 | /* alpn_chosen should point to an item in the configured list */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4692 | for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) { |
| 4693 | if (strlen(*cur) == alpn_len && |
| 4694 | memcmp(p, cur, alpn_len) == 0) { |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4695 | ssl->alpn_chosen = *cur; |
| 4696 | break; |
| 4697 | } |
| 4698 | } |
| 4699 | } |
| 4700 | |
| 4701 | /* can only happen on conf mismatch */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4702 | if (alpn_len != 0 && ssl->alpn_chosen == NULL) { |
| 4703 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4704 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4705 | |
| 4706 | p += alpn_len; |
| 4707 | } |
| 4708 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4709 | |
| 4710 | /* |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4711 | * Forced fields from top-level ssl_context structure |
| 4712 | * |
| 4713 | * Most of them already set to the correct value by mbedtls_ssl_init() and |
| 4714 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. |
| 4715 | */ |
| 4716 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 4717 | ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4718 | |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 4719 | /* Adjust pointers for header fields of outgoing records to |
| 4720 | * the given transform, accounting for explicit IV and CID. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4721 | mbedtls_ssl_update_out_pointers(ssl, ssl->transform); |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 4722 | |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4723 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4724 | ssl->in_epoch = 1; |
| 4725 | #endif |
| 4726 | |
| 4727 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, |
| 4728 | * 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] | 4729 | * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform() |
| 4730 | * inappropriately. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4731 | if (ssl->handshake != NULL) { |
| 4732 | mbedtls_ssl_handshake_free(ssl); |
| 4733 | mbedtls_free(ssl->handshake); |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4734 | ssl->handshake = NULL; |
| 4735 | } |
| 4736 | |
| 4737 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4738 | * Done - should have consumed entire buffer |
| 4739 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4740 | if (p != end) { |
| 4741 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4742 | } |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4743 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4744 | return 0; |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4745 | } |
| 4746 | |
| 4747 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 4748 | * Deserialize context: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4749 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4750 | int mbedtls_ssl_context_load(mbedtls_ssl_context *context, |
| 4751 | const unsigned char *buf, |
| 4752 | size_t len) |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4753 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4754 | int ret = ssl_context_load(context, buf, len); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4755 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4756 | if (ret != 0) { |
| 4757 | mbedtls_ssl_free(context); |
| 4758 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4759 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4760 | return ret; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4761 | } |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 4762 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4763 | |
| 4764 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4765 | * Free an SSL context |
| 4766 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4767 | void mbedtls_ssl_free(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4768 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4769 | if (ssl == NULL) { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4770 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4771 | } |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4772 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4773 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> free")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4774 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4775 | if (ssl->out_buf != NULL) { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 4776 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4777 | size_t out_buf_len = ssl->out_buf_len; |
| 4778 | #else |
| 4779 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 4780 | #endif |
| 4781 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4782 | mbedtls_platform_zeroize(ssl->out_buf, out_buf_len); |
| 4783 | mbedtls_free(ssl->out_buf); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4784 | ssl->out_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4785 | } |
| 4786 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4787 | if (ssl->in_buf != NULL) { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 4788 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4789 | size_t in_buf_len = ssl->in_buf_len; |
| 4790 | #else |
| 4791 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 4792 | #endif |
| 4793 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4794 | mbedtls_platform_zeroize(ssl->in_buf, in_buf_len); |
| 4795 | mbedtls_free(ssl->in_buf); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4796 | ssl->in_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4797 | } |
| 4798 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4799 | if (ssl->transform) { |
| 4800 | mbedtls_ssl_transform_free(ssl->transform); |
| 4801 | mbedtls_free(ssl->transform); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4802 | } |
| 4803 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4804 | if (ssl->handshake) { |
| 4805 | mbedtls_ssl_handshake_free(ssl); |
| 4806 | mbedtls_free(ssl->handshake); |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 4807 | |
| 4808 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4809 | mbedtls_ssl_transform_free(ssl->transform_negotiate); |
| 4810 | mbedtls_free(ssl->transform_negotiate); |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 4811 | #endif |
| 4812 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4813 | mbedtls_ssl_session_free(ssl->session_negotiate); |
| 4814 | mbedtls_free(ssl->session_negotiate); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4815 | } |
| 4816 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4817 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4818 | mbedtls_ssl_transform_free(ssl->transform_application); |
| 4819 | mbedtls_free(ssl->transform_application); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4820 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 4821 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4822 | if (ssl->session) { |
| 4823 | mbedtls_ssl_session_free(ssl->session); |
| 4824 | mbedtls_free(ssl->session); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 4825 | } |
| 4826 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 4827 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4828 | if (ssl->hostname != NULL) { |
| 4829 | mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname)); |
| 4830 | mbedtls_free(ssl->hostname); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4831 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 4832 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4833 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 4834 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4835 | mbedtls_free(ssl->cli_id); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 4836 | #endif |
| 4837 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4838 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= free")); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 4839 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 4840 | /* Actually clear after last debug message */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4841 | mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context)); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4842 | } |
| 4843 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4844 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 4845 | * Initialize mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4846 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4847 | void mbedtls_ssl_config_init(mbedtls_ssl_config *conf) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4848 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4849 | memset(conf, 0, sizeof(mbedtls_ssl_config)); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4850 | } |
| 4851 | |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4852 | /* The selection should be the same as mbedtls_x509_crt_profile_default in |
| 4853 | * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters: |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4854 | * curves with a lower resource usage come first. |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4855 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4856 | * about this list. |
| 4857 | */ |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4858 | static uint16_t ssl_preset_default_groups[] = { |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4859 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4860 | MBEDTLS_SSL_IANA_TLS_GROUP_X25519, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4861 | #endif |
| 4862 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4863 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4864 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4865 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4866 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4867 | #endif |
| 4868 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4869 | MBEDTLS_SSL_IANA_TLS_GROUP_X448, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4870 | #endif |
| 4871 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4872 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4873 | #endif |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4874 | #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4875 | MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4876 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4877 | #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4878 | MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4879 | #endif |
| 4880 | #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4881 | MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4882 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4883 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4884 | }; |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4885 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4886 | static int ssl_preset_suiteb_ciphersuites[] = { |
| 4887 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 4888 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 4889 | 0 |
| 4890 | }; |
| 4891 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4892 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4893 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4894 | /* NOTICE: |
Jerry Yu | 0b994b8 | 2022-01-25 17:22:12 +0800 | [diff] [blame] | 4895 | * 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] | 4896 | * rules SHOULD be upheld. |
| 4897 | * - No duplicate entries. |
| 4898 | * - 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] | 4899 | * - ssl_tls12_preset* is for TLS 1.2 use only. |
Jerry Yu | 370e146 | 2022-01-25 10:36:53 +0800 | [diff] [blame] | 4900 | * - 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] | 4901 | */ |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4902 | static uint16_t ssl_preset_default_sig_algs[] = { |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4903 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4904 | #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] | 4905 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 4906 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4907 | #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] | 4908 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4909 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4910 | #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] | 4911 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 4912 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4913 | #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] | 4914 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
| 4915 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4916 | #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] | 4917 | defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
| 4918 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4919 | #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] | 4920 | MBEDTLS_ECP_DP_SECP521R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4921 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4922 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \ |
| 4923 | 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] | 4924 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4925 | #endif \ |
| 4926 | /* 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] | 4927 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4928 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \ |
| 4929 | 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] | 4930 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4931 | #endif \ |
| 4932 | /* 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] | 4933 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4934 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \ |
| 4935 | 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] | 4936 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4937 | #endif \ |
| 4938 | /* 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] | 4939 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4940 | #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] | 4941 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512, |
| 4942 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */ |
| 4943 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4944 | #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] | 4945 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384, |
| 4946 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */ |
| 4947 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4948 | #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] | 4949 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
| 4950 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */ |
| 4951 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4952 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4953 | }; |
| 4954 | |
| 4955 | /* NOTICE: see above */ |
| 4956 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4957 | static uint16_t ssl_tls12_preset_default_sig_algs[] = { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4958 | #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] | 4959 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4960 | 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] | 4961 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4962 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 4963 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, |
| 4964 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4965 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4966 | 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] | 4967 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4968 | #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] | 4969 | #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] | 4970 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4971 | 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] | 4972 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4973 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 4974 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, |
| 4975 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4976 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4977 | 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] | 4978 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4979 | #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] | 4980 | #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] | 4981 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4982 | 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] | 4983 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4984 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 4985 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
| 4986 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4987 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4988 | 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] | 4989 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4990 | #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] | 4991 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4992 | }; |
| 4993 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4994 | /* NOTICE: see above */ |
| 4995 | static uint16_t ssl_preset_suiteb_sig_algs[] = { |
| 4996 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4997 | #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] | 4998 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 4999 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5000 | #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] | 5001 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
| 5002 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5003 | #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] | 5004 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 5005 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5006 | #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] | 5007 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5008 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5009 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \ |
| 5010 | 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] | 5011 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5012 | #endif \ |
| 5013 | /* 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] | 5014 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5015 | #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] | 5016 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5017 | #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] | 5018 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5019 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 5020 | }; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 5021 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5022 | /* NOTICE: see above */ |
| 5023 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5024 | static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5025 | #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] | 5026 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5027 | 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] | 5028 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5029 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5030 | 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] | 5031 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5032 | #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] | 5033 | #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] | 5034 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5035 | 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] | 5036 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5037 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5038 | 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] | 5039 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5040 | #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] | 5041 | MBEDTLS_TLS_SIG_NONE |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 5042 | }; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5043 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5044 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5045 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5046 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5047 | static uint16_t ssl_preset_suiteb_groups[] = { |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 5048 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5049 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 5050 | #endif |
| 5051 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5052 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 5053 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5054 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5055 | }; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5056 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5057 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5058 | /* 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] | 5059 | * to make sure there are no duplicated signature algorithm entries. */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5060 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5061 | static int ssl_check_no_sig_alg_duplication(uint16_t *sig_algs) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5062 | { |
| 5063 | size_t i, j; |
| 5064 | int ret = 0; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5065 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5066 | for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) { |
| 5067 | for (j = 0; j < i; j++) { |
| 5068 | if (sig_algs[i] != sig_algs[j]) { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 5069 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5070 | } |
| 5071 | mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET |
| 5072 | ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n", |
| 5073 | sig_algs[i], j, i); |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 5074 | ret = -1; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5075 | } |
| 5076 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5077 | return ret; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5078 | } |
| 5079 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5080 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5081 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5082 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 5083 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5084 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5085 | int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf, |
| 5086 | int endpoint, int transport, int preset) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5087 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 5088 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5089 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 5090 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5091 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5092 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5093 | if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) { |
| 5094 | mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n"); |
| 5095 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5096 | } |
| 5097 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5098 | if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) { |
| 5099 | mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n"); |
| 5100 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5101 | } |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5102 | |
| 5103 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5104 | if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) { |
| 5105 | mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n"); |
| 5106 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5107 | } |
| 5108 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5109 | if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) { |
| 5110 | mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n"); |
| 5111 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5112 | } |
| 5113 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5114 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5115 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 5116 | /* Use the functions here so that they are covered in tests, |
| 5117 | * but otherwise access member directly for efficiency */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5118 | mbedtls_ssl_conf_endpoint(conf, endpoint); |
| 5119 | mbedtls_ssl_conf_transport(conf, transport); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5120 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5121 | /* |
| 5122 | * Things that are common to all presets |
| 5123 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 5124 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5125 | if (endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 5126 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 5127 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 5128 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
| 5129 | #endif |
| 5130 | } |
| 5131 | #endif |
| 5132 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5133 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 5134 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 5135 | #endif |
| 5136 | |
| 5137 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 5138 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 5139 | #endif |
| 5140 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 5141 | #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] | 5142 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 5143 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 5144 | #endif |
| 5145 | |
| 5146 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 5147 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 5148 | #endif |
| 5149 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 5150 | #if defined(MBEDTLS_SSL_SRV_C) |
| 5151 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
TRodziewicz | 3946f79 | 2021-06-14 12:11:18 +0200 | [diff] [blame] | 5152 | conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER; |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 5153 | #endif |
| 5154 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5155 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5156 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
| 5157 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
| 5158 | #endif |
| 5159 | |
| 5160 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 5161 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5162 | memset(conf->renego_period, 0x00, 2); |
| 5163 | memset(conf->renego_period + 2, 0xFF, 6); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5164 | #endif |
| 5165 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5166 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5167 | if (endpoint == MBEDTLS_SSL_IS_SERVER) { |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 5168 | const unsigned char dhm_p[] = |
| 5169 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 5170 | const unsigned char dhm_g[] = |
| 5171 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 5172 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5173 | if ((ret = mbedtls_ssl_conf_dh_param_bin(conf, |
| 5174 | dhm_p, sizeof(dhm_p), |
| 5175 | dhm_g, sizeof(dhm_g))) != 0) { |
| 5176 | return ret; |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 5177 | } |
| 5178 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 5179 | #endif |
| 5180 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5181 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 6ee56aa | 2022-12-06 17:47:22 +0800 | [diff] [blame] | 5182 | |
| 5183 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5184 | mbedtls_ssl_tls13_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED); |
Jerry Yu | 6ee56aa | 2022-12-06 17:47:22 +0800 | [diff] [blame] | 5185 | #if defined(MBEDTLS_SSL_SRV_C) |
| 5186 | mbedtls_ssl_tls13_conf_max_early_data_size( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5187 | conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE); |
Jerry Yu | 6ee56aa | 2022-12-06 17:47:22 +0800 | [diff] [blame] | 5188 | #endif |
| 5189 | #endif /* MBEDTLS_SSL_EARLY_DATA */ |
| 5190 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 5191 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 5192 | mbedtls_ssl_conf_new_session_tickets( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5193 | conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS); |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 5194 | #endif |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 5195 | /* |
| 5196 | * Allow all TLS 1.3 key exchange modes by default. |
| 5197 | */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 5198 | conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL; |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5199 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 5200 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5201 | if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 5202 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5203 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 5204 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5205 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5206 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 5207 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5208 | } else { |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5209 | #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] | 5210 | if (endpoint == MBEDTLS_SSL_IS_CLIENT) { |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5211 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5212 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5213 | } else { |
| 5214 | /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */ |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5215 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5216 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5217 | } |
| 5218 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 5219 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 5220 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 5221 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5222 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5223 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5224 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5225 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5226 | #endif |
| 5227 | } |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 5228 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5229 | /* |
| 5230 | * Preset-specific defaults |
| 5231 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5232 | switch (preset) { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5233 | /* |
| 5234 | * NSA Suite B |
| 5235 | */ |
| 5236 | case MBEDTLS_SSL_PRESET_SUITEB: |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5237 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 5238 | conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5239 | |
| 5240 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5241 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5242 | #endif |
| 5243 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5244 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5245 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5246 | if (mbedtls_ssl_conf_is_tls12_only(conf)) { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5247 | conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5248 | } else |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5249 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5250 | conf->sig_algs = ssl_preset_suiteb_sig_algs; |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5251 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5252 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5253 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 5254 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5255 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5256 | conf->group_list = ssl_preset_suiteb_groups; |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 5257 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5258 | |
| 5259 | /* |
| 5260 | * Default |
| 5261 | */ |
| 5262 | default: |
Ronald Cron | f660655 | 2022-03-15 11:23:25 +0100 | [diff] [blame] | 5263 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 5264 | conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites(); |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5265 | |
| 5266 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5267 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 5268 | #endif |
| 5269 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5270 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5271 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5272 | if (mbedtls_ssl_conf_is_tls12_only(conf)) { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5273 | conf->sig_algs = ssl_tls12_preset_default_sig_algs; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5274 | } else |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5275 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5276 | conf->sig_algs = ssl_preset_default_sig_algs; |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5277 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5278 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5279 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 5280 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5281 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5282 | conf->group_list = ssl_preset_default_groups; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5283 | |
| 5284 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 5285 | conf->dhm_min_bitlen = 1024; |
| 5286 | #endif |
| 5287 | } |
| 5288 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5289 | return 0; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5290 | } |
| 5291 | |
| 5292 | /* |
| 5293 | * Free mbedtls_ssl_config |
| 5294 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5295 | void mbedtls_ssl_config_free(mbedtls_ssl_config *conf) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5296 | { |
| 5297 | #if defined(MBEDTLS_DHM_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5298 | mbedtls_mpi_free(&conf->dhm_P); |
| 5299 | mbedtls_mpi_free(&conf->dhm_G); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5300 | #endif |
| 5301 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 5302 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 5303 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5304 | if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 5305 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 5306 | } |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5307 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5308 | if (conf->psk != NULL) { |
| 5309 | mbedtls_platform_zeroize(conf->psk, conf->psk_len); |
| 5310 | mbedtls_free(conf->psk); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 5311 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5312 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 5313 | } |
| 5314 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5315 | if (conf->psk_identity != NULL) { |
| 5316 | mbedtls_platform_zeroize(conf->psk_identity, conf->psk_identity_len); |
| 5317 | mbedtls_free(conf->psk_identity); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 5318 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5319 | conf->psk_identity_len = 0; |
| 5320 | } |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 5321 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5322 | |
| 5323 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5324 | ssl_key_cert_free(conf->key_cert); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5325 | #endif |
| 5326 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5327 | mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config)); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5328 | } |
| 5329 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 5330 | #if defined(MBEDTLS_PK_C) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5331 | (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C)) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5332 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5333 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5334 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5335 | 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] | 5336 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5337 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5338 | if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) { |
| 5339 | return MBEDTLS_SSL_SIG_RSA; |
| 5340 | } |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5341 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5342 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5343 | if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) { |
| 5344 | return MBEDTLS_SSL_SIG_ECDSA; |
| 5345 | } |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5346 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5347 | return MBEDTLS_SSL_SIG_ANON; |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5348 | } |
| 5349 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5350 | 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] | 5351 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5352 | switch (type) { |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 5353 | case MBEDTLS_PK_RSA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5354 | return MBEDTLS_SSL_SIG_RSA; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 5355 | case MBEDTLS_PK_ECDSA: |
| 5356 | case MBEDTLS_PK_ECKEY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5357 | return MBEDTLS_SSL_SIG_ECDSA; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 5358 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5359 | return MBEDTLS_SSL_SIG_ANON; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 5360 | } |
| 5361 | } |
| 5362 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5363 | 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] | 5364 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5365 | switch (sig) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5366 | #if defined(MBEDTLS_RSA_C) |
| 5367 | case MBEDTLS_SSL_SIG_RSA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5368 | return MBEDTLS_PK_RSA; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5369 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5370 | #if defined(MBEDTLS_ECDSA_C) |
| 5371 | case MBEDTLS_SSL_SIG_ECDSA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5372 | return MBEDTLS_PK_ECDSA; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5373 | #endif |
| 5374 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5375 | return MBEDTLS_PK_NONE; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5376 | } |
| 5377 | } |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 5378 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5379 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 5380 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5381 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 5382 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5383 | 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] | 5384 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5385 | switch (hash) { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5386 | #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] | 5387 | case MBEDTLS_SSL_HASH_MD5: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5388 | return MBEDTLS_MD_MD5; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5389 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5390 | #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] | 5391 | case MBEDTLS_SSL_HASH_SHA1: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5392 | return MBEDTLS_MD_SHA1; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5393 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5394 | #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] | 5395 | case MBEDTLS_SSL_HASH_SHA224: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5396 | return MBEDTLS_MD_SHA224; |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 5397 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5398 | #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] | 5399 | case MBEDTLS_SSL_HASH_SHA256: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5400 | return MBEDTLS_MD_SHA256; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5401 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5402 | #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] | 5403 | case MBEDTLS_SSL_HASH_SHA384: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5404 | return MBEDTLS_MD_SHA384; |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 5405 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5406 | #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] | 5407 | case MBEDTLS_SSL_HASH_SHA512: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5408 | return MBEDTLS_MD_SHA512; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5409 | #endif |
| 5410 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5411 | return MBEDTLS_MD_NONE; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5412 | } |
| 5413 | } |
| 5414 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5415 | /* |
| 5416 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 5417 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5418 | unsigned char mbedtls_ssl_hash_from_md_alg(int md) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5419 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5420 | switch (md) { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5421 | #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] | 5422 | case MBEDTLS_MD_MD5: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5423 | return MBEDTLS_SSL_HASH_MD5; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5424 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5425 | #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] | 5426 | case MBEDTLS_MD_SHA1: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5427 | return MBEDTLS_SSL_HASH_SHA1; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5428 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5429 | #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] | 5430 | case MBEDTLS_MD_SHA224: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5431 | return MBEDTLS_SSL_HASH_SHA224; |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 5432 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5433 | #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] | 5434 | case MBEDTLS_MD_SHA256: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5435 | return MBEDTLS_SSL_HASH_SHA256; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5436 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5437 | #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] | 5438 | case MBEDTLS_MD_SHA384: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5439 | return MBEDTLS_SSL_HASH_SHA384; |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 5440 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5441 | #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] | 5442 | case MBEDTLS_MD_SHA512: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5443 | return MBEDTLS_SSL_HASH_SHA512; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5444 | #endif |
| 5445 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5446 | return MBEDTLS_SSL_HASH_NONE; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5447 | } |
| 5448 | } |
| 5449 | |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5450 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5451 | * 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] | 5452 | * 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] | 5453 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5454 | 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] | 5455 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5456 | const uint16_t *group_list = mbedtls_ssl_get_groups(ssl); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5457 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5458 | if (group_list == NULL) { |
| 5459 | return -1; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5460 | } |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5461 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5462 | for (; *group_list != 0; group_list++) { |
| 5463 | if (*group_list == tls_id) { |
| 5464 | return 0; |
| 5465 | } |
| 5466 | } |
| 5467 | |
| 5468 | return -1; |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5469 | } |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 5470 | |
| 5471 | #if defined(MBEDTLS_ECP_C) |
| 5472 | /* |
| 5473 | * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id. |
| 5474 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5475 | 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] | 5476 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5477 | 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] | 5478 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5479 | if (tls_id == 0) { |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 5480 | return -1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5481 | } |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 5482 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5483 | return mbedtls_ssl_check_curve_tls_id(ssl, tls_id); |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 5484 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 5485 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5486 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5487 | #if defined(MBEDTLS_DEBUG_C) |
Valerio Setti | 67419f0 | 2023-01-04 16:12:42 +0100 | [diff] [blame] | 5488 | #define EC_NAME(_name_) _name_ |
| 5489 | #else |
| 5490 | #define EC_NAME(_name_) NULL |
| 5491 | #endif |
| 5492 | |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5493 | static const struct { |
| 5494 | uint16_t tls_id; |
| 5495 | mbedtls_ecp_group_id ecp_group_id; |
| 5496 | psa_ecc_family_t psa_family; |
| 5497 | uint16_t bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5498 | const char *name; |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5499 | } tls_id_match_table[] = |
| 5500 | { |
| 5501 | #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] | 5502 | { 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] | 5503 | #endif |
| 5504 | #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] | 5505 | { 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] | 5506 | #endif |
Valerio Setti | 67419f0 | 2023-01-04 16:12:42 +0100 | [diff] [blame] | 5507 | #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] | 5508 | { 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] | 5509 | #endif |
Valerio Setti | 67419f0 | 2023-01-04 16:12:42 +0100 | [diff] [blame] | 5510 | #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] | 5511 | { 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] | 5512 | #endif |
Valerio Setti | 67419f0 | 2023-01-04 16:12:42 +0100 | [diff] [blame] | 5513 | #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] | 5514 | { 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] | 5515 | #endif |
| 5516 | #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] | 5517 | { 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] | 5518 | #endif |
Valerio Setti | 67419f0 | 2023-01-04 16:12:42 +0100 | [diff] [blame] | 5519 | #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] | 5520 | { 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] | 5521 | #endif |
| 5522 | #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] | 5523 | { 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] | 5524 | #endif |
| 5525 | #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] | 5526 | { 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] | 5527 | #endif |
| 5528 | #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] | 5529 | { 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] | 5530 | #endif |
| 5531 | #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] | 5532 | { 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] | 5533 | #endif |
| 5534 | #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] | 5535 | { 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] | 5536 | #endif |
| 5537 | #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] | 5538 | { 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] | 5539 | #endif |
| 5540 | { 0, MBEDTLS_ECP_DP_NONE, 0, 0, NULL }, |
| 5541 | }; |
| 5542 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5543 | int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id, |
| 5544 | psa_ecc_family_t *family, |
| 5545 | size_t *bits) |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5546 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5547 | for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) { |
| 5548 | if (tls_id_match_table[i].tls_id == tls_id) { |
| 5549 | if (family != NULL) { |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5550 | *family = tls_id_match_table[i].psa_family; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5551 | } |
| 5552 | if (bits != NULL) { |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5553 | *bits = tls_id_match_table[i].bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5554 | } |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5555 | return PSA_SUCCESS; |
| 5556 | } |
| 5557 | } |
| 5558 | |
| 5559 | return PSA_ERROR_NOT_SUPPORTED; |
| 5560 | } |
| 5561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5562 | 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] | 5563 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5564 | for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) { |
| 5565 | if (tls_id_match_table[i].tls_id == tls_id) { |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5566 | return tls_id_match_table[i].ecp_group_id; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5567 | } |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5568 | } |
| 5569 | |
| 5570 | return MBEDTLS_ECP_DP_NONE; |
| 5571 | } |
| 5572 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5573 | 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] | 5574 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5575 | for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE; |
| 5576 | i++) { |
| 5577 | if (tls_id_match_table[i].ecp_group_id == grp_id) { |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5578 | return tls_id_match_table[i].tls_id; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5579 | } |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5580 | } |
| 5581 | |
| 5582 | return 0; |
| 5583 | } |
| 5584 | |
Valerio Setti | 67419f0 | 2023-01-04 16:12:42 +0100 | [diff] [blame] | 5585 | #if defined(MBEDTLS_DEBUG_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5586 | 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] | 5587 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5588 | for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) { |
| 5589 | if (tls_id_match_table[i].tls_id == tls_id) { |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5590 | return tls_id_match_table[i].name; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5591 | } |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5592 | } |
| 5593 | |
| 5594 | return NULL; |
| 5595 | } |
Valerio Setti | 67419f0 | 2023-01-04 16:12:42 +0100 | [diff] [blame] | 5596 | #endif |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5597 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5598 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5599 | int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert, |
| 5600 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
| 5601 | int cert_endpoint, |
| 5602 | uint32_t *flags) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5603 | { |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5604 | int ret = 0; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5605 | int usage = 0; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5606 | const char *ext_oid; |
| 5607 | size_t ext_len; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5608 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5609 | if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) { |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5610 | /* Server part of the key exchange */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5611 | switch (ciphersuite->key_exchange) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5612 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 5613 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5614 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5615 | break; |
| 5616 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5617 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 5618 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 5619 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 5620 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5621 | break; |
| 5622 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5623 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 5624 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5625 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5626 | break; |
| 5627 | |
| 5628 | /* 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] | 5629 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 5630 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 5631 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 5632 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 5633 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5634 | usage = 0; |
| 5635 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5636 | } else { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5637 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ |
| 5638 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5639 | } |
| 5640 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5641 | if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5642 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5643 | ret = -1; |
| 5644 | } |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5645 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5646 | if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5647 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5648 | ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH); |
| 5649 | } else { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5650 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5651 | ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5652 | } |
| 5653 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5654 | 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] | 5655 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5656 | ret = -1; |
| 5657 | } |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5658 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5659 | return ret; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5660 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5661 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 5662 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5663 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5664 | int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl, |
| 5665 | const mbedtls_md_type_t md, |
| 5666 | unsigned char *dst, |
| 5667 | size_t dst_len, |
| 5668 | size_t *olen) |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5669 | { |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5670 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5671 | psa_hash_operation_t *hash_operation_to_clone; |
| 5672 | psa_hash_operation_t hash_operation = psa_hash_operation_init(); |
| 5673 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5674 | *olen = 0; |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5676 | switch (md) { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5677 | #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] | 5678 | case MBEDTLS_MD_SHA384: |
| 5679 | hash_operation_to_clone = &ssl->handshake->fin_sha384_psa; |
| 5680 | break; |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5681 | #endif |
| 5682 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5683 | #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] | 5684 | case MBEDTLS_MD_SHA256: |
| 5685 | hash_operation_to_clone = &ssl->handshake->fin_sha256_psa; |
| 5686 | break; |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5687 | #endif |
| 5688 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5689 | default: |
| 5690 | goto exit; |
| 5691 | } |
| 5692 | |
| 5693 | status = psa_hash_clone(hash_operation_to_clone, &hash_operation); |
| 5694 | if (status != PSA_SUCCESS) { |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5695 | goto exit; |
| 5696 | } |
| 5697 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5698 | status = psa_hash_finish(&hash_operation, dst, dst_len, olen); |
| 5699 | if (status != PSA_SUCCESS) { |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5700 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5701 | } |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5702 | |
| 5703 | exit: |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 5704 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 5705 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 5706 | (void) ssl; |
| 5707 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5708 | return psa_ssl_status_to_mbedtls(status); |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5709 | } |
| 5710 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5711 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5712 | #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] | 5713 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5714 | static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl, |
| 5715 | unsigned char *dst, |
| 5716 | size_t dst_len, |
| 5717 | size_t *olen) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5718 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5719 | int ret; |
| 5720 | mbedtls_sha512_context sha512; |
| 5721 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5722 | if (dst_len < 48) { |
| 5723 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 5724 | } |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5725 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5726 | mbedtls_sha512_init(&sha512); |
| 5727 | mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5728 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5729 | if ((ret = mbedtls_sha512_finish(&sha512, dst)) != 0) { |
| 5730 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha512_finish", ret); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5731 | goto exit; |
| 5732 | } |
| 5733 | |
| 5734 | *olen = 48; |
| 5735 | |
| 5736 | exit: |
| 5737 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5738 | mbedtls_sha512_free(&sha512); |
| 5739 | return ret; |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5740 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5741 | #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] | 5742 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5743 | #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] | 5744 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5745 | static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl, |
| 5746 | unsigned char *dst, |
| 5747 | size_t dst_len, |
| 5748 | size_t *olen) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5749 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5750 | int ret; |
| 5751 | mbedtls_sha256_context sha256; |
| 5752 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5753 | if (dst_len < 32) { |
| 5754 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 5755 | } |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5756 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5757 | mbedtls_sha256_init(&sha256); |
| 5758 | mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256); |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5759 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5760 | if ((ret = mbedtls_sha256_finish(&sha256, dst)) != 0) { |
| 5761 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha256_finish", ret); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5762 | goto exit; |
| 5763 | } |
| 5764 | |
| 5765 | *olen = 32; |
| 5766 | |
| 5767 | exit: |
| 5768 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5769 | mbedtls_sha256_free(&sha256); |
| 5770 | return ret; |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5771 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5772 | #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] | 5773 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5774 | int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl, |
| 5775 | const mbedtls_md_type_t md, |
| 5776 | unsigned char *dst, |
| 5777 | size_t dst_len, |
| 5778 | size_t *olen) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5779 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5780 | switch (md) { |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5781 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5782 | #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] | 5783 | case MBEDTLS_MD_SHA384: |
| 5784 | return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5785 | #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] | 5786 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5787 | #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] | 5788 | case MBEDTLS_MD_SHA256: |
| 5789 | return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5790 | #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] | 5791 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5792 | default: |
Andrzej Kurek | 409248a | 2022-10-24 10:33:21 -0400 | [diff] [blame] | 5793 | #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] | 5794 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 5795 | (void) ssl; |
| 5796 | (void) dst; |
| 5797 | (void) dst_len; |
| 5798 | (void) olen; |
Andrzej Kurek | 409248a | 2022-10-24 10:33:21 -0400 | [diff] [blame] | 5799 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5800 | break; |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5801 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5802 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5803 | } |
XiaokangQian | 647719a | 2021-12-07 09:16:29 +0000 | [diff] [blame] | 5804 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5805 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5806 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5807 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5808 | /* mbedtls_ssl_parse_sig_alg_ext() |
| 5809 | * |
| 5810 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 5811 | * value (TLS 1.3 RFC8446): |
| 5812 | * enum { |
| 5813 | * .... |
| 5814 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 5815 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 5816 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 5817 | * .... |
| 5818 | * } SignatureScheme; |
| 5819 | * |
| 5820 | * struct { |
| 5821 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 5822 | * } SignatureSchemeList; |
| 5823 | * |
| 5824 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 5825 | * value (TLS 1.2 RFC5246): |
| 5826 | * enum { |
| 5827 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 5828 | * sha512(6), (255) |
| 5829 | * } HashAlgorithm; |
| 5830 | * |
| 5831 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 5832 | * SignatureAlgorithm; |
| 5833 | * |
| 5834 | * struct { |
| 5835 | * HashAlgorithm hash; |
| 5836 | * SignatureAlgorithm signature; |
| 5837 | * } SignatureAndHashAlgorithm; |
| 5838 | * |
| 5839 | * SignatureAndHashAlgorithm |
| 5840 | * supported_signature_algorithms<2..2^16-2>; |
| 5841 | * |
| 5842 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 5843 | * generalization of the TLS 1.2 signature algorithm extension. |
| 5844 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 5845 | * `SignatureScheme` field of TLS 1.3 |
| 5846 | * |
| 5847 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5848 | int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl, |
| 5849 | const unsigned char *buf, |
| 5850 | const unsigned char *end) |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5851 | { |
| 5852 | const unsigned char *p = buf; |
| 5853 | size_t supported_sig_algs_len = 0; |
| 5854 | const unsigned char *supported_sig_algs_end; |
| 5855 | uint16_t sig_alg; |
| 5856 | uint32_t common_idx = 0; |
| 5857 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5858 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2); |
| 5859 | supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5860 | p += 2; |
| 5861 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5862 | memset(ssl->handshake->received_sig_algs, 0, |
| 5863 | sizeof(ssl->handshake->received_sig_algs)); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5864 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5865 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5866 | supported_sig_algs_end = p + supported_sig_algs_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5867 | while (p < supported_sig_algs_end) { |
| 5868 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2); |
| 5869 | sig_alg = MBEDTLS_GET_UINT16_BE(p, 0); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5870 | p += 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5871 | MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s", |
| 5872 | sig_alg, |
| 5873 | mbedtls_ssl_sig_alg_to_str(sig_alg))); |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5874 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5875 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 && |
| 5876 | (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) && |
| 5877 | mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5878 | continue; |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5879 | } |
| 5880 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5881 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5882 | MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s", |
| 5883 | mbedtls_ssl_sig_alg_to_str(sig_alg))); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5884 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5885 | if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5886 | ssl->handshake->received_sig_algs[common_idx] = sig_alg; |
| 5887 | common_idx += 1; |
| 5888 | } |
| 5889 | } |
| 5890 | /* Check that we consumed all the message. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5891 | if (p != end) { |
| 5892 | MBEDTLS_SSL_DEBUG_MSG(1, |
| 5893 | ("Signature algorithms extension length misaligned")); |
| 5894 | MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, |
| 5895 | MBEDTLS_ERR_SSL_DECODE_ERROR); |
| 5896 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5897 | } |
| 5898 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5899 | if (common_idx == 0) { |
| 5900 | MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common")); |
| 5901 | MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE, |
| 5902 | MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE); |
| 5903 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5904 | } |
| 5905 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5906 | ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5907 | return 0; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5908 | } |
| 5909 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5910 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5911 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5912 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5913 | |
| 5914 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5915 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5916 | static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation, |
| 5917 | mbedtls_svc_key_id_t key, |
| 5918 | psa_algorithm_t alg, |
| 5919 | const unsigned char *raw_psk, size_t raw_psk_length, |
| 5920 | const unsigned char *seed, size_t seed_length, |
| 5921 | const unsigned char *label, size_t label_length, |
| 5922 | const unsigned char *other_secret, |
| 5923 | size_t other_secret_length, |
| 5924 | size_t capacity) |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5925 | { |
| 5926 | psa_status_t status; |
| 5927 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5928 | status = psa_key_derivation_setup(derivation, alg); |
| 5929 | if (status != PSA_SUCCESS) { |
| 5930 | return status; |
| 5931 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5932 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5933 | if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 5934 | status = psa_key_derivation_input_bytes(derivation, |
| 5935 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 5936 | seed, seed_length); |
| 5937 | if (status != PSA_SUCCESS) { |
| 5938 | return status; |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5939 | } |
| 5940 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5941 | if (other_secret != NULL) { |
| 5942 | status = psa_key_derivation_input_bytes(derivation, |
| 5943 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 5944 | other_secret, other_secret_length); |
| 5945 | if (status != PSA_SUCCESS) { |
| 5946 | return status; |
| 5947 | } |
| 5948 | } |
| 5949 | |
| 5950 | if (mbedtls_svc_key_id_is_null(key)) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5951 | status = psa_key_derivation_input_bytes( |
| 5952 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5953 | raw_psk, raw_psk_length); |
| 5954 | } else { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5955 | status = psa_key_derivation_input_key( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5956 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5957 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5958 | if (status != PSA_SUCCESS) { |
| 5959 | return status; |
| 5960 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5961 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5962 | status = psa_key_derivation_input_bytes(derivation, |
| 5963 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 5964 | label, label_length); |
| 5965 | if (status != PSA_SUCCESS) { |
| 5966 | return status; |
| 5967 | } |
| 5968 | } else { |
| 5969 | return PSA_ERROR_NOT_SUPPORTED; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5970 | } |
| 5971 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5972 | status = psa_key_derivation_set_capacity(derivation, capacity); |
| 5973 | if (status != PSA_SUCCESS) { |
| 5974 | return status; |
| 5975 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5976 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5977 | return PSA_SUCCESS; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5978 | } |
| 5979 | |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 5980 | #if defined(PSA_WANT_ALG_SHA_384) || \ |
| 5981 | defined(PSA_WANT_ALG_SHA_256) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5982 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5983 | static int tls_prf_generic(mbedtls_md_type_t md_type, |
| 5984 | const unsigned char *secret, size_t slen, |
| 5985 | const char *label, |
| 5986 | const unsigned char *random, size_t rlen, |
| 5987 | unsigned char *dstbuf, size_t dlen) |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5988 | { |
| 5989 | psa_status_t status; |
| 5990 | psa_algorithm_t alg; |
| 5991 | mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5992 | psa_key_derivation_operation_t derivation = |
| 5993 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5994 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5995 | if (md_type == MBEDTLS_MD_SHA384) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5996 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5997 | } else { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5998 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5999 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6000 | |
| 6001 | /* Normally a "secret" should be long enough to be impossible to |
| 6002 | * find by brute force, and in particular should not be empty. But |
| 6003 | * this PRF is also used to derive an IV, in particular in EAP-TLS, |
| 6004 | * and for this use case it makes sense to have a 0-length "secret". |
| 6005 | * Since the key API doesn't allow importing a key of length 0, |
| 6006 | * keep master_key=0, which setup_psa_key_derivation() understands |
| 6007 | * to mean a 0-length "secret" input. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6008 | if (slen != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6009 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6010 | psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE); |
| 6011 | psa_set_key_algorithm(&key_attributes, alg); |
| 6012 | psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6013 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6014 | status = psa_import_key(&key_attributes, secret, slen, &master_key); |
| 6015 | if (status != PSA_SUCCESS) { |
| 6016 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 6017 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6018 | } |
| 6019 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6020 | status = setup_psa_key_derivation(&derivation, |
| 6021 | master_key, alg, |
| 6022 | NULL, 0, |
| 6023 | random, rlen, |
| 6024 | (unsigned char const *) label, |
| 6025 | (size_t) strlen(label), |
| 6026 | NULL, 0, |
| 6027 | dlen); |
| 6028 | if (status != PSA_SUCCESS) { |
| 6029 | psa_key_derivation_abort(&derivation); |
| 6030 | psa_destroy_key(master_key); |
| 6031 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6032 | } |
| 6033 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6034 | status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen); |
| 6035 | if (status != PSA_SUCCESS) { |
| 6036 | psa_key_derivation_abort(&derivation); |
| 6037 | psa_destroy_key(master_key); |
| 6038 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6039 | } |
| 6040 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6041 | status = psa_key_derivation_abort(&derivation); |
| 6042 | if (status != PSA_SUCCESS) { |
| 6043 | psa_destroy_key(master_key); |
| 6044 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6045 | } |
| 6046 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6047 | if (!mbedtls_svc_key_id_is_null(master_key)) { |
| 6048 | status = psa_destroy_key(master_key); |
| 6049 | } |
| 6050 | if (status != PSA_SUCCESS) { |
| 6051 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 6052 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6053 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6054 | return 0; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6055 | } |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 6056 | #endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6057 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6058 | |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 6059 | #if defined(MBEDTLS_MD_C) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6060 | (defined(MBEDTLS_SHA256_C) || \ |
| 6061 | defined(MBEDTLS_SHA384_C)) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6062 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6063 | static int tls_prf_generic(mbedtls_md_type_t md_type, |
| 6064 | const unsigned char *secret, size_t slen, |
| 6065 | const char *label, |
| 6066 | const unsigned char *random, size_t rlen, |
| 6067 | unsigned char *dstbuf, size_t dlen) |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6068 | { |
| 6069 | size_t nb; |
| 6070 | size_t i, j, k, md_len; |
| 6071 | unsigned char *tmp; |
| 6072 | size_t tmp_len = 0; |
| 6073 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 6074 | const mbedtls_md_info_t *md_info; |
| 6075 | mbedtls_md_context_t md_ctx; |
| 6076 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6077 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6078 | mbedtls_md_init(&md_ctx); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6079 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6080 | if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) { |
| 6081 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 6082 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6083 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6084 | md_len = mbedtls_md_get_size(md_info); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6085 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6086 | tmp_len = md_len + strlen(label) + rlen; |
| 6087 | tmp = mbedtls_calloc(1, tmp_len); |
| 6088 | if (tmp == NULL) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6089 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 6090 | goto exit; |
| 6091 | } |
| 6092 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6093 | nb = strlen(label); |
| 6094 | memcpy(tmp + md_len, label, nb); |
| 6095 | memcpy(tmp + md_len + nb, random, rlen); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6096 | nb += rlen; |
| 6097 | |
| 6098 | /* |
| 6099 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 6100 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6101 | if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6102 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6103 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6104 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6105 | ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen); |
| 6106 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6107 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6108 | } |
| 6109 | ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb); |
| 6110 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6111 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6112 | } |
| 6113 | ret = mbedtls_md_hmac_finish(&md_ctx, tmp); |
| 6114 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6115 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6116 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6117 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6118 | for (i = 0; i < dlen; i += md_len) { |
| 6119 | ret = mbedtls_md_hmac_reset(&md_ctx); |
| 6120 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6121 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6122 | } |
| 6123 | ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb); |
| 6124 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6125 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6126 | } |
| 6127 | ret = mbedtls_md_hmac_finish(&md_ctx, h_i); |
| 6128 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6129 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6130 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6131 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6132 | ret = mbedtls_md_hmac_reset(&md_ctx); |
| 6133 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6134 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6135 | } |
| 6136 | ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len); |
| 6137 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6138 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6139 | } |
| 6140 | ret = mbedtls_md_hmac_finish(&md_ctx, tmp); |
| 6141 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6142 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6143 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6144 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6145 | k = (i + md_len > dlen) ? dlen % md_len : md_len; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6146 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6147 | for (j = 0; j < k; j++) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6148 | dstbuf[i + j] = h_i[j]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6149 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6150 | } |
| 6151 | |
| 6152 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6153 | mbedtls_md_free(&md_ctx); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6154 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6155 | if (tmp != NULL) { |
| 6156 | mbedtls_platform_zeroize(tmp, tmp_len); |
| 6157 | } |
Dave Rodgman | 29b9b2b | 2022-11-01 16:08:14 +0000 | [diff] [blame] | 6158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6159 | mbedtls_platform_zeroize(h_i, sizeof(h_i)); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6160 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6161 | mbedtls_free(tmp); |
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 | return ret; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6164 | } |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 6165 | #endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6166 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6167 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6168 | #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] | 6169 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6170 | static int tls_prf_sha256(const unsigned char *secret, size_t slen, |
| 6171 | const char *label, |
| 6172 | const unsigned char *random, size_t rlen, |
| 6173 | unsigned char *dstbuf, size_t dlen) |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6174 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6175 | return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen, |
| 6176 | label, random, rlen, dstbuf, dlen); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6177 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 6178 | #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] | 6179 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6180 | #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] | 6181 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6182 | static int tls_prf_sha384(const unsigned char *secret, size_t slen, |
| 6183 | const char *label, |
| 6184 | const unsigned char *random, size_t rlen, |
| 6185 | unsigned char *dstbuf, size_t dlen) |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6186 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6187 | return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen, |
| 6188 | label, random, rlen, dstbuf, dlen); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6189 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 6190 | #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] | 6191 | |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6192 | /* |
| 6193 | * Set appropriate PRF function and other SSL / TLS1.2 functions |
| 6194 | * |
| 6195 | * Inputs: |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6196 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
| 6197 | * |
| 6198 | * Outputs: |
| 6199 | * - the tls_prf, calc_verify and calc_finished members of handshake structure |
| 6200 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6201 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6202 | static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake, |
| 6203 | mbedtls_md_type_t hash) |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6204 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6205 | #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] | 6206 | if (hash == MBEDTLS_MD_SHA384) { |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6207 | handshake->tls_prf = tls_prf_sha384; |
| 6208 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 6209 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6210 | } else |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6211 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6212 | #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] | 6213 | { |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 6214 | (void) hash; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6215 | handshake->tls_prf = tls_prf_sha256; |
| 6216 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 6217 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
| 6218 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 6219 | #else |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6220 | { |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6221 | (void) handshake; |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 6222 | (void) hash; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6223 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6224 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 6225 | #endif |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6226 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6227 | return 0; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6228 | } |
Jerry Yu | d6ab235 | 2022-02-17 14:03:43 +0800 | [diff] [blame] | 6229 | |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6230 | /* |
| 6231 | * Compute master secret if needed |
| 6232 | * |
| 6233 | * Parameters: |
| 6234 | * [in/out] handshake |
| 6235 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf |
| 6236 | * (PSA-PSK) ciphersuite_info, psk_opaque |
| 6237 | * [out] premaster (cleared) |
| 6238 | * [out] master |
| 6239 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK |
| 6240 | * debug: conf->f_dbg, conf->p_dbg |
| 6241 | * EMS: passed to calc_verify (debug + session_negotiate) |
Ronald Cron | a25cf58 | 2022-03-07 11:10:36 +0100 | [diff] [blame] | 6242 | * PSA-PSA: conf |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6243 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6244 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6245 | static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake, |
| 6246 | unsigned char *master, |
| 6247 | const mbedtls_ssl_context *ssl) |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6248 | { |
| 6249 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6250 | |
| 6251 | /* cf. RFC 5246, Section 8.1: |
| 6252 | * "The master secret is always exactly 48 bytes in length." */ |
| 6253 | size_t const master_secret_len = 48; |
| 6254 | |
| 6255 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 6256 | unsigned char session_hash[48]; |
| 6257 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 6258 | |
| 6259 | /* The label for the KDF used for key expansion. |
| 6260 | * This is either "master secret" or "extended master secret" |
| 6261 | * depending on whether the Extended Master Secret extension |
| 6262 | * is used. */ |
| 6263 | char const *lbl = "master secret"; |
| 6264 | |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 6265 | /* The seed for the KDF used for key expansion. |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6266 | * - If the Extended Master Secret extension is not used, |
| 6267 | * this is ClientHello.Random + ServerHello.Random |
| 6268 | * (see Sect. 8.1 in RFC 5246). |
| 6269 | * - If the Extended Master Secret extension is used, |
| 6270 | * this is the transcript of the handshake so far. |
| 6271 | * (see Sect. 4 in RFC 7627). */ |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 6272 | unsigned char const *seed = handshake->randbytes; |
| 6273 | size_t seed_len = 64; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6274 | |
| 6275 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 6276 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ |
| 6277 | !(defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6278 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6279 | ssl = NULL; /* make sure we don't use it except for those cases */ |
| 6280 | (void) ssl; |
| 6281 | #endif |
| 6282 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6283 | if (handshake->resume != 0) { |
| 6284 | MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)")); |
| 6285 | return 0; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6286 | } |
| 6287 | |
| 6288 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6289 | if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) { |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6290 | lbl = "extended master secret"; |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 6291 | seed = session_hash; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6292 | handshake->calc_verify(ssl, session_hash, &seed_len); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6293 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6294 | MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret", |
| 6295 | session_hash, seed_len); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6296 | } |
Przemek Stekiel | 169bf0b | 2022-04-29 07:53:29 +0200 | [diff] [blame] | 6297 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6298 | |
Przemek Stekiel | 99114f3 | 2022-04-22 11:20:09 +0200 | [diff] [blame] | 6299 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Przemek Stekiel | 8a4b7fd | 2022-04-28 09:22:22 +0200 | [diff] [blame] | 6300 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6301 | if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) { |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6302 | /* Perform PSK-to-MS expansion in a single step. */ |
| 6303 | psa_status_t status; |
| 6304 | psa_algorithm_t alg; |
| 6305 | mbedtls_svc_key_id_t psk; |
| 6306 | psa_key_derivation_operation_t derivation = |
| 6307 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 6308 | mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac; |
| 6309 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6310 | MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion")); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6311 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6312 | psk = mbedtls_ssl_get_opaque_psk(ssl); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6314 | if (hash_alg == MBEDTLS_MD_SHA384) { |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6315 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6316 | } else { |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6317 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6318 | } |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6319 | |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 6320 | size_t other_secret_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6321 | unsigned char *other_secret = NULL; |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 6322 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6323 | switch (handshake->ciphersuite_info->key_exchange) { |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6324 | /* Provide other secret. |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 6325 | * Other secret is stored in premaster, where first 2 bytes hold the |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6326 | * length of the other key. |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 6327 | */ |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6328 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 6329 | /* For RSA-PSK other key length is always 48 bytes. */ |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6330 | other_secret_len = 48; |
| 6331 | other_secret = handshake->premaster + 2; |
| 6332 | break; |
| 6333 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6334 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6335 | other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0); |
| 6336 | other_secret = handshake->premaster + 2; |
| 6337 | break; |
| 6338 | default: |
| 6339 | break; |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 6340 | } |
| 6341 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6342 | status = setup_psa_key_derivation(&derivation, psk, alg, |
| 6343 | ssl->conf->psk, ssl->conf->psk_len, |
| 6344 | seed, seed_len, |
| 6345 | (unsigned char const *) lbl, |
| 6346 | (size_t) strlen(lbl), |
| 6347 | other_secret, other_secret_len, |
| 6348 | master_secret_len); |
| 6349 | if (status != PSA_SUCCESS) { |
| 6350 | psa_key_derivation_abort(&derivation); |
| 6351 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6352 | } |
| 6353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6354 | status = psa_key_derivation_output_bytes(&derivation, |
| 6355 | master, |
| 6356 | master_secret_len); |
| 6357 | if (status != PSA_SUCCESS) { |
| 6358 | psa_key_derivation_abort(&derivation); |
| 6359 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6360 | } |
| 6361 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6362 | status = psa_key_derivation_abort(&derivation); |
| 6363 | if (status != PSA_SUCCESS) { |
| 6364 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 6365 | } |
| 6366 | } else |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6367 | #endif |
| 6368 | { |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 6369 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6370 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 6371 | if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) { |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 6372 | psa_status_t status; |
| 6373 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 6374 | psa_key_derivation_operation_t derivation = |
| 6375 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 6376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6377 | MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE")); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 6378 | |
| 6379 | handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE; |
| 6380 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6381 | status = psa_key_derivation_setup(&derivation, alg); |
| 6382 | if (status != PSA_SUCCESS) { |
| 6383 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 6384 | } |
| 6385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6386 | status = psa_key_derivation_set_capacity(&derivation, |
| 6387 | PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE); |
| 6388 | if (status != PSA_SUCCESS) { |
| 6389 | psa_key_derivation_abort(&derivation); |
| 6390 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 6391 | } |
| 6392 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6393 | status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx, |
| 6394 | &derivation); |
| 6395 | if (status != PSA_SUCCESS) { |
| 6396 | psa_key_derivation_abort(&derivation); |
| 6397 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 6398 | } |
| 6399 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6400 | status = psa_key_derivation_output_bytes(&derivation, |
| 6401 | handshake->premaster, |
| 6402 | handshake->pmslen); |
| 6403 | if (status != PSA_SUCCESS) { |
| 6404 | psa_key_derivation_abort(&derivation); |
| 6405 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 6406 | } |
| 6407 | |
| 6408 | status = psa_key_derivation_abort(&derivation); |
| 6409 | if (status != PSA_SUCCESS) { |
| 6410 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 6411 | } |
| 6412 | } |
| 6413 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6414 | ret = handshake->tls_prf(handshake->premaster, handshake->pmslen, |
| 6415 | lbl, seed, seed_len, |
| 6416 | master, |
| 6417 | master_secret_len); |
| 6418 | if (ret != 0) { |
| 6419 | MBEDTLS_SSL_DEBUG_RET(1, "prf", ret); |
| 6420 | return ret; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6421 | } |
| 6422 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6423 | MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret", |
| 6424 | handshake->premaster, |
| 6425 | handshake->pmslen); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6426 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6427 | mbedtls_platform_zeroize(handshake->premaster, |
| 6428 | sizeof(handshake->premaster)); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6429 | } |
| 6430 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6431 | return 0; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6432 | } |
| 6433 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6434 | int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl) |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6435 | { |
| 6436 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6437 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 6438 | ssl->handshake->ciphersuite_info; |
| 6439 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6440 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys")); |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6441 | |
| 6442 | /* Set PRF, calc_verify and calc_finished function pointers */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6443 | ret = ssl_set_handshake_prfs(ssl->handshake, |
| 6444 | ciphersuite_info->mac); |
| 6445 | if (ret != 0) { |
| 6446 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret); |
| 6447 | return ret; |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6448 | } |
| 6449 | |
| 6450 | /* Compute master secret if needed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6451 | ret = ssl_compute_master(ssl->handshake, |
| 6452 | ssl->session_negotiate->master, |
| 6453 | ssl); |
| 6454 | if (ret != 0) { |
| 6455 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret); |
| 6456 | return ret; |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6457 | } |
| 6458 | |
| 6459 | /* Swap the client and server random values: |
| 6460 | * - MS derivation wanted client+server (RFC 5246 8.1) |
| 6461 | * - key derivation wants server+client (RFC 5246 6.3) */ |
| 6462 | { |
| 6463 | unsigned char tmp[64]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6464 | memcpy(tmp, ssl->handshake->randbytes, 64); |
| 6465 | memcpy(ssl->handshake->randbytes, tmp + 32, 32); |
| 6466 | memcpy(ssl->handshake->randbytes + 32, tmp, 32); |
| 6467 | mbedtls_platform_zeroize(tmp, sizeof(tmp)); |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6468 | } |
| 6469 | |
| 6470 | /* Populate transform structure */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6471 | ret = ssl_tls12_populate_transform(ssl->transform_negotiate, |
| 6472 | ssl->session_negotiate->ciphersuite, |
| 6473 | ssl->session_negotiate->master, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 6474 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6475 | ssl->session_negotiate->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 6476 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6477 | ssl->handshake->tls_prf, |
| 6478 | ssl->handshake->randbytes, |
| 6479 | ssl->tls_version, |
| 6480 | ssl->conf->endpoint, |
| 6481 | ssl); |
| 6482 | if (ret != 0) { |
| 6483 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret); |
| 6484 | return ret; |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6485 | } |
| 6486 | |
| 6487 | /* We no longer need Server/ClientHello.random values */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6488 | mbedtls_platform_zeroize(ssl->handshake->randbytes, |
| 6489 | sizeof(ssl->handshake->randbytes)); |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6490 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6491 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys")); |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6492 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6493 | return 0; |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6494 | } |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6495 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6496 | 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] | 6497 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6498 | switch (md) { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6499 | #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] | 6500 | case MBEDTLS_SSL_HASH_SHA384: |
| 6501 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 6502 | break; |
| 6503 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6504 | #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] | 6505 | case MBEDTLS_SSL_HASH_SHA256: |
| 6506 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 6507 | break; |
| 6508 | #endif |
| 6509 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6510 | return -1; |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 6511 | } |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 6512 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 6513 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 6514 | (void) ssl; |
| 6515 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6516 | return 0; |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 6517 | } |
| 6518 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6519 | #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^] | 6520 | int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl, |
| 6521 | unsigned char *hash, |
| 6522 | size_t *hlen) |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6523 | { |
| 6524 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6525 | size_t hash_size; |
| 6526 | psa_status_t status; |
| 6527 | psa_hash_operation_t sha256_psa = psa_hash_operation_init(); |
| 6528 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6529 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha256")); |
| 6530 | status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa); |
| 6531 | if (status != PSA_SUCCESS) { |
| 6532 | MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed")); |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame^] | 6533 | return 0; |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6534 | } |
| 6535 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6536 | status = psa_hash_finish(&sha256_psa, hash, 32, &hash_size); |
| 6537 | if (status != PSA_SUCCESS) { |
| 6538 | MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed")); |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame^] | 6539 | return 0; |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6540 | } |
| 6541 | |
| 6542 | *hlen = 32; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6543 | MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen); |
| 6544 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify")); |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6545 | #else |
| 6546 | mbedtls_sha256_context sha256; |
| 6547 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6548 | mbedtls_sha256_init(&sha256); |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6549 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6550 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha256")); |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6551 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6552 | mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256); |
| 6553 | mbedtls_sha256_finish(&sha256, hash); |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6554 | |
| 6555 | *hlen = 32; |
| 6556 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6557 | MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen); |
| 6558 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify")); |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6559 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6560 | mbedtls_sha256_free(&sha256); |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6561 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame^] | 6562 | return 0; |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6563 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6564 | #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] | 6565 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6566 | #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^] | 6567 | int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl, |
| 6568 | unsigned char *hash, |
| 6569 | size_t *hlen) |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6570 | { |
| 6571 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6572 | size_t hash_size; |
| 6573 | psa_status_t status; |
| 6574 | psa_hash_operation_t sha384_psa = psa_hash_operation_init(); |
| 6575 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6576 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha384")); |
| 6577 | status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa); |
| 6578 | if (status != PSA_SUCCESS) { |
| 6579 | MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed")); |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame^] | 6580 | return 0; |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6581 | } |
| 6582 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6583 | status = psa_hash_finish(&sha384_psa, hash, 48, &hash_size); |
| 6584 | if (status != PSA_SUCCESS) { |
| 6585 | MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed")); |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame^] | 6586 | return 0; |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6587 | } |
| 6588 | |
| 6589 | *hlen = 48; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6590 | MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen); |
| 6591 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify")); |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6592 | #else |
| 6593 | mbedtls_sha512_context sha512; |
| 6594 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6595 | mbedtls_sha512_init(&sha512); |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6596 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6597 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha384")); |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6598 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6599 | mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384); |
| 6600 | mbedtls_sha512_finish(&sha512, hash); |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6601 | |
| 6602 | *hlen = 48; |
| 6603 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6604 | MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen); |
| 6605 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify")); |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6606 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6607 | mbedtls_sha512_free(&sha512); |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6608 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame^] | 6609 | return 0; |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6610 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6611 | #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] | 6612 | |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6613 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 6614 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6615 | 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] | 6616 | { |
| 6617 | unsigned char *p = ssl->handshake->premaster; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6618 | unsigned char *end = p + sizeof(ssl->handshake->premaster); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6619 | const unsigned char *psk = NULL; |
| 6620 | size_t psk_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6621 | int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6622 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6623 | if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) { |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6624 | /* |
| 6625 | * This should never happen because the existence of a PSK is always |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6626 | * checked before calling this function. |
| 6627 | * |
| 6628 | * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with |
Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 6629 | * the shared secret without PSK. |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6630 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6631 | if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) { |
| 6632 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 6633 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6634 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6635 | } |
| 6636 | |
| 6637 | /* |
| 6638 | * PMS = struct { |
| 6639 | * opaque other_secret<0..2^16-1>; |
| 6640 | * opaque psk<0..2^16-1>; |
| 6641 | * }; |
| 6642 | * with "other_secret" depending on the particular key exchange |
| 6643 | */ |
| 6644 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6645 | if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) { |
| 6646 | if (end - p < 2) { |
| 6647 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 6648 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6649 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6650 | MBEDTLS_PUT_UINT16_BE(psk_len, p, 0); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6651 | p += 2; |
| 6652 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6653 | if (end < p || (size_t) (end - p) < psk_len) { |
| 6654 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 6655 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6656 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6657 | memset(p, 0, psk_len); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6658 | p += psk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6659 | } else |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6660 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 6661 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6662 | if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) { |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6663 | /* |
| 6664 | * other_secret already set by the ClientKeyExchange message, |
| 6665 | * and is 48 bytes long |
| 6666 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6667 | if (end - p < 2) { |
| 6668 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 6669 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6670 | |
| 6671 | *p++ = 0; |
| 6672 | *p++ = 48; |
| 6673 | p += 48; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6674 | } else |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6675 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 6676 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6677 | if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) { |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6678 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6679 | size_t len; |
| 6680 | |
| 6681 | /* Write length only when we know the actual value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6682 | if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx, |
| 6683 | p + 2, end - (p + 2), &len, |
| 6684 | ssl->conf->f_rng, ssl->conf->p_rng)) != 0) { |
| 6685 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret); |
| 6686 | return ret; |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6687 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6688 | MBEDTLS_PUT_UINT16_BE(len, p, 0); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6689 | p += 2 + len; |
| 6690 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6691 | MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K); |
| 6692 | } else |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6693 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6694 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6695 | if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) { |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6696 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6697 | size_t zlen; |
| 6698 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6699 | if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen, |
| 6700 | p + 2, end - (p + 2), |
| 6701 | ssl->conf->f_rng, ssl->conf->p_rng)) != 0) { |
| 6702 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret); |
| 6703 | return ret; |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6704 | } |
| 6705 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6706 | MBEDTLS_PUT_UINT16_BE(zlen, p, 0); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6707 | p += 2 + zlen; |
| 6708 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6709 | MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx, |
| 6710 | MBEDTLS_DEBUG_ECDH_Z); |
| 6711 | } else |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6712 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6713 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6714 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 6715 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6716 | } |
| 6717 | |
| 6718 | /* opaque psk<0..2^16-1>; */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6719 | if (end - p < 2) { |
| 6720 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 6721 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6722 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6723 | MBEDTLS_PUT_UINT16_BE(psk_len, p, 0); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6724 | p += 2; |
| 6725 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6726 | if (end < p || (size_t) (end - p) < psk_len) { |
| 6727 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 6728 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6729 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6730 | memcpy(p, psk, psk_len); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6731 | p += psk_len; |
| 6732 | |
| 6733 | ssl->handshake->pmslen = p - ssl->handshake->premaster; |
| 6734 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6735 | return 0; |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6736 | } |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6737 | #endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6738 | |
| 6739 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6740 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6741 | static int ssl_write_hello_request(mbedtls_ssl_context *ssl); |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6742 | |
| 6743 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6744 | int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl) |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6745 | { |
| 6746 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 6747 | * timeout if we were using the usual handshake doubling scheme */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6748 | if (ssl->conf->renego_max_records < 0) { |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6749 | uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1; |
| 6750 | unsigned char doublings = 1; |
| 6751 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6752 | while (ratio != 0) { |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6753 | ++doublings; |
| 6754 | ratio >>= 1; |
| 6755 | } |
| 6756 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6757 | if (++ssl->renego_records_seen > doublings) { |
| 6758 | MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request")); |
| 6759 | return 0; |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6760 | } |
| 6761 | } |
| 6762 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6763 | return ssl_write_hello_request(ssl); |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6764 | } |
| 6765 | #endif |
| 6766 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6767 | |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6768 | /* |
| 6769 | * Handshake functions |
| 6770 | */ |
| 6771 | #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 6772 | /* No certificate support -> dummy functions */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6773 | int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6774 | { |
| 6775 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6776 | ssl->handshake->ciphersuite_info; |
| 6777 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6778 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6779 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6780 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { |
| 6781 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6782 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6783 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6784 | } |
| 6785 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6786 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 6787 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6788 | } |
| 6789 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6790 | int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6791 | { |
| 6792 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6793 | ssl->handshake->ciphersuite_info; |
| 6794 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6795 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6796 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6797 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { |
| 6798 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6799 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6800 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6801 | } |
| 6802 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6803 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 6804 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6805 | } |
| 6806 | |
| 6807 | #else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 6808 | /* Some certificate support -> implement write and parse */ |
| 6809 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6810 | int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6811 | { |
| 6812 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 6813 | size_t i, n; |
| 6814 | const mbedtls_x509_crt *crt; |
| 6815 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6816 | ssl->handshake->ciphersuite_info; |
| 6817 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6818 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6819 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6820 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { |
| 6821 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6822 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6823 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6824 | } |
| 6825 | |
| 6826 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6827 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 6828 | if (ssl->handshake->client_auth == 0) { |
| 6829 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6830 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6831 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6832 | } |
| 6833 | } |
| 6834 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 6835 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6836 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 6837 | if (mbedtls_ssl_own_cert(ssl) == NULL) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6838 | /* Should never happen because we shouldn't have picked the |
| 6839 | * ciphersuite if we don't have a certificate. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6840 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6841 | } |
| 6842 | } |
| 6843 | #endif |
| 6844 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6845 | MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl)); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6846 | |
| 6847 | /* |
| 6848 | * 0 . 0 handshake type |
| 6849 | * 1 . 3 handshake length |
| 6850 | * 4 . 6 length of all certs |
| 6851 | * 7 . 9 length of cert. 1 |
| 6852 | * 10 . n-1 peer certificate |
| 6853 | * n . n+2 length of cert. 2 |
| 6854 | * n+3 . ... upper level cert, etc. |
| 6855 | */ |
| 6856 | i = 7; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6857 | crt = mbedtls_ssl_own_cert(ssl); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6858 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6859 | while (crt != NULL) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6860 | n = crt->raw.len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6861 | if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) { |
| 6862 | MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET |
| 6863 | " > %" MBEDTLS_PRINTF_SIZET, |
| 6864 | i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN)); |
| 6865 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6866 | } |
| 6867 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6868 | ssl->out_msg[i] = MBEDTLS_BYTE_2(n); |
| 6869 | ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n); |
| 6870 | ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6871 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6872 | i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6873 | i += n; crt = crt->next; |
| 6874 | } |
| 6875 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6876 | ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7); |
| 6877 | ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7); |
| 6878 | ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6879 | |
| 6880 | ssl->out_msglen = i; |
| 6881 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 6882 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
| 6883 | |
| 6884 | ssl->state++; |
| 6885 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6886 | if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { |
| 6887 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret); |
| 6888 | return ret; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6889 | } |
| 6890 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6891 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6892 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6893 | return ret; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6894 | } |
| 6895 | |
| 6896 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6897 | |
| 6898 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6899 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6900 | static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl, |
| 6901 | unsigned char *crt_buf, |
| 6902 | size_t crt_buf_len) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6903 | { |
| 6904 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; |
| 6905 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6906 | if (peer_crt == NULL) { |
| 6907 | return -1; |
| 6908 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6909 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6910 | if (peer_crt->raw.len != crt_buf_len) { |
| 6911 | return -1; |
| 6912 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6913 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6914 | return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6915 | } |
| 6916 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6917 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6918 | static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl, |
| 6919 | unsigned char *crt_buf, |
| 6920 | size_t crt_buf_len) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6921 | { |
| 6922 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6923 | unsigned char const * const peer_cert_digest = |
| 6924 | ssl->session->peer_cert_digest; |
| 6925 | mbedtls_md_type_t const peer_cert_digest_type = |
| 6926 | ssl->session->peer_cert_digest_type; |
| 6927 | mbedtls_md_info_t const * const digest_info = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6928 | mbedtls_md_info_from_type(peer_cert_digest_type); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6929 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; |
| 6930 | size_t digest_len; |
| 6931 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6932 | if (peer_cert_digest == NULL || digest_info == NULL) { |
| 6933 | return -1; |
| 6934 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6935 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6936 | digest_len = mbedtls_md_get_size(digest_info); |
| 6937 | if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) { |
| 6938 | return -1; |
| 6939 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6940 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6941 | ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest); |
| 6942 | if (ret != 0) { |
| 6943 | return -1; |
| 6944 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6945 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6946 | return memcmp(tmp_digest, peer_cert_digest, digest_len); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6947 | } |
| 6948 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6949 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 6950 | |
| 6951 | /* |
| 6952 | * Once the certificate message is read, parse it into a cert chain and |
| 6953 | * perform basic checks, but leave actual verification to the caller |
| 6954 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6955 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6956 | static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl, |
| 6957 | mbedtls_x509_crt *chain) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6958 | { |
| 6959 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6960 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6961 | int crt_cnt = 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6962 | #endif |
| 6963 | size_t i, n; |
| 6964 | uint8_t alert; |
| 6965 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6966 | if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) { |
| 6967 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 6968 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6969 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
| 6970 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6971 | } |
| 6972 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6973 | if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) { |
| 6974 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6975 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
| 6976 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6977 | } |
| 6978 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6979 | if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) { |
| 6980 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 6981 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6982 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
| 6983 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6984 | } |
| 6985 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6986 | i = mbedtls_ssl_hs_hdr_len(ssl); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6987 | |
| 6988 | /* |
| 6989 | * Same message structure as in mbedtls_ssl_write_certificate() |
| 6990 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6991 | n = (ssl->in_msg[i+1] << 8) | ssl->in_msg[i+2]; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6992 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6993 | if (ssl->in_msg[i] != 0 || |
| 6994 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) { |
| 6995 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 6996 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6997 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
| 6998 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6999 | } |
| 7000 | |
| 7001 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 7002 | i += 3; |
| 7003 | |
| 7004 | /* Iterate through and parse the CRTs in the provided chain. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7005 | while (i < ssl->in_hslen) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7006 | /* Check that there's room for the next CRT's length fields. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7007 | if (i + 3 > ssl->in_hslen) { |
| 7008 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7009 | mbedtls_ssl_send_alert_message(ssl, |
| 7010 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7011 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
| 7012 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7013 | } |
| 7014 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 7015 | * anything beyond 2**16 ~ 64K. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7016 | if (ssl->in_msg[i] != 0) { |
| 7017 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7018 | mbedtls_ssl_send_alert_message(ssl, |
| 7019 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7020 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT); |
| 7021 | return MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7022 | } |
| 7023 | |
| 7024 | /* Read length of the next CRT in the chain. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7025 | n = ((unsigned int) ssl->in_msg[i + 1] << 8) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7026 | | (unsigned int) ssl->in_msg[i + 2]; |
| 7027 | i += 3; |
| 7028 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7029 | if (n < 128 || i + n > ssl->in_hslen) { |
| 7030 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7031 | mbedtls_ssl_send_alert_message(ssl, |
| 7032 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7033 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
| 7034 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7035 | } |
| 7036 | |
| 7037 | /* Check if we're handling the first CRT in the chain. */ |
| 7038 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7039 | if (crt_cnt++ == 0 && |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7040 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7041 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7042 | /* During client-side renegotiation, check that the server's |
| 7043 | * end-CRTs hasn't changed compared to the initial handshake, |
| 7044 | * mitigating the triple handshake attack. On success, reuse |
| 7045 | * the original end-CRT instead of parsing it again. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7046 | MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation")); |
| 7047 | if (ssl_check_peer_crt_unchanged(ssl, |
| 7048 | &ssl->in_msg[i], |
| 7049 | n) != 0) { |
| 7050 | MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation")); |
| 7051 | mbedtls_ssl_send_alert_message(ssl, |
| 7052 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7053 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED); |
| 7054 | return MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7055 | } |
| 7056 | |
| 7057 | /* Now we can safely free the original chain. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7058 | ssl_clear_peer_cert(ssl->session); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7059 | } |
| 7060 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 7061 | |
| 7062 | /* Parse the next certificate in the chain. */ |
| 7063 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7064 | ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7065 | #else |
| 7066 | /* If we don't need to store the CRT chain permanently, parse |
| 7067 | * it in-place from the input buffer instead of making a copy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7068 | 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] | 7069 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7070 | switch (ret) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7071 | case 0: /*ok*/ |
| 7072 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 7073 | /* Ignore certificate with an unknown algorithm: maybe a |
| 7074 | prior certificate was already trusted. */ |
| 7075 | break; |
| 7076 | |
| 7077 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 7078 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 7079 | goto crt_parse_der_failed; |
| 7080 | |
| 7081 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 7082 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7083 | goto crt_parse_der_failed; |
| 7084 | |
| 7085 | default: |
| 7086 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7087 | crt_parse_der_failed: |
| 7088 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert); |
| 7089 | MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret); |
| 7090 | return ret; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7091 | } |
| 7092 | |
| 7093 | i += n; |
| 7094 | } |
| 7095 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7096 | MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain); |
| 7097 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7098 | } |
| 7099 | |
| 7100 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7101 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7102 | 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] | 7103 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7104 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 7105 | return -1; |
| 7106 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7107 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7108 | if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) && |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7109 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 7110 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7111 | memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) { |
| 7112 | MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate")); |
| 7113 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7114 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7115 | return -1; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7116 | } |
| 7117 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7118 | |
| 7119 | /* Check if a certificate message is expected. |
| 7120 | * Return either |
| 7121 | * - SSL_CERTIFICATE_EXPECTED, or |
| 7122 | * - SSL_CERTIFICATE_SKIP |
| 7123 | * indicating whether a Certificate message is expected or not. |
| 7124 | */ |
| 7125 | #define SSL_CERTIFICATE_EXPECTED 0 |
| 7126 | #define SSL_CERTIFICATE_SKIP 1 |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7127 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7128 | static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl, |
| 7129 | int authmode) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7130 | { |
| 7131 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 7132 | ssl->handshake->ciphersuite_info; |
| 7133 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7134 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { |
| 7135 | return SSL_CERTIFICATE_SKIP; |
| 7136 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7137 | |
| 7138 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7139 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 7140 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) { |
| 7141 | return SSL_CERTIFICATE_SKIP; |
| 7142 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7143 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7144 | if (authmode == MBEDTLS_SSL_VERIFY_NONE) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7145 | ssl->session_negotiate->verify_result = |
| 7146 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7147 | return SSL_CERTIFICATE_SKIP; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7148 | } |
| 7149 | } |
| 7150 | #else |
| 7151 | ((void) authmode); |
| 7152 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7153 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7154 | return SSL_CERTIFICATE_EXPECTED; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7155 | } |
| 7156 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7157 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7158 | static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl, |
| 7159 | int authmode, |
| 7160 | mbedtls_x509_crt *chain, |
| 7161 | void *rs_ctx) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7162 | { |
| 7163 | int ret = 0; |
| 7164 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 7165 | ssl->handshake->ciphersuite_info; |
| 7166 | int have_ca_chain = 0; |
| 7167 | |
| 7168 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); |
| 7169 | void *p_vrfy; |
| 7170 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7171 | if (authmode == MBEDTLS_SSL_VERIFY_NONE) { |
| 7172 | return 0; |
| 7173 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7174 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7175 | if (ssl->f_vrfy != NULL) { |
| 7176 | MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7177 | f_vrfy = ssl->f_vrfy; |
| 7178 | p_vrfy = ssl->p_vrfy; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7179 | } else { |
| 7180 | MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7181 | f_vrfy = ssl->conf->f_vrfy; |
| 7182 | p_vrfy = ssl->conf->p_vrfy; |
| 7183 | } |
| 7184 | |
| 7185 | /* |
| 7186 | * Main check: verify certificate |
| 7187 | */ |
| 7188 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7189 | if (ssl->conf->f_ca_cb != NULL) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7190 | ((void) rs_ctx); |
| 7191 | have_ca_chain = 1; |
| 7192 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7193 | 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] | 7194 | ret = mbedtls_x509_crt_verify_with_ca_cb( |
| 7195 | chain, |
| 7196 | ssl->conf->f_ca_cb, |
| 7197 | ssl->conf->p_ca_cb, |
| 7198 | ssl->conf->cert_profile, |
| 7199 | ssl->hostname, |
| 7200 | &ssl->session_negotiate->verify_result, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7201 | f_vrfy, p_vrfy); |
| 7202 | } else |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7203 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 7204 | { |
| 7205 | mbedtls_x509_crt *ca_chain; |
| 7206 | mbedtls_x509_crl *ca_crl; |
| 7207 | |
| 7208 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7209 | if (ssl->handshake->sni_ca_chain != NULL) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7210 | ca_chain = ssl->handshake->sni_ca_chain; |
| 7211 | ca_crl = ssl->handshake->sni_ca_crl; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7212 | } else |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7213 | #endif |
| 7214 | { |
| 7215 | ca_chain = ssl->conf->ca_chain; |
| 7216 | ca_crl = ssl->conf->ca_crl; |
| 7217 | } |
| 7218 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7219 | if (ca_chain != NULL) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7220 | have_ca_chain = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7221 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7222 | |
| 7223 | ret = mbedtls_x509_crt_verify_restartable( |
| 7224 | chain, |
| 7225 | ca_chain, ca_crl, |
| 7226 | ssl->conf->cert_profile, |
| 7227 | ssl->hostname, |
| 7228 | &ssl->session_negotiate->verify_result, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7229 | f_vrfy, p_vrfy, rs_ctx); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7230 | } |
| 7231 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7232 | if (ret != 0) { |
| 7233 | MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7234 | } |
| 7235 | |
| 7236 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7237 | if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) { |
| 7238 | return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; |
| 7239 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7240 | #endif |
| 7241 | |
| 7242 | /* |
| 7243 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 7244 | */ |
| 7245 | |
| 7246 | #if defined(MBEDTLS_ECP_C) |
| 7247 | { |
| 7248 | const mbedtls_pk_context *pk = &chain->pk; |
| 7249 | |
Manuel Pégourié-Gonnard | 66b0d61 | 2022-06-17 10:49:29 +0200 | [diff] [blame] | 7250 | /* If certificate uses an EC key, make sure the curve is OK. |
| 7251 | * This is a public key, so it can't be opaque, so can_do() is a good |
| 7252 | * enough check to ensure pk_ec() is safe to use here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7253 | if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY)) { |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 7254 | /* and in the unlikely case the above assumption no longer holds |
| 7255 | * we are making sure that pk_ec() here does not return a NULL |
| 7256 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7257 | const mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*pk); |
| 7258 | if (ec == NULL) { |
| 7259 | MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_pk_ec() returned NULL")); |
| 7260 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 7261 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7263 | if (mbedtls_ssl_check_curve(ssl, ec->grp.id) != 0) { |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 7264 | ssl->session_negotiate->verify_result |= |
| 7265 | MBEDTLS_X509_BADCERT_BAD_KEY; |
| 7266 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7267 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)")); |
| 7268 | if (ret == 0) { |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 7269 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7270 | } |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 7271 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7272 | } |
| 7273 | } |
| 7274 | #endif /* MBEDTLS_ECP_C */ |
| 7275 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7276 | if (mbedtls_ssl_check_cert_usage(chain, |
| 7277 | ciphersuite_info, |
| 7278 | !ssl->conf->endpoint, |
| 7279 | &ssl->session_negotiate->verify_result) != 0) { |
| 7280 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)")); |
| 7281 | if (ret == 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7282 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7283 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7284 | } |
| 7285 | |
| 7286 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a |
| 7287 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, |
| 7288 | * with details encoded in the verification flags. All other kinds |
| 7289 | * of error codes, including those from the user provided f_vrfy |
| 7290 | * functions, are treated as fatal and lead to a failure of |
| 7291 | * ssl_parse_certificate even if verification was optional. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7292 | if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
| 7293 | (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
| 7294 | ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7295 | ret = 0; |
| 7296 | } |
| 7297 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7298 | if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) { |
| 7299 | MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7300 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
| 7301 | } |
| 7302 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7303 | if (ret != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7304 | uint8_t alert; |
| 7305 | |
| 7306 | /* The certificate may have been rejected for several reasons. |
| 7307 | Pick one and send the corresponding alert. Which alert to send |
| 7308 | may be a subject of debate in some cases. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7309 | if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7310 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7311 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7312 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7313 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7314 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7315 | } 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] | 7316 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7317 | } 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] | 7318 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7319 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7320 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7321 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7322 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7323 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7324 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7325 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7326 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7327 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7328 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7329 | } else { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7330 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7331 | } |
| 7332 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7333 | alert); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7334 | } |
| 7335 | |
| 7336 | #if defined(MBEDTLS_DEBUG_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7337 | if (ssl->session_negotiate->verify_result != 0) { |
| 7338 | MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x", |
| 7339 | (unsigned int) ssl->session_negotiate->verify_result)); |
| 7340 | } else { |
| 7341 | MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7342 | } |
| 7343 | #endif /* MBEDTLS_DEBUG_C */ |
| 7344 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7345 | return ret; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7346 | } |
| 7347 | |
| 7348 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7349 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7350 | static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl, |
| 7351 | unsigned char *start, size_t len) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7352 | { |
| 7353 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7354 | /* Remember digest of the peer's end-CRT. */ |
| 7355 | ssl->session_negotiate->peer_cert_digest = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7356 | mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN); |
| 7357 | if (ssl->session_negotiate->peer_cert_digest == NULL) { |
| 7358 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed", |
| 7359 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN)); |
| 7360 | mbedtls_ssl_send_alert_message(ssl, |
| 7361 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7362 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7363 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7364 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7365 | } |
| 7366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7367 | ret = mbedtls_md(mbedtls_md_info_from_type( |
| 7368 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE), |
| 7369 | start, len, |
| 7370 | ssl->session_negotiate->peer_cert_digest); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7371 | |
| 7372 | ssl->session_negotiate->peer_cert_digest_type = |
| 7373 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 7374 | ssl->session_negotiate->peer_cert_digest_len = |
| 7375 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 7376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7377 | return ret; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7378 | } |
| 7379 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7380 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7381 | static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl, |
| 7382 | unsigned char *start, size_t len) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7383 | { |
| 7384 | unsigned char *end = start + len; |
| 7385 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7386 | |
| 7387 | /* Make a copy of the peer's raw public key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7388 | mbedtls_pk_init(&ssl->handshake->peer_pubkey); |
| 7389 | ret = mbedtls_pk_parse_subpubkey(&start, end, |
| 7390 | &ssl->handshake->peer_pubkey); |
| 7391 | if (ret != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7392 | /* We should have parsed the public key before. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7393 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7394 | } |
| 7395 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7396 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7397 | } |
| 7398 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7399 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7400 | int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7401 | { |
| 7402 | int ret = 0; |
| 7403 | int crt_expected; |
| 7404 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 7405 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 7406 | ? ssl->handshake->sni_authmode |
| 7407 | : ssl->conf->authmode; |
| 7408 | #else |
| 7409 | const int authmode = ssl->conf->authmode; |
| 7410 | #endif |
| 7411 | void *rs_ctx = NULL; |
| 7412 | mbedtls_x509_crt *chain = NULL; |
| 7413 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7414 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7415 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7416 | crt_expected = ssl_parse_certificate_coordinate(ssl, authmode); |
| 7417 | if (crt_expected == SSL_CERTIFICATE_SKIP) { |
| 7418 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7419 | goto exit; |
| 7420 | } |
| 7421 | |
| 7422 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7423 | if (ssl->handshake->ecrs_enabled && |
| 7424 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7425 | chain = ssl->handshake->ecrs_peer_cert; |
| 7426 | ssl->handshake->ecrs_peer_cert = NULL; |
| 7427 | goto crt_verify; |
| 7428 | } |
| 7429 | #endif |
| 7430 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7431 | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7432 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 7433 | let it decide whether to alert. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7434 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7435 | goto exit; |
| 7436 | } |
| 7437 | |
| 7438 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7439 | if (ssl_srv_check_client_no_crt_notification(ssl) == 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7440 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
| 7441 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7442 | if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7443 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7444 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7445 | |
| 7446 | goto exit; |
| 7447 | } |
| 7448 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7449 | |
| 7450 | /* Clear existing peer CRT structure in case we tried to |
| 7451 | * reuse a session but it failed, and allocate a new one. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7452 | ssl_clear_peer_cert(ssl->session_negotiate); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7453 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7454 | chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt)); |
| 7455 | if (chain == NULL) { |
| 7456 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", |
| 7457 | sizeof(mbedtls_x509_crt))); |
| 7458 | mbedtls_ssl_send_alert_message(ssl, |
| 7459 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7460 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7461 | |
| 7462 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 7463 | goto exit; |
| 7464 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7465 | mbedtls_x509_crt_init(chain); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7466 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7467 | ret = ssl_parse_certificate_chain(ssl, chain); |
| 7468 | if (ret != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7469 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7470 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7471 | |
| 7472 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7473 | if (ssl->handshake->ecrs_enabled) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7474 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7475 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7476 | |
| 7477 | crt_verify: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7478 | if (ssl->handshake->ecrs_enabled) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7479 | rs_ctx = &ssl->handshake->ecrs_ctx; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7480 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7481 | #endif |
| 7482 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7483 | ret = ssl_parse_certificate_verify(ssl, authmode, |
| 7484 | chain, rs_ctx); |
| 7485 | if (ret != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7486 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7487 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7488 | |
| 7489 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7490 | { |
| 7491 | unsigned char *crt_start, *pk_start; |
| 7492 | size_t crt_len, pk_len; |
| 7493 | |
| 7494 | /* We parse the CRT chain without copying, so |
| 7495 | * these pointers point into the input buffer, |
| 7496 | * and are hence still valid after freeing the |
| 7497 | * CRT chain. */ |
| 7498 | |
| 7499 | crt_start = chain->raw.p; |
| 7500 | crt_len = chain->raw.len; |
| 7501 | |
| 7502 | pk_start = chain->pk_raw.p; |
| 7503 | pk_len = chain->pk_raw.len; |
| 7504 | |
| 7505 | /* Free the CRT structures before computing |
| 7506 | * digest and copying the peer's public key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7507 | mbedtls_x509_crt_free(chain); |
| 7508 | mbedtls_free(chain); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7509 | chain = NULL; |
| 7510 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7511 | ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len); |
| 7512 | if (ret != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7513 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7514 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7515 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7516 | ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len); |
| 7517 | if (ret != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7518 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7519 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7520 | } |
| 7521 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7522 | /* Pass ownership to session structure. */ |
| 7523 | ssl->session_negotiate->peer_cert = chain; |
| 7524 | chain = NULL; |
| 7525 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7526 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7527 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7528 | |
| 7529 | exit: |
| 7530 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7531 | if (ret == 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7532 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7533 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7534 | |
| 7535 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7536 | if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7537 | ssl->handshake->ecrs_peer_cert = chain; |
| 7538 | chain = NULL; |
| 7539 | } |
| 7540 | #endif |
| 7541 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7542 | if (chain != NULL) { |
| 7543 | mbedtls_x509_crt_free(chain); |
| 7544 | mbedtls_free(chain); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7545 | } |
| 7546 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7547 | return ret; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7548 | } |
| 7549 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 7550 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7551 | #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^] | 7552 | static int ssl_calc_finished_tls_sha256( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7553 | mbedtls_ssl_context *ssl, unsigned char *buf, int from) |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7554 | { |
| 7555 | int len = 12; |
| 7556 | const char *sender; |
| 7557 | unsigned char padbuf[32]; |
| 7558 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7559 | size_t hash_size; |
| 7560 | psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT; |
| 7561 | psa_status_t status; |
| 7562 | #else |
| 7563 | mbedtls_sha256_context sha256; |
| 7564 | #endif |
| 7565 | |
| 7566 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7567 | if (!session) { |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7568 | session = ssl->session; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7569 | } |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7570 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7571 | sender = (from == MBEDTLS_SSL_IS_CLIENT) |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7572 | ? "client finished" |
| 7573 | : "server finished"; |
| 7574 | |
| 7575 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7576 | sha256_psa = psa_hash_operation_init(); |
| 7577 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7578 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha256")); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7579 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7580 | status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa); |
| 7581 | if (status != PSA_SUCCESS) { |
| 7582 | MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed")); |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame^] | 7583 | return 0; |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7584 | } |
| 7585 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7586 | status = psa_hash_finish(&sha256_psa, padbuf, sizeof(padbuf), &hash_size); |
| 7587 | if (status != PSA_SUCCESS) { |
| 7588 | MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed")); |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame^] | 7589 | return 0; |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7590 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7591 | MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 32); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7592 | #else |
| 7593 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7594 | mbedtls_sha256_init(&sha256); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7595 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7596 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha256")); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7597 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7598 | mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7599 | |
| 7600 | /* |
| 7601 | * TLSv1.2: |
| 7602 | * hash = PRF( master, finished_label, |
| 7603 | * Hash( handshake ) )[0.11] |
| 7604 | */ |
| 7605 | |
| 7606 | #if !defined(MBEDTLS_SHA256_ALT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7607 | MBEDTLS_SSL_DEBUG_BUF(4, "finished sha2 state", (unsigned char *) |
| 7608 | sha256.state, sizeof(sha256.state)); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7609 | #endif |
| 7610 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7611 | mbedtls_sha256_finish(&sha256, padbuf); |
| 7612 | mbedtls_sha256_free(&sha256); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7613 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7614 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7615 | ssl->handshake->tls_prf(session->master, 48, sender, |
| 7616 | padbuf, 32, buf, len); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7617 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7618 | MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7619 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7620 | mbedtls_platform_zeroize(padbuf, sizeof(padbuf)); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7621 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7622 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished")); |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame^] | 7623 | return 0; |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7624 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 7625 | #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] | 7626 | |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7627 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7628 | #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^] | 7629 | static int ssl_calc_finished_tls_sha384( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7630 | mbedtls_ssl_context *ssl, unsigned char *buf, int from) |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7631 | { |
| 7632 | int len = 12; |
| 7633 | const char *sender; |
| 7634 | unsigned char padbuf[48]; |
| 7635 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7636 | size_t hash_size; |
| 7637 | psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT; |
| 7638 | psa_status_t status; |
| 7639 | #else |
| 7640 | mbedtls_sha512_context sha512; |
| 7641 | #endif |
| 7642 | |
| 7643 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7644 | if (!session) { |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7645 | session = ssl->session; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7646 | } |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7647 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7648 | sender = (from == MBEDTLS_SSL_IS_CLIENT) |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7649 | ? "client finished" |
| 7650 | : "server finished"; |
| 7651 | |
| 7652 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7653 | sha384_psa = psa_hash_operation_init(); |
| 7654 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7655 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha384")); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7656 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7657 | status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa); |
| 7658 | if (status != PSA_SUCCESS) { |
| 7659 | MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed")); |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame^] | 7660 | return 0; |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7661 | } |
| 7662 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7663 | status = psa_hash_finish(&sha384_psa, padbuf, sizeof(padbuf), &hash_size); |
| 7664 | if (status != PSA_SUCCESS) { |
| 7665 | MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed")); |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame^] | 7666 | return 0; |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7667 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7668 | MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 48); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7669 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7670 | mbedtls_sha512_init(&sha512); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7671 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7672 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha384")); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7673 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7674 | mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7675 | |
| 7676 | /* |
| 7677 | * TLSv1.2: |
| 7678 | * hash = PRF( master, finished_label, |
| 7679 | * Hash( handshake ) )[0.11] |
| 7680 | */ |
| 7681 | |
| 7682 | #if !defined(MBEDTLS_SHA512_ALT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7683 | MBEDTLS_SSL_DEBUG_BUF(4, "finished sha512 state", (unsigned char *) |
| 7684 | sha512.state, sizeof(sha512.state)); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7685 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7686 | mbedtls_sha512_finish(&sha512, padbuf); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7687 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7688 | mbedtls_sha512_free(&sha512); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7689 | #endif |
| 7690 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7691 | ssl->handshake->tls_prf(session->master, 48, sender, |
| 7692 | padbuf, 48, buf, len); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7693 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7694 | MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7695 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7696 | mbedtls_platform_zeroize(padbuf, sizeof(padbuf)); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7697 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7698 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished")); |
Manuel Pégourié-Gonnard | 226aa15 | 2023-02-05 09:46:59 +0100 | [diff] [blame^] | 7699 | return 0; |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7700 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 7701 | #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] | 7702 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7703 | void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl) |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7704 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7705 | MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free")); |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7706 | |
| 7707 | /* |
| 7708 | * Free our handshake params |
| 7709 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7710 | mbedtls_ssl_handshake_free(ssl); |
| 7711 | mbedtls_free(ssl->handshake); |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7712 | ssl->handshake = NULL; |
| 7713 | |
| 7714 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 7715 | * Free the previous transform and switch in the current one |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7716 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7717 | if (ssl->transform) { |
| 7718 | mbedtls_ssl_transform_free(ssl->transform); |
| 7719 | mbedtls_free(ssl->transform); |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7720 | } |
| 7721 | ssl->transform = ssl->transform_negotiate; |
| 7722 | ssl->transform_negotiate = NULL; |
| 7723 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7724 | MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free")); |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7725 | } |
| 7726 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7727 | void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl) |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7728 | { |
| 7729 | int resume = ssl->handshake->resume; |
| 7730 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7731 | MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup")); |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7732 | |
| 7733 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7734 | if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) { |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7735 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
| 7736 | ssl->renego_records_seen = 0; |
| 7737 | } |
| 7738 | #endif |
| 7739 | |
| 7740 | /* |
| 7741 | * Free the previous session and switch in the current one |
| 7742 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7743 | if (ssl->session) { |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7744 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 7745 | /* RFC 7366 3.1: keep the EtM state */ |
| 7746 | ssl->session_negotiate->encrypt_then_mac = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7747 | ssl->session->encrypt_then_mac; |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7748 | #endif |
| 7749 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7750 | mbedtls_ssl_session_free(ssl->session); |
| 7751 | mbedtls_free(ssl->session); |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7752 | } |
| 7753 | ssl->session = ssl->session_negotiate; |
| 7754 | ssl->session_negotiate = NULL; |
| 7755 | |
| 7756 | /* |
| 7757 | * Add cache entry |
| 7758 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7759 | if (ssl->conf->f_set_cache != NULL && |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7760 | ssl->session->id_len != 0 && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7761 | resume == 0) { |
| 7762 | if (ssl->conf->f_set_cache(ssl->conf->p_cache, |
| 7763 | ssl->session->id, |
| 7764 | ssl->session->id_len, |
| 7765 | ssl->session) != 0) { |
| 7766 | MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session")); |
| 7767 | } |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7768 | } |
| 7769 | |
| 7770 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7771 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 7772 | ssl->handshake->flight != NULL) { |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7773 | /* Cancel handshake timer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7774 | mbedtls_ssl_set_timer(ssl, 0); |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7775 | |
| 7776 | /* Keep last flight around in case we need to resend it: |
| 7777 | * we need the handshake and transform structures for that */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7778 | MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform")); |
| 7779 | } else |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7780 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7781 | mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl); |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7782 | |
Jerry Yu | 5ed73ff | 2022-10-27 13:08:42 +0800 | [diff] [blame] | 7783 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7784 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7785 | MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup")); |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7786 | } |
| 7787 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7788 | int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl) |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7789 | { |
| 7790 | int ret, hash_len; |
| 7791 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7792 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished")); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7793 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7794 | mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7795 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7796 | ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7797 | |
| 7798 | /* |
| 7799 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 7800 | * may define some other value. Currently (early 2016), no defined |
| 7801 | * ciphersuite does this (and this is unlikely to change as activity has |
| 7802 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 7803 | */ |
| 7804 | hash_len = 12; |
| 7805 | |
| 7806 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7807 | ssl->verify_data_len = hash_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7808 | memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7809 | #endif |
| 7810 | |
| 7811 | ssl->out_msglen = 4 + hash_len; |
| 7812 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 7813 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
| 7814 | |
| 7815 | /* |
| 7816 | * In case of session resuming, invert the client and server |
| 7817 | * ChangeCipherSpec messages order. |
| 7818 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7819 | if (ssl->handshake->resume != 0) { |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7820 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7821 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7822 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7823 | } |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7824 | #endif |
| 7825 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7826 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7827 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7828 | } |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7829 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7830 | } else { |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7831 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7832 | } |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7833 | |
| 7834 | /* |
| 7835 | * Switch to our negotiated transform and session parameters for outbound |
| 7836 | * data. |
| 7837 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7838 | 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] | 7839 | |
| 7840 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7841 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7842 | unsigned char i; |
| 7843 | |
| 7844 | /* Remember current epoch settings for resending */ |
| 7845 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7846 | memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, |
| 7847 | sizeof(ssl->handshake->alt_out_ctr)); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7848 | |
| 7849 | /* Set sequence_number to zero */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7850 | 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] | 7851 | |
| 7852 | |
| 7853 | /* Increment epoch */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7854 | for (i = 2; i > 0; i--) { |
| 7855 | if (++ssl->cur_out_ctr[i - 1] != 0) { |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7856 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7857 | } |
| 7858 | } |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7859 | |
| 7860 | /* The loop goes to its end iff the counter is wrapping */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7861 | if (i == 0) { |
| 7862 | MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap")); |
| 7863 | return MBEDTLS_ERR_SSL_COUNTER_WRAPPING; |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7864 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7865 | } else |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7866 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7867 | memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr)); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7868 | |
| 7869 | ssl->transform_out = ssl->transform_negotiate; |
| 7870 | ssl->session_out = ssl->session_negotiate; |
| 7871 | |
| 7872 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7873 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 7874 | mbedtls_ssl_send_flight_completed(ssl); |
| 7875 | } |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7876 | #endif |
| 7877 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7878 | if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { |
| 7879 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret); |
| 7880 | return ret; |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7881 | } |
| 7882 | |
| 7883 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7884 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 7885 | (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) { |
| 7886 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret); |
| 7887 | return ret; |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7888 | } |
| 7889 | #endif |
| 7890 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7891 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished")); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7892 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7893 | return 0; |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7894 | } |
| 7895 | |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7896 | #define SSL_MAX_HASH_LEN 12 |
| 7897 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7898 | int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl) |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7899 | { |
| 7900 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7901 | unsigned int hash_len = 12; |
| 7902 | unsigned char buf[SSL_MAX_HASH_LEN]; |
| 7903 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7904 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished")); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7905 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7906 | ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7907 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7908 | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { |
| 7909 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7910 | goto exit; |
| 7911 | } |
| 7912 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7913 | if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) { |
| 7914 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message")); |
| 7915 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7916 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7917 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 7918 | goto exit; |
| 7919 | } |
| 7920 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7921 | if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) { |
| 7922 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7923 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7924 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 7925 | goto exit; |
| 7926 | } |
| 7927 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7928 | if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) { |
| 7929 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message")); |
| 7930 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7931 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7932 | ret = MBEDTLS_ERR_SSL_DECODE_ERROR; |
| 7933 | goto exit; |
| 7934 | } |
| 7935 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7936 | if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), |
| 7937 | buf, hash_len) != 0) { |
| 7938 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message")); |
| 7939 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7940 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7941 | ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
| 7942 | goto exit; |
| 7943 | } |
| 7944 | |
| 7945 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7946 | ssl->verify_data_len = hash_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7947 | memcpy(ssl->peer_verify_data, buf, hash_len); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7948 | #endif |
| 7949 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7950 | if (ssl->handshake->resume != 0) { |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7951 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7952 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7953 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7954 | } |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7955 | #endif |
| 7956 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7957 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7958 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7959 | } |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7960 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7961 | } else { |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7962 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7963 | } |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7964 | |
| 7965 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7966 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 7967 | mbedtls_ssl_recv_flight_completed(ssl); |
| 7968 | } |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7969 | #endif |
| 7970 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7971 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished")); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7972 | |
| 7973 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7974 | mbedtls_platform_zeroize(buf, hash_len); |
| 7975 | return ret; |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7976 | } |
| 7977 | |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7978 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 7979 | /* |
| 7980 | * Helper to get TLS 1.2 PRF from ciphersuite |
| 7981 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) |
| 7982 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7983 | static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id) |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7984 | { |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7985 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7986 | mbedtls_ssl_ciphersuite_from_id(ciphersuite_id); |
Andrzej Kurek | 6832774 | 2022-10-03 06:18:18 -0400 | [diff] [blame] | 7987 | #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] | 7988 | if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) { |
| 7989 | return tls_prf_sha384; |
| 7990 | } else |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7991 | #endif |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 7992 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 7993 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7994 | if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) { |
| 7995 | return tls_prf_sha256; |
| 7996 | } |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 7997 | } |
| 7998 | #endif |
| 7999 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 8000 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 8001 | (void) ciphersuite_info; |
| 8002 | #endif |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 8003 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8004 | return NULL; |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 8005 | } |
| 8006 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 8007 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8008 | 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] | 8009 | { |
| 8010 | ((void) tls_prf); |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 8011 | #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] | 8012 | if (tls_prf == tls_prf_sha384) { |
| 8013 | return MBEDTLS_SSL_TLS_PRF_SHA384; |
| 8014 | } else |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 8015 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 8016 | #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] | 8017 | if (tls_prf == tls_prf_sha256) { |
| 8018 | return MBEDTLS_SSL_TLS_PRF_SHA256; |
| 8019 | } else |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 8020 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8021 | return MBEDTLS_SSL_TLS_PRF_NONE; |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 8022 | } |
| 8023 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8024 | /* |
| 8025 | * Populate a transform structure with session keys and all the other |
| 8026 | * necessary information. |
| 8027 | * |
| 8028 | * Parameters: |
| 8029 | * - [in/out]: transform: structure to populate |
| 8030 | * [in] must be just initialised with mbedtls_ssl_transform_init() |
| 8031 | * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf() |
| 8032 | * - [in] ciphersuite |
| 8033 | * - [in] master |
| 8034 | * - [in] encrypt_then_mac |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8035 | * - [in] tls_prf: pointer to PRF to use for key derivation |
| 8036 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 8037 | * - [in] tls_version: TLS version |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8038 | * - [in] endpoint: client or server |
| 8039 | * - [in] ssl: used for: |
| 8040 | * - ssl->conf->{f,p}_export_keys |
| 8041 | * [in] optionally used for: |
| 8042 | * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg |
| 8043 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 8044 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8045 | static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform, |
| 8046 | int ciphersuite, |
| 8047 | const unsigned char master[48], |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8048 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8049 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8050 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8051 | ssl_tls_prf_t tls_prf, |
| 8052 | const unsigned char randbytes[64], |
| 8053 | mbedtls_ssl_protocol_version tls_version, |
| 8054 | unsigned endpoint, |
| 8055 | const mbedtls_ssl_context *ssl) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8056 | { |
| 8057 | int ret = 0; |
| 8058 | unsigned char keyblk[256]; |
| 8059 | unsigned char *key1; |
| 8060 | unsigned char *key2; |
| 8061 | unsigned char *mac_enc; |
| 8062 | unsigned char *mac_dec; |
| 8063 | size_t mac_key_len = 0; |
| 8064 | size_t iv_copy_len; |
| 8065 | size_t keylen; |
| 8066 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8067 | mbedtls_ssl_mode_t ssl_mode; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8068 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8069 | const mbedtls_cipher_info_t *cipher_info; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8070 | const mbedtls_md_info_t *md_info; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8071 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8072 | |
| 8073 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8074 | psa_key_type_t key_type; |
| 8075 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8076 | psa_algorithm_t alg; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8077 | psa_algorithm_t mac_alg = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8078 | size_t key_bits; |
| 8079 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 8080 | #endif |
| 8081 | |
| 8082 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 8083 | !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8084 | if (ssl->f_export_keys == NULL) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8085 | ssl = NULL; /* make sure we don't use it except for these cases */ |
| 8086 | (void) ssl; |
| 8087 | } |
| 8088 | #endif |
| 8089 | |
| 8090 | /* |
| 8091 | * Some data just needs copying into the structure |
| 8092 | */ |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8093 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8094 | transform->encrypt_then_mac = encrypt_then_mac; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8095 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 8096 | transform->tls_version = tls_version; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8097 | |
| 8098 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8099 | memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes)); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8100 | #endif |
| 8101 | |
| 8102 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8103 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8104 | /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform |
| 8105 | * generation separate. This should never happen. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8106 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8107 | } |
| 8108 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 8109 | |
| 8110 | /* |
| 8111 | * Get various info structures |
| 8112 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8113 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite); |
| 8114 | if (ciphersuite_info == NULL) { |
| 8115 | MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found", |
| 8116 | ciphersuite)); |
| 8117 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8118 | } |
| 8119 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 8120 | ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8121 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8122 | encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8123 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8124 | ciphersuite_info); |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8125 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8126 | if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) { |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8127 | transform->taglen = |
| 8128 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8129 | } |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8130 | |
| 8131 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8132 | if ((status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher, |
| 8133 | transform->taglen, |
| 8134 | &alg, |
| 8135 | &key_type, |
| 8136 | &key_bits)) != PSA_SUCCESS) { |
| 8137 | ret = psa_ssl_status_to_mbedtls(status); |
| 8138 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8139 | goto end; |
| 8140 | } |
| 8141 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8142 | cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher); |
| 8143 | if (cipher_info == NULL) { |
| 8144 | MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found", |
| 8145 | ciphersuite_info->cipher)); |
| 8146 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8147 | } |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8148 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8149 | |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8150 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8151 | mac_alg = mbedtls_hash_info_psa_from_md(ciphersuite_info->mac); |
| 8152 | if (mac_alg == 0) { |
| 8153 | MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_hash_info_psa_from_md for %u not found", |
| 8154 | (unsigned) ciphersuite_info->mac)); |
| 8155 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8156 | } |
| 8157 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8158 | md_info = mbedtls_md_info_from_type(ciphersuite_info->mac); |
| 8159 | if (md_info == NULL) { |
| 8160 | MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found", |
| 8161 | (unsigned) ciphersuite_info->mac)); |
| 8162 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8163 | } |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8164 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8165 | |
| 8166 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 8167 | /* Copy own and peer's CID if the use of the CID |
| 8168 | * extension has been negotiated. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8169 | if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) { |
| 8170 | MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform")); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8171 | |
| 8172 | transform->in_cid_len = ssl->own_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8173 | memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len); |
| 8174 | MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid, |
| 8175 | transform->in_cid_len); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8176 | |
| 8177 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8178 | memcpy(transform->out_cid, ssl->handshake->peer_cid, |
| 8179 | ssl->handshake->peer_cid_len); |
| 8180 | MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid, |
| 8181 | transform->out_cid_len); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8182 | } |
| 8183 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 8184 | |
| 8185 | /* |
| 8186 | * Compute key block using the PRF |
| 8187 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8188 | ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256); |
| 8189 | if (ret != 0) { |
| 8190 | MBEDTLS_SSL_DEBUG_RET(1, "prf", ret); |
| 8191 | return ret; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8192 | } |
| 8193 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8194 | MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s", |
| 8195 | mbedtls_ssl_get_ciphersuite_name(ciphersuite))); |
| 8196 | MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48); |
| 8197 | MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64); |
| 8198 | MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8199 | |
| 8200 | /* |
| 8201 | * Determine the appropriate key, IV and MAC length. |
| 8202 | */ |
| 8203 | |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8204 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8205 | keylen = PSA_BITS_TO_BYTES(key_bits); |
| 8206 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8207 | keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8208 | #endif |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8209 | |
| 8210 | #if defined(MBEDTLS_GCM_C) || \ |
| 8211 | defined(MBEDTLS_CCM_C) || \ |
| 8212 | defined(MBEDTLS_CHACHAPOLY_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8213 | if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8214 | size_t explicit_ivlen; |
| 8215 | |
| 8216 | transform->maclen = 0; |
| 8217 | mac_key_len = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8218 | |
| 8219 | /* All modes haves 96-bit IVs, but the length of the static parts vary |
| 8220 | * with mode and version: |
| 8221 | * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes |
| 8222 | * (to be concatenated with a dynamically chosen IV of 8 Bytes) |
| 8223 | * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's |
| 8224 | * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record |
| 8225 | * sequence number). |
| 8226 | */ |
| 8227 | transform->ivlen = 12; |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 8228 | |
| 8229 | int is_chachapoly = 0; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8230 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8231 | is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8232 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8233 | is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info) |
| 8234 | == MBEDTLS_MODE_CHACHAPOLY); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8235 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 8236 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8237 | if (is_chachapoly) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8238 | transform->fixed_ivlen = 12; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8239 | } else { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8240 | transform->fixed_ivlen = 4; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8241 | } |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8242 | |
| 8243 | /* Minimum length of encrypted record */ |
| 8244 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
| 8245 | transform->minlen = explicit_ivlen + transform->taglen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8246 | } else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8247 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
| 8248 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8249 | if (ssl_mode == MBEDTLS_SSL_MODE_STREAM || |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8250 | ssl_mode == MBEDTLS_SSL_MODE_CBC || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8251 | ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) { |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8252 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8253 | size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8254 | #else |
| 8255 | size_t block_size = cipher_info->block_size; |
| 8256 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8257 | |
| 8258 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8259 | /* Get MAC length */ |
| 8260 | mac_key_len = PSA_HASH_LENGTH(mac_alg); |
| 8261 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8262 | /* Initialize HMAC contexts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8263 | if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 || |
| 8264 | (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) { |
| 8265 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8266 | goto end; |
| 8267 | } |
| 8268 | |
| 8269 | /* Get MAC length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8270 | mac_key_len = mbedtls_md_get_size(md_info); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8271 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8272 | transform->maclen = mac_key_len; |
| 8273 | |
| 8274 | /* IV length */ |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8275 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8276 | transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8277 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8278 | transform->ivlen = cipher_info->iv_size; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8279 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8280 | |
| 8281 | /* Minimum length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8282 | if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8283 | transform->minlen = transform->maclen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8284 | } else { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8285 | /* |
| 8286 | * GenericBlockCipher: |
| 8287 | * 1. if EtM is in use: one block plus MAC |
| 8288 | * otherwise: * first multiple of blocklen greater than maclen |
| 8289 | * 2. IV |
| 8290 | */ |
| 8291 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8292 | if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8293 | transform->minlen = transform->maclen |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8294 | + block_size; |
| 8295 | } else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8296 | #endif |
| 8297 | { |
| 8298 | transform->minlen = transform->maclen |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8299 | + block_size |
| 8300 | - transform->maclen % block_size; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8301 | } |
| 8302 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8303 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8304 | transform->minlen += transform->ivlen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8305 | } else { |
| 8306 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8307 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 8308 | goto end; |
| 8309 | } |
| 8310 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8311 | } else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8312 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 8313 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8314 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 8315 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8316 | } |
| 8317 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8318 | MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 8319 | (unsigned) keylen, |
| 8320 | (unsigned) transform->minlen, |
| 8321 | (unsigned) transform->ivlen, |
| 8322 | (unsigned) transform->maclen)); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8323 | |
| 8324 | /* |
| 8325 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 8326 | */ |
| 8327 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8328 | if (endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8329 | key1 = keyblk + mac_key_len * 2; |
| 8330 | key2 = keyblk + mac_key_len * 2 + keylen; |
| 8331 | |
| 8332 | mac_enc = keyblk; |
| 8333 | mac_dec = keyblk + mac_key_len; |
| 8334 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8335 | iv_copy_len = (transform->fixed_ivlen) ? |
| 8336 | transform->fixed_ivlen : transform->ivlen; |
| 8337 | memcpy(transform->iv_enc, key2 + keylen, iv_copy_len); |
| 8338 | memcpy(transform->iv_dec, key2 + keylen + iv_copy_len, |
| 8339 | iv_copy_len); |
| 8340 | } else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8341 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 8342 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8343 | if (endpoint == MBEDTLS_SSL_IS_SERVER) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8344 | key1 = keyblk + mac_key_len * 2 + keylen; |
| 8345 | key2 = keyblk + mac_key_len * 2; |
| 8346 | |
| 8347 | mac_enc = keyblk + mac_key_len; |
| 8348 | mac_dec = keyblk; |
| 8349 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8350 | iv_copy_len = (transform->fixed_ivlen) ? |
| 8351 | transform->fixed_ivlen : transform->ivlen; |
| 8352 | memcpy(transform->iv_dec, key1 + keylen, iv_copy_len); |
| 8353 | memcpy(transform->iv_enc, key1 + keylen + iv_copy_len, |
| 8354 | iv_copy_len); |
| 8355 | } else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8356 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 8357 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8358 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8359 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 8360 | goto end; |
| 8361 | } |
| 8362 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8363 | if (ssl != NULL && ssl->f_export_keys != NULL) { |
| 8364 | ssl->f_export_keys(ssl->p_export_keys, |
| 8365 | MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET, |
| 8366 | master, 48, |
| 8367 | randbytes + 32, |
| 8368 | randbytes, |
| 8369 | tls_prf_get_type(tls_prf)); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8370 | } |
| 8371 | |
| 8372 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8373 | transform->psa_alg = alg; |
| 8374 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8375 | if (alg != MBEDTLS_SSL_NULL_CIPHER) { |
| 8376 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 8377 | psa_set_key_algorithm(&attributes, alg); |
| 8378 | psa_set_key_type(&attributes, key_type); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8379 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8380 | if ((status = psa_import_key(&attributes, |
| 8381 | key1, |
| 8382 | PSA_BITS_TO_BYTES(key_bits), |
| 8383 | &transform->psa_key_enc)) != PSA_SUCCESS) { |
| 8384 | MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status); |
| 8385 | ret = psa_ssl_status_to_mbedtls(status); |
| 8386 | MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8387 | goto end; |
| 8388 | } |
| 8389 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8390 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8392 | if ((status = psa_import_key(&attributes, |
| 8393 | key2, |
| 8394 | PSA_BITS_TO_BYTES(key_bits), |
| 8395 | &transform->psa_key_dec)) != PSA_SUCCESS) { |
| 8396 | ret = psa_ssl_status_to_mbedtls(status); |
| 8397 | MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8398 | goto end; |
| 8399 | } |
| 8400 | } |
| 8401 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8402 | if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc, |
| 8403 | cipher_info)) != 0) { |
| 8404 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8405 | goto end; |
| 8406 | } |
| 8407 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8408 | if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec, |
| 8409 | cipher_info)) != 0) { |
| 8410 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8411 | goto end; |
| 8412 | } |
| 8413 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8414 | if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1, |
| 8415 | (int) mbedtls_cipher_info_get_key_bitlen(cipher_info), |
| 8416 | MBEDTLS_ENCRYPT)) != 0) { |
| 8417 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8418 | goto end; |
| 8419 | } |
| 8420 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8421 | if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2, |
| 8422 | (int) mbedtls_cipher_info_get_key_bitlen(cipher_info), |
| 8423 | MBEDTLS_DECRYPT)) != 0) { |
| 8424 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8425 | goto end; |
| 8426 | } |
| 8427 | |
| 8428 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8429 | if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) { |
| 8430 | if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc, |
| 8431 | MBEDTLS_PADDING_NONE)) != 0) { |
| 8432 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8433 | goto end; |
| 8434 | } |
| 8435 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8436 | if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec, |
| 8437 | MBEDTLS_PADDING_NONE)) != 0) { |
| 8438 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8439 | goto end; |
| 8440 | } |
| 8441 | } |
| 8442 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 8443 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8444 | |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8445 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 8446 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 8447 | For AEAD-based ciphersuites, there is nothing to do here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8448 | if (mac_key_len != 0) { |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8449 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8450 | transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8451 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8452 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 8453 | psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg)); |
| 8454 | psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8455 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8456 | if ((status = psa_import_key(&attributes, |
| 8457 | mac_enc, mac_key_len, |
| 8458 | &transform->psa_mac_enc)) != PSA_SUCCESS) { |
| 8459 | ret = psa_ssl_status_to_mbedtls(status); |
| 8460 | MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8461 | goto end; |
| 8462 | } |
| 8463 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8464 | if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) || |
| 8465 | ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING) |
Andrzej Kurek | 8c95ac4 | 2022-08-17 16:17:00 -0400 | [diff] [blame] | 8466 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8467 | && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED) |
Andrzej Kurek | 8c95ac4 | 2022-08-17 16:17:00 -0400 | [diff] [blame] | 8468 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8469 | )) { |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8470 | /* mbedtls_ct_hmac() requires the key to be exportable */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8471 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT | |
| 8472 | PSA_KEY_USAGE_VERIFY_HASH); |
| 8473 | } else { |
| 8474 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 8475 | } |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8476 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8477 | if ((status = psa_import_key(&attributes, |
| 8478 | mac_dec, mac_key_len, |
| 8479 | &transform->psa_mac_dec)) != PSA_SUCCESS) { |
| 8480 | ret = psa_ssl_status_to_mbedtls(status); |
| 8481 | MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8482 | goto end; |
| 8483 | } |
| 8484 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8485 | ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len); |
| 8486 | if (ret != 0) { |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8487 | goto end; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8488 | } |
| 8489 | ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len); |
| 8490 | if (ret != 0) { |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8491 | goto end; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8492 | } |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8493 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8494 | } |
| 8495 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 8496 | |
| 8497 | ((void) mac_dec); |
| 8498 | ((void) mac_enc); |
| 8499 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8500 | end: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8501 | mbedtls_platform_zeroize(keyblk, sizeof(keyblk)); |
| 8502 | return ret; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8503 | } |
| 8504 | |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8505 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \ |
| 8506 | defined(MBEDTLS_USE_PSA_CRYPTO) |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8507 | int mbedtls_psa_ecjpake_read_round( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8508 | psa_pake_operation_t *pake_ctx, |
| 8509 | const unsigned char *buf, |
| 8510 | size_t len, mbedtls_ecjpake_rounds_t round) |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8511 | { |
| 8512 | psa_status_t status; |
| 8513 | size_t input_offset = 0; |
Valerio Setti | 819de86 | 2022-11-17 18:05:19 +0100 | [diff] [blame] | 8514 | /* |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8515 | * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice |
| 8516 | * At round two perform a single cycle |
| 8517 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8518 | unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8519 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8520 | for (; remaining_steps > 0; remaining_steps--) { |
| 8521 | for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8522 | step <= PSA_PAKE_STEP_ZK_PROOF; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8523 | ++step) { |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8524 | /* Length is stored at the first byte */ |
| 8525 | size_t length = buf[input_offset]; |
| 8526 | input_offset += 1; |
| 8527 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8528 | if (input_offset + length > len) { |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8529 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
| 8530 | } |
| 8531 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8532 | status = psa_pake_input(pake_ctx, step, |
| 8533 | buf + input_offset, length); |
| 8534 | if (status != PSA_SUCCESS) { |
| 8535 | return psa_ssl_status_to_mbedtls(status); |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8536 | } |
| 8537 | |
| 8538 | input_offset += length; |
| 8539 | } |
| 8540 | } |
| 8541 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8542 | if (input_offset != len) { |
Valerio Setti | 61ea17d | 2022-11-18 12:11:00 +0100 | [diff] [blame] | 8543 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8544 | } |
Valerio Setti | 30ebe11 | 2022-11-17 16:23:34 +0100 | [diff] [blame] | 8545 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8546 | return 0; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8547 | } |
| 8548 | |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8549 | int mbedtls_psa_ecjpake_write_round( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8550 | psa_pake_operation_t *pake_ctx, |
| 8551 | unsigned char *buf, |
| 8552 | size_t len, size_t *olen, |
| 8553 | mbedtls_ecjpake_rounds_t round) |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8554 | { |
| 8555 | psa_status_t status; |
| 8556 | size_t output_offset = 0; |
| 8557 | size_t output_len; |
Valerio Setti | 819de86 | 2022-11-17 18:05:19 +0100 | [diff] [blame] | 8558 | /* |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8559 | * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice |
| 8560 | * At round two perform a single cycle |
| 8561 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8562 | unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8563 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8564 | for (; remaining_steps > 0; remaining_steps--) { |
| 8565 | for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE; |
| 8566 | step <= PSA_PAKE_STEP_ZK_PROOF; |
| 8567 | ++step) { |
Valerio Setti | 79f6b6b | 2022-11-21 14:17:03 +0100 | [diff] [blame] | 8568 | /* |
Valerio Setti | d4a9b1a | 2022-11-22 11:11:10 +0100 | [diff] [blame] | 8569 | * For each step, prepend 1 byte with the length of the data as |
| 8570 | * given by psa_pake_output(). |
Valerio Setti | 79f6b6b | 2022-11-21 14:17:03 +0100 | [diff] [blame] | 8571 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8572 | status = psa_pake_output(pake_ctx, step, |
| 8573 | buf + output_offset + 1, |
| 8574 | len - output_offset - 1, |
| 8575 | &output_len); |
| 8576 | if (status != PSA_SUCCESS) { |
| 8577 | return psa_ssl_status_to_mbedtls(status); |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8578 | } |
| 8579 | |
Valerio Setti | 99d88c1 | 2022-11-22 16:03:43 +0100 | [diff] [blame] | 8580 | *(buf + output_offset) = (uint8_t) output_len; |
Valerio Setti | 79f6b6b | 2022-11-21 14:17:03 +0100 | [diff] [blame] | 8581 | |
| 8582 | output_offset += output_len + 1; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8583 | } |
| 8584 | } |
| 8585 | |
| 8586 | *olen = output_offset; |
| 8587 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8588 | return 0; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8589 | } |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8590 | #endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO |
| 8591 | |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8592 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8593 | int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl, |
| 8594 | unsigned char *hash, size_t *hashlen, |
| 8595 | unsigned char *data, size_t data_len, |
| 8596 | mbedtls_md_type_t md_alg) |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8597 | { |
| 8598 | psa_status_t status; |
| 8599 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8600 | 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] | 8601 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8602 | 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] | 8603 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8604 | if ((status = psa_hash_setup(&hash_operation, |
| 8605 | hash_alg)) != PSA_SUCCESS) { |
| 8606 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8607 | goto exit; |
| 8608 | } |
| 8609 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8610 | if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes, |
| 8611 | 64)) != PSA_SUCCESS) { |
| 8612 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8613 | goto exit; |
| 8614 | } |
| 8615 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8616 | if ((status = psa_hash_update(&hash_operation, |
| 8617 | data, data_len)) != PSA_SUCCESS) { |
| 8618 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8619 | goto exit; |
| 8620 | } |
| 8621 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8622 | if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE, |
| 8623 | hashlen)) != PSA_SUCCESS) { |
| 8624 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status); |
| 8625 | goto exit; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8626 | } |
| 8627 | |
| 8628 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8629 | if (status != PSA_SUCCESS) { |
| 8630 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8631 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
| 8632 | switch (status) { |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8633 | case PSA_ERROR_NOT_SUPPORTED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8634 | return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8635 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ |
| 8636 | case PSA_ERROR_BUFFER_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8637 | return MBEDTLS_ERR_MD_BAD_INPUT_DATA; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8638 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8639 | return MBEDTLS_ERR_MD_ALLOC_FAILED; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8640 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8641 | return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8642 | } |
| 8643 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8644 | return 0; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8645 | } |
| 8646 | |
| 8647 | #else |
| 8648 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8649 | int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl, |
| 8650 | unsigned char *hash, size_t *hashlen, |
| 8651 | unsigned char *data, size_t data_len, |
| 8652 | mbedtls_md_type_t md_alg) |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8653 | { |
| 8654 | int ret = 0; |
| 8655 | mbedtls_md_context_t ctx; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8656 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg); |
| 8657 | *hashlen = mbedtls_md_get_size(md_info); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8658 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8659 | 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] | 8660 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8661 | mbedtls_md_init(&ctx); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8662 | |
| 8663 | /* |
| 8664 | * digitally-signed struct { |
| 8665 | * opaque client_random[32]; |
| 8666 | * opaque server_random[32]; |
| 8667 | * ServerDHParams params; |
| 8668 | * }; |
| 8669 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8670 | if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) { |
| 8671 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8672 | goto exit; |
| 8673 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8674 | if ((ret = mbedtls_md_starts(&ctx)) != 0) { |
| 8675 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8676 | goto exit; |
| 8677 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8678 | if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) { |
| 8679 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8680 | goto exit; |
| 8681 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8682 | if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) { |
| 8683 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8684 | goto exit; |
| 8685 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8686 | if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) { |
| 8687 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8688 | goto exit; |
| 8689 | } |
| 8690 | |
| 8691 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8692 | mbedtls_md_free(&ctx); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8693 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8694 | if (ret != 0) { |
| 8695 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8696 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
| 8697 | } |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8698 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8699 | return ret; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8700 | } |
| 8701 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8702 | |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8703 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 8704 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8705 | /* Find the preferred hash for a given signature algorithm. */ |
| 8706 | unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8707 | mbedtls_ssl_context *ssl, |
| 8708 | unsigned int sig_alg) |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8709 | { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8710 | unsigned int i; |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8711 | uint16_t *received_sig_algs = ssl->handshake->received_sig_algs; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8712 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8713 | if (sig_alg == MBEDTLS_SSL_SIG_ANON) { |
| 8714 | return MBEDTLS_SSL_HASH_NONE; |
| 8715 | } |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8716 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8717 | for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) { |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8718 | unsigned int hash_alg_received = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8719 | MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG( |
| 8720 | received_sig_algs[i]); |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8721 | unsigned int sig_alg_received = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8722 | MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG( |
| 8723 | received_sig_algs[i]); |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8724 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8725 | if (sig_alg == sig_alg_received) { |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8726 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8727 | if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) { |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8728 | psa_algorithm_t psa_hash_alg = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8729 | mbedtls_hash_info_psa_from_md(hash_alg_received); |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8730 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8731 | if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA && |
| 8732 | !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key, |
| 8733 | PSA_ALG_ECDSA(psa_hash_alg), |
| 8734 | PSA_KEY_USAGE_SIGN_HASH)) { |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8735 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8736 | } |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8737 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8738 | if (sig_alg_received == MBEDTLS_SSL_SIG_RSA && |
| 8739 | !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key, |
| 8740 | PSA_ALG_RSA_PKCS1V15_SIGN( |
| 8741 | psa_hash_alg), |
| 8742 | PSA_KEY_USAGE_SIGN_HASH)) { |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8743 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8744 | } |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8745 | } |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8746 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8747 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8748 | return hash_alg_received; |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8749 | } |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8750 | } |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8751 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8752 | return MBEDTLS_SSL_HASH_NONE; |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8753 | } |
| 8754 | |
| 8755 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 8756 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8757 | /* Serialization of TLS 1.2 sessions: |
| 8758 | * |
| 8759 | * struct { |
| 8760 | * uint64 start_time; |
| 8761 | * uint8 ciphersuite[2]; // defined by the standard |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8762 | * uint8 session_id_len; // at most 32 |
| 8763 | * opaque session_id[32]; |
| 8764 | * opaque master[48]; // fixed length in the standard |
| 8765 | * uint32 verify_result; |
| 8766 | * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert |
| 8767 | * opaque ticket<0..2^24-1>; // length 0 means no ticket |
| 8768 | * uint32 ticket_lifetime; |
| 8769 | * uint8 mfl_code; // up to 255 according to standard |
| 8770 | * uint8 encrypt_then_mac; // 0 or 1 |
| 8771 | * } serialized_session_tls12; |
| 8772 | * |
| 8773 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8774 | static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session, |
| 8775 | unsigned char *buf, |
| 8776 | size_t buf_len) |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8777 | { |
| 8778 | unsigned char *p = buf; |
| 8779 | size_t used = 0; |
| 8780 | |
| 8781 | #if defined(MBEDTLS_HAVE_TIME) |
| 8782 | uint64_t start; |
| 8783 | #endif |
| 8784 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8785 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8786 | size_t cert_len; |
| 8787 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8788 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8789 | |
| 8790 | /* |
| 8791 | * Time |
| 8792 | */ |
| 8793 | #if defined(MBEDTLS_HAVE_TIME) |
| 8794 | used += 8; |
| 8795 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8796 | if (used <= buf_len) { |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8797 | start = (uint64_t) session->start; |
| 8798 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8799 | MBEDTLS_PUT_UINT64_BE(start, p, 0); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8800 | p += 8; |
| 8801 | } |
| 8802 | #endif /* MBEDTLS_HAVE_TIME */ |
| 8803 | |
| 8804 | /* |
| 8805 | * Basic mandatory fields |
| 8806 | */ |
| 8807 | used += 2 /* ciphersuite */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8808 | + 1 /* id_len */ |
| 8809 | + sizeof(session->id) |
| 8810 | + sizeof(session->master) |
| 8811 | + 4; /* verify_result */ |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8812 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8813 | if (used <= buf_len) { |
| 8814 | MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8815 | p += 2; |
| 8816 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8817 | *p++ = MBEDTLS_BYTE_0(session->id_len); |
| 8818 | memcpy(p, session->id, 32); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8819 | p += 32; |
| 8820 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8821 | memcpy(p, session->master, 48); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8822 | p += 48; |
| 8823 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8824 | MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8825 | p += 4; |
| 8826 | } |
| 8827 | |
| 8828 | /* |
| 8829 | * Peer's end-entity certificate |
| 8830 | */ |
| 8831 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8832 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8833 | if (session->peer_cert == NULL) { |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8834 | cert_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8835 | } else { |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8836 | cert_len = session->peer_cert->raw.len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8837 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8838 | |
| 8839 | used += 3 + cert_len; |
| 8840 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8841 | if (used <= buf_len) { |
| 8842 | *p++ = MBEDTLS_BYTE_2(cert_len); |
| 8843 | *p++ = MBEDTLS_BYTE_1(cert_len); |
| 8844 | *p++ = MBEDTLS_BYTE_0(cert_len); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8845 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8846 | if (session->peer_cert != NULL) { |
| 8847 | memcpy(p, session->peer_cert->raw.p, cert_len); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8848 | p += cert_len; |
| 8849 | } |
| 8850 | } |
| 8851 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8852 | if (session->peer_cert_digest != NULL) { |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8853 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8854 | if (used <= buf_len) { |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8855 | *p++ = (unsigned char) session->peer_cert_digest_type; |
| 8856 | *p++ = (unsigned char) session->peer_cert_digest_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8857 | memcpy(p, session->peer_cert_digest, |
| 8858 | session->peer_cert_digest_len); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8859 | p += session->peer_cert_digest_len; |
| 8860 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8861 | } else { |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8862 | used += 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8863 | if (used <= buf_len) { |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8864 | *p++ = (unsigned char) MBEDTLS_MD_NONE; |
| 8865 | *p++ = 0; |
| 8866 | } |
| 8867 | } |
| 8868 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8869 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8870 | |
| 8871 | /* |
| 8872 | * Session ticket if any, plus associated data |
| 8873 | */ |
| 8874 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 8875 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ |
| 8876 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8877 | if (used <= buf_len) { |
| 8878 | *p++ = MBEDTLS_BYTE_2(session->ticket_len); |
| 8879 | *p++ = MBEDTLS_BYTE_1(session->ticket_len); |
| 8880 | *p++ = MBEDTLS_BYTE_0(session->ticket_len); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8881 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8882 | if (session->ticket != NULL) { |
| 8883 | memcpy(p, session->ticket, session->ticket_len); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8884 | p += session->ticket_len; |
| 8885 | } |
| 8886 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8887 | MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8888 | p += 4; |
| 8889 | } |
| 8890 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 8891 | |
| 8892 | /* |
| 8893 | * Misc extension-related info |
| 8894 | */ |
| 8895 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 8896 | used += 1; |
| 8897 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8898 | if (used <= buf_len) { |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8899 | *p++ = session->mfl_code; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8900 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8901 | #endif |
| 8902 | |
| 8903 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 8904 | used += 1; |
| 8905 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8906 | if (used <= buf_len) { |
| 8907 | *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac); |
| 8908 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8909 | #endif |
| 8910 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8911 | return used; |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8912 | } |
| 8913 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 8914 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8915 | static int ssl_tls12_session_load(mbedtls_ssl_session *session, |
| 8916 | const unsigned char *buf, |
| 8917 | size_t len) |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8918 | { |
| 8919 | #if defined(MBEDTLS_HAVE_TIME) |
| 8920 | uint64_t start; |
| 8921 | #endif |
| 8922 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8923 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8924 | size_t cert_len; |
| 8925 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8926 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8927 | |
| 8928 | const unsigned char *p = buf; |
| 8929 | const unsigned char * const end = buf + len; |
| 8930 | |
| 8931 | /* |
| 8932 | * Time |
| 8933 | */ |
| 8934 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8935 | if (8 > (size_t) (end - p)) { |
| 8936 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 8937 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8938 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8939 | start = ((uint64_t) p[0] << 56) | |
| 8940 | ((uint64_t) p[1] << 48) | |
| 8941 | ((uint64_t) p[2] << 40) | |
| 8942 | ((uint64_t) p[3] << 32) | |
| 8943 | ((uint64_t) p[4] << 24) | |
| 8944 | ((uint64_t) p[5] << 16) | |
| 8945 | ((uint64_t) p[6] << 8) | |
| 8946 | ((uint64_t) p[7]); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8947 | p += 8; |
| 8948 | |
| 8949 | session->start = (time_t) start; |
| 8950 | #endif /* MBEDTLS_HAVE_TIME */ |
| 8951 | |
| 8952 | /* |
| 8953 | * Basic mandatory fields |
| 8954 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8955 | if (2 + 1 + 32 + 48 + 4 > (size_t) (end - p)) { |
| 8956 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 8957 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8958 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8959 | session->ciphersuite = (p[0] << 8) | p[1]; |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8960 | p += 2; |
| 8961 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8962 | session->id_len = *p++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8963 | memcpy(session->id, p, 32); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8964 | p += 32; |
| 8965 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8966 | memcpy(session->master, p, 48); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8967 | p += 48; |
| 8968 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8969 | session->verify_result = ((uint32_t) p[0] << 24) | |
| 8970 | ((uint32_t) p[1] << 16) | |
| 8971 | ((uint32_t) p[2] << 8) | |
| 8972 | ((uint32_t) p[3]); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8973 | p += 4; |
| 8974 | |
| 8975 | /* Immediately clear invalid pointer values that have been read, in case |
| 8976 | * we exit early before we replaced them with valid ones. */ |
| 8977 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8978 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8979 | session->peer_cert = NULL; |
| 8980 | #else |
| 8981 | session->peer_cert_digest = NULL; |
| 8982 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8983 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8984 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 8985 | session->ticket = NULL; |
| 8986 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 8987 | |
| 8988 | /* |
| 8989 | * Peer certificate |
| 8990 | */ |
| 8991 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8992 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8993 | /* Deserialize CRT from the end of the ticket. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8994 | if (3 > (size_t) (end - p)) { |
| 8995 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 8996 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8997 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8998 | cert_len = (p[0] << 16) | (p[1] << 8) | p[2]; |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8999 | p += 3; |
| 9000 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9001 | if (cert_len != 0) { |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9002 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 9003 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9004 | if (cert_len > (size_t) (end - p)) { |
| 9005 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9006 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9007 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9008 | session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt)); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9009 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9010 | if (session->peer_cert == NULL) { |
| 9011 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 9012 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9013 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9014 | mbedtls_x509_crt_init(session->peer_cert); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9015 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9016 | if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert, |
| 9017 | p, cert_len)) != 0) { |
| 9018 | mbedtls_x509_crt_free(session->peer_cert); |
| 9019 | mbedtls_free(session->peer_cert); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9020 | session->peer_cert = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9021 | return ret; |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9022 | } |
| 9023 | |
| 9024 | p += cert_len; |
| 9025 | } |
| 9026 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 9027 | /* Deserialize CRT digest from the end of the ticket. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9028 | if (2 > (size_t) (end - p)) { |
| 9029 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9030 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9031 | |
| 9032 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; |
| 9033 | session->peer_cert_digest_len = (size_t) *p++; |
| 9034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9035 | if (session->peer_cert_digest_len != 0) { |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9036 | const mbedtls_md_info_t *md_info = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9037 | mbedtls_md_info_from_type(session->peer_cert_digest_type); |
| 9038 | if (md_info == NULL) { |
| 9039 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9040 | } |
| 9041 | if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) { |
| 9042 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9043 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9044 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9045 | if (session->peer_cert_digest_len > (size_t) (end - p)) { |
| 9046 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9047 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9048 | |
| 9049 | session->peer_cert_digest = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9050 | mbedtls_calloc(1, session->peer_cert_digest_len); |
| 9051 | if (session->peer_cert_digest == NULL) { |
| 9052 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 9053 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9054 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9055 | memcpy(session->peer_cert_digest, p, |
| 9056 | session->peer_cert_digest_len); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9057 | p += session->peer_cert_digest_len; |
| 9058 | } |
| 9059 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 9060 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 9061 | |
| 9062 | /* |
| 9063 | * Session ticket and associated data |
| 9064 | */ |
| 9065 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9066 | if (3 > (size_t) (end - p)) { |
| 9067 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9068 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9069 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9070 | session->ticket_len = (p[0] << 16) | (p[1] << 8) | p[2]; |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9071 | p += 3; |
| 9072 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9073 | if (session->ticket_len != 0) { |
| 9074 | if (session->ticket_len > (size_t) (end - p)) { |
| 9075 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9076 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9077 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9078 | session->ticket = mbedtls_calloc(1, session->ticket_len); |
| 9079 | if (session->ticket == NULL) { |
| 9080 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 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 | memcpy(session->ticket, p, session->ticket_len); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9084 | p += session->ticket_len; |
| 9085 | } |
| 9086 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9087 | if (4 > (size_t) (end - p)) { |
| 9088 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9089 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9090 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9091 | session->ticket_lifetime = ((uint32_t) p[0] << 24) | |
| 9092 | ((uint32_t) p[1] << 16) | |
| 9093 | ((uint32_t) p[2] << 8) | |
| 9094 | ((uint32_t) p[3]); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9095 | p += 4; |
| 9096 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 9097 | |
| 9098 | /* |
| 9099 | * Misc extension-related info |
| 9100 | */ |
| 9101 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9102 | if (1 > (size_t) (end - p)) { |
| 9103 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9104 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9105 | |
| 9106 | session->mfl_code = *p++; |
| 9107 | #endif |
| 9108 | |
| 9109 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9110 | if (1 > (size_t) (end - p)) { |
| 9111 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9112 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9113 | |
| 9114 | session->encrypt_then_mac = *p++; |
| 9115 | #endif |
| 9116 | |
| 9117 | /* Done, should have consumed entire buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9118 | if (p != end) { |
| 9119 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9120 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9121 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9122 | return 0; |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9123 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 9124 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 9125 | |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9126 | int mbedtls_ssl_validate_ciphersuite( |
| 9127 | const mbedtls_ssl_context *ssl, |
| 9128 | const mbedtls_ssl_ciphersuite_t *suite_info, |
| 9129 | mbedtls_ssl_protocol_version min_tls_version, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9130 | mbedtls_ssl_protocol_version max_tls_version) |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9131 | { |
| 9132 | (void) ssl; |
| 9133 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9134 | if (suite_info == NULL) { |
| 9135 | return -1; |
| 9136 | } |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9137 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9138 | if ((suite_info->min_tls_version > max_tls_version) || |
| 9139 | (suite_info->max_tls_version < min_tls_version)) { |
| 9140 | return -1; |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9141 | } |
| 9142 | |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 9143 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C) |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9144 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 9145 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9146 | if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
| 9147 | ssl->handshake->psa_pake_ctx_is_ok != 1) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 9148 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9149 | if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
| 9150 | mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 9151 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9152 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9153 | return -1; |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9154 | } |
| 9155 | #endif |
| 9156 | |
| 9157 | /* Don't suggest PSK-based ciphersuite if no PSK is available. */ |
| 9158 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9159 | if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) && |
| 9160 | mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) { |
| 9161 | return -1; |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9162 | } |
| 9163 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
| 9164 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 9165 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9166 | return 0; |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9167 | } |
| 9168 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 9169 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9170 | /* |
| 9171 | * Function for writing a signature algorithm extension. |
| 9172 | * |
| 9173 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 9174 | * value (TLS 1.3 RFC8446): |
| 9175 | * enum { |
| 9176 | * .... |
| 9177 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 9178 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 9179 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 9180 | * .... |
| 9181 | * } SignatureScheme; |
| 9182 | * |
| 9183 | * struct { |
| 9184 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 9185 | * } SignatureSchemeList; |
| 9186 | * |
| 9187 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 9188 | * value (TLS 1.2 RFC5246): |
| 9189 | * enum { |
| 9190 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 9191 | * sha512(6), (255) |
| 9192 | * } HashAlgorithm; |
| 9193 | * |
| 9194 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 9195 | * SignatureAlgorithm; |
| 9196 | * |
| 9197 | * struct { |
| 9198 | * HashAlgorithm hash; |
| 9199 | * SignatureAlgorithm signature; |
| 9200 | * } SignatureAndHashAlgorithm; |
| 9201 | * |
| 9202 | * SignatureAndHashAlgorithm |
| 9203 | * supported_signature_algorithms<2..2^16-2>; |
| 9204 | * |
| 9205 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 9206 | * generalization of the TLS 1.2 signature algorithm extension. |
| 9207 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 9208 | * `SignatureScheme` field of TLS 1.3 |
| 9209 | * |
| 9210 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9211 | int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf, |
| 9212 | const unsigned char *end, size_t *out_len) |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9213 | { |
| 9214 | unsigned char *p = buf; |
| 9215 | unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */ |
| 9216 | size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */ |
| 9217 | |
| 9218 | *out_len = 0; |
| 9219 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9220 | MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension")); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9221 | |
| 9222 | /* Check if we have space for header and length field: |
| 9223 | * - extension_type (2 bytes) |
| 9224 | * - extension_data_length (2 bytes) |
| 9225 | * - supported_signature_algorithms_length (2 bytes) |
| 9226 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9227 | MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9228 | p += 6; |
| 9229 | |
| 9230 | /* |
| 9231 | * Write supported_signature_algorithms |
| 9232 | */ |
| 9233 | supported_sig_alg = p; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9234 | const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl); |
| 9235 | if (sig_alg == NULL) { |
| 9236 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
| 9237 | } |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9238 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9239 | for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) { |
| 9240 | MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s", |
| 9241 | *sig_alg, |
| 9242 | mbedtls_ssl_sig_alg_to_str(*sig_alg))); |
| 9243 | if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) { |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9244 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9245 | } |
| 9246 | MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2); |
| 9247 | MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9248 | p += 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9249 | MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s", |
| 9250 | *sig_alg, |
| 9251 | mbedtls_ssl_sig_alg_to_str(*sig_alg))); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9252 | } |
| 9253 | |
| 9254 | /* Length of supported_signature_algorithms */ |
| 9255 | supported_sig_alg_len = p - supported_sig_alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9256 | if (supported_sig_alg_len == 0) { |
| 9257 | MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined.")); |
| 9258 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9259 | } |
| 9260 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9261 | MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0); |
| 9262 | MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2); |
| 9263 | MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9264 | |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9265 | *out_len = p - buf; |
| 9266 | |
| 9267 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9268 | 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] | 9269 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | 0c354a2 | 2022-08-29 15:25:36 +0800 | [diff] [blame] | 9270 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9271 | return 0; |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9272 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 9273 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9274 | |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9275 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9276 | /* |
| 9277 | * mbedtls_ssl_parse_server_name_ext |
| 9278 | * |
| 9279 | * Structure of server_name extension: |
| 9280 | * |
| 9281 | * enum { |
| 9282 | * host_name(0), (255) |
| 9283 | * } NameType; |
| 9284 | * opaque HostName<1..2^16-1>; |
| 9285 | * |
| 9286 | * struct { |
| 9287 | * NameType name_type; |
| 9288 | * select (name_type) { |
| 9289 | * case host_name: HostName; |
| 9290 | * } name; |
| 9291 | * } ServerName; |
| 9292 | * struct { |
| 9293 | * ServerName server_name_list<1..2^16-1> |
| 9294 | * } ServerNameList; |
| 9295 | */ |
Ronald Cron | ce7d76e | 2022-07-08 18:56:49 +0200 | [diff] [blame] | 9296 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9297 | int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl, |
| 9298 | const unsigned char *buf, |
| 9299 | const unsigned char *end) |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9300 | { |
| 9301 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 9302 | const unsigned char *p = buf; |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9303 | size_t server_name_list_len, hostname_len; |
| 9304 | const unsigned char *server_name_list_end; |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9306 | MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension")); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9307 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9308 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2); |
| 9309 | server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9310 | p += 2; |
| 9311 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9312 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len); |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9313 | server_name_list_end = p + server_name_list_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9314 | while (p < server_name_list_end) { |
| 9315 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3); |
| 9316 | hostname_len = MBEDTLS_GET_UINT16_BE(p, 1); |
| 9317 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, |
| 9318 | hostname_len + 3); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9319 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9320 | if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) { |
XiaokangQian | 75fe8c7 | 2022-06-15 09:42:45 +0000 | [diff] [blame] | 9321 | /* sni_name is intended to be used only during the parsing of the |
| 9322 | * ClientHello message (it is reset to NULL before the end of |
| 9323 | * the message parsing). Thus it is ok to just point to the |
| 9324 | * reception buffer and not make a copy of it. |
| 9325 | */ |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 9326 | ssl->handshake->sni_name = p + 3; |
| 9327 | ssl->handshake->sni_name_len = hostname_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9328 | if (ssl->conf->f_sni == NULL) { |
| 9329 | return 0; |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9330 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9331 | ret = ssl->conf->f_sni(ssl->conf->p_sni, |
| 9332 | ssl, p + 3, hostname_len); |
| 9333 | if (ret != 0) { |
| 9334 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret); |
| 9335 | MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME, |
| 9336 | MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME); |
| 9337 | return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME; |
| 9338 | } |
| 9339 | return 0; |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9340 | } |
| 9341 | |
| 9342 | p += hostname_len + 3; |
| 9343 | } |
| 9344 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9345 | return 0; |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9346 | } |
| 9347 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 9348 | |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9349 | #if defined(MBEDTLS_SSL_ALPN) |
Ronald Cron | ce7d76e | 2022-07-08 18:56:49 +0200 | [diff] [blame] | 9350 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9351 | int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl, |
| 9352 | const unsigned char *buf, |
| 9353 | const unsigned char *end) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9354 | { |
| 9355 | const unsigned char *p = buf; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9356 | size_t protocol_name_list_len; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9357 | const unsigned char *protocol_name_list; |
| 9358 | const unsigned char *protocol_name_list_end; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9359 | size_t protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9360 | |
| 9361 | /* If ALPN not configured, just ignore the extension */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9362 | if (ssl->conf->alpn_list == NULL) { |
| 9363 | return 0; |
| 9364 | } |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9365 | |
| 9366 | /* |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9367 | * RFC7301, section 3.1 |
| 9368 | * opaque ProtocolName<1..2^8-1>; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9369 | * |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9370 | * struct { |
| 9371 | * ProtocolName protocol_name_list<2..2^16-1> |
| 9372 | * } ProtocolNameList; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9373 | */ |
| 9374 | |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9375 | /* |
XiaokangQian | 0b776e2 | 2022-06-24 09:04:59 +0000 | [diff] [blame] | 9376 | * protocol_name_list_len 2 bytes |
| 9377 | * protocol_name_len 1 bytes |
| 9378 | * protocol_name >=1 byte |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9379 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9380 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9381 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9382 | protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9383 | p += 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9384 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9385 | protocol_name_list = p; |
| 9386 | protocol_name_list_end = p + protocol_name_list_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9387 | |
| 9388 | /* Validate peer's list (lengths) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9389 | while (p < protocol_name_list_end) { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9390 | protocol_name_len = *p++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9391 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end, |
| 9392 | protocol_name_len); |
| 9393 | if (protocol_name_len == 0) { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9394 | MBEDTLS_SSL_PEND_FATAL_ALERT( |
| 9395 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9396 | MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER); |
| 9397 | return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9398 | } |
| 9399 | |
| 9400 | p += protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9401 | } |
| 9402 | |
| 9403 | /* Use our order of preference */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9404 | for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) { |
| 9405 | size_t const alpn_len = strlen(*alpn); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9406 | p = protocol_name_list; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9407 | while (p < protocol_name_list_end) { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9408 | protocol_name_len = *p++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9409 | if (protocol_name_len == alpn_len && |
| 9410 | memcmp(p, *alpn, alpn_len) == 0) { |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9411 | ssl->alpn_chosen = *alpn; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9412 | return 0; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9413 | } |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9414 | |
| 9415 | p += protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9416 | } |
| 9417 | } |
| 9418 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9419 | /* If we get here, no match was found */ |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9420 | MBEDTLS_SSL_PEND_FATAL_ALERT( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9421 | MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL, |
| 9422 | MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL); |
| 9423 | return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9424 | } |
| 9425 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9426 | int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl, |
| 9427 | unsigned char *buf, |
| 9428 | unsigned char *end, |
| 9429 | size_t *out_len) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9430 | { |
| 9431 | unsigned char *p = buf; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9432 | size_t protocol_name_len; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9433 | *out_len = 0; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9434 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9435 | if (ssl->alpn_chosen == NULL) { |
| 9436 | return 0; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9437 | } |
| 9438 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9439 | protocol_name_len = strlen(ssl->alpn_chosen); |
| 9440 | MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9441 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9442 | MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension")); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9443 | /* |
| 9444 | * 0 . 1 ext identifier |
| 9445 | * 2 . 3 ext length |
| 9446 | * 4 . 5 protocol list length |
| 9447 | * 6 . 6 protocol name length |
| 9448 | * 7 . 7+n protocol name |
| 9449 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9450 | MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9451 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9452 | *out_len = 7 + protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9453 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9454 | MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2); |
| 9455 | MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4); |
XiaokangQian | 0b776e2 | 2022-06-24 09:04:59 +0000 | [diff] [blame] | 9456 | /* Note: the length of the chosen protocol has been checked to be less |
| 9457 | * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`. |
| 9458 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9459 | p[6] = MBEDTLS_BYTE_0(protocol_name_len); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9460 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9461 | memcpy(p + 7, ssl->alpn_chosen, protocol_name_len); |
Jerry Yu | b95dd36 | 2022-11-08 21:19:34 +0800 | [diff] [blame] | 9462 | |
| 9463 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9464 | 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] | 9465 | #endif |
| 9466 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9467 | return 0; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9468 | } |
| 9469 | #endif /* MBEDTLS_SSL_ALPN */ |
| 9470 | |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9471 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
Xiaokang Qian | 0340929 | 2022-10-12 02:49:52 +0000 | [diff] [blame] | 9472 | defined(MBEDTLS_SSL_SESSION_TICKETS) && \ |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 9473 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \ |
Xiaokang Qian | ed3afcd | 2022-10-12 08:31:11 +0000 | [diff] [blame] | 9474 | defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9475 | int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session, |
| 9476 | const char *hostname) |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9477 | { |
| 9478 | /* Initialize to suppress unnecessary compiler warning */ |
| 9479 | size_t hostname_len = 0; |
| 9480 | |
| 9481 | /* Check if new hostname is valid before |
| 9482 | * making any change to current one */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9483 | if (hostname != NULL) { |
| 9484 | hostname_len = strlen(hostname); |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9485 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9486 | if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) { |
| 9487 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9488 | } |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9489 | } |
| 9490 | |
| 9491 | /* Now it's clear that we will overwrite the old hostname, |
| 9492 | * so we can free it safely */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9493 | if (session->hostname != NULL) { |
| 9494 | mbedtls_platform_zeroize(session->hostname, |
| 9495 | strlen(session->hostname)); |
| 9496 | mbedtls_free(session->hostname); |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9497 | } |
| 9498 | |
| 9499 | /* Passing NULL as hostname shall clear the old one */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9500 | if (hostname == NULL) { |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9501 | session->hostname = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9502 | } else { |
| 9503 | session->hostname = mbedtls_calloc(1, hostname_len + 1); |
| 9504 | if (session->hostname == NULL) { |
| 9505 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 9506 | } |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9507 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9508 | memcpy(session->hostname, hostname, hostname_len); |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9509 | } |
| 9510 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9511 | return 0; |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9512 | } |
Xiaokang Qian | 0340929 | 2022-10-12 02:49:52 +0000 | [diff] [blame] | 9513 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 && |
| 9514 | MBEDTLS_SSL_SESSION_TICKETS && |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 9515 | MBEDTLS_SSL_SERVER_NAME_INDICATION && |
Xiaokang Qian | ed3afcd | 2022-10-12 08:31:11 +0000 | [diff] [blame] | 9516 | MBEDTLS_SSL_CLI_C */ |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9517 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9518 | #endif /* MBEDTLS_SSL_TLS_C */ |