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); |
| 421 | static void ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *); |
| 422 | static void ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int); |
| 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 | |
| 433 | static void ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *); |
| 434 | static void ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int); |
| 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 | |
| 447 | static void ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t); |
| 448 | |
| 449 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 450 | static void ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t); |
| 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) |
| 454 | static void ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t); |
| 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 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 815 | void 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 |
| 834 | } |
| 835 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 836 | static void ssl_update_checksum_start(mbedtls_ssl_context *ssl, |
| 837 | const unsigned char *buf, size_t len) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 838 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 839 | #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] | 840 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 841 | psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 842 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 843 | mbedtls_sha256_update(&ssl->handshake->fin_sha256, buf, len); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 844 | #endif |
| 845 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 846 | #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] | 847 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 848 | psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 849 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 850 | mbedtls_sha512_update(&ssl->handshake->fin_sha384, buf, len); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 851 | #endif |
| 852 | #endif |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 853 | #if !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 854 | !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 855 | (void) ssl; |
| 856 | (void) buf; |
| 857 | (void) len; |
| 858 | #endif |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 859 | } |
| 860 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 861 | #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] | 862 | static void ssl_update_checksum_sha256(mbedtls_ssl_context *ssl, |
| 863 | const unsigned char *buf, size_t len) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 864 | { |
| 865 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 866 | psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 867 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 868 | mbedtls_sha256_update(&ssl->handshake->fin_sha256, buf, len); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 869 | #endif |
| 870 | } |
| 871 | #endif |
| 872 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 873 | #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] | 874 | static void ssl_update_checksum_sha384(mbedtls_ssl_context *ssl, |
| 875 | const unsigned char *buf, size_t len) |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 876 | { |
| 877 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 878 | psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 879 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 880 | mbedtls_sha512_update(&ssl->handshake->fin_sha384, buf, len); |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 881 | #endif |
| 882 | } |
| 883 | #endif |
| 884 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 885 | static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 886 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 887 | memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params)); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 888 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 889 | #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] | 890 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 891 | handshake->fin_sha256_psa = psa_hash_operation_init(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 892 | psa_hash_setup(&handshake->fin_sha256_psa, PSA_ALG_SHA_256); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 893 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 894 | mbedtls_sha256_init(&handshake->fin_sha256); |
| 895 | mbedtls_sha256_starts(&handshake->fin_sha256, 0); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 896 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 897 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 898 | #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] | 899 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 900 | handshake->fin_sha384_psa = psa_hash_operation_init(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 901 | psa_hash_setup(&handshake->fin_sha384_psa, PSA_ALG_SHA_384); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 902 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 903 | mbedtls_sha512_init(&handshake->fin_sha384); |
| 904 | mbedtls_sha512_starts(&handshake->fin_sha384, 1); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 905 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 906 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 907 | |
| 908 | handshake->update_checksum = ssl_update_checksum_start; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 909 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 910 | #if defined(MBEDTLS_DHM_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 911 | mbedtls_dhm_init(&handshake->dhm_ctx); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 912 | #endif |
Neil Armstrong | f3f4641 | 2022-04-12 14:43:39 +0200 | [diff] [blame] | 913 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 914 | mbedtls_ecdh_init(&handshake->ecdh_ctx); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 915 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 916 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 917 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 918 | handshake->psa_pake_ctx = psa_pake_operation_init(); |
| 919 | handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT; |
| 920 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 921 | mbedtls_ecjpake_init(&handshake->ecjpake_ctx); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 922 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 923 | #if defined(MBEDTLS_SSL_CLI_C) |
| 924 | handshake->ecjpake_cache = NULL; |
| 925 | handshake->ecjpake_cache_len = 0; |
| 926 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 927 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 928 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 929 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 930 | mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 931 | #endif |
| 932 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 933 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 934 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 935 | #endif |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 936 | |
| 937 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 938 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 939 | mbedtls_pk_init(&handshake->peer_pubkey); |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 940 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 941 | } |
| 942 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 943 | void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 944 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 945 | memset(transform, 0, sizeof(mbedtls_ssl_transform)); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 946 | |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 947 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 948 | transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 949 | transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 950 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 951 | mbedtls_cipher_init(&transform->cipher_ctx_enc); |
| 952 | mbedtls_cipher_init(&transform->cipher_ctx_dec); |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 953 | #endif |
| 954 | |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 955 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 39b8e7d | 2022-02-23 09:24:45 +0100 | [diff] [blame] | 956 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 957 | transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 958 | transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 959 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 960 | mbedtls_md_init(&transform->md_ctx_enc); |
| 961 | mbedtls_md_init(&transform->md_ctx_dec); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 962 | #endif |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 963 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 964 | } |
| 965 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 966 | void mbedtls_ssl_session_init(mbedtls_ssl_session *session) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 967 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 968 | memset(session, 0, sizeof(mbedtls_ssl_session)); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 969 | } |
| 970 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 971 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 972 | static int ssl_handshake_init(mbedtls_ssl_context *ssl) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 973 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 974 | /* Clear old handshake information if present */ |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 975 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 976 | if (ssl->transform_negotiate) { |
| 977 | mbedtls_ssl_transform_free(ssl->transform_negotiate); |
| 978 | } |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 979 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 980 | if (ssl->session_negotiate) { |
| 981 | mbedtls_ssl_session_free(ssl->session_negotiate); |
| 982 | } |
| 983 | if (ssl->handshake) { |
| 984 | mbedtls_ssl_handshake_free(ssl); |
| 985 | } |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 986 | |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 987 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 988 | /* |
| 989 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 990 | * Now allocate missing structures. |
| 991 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 992 | if (ssl->transform_negotiate == NULL) { |
| 993 | ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform)); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 994 | } |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 995 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 996 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 997 | if (ssl->session_negotiate == NULL) { |
| 998 | ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session)); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 999 | } |
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->handshake == NULL) { |
| 1002 | ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params)); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 1003 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1004 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1005 | /* If the buffers are too small - reallocate */ |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 1006 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1007 | handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN, |
| 1008 | MBEDTLS_SSL_OUT_BUFFER_LEN); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1009 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1010 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1011 | /* All pointers should exist and can be directly freed without issue */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1012 | if (ssl->handshake == NULL || |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1013 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1014 | ssl->transform_negotiate == NULL || |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1015 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1016 | ssl->session_negotiate == NULL) { |
| 1017 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed")); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1018 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1019 | mbedtls_free(ssl->handshake); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1020 | ssl->handshake = NULL; |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1021 | |
| 1022 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1023 | mbedtls_free(ssl->transform_negotiate); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1024 | ssl->transform_negotiate = NULL; |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1025 | #endif |
| 1026 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1027 | mbedtls_free(ssl->session_negotiate); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1028 | ssl->session_negotiate = NULL; |
| 1029 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1030 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 1033 | /* Initialize structures */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1034 | mbedtls_ssl_session_init(ssl->session_negotiate); |
| 1035 | ssl_handshake_params_init(ssl->handshake); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 1036 | |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1037 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1038 | mbedtls_ssl_transform_init(ssl->transform_negotiate); |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 1039 | #endif |
| 1040 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 1041 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 1042 | defined(MBEDTLS_SSL_SRV_C) && \ |
| 1043 | defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 1044 | ssl->handshake->new_session_tickets_count = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1045 | ssl->conf->new_session_tickets_count; |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 1046 | #endif |
| 1047 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1048 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1049 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1050 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1051 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1052 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1053 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1054 | } else { |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1055 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1056 | } |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1057 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1058 | mbedtls_ssl_set_timer(ssl, 0); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 1059 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1060 | #endif |
| 1061 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1062 | /* |
| 1063 | * curve_list is translated to IANA TLS group identifiers here because |
| 1064 | * mbedtls_ssl_conf_curves returns void and so can't return |
| 1065 | * any error codes. |
| 1066 | */ |
| 1067 | #if defined(MBEDTLS_ECP_C) |
| 1068 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 1069 | /* Heap allocate and translate curve_list from internal to IANA group ids */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1070 | if (ssl->conf->curve_list != NULL) { |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1071 | size_t length; |
| 1072 | const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list; |
| 1073 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1074 | for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE) && |
| 1075 | (length < MBEDTLS_ECP_DP_MAX); length++) { |
| 1076 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1077 | |
| 1078 | /* Leave room for zero termination */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1079 | uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t)); |
| 1080 | if (group_list == NULL) { |
| 1081 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 1082 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1083 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1084 | for (size_t i = 0; i < length; i++) { |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 1085 | 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] | 1086 | curve_list[i]); |
| 1087 | if (tls_id == 0) { |
| 1088 | mbedtls_free(group_list); |
| 1089 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1090 | } |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 1091 | group_list[i] = tls_id; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | group_list[length] = 0; |
| 1095 | |
| 1096 | ssl->handshake->group_list = group_list; |
| 1097 | ssl->handshake->group_list_heap_allocated = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1098 | } else { |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1099 | ssl->handshake->group_list = ssl->conf->group_list; |
| 1100 | ssl->handshake->group_list_heap_allocated = 0; |
| 1101 | } |
| 1102 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 1103 | #endif /* MBEDTLS_ECP_C */ |
| 1104 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1105 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1106 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 1107 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 1108 | /* Heap allocate and translate sig_hashes from internal hash identifiers to |
| 1109 | signature algorithms IANA identifiers. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1110 | if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) && |
| 1111 | ssl->conf->sig_hashes != NULL) { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1112 | const int *md; |
| 1113 | const int *sig_hashes = ssl->conf->sig_hashes; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1114 | size_t sig_algs_len = 0; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1115 | uint16_t *p; |
| 1116 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1117 | #if defined(static_assert) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1118 | static_assert(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN |
| 1119 | <= (SIZE_MAX - (2 * sizeof(uint16_t))), |
| 1120 | "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big"); |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 1121 | #endif |
| 1122 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1123 | for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) { |
| 1124 | if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1125 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1126 | } |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1127 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1128 | sig_algs_len += sizeof(uint16_t); |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1129 | #endif |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 1130 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 1131 | #if defined(MBEDTLS_RSA_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 |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1134 | if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) { |
| 1135 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
| 1136 | } |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1137 | } |
| 1138 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1139 | if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) { |
| 1140 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
| 1141 | } |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1142 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1143 | ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len + |
| 1144 | sizeof(uint16_t)); |
| 1145 | if (ssl->handshake->sig_algs == NULL) { |
| 1146 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 1147 | } |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1149 | p = (uint16_t *) ssl->handshake->sig_algs; |
| 1150 | for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) { |
| 1151 | unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md); |
| 1152 | if (hash == MBEDTLS_SSL_HASH_NONE) { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1153 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1154 | } |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 1155 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1156 | *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1157 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 1158 | #endif |
| 1159 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1160 | *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA); |
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 |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1163 | } |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 1164 | *p = MBEDTLS_TLS_SIG_NONE; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1165 | ssl->handshake->sig_algs_heap_allocated = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1166 | } else |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 1167 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1168 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1169 | ssl->handshake->sig_algs_heap_allocated = 0; |
| 1170 | } |
Jerry Yu | cc53910 | 2022-06-27 16:27:35 +0800 | [diff] [blame] | 1171 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1172 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1173 | return 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1176 | #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] | 1177 | /* Dummy cookie callbacks for defaults */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1178 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1179 | static int ssl_cookie_write_dummy(void *ctx, |
| 1180 | unsigned char **p, unsigned char *end, |
| 1181 | const unsigned char *cli_id, size_t cli_id_len) |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1182 | { |
| 1183 | ((void) ctx); |
| 1184 | ((void) p); |
| 1185 | ((void) end); |
| 1186 | ((void) cli_id); |
| 1187 | ((void) cli_id_len); |
| 1188 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1189 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1190 | } |
| 1191 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1192 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1193 | static int ssl_cookie_check_dummy(void *ctx, |
| 1194 | const unsigned char *cookie, size_t cookie_len, |
| 1195 | const unsigned char *cli_id, size_t cli_id_len) |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1196 | { |
| 1197 | ((void) ctx); |
| 1198 | ((void) cookie); |
| 1199 | ((void) cookie_len); |
| 1200 | ((void) cli_id); |
| 1201 | ((void) cli_id_len); |
| 1202 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1203 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1204 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1205 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 1206 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1207 | /* |
| 1208 | * Initialize an SSL context |
| 1209 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1210 | void mbedtls_ssl_init(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1211 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1212 | memset(ssl, 0, sizeof(mbedtls_ssl_context)); |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1213 | } |
| 1214 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1215 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1216 | static int ssl_conf_version_check(const mbedtls_ssl_context *ssl) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1217 | { |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 1218 | const mbedtls_ssl_config *conf = ssl->conf; |
| 1219 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1220 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1221 | if (mbedtls_ssl_conf_is_tls13_only(conf)) { |
| 1222 | if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 1223 | MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported.")); |
| 1224 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1227 | MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only.")); |
| 1228 | return 0; |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1229 | } |
| 1230 | #endif |
| 1231 | |
| 1232 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1233 | if (mbedtls_ssl_conf_is_tls12_only(conf)) { |
| 1234 | MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only.")); |
| 1235 | return 0; |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1236 | } |
| 1237 | #endif |
| 1238 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1239 | #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] | 1240 | if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) { |
| 1241 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 1242 | MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2")); |
| 1243 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1246 | if (conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 1247 | MBEDTLS_SSL_DEBUG_MSG(1, ("TLS 1.3 server is not supported yet.")); |
| 1248 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
XiaokangQian | 8f9dfe4 | 2022-04-15 02:52:39 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1252 | MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2.")); |
| 1253 | return 0; |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1254 | } |
| 1255 | #endif |
| 1256 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1257 | MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid.")); |
| 1258 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1259 | } |
| 1260 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1261 | MBEDTLS_CHECK_RETURN_CRITICAL |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1262 | static int ssl_conf_check(const mbedtls_ssl_context *ssl) |
| 1263 | { |
| 1264 | int ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1265 | ret = ssl_conf_version_check(ssl); |
| 1266 | if (ret != 0) { |
| 1267 | return ret; |
| 1268 | } |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1269 | |
Jerry Yu | def7ae4 | 2022-10-30 14:13:19 +0800 | [diff] [blame] | 1270 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 1271 | /* RFC 8446 section 4.4.3 |
| 1272 | * |
| 1273 | * If the verification fails, the receiver MUST terminate the handshake with |
| 1274 | * a "decrypt_error" alert. |
| 1275 | * |
| 1276 | * If the client is configured as TLS 1.3 only with optional verify, return |
| 1277 | * bad config. |
| 1278 | * |
| 1279 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1280 | if (mbedtls_ssl_conf_tls13_ephemeral_enabled( |
| 1281 | (mbedtls_ssl_context *) ssl) && |
Jerry Yu | def7ae4 | 2022-10-30 14:13:19 +0800 | [diff] [blame] | 1282 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 1283 | ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 && |
| 1284 | ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1285 | ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) { |
Jerry Yu | def7ae4 | 2022-10-30 14:13:19 +0800 | [diff] [blame] | 1286 | MBEDTLS_SSL_DEBUG_MSG( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1287 | 1, ("Optional verify auth mode " |
| 1288 | "is not available for TLS 1.3 client")); |
| 1289 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
Jerry Yu | def7ae4 | 2022-10-30 14:13:19 +0800 | [diff] [blame] | 1290 | } |
| 1291 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 1292 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1293 | /* Space for further checks */ |
| 1294 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1295 | return 0; |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1296 | } |
| 1297 | |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1298 | /* |
| 1299 | * Setup an SSL context |
| 1300 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 1301 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1302 | int mbedtls_ssl_setup(mbedtls_ssl_context *ssl, |
| 1303 | const mbedtls_ssl_config *conf) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1304 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1305 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1306 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1307 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1308 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 1309 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1310 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1311 | if ((ret = ssl_conf_check(ssl)) != 0) { |
| 1312 | return ret; |
| 1313 | } |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 1314 | |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1315 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 1316 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 1317 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 1318 | |
| 1319 | /* Set to NULL in case of an error condition */ |
| 1320 | ssl->out_buf = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1321 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1322 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1323 | ssl->in_buf_len = in_buf_len; |
| 1324 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1325 | ssl->in_buf = mbedtls_calloc(1, in_buf_len); |
| 1326 | if (ssl->in_buf == NULL) { |
| 1327 | 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] | 1328 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1329 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1330 | } |
| 1331 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1332 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1333 | ssl->out_buf_len = out_buf_len; |
| 1334 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1335 | ssl->out_buf = mbedtls_calloc(1, out_buf_len); |
| 1336 | if (ssl->out_buf == NULL) { |
| 1337 | 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] | 1338 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1339 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1342 | mbedtls_ssl_reset_in_out_pointers(ssl); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 1343 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1344 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1345 | memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info)); |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1346 | #endif |
| 1347 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1348 | if ((ret = ssl_handshake_init(ssl)) != 0) { |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1349 | goto error; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1350 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1352 | return 0; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1353 | |
| 1354 | error: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1355 | mbedtls_free(ssl->in_buf); |
| 1356 | mbedtls_free(ssl->out_buf); |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1357 | |
| 1358 | ssl->conf = NULL; |
| 1359 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1360 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1361 | ssl->in_buf_len = 0; |
| 1362 | ssl->out_buf_len = 0; |
| 1363 | #endif |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1364 | ssl->in_buf = NULL; |
| 1365 | ssl->out_buf = NULL; |
| 1366 | |
| 1367 | ssl->in_hdr = NULL; |
| 1368 | ssl->in_ctr = NULL; |
| 1369 | ssl->in_len = NULL; |
| 1370 | ssl->in_iv = NULL; |
| 1371 | ssl->in_msg = NULL; |
| 1372 | |
| 1373 | ssl->out_hdr = NULL; |
| 1374 | ssl->out_ctr = NULL; |
| 1375 | ssl->out_len = NULL; |
| 1376 | ssl->out_iv = NULL; |
| 1377 | ssl->out_msg = NULL; |
| 1378 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1379 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
| 1382 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1383 | * Reset an initialized and used SSL context for re-use while retaining |
| 1384 | * all application-set variables, function pointers and data. |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1385 | * |
| 1386 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 1387 | * (Use when a DTLS client reconnects from the same port.) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1388 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1389 | void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl, |
| 1390 | int partial) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1391 | { |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1392 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1393 | size_t in_buf_len = ssl->in_buf_len; |
| 1394 | size_t out_buf_len = ssl->out_buf_len; |
| 1395 | #else |
| 1396 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1397 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 1398 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1399 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1400 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C) |
| 1401 | partial = 0; |
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 1402 | #endif |
| 1403 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1404 | /* Cancel any possibly running timer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1405 | mbedtls_ssl_set_timer(ssl, 0); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1406 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1407 | mbedtls_ssl_reset_in_out_pointers(ssl); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1408 | |
| 1409 | /* Reset incoming message parsing */ |
| 1410 | ssl->in_offt = NULL; |
| 1411 | ssl->nb_zero = 0; |
| 1412 | ssl->in_msgtype = 0; |
| 1413 | ssl->in_msglen = 0; |
| 1414 | ssl->in_hslen = 0; |
| 1415 | ssl->keep_current_message = 0; |
| 1416 | ssl->transform_in = NULL; |
| 1417 | |
| 1418 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1419 | ssl->next_record_offset = 0; |
| 1420 | ssl->in_epoch = 0; |
| 1421 | #endif |
| 1422 | |
| 1423 | /* Keep current datagram if partial == 1 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1424 | if (partial == 0) { |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1425 | ssl->in_left = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1426 | memset(ssl->in_buf, 0, in_buf_len); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1427 | } |
| 1428 | |
Ronald Cron | ad8c17b | 2022-06-10 17:18:09 +0200 | [diff] [blame] | 1429 | ssl->send_alert = 0; |
| 1430 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1431 | /* Reset outgoing message writing */ |
| 1432 | ssl->out_msgtype = 0; |
| 1433 | ssl->out_msglen = 0; |
| 1434 | ssl->out_left = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1435 | memset(ssl->out_buf, 0, out_buf_len); |
| 1436 | memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr)); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1437 | ssl->transform_out = NULL; |
| 1438 | |
| 1439 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1440 | mbedtls_ssl_dtls_replay_reset(ssl); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1441 | #endif |
| 1442 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1443 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1444 | if (ssl->transform) { |
| 1445 | mbedtls_ssl_transform_free(ssl->transform); |
| 1446 | mbedtls_free(ssl->transform); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1447 | ssl->transform = NULL; |
| 1448 | } |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1449 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 1450 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1451 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1452 | mbedtls_ssl_transform_free(ssl->transform_application); |
| 1453 | mbedtls_free(ssl->transform_application); |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1454 | ssl->transform_application = NULL; |
| 1455 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1456 | if (ssl->handshake != NULL) { |
Jerry Yu | 3d9b590 | 2022-11-04 14:07:25 +0800 | [diff] [blame] | 1457 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1458 | mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata); |
| 1459 | mbedtls_free(ssl->handshake->transform_earlydata); |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1460 | ssl->handshake->transform_earlydata = NULL; |
Jerry Yu | 3d9b590 | 2022-11-04 14:07:25 +0800 | [diff] [blame] | 1461 | #endif |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1462 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1463 | mbedtls_ssl_transform_free(ssl->handshake->transform_handshake); |
| 1464 | mbedtls_free(ssl->handshake->transform_handshake); |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1465 | ssl->handshake->transform_handshake = NULL; |
| 1466 | } |
| 1467 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1468 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1469 | } |
| 1470 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1471 | int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial) |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1472 | { |
| 1473 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1474 | |
| 1475 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 1476 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1477 | mbedtls_ssl_session_reset_msg_layer(ssl, partial); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1478 | |
| 1479 | /* Reset renegotiation state */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1480 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1481 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1482 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1483 | |
| 1484 | ssl->verify_data_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1485 | memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN); |
| 1486 | 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] | 1487 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1488 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1489 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1490 | ssl->session_in = NULL; |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 1491 | ssl->session_out = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1492 | if (ssl->session) { |
| 1493 | mbedtls_ssl_session_free(ssl->session); |
| 1494 | mbedtls_free(ssl->session); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1495 | ssl->session = NULL; |
| 1496 | } |
| 1497 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1498 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 1499 | ssl->alpn_chosen = NULL; |
| 1500 | #endif |
| 1501 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1502 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 1503 | int free_cli_id = 1; |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1504 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1505 | free_cli_id = (partial == 0); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1506 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1507 | if (free_cli_id) { |
| 1508 | mbedtls_free(ssl->cli_id); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1509 | ssl->cli_id = NULL; |
| 1510 | ssl->cli_id_len = 0; |
| 1511 | } |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 1512 | #endif |
| 1513 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1514 | if ((ret = ssl_handshake_init(ssl)) != 0) { |
| 1515 | return ret; |
| 1516 | } |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1517 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1518 | return 0; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 1521 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1522 | * Reset an initialized and used SSL context for re-use while retaining |
| 1523 | * all application-set variables, function pointers and data. |
| 1524 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1525 | int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1526 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1527 | return mbedtls_ssl_session_reset_int(ssl, 0); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1531 | * SSL set accessors |
| 1532 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1533 | void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1534 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1535 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1538 | 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] | 1539 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1540 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 1541 | } |
| 1542 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1543 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1544 | 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] | 1545 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1546 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 1547 | } |
| 1548 | #endif |
| 1549 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1550 | 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] | 1551 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1552 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1553 | } |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1554 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1555 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1556 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1557 | void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl, |
| 1558 | unsigned allow_packing) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1559 | { |
| 1560 | ssl->disable_datagram_packing = !allow_packing; |
| 1561 | } |
| 1562 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1563 | void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf, |
| 1564 | uint32_t min, uint32_t max) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1565 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1566 | conf->hs_timeout_min = min; |
| 1567 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1568 | } |
| 1569 | #endif |
| 1570 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1571 | void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1572 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1573 | conf->authmode = authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1574 | } |
| 1575 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1576 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1577 | void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf, |
| 1578 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 1579 | void *p_vrfy) |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1580 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1581 | conf->f_vrfy = f_vrfy; |
| 1582 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1583 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1584 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1585 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1586 | void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf, |
| 1587 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1588 | void *p_rng) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1589 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1590 | conf->f_rng = f_rng; |
| 1591 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1594 | void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf, |
| 1595 | void (*f_dbg)(void *, int, const char *, int, const char *), |
| 1596 | void *p_dbg) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1597 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1598 | conf->f_dbg = f_dbg; |
| 1599 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1600 | } |
| 1601 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1602 | void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl, |
| 1603 | void *p_bio, |
| 1604 | mbedtls_ssl_send_t *f_send, |
| 1605 | mbedtls_ssl_recv_t *f_recv, |
| 1606 | mbedtls_ssl_recv_timeout_t *f_recv_timeout) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1607 | { |
| 1608 | ssl->p_bio = p_bio; |
| 1609 | ssl->f_send = f_send; |
| 1610 | ssl->f_recv = f_recv; |
| 1611 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 1612 | } |
| 1613 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 1614 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1615 | 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] | 1616 | { |
| 1617 | ssl->mtu = mtu; |
| 1618 | } |
| 1619 | #endif |
| 1620 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1621 | 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] | 1622 | { |
| 1623 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1624 | } |
| 1625 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1626 | void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl, |
| 1627 | void *p_timer, |
| 1628 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 1629 | mbedtls_ssl_get_timer_t *f_get_timer) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1630 | { |
| 1631 | ssl->p_timer = p_timer; |
| 1632 | ssl->f_set_timer = f_set_timer; |
| 1633 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1634 | |
| 1635 | /* Make sure we start with no timer running */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1636 | mbedtls_ssl_set_timer(ssl, 0); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1637 | } |
| 1638 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1639 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1640 | void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf, |
| 1641 | void *p_cache, |
| 1642 | mbedtls_ssl_cache_get_t *f_get_cache, |
| 1643 | mbedtls_ssl_cache_set_t *f_set_cache) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1644 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 1645 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1646 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1647 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1648 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1649 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1650 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1651 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1652 | 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] | 1653 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1654 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1655 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1656 | if (ssl == NULL || |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1657 | session == NULL || |
| 1658 | ssl->session_negotiate == NULL || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1659 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) { |
| 1660 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1661 | } |
| 1662 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1663 | if (ssl->handshake->resume == 1) { |
| 1664 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 1665 | } |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 1666 | |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1667 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1668 | if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1669 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1670 | mbedtls_ssl_ciphersuite_from_id(session->ciphersuite); |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1671 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1672 | if (mbedtls_ssl_validate_ciphersuite( |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1673 | ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1674 | MBEDTLS_SSL_VERSION_TLS1_3) != 0) { |
| 1675 | MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.", |
| 1676 | session->ciphersuite)); |
| 1677 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1678 | } |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1679 | } |
Jerry Yu | 2109206 | 2022-10-10 21:21:31 +0800 | [diff] [blame] | 1680 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | 40afab6 | 2022-10-08 10:42:13 +0800 | [diff] [blame] | 1681 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1682 | if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate, |
| 1683 | session)) != 0) { |
| 1684 | return ret; |
| 1685 | } |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1686 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1687 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1688 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1689 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1690 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1691 | #endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1692 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1693 | void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf, |
| 1694 | const int *ciphersuites) |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1695 | { |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 1696 | conf->ciphersuite_list = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1699 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1700 | void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf, |
| 1701 | const int kex_modes) |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1702 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1703 | 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] | 1704 | } |
Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame] | 1705 | |
| 1706 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1707 | void mbedtls_ssl_tls13_conf_early_data(mbedtls_ssl_config *conf, |
| 1708 | int early_data_enabled) |
Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame] | 1709 | { |
| 1710 | conf->early_data_enabled = early_data_enabled; |
| 1711 | } |
Jerry Yu | cc4e007 | 2022-11-22 17:22:22 +0800 | [diff] [blame] | 1712 | |
| 1713 | #if defined(MBEDTLS_SSL_SRV_C) |
| 1714 | void mbedtls_ssl_tls13_conf_max_early_data_size( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1715 | mbedtls_ssl_config *conf, uint32_t max_early_data_size) |
Jerry Yu | cc4e007 | 2022-11-22 17:22:22 +0800 | [diff] [blame] | 1716 | { |
Jerry Yu | 39da985 | 2022-12-06 16:58:36 +0800 | [diff] [blame] | 1717 | conf->max_early_data_size = max_early_data_size; |
Jerry Yu | cc4e007 | 2022-11-22 17:22:22 +0800 | [diff] [blame] | 1718 | } |
| 1719 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 1720 | |
Xiaokang Qian | 72de95d | 2022-10-25 02:54:33 +0000 | [diff] [blame] | 1721 | #endif /* MBEDTLS_SSL_EARLY_DATA */ |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1722 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1723 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1724 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1725 | void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf, |
| 1726 | const mbedtls_x509_crt_profile *profile) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1727 | { |
| 1728 | conf->cert_profile = profile; |
| 1729 | } |
| 1730 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1731 | static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert) |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1732 | { |
| 1733 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
| 1734 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1735 | while (cur != NULL) { |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1736 | next = cur->next; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1737 | mbedtls_free(cur); |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1738 | cur = next; |
| 1739 | } |
| 1740 | } |
| 1741 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1742 | /* Append a new keycert entry to a (possibly empty) list */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 1743 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1744 | static int ssl_append_key_cert(mbedtls_ssl_key_cert **head, |
| 1745 | mbedtls_x509_crt *cert, |
| 1746 | mbedtls_pk_context *key) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1747 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1748 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1749 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1750 | if (cert == NULL) { |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1751 | /* Free list if cert is null */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1752 | ssl_key_cert_free(*head); |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1753 | *head = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1754 | return 0; |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1755 | } |
| 1756 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1757 | new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert)); |
| 1758 | if (new_cert == NULL) { |
| 1759 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 1760 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1761 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1762 | new_cert->cert = cert; |
| 1763 | new_cert->key = key; |
| 1764 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1765 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1766 | /* Update head if the list was null, else add to the end */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1767 | if (*head == NULL) { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1768 | *head = new_cert; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1769 | } else { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1770 | mbedtls_ssl_key_cert *cur = *head; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1771 | while (cur->next != NULL) { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1772 | cur = cur->next; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1773 | } |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1774 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1775 | } |
| 1776 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1777 | return 0; |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1778 | } |
| 1779 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1780 | int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1781 | mbedtls_x509_crt *own_cert, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1782 | mbedtls_pk_context *pk_key) |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1783 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1784 | 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] | 1785 | } |
| 1786 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1787 | void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1788 | mbedtls_x509_crt *ca_chain, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1789 | mbedtls_x509_crl *ca_crl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1790 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1791 | conf->ca_chain = ca_chain; |
| 1792 | conf->ca_crl = ca_crl; |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1793 | |
| 1794 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1795 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1796 | * cannot be used together. */ |
| 1797 | conf->f_ca_cb = NULL; |
| 1798 | conf->p_ca_cb = NULL; |
| 1799 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1800 | } |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1801 | |
| 1802 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1803 | void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf, |
| 1804 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
| 1805 | void *p_ca_cb) |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1806 | { |
| 1807 | conf->f_ca_cb = f_ca_cb; |
| 1808 | conf->p_ca_cb = p_ca_cb; |
| 1809 | |
| 1810 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1811 | * cannot be used together. */ |
| 1812 | conf->ca_chain = NULL; |
| 1813 | conf->ca_crl = NULL; |
| 1814 | } |
| 1815 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1816 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 1817 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1818 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1819 | const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl, |
| 1820 | size_t *name_len) |
Glenn Strauss | 6989407 | 2022-01-24 12:58:00 -0500 | [diff] [blame] | 1821 | { |
| 1822 | *name_len = ssl->handshake->sni_name_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1823 | return ssl->handshake->sni_name; |
Glenn Strauss | 6989407 | 2022-01-24 12:58:00 -0500 | [diff] [blame] | 1824 | } |
| 1825 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1826 | int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl, |
| 1827 | mbedtls_x509_crt *own_cert, |
| 1828 | mbedtls_pk_context *pk_key) |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1829 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1830 | return ssl_append_key_cert(&ssl->handshake->sni_key_cert, |
| 1831 | own_cert, pk_key); |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1832 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 1833 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1834 | void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl, |
| 1835 | mbedtls_x509_crt *ca_chain, |
| 1836 | mbedtls_x509_crl *ca_crl) |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 1837 | { |
| 1838 | ssl->handshake->sni_ca_chain = ca_chain; |
| 1839 | ssl->handshake->sni_ca_crl = ca_crl; |
| 1840 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1841 | |
Glenn Strauss | 999ef70 | 2022-03-11 01:37:23 -0500 | [diff] [blame] | 1842 | #if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1843 | void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl, |
| 1844 | const mbedtls_x509_crt *crt) |
Glenn Strauss | 999ef70 | 2022-03-11 01:37:23 -0500 | [diff] [blame] | 1845 | { |
| 1846 | ssl->handshake->dn_hints = crt; |
| 1847 | } |
| 1848 | #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */ |
| 1849 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1850 | void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl, |
| 1851 | int authmode) |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1852 | { |
| 1853 | ssl->handshake->sni_authmode = authmode; |
| 1854 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1855 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 1856 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1857 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1858 | void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl, |
| 1859 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 1860 | void *p_vrfy) |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1861 | { |
| 1862 | ssl->f_vrfy = f_vrfy; |
| 1863 | ssl->p_vrfy = p_vrfy; |
| 1864 | } |
| 1865 | #endif |
| 1866 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1867 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1868 | |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1869 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1870 | static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1871 | mbedtls_ssl_context *ssl, |
| 1872 | mbedtls_svc_key_id_t pwd) |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1873 | { |
| 1874 | psa_status_t status; |
| 1875 | psa_pake_role_t psa_role; |
| 1876 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 1877 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1878 | psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE); |
| 1879 | psa_pake_cs_set_primitive(&cipher_suite, |
| 1880 | PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, |
| 1881 | PSA_ECC_FAMILY_SECP_R1, |
| 1882 | 256)); |
| 1883 | psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256); |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1884 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1885 | status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite); |
| 1886 | if (status != PSA_SUCCESS) { |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1887 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1888 | } |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1889 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1890 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1891 | psa_role = PSA_PAKE_ROLE_SERVER; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1892 | } else { |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1893 | psa_role = PSA_PAKE_ROLE_CLIENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1894 | } |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1895 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1896 | status = psa_pake_set_role(&ssl->handshake->psa_pake_ctx, psa_role); |
| 1897 | if (status != PSA_SUCCESS) { |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1898 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1899 | } |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1900 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1901 | status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd); |
| 1902 | if (status != PSA_SUCCESS) { |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1903 | return status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1904 | } |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1905 | |
| 1906 | ssl->handshake->psa_pake_ctx_is_ok = 1; |
| 1907 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1908 | return PSA_SUCCESS; |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1909 | } |
| 1910 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1911 | int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl, |
| 1912 | const unsigned char *pw, |
| 1913 | size_t pw_len) |
Valerio Setti | 016f682 | 2022-12-09 14:17:50 +0100 | [diff] [blame] | 1914 | { |
| 1915 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1916 | psa_status_t status; |
| 1917 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1918 | if (ssl->handshake == NULL || ssl->conf == NULL) { |
| 1919 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1920 | } |
| 1921 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1922 | /* Empty password is not valid */ |
| 1923 | if ((pw == NULL) || (pw_len == 0)) { |
| 1924 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 1925 | } |
| 1926 | |
| 1927 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 1928 | psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE); |
| 1929 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 1930 | |
| 1931 | status = psa_import_key(&attributes, pw, pw_len, |
| 1932 | &ssl->handshake->psa_pake_password); |
| 1933 | if (status != PSA_SUCCESS) { |
| 1934 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 1935 | } |
| 1936 | |
| 1937 | status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, |
| 1938 | ssl->handshake->psa_pake_password); |
| 1939 | if (status != PSA_SUCCESS) { |
| 1940 | psa_destroy_key(ssl->handshake->psa_pake_password); |
| 1941 | psa_pake_abort(&ssl->handshake->psa_pake_ctx); |
| 1942 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 1943 | } |
| 1944 | |
| 1945 | return 0; |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 1946 | } |
Valerio Setti | a9a97dc | 2022-11-28 18:26:16 +0100 | [diff] [blame] | 1947 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1948 | int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl, |
| 1949 | mbedtls_svc_key_id_t pwd) |
Valerio Setti | a9a97dc | 2022-11-28 18:26:16 +0100 | [diff] [blame] | 1950 | { |
Valerio Setti | a9a97dc | 2022-11-28 18:26:16 +0100 | [diff] [blame] | 1951 | psa_status_t status; |
| 1952 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1953 | if (ssl->handshake == NULL || ssl->conf == NULL) { |
| 1954 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 1955 | } |
| 1956 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1957 | if (mbedtls_svc_key_id_is_null(pwd)) { |
| 1958 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 1959 | } |
| 1960 | |
| 1961 | status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd); |
| 1962 | if (status != PSA_SUCCESS) { |
| 1963 | psa_pake_abort(&ssl->handshake->psa_pake_ctx); |
| 1964 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 1965 | } |
| 1966 | |
| 1967 | return 0; |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 1968 | } |
| 1969 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1970 | int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl, |
| 1971 | const unsigned char *pw, |
| 1972 | size_t pw_len) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1973 | { |
| 1974 | mbedtls_ecjpake_role role; |
| 1975 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1976 | if (ssl->handshake == NULL || ssl->conf == NULL) { |
| 1977 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 1978 | } |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1979 | |
Valerio Setti | c689ed8 | 2022-12-07 14:40:38 +0100 | [diff] [blame] | 1980 | /* Empty password is not valid */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1981 | if ((pw == NULL) || (pw_len == 0)) { |
| 1982 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 1983 | } |
Valerio Setti | c689ed8 | 2022-12-07 14:40:38 +0100 | [diff] [blame] | 1984 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1985 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1986 | role = MBEDTLS_ECJPAKE_SERVER; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1987 | } else { |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1988 | role = MBEDTLS_ECJPAKE_CLIENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1989 | } |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1990 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1991 | return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx, |
| 1992 | role, |
| 1993 | MBEDTLS_MD_SHA256, |
| 1994 | MBEDTLS_ECP_DP_SECP256R1, |
| 1995 | pw, pw_len); |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1996 | } |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 1997 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1998 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1999 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 2000 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2001 | int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2002 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2003 | if (conf->psk_identity == NULL || |
| 2004 | conf->psk_identity_len == 0) { |
| 2005 | return 0; |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 2006 | } |
| 2007 | |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2008 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2009 | if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) { |
| 2010 | return 1; |
| 2011 | } |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2012 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Ronald Cron | d29e13e | 2022-10-19 10:33:48 +0200 | [diff] [blame] | 2013 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2014 | if (conf->psk != NULL && conf->psk_len != 0) { |
| 2015 | return 1; |
| 2016 | } |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2017 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2018 | return 0; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2019 | } |
| 2020 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2021 | static void ssl_conf_remove_psk(mbedtls_ssl_config *conf) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2022 | { |
| 2023 | /* Remove reference to existing PSK, if any. */ |
| 2024 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2025 | if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) { |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2026 | /* The maintenance of the PSK key slot is the |
| 2027 | * user's responsibility. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2028 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2029 | } |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2030 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2031 | if (conf->psk != NULL) { |
| 2032 | mbedtls_platform_zeroize(conf->psk, conf->psk_len); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2033 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2034 | mbedtls_free(conf->psk); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2035 | conf->psk = NULL; |
| 2036 | conf->psk_len = 0; |
| 2037 | } |
| 2038 | |
| 2039 | /* Remove reference to PSK identity, if any. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2040 | if (conf->psk_identity != NULL) { |
| 2041 | mbedtls_free(conf->psk_identity); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2042 | conf->psk_identity = NULL; |
| 2043 | conf->psk_identity_len = 0; |
| 2044 | } |
| 2045 | } |
| 2046 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2047 | /* This function assumes that PSK identity in the SSL config is unset. |
| 2048 | * It checks that the provided identity is well-formed and attempts |
| 2049 | * to make a copy of it in the SSL config. |
| 2050 | * On failure, the PSK identity in the config remains unset. */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 2051 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2052 | static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf, |
| 2053 | unsigned char const *psk_identity, |
| 2054 | size_t psk_identity_len) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 2055 | { |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 2056 | /* Identity len will be encoded on two bytes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2057 | if (psk_identity == NULL || |
Ronald Cron | 2a87e9b | 2022-10-19 10:55:26 +0200 | [diff] [blame] | 2058 | psk_identity_len == 0 || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2059 | (psk_identity_len >> 16) != 0 || |
| 2060 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) { |
| 2061 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 2062 | } |
| 2063 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2064 | conf->psk_identity = mbedtls_calloc(1, psk_identity_len); |
| 2065 | if (conf->psk_identity == NULL) { |
| 2066 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2067 | } |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 2068 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 2069 | conf->psk_identity_len = psk_identity_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2070 | memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len); |
Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 2071 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2072 | return 0; |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 2073 | } |
| 2074 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2075 | int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf, |
| 2076 | const unsigned char *psk, size_t psk_len, |
| 2077 | const unsigned char *psk_identity, size_t psk_identity_len) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2078 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2079 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2080 | |
| 2081 | /* We currently only support one PSK, raw or opaque. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2082 | if (mbedtls_ssl_conf_has_static_psk(conf)) { |
| 2083 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 2084 | } |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2085 | |
| 2086 | /* Check and set raw PSK */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2087 | if (psk == NULL) { |
| 2088 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2089 | } |
| 2090 | if (psk_len == 0) { |
| 2091 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2092 | } |
| 2093 | if (psk_len > MBEDTLS_PSK_MAX_LEN) { |
| 2094 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2095 | } |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 2096 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2097 | if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) { |
| 2098 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2099 | } |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2100 | conf->psk_len = psk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2101 | memcpy(conf->psk, psk, conf->psk_len); |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2102 | |
| 2103 | /* Check and set PSK Identity */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2104 | ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len); |
| 2105 | if (ret != 0) { |
| 2106 | ssl_conf_remove_psk(conf); |
| 2107 | } |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2108 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2109 | return ret; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2110 | } |
| 2111 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2112 | static void ssl_remove_psk(mbedtls_ssl_context *ssl) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2113 | { |
| 2114 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2115 | if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2116 | /* The maintenance of the external PSK key slot is the |
| 2117 | * user's responsibility. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2118 | if (ssl->handshake->psk_opaque_is_internal) { |
| 2119 | psa_destroy_key(ssl->handshake->psk_opaque); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2120 | ssl->handshake->psk_opaque_is_internal = 0; |
| 2121 | } |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2122 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2123 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 2124 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2125 | if (ssl->handshake->psk != NULL) { |
| 2126 | mbedtls_platform_zeroize(ssl->handshake->psk, |
| 2127 | ssl->handshake->psk_len); |
| 2128 | mbedtls_free(ssl->handshake->psk); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2129 | ssl->handshake->psk_len = 0; |
| 2130 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 2131 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2132 | } |
| 2133 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2134 | int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl, |
| 2135 | const unsigned char *psk, size_t psk_len) |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2136 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2137 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2138 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
Jerry Yu | 5d01c05 | 2022-08-17 10:18:10 +0800 | [diff] [blame] | 2139 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 24b8c81 | 2022-08-20 19:06:56 +0800 | [diff] [blame] | 2140 | psa_algorithm_t alg = PSA_ALG_NONE; |
Jerry Yu | 5d01c05 | 2022-08-17 10:18:10 +0800 | [diff] [blame] | 2141 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2142 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2143 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2144 | if (psk == NULL || ssl->handshake == NULL) { |
| 2145 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2146 | } |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2147 | |
Ronald Cron | 0a1c504 | 2023-02-20 10:44:22 +0100 | [diff] [blame^] | 2148 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2149 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 && |
| 2150 | psk_len > MBEDTLS_PSK_MAX_LEN) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2151 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2152 | } |
Ronald Cron | 0a1c504 | 2023-02-20 10:44:22 +0100 | [diff] [blame^] | 2153 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2154 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2155 | ssl_remove_psk(ssl); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2156 | |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2157 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 2158 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2159 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) { |
| 2160 | if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) { |
| 2161 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
| 2162 | } else { |
| 2163 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 2164 | } |
| 2165 | psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE); |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 2166 | } |
| 2167 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2168 | |
Jerry Yu | 568ec25 | 2022-07-22 21:27:34 +0800 | [diff] [blame] | 2169 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2170 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
| 2171 | alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH); |
| 2172 | psa_set_key_usage_flags(&key_attributes, |
| 2173 | PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT); |
Jerry Yu | ccc68a4 | 2022-07-26 16:39:20 +0800 | [diff] [blame] | 2174 | } |
| 2175 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 2176 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2177 | psa_set_key_algorithm(&key_attributes, alg); |
| 2178 | psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2179 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2180 | status = psa_import_key(&key_attributes, psk, psk_len, &key); |
| 2181 | if (status != PSA_SUCCESS) { |
| 2182 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 2183 | } |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2184 | |
| 2185 | /* Allow calling psa_destroy_key() on psk remove */ |
| 2186 | ssl->handshake->psk_opaque_is_internal = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2187 | return mbedtls_ssl_set_hs_psk_opaque(ssl, key); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2188 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2189 | if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) { |
| 2190 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2191 | } |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2192 | |
| 2193 | ssl->handshake->psk_len = psk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2194 | memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2195 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2196 | return 0; |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 2197 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2198 | } |
| 2199 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2200 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2201 | int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf, |
| 2202 | mbedtls_svc_key_id_t psk, |
| 2203 | const unsigned char *psk_identity, |
| 2204 | size_t psk_identity_len) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2205 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2206 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 2207 | |
| 2208 | /* We currently only support one PSK, raw or opaque. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2209 | if (mbedtls_ssl_conf_has_static_psk(conf)) { |
| 2210 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 2211 | } |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2212 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2213 | /* Check and set opaque PSK */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2214 | if (mbedtls_svc_key_id_is_null(psk)) { |
| 2215 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2216 | } |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 2217 | conf->psk_opaque = psk; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2218 | |
| 2219 | /* Check and set PSK Identity */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2220 | ret = ssl_conf_set_psk_identity(conf, psk_identity, |
| 2221 | psk_identity_len); |
| 2222 | if (ret != 0) { |
| 2223 | ssl_conf_remove_psk(conf); |
| 2224 | } |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 2225 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2226 | return ret; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2227 | } |
| 2228 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2229 | int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl, |
| 2230 | mbedtls_svc_key_id_t psk) |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2231 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2232 | if ((mbedtls_svc_key_id_is_null(psk)) || |
| 2233 | (ssl->handshake == NULL)) { |
| 2234 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2235 | } |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2236 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2237 | ssl_remove_psk(ssl); |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2238 | ssl->handshake->psk_opaque = psk; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2239 | return 0; |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2240 | } |
| 2241 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2242 | |
Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 2243 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2244 | void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf, |
| 2245 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
| 2246 | size_t), |
| 2247 | void *p_psk) |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2248 | { |
| 2249 | conf->f_psk = f_psk; |
| 2250 | conf->p_psk = p_psk; |
| 2251 | } |
Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 2252 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 2253 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 2254 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 2255 | |
| 2256 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2257 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2258 | psa_algorithm_t alg) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2259 | { |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2260 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2261 | if (alg == PSA_ALG_CBC_NO_PADDING) { |
| 2262 | return MBEDTLS_SSL_MODE_CBC; |
| 2263 | } |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2264 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2265 | if (PSA_ALG_IS_AEAD(alg)) { |
| 2266 | return MBEDTLS_SSL_MODE_AEAD; |
| 2267 | } |
| 2268 | return MBEDTLS_SSL_MODE_STREAM; |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2269 | } |
| 2270 | |
| 2271 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2272 | |
| 2273 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2274 | mbedtls_cipher_mode_t mode) |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2275 | { |
| 2276 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2277 | if (mode == MBEDTLS_MODE_CBC) { |
| 2278 | return MBEDTLS_SSL_MODE_CBC; |
| 2279 | } |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2280 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 2281 | |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2282 | #if defined(MBEDTLS_GCM_C) || \ |
| 2283 | defined(MBEDTLS_CCM_C) || \ |
| 2284 | defined(MBEDTLS_CHACHAPOLY_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2285 | if (mode == MBEDTLS_MODE_GCM || |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2286 | mode == MBEDTLS_MODE_CCM || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2287 | mode == MBEDTLS_MODE_CHACHAPOLY) { |
| 2288 | return MBEDTLS_SSL_MODE_AEAD; |
| 2289 | } |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2290 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2291 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2292 | return MBEDTLS_SSL_MODE_STREAM; |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2293 | } |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 2294 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 2295 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2296 | static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode( |
| 2297 | mbedtls_ssl_mode_t base_mode, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2298 | int encrypt_then_mac) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2299 | { |
| 2300 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2301 | if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED && |
| 2302 | base_mode == MBEDTLS_SSL_MODE_CBC) { |
| 2303 | return MBEDTLS_SSL_MODE_CBC_ETM; |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2304 | } |
| 2305 | #else |
| 2306 | (void) encrypt_then_mac; |
| 2307 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2308 | return base_mode; |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2309 | } |
| 2310 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 2311 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2312 | const mbedtls_ssl_transform *transform) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2313 | { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2314 | mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode( |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2315 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2316 | transform->psa_alg |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2317 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2318 | mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2319 | #endif |
| 2320 | ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2321 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2322 | int encrypt_then_mac = 0; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2323 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2324 | encrypt_then_mac = transform->encrypt_then_mac; |
| 2325 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2326 | return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2327 | } |
| 2328 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 2329 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2330 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2331 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 2332 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2333 | const mbedtls_ssl_ciphersuite_t *suite) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2334 | { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2335 | mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM; |
| 2336 | |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2337 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2338 | psa_status_t status; |
| 2339 | psa_algorithm_t alg; |
| 2340 | psa_key_type_t type; |
| 2341 | size_t size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2342 | status = mbedtls_ssl_cipher_to_psa(suite->cipher, 0, &alg, &type, &size); |
| 2343 | if (status == PSA_SUCCESS) { |
| 2344 | base_mode = mbedtls_ssl_get_base_mode(alg); |
| 2345 | } |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2346 | #else |
| 2347 | const mbedtls_cipher_info_t *cipher = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2348 | mbedtls_cipher_info_from_type(suite->cipher); |
| 2349 | if (cipher != NULL) { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2350 | base_mode = |
| 2351 | mbedtls_ssl_get_base_mode( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2352 | mbedtls_cipher_info_get_mode(cipher)); |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2353 | } |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2354 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2355 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 2356 | #if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 2357 | int encrypt_then_mac = 0; |
| 2358 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2359 | return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 2360 | } |
| 2361 | |
Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 2362 | #if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2363 | |
| 2364 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2365 | /* Serialization of TLS 1.3 sessions: |
| 2366 | * |
| 2367 | * struct { |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 2368 | * opaque hostname<0..2^16-1>; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2369 | * uint64 ticket_received; |
| 2370 | * uint32 ticket_lifetime; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2371 | * opaque ticket<1..2^16-1>; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2372 | * } ClientOnlyData; |
| 2373 | * |
| 2374 | * struct { |
| 2375 | * uint8 endpoint; |
| 2376 | * uint8 ciphersuite[2]; |
| 2377 | * uint32 ticket_age_add; |
| 2378 | * uint8 ticket_flags; |
| 2379 | * opaque resumption_key<0..255>; |
| 2380 | * select ( endpoint ) { |
| 2381 | * case client: ClientOnlyData; |
| 2382 | * case server: uint64 start_time; |
| 2383 | * }; |
| 2384 | * } serialized_session_tls13; |
| 2385 | * |
| 2386 | */ |
| 2387 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2388 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2389 | static int ssl_tls13_session_save(const mbedtls_ssl_session *session, |
| 2390 | unsigned char *buf, |
| 2391 | size_t buf_len, |
| 2392 | size_t *olen) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2393 | { |
| 2394 | unsigned char *p = buf; |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 2395 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
| 2396 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2397 | size_t hostname_len = (session->hostname == NULL) ? |
| 2398 | 0 : strlen(session->hostname) + 1; |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 2399 | #endif |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2400 | size_t needed = 1 /* endpoint */ |
| 2401 | + 2 /* ciphersuite */ |
| 2402 | + 4 /* ticket_age_add */ |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2403 | + 1 /* ticket_flags */ |
| 2404 | + 1; /* resumption_key length */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2405 | *olen = 0; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2406 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2407 | if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) { |
| 2408 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2409 | } |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2410 | needed += session->resumption_key_len; /* resumption_key */ |
| 2411 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2412 | #if defined(MBEDTLS_HAVE_TIME) |
| 2413 | needed += 8; /* start_time or ticket_received */ |
| 2414 | #endif |
| 2415 | |
| 2416 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2417 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Xiaokang Qian | 126bf8e | 2022-10-13 02:22:40 +0000 | [diff] [blame] | 2418 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Xiaokang Qian | bc663a0 | 2022-10-09 11:14:39 +0000 | [diff] [blame] | 2419 | needed += 2 /* hostname_len */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2420 | + hostname_len; /* hostname */ |
Xiaokang Qian | 281fd1b | 2022-09-20 11:35:41 +0000 | [diff] [blame] | 2421 | #endif |
| 2422 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2423 | needed += 4 /* ticket_lifetime */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2424 | + 2; /* ticket_len */ |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2425 | |
| 2426 | /* Check size_t overflow */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2427 | if (session->ticket_len > SIZE_MAX - needed) { |
| 2428 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2429 | } |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2430 | |
Jerry Yu | e28d974 | 2022-08-18 15:44:03 +0800 | [diff] [blame] | 2431 | needed += session->ticket_len; /* ticket */ |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2432 | } |
| 2433 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 2434 | |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2435 | *olen = needed; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2436 | if (needed > buf_len) { |
| 2437 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 2438 | } |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2439 | |
| 2440 | p[0] = session->endpoint; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2441 | MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 1); |
| 2442 | MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 3); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2443 | p[7] = session->ticket_flags; |
| 2444 | |
| 2445 | /* save resumption_key */ |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2446 | p[8] = session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2447 | p += 9; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2448 | memcpy(p, session->resumption_key, session->resumption_key_len); |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2449 | p += session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2450 | |
| 2451 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2452 | if (session->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 2453 | MBEDTLS_PUT_UINT64_BE((uint64_t) session->start, p, 0); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2454 | p += 8; |
| 2455 | } |
| 2456 | #endif /* MBEDTLS_HAVE_TIME */ |
| 2457 | |
| 2458 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2459 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2460 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2461 | MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0); |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2462 | p += 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2463 | if (hostname_len > 0) { |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2464 | /* save host name */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2465 | memcpy(p, session->hostname, hostname_len); |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2466 | p += hostname_len; |
| 2467 | } |
| 2468 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 2469 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2470 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2471 | MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_received, p, 0); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2472 | p += 8; |
| 2473 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2474 | MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2475 | p += 4; |
| 2476 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2477 | MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2478 | p += 2; |
Jerry Yu | 3419107 | 2022-08-18 10:32:09 +0800 | [diff] [blame] | 2479 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2480 | if (session->ticket != NULL && session->ticket_len > 0) { |
| 2481 | memcpy(p, session->ticket, session->ticket_len); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2482 | p += session->ticket_len; |
| 2483 | } |
| 2484 | } |
| 2485 | #endif /* MBEDTLS_SSL_CLI_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2486 | return 0; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2487 | } |
| 2488 | |
| 2489 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2490 | static int ssl_tls13_session_load(mbedtls_ssl_session *session, |
| 2491 | const unsigned char *buf, |
| 2492 | size_t len) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2493 | { |
| 2494 | const unsigned char *p = buf; |
| 2495 | const unsigned char *end = buf + len; |
| 2496 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2497 | if (end - p < 9) { |
| 2498 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2499 | } |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2500 | session->endpoint = p[0]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2501 | session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 1); |
| 2502 | session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 3); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2503 | session->ticket_flags = p[7]; |
| 2504 | |
| 2505 | /* load resumption_key */ |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2506 | session->resumption_key_len = p[8]; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2507 | p += 9; |
| 2508 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2509 | if (end - p < session->resumption_key_len) { |
| 2510 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2511 | } |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2512 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2513 | if (sizeof(session->resumption_key) < session->resumption_key_len) { |
| 2514 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2515 | } |
| 2516 | memcpy(session->resumption_key, p, session->resumption_key_len); |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 2517 | p += session->resumption_key_len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2518 | |
| 2519 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2520 | if (session->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 2521 | if (end - p < 8) { |
| 2522 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2523 | } |
| 2524 | session->start = MBEDTLS_GET_UINT64_BE(p, 0); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2525 | p += 8; |
| 2526 | } |
| 2527 | #endif /* MBEDTLS_HAVE_TIME */ |
| 2528 | |
| 2529 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2530 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2531 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2532 | defined(MBEDTLS_SSL_SESSION_TICKETS) |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2533 | size_t hostname_len; |
| 2534 | /* load host name */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2535 | if (end - p < 2) { |
| 2536 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2537 | } |
| 2538 | hostname_len = MBEDTLS_GET_UINT16_BE(p, 0); |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2539 | p += 2; |
| 2540 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2541 | if (end - p < (long int) hostname_len) { |
| 2542 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2543 | } |
| 2544 | if (hostname_len > 0) { |
| 2545 | session->hostname = mbedtls_calloc(1, hostname_len); |
| 2546 | if (session->hostname == NULL) { |
| 2547 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2548 | } |
| 2549 | memcpy(session->hostname, p, hostname_len); |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 2550 | p += hostname_len; |
| 2551 | } |
| 2552 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION && |
| 2553 | MBEDTLS_SSL_SESSION_TICKETS */ |
| 2554 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2555 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2556 | if (end - p < 8) { |
| 2557 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2558 | } |
| 2559 | session->ticket_received = MBEDTLS_GET_UINT64_BE(p, 0); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2560 | p += 8; |
| 2561 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2562 | if (end - p < 4) { |
| 2563 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2564 | } |
| 2565 | session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2566 | p += 4; |
| 2567 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2568 | if (end - p < 2) { |
| 2569 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2570 | } |
| 2571 | session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2572 | p += 2; |
| 2573 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2574 | if (end - p < (long int) session->ticket_len) { |
| 2575 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2576 | } |
| 2577 | if (session->ticket_len > 0) { |
| 2578 | session->ticket = mbedtls_calloc(1, session->ticket_len); |
| 2579 | if (session->ticket == NULL) { |
| 2580 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2581 | } |
| 2582 | memcpy(session->ticket, p, session->ticket_len); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2583 | p += session->ticket_len; |
| 2584 | } |
| 2585 | } |
| 2586 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 2587 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2588 | return 0; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2589 | |
| 2590 | } |
| 2591 | #else /* MBEDTLS_SSL_SESSION_TICKETS */ |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2592 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2593 | static int ssl_tls13_session_save(const mbedtls_ssl_session *session, |
| 2594 | unsigned char *buf, |
| 2595 | size_t buf_len, |
| 2596 | size_t *olen) |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2597 | { |
| 2598 | ((void) session); |
| 2599 | ((void) buf); |
| 2600 | ((void) buf_len); |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 2601 | *olen = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2602 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2603 | } |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2604 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2605 | static int ssl_tls13_session_load(const mbedtls_ssl_session *session, |
| 2606 | unsigned char *buf, |
| 2607 | size_t buf_len) |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2608 | { |
| 2609 | ((void) session); |
| 2610 | ((void) buf); |
| 2611 | ((void) buf_len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2612 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 2613 | } |
| 2614 | #endif /* !MBEDTLS_SSL_SESSION_TICKETS */ |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 2615 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 2616 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2617 | psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type, |
| 2618 | size_t taglen, |
| 2619 | psa_algorithm_t *alg, |
| 2620 | psa_key_type_t *key_type, |
| 2621 | size_t *key_size) |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2622 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2623 | switch (mbedtls_cipher_type) { |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 2624 | case MBEDTLS_CIPHER_AES_128_CBC: |
| 2625 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2626 | *key_type = PSA_KEY_TYPE_AES; |
| 2627 | *key_size = 128; |
| 2628 | break; |
| 2629 | case MBEDTLS_CIPHER_AES_128_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2630 | *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] | 2631 | *key_type = PSA_KEY_TYPE_AES; |
| 2632 | *key_size = 128; |
| 2633 | break; |
| 2634 | case MBEDTLS_CIPHER_AES_128_GCM: |
| 2635 | *alg = PSA_ALG_GCM; |
| 2636 | *key_type = PSA_KEY_TYPE_AES; |
| 2637 | *key_size = 128; |
| 2638 | break; |
| 2639 | case MBEDTLS_CIPHER_AES_192_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2640 | *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] | 2641 | *key_type = PSA_KEY_TYPE_AES; |
| 2642 | *key_size = 192; |
| 2643 | break; |
| 2644 | case MBEDTLS_CIPHER_AES_192_GCM: |
| 2645 | *alg = PSA_ALG_GCM; |
| 2646 | *key_type = PSA_KEY_TYPE_AES; |
| 2647 | *key_size = 192; |
| 2648 | break; |
| 2649 | case MBEDTLS_CIPHER_AES_256_CBC: |
| 2650 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2651 | *key_type = PSA_KEY_TYPE_AES; |
| 2652 | *key_size = 256; |
| 2653 | break; |
| 2654 | case MBEDTLS_CIPHER_AES_256_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2655 | *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] | 2656 | *key_type = PSA_KEY_TYPE_AES; |
| 2657 | *key_size = 256; |
| 2658 | break; |
| 2659 | case MBEDTLS_CIPHER_AES_256_GCM: |
| 2660 | *alg = PSA_ALG_GCM; |
| 2661 | *key_type = PSA_KEY_TYPE_AES; |
| 2662 | *key_size = 256; |
| 2663 | break; |
| 2664 | case MBEDTLS_CIPHER_ARIA_128_CBC: |
| 2665 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2666 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2667 | *key_size = 128; |
| 2668 | break; |
| 2669 | case MBEDTLS_CIPHER_ARIA_128_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2670 | *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] | 2671 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2672 | *key_size = 128; |
| 2673 | break; |
| 2674 | case MBEDTLS_CIPHER_ARIA_128_GCM: |
| 2675 | *alg = PSA_ALG_GCM; |
| 2676 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2677 | *key_size = 128; |
| 2678 | break; |
| 2679 | case MBEDTLS_CIPHER_ARIA_192_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2680 | *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] | 2681 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2682 | *key_size = 192; |
| 2683 | break; |
| 2684 | case MBEDTLS_CIPHER_ARIA_192_GCM: |
| 2685 | *alg = PSA_ALG_GCM; |
| 2686 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2687 | *key_size = 192; |
| 2688 | break; |
| 2689 | case MBEDTLS_CIPHER_ARIA_256_CBC: |
| 2690 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2691 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2692 | *key_size = 256; |
| 2693 | break; |
| 2694 | case MBEDTLS_CIPHER_ARIA_256_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2695 | *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] | 2696 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2697 | *key_size = 256; |
| 2698 | break; |
| 2699 | case MBEDTLS_CIPHER_ARIA_256_GCM: |
| 2700 | *alg = PSA_ALG_GCM; |
| 2701 | *key_type = PSA_KEY_TYPE_ARIA; |
| 2702 | *key_size = 256; |
| 2703 | break; |
| 2704 | case MBEDTLS_CIPHER_CAMELLIA_128_CBC: |
| 2705 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2706 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2707 | *key_size = 128; |
| 2708 | break; |
| 2709 | case MBEDTLS_CIPHER_CAMELLIA_128_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2710 | *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] | 2711 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2712 | *key_size = 128; |
| 2713 | break; |
| 2714 | case MBEDTLS_CIPHER_CAMELLIA_128_GCM: |
| 2715 | *alg = PSA_ALG_GCM; |
| 2716 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2717 | *key_size = 128; |
| 2718 | break; |
| 2719 | case MBEDTLS_CIPHER_CAMELLIA_192_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2720 | *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] | 2721 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2722 | *key_size = 192; |
| 2723 | break; |
| 2724 | case MBEDTLS_CIPHER_CAMELLIA_192_GCM: |
| 2725 | *alg = PSA_ALG_GCM; |
| 2726 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2727 | *key_size = 192; |
| 2728 | break; |
| 2729 | case MBEDTLS_CIPHER_CAMELLIA_256_CBC: |
| 2730 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 2731 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2732 | *key_size = 256; |
| 2733 | break; |
| 2734 | case MBEDTLS_CIPHER_CAMELLIA_256_CCM: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2735 | *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] | 2736 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2737 | *key_size = 256; |
| 2738 | break; |
| 2739 | case MBEDTLS_CIPHER_CAMELLIA_256_GCM: |
| 2740 | *alg = PSA_ALG_GCM; |
| 2741 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 2742 | *key_size = 256; |
| 2743 | break; |
| 2744 | case MBEDTLS_CIPHER_CHACHA20_POLY1305: |
| 2745 | *alg = PSA_ALG_CHACHA20_POLY1305; |
| 2746 | *key_type = PSA_KEY_TYPE_CHACHA20; |
| 2747 | *key_size = 256; |
| 2748 | break; |
| 2749 | case MBEDTLS_CIPHER_NULL: |
| 2750 | *alg = MBEDTLS_SSL_NULL_CIPHER; |
| 2751 | *key_type = 0; |
| 2752 | *key_size = 0; |
| 2753 | break; |
| 2754 | default: |
| 2755 | return PSA_ERROR_NOT_SUPPORTED; |
| 2756 | } |
| 2757 | |
| 2758 | return PSA_SUCCESS; |
| 2759 | } |
Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 2760 | #endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 2761 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2762 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2763 | int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf, |
| 2764 | const unsigned char *dhm_P, size_t P_len, |
| 2765 | const unsigned char *dhm_G, size_t G_len) |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2766 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2767 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2768 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2769 | mbedtls_mpi_free(&conf->dhm_P); |
| 2770 | mbedtls_mpi_free(&conf->dhm_G); |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2771 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2772 | if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 || |
| 2773 | (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) { |
| 2774 | mbedtls_mpi_free(&conf->dhm_P); |
| 2775 | mbedtls_mpi_free(&conf->dhm_G); |
| 2776 | return ret; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2777 | } |
| 2778 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2779 | return 0; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2780 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2781 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2782 | 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] | 2783 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2784 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2785 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2786 | mbedtls_mpi_free(&conf->dhm_P); |
| 2787 | mbedtls_mpi_free(&conf->dhm_G); |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2788 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2789 | if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P, |
| 2790 | &conf->dhm_P)) != 0 || |
| 2791 | (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G, |
| 2792 | &conf->dhm_G)) != 0) { |
| 2793 | mbedtls_mpi_free(&conf->dhm_P); |
| 2794 | mbedtls_mpi_free(&conf->dhm_G); |
| 2795 | return ret; |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 2796 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2797 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2798 | return 0; |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2799 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2800 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2801 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 2802 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 2803 | /* |
| 2804 | * Set the minimum length for Diffie-Hellman parameters |
| 2805 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2806 | void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf, |
| 2807 | unsigned int bitlen) |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 2808 | { |
| 2809 | conf->dhm_min_bitlen = bitlen; |
| 2810 | } |
| 2811 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 2812 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2813 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2814 | #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] | 2815 | /* |
| 2816 | * Set allowed/preferred hashes for handshake signatures |
| 2817 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2818 | void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf, |
| 2819 | const int *hashes) |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 2820 | { |
| 2821 | conf->sig_hashes = hashes; |
| 2822 | } |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2823 | #endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2824 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2825 | /* Configure allowed signature algorithms for handshake */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2826 | void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf, |
| 2827 | const uint16_t *sig_algs) |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2828 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2829 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2830 | conf->sig_hashes = NULL; |
| 2831 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 2832 | conf->sig_algs = sig_algs; |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2833 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2834 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 2835 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 2836 | #if defined(MBEDTLS_ECP_C) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2837 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2838 | /* |
| 2839 | * Set the allowed elliptic curves |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2840 | * |
| 2841 | * mbedtls_ssl_setup() takes the provided list |
| 2842 | * and translates it to a list of IANA TLS group identifiers, |
| 2843 | * stored in ssl->handshake->group_list. |
| 2844 | * |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2845 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2846 | void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf, |
| 2847 | const mbedtls_ecp_group_id *curve_list) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2848 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2849 | conf->curve_list = curve_list; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2850 | conf->group_list = NULL; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2851 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2852 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2853 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2854 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2855 | /* |
| 2856 | * Set the allowed groups |
| 2857 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2858 | void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf, |
| 2859 | const uint16_t *group_list) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2860 | { |
| 2861 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2862 | conf->curve_list = NULL; |
| 2863 | #endif |
| 2864 | conf->group_list = group_list; |
| 2865 | } |
| 2866 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2867 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2868 | int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2869 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2870 | /* Initialize to suppress unnecessary compiler warning */ |
| 2871 | size_t hostname_len = 0; |
| 2872 | |
| 2873 | /* Check if new hostname is valid before |
| 2874 | * making any change to current one */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2875 | if (hostname != NULL) { |
| 2876 | hostname_len = strlen(hostname); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2877 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2878 | if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) { |
| 2879 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2880 | } |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2881 | } |
| 2882 | |
| 2883 | /* Now it's clear that we will overwrite the old hostname, |
| 2884 | * so we can free it safely */ |
| 2885 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2886 | if (ssl->hostname != NULL) { |
| 2887 | mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname)); |
| 2888 | mbedtls_free(ssl->hostname); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2889 | } |
| 2890 | |
| 2891 | /* Passing NULL as hostname shall clear the old one */ |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 2892 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2893 | if (hostname == NULL) { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2894 | ssl->hostname = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2895 | } else { |
| 2896 | ssl->hostname = mbedtls_calloc(1, hostname_len + 1); |
| 2897 | if (ssl->hostname == NULL) { |
| 2898 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2899 | } |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2900 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2901 | memcpy(ssl->hostname, hostname, hostname_len); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2902 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2903 | ssl->hostname[hostname_len] = '\0'; |
| 2904 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2905 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2906 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2907 | } |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 2908 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2909 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2910 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2911 | void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf, |
| 2912 | int (*f_sni)(void *, mbedtls_ssl_context *, |
| 2913 | const unsigned char *, size_t), |
| 2914 | void *p_sni) |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2915 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2916 | conf->f_sni = f_sni; |
| 2917 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2918 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2919 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2920 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2921 | #if defined(MBEDTLS_SSL_ALPN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2922 | 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] | 2923 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2924 | size_t cur_len, tot_len; |
| 2925 | const char **p; |
| 2926 | |
| 2927 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 2928 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 2929 | * MUST NOT be truncated." |
| 2930 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2931 | */ |
| 2932 | tot_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2933 | for (p = protos; *p != NULL; p++) { |
| 2934 | cur_len = strlen(*p); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2935 | tot_len += cur_len; |
| 2936 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2937 | if ((cur_len == 0) || |
| 2938 | (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) || |
| 2939 | (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) { |
| 2940 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 2941 | } |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2942 | } |
| 2943 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2944 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2945 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2946 | return 0; |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2947 | } |
| 2948 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2949 | 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] | 2950 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2951 | return ssl->alpn_chosen; |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2952 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2953 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2954 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2955 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2956 | void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf, |
| 2957 | int support_mki_value) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2958 | { |
| 2959 | conf->dtls_srtp_mki_support = support_mki_value; |
| 2960 | } |
| 2961 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2962 | int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl, |
| 2963 | unsigned char *mki_value, |
| 2964 | uint16_t mki_len) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2965 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2966 | if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) { |
| 2967 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2968 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2969 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2970 | if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) { |
| 2971 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2972 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2973 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2974 | memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len); |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2975 | ssl->dtls_srtp_info.mki_len = mki_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2976 | return 0; |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2977 | } |
| 2978 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2979 | int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf, |
| 2980 | const mbedtls_ssl_srtp_profile *profiles) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2981 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2982 | const mbedtls_ssl_srtp_profile *p; |
| 2983 | size_t list_size = 0; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2984 | |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2985 | /* check the profiles list: all entry must be valid, |
| 2986 | * 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] | 2987 | for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET && |
| 2988 | list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH; |
| 2989 | p++) { |
| 2990 | if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) { |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 2991 | list_size++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2992 | } else { |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 2993 | /* unsupported value, stop parsing and set the size to an error value */ |
| 2994 | list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2995 | } |
| 2996 | } |
| 2997 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2998 | if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) { |
| 2999 | conf->dtls_srtp_profile_list = NULL; |
| 3000 | conf->dtls_srtp_profile_list_len = 0; |
| 3001 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 3002 | } |
| 3003 | |
Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 3004 | conf->dtls_srtp_profile_list = profiles; |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 3005 | conf->dtls_srtp_profile_list_len = list_size; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3006 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3007 | return 0; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3008 | } |
| 3009 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3010 | void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl, |
| 3011 | mbedtls_dtls_srtp_info *dtls_srtp_info) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3012 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 3013 | dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile; |
| 3014 | /* do not copy the mki value if there is no chosen profile */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3015 | if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 3016 | dtls_srtp_info->mki_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3017 | } else { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 3018 | dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3019 | memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value, |
| 3020 | ssl->dtls_srtp_info.mki_len); |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 3021 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3022 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3023 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 3024 | |
Aditya Patwardhan | 3096f33 | 2022-07-26 14:31:46 +0530 | [diff] [blame] | 3025 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3026 | 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] | 3027 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 3028 | conf->max_tls_version = (major << 8) | minor; |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 3029 | } |
| 3030 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3031 | 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] | 3032 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 3033 | conf->min_tls_version = (major << 8) | minor; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 3034 | } |
Aditya Patwardhan | 3096f33 | 2022-07-26 14:31:46 +0530 | [diff] [blame] | 3035 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 3036 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3037 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3038 | void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf, |
| 3039 | char cert_req_ca_list) |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3040 | { |
| 3041 | conf->cert_req_ca_list = cert_req_ca_list; |
| 3042 | } |
| 3043 | #endif |
| 3044 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3045 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3046 | 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] | 3047 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3048 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 3049 | } |
| 3050 | #endif |
| 3051 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3052 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3053 | 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] | 3054 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3055 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 3056 | } |
| 3057 | #endif |
| 3058 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3059 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3060 | 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] | 3061 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3062 | if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
| 3063 | ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) { |
| 3064 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 3065 | } |
| 3066 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 3067 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 3068 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3069 | return 0; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 3070 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3071 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 3072 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3073 | 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] | 3074 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3075 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3076 | } |
| 3077 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3078 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3079 | 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] | 3080 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3081 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 3082 | } |
| 3083 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3084 | 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] | 3085 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3086 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 3087 | } |
| 3088 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3089 | void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf, |
| 3090 | const unsigned char period[8]) |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 3091 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3092 | memcpy(conf->renego_period, period, 8); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 3093 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3094 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3095 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3096 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3097 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3098 | 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] | 3099 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 3100 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 3101 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3102 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 3103 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3104 | #if defined(MBEDTLS_SSL_SRV_C) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 3105 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 3106 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3107 | void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf, |
| 3108 | uint16_t num_tickets) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 3109 | { |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 3110 | conf->new_session_tickets_count = num_tickets; |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 3111 | } |
| 3112 | #endif |
| 3113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3114 | void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf, |
| 3115 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 3116 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 3117 | void *p_ticket) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 3118 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 3119 | conf->f_ticket_write = f_ticket_write; |
| 3120 | conf->f_ticket_parse = f_ticket_parse; |
| 3121 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 3122 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3123 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3124 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 3125 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3126 | void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl, |
| 3127 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 3128 | void *p_export_keys) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 3129 | { |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 3130 | ssl->f_export_keys = f_export_keys; |
| 3131 | ssl->p_export_keys = p_export_keys; |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 3132 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 3133 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3134 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3135 | void mbedtls_ssl_conf_async_private_cb( |
| 3136 | mbedtls_ssl_config *conf, |
| 3137 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 3138 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 3139 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 3140 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3141 | void *async_config_data) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3142 | { |
| 3143 | conf->f_async_sign_start = f_async_sign; |
| 3144 | conf->f_async_decrypt_start = f_async_decrypt; |
| 3145 | conf->f_async_resume = f_async_resume; |
| 3146 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3147 | conf->p_async_config_data = async_config_data; |
| 3148 | } |
| 3149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3150 | 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] | 3151 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3152 | return conf->p_async_config_data; |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3153 | } |
| 3154 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3155 | void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3156 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3157 | if (ssl->handshake == NULL) { |
| 3158 | return NULL; |
| 3159 | } else { |
| 3160 | return ssl->handshake->user_async_ctx; |
| 3161 | } |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3162 | } |
| 3163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3164 | void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl, |
| 3165 | void *ctx) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3166 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3167 | if (ssl->handshake != NULL) { |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3168 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3169 | } |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3170 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 3171 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 3172 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3173 | /* |
| 3174 | * SSL get accessors |
| 3175 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3176 | uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3177 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3178 | if (ssl->session != NULL) { |
| 3179 | return ssl->session->verify_result; |
| 3180 | } |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 3181 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3182 | if (ssl->session_negotiate != NULL) { |
| 3183 | return ssl->session_negotiate->verify_result; |
| 3184 | } |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 3185 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3186 | return 0xFFFFFFFF; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3187 | } |
| 3188 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3189 | 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] | 3190 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3191 | if (ssl == NULL || ssl->session == NULL) { |
| 3192 | return 0; |
| 3193 | } |
Glenn Strauss | 8f52690 | 2022-01-13 00:04:49 -0500 | [diff] [blame] | 3194 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3195 | return ssl->session->ciphersuite; |
Glenn Strauss | 8f52690 | 2022-01-13 00:04:49 -0500 | [diff] [blame] | 3196 | } |
| 3197 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3198 | const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 3199 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3200 | if (ssl == NULL || ssl->session == NULL) { |
| 3201 | return NULL; |
| 3202 | } |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 3203 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3204 | return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 3205 | } |
| 3206 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3207 | const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3208 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3209 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3210 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 3211 | switch (ssl->tls_version) { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3212 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3213 | return "DTLSv1.2"; |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 3214 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3215 | return "unknown (DTLS)"; |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 3216 | } |
| 3217 | } |
| 3218 | #endif |
| 3219 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3220 | switch (ssl->tls_version) { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3221 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3222 | return "TLSv1.2"; |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3223 | case MBEDTLS_SSL_VERSION_TLS1_3: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3224 | return "TLSv1.3"; |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3225 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3226 | return "unknown"; |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3227 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 3228 | } |
| 3229 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 3230 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3231 | 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] | 3232 | { |
David Horstmann | 95d516f | 2021-05-04 18:36:56 +0100 | [diff] [blame] | 3233 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3234 | size_t read_mfl; |
| 3235 | |
Jerry Yu | ddda050 | 2022-12-01 19:43:12 +0800 | [diff] [blame] | 3236 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3237 | /* 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] | 3238 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 3239 | ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) { |
| 3240 | return ssl_mfl_code_to_length(ssl->conf->mfl_code); |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3241 | } |
Jerry Yu | ddda050 | 2022-12-01 19:43:12 +0800 | [diff] [blame] | 3242 | #endif |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3243 | |
| 3244 | /* Check if a smaller max length was negotiated */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3245 | if (ssl->session_out != NULL) { |
| 3246 | read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code); |
| 3247 | if (read_mfl < max_len) { |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3248 | max_len = read_mfl; |
| 3249 | } |
| 3250 | } |
| 3251 | |
Jerry Yu | ddda050 | 2022-12-01 19:43:12 +0800 | [diff] [blame] | 3252 | /* During a handshake, use the value being negotiated */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3253 | if (ssl->session_negotiate != NULL) { |
| 3254 | read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code); |
| 3255 | if (read_mfl < max_len) { |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3256 | max_len = read_mfl; |
| 3257 | } |
| 3258 | } |
| 3259 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3260 | return max_len; |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 3261 | } |
| 3262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3263 | 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] | 3264 | { |
| 3265 | size_t max_len; |
| 3266 | |
| 3267 | /* |
| 3268 | * Assume mfl_code is correct since it was checked when set |
| 3269 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3270 | 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] | 3271 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 3272 | /* Check if a smaller max length was negotiated */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3273 | if (ssl->session_out != NULL && |
| 3274 | ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) { |
| 3275 | 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] | 3276 | } |
| 3277 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 3278 | /* During a handshake, use the value being negotiated */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3279 | if (ssl->session_negotiate != NULL && |
| 3280 | ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) { |
| 3281 | 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] | 3282 | } |
| 3283 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3284 | return max_len; |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 3285 | } |
| 3286 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 3287 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3288 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3289 | 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] | 3290 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 3291 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3292 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 3293 | (ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 3294 | ssl->state == MBEDTLS_SSL_SERVER_HELLO)) { |
| 3295 | return 0; |
| 3296 | } |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 3297 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3298 | if (ssl->handshake == NULL || ssl->handshake->mtu == 0) { |
| 3299 | return ssl->mtu; |
| 3300 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3301 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3302 | if (ssl->mtu == 0) { |
| 3303 | return ssl->handshake->mtu; |
| 3304 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3306 | return ssl->mtu < ssl->handshake->mtu ? |
| 3307 | ssl->mtu : ssl->handshake->mtu; |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3308 | } |
| 3309 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3310 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3311 | 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] | 3312 | { |
| 3313 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 3314 | |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 3315 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 3316 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3317 | (void) ssl; |
| 3318 | #endif |
| 3319 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3320 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3321 | 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] | 3322 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3323 | if (max_len > mfl) { |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3324 | max_len = mfl; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3325 | } |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3326 | #endif |
| 3327 | |
| 3328 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3329 | if (mbedtls_ssl_get_current_mtu(ssl) != 0) { |
| 3330 | const size_t mtu = mbedtls_ssl_get_current_mtu(ssl); |
| 3331 | const int ret = mbedtls_ssl_get_record_expansion(ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3332 | const size_t overhead = (size_t) ret; |
| 3333 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3334 | if (ret < 0) { |
| 3335 | return ret; |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3336 | } |
| 3337 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3338 | if (mtu <= overhead) { |
| 3339 | MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion")); |
| 3340 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 3341 | } |
| 3342 | |
| 3343 | if (max_len > mtu - overhead) { |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3344 | max_len = mtu - overhead; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3345 | } |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3346 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 3347 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3348 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 3349 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 3350 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3351 | ((void) ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3352 | #endif |
| 3353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3354 | return (int) max_len; |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 3355 | } |
| 3356 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3357 | 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] | 3358 | { |
| 3359 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
| 3360 | |
| 3361 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 3362 | (void) ssl; |
| 3363 | #endif |
| 3364 | |
| 3365 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3366 | const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl); |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 3367 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3368 | if (max_len > mfl) { |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 3369 | max_len = mfl; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3370 | } |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 3371 | #endif |
| 3372 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3373 | return (int) max_len; |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 3374 | } |
| 3375 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3376 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3377 | 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] | 3378 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3379 | if (ssl == NULL || ssl->session == NULL) { |
| 3380 | return NULL; |
| 3381 | } |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3382 | |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 3383 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3384 | return ssl->session->peer_cert; |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 3385 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3386 | return NULL; |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 3387 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3388 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3389 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 3390 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3391 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3392 | int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl, |
| 3393 | mbedtls_ssl_session *dst) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3394 | { |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3395 | int ret; |
| 3396 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3397 | if (ssl == NULL || |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3398 | dst == NULL || |
| 3399 | ssl->session == NULL || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3400 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) { |
| 3401 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3402 | } |
| 3403 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3404 | /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer |
| 3405 | * idempotent: Each session can only be exported once. |
| 3406 | * |
| 3407 | * (This is in preparation for TLS 1.3 support where we will |
| 3408 | * need the ability to export multiple sessions (aka tickets), |
| 3409 | * which will be achieved by calling mbedtls_ssl_get_session() |
| 3410 | * multiple times until it fails.) |
| 3411 | * |
| 3412 | * Check whether we have already exported the current session, |
| 3413 | * and fail if so. |
| 3414 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3415 | if (ssl->session->exported == 1) { |
| 3416 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 3417 | } |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3418 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3419 | ret = mbedtls_ssl_session_copy(dst, ssl->session); |
| 3420 | if (ret != 0) { |
| 3421 | return ret; |
| 3422 | } |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3423 | |
| 3424 | /* Remember that we've exported the session. */ |
| 3425 | ssl->session->exported = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3426 | return 0; |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3427 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3428 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 3429 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3430 | /* |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 3431 | * Define ticket header determining Mbed TLS version |
| 3432 | * and structure of the ticket. |
| 3433 | */ |
| 3434 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3435 | /* |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3436 | * Define bitflag determining compile-time settings influencing |
| 3437 | * structure of serialized SSL sessions. |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3438 | */ |
| 3439 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3440 | #if defined(MBEDTLS_HAVE_TIME) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3441 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3442 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3443 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3444 | #endif /* MBEDTLS_HAVE_TIME */ |
| 3445 | |
| 3446 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3447 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3448 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3449 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3450 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 3451 | |
| 3452 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3453 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3454 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3455 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3456 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ |
| 3457 | |
| 3458 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3459 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3460 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3461 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3462 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 3463 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3464 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3465 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3466 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3467 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3468 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 3469 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3470 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 3471 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 |
| 3472 | #else |
| 3473 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 |
| 3474 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 3475 | |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3476 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0 |
| 3477 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1 |
| 3478 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 |
| 3479 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3 |
Hanno Becker | 37bdbe6 | 2021-08-01 05:38:58 +0100 | [diff] [blame] | 3480 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4 |
| 3481 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5 |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 3482 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 3483 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3484 | ((uint16_t) ( \ |
| 3485 | (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \ |
| 3486 | (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \ |
| 3487 | (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \ |
| 3488 | SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \ |
| 3489 | (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \ |
| 3490 | (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \ |
| 3491 | (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT))) |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3492 | |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 3493 | static unsigned char ssl_serialized_session_header[] = { |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 3494 | MBEDTLS_VERSION_MAJOR, |
| 3495 | MBEDTLS_VERSION_MINOR, |
| 3496 | MBEDTLS_VERSION_PATCH, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3497 | MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG), |
| 3498 | MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG), |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 3499 | }; |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 3500 | |
| 3501 | /* |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3502 | * Serialize a session in the following format: |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 3503 | * (in the presentation language of TLS, RFC 8446 section 3) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3504 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3505 | * struct { |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 3506 | * |
Hanno Becker | dce5097 | 2021-08-01 05:39:23 +0100 | [diff] [blame] | 3507 | * opaque mbedtls_version[3]; // library version: major, minor, patch |
| 3508 | * opaque session_format[2]; // library-version specific 16-bit field |
| 3509 | * // determining the format of the remaining |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3510 | * // serialized data. |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 3511 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3512 | * Note: When updating the format, remember to keep |
| 3513 | * these version+format bytes. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3514 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3515 | * // In this version, `session_format` determines |
| 3516 | * // the setting of those compile-time |
| 3517 | * // configuration options which influence |
| 3518 | * // the structure of mbedtls_ssl_session. |
| 3519 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3520 | * uint8_t minor_ver; // Protocol minor version. Possible values: |
Jerry Yu | 0c2a738 | 2022-12-06 13:27:25 +0800 | [diff] [blame] | 3521 | * // - TLS 1.2 (0x0303) |
| 3522 | * // - TLS 1.3 (0x0304) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3523 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3524 | * select (serialized_session.tls_version) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3525 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3526 | * case MBEDTLS_SSL_VERSION_TLS1_2: |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3527 | * serialized_session_tls12 data; |
Jerry Yu | 0c2a738 | 2022-12-06 13:27:25 +0800 | [diff] [blame] | 3528 | * case MBEDTLS_SSL_VERSION_TLS1_3: |
Jerry Yu | ddda050 | 2022-12-01 19:43:12 +0800 | [diff] [blame] | 3529 | * serialized_session_tls13 data; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3530 | * |
| 3531 | * }; |
| 3532 | * |
| 3533 | * } serialized_session; |
| 3534 | * |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3535 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3536 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3537 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3538 | static int ssl_session_save(const mbedtls_ssl_session *session, |
| 3539 | unsigned char omit_header, |
| 3540 | unsigned char *buf, |
| 3541 | size_t buf_len, |
| 3542 | size_t *olen) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3543 | { |
| 3544 | unsigned char *p = buf; |
| 3545 | size_t used = 0; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3546 | size_t remaining_len; |
Jerry Yu | e36fdd6 | 2022-08-17 21:31:36 +0800 | [diff] [blame] | 3547 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3548 | size_t out_len; |
| 3549 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3550 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3551 | if (session == NULL) { |
| 3552 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 3553 | } |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3554 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3555 | if (!omit_header) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3556 | /* |
| 3557 | * Add Mbed TLS version identifier |
| 3558 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3559 | used += sizeof(ssl_serialized_session_header); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3560 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3561 | if (used <= buf_len) { |
| 3562 | memcpy(p, ssl_serialized_session_header, |
| 3563 | sizeof(ssl_serialized_session_header)); |
| 3564 | p += sizeof(ssl_serialized_session_header); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3565 | } |
| 3566 | } |
| 3567 | |
| 3568 | /* |
| 3569 | * TLS version identifier |
| 3570 | */ |
| 3571 | used += 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3572 | if (used <= buf_len) { |
| 3573 | *p++ = MBEDTLS_BYTE_0(session->tls_version); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3574 | } |
| 3575 | |
| 3576 | /* Forward to version-specific serialization routine. */ |
Jerry Yu | fca4d57 | 2022-07-21 10:37:48 +0800 | [diff] [blame] | 3577 | remaining_len = (buf_len >= used) ? buf_len - used : 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3578 | switch (session->tls_version) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3579 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3580 | case MBEDTLS_SSL_VERSION_TLS1_2: |
| 3581 | used += ssl_tls12_session_save(session, p, remaining_len); |
| 3582 | break; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3583 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3584 | |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3585 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3586 | case MBEDTLS_SSL_VERSION_TLS1_3: |
| 3587 | ret = ssl_tls13_session_save(session, p, remaining_len, &out_len); |
| 3588 | if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) { |
| 3589 | return ret; |
| 3590 | } |
| 3591 | used += out_len; |
| 3592 | break; |
Jerry Yu | 251a12e | 2022-07-13 15:15:48 +0800 | [diff] [blame] | 3593 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 3594 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3595 | default: |
| 3596 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3597 | } |
| 3598 | |
| 3599 | *olen = used; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3600 | if (used > buf_len) { |
| 3601 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 3602 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3603 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3604 | return 0; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3605 | } |
| 3606 | |
| 3607 | /* |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3608 | * Public wrapper for ssl_session_save() |
| 3609 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3610 | int mbedtls_ssl_session_save(const mbedtls_ssl_session *session, |
| 3611 | unsigned char *buf, |
| 3612 | size_t buf_len, |
| 3613 | size_t *olen) |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3614 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3615 | return ssl_session_save(session, 0, buf, buf_len, olen); |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3616 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3617 | |
Jerry Yu | f1b23ca | 2022-02-18 11:48:47 +0800 | [diff] [blame] | 3618 | /* |
| 3619 | * Deserialize session, see mbedtls_ssl_session_save() for format. |
| 3620 | * |
| 3621 | * This internal version is wrapped by a public function that cleans up in |
| 3622 | * case of error, and has an extra option omit_header. |
| 3623 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3624 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3625 | static int ssl_session_load(mbedtls_ssl_session *session, |
| 3626 | unsigned char omit_header, |
| 3627 | const unsigned char *buf, |
| 3628 | size_t len) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3629 | { |
| 3630 | const unsigned char *p = buf; |
| 3631 | const unsigned char * const end = buf + len; |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3632 | size_t remaining_len; |
| 3633 | |
| 3634 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3635 | if (session == NULL) { |
| 3636 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 3637 | } |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3638 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3639 | if (!omit_header) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3640 | /* |
| 3641 | * Check Mbed TLS version identifier |
| 3642 | */ |
| 3643 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3644 | if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) { |
| 3645 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3646 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3647 | |
| 3648 | if (memcmp(p, ssl_serialized_session_header, |
| 3649 | sizeof(ssl_serialized_session_header)) != 0) { |
| 3650 | return MBEDTLS_ERR_SSL_VERSION_MISMATCH; |
| 3651 | } |
| 3652 | p += sizeof(ssl_serialized_session_header); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3653 | } |
| 3654 | |
| 3655 | /* |
| 3656 | * TLS version identifier |
| 3657 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3658 | if (1 > (size_t) (end - p)) { |
| 3659 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3660 | } |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 3661 | session->tls_version = 0x0300 | *p++; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3662 | |
| 3663 | /* Dispatch according to TLS version. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3664 | remaining_len = (end - p); |
| 3665 | switch (session->tls_version) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3666 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3667 | case MBEDTLS_SSL_VERSION_TLS1_2: |
| 3668 | return ssl_tls12_session_load(session, p, remaining_len); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3669 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3670 | |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3671 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3672 | case MBEDTLS_SSL_VERSION_TLS1_3: |
| 3673 | return ssl_tls13_session_load(session, p, remaining_len); |
Jerry Yu | 438ddd8 | 2022-07-07 06:55:50 +0000 | [diff] [blame] | 3674 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 3675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3676 | default: |
| 3677 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 3678 | } |
| 3679 | } |
| 3680 | |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 3681 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 3682 | * Deserialize session: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3683 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3684 | int mbedtls_ssl_session_load(mbedtls_ssl_session *session, |
| 3685 | const unsigned char *buf, |
| 3686 | size_t len) |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3687 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3688 | int ret = ssl_session_load(session, 0, buf, len); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3689 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3690 | if (ret != 0) { |
| 3691 | mbedtls_ssl_session_free(session); |
| 3692 | } |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3693 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3694 | return ret; |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3695 | } |
| 3696 | |
| 3697 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3698 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3699 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3700 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3701 | static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl) |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3702 | { |
| 3703 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3704 | |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3705 | /* |
| 3706 | * 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] | 3707 | * that were written into the output buffer by the previous handshake step, |
| 3708 | * if the write to the network callback returned with the |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3709 | * #MBEDTLS_ERR_SSL_WANT_WRITE error code. |
| 3710 | * We proceed to the next handshake step only when all data from the |
| 3711 | * previous one have been sent to the peer, thus we make sure that this is |
| 3712 | * the case here by calling `mbedtls_ssl_flush_output()`. The function may |
| 3713 | * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case |
| 3714 | * we have to wait before to go ahead. |
| 3715 | * In the case of TLS 1.3, handshake step handlers do not send data to the |
| 3716 | * peer. Data are only sent here and through |
| 3717 | * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an |
Andrzej Kurek | 5c65c57 | 2022-04-13 14:28:52 -0400 | [diff] [blame] | 3718 | * alert occurred. |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 3719 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3720 | if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) { |
| 3721 | return ret; |
| 3722 | } |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3723 | |
| 3724 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3725 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3726 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) { |
| 3727 | if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) { |
| 3728 | return ret; |
| 3729 | } |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3730 | } |
| 3731 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3732 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3733 | return ret; |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3734 | } |
| 3735 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3736 | int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3737 | { |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3738 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3739 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3740 | if (ssl == NULL || |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3741 | ssl->conf == NULL || |
| 3742 | ssl->handshake == NULL || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3743 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 3744 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 3745 | } |
| 3746 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3747 | ret = ssl_prepare_handshake_step(ssl); |
| 3748 | if (ret != 0) { |
| 3749 | return ret; |
| 3750 | } |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3751 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3752 | ret = mbedtls_ssl_handle_pending_alert(ssl); |
| 3753 | if (ret != 0) { |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3754 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3755 | } |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3756 | |
Tom Cosgrove | 2fdc7b3 | 2022-09-21 12:33:17 +0100 | [diff] [blame] | 3757 | /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or |
| 3758 | * MBEDTLS_SSL_IS_SERVER, this is the return code we give */ |
| 3759 | ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3760 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3761 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3762 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 3763 | MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s", |
| 3764 | mbedtls_ssl_states_str(ssl->state))); |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3765 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3766 | switch (ssl->state) { |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3767 | case MBEDTLS_SSL_HELLO_REQUEST: |
| 3768 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; |
Tom Cosgrove | 87d9c6c | 2022-09-22 09:27:56 +0100 | [diff] [blame] | 3769 | ret = 0; |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3770 | break; |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3771 | |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3772 | case MBEDTLS_SSL_CLIENT_HELLO: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3773 | ret = mbedtls_ssl_write_client_hello(ssl); |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3774 | break; |
| 3775 | |
| 3776 | default: |
| 3777 | #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] | 3778 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
| 3779 | ret = mbedtls_ssl_tls13_handshake_client_step(ssl); |
| 3780 | } else { |
| 3781 | ret = mbedtls_ssl_handshake_client_step(ssl); |
| 3782 | } |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3783 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3784 | ret = mbedtls_ssl_handshake_client_step(ssl); |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3785 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3786 | ret = mbedtls_ssl_tls13_handshake_client_step(ssl); |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 3787 | #endif |
| 3788 | } |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3789 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3790 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3791 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3792 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3793 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3794 | if (mbedtls_ssl_conf_is_tls13_only(ssl->conf)) { |
| 3795 | ret = mbedtls_ssl_tls13_handshake_server_step(ssl); |
| 3796 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3797 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3798 | |
| 3799 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3800 | if (mbedtls_ssl_conf_is_tls12_only(ssl->conf)) { |
| 3801 | ret = mbedtls_ssl_handshake_server_step(ssl); |
| 3802 | } |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 3803 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3804 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3805 | #endif |
| 3806 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3807 | if (ret != 0) { |
Jerry Yu | bbd5a3f | 2021-09-18 20:50:22 +0800 | [diff] [blame] | 3808 | /* handshake_step return error. And it is same |
| 3809 | * with alert_reason. |
| 3810 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3811 | if (ssl->send_alert) { |
| 3812 | ret = mbedtls_ssl_handle_pending_alert(ssl); |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3813 | goto cleanup; |
| 3814 | } |
| 3815 | } |
| 3816 | |
| 3817 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3818 | return ret; |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3819 | } |
| 3820 | |
| 3821 | /* |
| 3822 | * Perform the SSL handshake |
| 3823 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3824 | int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3825 | { |
| 3826 | int ret = 0; |
| 3827 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3828 | /* Sanity checks */ |
| 3829 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3830 | if (ssl == NULL || ssl->conf == NULL) { |
| 3831 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3832 | } |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3833 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3834 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3835 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3836 | (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) { |
| 3837 | MBEDTLS_SSL_DEBUG_MSG(1, ("You must use " |
| 3838 | "mbedtls_ssl_set_timer_cb() for DTLS")); |
| 3839 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3840 | } |
| 3841 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3842 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3843 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake")); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3844 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3845 | /* Main handshake loop */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3846 | while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 3847 | ret = mbedtls_ssl_handshake_step(ssl); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3848 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3849 | if (ret != 0) { |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3850 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3851 | } |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3852 | } |
| 3853 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3854 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3855 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3856 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3857 | } |
| 3858 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3859 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3860 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3861 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3862 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3863 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3864 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3865 | static int ssl_write_hello_request(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3866 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3867 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3868 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3869 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request")); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3870 | |
| 3871 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3872 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3873 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3874 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3875 | if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { |
| 3876 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret); |
| 3877 | return ret; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3878 | } |
| 3879 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3880 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request")); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3881 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3882 | return 0; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3883 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3884 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3885 | |
| 3886 | /* |
| 3887 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3888 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 3889 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 3890 | * - 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] | 3891 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3892 | * 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] | 3893 | * 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] | 3894 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3895 | int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3896 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3897 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3898 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3899 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate")); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3900 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3901 | if ((ret = ssl_handshake_init(ssl)) != 0) { |
| 3902 | return ret; |
| 3903 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3904 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3905 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 3906 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3907 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3908 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3909 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) { |
| 3910 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3911 | ssl->handshake->out_msg_seq = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3912 | } else { |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3913 | ssl->handshake->in_msg_seq = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3914 | } |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3915 | } |
| 3916 | #endif |
| 3917 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3918 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 3919 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3920 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3921 | if ((ret = mbedtls_ssl_handshake(ssl)) != 0) { |
| 3922 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret); |
| 3923 | return ret; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3924 | } |
| 3925 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3926 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate")); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3927 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3928 | return 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3929 | } |
| 3930 | |
| 3931 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3932 | * Renegotiate current connection on client, |
| 3933 | * or request renegotiation on server |
| 3934 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3935 | int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3936 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3937 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3938 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3939 | if (ssl == NULL || ssl->conf == NULL) { |
| 3940 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3941 | } |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3942 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3943 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3944 | /* On server, just send the request */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3945 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 3946 | if (mbedtls_ssl_is_handshake_over(ssl) == 0) { |
| 3947 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3948 | } |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3949 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3950 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 3951 | |
| 3952 | /* Did we already try/start sending HelloRequest? */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3953 | if (ssl->out_left != 0) { |
| 3954 | return mbedtls_ssl_flush_output(ssl); |
| 3955 | } |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 3956 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3957 | return ssl_write_hello_request(ssl); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3958 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3959 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3960 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3961 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3962 | /* |
| 3963 | * On client, either start the renegotiation process or, |
| 3964 | * if already in progress, continue the handshake |
| 3965 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3966 | if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) { |
| 3967 | if (mbedtls_ssl_is_handshake_over(ssl) == 0) { |
| 3968 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3969 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3970 | |
| 3971 | if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) { |
| 3972 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret); |
| 3973 | return ret; |
| 3974 | } |
| 3975 | } else { |
| 3976 | if ((ret = mbedtls_ssl_handshake(ssl)) != 0) { |
| 3977 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret); |
| 3978 | return ret; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3979 | } |
| 3980 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3981 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3982 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3983 | return ret; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3984 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3985 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3986 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3987 | void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3988 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3989 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 3990 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3991 | if (handshake == NULL) { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3992 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3993 | } |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3994 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3995 | #if defined(MBEDTLS_ECP_C) |
| 3996 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3997 | if (ssl->handshake->group_list_heap_allocated) { |
| 3998 | mbedtls_free((void *) handshake->group_list); |
| 3999 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4000 | handshake->group_list = NULL; |
| 4001 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 4002 | #endif /* MBEDTLS_ECP_C */ |
| 4003 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4004 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4005 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4006 | if (ssl->handshake->sig_algs_heap_allocated) { |
| 4007 | mbedtls_free((void *) handshake->sig_algs); |
| 4008 | } |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4009 | handshake->sig_algs = NULL; |
| 4010 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Xiaofei Bai | c234ecf | 2022-02-08 09:59:23 +0000 | [diff] [blame] | 4011 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4012 | if (ssl->handshake->certificate_request_context) { |
| 4013 | mbedtls_free((void *) handshake->certificate_request_context); |
Xiaofei Bai | c234ecf | 2022-02-08 09:59:23 +0000 | [diff] [blame] | 4014 | } |
| 4015 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4016 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4017 | |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4018 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4019 | if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) { |
| 4020 | ssl->conf->f_async_cancel(ssl); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4021 | handshake->async_in_progress = 0; |
| 4022 | } |
| 4023 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 4024 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4025 | #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] | 4026 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4027 | psa_hash_abort(&handshake->fin_sha256_psa); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4028 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4029 | mbedtls_sha256_free(&handshake->fin_sha256); |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 4030 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4031 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4032 | #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] | 4033 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4034 | psa_hash_abort(&handshake->fin_sha384_psa); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4035 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4036 | mbedtls_sha512_free(&handshake->fin_sha384); |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 4037 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 4038 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 4039 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4040 | #if defined(MBEDTLS_DHM_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4041 | mbedtls_dhm_free(&handshake->dhm_ctx); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4042 | #endif |
Neil Armstrong | f3f4641 | 2022-04-12 14:43:39 +0200 | [diff] [blame] | 4043 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4044 | mbedtls_ecdh_free(&handshake->ecdh_ctx); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 4045 | #endif |
Valerio Setti | 02c25b5 | 2022-11-15 14:08:42 +0100 | [diff] [blame] | 4046 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 4047 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 4048 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4049 | psa_pake_abort(&handshake->psa_pake_ctx); |
Valerio Setti | eb3f788 | 2022-12-08 18:42:58 +0100 | [diff] [blame] | 4050 | /* |
| 4051 | * Opaque keys are not stored in the handshake's data and it's the user |
| 4052 | * responsibility to destroy them. Clear ones, instead, are created by |
| 4053 | * the TLS library and should be destroyed at the same level |
| 4054 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4055 | if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) { |
| 4056 | psa_destroy_key(handshake->psa_pake_password); |
Valerio Setti | eb3f788 | 2022-12-08 18:42:58 +0100 | [diff] [blame] | 4057 | } |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 4058 | handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT; |
| 4059 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4060 | mbedtls_ecjpake_free(&handshake->ecjpake_ctx); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 4061 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 4062 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4063 | mbedtls_free(handshake->ecjpake_cache); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 4064 | handshake->ecjpake_cache = NULL; |
| 4065 | handshake->ecjpake_cache_len = 0; |
| 4066 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 4067 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 4068 | |
Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 4069 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 4070 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 4071 | /* explicit void pointer cast for buggy MS compiler */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4072 | mbedtls_free((void *) handshake->curves_tls_id); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 4073 | #endif |
| 4074 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 4075 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4076 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4077 | if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4078 | /* The maintenance of the external PSK key slot is the |
| 4079 | * user's responsibility. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4080 | if (ssl->handshake->psk_opaque_is_internal) { |
| 4081 | psa_destroy_key(ssl->handshake->psk_opaque); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4082 | ssl->handshake->psk_opaque_is_internal = 0; |
| 4083 | } |
| 4084 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 4085 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 4086 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4087 | if (handshake->psk != NULL) { |
| 4088 | mbedtls_platform_zeroize(handshake->psk, handshake->psk_len); |
| 4089 | mbedtls_free(handshake->psk); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 4090 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 4091 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 4092 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 4093 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4094 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 4095 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 4096 | /* |
| 4097 | * Free only the linked list wrapper, not the keys themselves |
| 4098 | * since the belong to the SNI callback |
| 4099 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4100 | ssl_key_cert_free(handshake->sni_key_cert); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4101 | #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] | 4102 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4103 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4104 | mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx); |
| 4105 | if (handshake->ecrs_peer_cert != NULL) { |
| 4106 | mbedtls_x509_crt_free(handshake->ecrs_peer_cert); |
| 4107 | mbedtls_free(handshake->ecrs_peer_cert); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 4108 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 4109 | #endif |
| 4110 | |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 4111 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 4112 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4113 | mbedtls_pk_free(&handshake->peer_pubkey); |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 4114 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4115 | |
XiaokangQian | 9b93c0d | 2022-02-09 06:02:25 +0000 | [diff] [blame] | 4116 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4117 | (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3)) |
| 4118 | mbedtls_free(handshake->cookie); |
XiaokangQian | 9b93c0d | 2022-02-09 06:02:25 +0000 | [diff] [blame] | 4119 | #endif /* MBEDTLS_SSL_CLI_C && |
| 4120 | ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */ |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 4121 | |
| 4122 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4123 | mbedtls_ssl_flight_free(handshake->flight); |
| 4124 | mbedtls_ssl_buffering_free(ssl); |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 4125 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 4126 | |
Ronald Cron | f12b81d | 2022-03-15 10:42:41 +0100 | [diff] [blame] | 4127 | #if defined(MBEDTLS_ECDH_C) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4128 | (defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)) |
| 4129 | if (handshake->ecdh_psa_privkey_is_external == 0) { |
| 4130 | psa_destroy_key(handshake->ecdh_psa_privkey); |
| 4131 | } |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 4132 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */ |
| 4133 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4134 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4135 | mbedtls_ssl_transform_free(handshake->transform_handshake); |
| 4136 | mbedtls_free(handshake->transform_handshake); |
Jerry Yu | 3d9b590 | 2022-11-04 14:07:25 +0800 | [diff] [blame] | 4137 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4138 | mbedtls_ssl_transform_free(handshake->transform_earlydata); |
| 4139 | mbedtls_free(handshake->transform_earlydata); |
Jerry Yu | 3d9b590 | 2022-11-04 14:07:25 +0800 | [diff] [blame] | 4140 | #endif |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4141 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 4142 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4143 | |
| 4144 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4145 | /* If the buffers are too big - reallocate. Because of the way Mbed TLS |
| 4146 | * processes datagrams and the fact that a datagram is allowed to have |
| 4147 | * several records in it, it is possible that the I/O buffers are not |
| 4148 | * empty at this stage */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4149 | handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl), |
| 4150 | mbedtls_ssl_get_output_buflen(ssl)); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4151 | #endif |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 4152 | |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 4153 | /* mbedtls_platform_zeroize MUST be last one in this function */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4154 | mbedtls_platform_zeroize(handshake, |
| 4155 | sizeof(mbedtls_ssl_handshake_params)); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4156 | } |
| 4157 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4158 | void mbedtls_ssl_session_free(mbedtls_ssl_session *session) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4159 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4160 | if (session == NULL) { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4161 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4162 | } |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4163 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4164 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4165 | ssl_clear_peer_cert(session); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 4166 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 4167 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4168 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Xiaokang Qian | bc663a0 | 2022-10-09 11:14:39 +0000 | [diff] [blame] | 4169 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
| 4170 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4171 | mbedtls_free(session->hostname); |
Xiaokang Qian | 281fd1b | 2022-09-20 11:35:41 +0000 | [diff] [blame] | 4172 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4173 | mbedtls_free(session->ticket); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 4174 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 4175 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4176 | mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session)); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4177 | } |
| 4178 | |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 4179 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4180 | |
| 4181 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4182 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u |
| 4183 | #else |
| 4184 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u |
| 4185 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4186 | |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4187 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4188 | |
| 4189 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4190 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u |
| 4191 | #else |
| 4192 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u |
| 4193 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 4194 | |
| 4195 | #if defined(MBEDTLS_SSL_ALPN) |
| 4196 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u |
| 4197 | #else |
| 4198 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u |
| 4199 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4200 | |
| 4201 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0 |
| 4202 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1 |
| 4203 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2 |
| 4204 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3 |
| 4205 | |
| 4206 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4207 | ((uint32_t) ( \ |
| 4208 | (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \ |
| 4209 | SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \ |
| 4210 | (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \ |
| 4211 | SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \ |
| 4212 | (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \ |
| 4213 | SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \ |
| 4214 | (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \ |
| 4215 | 0u)) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4216 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4217 | static unsigned char ssl_serialized_context_header[] = { |
| 4218 | MBEDTLS_VERSION_MAJOR, |
| 4219 | MBEDTLS_VERSION_MINOR, |
| 4220 | MBEDTLS_VERSION_PATCH, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4221 | MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG), |
| 4222 | MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG), |
| 4223 | MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG), |
| 4224 | MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG), |
| 4225 | MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG), |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4226 | }; |
| 4227 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4228 | /* |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4229 | * Serialize a full SSL context |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 4230 | * |
| 4231 | * The format of the serialized data is: |
| 4232 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 4233 | * |
| 4234 | * // header |
| 4235 | * opaque mbedtls_version[3]; // major, minor, patch |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4236 | * opaque context_format[5]; // version-specific field determining |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 4237 | * // the format of the remaining |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4238 | * // serialized data. |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 4239 | * Note: When updating the format, remember to keep these |
| 4240 | * 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] | 4241 | * |
| 4242 | * // session sub-structure |
| 4243 | * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save() |
| 4244 | * // transform sub-structure |
| 4245 | * uint8 random[64]; // ServerHello.random+ClientHello.random |
| 4246 | * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value |
| 4247 | * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use |
| 4248 | * // fields from ssl_context |
| 4249 | * uint32 badmac_seen; // DTLS: number of records with failing MAC |
| 4250 | * uint64 in_window_top; // DTLS: last validated record seq_num |
| 4251 | * uint64 in_window; // DTLS: bitmask for replay protection |
| 4252 | * uint8 disable_datagram_packing; // DTLS: only one record per datagram |
| 4253 | * uint64 cur_out_ctr; // Record layer: outgoing sequence number |
| 4254 | * uint16 mtu; // DTLS: path mtu (max outgoing fragment size) |
| 4255 | * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol |
| 4256 | * |
| 4257 | * Note that many fields of the ssl_context or sub-structures are not |
| 4258 | * serialized, as they fall in one of the following categories: |
| 4259 | * |
| 4260 | * 1. forced value (eg in_left must be 0) |
| 4261 | * 2. pointer to dynamically-allocated memory (eg session, transform) |
| 4262 | * 3. value can be re-derived from other data (eg session keys from MS) |
| 4263 | * 4. value was temporary (eg content of input buffer) |
| 4264 | * 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] | 4265 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4266 | int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl, |
| 4267 | unsigned char *buf, |
| 4268 | size_t buf_len, |
| 4269 | size_t *olen) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4270 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4271 | unsigned char *p = buf; |
| 4272 | size_t used = 0; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4273 | size_t session_len; |
| 4274 | int ret = 0; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4275 | |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 4276 | /* |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4277 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in |
| 4278 | * this function's documentation. |
| 4279 | * |
| 4280 | * These are due to assumptions/limitations in the implementation. Some of |
| 4281 | * them are likely to stay (no handshake in progress) some might go away |
| 4282 | * (only DTLS) but are currently used to simplify the implementation. |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 4283 | */ |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4284 | /* The initial handshake must be over */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4285 | if (mbedtls_ssl_is_handshake_over(ssl) == 0) { |
| 4286 | MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over")); |
| 4287 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4288 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4289 | if (ssl->handshake != NULL) { |
| 4290 | MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed")); |
| 4291 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4292 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4293 | /* Double-check that sub-structures are indeed ready */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4294 | if (ssl->transform == NULL || ssl->session == NULL) { |
| 4295 | MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready")); |
| 4296 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4297 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4298 | /* There must be no pending incoming or outgoing data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4299 | if (mbedtls_ssl_check_pending(ssl) != 0) { |
| 4300 | MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data")); |
| 4301 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4302 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4303 | if (ssl->out_left != 0) { |
| 4304 | MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data")); |
| 4305 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4306 | } |
Dave Rodgman | 556e8a3 | 2022-12-06 16:31:25 +0000 | [diff] [blame] | 4307 | /* Protocol must be DTLS, not TLS */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4308 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 4309 | MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported")); |
| 4310 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4311 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4312 | /* Version must be 1.2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4313 | if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) { |
| 4314 | MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported")); |
| 4315 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4316 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4317 | /* We must be using an AEAD ciphersuite */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4318 | if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) { |
| 4319 | MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported")); |
| 4320 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4321 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4322 | /* Renegotiation must not be enabled */ |
| 4323 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4324 | if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) { |
| 4325 | MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled")); |
| 4326 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 4327 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 4328 | #endif |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4329 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4330 | /* |
| 4331 | * Version and format identifier |
| 4332 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4333 | used += sizeof(ssl_serialized_context_header); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4334 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4335 | if (used <= buf_len) { |
| 4336 | memcpy(p, ssl_serialized_context_header, |
| 4337 | sizeof(ssl_serialized_context_header)); |
| 4338 | p += sizeof(ssl_serialized_context_header); |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4339 | } |
| 4340 | |
| 4341 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4342 | * Session (length + data) |
| 4343 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4344 | ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len); |
| 4345 | if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) { |
| 4346 | return ret; |
| 4347 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4348 | |
| 4349 | used += 4 + session_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4350 | if (used <= buf_len) { |
| 4351 | MBEDTLS_PUT_UINT32_BE(session_len, p, 0); |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4352 | p += 4; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4354 | ret = ssl_session_save(ssl->session, 1, |
| 4355 | p, session_len, &session_len); |
| 4356 | if (ret != 0) { |
| 4357 | return ret; |
| 4358 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4359 | |
| 4360 | p += session_len; |
| 4361 | } |
| 4362 | |
| 4363 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4364 | * Transform |
| 4365 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4366 | used += sizeof(ssl->transform->randbytes); |
| 4367 | if (used <= buf_len) { |
| 4368 | memcpy(p, ssl->transform->randbytes, |
| 4369 | sizeof(ssl->transform->randbytes)); |
| 4370 | p += sizeof(ssl->transform->randbytes); |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4371 | } |
| 4372 | |
| 4373 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4374 | used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4375 | if (used <= buf_len) { |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4376 | *p++ = ssl->transform->in_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4377 | 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] | 4378 | p += ssl->transform->in_cid_len; |
| 4379 | |
| 4380 | *p++ = ssl->transform->out_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4381 | 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] | 4382 | p += ssl->transform->out_cid_len; |
| 4383 | } |
| 4384 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4385 | |
| 4386 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4387 | * Saved fields from top-level ssl_context structure |
| 4388 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4389 | used += 4; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4390 | if (used <= buf_len) { |
| 4391 | MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0); |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4392 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4393 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4394 | |
| 4395 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4396 | used += 16; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4397 | if (used <= buf_len) { |
| 4398 | MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0); |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4399 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4400 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4401 | MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0); |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4402 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4403 | } |
| 4404 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 4405 | |
| 4406 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4407 | used += 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4408 | if (used <= buf_len) { |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4409 | *p++ = ssl->disable_datagram_packing; |
| 4410 | } |
| 4411 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4412 | |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 4413 | used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4414 | if (used <= buf_len) { |
| 4415 | memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN); |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 4416 | p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4417 | } |
| 4418 | |
| 4419 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4420 | used += 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4421 | if (used <= buf_len) { |
| 4422 | MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0); |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4423 | p += 2; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4424 | } |
| 4425 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4426 | |
| 4427 | #if defined(MBEDTLS_SSL_ALPN) |
| 4428 | { |
| 4429 | const uint8_t alpn_len = ssl->alpn_chosen |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4430 | ? (uint8_t) strlen(ssl->alpn_chosen) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4431 | : 0; |
| 4432 | |
| 4433 | used += 1 + alpn_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4434 | if (used <= buf_len) { |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4435 | *p++ = alpn_len; |
| 4436 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4437 | if (ssl->alpn_chosen != NULL) { |
| 4438 | memcpy(p, ssl->alpn_chosen, alpn_len); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4439 | p += alpn_len; |
| 4440 | } |
| 4441 | } |
| 4442 | } |
| 4443 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4444 | |
| 4445 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4446 | * Done |
| 4447 | */ |
| 4448 | *olen = used; |
| 4449 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4450 | if (used > buf_len) { |
| 4451 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 4452 | } |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4453 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4454 | MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4455 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4456 | return mbedtls_ssl_session_reset_int(ssl, 0); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4457 | } |
| 4458 | |
| 4459 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 4460 | * Deserialize context, see mbedtls_ssl_context_save() for format. |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4461 | * |
| 4462 | * This internal version is wrapped by a public function that cleans up in |
| 4463 | * case of error. |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4464 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4465 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4466 | static int ssl_context_load(mbedtls_ssl_context *ssl, |
| 4467 | const unsigned char *buf, |
| 4468 | size_t len) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4469 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4470 | const unsigned char *p = buf; |
| 4471 | const unsigned char * const end = buf + len; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4472 | size_t session_len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4473 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4474 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 4475 | tls_prf_fn prf_func = NULL; |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4476 | #endif |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4477 | |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4478 | /* |
| 4479 | * The context should have been freshly setup or reset. |
| 4480 | * Give the user an error in case of obvious misuse. |
Manuel Pégourié-Gonnard | 4ca930f | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 4481 | * (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] | 4482 | * renegotiating, or if the user mistakenly loaded a session first.) |
| 4483 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4484 | if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST || |
| 4485 | ssl->session != NULL) { |
| 4486 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4487 | } |
| 4488 | |
| 4489 | /* |
| 4490 | * We can't check that the config matches the initial one, but we can at |
| 4491 | * least check it matches the requirements for serializing. |
| 4492 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4493 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 4494 | ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 || |
| 4495 | ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4496 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 4497 | ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4498 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4499 | 0) { |
| 4500 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 4501 | } |
| 4502 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4503 | MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4504 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4505 | /* |
| 4506 | * Check version identifier |
| 4507 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4508 | if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) { |
| 4509 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4510 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4511 | |
| 4512 | if (memcmp(p, ssl_serialized_context_header, |
| 4513 | sizeof(ssl_serialized_context_header)) != 0) { |
| 4514 | return MBEDTLS_ERR_SSL_VERSION_MISMATCH; |
| 4515 | } |
| 4516 | p += sizeof(ssl_serialized_context_header); |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4517 | |
| 4518 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4519 | * Session |
| 4520 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4521 | if ((size_t) (end - p) < 4) { |
| 4522 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4523 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4524 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4525 | session_len = ((size_t) p[0] << 24) | |
| 4526 | ((size_t) p[1] << 16) | |
| 4527 | ((size_t) p[2] << 8) | |
| 4528 | ((size_t) p[3]); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4529 | p += 4; |
| 4530 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4531 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4532 | * 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] | 4533 | ssl->session = ssl->session_negotiate; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4534 | ssl->session_in = ssl->session; |
| 4535 | ssl->session_out = ssl->session; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4536 | ssl->session_negotiate = NULL; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4537 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4538 | if ((size_t) (end - p) < session_len) { |
| 4539 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4540 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4541 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4542 | ret = ssl_session_load(ssl->session, 1, p, session_len); |
| 4543 | if (ret != 0) { |
| 4544 | mbedtls_ssl_session_free(ssl->session); |
| 4545 | return ret; |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4546 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4547 | |
| 4548 | p += session_len; |
| 4549 | |
| 4550 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4551 | * Transform |
| 4552 | */ |
| 4553 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4554 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 4555 | * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */ |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 4556 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4557 | ssl->transform = ssl->transform_negotiate; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4558 | ssl->transform_in = ssl->transform; |
| 4559 | ssl->transform_out = ssl->transform; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 4560 | ssl->transform_negotiate = NULL; |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 4561 | #endif |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4562 | |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4563 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4564 | prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite); |
| 4565 | if (prf_func == NULL) { |
| 4566 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4567 | } |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 4568 | |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4569 | /* Read random bytes and populate structure */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4570 | if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) { |
| 4571 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4572 | } |
Andrzej Kurek | 2d59dbc | 2022-10-13 08:34:38 -0400 | [diff] [blame] | 4573 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4574 | ret = ssl_tls12_populate_transform(ssl->transform, |
| 4575 | ssl->session->ciphersuite, |
| 4576 | ssl->session->master, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 4577 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4578 | ssl->session->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 4579 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4580 | prf_func, |
| 4581 | p, /* currently pointing to randbytes */ |
| 4582 | MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */ |
| 4583 | ssl->conf->endpoint, |
| 4584 | ssl); |
| 4585 | if (ret != 0) { |
| 4586 | return ret; |
| 4587 | } |
Jerry Yu | 840fbb2 | 2022-02-17 14:59:29 +0800 | [diff] [blame] | 4588 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4589 | p += sizeof(ssl->transform->randbytes); |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4590 | |
| 4591 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4592 | /* Read connection IDs and store them */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4593 | if ((size_t) (end - p) < 1) { |
| 4594 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4595 | } |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4596 | |
| 4597 | ssl->transform->in_cid_len = *p++; |
| 4598 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4599 | if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) { |
| 4600 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4601 | } |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4602 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4603 | 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] | 4604 | p += ssl->transform->in_cid_len; |
| 4605 | |
| 4606 | ssl->transform->out_cid_len = *p++; |
| 4607 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4608 | if ((size_t) (end - p) < ssl->transform->out_cid_len) { |
| 4609 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4610 | } |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 4611 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4612 | 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] | 4613 | p += ssl->transform->out_cid_len; |
| 4614 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4615 | |
| 4616 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4617 | * Saved fields from top-level ssl_context structure |
| 4618 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4619 | if ((size_t) (end - p) < 4) { |
| 4620 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4621 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4622 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4623 | ssl->badmac_seen = ((uint32_t) p[0] << 24) | |
| 4624 | ((uint32_t) p[1] << 16) | |
| 4625 | ((uint32_t) p[2] << 8) | |
| 4626 | ((uint32_t) p[3]); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4627 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4628 | |
| 4629 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4630 | if ((size_t) (end - p) < 16) { |
| 4631 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4632 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4633 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4634 | ssl->in_window_top = ((uint64_t) p[0] << 56) | |
| 4635 | ((uint64_t) p[1] << 48) | |
| 4636 | ((uint64_t) p[2] << 40) | |
| 4637 | ((uint64_t) p[3] << 32) | |
| 4638 | ((uint64_t) p[4] << 24) | |
| 4639 | ((uint64_t) p[5] << 16) | |
| 4640 | ((uint64_t) p[6] << 8) | |
| 4641 | ((uint64_t) p[7]); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4642 | p += 8; |
| 4643 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4644 | ssl->in_window = ((uint64_t) p[0] << 56) | |
| 4645 | ((uint64_t) p[1] << 48) | |
| 4646 | ((uint64_t) p[2] << 40) | |
| 4647 | ((uint64_t) p[3] << 32) | |
| 4648 | ((uint64_t) p[4] << 24) | |
| 4649 | ((uint64_t) p[5] << 16) | |
| 4650 | ((uint64_t) p[6] << 8) | |
| 4651 | ((uint64_t) p[7]); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4652 | p += 8; |
| 4653 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 4654 | |
| 4655 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4656 | if ((size_t) (end - p) < 1) { |
| 4657 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4658 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4659 | |
| 4660 | ssl->disable_datagram_packing = *p++; |
| 4661 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4662 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4663 | if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) { |
| 4664 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4665 | } |
| 4666 | memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr)); |
| 4667 | p += sizeof(ssl->cur_out_ctr); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4668 | |
| 4669 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4670 | if ((size_t) (end - p) < 2) { |
| 4671 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4672 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4673 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4674 | ssl->mtu = (p[0] << 8) | p[1]; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4675 | p += 2; |
| 4676 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4677 | |
| 4678 | #if defined(MBEDTLS_SSL_ALPN) |
| 4679 | { |
| 4680 | uint8_t alpn_len; |
| 4681 | const char **cur; |
| 4682 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4683 | if ((size_t) (end - p) < 1) { |
| 4684 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4685 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4686 | |
| 4687 | alpn_len = *p++; |
| 4688 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4689 | if (alpn_len != 0 && ssl->conf->alpn_list != NULL) { |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4690 | /* alpn_chosen should point to an item in the configured list */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4691 | for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) { |
| 4692 | if (strlen(*cur) == alpn_len && |
| 4693 | memcmp(p, cur, alpn_len) == 0) { |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4694 | ssl->alpn_chosen = *cur; |
| 4695 | break; |
| 4696 | } |
| 4697 | } |
| 4698 | } |
| 4699 | |
| 4700 | /* can only happen on conf mismatch */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4701 | if (alpn_len != 0 && ssl->alpn_chosen == NULL) { |
| 4702 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4703 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 4704 | |
| 4705 | p += alpn_len; |
| 4706 | } |
| 4707 | #endif /* MBEDTLS_SSL_ALPN */ |
| 4708 | |
| 4709 | /* |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4710 | * Forced fields from top-level ssl_context structure |
| 4711 | * |
| 4712 | * Most of them already set to the correct value by mbedtls_ssl_init() and |
| 4713 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. |
| 4714 | */ |
| 4715 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 4716 | ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4717 | |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 4718 | /* Adjust pointers for header fields of outgoing records to |
| 4719 | * the given transform, accounting for explicit IV and CID. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4720 | mbedtls_ssl_update_out_pointers(ssl, ssl->transform); |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 4721 | |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4722 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4723 | ssl->in_epoch = 1; |
| 4724 | #endif |
| 4725 | |
| 4726 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, |
| 4727 | * 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] | 4728 | * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform() |
| 4729 | * inappropriately. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4730 | if (ssl->handshake != NULL) { |
| 4731 | mbedtls_ssl_handshake_free(ssl); |
| 4732 | mbedtls_free(ssl->handshake); |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 4733 | ssl->handshake = NULL; |
| 4734 | } |
| 4735 | |
| 4736 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 4737 | * Done - should have consumed entire buffer |
| 4738 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4739 | if (p != end) { |
| 4740 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4741 | } |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4742 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4743 | return 0; |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 4744 | } |
| 4745 | |
| 4746 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 4747 | * Deserialize context: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4748 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4749 | int mbedtls_ssl_context_load(mbedtls_ssl_context *context, |
| 4750 | const unsigned char *buf, |
| 4751 | size_t len) |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4752 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4753 | int ret = ssl_context_load(context, buf, len); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4754 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4755 | if (ret != 0) { |
| 4756 | mbedtls_ssl_free(context); |
| 4757 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4758 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4759 | return ret; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4760 | } |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 4761 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 4762 | |
| 4763 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4764 | * Free an SSL context |
| 4765 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4766 | void mbedtls_ssl_free(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4767 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4768 | if (ssl == NULL) { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4769 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4770 | } |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4771 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4772 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> free")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4773 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4774 | if (ssl->out_buf != NULL) { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 4775 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4776 | size_t out_buf_len = ssl->out_buf_len; |
| 4777 | #else |
| 4778 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 4779 | #endif |
| 4780 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4781 | mbedtls_platform_zeroize(ssl->out_buf, out_buf_len); |
| 4782 | mbedtls_free(ssl->out_buf); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4783 | ssl->out_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4784 | } |
| 4785 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4786 | if (ssl->in_buf != NULL) { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 4787 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4788 | size_t in_buf_len = ssl->in_buf_len; |
| 4789 | #else |
| 4790 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 4791 | #endif |
| 4792 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4793 | mbedtls_platform_zeroize(ssl->in_buf, in_buf_len); |
| 4794 | mbedtls_free(ssl->in_buf); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4795 | ssl->in_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4796 | } |
| 4797 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4798 | if (ssl->transform) { |
| 4799 | mbedtls_ssl_transform_free(ssl->transform); |
| 4800 | mbedtls_free(ssl->transform); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4801 | } |
| 4802 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4803 | if (ssl->handshake) { |
| 4804 | mbedtls_ssl_handshake_free(ssl); |
| 4805 | mbedtls_free(ssl->handshake); |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 4806 | |
| 4807 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4808 | mbedtls_ssl_transform_free(ssl->transform_negotiate); |
| 4809 | mbedtls_free(ssl->transform_negotiate); |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 4810 | #endif |
| 4811 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4812 | mbedtls_ssl_session_free(ssl->session_negotiate); |
| 4813 | mbedtls_free(ssl->session_negotiate); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4814 | } |
| 4815 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4816 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4817 | mbedtls_ssl_transform_free(ssl->transform_application); |
| 4818 | mbedtls_free(ssl->transform_application); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4819 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 4820 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4821 | if (ssl->session) { |
| 4822 | mbedtls_ssl_session_free(ssl->session); |
| 4823 | mbedtls_free(ssl->session); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 4824 | } |
| 4825 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 4826 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4827 | if (ssl->hostname != NULL) { |
| 4828 | mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname)); |
| 4829 | mbedtls_free(ssl->hostname); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4830 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 4831 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4832 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 4833 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4834 | mbedtls_free(ssl->cli_id); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 4835 | #endif |
| 4836 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4837 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= free")); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 4838 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 4839 | /* Actually clear after last debug message */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4840 | mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context)); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4841 | } |
| 4842 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4843 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 4844 | * Initialize mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4845 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4846 | void mbedtls_ssl_config_init(mbedtls_ssl_config *conf) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4847 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4848 | memset(conf, 0, sizeof(mbedtls_ssl_config)); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4849 | } |
| 4850 | |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4851 | /* The selection should be the same as mbedtls_x509_crt_profile_default in |
| 4852 | * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters: |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4853 | * curves with a lower resource usage come first. |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4854 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4855 | * about this list. |
| 4856 | */ |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4857 | static uint16_t ssl_preset_default_groups[] = { |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4858 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4859 | MBEDTLS_SSL_IANA_TLS_GROUP_X25519, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4860 | #endif |
| 4861 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4862 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4863 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4864 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4865 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4866 | #endif |
| 4867 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4868 | MBEDTLS_SSL_IANA_TLS_GROUP_X448, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4869 | #endif |
| 4870 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4871 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4872 | #endif |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4873 | #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4874 | MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4875 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4876 | #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4877 | MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4878 | #endif |
| 4879 | #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4880 | MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4881 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4882 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4883 | }; |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4884 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4885 | static int ssl_preset_suiteb_ciphersuites[] = { |
| 4886 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 4887 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 4888 | 0 |
| 4889 | }; |
| 4890 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4891 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4892 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4893 | /* NOTICE: |
Jerry Yu | 0b994b8 | 2022-01-25 17:22:12 +0800 | [diff] [blame] | 4894 | * 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] | 4895 | * rules SHOULD be upheld. |
| 4896 | * - No duplicate entries. |
| 4897 | * - 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] | 4898 | * - ssl_tls12_preset* is for TLS 1.2 use only. |
Jerry Yu | 370e146 | 2022-01-25 10:36:53 +0800 | [diff] [blame] | 4899 | * - 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] | 4900 | */ |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4901 | static uint16_t ssl_preset_default_sig_algs[] = { |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4902 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4903 | #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] | 4904 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 4905 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4906 | #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] | 4907 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4908 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4909 | #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] | 4910 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 4911 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4912 | #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] | 4913 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
| 4914 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4915 | #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] | 4916 | defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
| 4917 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4918 | #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] | 4919 | MBEDTLS_ECP_DP_SECP521R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4920 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4921 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \ |
| 4922 | 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] | 4923 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4924 | #endif \ |
| 4925 | /* 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] | 4926 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4927 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \ |
| 4928 | 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] | 4929 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4930 | #endif \ |
| 4931 | /* 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] | 4932 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4933 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \ |
| 4934 | 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] | 4935 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4936 | #endif \ |
| 4937 | /* 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] | 4938 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4939 | #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] | 4940 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512, |
| 4941 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */ |
| 4942 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4943 | #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] | 4944 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384, |
| 4945 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */ |
| 4946 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4947 | #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] | 4948 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
| 4949 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */ |
| 4950 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4951 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4952 | }; |
| 4953 | |
| 4954 | /* NOTICE: see above */ |
| 4955 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4956 | static uint16_t ssl_tls12_preset_default_sig_algs[] = { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4957 | #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] | 4958 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4959 | 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] | 4960 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4961 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 4962 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512, |
| 4963 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4964 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4965 | 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] | 4966 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4967 | #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] | 4968 | #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] | 4969 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4970 | 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] | 4971 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4972 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 4973 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384, |
| 4974 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4975 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4976 | 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] | 4977 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4978 | #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] | 4979 | #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] | 4980 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4981 | 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] | 4982 | #endif |
Jerry Yu | 09a99fc | 2022-07-28 14:22:17 +0800 | [diff] [blame] | 4983 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
| 4984 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
| 4985 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4986 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4987 | 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] | 4988 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4989 | #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] | 4990 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4991 | }; |
| 4992 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4993 | /* NOTICE: see above */ |
| 4994 | static uint16_t ssl_preset_suiteb_sig_algs[] = { |
| 4995 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 4996 | #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] | 4997 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 4998 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 4999 | #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] | 5000 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
| 5001 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5002 | #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] | 5003 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 5004 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5005 | #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] | 5006 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5007 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5008 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \ |
| 5009 | 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] | 5010 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5011 | #endif \ |
| 5012 | /* 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] | 5013 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5014 | #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] | 5015 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5016 | #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] | 5017 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5018 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 5019 | }; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 5020 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5021 | /* NOTICE: see above */ |
| 5022 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5023 | static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5024 | #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] | 5025 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5026 | 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] | 5027 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5028 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5029 | 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] | 5030 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5031 | #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] | 5032 | #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] | 5033 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5034 | 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] | 5035 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 5036 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5037 | 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] | 5038 | #endif |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5039 | #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] | 5040 | MBEDTLS_TLS_SIG_NONE |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 5041 | }; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5042 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5043 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5044 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5045 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5046 | static uint16_t ssl_preset_suiteb_groups[] = { |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 5047 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5048 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 5049 | #endif |
| 5050 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5051 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 5052 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5053 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5054 | }; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5055 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5056 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5057 | /* 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] | 5058 | * to make sure there are no duplicated signature algorithm entries. */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5059 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5060 | static int ssl_check_no_sig_alg_duplication(uint16_t *sig_algs) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5061 | { |
| 5062 | size_t i, j; |
| 5063 | int ret = 0; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5064 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5065 | for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) { |
| 5066 | for (j = 0; j < i; j++) { |
| 5067 | if (sig_algs[i] != sig_algs[j]) { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 5068 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5069 | } |
| 5070 | mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET |
| 5071 | ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n", |
| 5072 | sig_algs[i], j, i); |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 5073 | ret = -1; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5074 | } |
| 5075 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5076 | return ret; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5077 | } |
| 5078 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5079 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5080 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5081 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 5082 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5083 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5084 | int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf, |
| 5085 | int endpoint, int transport, int preset) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5086 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 5087 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5088 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 5089 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5090 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5091 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5092 | if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) { |
| 5093 | mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n"); |
| 5094 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5095 | } |
| 5096 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5097 | if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) { |
| 5098 | mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n"); |
| 5099 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5100 | } |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5101 | |
| 5102 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5103 | if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) { |
| 5104 | mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n"); |
| 5105 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5106 | } |
| 5107 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5108 | if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) { |
| 5109 | mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n"); |
| 5110 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5111 | } |
| 5112 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5113 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 5114 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 5115 | /* Use the functions here so that they are covered in tests, |
| 5116 | * but otherwise access member directly for efficiency */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5117 | mbedtls_ssl_conf_endpoint(conf, endpoint); |
| 5118 | mbedtls_ssl_conf_transport(conf, transport); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5119 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5120 | /* |
| 5121 | * Things that are common to all presets |
| 5122 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 5123 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5124 | if (endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 5125 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 5126 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 5127 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
| 5128 | #endif |
| 5129 | } |
| 5130 | #endif |
| 5131 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5132 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 5133 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 5134 | #endif |
| 5135 | |
| 5136 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 5137 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 5138 | #endif |
| 5139 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 5140 | #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] | 5141 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 5142 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 5143 | #endif |
| 5144 | |
| 5145 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 5146 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 5147 | #endif |
| 5148 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 5149 | #if defined(MBEDTLS_SSL_SRV_C) |
| 5150 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
TRodziewicz | 3946f79 | 2021-06-14 12:11:18 +0200 | [diff] [blame] | 5151 | conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER; |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 5152 | #endif |
| 5153 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5154 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5155 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
| 5156 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
| 5157 | #endif |
| 5158 | |
| 5159 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 5160 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5161 | memset(conf->renego_period, 0x00, 2); |
| 5162 | memset(conf->renego_period + 2, 0xFF, 6); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5163 | #endif |
| 5164 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5165 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5166 | if (endpoint == MBEDTLS_SSL_IS_SERVER) { |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 5167 | const unsigned char dhm_p[] = |
| 5168 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 5169 | const unsigned char dhm_g[] = |
| 5170 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 5171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5172 | if ((ret = mbedtls_ssl_conf_dh_param_bin(conf, |
| 5173 | dhm_p, sizeof(dhm_p), |
| 5174 | dhm_g, sizeof(dhm_g))) != 0) { |
| 5175 | return ret; |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 5176 | } |
| 5177 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 5178 | #endif |
| 5179 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5180 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 6ee56aa | 2022-12-06 17:47:22 +0800 | [diff] [blame] | 5181 | |
| 5182 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5183 | mbedtls_ssl_tls13_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED); |
Jerry Yu | 6ee56aa | 2022-12-06 17:47:22 +0800 | [diff] [blame] | 5184 | #if defined(MBEDTLS_SSL_SRV_C) |
| 5185 | mbedtls_ssl_tls13_conf_max_early_data_size( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5186 | conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE); |
Jerry Yu | 6ee56aa | 2022-12-06 17:47:22 +0800 | [diff] [blame] | 5187 | #endif |
| 5188 | #endif /* MBEDTLS_SSL_EARLY_DATA */ |
| 5189 | |
Jerry Yu | d0766ec | 2022-09-22 10:46:57 +0800 | [diff] [blame] | 5190 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 5191 | mbedtls_ssl_conf_new_session_tickets( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5192 | conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS); |
Jerry Yu | 1ad7ace | 2022-08-09 13:28:39 +0800 | [diff] [blame] | 5193 | #endif |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 5194 | /* |
| 5195 | * Allow all TLS 1.3 key exchange modes by default. |
| 5196 | */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 5197 | conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL; |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5198 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 5199 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5200 | if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 5201 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5202 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 5203 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5204 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5205 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 5206 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5207 | } else { |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5208 | #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] | 5209 | if (endpoint == MBEDTLS_SSL_IS_CLIENT) { |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5210 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5211 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5212 | } else { |
| 5213 | /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */ |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5214 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5215 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5216 | } |
| 5217 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 5218 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 5219 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 5220 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5221 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5222 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 5223 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5224 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 5225 | #endif |
| 5226 | } |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 5227 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5228 | /* |
| 5229 | * Preset-specific defaults |
| 5230 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5231 | switch (preset) { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5232 | /* |
| 5233 | * NSA Suite B |
| 5234 | */ |
| 5235 | case MBEDTLS_SSL_PRESET_SUITEB: |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5236 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 5237 | conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5238 | |
| 5239 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5240 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5241 | #endif |
| 5242 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5243 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5244 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5245 | if (mbedtls_ssl_conf_is_tls12_only(conf)) { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5246 | conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5247 | } else |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5248 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5249 | conf->sig_algs = ssl_preset_suiteb_sig_algs; |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5250 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5251 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5252 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 5253 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5254 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5255 | conf->group_list = ssl_preset_suiteb_groups; |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 5256 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5257 | |
| 5258 | /* |
| 5259 | * Default |
| 5260 | */ |
| 5261 | default: |
Ronald Cron | f660655 | 2022-03-15 11:23:25 +0100 | [diff] [blame] | 5262 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 5263 | conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites(); |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5264 | |
| 5265 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5266 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 5267 | #endif |
| 5268 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5269 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5270 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5271 | if (mbedtls_ssl_conf_is_tls12_only(conf)) { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5272 | conf->sig_algs = ssl_tls12_preset_default_sig_algs; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5273 | } else |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 5274 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5275 | conf->sig_algs = ssl_preset_default_sig_algs; |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5276 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5277 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5278 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 5279 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5280 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5281 | conf->group_list = ssl_preset_default_groups; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5282 | |
| 5283 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 5284 | conf->dhm_min_bitlen = 1024; |
| 5285 | #endif |
| 5286 | } |
| 5287 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5288 | return 0; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5289 | } |
| 5290 | |
| 5291 | /* |
| 5292 | * Free mbedtls_ssl_config |
| 5293 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5294 | void mbedtls_ssl_config_free(mbedtls_ssl_config *conf) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5295 | { |
| 5296 | #if defined(MBEDTLS_DHM_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5297 | mbedtls_mpi_free(&conf->dhm_P); |
| 5298 | mbedtls_mpi_free(&conf->dhm_G); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5299 | #endif |
| 5300 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 5301 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 5302 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5303 | if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 5304 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 5305 | } |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5306 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5307 | if (conf->psk != NULL) { |
| 5308 | mbedtls_platform_zeroize(conf->psk, conf->psk_len); |
| 5309 | mbedtls_free(conf->psk); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 5310 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5311 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 5312 | } |
| 5313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5314 | if (conf->psk_identity != NULL) { |
| 5315 | mbedtls_platform_zeroize(conf->psk_identity, conf->psk_identity_len); |
| 5316 | mbedtls_free(conf->psk_identity); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 5317 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5318 | conf->psk_identity_len = 0; |
| 5319 | } |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 5320 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5321 | |
| 5322 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5323 | ssl_key_cert_free(conf->key_cert); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5324 | #endif |
| 5325 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5326 | mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config)); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5327 | } |
| 5328 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 5329 | #if defined(MBEDTLS_PK_C) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5330 | (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C)) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5331 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5332 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5333 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5334 | 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] | 5335 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5336 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5337 | if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) { |
| 5338 | return MBEDTLS_SSL_SIG_RSA; |
| 5339 | } |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5340 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5341 | #if defined(MBEDTLS_ECDSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5342 | if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) { |
| 5343 | return MBEDTLS_SSL_SIG_ECDSA; |
| 5344 | } |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5345 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5346 | return MBEDTLS_SSL_SIG_ANON; |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 5347 | } |
| 5348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5349 | 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] | 5350 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5351 | switch (type) { |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 5352 | case MBEDTLS_PK_RSA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5353 | return MBEDTLS_SSL_SIG_RSA; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 5354 | case MBEDTLS_PK_ECDSA: |
| 5355 | case MBEDTLS_PK_ECKEY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5356 | return MBEDTLS_SSL_SIG_ECDSA; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 5357 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5358 | return MBEDTLS_SSL_SIG_ANON; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 5359 | } |
| 5360 | } |
| 5361 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5362 | 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] | 5363 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5364 | switch (sig) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5365 | #if defined(MBEDTLS_RSA_C) |
| 5366 | case MBEDTLS_SSL_SIG_RSA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5367 | return MBEDTLS_PK_RSA; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5368 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5369 | #if defined(MBEDTLS_ECDSA_C) |
| 5370 | case MBEDTLS_SSL_SIG_ECDSA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5371 | return MBEDTLS_PK_ECDSA; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5372 | #endif |
| 5373 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5374 | return MBEDTLS_PK_NONE; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5375 | } |
| 5376 | } |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 5377 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5378 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 5379 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5380 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 5381 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5382 | 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] | 5383 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5384 | switch (hash) { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5385 | #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] | 5386 | case MBEDTLS_SSL_HASH_MD5: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5387 | return MBEDTLS_MD_MD5; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5388 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5389 | #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] | 5390 | case MBEDTLS_SSL_HASH_SHA1: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5391 | return MBEDTLS_MD_SHA1; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5392 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5393 | #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] | 5394 | case MBEDTLS_SSL_HASH_SHA224: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5395 | return MBEDTLS_MD_SHA224; |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 5396 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5397 | #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] | 5398 | case MBEDTLS_SSL_HASH_SHA256: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5399 | return MBEDTLS_MD_SHA256; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5400 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5401 | #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] | 5402 | case MBEDTLS_SSL_HASH_SHA384: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5403 | return MBEDTLS_MD_SHA384; |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 5404 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5405 | #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] | 5406 | case MBEDTLS_SSL_HASH_SHA512: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5407 | return MBEDTLS_MD_SHA512; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5408 | #endif |
| 5409 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5410 | return MBEDTLS_MD_NONE; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 5411 | } |
| 5412 | } |
| 5413 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5414 | /* |
| 5415 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 5416 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5417 | unsigned char mbedtls_ssl_hash_from_md_alg(int md) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5418 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5419 | switch (md) { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5420 | #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] | 5421 | case MBEDTLS_MD_MD5: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5422 | return MBEDTLS_SSL_HASH_MD5; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5423 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5424 | #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] | 5425 | case MBEDTLS_MD_SHA1: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5426 | return MBEDTLS_SSL_HASH_SHA1; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5427 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5428 | #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] | 5429 | case MBEDTLS_MD_SHA224: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5430 | return MBEDTLS_SSL_HASH_SHA224; |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 5431 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5432 | #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] | 5433 | case MBEDTLS_MD_SHA256: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5434 | return MBEDTLS_SSL_HASH_SHA256; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5435 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5436 | #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] | 5437 | case MBEDTLS_MD_SHA384: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5438 | return MBEDTLS_SSL_HASH_SHA384; |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 5439 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5440 | #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] | 5441 | case MBEDTLS_MD_SHA512: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5442 | return MBEDTLS_SSL_HASH_SHA512; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5443 | #endif |
| 5444 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5445 | return MBEDTLS_SSL_HASH_NONE; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5446 | } |
| 5447 | } |
| 5448 | |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5449 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 5450 | * 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] | 5451 | * 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] | 5452 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5453 | 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] | 5454 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5455 | const uint16_t *group_list = mbedtls_ssl_get_groups(ssl); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5456 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5457 | if (group_list == NULL) { |
| 5458 | return -1; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5459 | } |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5460 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5461 | for (; *group_list != 0; group_list++) { |
| 5462 | if (*group_list == tls_id) { |
| 5463 | return 0; |
| 5464 | } |
| 5465 | } |
| 5466 | |
| 5467 | return -1; |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5468 | } |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 5469 | |
| 5470 | #if defined(MBEDTLS_ECP_C) |
| 5471 | /* |
| 5472 | * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id. |
| 5473 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5474 | 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] | 5475 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5476 | 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] | 5477 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5478 | if (tls_id == 0) { |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 5479 | return -1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5480 | } |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 5481 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5482 | return mbedtls_ssl_check_curve_tls_id(ssl, tls_id); |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 5483 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 5484 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5485 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5486 | #if defined(MBEDTLS_DEBUG_C) |
Valerio Setti | 67419f0 | 2023-01-04 16:12:42 +0100 | [diff] [blame] | 5487 | #define EC_NAME(_name_) _name_ |
| 5488 | #else |
| 5489 | #define EC_NAME(_name_) NULL |
| 5490 | #endif |
| 5491 | |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5492 | static const struct { |
| 5493 | uint16_t tls_id; |
| 5494 | mbedtls_ecp_group_id ecp_group_id; |
| 5495 | psa_ecc_family_t psa_family; |
| 5496 | uint16_t bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5497 | const char *name; |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5498 | } tls_id_match_table[] = |
| 5499 | { |
| 5500 | #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] | 5501 | { 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] | 5502 | #endif |
| 5503 | #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] | 5504 | { 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] | 5505 | #endif |
Valerio Setti | 67419f0 | 2023-01-04 16:12:42 +0100 | [diff] [blame] | 5506 | #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] | 5507 | { 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] | 5508 | #endif |
Valerio Setti | 67419f0 | 2023-01-04 16:12:42 +0100 | [diff] [blame] | 5509 | #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] | 5510 | { 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] | 5511 | #endif |
Valerio Setti | 67419f0 | 2023-01-04 16:12:42 +0100 | [diff] [blame] | 5512 | #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] | 5513 | { 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] | 5514 | #endif |
| 5515 | #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] | 5516 | { 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] | 5517 | #endif |
Valerio Setti | 67419f0 | 2023-01-04 16:12:42 +0100 | [diff] [blame] | 5518 | #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] | 5519 | { 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] | 5520 | #endif |
| 5521 | #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] | 5522 | { 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] | 5523 | #endif |
| 5524 | #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] | 5525 | { 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] | 5526 | #endif |
| 5527 | #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] | 5528 | { 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] | 5529 | #endif |
| 5530 | #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] | 5531 | { 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] | 5532 | #endif |
| 5533 | #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] | 5534 | { 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] | 5535 | #endif |
| 5536 | #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] | 5537 | { 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] | 5538 | #endif |
| 5539 | { 0, MBEDTLS_ECP_DP_NONE, 0, 0, NULL }, |
| 5540 | }; |
| 5541 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5542 | int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id, |
| 5543 | psa_ecc_family_t *family, |
| 5544 | size_t *bits) |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5545 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5546 | for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) { |
| 5547 | if (tls_id_match_table[i].tls_id == tls_id) { |
| 5548 | if (family != NULL) { |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5549 | *family = tls_id_match_table[i].psa_family; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5550 | } |
| 5551 | if (bits != NULL) { |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5552 | *bits = tls_id_match_table[i].bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5553 | } |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5554 | return PSA_SUCCESS; |
| 5555 | } |
| 5556 | } |
| 5557 | |
| 5558 | return PSA_ERROR_NOT_SUPPORTED; |
| 5559 | } |
| 5560 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5561 | 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] | 5562 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5563 | for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) { |
| 5564 | if (tls_id_match_table[i].tls_id == tls_id) { |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5565 | return tls_id_match_table[i].ecp_group_id; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5566 | } |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5567 | } |
| 5568 | |
| 5569 | return MBEDTLS_ECP_DP_NONE; |
| 5570 | } |
| 5571 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5572 | 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] | 5573 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5574 | for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE; |
| 5575 | i++) { |
| 5576 | if (tls_id_match_table[i].ecp_group_id == grp_id) { |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5577 | return tls_id_match_table[i].tls_id; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5578 | } |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5579 | } |
| 5580 | |
| 5581 | return 0; |
| 5582 | } |
| 5583 | |
Valerio Setti | 67419f0 | 2023-01-04 16:12:42 +0100 | [diff] [blame] | 5584 | #if defined(MBEDTLS_DEBUG_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5585 | 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] | 5586 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5587 | for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) { |
| 5588 | if (tls_id_match_table[i].tls_id == tls_id) { |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5589 | return tls_id_match_table[i].name; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5590 | } |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5591 | } |
| 5592 | |
| 5593 | return NULL; |
| 5594 | } |
Valerio Setti | 67419f0 | 2023-01-04 16:12:42 +0100 | [diff] [blame] | 5595 | #endif |
Valerio Setti | 18c9fed | 2022-12-30 17:44:24 +0100 | [diff] [blame] | 5596 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5597 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5598 | int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert, |
| 5599 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
| 5600 | int cert_endpoint, |
| 5601 | uint32_t *flags) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5602 | { |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5603 | int ret = 0; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5604 | int usage = 0; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5605 | const char *ext_oid; |
| 5606 | size_t ext_len; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5607 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5608 | if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) { |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5609 | /* Server part of the key exchange */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5610 | switch (ciphersuite->key_exchange) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5611 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 5612 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5613 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5614 | break; |
| 5615 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5616 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 5617 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 5618 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 5619 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5620 | break; |
| 5621 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5622 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 5623 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5624 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5625 | break; |
| 5626 | |
| 5627 | /* 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] | 5628 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 5629 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 5630 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 5631 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 5632 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5633 | usage = 0; |
| 5634 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5635 | } else { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5636 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ |
| 5637 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5638 | } |
| 5639 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5640 | if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5641 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5642 | ret = -1; |
| 5643 | } |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5644 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5645 | if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5646 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5647 | ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH); |
| 5648 | } else { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5649 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5650 | ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5651 | } |
| 5652 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5653 | 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] | 5654 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5655 | ret = -1; |
| 5656 | } |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 5657 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5658 | return ret; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5659 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5660 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 5661 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5662 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5663 | int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl, |
| 5664 | const mbedtls_md_type_t md, |
| 5665 | unsigned char *dst, |
| 5666 | size_t dst_len, |
| 5667 | size_t *olen) |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5668 | { |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5669 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 5670 | psa_hash_operation_t *hash_operation_to_clone; |
| 5671 | psa_hash_operation_t hash_operation = psa_hash_operation_init(); |
| 5672 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5673 | *olen = 0; |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5674 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5675 | switch (md) { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5676 | #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] | 5677 | case MBEDTLS_MD_SHA384: |
| 5678 | hash_operation_to_clone = &ssl->handshake->fin_sha384_psa; |
| 5679 | break; |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5680 | #endif |
| 5681 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5682 | #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] | 5683 | case MBEDTLS_MD_SHA256: |
| 5684 | hash_operation_to_clone = &ssl->handshake->fin_sha256_psa; |
| 5685 | break; |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5686 | #endif |
| 5687 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5688 | default: |
| 5689 | goto exit; |
| 5690 | } |
| 5691 | |
| 5692 | status = psa_hash_clone(hash_operation_to_clone, &hash_operation); |
| 5693 | if (status != PSA_SUCCESS) { |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5694 | goto exit; |
| 5695 | } |
| 5696 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5697 | status = psa_hash_finish(&hash_operation, dst, dst_len, olen); |
| 5698 | if (status != PSA_SUCCESS) { |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5699 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5700 | } |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 5701 | |
| 5702 | exit: |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 5703 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 5704 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 5705 | (void) ssl; |
| 5706 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5707 | return psa_ssl_status_to_mbedtls(status); |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5708 | } |
| 5709 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5710 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5711 | #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] | 5712 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5713 | static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl, |
| 5714 | unsigned char *dst, |
| 5715 | size_t dst_len, |
| 5716 | size_t *olen) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5717 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5718 | int ret; |
| 5719 | mbedtls_sha512_context sha512; |
| 5720 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5721 | if (dst_len < 48) { |
| 5722 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 5723 | } |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5724 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5725 | mbedtls_sha512_init(&sha512); |
| 5726 | mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5727 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5728 | if ((ret = mbedtls_sha512_finish(&sha512, dst)) != 0) { |
| 5729 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha512_finish", ret); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5730 | goto exit; |
| 5731 | } |
| 5732 | |
| 5733 | *olen = 48; |
| 5734 | |
| 5735 | exit: |
| 5736 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5737 | mbedtls_sha512_free(&sha512); |
| 5738 | return ret; |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5739 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5740 | #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] | 5741 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5742 | #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] | 5743 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5744 | static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl, |
| 5745 | unsigned char *dst, |
| 5746 | size_t dst_len, |
| 5747 | size_t *olen) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5748 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5749 | int ret; |
| 5750 | mbedtls_sha256_context sha256; |
| 5751 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5752 | if (dst_len < 32) { |
| 5753 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 5754 | } |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5755 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5756 | mbedtls_sha256_init(&sha256); |
| 5757 | mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256); |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5758 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5759 | if ((ret = mbedtls_sha256_finish(&sha256, dst)) != 0) { |
| 5760 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha256_finish", ret); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5761 | goto exit; |
| 5762 | } |
| 5763 | |
| 5764 | *olen = 32; |
| 5765 | |
| 5766 | exit: |
| 5767 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5768 | mbedtls_sha256_free(&sha256); |
| 5769 | return ret; |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5770 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5771 | #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] | 5772 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5773 | int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl, |
| 5774 | const mbedtls_md_type_t md, |
| 5775 | unsigned char *dst, |
| 5776 | size_t dst_len, |
| 5777 | size_t *olen) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5778 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5779 | switch (md) { |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5780 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5781 | #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] | 5782 | case MBEDTLS_MD_SHA384: |
| 5783 | return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5784 | #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] | 5785 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 5786 | #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] | 5787 | case MBEDTLS_MD_SHA256: |
| 5788 | return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen); |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 5789 | #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] | 5790 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5791 | default: |
Andrzej Kurek | 409248a | 2022-10-24 10:33:21 -0400 | [diff] [blame] | 5792 | #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] | 5793 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 5794 | (void) ssl; |
| 5795 | (void) dst; |
| 5796 | (void) dst_len; |
| 5797 | (void) olen; |
Andrzej Kurek | 409248a | 2022-10-24 10:33:21 -0400 | [diff] [blame] | 5798 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5799 | break; |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 5800 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5801 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5802 | } |
XiaokangQian | 647719a | 2021-12-07 09:16:29 +0000 | [diff] [blame] | 5803 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 5804 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 5805 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5806 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5807 | /* mbedtls_ssl_parse_sig_alg_ext() |
| 5808 | * |
| 5809 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 5810 | * value (TLS 1.3 RFC8446): |
| 5811 | * enum { |
| 5812 | * .... |
| 5813 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 5814 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 5815 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 5816 | * .... |
| 5817 | * } SignatureScheme; |
| 5818 | * |
| 5819 | * struct { |
| 5820 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 5821 | * } SignatureSchemeList; |
| 5822 | * |
| 5823 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 5824 | * value (TLS 1.2 RFC5246): |
| 5825 | * enum { |
| 5826 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 5827 | * sha512(6), (255) |
| 5828 | * } HashAlgorithm; |
| 5829 | * |
| 5830 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 5831 | * SignatureAlgorithm; |
| 5832 | * |
| 5833 | * struct { |
| 5834 | * HashAlgorithm hash; |
| 5835 | * SignatureAlgorithm signature; |
| 5836 | * } SignatureAndHashAlgorithm; |
| 5837 | * |
| 5838 | * SignatureAndHashAlgorithm |
| 5839 | * supported_signature_algorithms<2..2^16-2>; |
| 5840 | * |
| 5841 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 5842 | * generalization of the TLS 1.2 signature algorithm extension. |
| 5843 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 5844 | * `SignatureScheme` field of TLS 1.3 |
| 5845 | * |
| 5846 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5847 | int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl, |
| 5848 | const unsigned char *buf, |
| 5849 | const unsigned char *end) |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5850 | { |
| 5851 | const unsigned char *p = buf; |
| 5852 | size_t supported_sig_algs_len = 0; |
| 5853 | const unsigned char *supported_sig_algs_end; |
| 5854 | uint16_t sig_alg; |
| 5855 | uint32_t common_idx = 0; |
| 5856 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5857 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2); |
| 5858 | supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5859 | p += 2; |
| 5860 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5861 | memset(ssl->handshake->received_sig_algs, 0, |
| 5862 | sizeof(ssl->handshake->received_sig_algs)); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5863 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5864 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5865 | supported_sig_algs_end = p + supported_sig_algs_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5866 | while (p < supported_sig_algs_end) { |
| 5867 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2); |
| 5868 | sig_alg = MBEDTLS_GET_UINT16_BE(p, 0); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5869 | p += 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5870 | MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s", |
| 5871 | sig_alg, |
| 5872 | mbedtls_ssl_sig_alg_to_str(sig_alg))); |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5873 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5874 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 && |
| 5875 | (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) && |
| 5876 | mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5877 | continue; |
Jerry Yu | 2fe6c63 | 2022-06-29 10:02:38 +0800 | [diff] [blame] | 5878 | } |
| 5879 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5880 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5881 | MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s", |
| 5882 | mbedtls_ssl_sig_alg_to_str(sig_alg))); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5883 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5884 | if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5885 | ssl->handshake->received_sig_algs[common_idx] = sig_alg; |
| 5886 | common_idx += 1; |
| 5887 | } |
| 5888 | } |
| 5889 | /* Check that we consumed all the message. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5890 | if (p != end) { |
| 5891 | MBEDTLS_SSL_DEBUG_MSG(1, |
| 5892 | ("Signature algorithms extension length misaligned")); |
| 5893 | MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, |
| 5894 | MBEDTLS_ERR_SSL_DECODE_ERROR); |
| 5895 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5896 | } |
| 5897 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5898 | if (common_idx == 0) { |
| 5899 | MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common")); |
| 5900 | MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE, |
| 5901 | MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE); |
| 5902 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5903 | } |
| 5904 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 5905 | ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5906 | return 0; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5907 | } |
| 5908 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5909 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 5910 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5911 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5912 | |
| 5913 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5914 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5915 | static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation, |
| 5916 | mbedtls_svc_key_id_t key, |
| 5917 | psa_algorithm_t alg, |
| 5918 | const unsigned char *raw_psk, size_t raw_psk_length, |
| 5919 | const unsigned char *seed, size_t seed_length, |
| 5920 | const unsigned char *label, size_t label_length, |
| 5921 | const unsigned char *other_secret, |
| 5922 | size_t other_secret_length, |
| 5923 | size_t capacity) |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5924 | { |
| 5925 | psa_status_t status; |
| 5926 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5927 | status = psa_key_derivation_setup(derivation, alg); |
| 5928 | if (status != PSA_SUCCESS) { |
| 5929 | return status; |
| 5930 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5931 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5932 | if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 5933 | status = psa_key_derivation_input_bytes(derivation, |
| 5934 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 5935 | seed, seed_length); |
| 5936 | if (status != PSA_SUCCESS) { |
| 5937 | return status; |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5938 | } |
| 5939 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5940 | if (other_secret != NULL) { |
| 5941 | status = psa_key_derivation_input_bytes(derivation, |
| 5942 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 5943 | other_secret, other_secret_length); |
| 5944 | if (status != PSA_SUCCESS) { |
| 5945 | return status; |
| 5946 | } |
| 5947 | } |
| 5948 | |
| 5949 | if (mbedtls_svc_key_id_is_null(key)) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5950 | status = psa_key_derivation_input_bytes( |
| 5951 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5952 | raw_psk, raw_psk_length); |
| 5953 | } else { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5954 | status = psa_key_derivation_input_key( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5955 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5956 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5957 | if (status != PSA_SUCCESS) { |
| 5958 | return status; |
| 5959 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5960 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5961 | status = psa_key_derivation_input_bytes(derivation, |
| 5962 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 5963 | label, label_length); |
| 5964 | if (status != PSA_SUCCESS) { |
| 5965 | return status; |
| 5966 | } |
| 5967 | } else { |
| 5968 | return PSA_ERROR_NOT_SUPPORTED; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5969 | } |
| 5970 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5971 | status = psa_key_derivation_set_capacity(derivation, capacity); |
| 5972 | if (status != PSA_SUCCESS) { |
| 5973 | return status; |
| 5974 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5975 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5976 | return PSA_SUCCESS; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5977 | } |
| 5978 | |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 5979 | #if defined(PSA_WANT_ALG_SHA_384) || \ |
| 5980 | defined(PSA_WANT_ALG_SHA_256) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5981 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5982 | static int tls_prf_generic(mbedtls_md_type_t md_type, |
| 5983 | const unsigned char *secret, size_t slen, |
| 5984 | const char *label, |
| 5985 | const unsigned char *random, size_t rlen, |
| 5986 | unsigned char *dstbuf, size_t dlen) |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5987 | { |
| 5988 | psa_status_t status; |
| 5989 | psa_algorithm_t alg; |
| 5990 | mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5991 | psa_key_derivation_operation_t derivation = |
| 5992 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5993 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5994 | if (md_type == MBEDTLS_MD_SHA384) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5995 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5996 | } else { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5997 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5998 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5999 | |
| 6000 | /* Normally a "secret" should be long enough to be impossible to |
| 6001 | * find by brute force, and in particular should not be empty. But |
| 6002 | * this PRF is also used to derive an IV, in particular in EAP-TLS, |
| 6003 | * and for this use case it makes sense to have a 0-length "secret". |
| 6004 | * Since the key API doesn't allow importing a key of length 0, |
| 6005 | * keep master_key=0, which setup_psa_key_derivation() understands |
| 6006 | * to mean a 0-length "secret" input. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6007 | if (slen != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6008 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6009 | psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE); |
| 6010 | psa_set_key_algorithm(&key_attributes, alg); |
| 6011 | psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6012 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6013 | status = psa_import_key(&key_attributes, secret, slen, &master_key); |
| 6014 | if (status != PSA_SUCCESS) { |
| 6015 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 6016 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6017 | } |
| 6018 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6019 | status = setup_psa_key_derivation(&derivation, |
| 6020 | master_key, alg, |
| 6021 | NULL, 0, |
| 6022 | random, rlen, |
| 6023 | (unsigned char const *) label, |
| 6024 | (size_t) strlen(label), |
| 6025 | NULL, 0, |
| 6026 | dlen); |
| 6027 | if (status != PSA_SUCCESS) { |
| 6028 | psa_key_derivation_abort(&derivation); |
| 6029 | psa_destroy_key(master_key); |
| 6030 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6031 | } |
| 6032 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6033 | status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen); |
| 6034 | if (status != PSA_SUCCESS) { |
| 6035 | psa_key_derivation_abort(&derivation); |
| 6036 | psa_destroy_key(master_key); |
| 6037 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6038 | } |
| 6039 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6040 | status = psa_key_derivation_abort(&derivation); |
| 6041 | if (status != PSA_SUCCESS) { |
| 6042 | psa_destroy_key(master_key); |
| 6043 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6044 | } |
| 6045 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6046 | if (!mbedtls_svc_key_id_is_null(master_key)) { |
| 6047 | status = psa_destroy_key(master_key); |
| 6048 | } |
| 6049 | if (status != PSA_SUCCESS) { |
| 6050 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 6051 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6052 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6053 | return 0; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6054 | } |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 6055 | #endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6056 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6057 | |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 6058 | #if defined(MBEDTLS_MD_C) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6059 | (defined(MBEDTLS_SHA256_C) || \ |
| 6060 | defined(MBEDTLS_SHA384_C)) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6061 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6062 | static int tls_prf_generic(mbedtls_md_type_t md_type, |
| 6063 | const unsigned char *secret, size_t slen, |
| 6064 | const char *label, |
| 6065 | const unsigned char *random, size_t rlen, |
| 6066 | unsigned char *dstbuf, size_t dlen) |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6067 | { |
| 6068 | size_t nb; |
| 6069 | size_t i, j, k, md_len; |
| 6070 | unsigned char *tmp; |
| 6071 | size_t tmp_len = 0; |
| 6072 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 6073 | const mbedtls_md_info_t *md_info; |
| 6074 | mbedtls_md_context_t md_ctx; |
| 6075 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6076 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6077 | mbedtls_md_init(&md_ctx); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6078 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6079 | if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) { |
| 6080 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 6081 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6082 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6083 | md_len = mbedtls_md_get_size(md_info); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6084 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6085 | tmp_len = md_len + strlen(label) + rlen; |
| 6086 | tmp = mbedtls_calloc(1, tmp_len); |
| 6087 | if (tmp == NULL) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6088 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 6089 | goto exit; |
| 6090 | } |
| 6091 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6092 | nb = strlen(label); |
| 6093 | memcpy(tmp + md_len, label, nb); |
| 6094 | memcpy(tmp + md_len + nb, random, rlen); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6095 | nb += rlen; |
| 6096 | |
| 6097 | /* |
| 6098 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 6099 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6100 | if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6101 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6102 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6103 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6104 | ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen); |
| 6105 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6106 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6107 | } |
| 6108 | ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb); |
| 6109 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6110 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6111 | } |
| 6112 | ret = mbedtls_md_hmac_finish(&md_ctx, tmp); |
| 6113 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6114 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6115 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6116 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6117 | for (i = 0; i < dlen; i += md_len) { |
| 6118 | ret = mbedtls_md_hmac_reset(&md_ctx); |
| 6119 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6120 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6121 | } |
| 6122 | ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb); |
| 6123 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6124 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6125 | } |
| 6126 | ret = mbedtls_md_hmac_finish(&md_ctx, h_i); |
| 6127 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6128 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6129 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6130 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6131 | ret = mbedtls_md_hmac_reset(&md_ctx); |
| 6132 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6133 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6134 | } |
| 6135 | ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len); |
| 6136 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6137 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6138 | } |
| 6139 | ret = mbedtls_md_hmac_finish(&md_ctx, tmp); |
| 6140 | if (ret != 0) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6141 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6142 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6143 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6144 | k = (i + md_len > dlen) ? dlen % md_len : md_len; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6145 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6146 | for (j = 0; j < k; j++) { |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6147 | dstbuf[i + j] = h_i[j]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6148 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6149 | } |
| 6150 | |
| 6151 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6152 | mbedtls_md_free(&md_ctx); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6153 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6154 | if (tmp != NULL) { |
| 6155 | mbedtls_platform_zeroize(tmp, tmp_len); |
| 6156 | } |
Dave Rodgman | 29b9b2b | 2022-11-01 16:08:14 +0000 | [diff] [blame] | 6157 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6158 | mbedtls_platform_zeroize(h_i, sizeof(h_i)); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6159 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6160 | mbedtls_free(tmp); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6161 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6162 | return ret; |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6163 | } |
Andrzej Kurek | 57d1063 | 2022-10-24 10:32:01 -0400 | [diff] [blame] | 6164 | #endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */ |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6165 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6166 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6167 | #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] | 6168 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6169 | static int tls_prf_sha256(const unsigned char *secret, size_t slen, |
| 6170 | const char *label, |
| 6171 | const unsigned char *random, size_t rlen, |
| 6172 | unsigned char *dstbuf, size_t dlen) |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6173 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6174 | return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen, |
| 6175 | label, random, rlen, dstbuf, dlen); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6176 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 6177 | #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] | 6178 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6179 | #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] | 6180 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6181 | static int tls_prf_sha384(const unsigned char *secret, size_t slen, |
| 6182 | const char *label, |
| 6183 | const unsigned char *random, size_t rlen, |
| 6184 | unsigned char *dstbuf, size_t dlen) |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6185 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6186 | return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen, |
| 6187 | label, random, rlen, dstbuf, dlen); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 6188 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 6189 | #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] | 6190 | |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6191 | /* |
| 6192 | * Set appropriate PRF function and other SSL / TLS1.2 functions |
| 6193 | * |
| 6194 | * Inputs: |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6195 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
| 6196 | * |
| 6197 | * Outputs: |
| 6198 | * - the tls_prf, calc_verify and calc_finished members of handshake structure |
| 6199 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6200 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6201 | static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake, |
| 6202 | mbedtls_md_type_t hash) |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6203 | { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6204 | #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] | 6205 | if (hash == MBEDTLS_MD_SHA384) { |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6206 | handshake->tls_prf = tls_prf_sha384; |
| 6207 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 6208 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6209 | } else |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6210 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6211 | #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] | 6212 | { |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 6213 | (void) hash; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6214 | handshake->tls_prf = tls_prf_sha256; |
| 6215 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 6216 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
| 6217 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 6218 | #else |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6219 | { |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6220 | (void) handshake; |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 6221 | (void) hash; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6222 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6223 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 6224 | #endif |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6225 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6226 | return 0; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 6227 | } |
Jerry Yu | d6ab235 | 2022-02-17 14:03:43 +0800 | [diff] [blame] | 6228 | |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6229 | /* |
| 6230 | * Compute master secret if needed |
| 6231 | * |
| 6232 | * Parameters: |
| 6233 | * [in/out] handshake |
| 6234 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf |
| 6235 | * (PSA-PSK) ciphersuite_info, psk_opaque |
| 6236 | * [out] premaster (cleared) |
| 6237 | * [out] master |
| 6238 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK |
| 6239 | * debug: conf->f_dbg, conf->p_dbg |
| 6240 | * EMS: passed to calc_verify (debug + session_negotiate) |
Ronald Cron | a25cf58 | 2022-03-07 11:10:36 +0100 | [diff] [blame] | 6241 | * PSA-PSA: conf |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6242 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6243 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6244 | static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake, |
| 6245 | unsigned char *master, |
| 6246 | const mbedtls_ssl_context *ssl) |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6247 | { |
| 6248 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6249 | |
| 6250 | /* cf. RFC 5246, Section 8.1: |
| 6251 | * "The master secret is always exactly 48 bytes in length." */ |
| 6252 | size_t const master_secret_len = 48; |
| 6253 | |
| 6254 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 6255 | unsigned char session_hash[48]; |
| 6256 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 6257 | |
| 6258 | /* The label for the KDF used for key expansion. |
| 6259 | * This is either "master secret" or "extended master secret" |
| 6260 | * depending on whether the Extended Master Secret extension |
| 6261 | * is used. */ |
| 6262 | char const *lbl = "master secret"; |
| 6263 | |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 6264 | /* The seed for the KDF used for key expansion. |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6265 | * - If the Extended Master Secret extension is not used, |
| 6266 | * this is ClientHello.Random + ServerHello.Random |
| 6267 | * (see Sect. 8.1 in RFC 5246). |
| 6268 | * - If the Extended Master Secret extension is used, |
| 6269 | * this is the transcript of the handshake so far. |
| 6270 | * (see Sect. 4 in RFC 7627). */ |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 6271 | unsigned char const *seed = handshake->randbytes; |
| 6272 | size_t seed_len = 64; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6273 | |
| 6274 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 6275 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ |
| 6276 | !(defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6277 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6278 | ssl = NULL; /* make sure we don't use it except for those cases */ |
| 6279 | (void) ssl; |
| 6280 | #endif |
| 6281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6282 | if (handshake->resume != 0) { |
| 6283 | MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)")); |
| 6284 | return 0; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6285 | } |
| 6286 | |
| 6287 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6288 | if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) { |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6289 | lbl = "extended master secret"; |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 6290 | seed = session_hash; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6291 | handshake->calc_verify(ssl, session_hash, &seed_len); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6292 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6293 | MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret", |
| 6294 | session_hash, seed_len); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6295 | } |
Przemek Stekiel | 169bf0b | 2022-04-29 07:53:29 +0200 | [diff] [blame] | 6296 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6297 | |
Przemek Stekiel | 99114f3 | 2022-04-22 11:20:09 +0200 | [diff] [blame] | 6298 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Przemek Stekiel | 8a4b7fd | 2022-04-28 09:22:22 +0200 | [diff] [blame] | 6299 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6300 | if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) { |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6301 | /* Perform PSK-to-MS expansion in a single step. */ |
| 6302 | psa_status_t status; |
| 6303 | psa_algorithm_t alg; |
| 6304 | mbedtls_svc_key_id_t psk; |
| 6305 | psa_key_derivation_operation_t derivation = |
| 6306 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 6307 | mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac; |
| 6308 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6309 | MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion")); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6310 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6311 | psk = mbedtls_ssl_get_opaque_psk(ssl); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6312 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6313 | if (hash_alg == MBEDTLS_MD_SHA384) { |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6314 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6315 | } else { |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6316 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6317 | } |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6318 | |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 6319 | size_t other_secret_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6320 | unsigned char *other_secret = NULL; |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 6321 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6322 | switch (handshake->ciphersuite_info->key_exchange) { |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6323 | /* Provide other secret. |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 6324 | * Other secret is stored in premaster, where first 2 bytes hold the |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6325 | * length of the other key. |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 6326 | */ |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6327 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 6328 | /* For RSA-PSK other key length is always 48 bytes. */ |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6329 | other_secret_len = 48; |
| 6330 | other_secret = handshake->premaster + 2; |
| 6331 | break; |
| 6332 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6333 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 6334 | other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0); |
| 6335 | other_secret = handshake->premaster + 2; |
| 6336 | break; |
| 6337 | default: |
| 6338 | break; |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 6339 | } |
| 6340 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6341 | status = setup_psa_key_derivation(&derivation, psk, alg, |
| 6342 | ssl->conf->psk, ssl->conf->psk_len, |
| 6343 | seed, seed_len, |
| 6344 | (unsigned char const *) lbl, |
| 6345 | (size_t) strlen(lbl), |
| 6346 | other_secret, other_secret_len, |
| 6347 | master_secret_len); |
| 6348 | if (status != PSA_SUCCESS) { |
| 6349 | psa_key_derivation_abort(&derivation); |
| 6350 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6351 | } |
| 6352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6353 | status = psa_key_derivation_output_bytes(&derivation, |
| 6354 | master, |
| 6355 | master_secret_len); |
| 6356 | if (status != PSA_SUCCESS) { |
| 6357 | psa_key_derivation_abort(&derivation); |
| 6358 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6359 | } |
| 6360 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6361 | status = psa_key_derivation_abort(&derivation); |
| 6362 | if (status != PSA_SUCCESS) { |
| 6363 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 6364 | } |
| 6365 | } else |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6366 | #endif |
| 6367 | { |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 6368 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6369 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 6370 | if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) { |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 6371 | psa_status_t status; |
| 6372 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 6373 | psa_key_derivation_operation_t derivation = |
| 6374 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 6375 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6376 | MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE")); |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 6377 | |
| 6378 | handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE; |
| 6379 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6380 | status = psa_key_derivation_setup(&derivation, alg); |
| 6381 | if (status != PSA_SUCCESS) { |
| 6382 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 6383 | } |
| 6384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6385 | status = psa_key_derivation_set_capacity(&derivation, |
| 6386 | PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE); |
| 6387 | if (status != PSA_SUCCESS) { |
| 6388 | psa_key_derivation_abort(&derivation); |
| 6389 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 6390 | } |
| 6391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6392 | status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx, |
| 6393 | &derivation); |
| 6394 | if (status != PSA_SUCCESS) { |
| 6395 | psa_key_derivation_abort(&derivation); |
| 6396 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 6397 | } |
| 6398 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6399 | status = psa_key_derivation_output_bytes(&derivation, |
| 6400 | handshake->premaster, |
| 6401 | handshake->pmslen); |
| 6402 | if (status != PSA_SUCCESS) { |
| 6403 | psa_key_derivation_abort(&derivation); |
| 6404 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 6405 | } |
| 6406 | |
| 6407 | status = psa_key_derivation_abort(&derivation); |
| 6408 | if (status != PSA_SUCCESS) { |
| 6409 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 6410 | } |
| 6411 | } |
| 6412 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6413 | ret = handshake->tls_prf(handshake->premaster, handshake->pmslen, |
| 6414 | lbl, seed, seed_len, |
| 6415 | master, |
| 6416 | master_secret_len); |
| 6417 | if (ret != 0) { |
| 6418 | MBEDTLS_SSL_DEBUG_RET(1, "prf", ret); |
| 6419 | return ret; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6420 | } |
| 6421 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6422 | MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret", |
| 6423 | handshake->premaster, |
| 6424 | handshake->pmslen); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6425 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6426 | mbedtls_platform_zeroize(handshake->premaster, |
| 6427 | sizeof(handshake->premaster)); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6428 | } |
| 6429 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6430 | return 0; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 6431 | } |
| 6432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6433 | int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl) |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6434 | { |
| 6435 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6436 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 6437 | ssl->handshake->ciphersuite_info; |
| 6438 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6439 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys")); |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6440 | |
| 6441 | /* Set PRF, calc_verify and calc_finished function pointers */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6442 | ret = ssl_set_handshake_prfs(ssl->handshake, |
| 6443 | ciphersuite_info->mac); |
| 6444 | if (ret != 0) { |
| 6445 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret); |
| 6446 | return ret; |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6447 | } |
| 6448 | |
| 6449 | /* Compute master secret if needed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6450 | ret = ssl_compute_master(ssl->handshake, |
| 6451 | ssl->session_negotiate->master, |
| 6452 | ssl); |
| 6453 | if (ret != 0) { |
| 6454 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret); |
| 6455 | return ret; |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6456 | } |
| 6457 | |
| 6458 | /* Swap the client and server random values: |
| 6459 | * - MS derivation wanted client+server (RFC 5246 8.1) |
| 6460 | * - key derivation wants server+client (RFC 5246 6.3) */ |
| 6461 | { |
| 6462 | unsigned char tmp[64]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6463 | memcpy(tmp, ssl->handshake->randbytes, 64); |
| 6464 | memcpy(ssl->handshake->randbytes, tmp + 32, 32); |
| 6465 | memcpy(ssl->handshake->randbytes + 32, tmp, 32); |
| 6466 | mbedtls_platform_zeroize(tmp, sizeof(tmp)); |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6467 | } |
| 6468 | |
| 6469 | /* Populate transform structure */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6470 | ret = ssl_tls12_populate_transform(ssl->transform_negotiate, |
| 6471 | ssl->session_negotiate->ciphersuite, |
| 6472 | ssl->session_negotiate->master, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 6473 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6474 | ssl->session_negotiate->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 6475 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6476 | ssl->handshake->tls_prf, |
| 6477 | ssl->handshake->randbytes, |
| 6478 | ssl->tls_version, |
| 6479 | ssl->conf->endpoint, |
| 6480 | ssl); |
| 6481 | if (ret != 0) { |
| 6482 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret); |
| 6483 | return ret; |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6484 | } |
| 6485 | |
| 6486 | /* We no longer need Server/ClientHello.random values */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6487 | mbedtls_platform_zeroize(ssl->handshake->randbytes, |
| 6488 | sizeof(ssl->handshake->randbytes)); |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6489 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6490 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys")); |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6491 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6492 | return 0; |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 6493 | } |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6494 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6495 | 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] | 6496 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6497 | switch (md) { |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6498 | #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] | 6499 | case MBEDTLS_SSL_HASH_SHA384: |
| 6500 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 6501 | break; |
| 6502 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6503 | #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] | 6504 | case MBEDTLS_SSL_HASH_SHA256: |
| 6505 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 6506 | break; |
| 6507 | #endif |
| 6508 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6509 | return -1; |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 6510 | } |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 6511 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 6512 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 6513 | (void) ssl; |
| 6514 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6515 | return 0; |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 6516 | } |
| 6517 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6518 | #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] | 6519 | void ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl, |
| 6520 | unsigned char *hash, |
| 6521 | size_t *hlen) |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6522 | { |
| 6523 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6524 | size_t hash_size; |
| 6525 | psa_status_t status; |
| 6526 | psa_hash_operation_t sha256_psa = psa_hash_operation_init(); |
| 6527 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6528 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha256")); |
| 6529 | status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa); |
| 6530 | if (status != PSA_SUCCESS) { |
| 6531 | MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed")); |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6532 | return; |
| 6533 | } |
| 6534 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6535 | status = psa_hash_finish(&sha256_psa, hash, 32, &hash_size); |
| 6536 | if (status != PSA_SUCCESS) { |
| 6537 | MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed")); |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6538 | return; |
| 6539 | } |
| 6540 | |
| 6541 | *hlen = 32; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6542 | MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen); |
| 6543 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify")); |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6544 | #else |
| 6545 | mbedtls_sha256_context sha256; |
| 6546 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6547 | mbedtls_sha256_init(&sha256); |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6548 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6549 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha256")); |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6550 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6551 | mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256); |
| 6552 | mbedtls_sha256_finish(&sha256, hash); |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6553 | |
| 6554 | *hlen = 32; |
| 6555 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6556 | MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen); |
| 6557 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify")); |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6558 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6559 | mbedtls_sha256_free(&sha256); |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 6560 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6561 | return; |
| 6562 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6563 | #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] | 6564 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6565 | #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] | 6566 | void ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl, |
| 6567 | unsigned char *hash, |
| 6568 | size_t *hlen) |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6569 | { |
| 6570 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6571 | size_t hash_size; |
| 6572 | psa_status_t status; |
| 6573 | psa_hash_operation_t sha384_psa = psa_hash_operation_init(); |
| 6574 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6575 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha384")); |
| 6576 | status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa); |
| 6577 | if (status != PSA_SUCCESS) { |
| 6578 | MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed")); |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6579 | return; |
| 6580 | } |
| 6581 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6582 | status = psa_hash_finish(&sha384_psa, hash, 48, &hash_size); |
| 6583 | if (status != PSA_SUCCESS) { |
| 6584 | MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed")); |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6585 | return; |
| 6586 | } |
| 6587 | |
| 6588 | *hlen = 48; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6589 | MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen); |
| 6590 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify")); |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6591 | #else |
| 6592 | mbedtls_sha512_context sha512; |
| 6593 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6594 | mbedtls_sha512_init(&sha512); |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6595 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6596 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha384")); |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6597 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6598 | mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384); |
| 6599 | mbedtls_sha512_finish(&sha512, hash); |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6600 | |
| 6601 | *hlen = 48; |
| 6602 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6603 | MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen); |
| 6604 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify")); |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6605 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6606 | mbedtls_sha512_free(&sha512); |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 6607 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6608 | return; |
| 6609 | } |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 6610 | #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] | 6611 | |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6612 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 6613 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6614 | 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] | 6615 | { |
| 6616 | unsigned char *p = ssl->handshake->premaster; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6617 | unsigned char *end = p + sizeof(ssl->handshake->premaster); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6618 | const unsigned char *psk = NULL; |
| 6619 | size_t psk_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6620 | int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6621 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6622 | if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) { |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6623 | /* |
| 6624 | * This should never happen because the existence of a PSK is always |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6625 | * checked before calling this function. |
| 6626 | * |
| 6627 | * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with |
Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 6628 | * the shared secret without PSK. |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6629 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6630 | if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) { |
| 6631 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 6632 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 6633 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6634 | } |
| 6635 | |
| 6636 | /* |
| 6637 | * PMS = struct { |
| 6638 | * opaque other_secret<0..2^16-1>; |
| 6639 | * opaque psk<0..2^16-1>; |
| 6640 | * }; |
| 6641 | * with "other_secret" depending on the particular key exchange |
| 6642 | */ |
| 6643 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6644 | if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) { |
| 6645 | if (end - p < 2) { |
| 6646 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 6647 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6648 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6649 | MBEDTLS_PUT_UINT16_BE(psk_len, p, 0); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6650 | p += 2; |
| 6651 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6652 | if (end < p || (size_t) (end - p) < psk_len) { |
| 6653 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 6654 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6655 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6656 | memset(p, 0, psk_len); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6657 | p += psk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6658 | } else |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6659 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 6660 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6661 | if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) { |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6662 | /* |
| 6663 | * other_secret already set by the ClientKeyExchange message, |
| 6664 | * and is 48 bytes long |
| 6665 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6666 | if (end - p < 2) { |
| 6667 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 6668 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6669 | |
| 6670 | *p++ = 0; |
| 6671 | *p++ = 48; |
| 6672 | p += 48; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6673 | } else |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6674 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 6675 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6676 | if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) { |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6677 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6678 | size_t len; |
| 6679 | |
| 6680 | /* Write length only when we know the actual value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6681 | if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx, |
| 6682 | p + 2, end - (p + 2), &len, |
| 6683 | ssl->conf->f_rng, ssl->conf->p_rng)) != 0) { |
| 6684 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret); |
| 6685 | return ret; |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6686 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6687 | MBEDTLS_PUT_UINT16_BE(len, p, 0); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6688 | p += 2 + len; |
| 6689 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6690 | MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K); |
| 6691 | } else |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6692 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6693 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6694 | if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) { |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6695 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6696 | size_t zlen; |
| 6697 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6698 | if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen, |
| 6699 | p + 2, end - (p + 2), |
| 6700 | ssl->conf->f_rng, ssl->conf->p_rng)) != 0) { |
| 6701 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret); |
| 6702 | return ret; |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6703 | } |
| 6704 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6705 | MBEDTLS_PUT_UINT16_BE(zlen, p, 0); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6706 | p += 2 + zlen; |
| 6707 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6708 | MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx, |
| 6709 | MBEDTLS_DEBUG_ECDH_Z); |
| 6710 | } else |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6711 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6712 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6713 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 6714 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6715 | } |
| 6716 | |
| 6717 | /* opaque psk<0..2^16-1>; */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6718 | if (end - p < 2) { |
| 6719 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 6720 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6721 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6722 | MBEDTLS_PUT_UINT16_BE(psk_len, p, 0); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6723 | p += 2; |
| 6724 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6725 | if (end < p || (size_t) (end - p) < psk_len) { |
| 6726 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 6727 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6728 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6729 | memcpy(p, psk, psk_len); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6730 | p += psk_len; |
| 6731 | |
| 6732 | ssl->handshake->pmslen = p - ssl->handshake->premaster; |
| 6733 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6734 | return 0; |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 6735 | } |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 6736 | #endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6737 | |
| 6738 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6739 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6740 | static int ssl_write_hello_request(mbedtls_ssl_context *ssl); |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6741 | |
| 6742 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6743 | int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl) |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6744 | { |
| 6745 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 6746 | * timeout if we were using the usual handshake doubling scheme */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6747 | if (ssl->conf->renego_max_records < 0) { |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6748 | uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1; |
| 6749 | unsigned char doublings = 1; |
| 6750 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6751 | while (ratio != 0) { |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6752 | ++doublings; |
| 6753 | ratio >>= 1; |
| 6754 | } |
| 6755 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6756 | if (++ssl->renego_records_seen > doublings) { |
| 6757 | MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request")); |
| 6758 | return 0; |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6759 | } |
| 6760 | } |
| 6761 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6762 | return ssl_write_hello_request(ssl); |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 6763 | } |
| 6764 | #endif |
| 6765 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6766 | |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6767 | /* |
| 6768 | * Handshake functions |
| 6769 | */ |
| 6770 | #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 6771 | /* No certificate support -> dummy functions */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6772 | int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6773 | { |
| 6774 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6775 | ssl->handshake->ciphersuite_info; |
| 6776 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6777 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6778 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6779 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { |
| 6780 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6781 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6782 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6783 | } |
| 6784 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6785 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 6786 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6787 | } |
| 6788 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6789 | int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6790 | { |
| 6791 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6792 | ssl->handshake->ciphersuite_info; |
| 6793 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6794 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6795 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6796 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { |
| 6797 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6798 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6799 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6800 | } |
| 6801 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6802 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 6803 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6804 | } |
| 6805 | |
| 6806 | #else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 6807 | /* Some certificate support -> implement write and parse */ |
| 6808 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6809 | int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6810 | { |
| 6811 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 6812 | size_t i, n; |
| 6813 | const mbedtls_x509_crt *crt; |
| 6814 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6815 | ssl->handshake->ciphersuite_info; |
| 6816 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6817 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6818 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6819 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { |
| 6820 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6821 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6822 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6823 | } |
| 6824 | |
| 6825 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6826 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 6827 | if (ssl->handshake->client_auth == 0) { |
| 6828 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6829 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6830 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6831 | } |
| 6832 | } |
| 6833 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 6834 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6835 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 6836 | if (mbedtls_ssl_own_cert(ssl) == NULL) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6837 | /* Should never happen because we shouldn't have picked the |
| 6838 | * ciphersuite if we don't have a certificate. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6839 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6840 | } |
| 6841 | } |
| 6842 | #endif |
| 6843 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6844 | MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl)); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6845 | |
| 6846 | /* |
| 6847 | * 0 . 0 handshake type |
| 6848 | * 1 . 3 handshake length |
| 6849 | * 4 . 6 length of all certs |
| 6850 | * 7 . 9 length of cert. 1 |
| 6851 | * 10 . n-1 peer certificate |
| 6852 | * n . n+2 length of cert. 2 |
| 6853 | * n+3 . ... upper level cert, etc. |
| 6854 | */ |
| 6855 | i = 7; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6856 | crt = mbedtls_ssl_own_cert(ssl); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6857 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6858 | while (crt != NULL) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6859 | n = crt->raw.len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6860 | if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) { |
| 6861 | MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET |
| 6862 | " > %" MBEDTLS_PRINTF_SIZET, |
| 6863 | i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN)); |
| 6864 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6865 | } |
| 6866 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6867 | ssl->out_msg[i] = MBEDTLS_BYTE_2(n); |
| 6868 | ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n); |
| 6869 | ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6870 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6871 | i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6872 | i += n; crt = crt->next; |
| 6873 | } |
| 6874 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6875 | ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7); |
| 6876 | ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7); |
| 6877 | ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6878 | |
| 6879 | ssl->out_msglen = i; |
| 6880 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 6881 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
| 6882 | |
| 6883 | ssl->state++; |
| 6884 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6885 | if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { |
| 6886 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret); |
| 6887 | return ret; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6888 | } |
| 6889 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6890 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6891 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6892 | return ret; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6893 | } |
| 6894 | |
| 6895 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6896 | |
| 6897 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6898 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6899 | static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl, |
| 6900 | unsigned char *crt_buf, |
| 6901 | size_t crt_buf_len) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6902 | { |
| 6903 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; |
| 6904 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6905 | if (peer_crt == NULL) { |
| 6906 | return -1; |
| 6907 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6908 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6909 | if (peer_crt->raw.len != crt_buf_len) { |
| 6910 | return -1; |
| 6911 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6912 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6913 | return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6914 | } |
| 6915 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6916 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6917 | static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl, |
| 6918 | unsigned char *crt_buf, |
| 6919 | size_t crt_buf_len) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6920 | { |
| 6921 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6922 | unsigned char const * const peer_cert_digest = |
| 6923 | ssl->session->peer_cert_digest; |
| 6924 | mbedtls_md_type_t const peer_cert_digest_type = |
| 6925 | ssl->session->peer_cert_digest_type; |
| 6926 | mbedtls_md_info_t const * const digest_info = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6927 | mbedtls_md_info_from_type(peer_cert_digest_type); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6928 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; |
| 6929 | size_t digest_len; |
| 6930 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6931 | if (peer_cert_digest == NULL || digest_info == NULL) { |
| 6932 | return -1; |
| 6933 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6934 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6935 | digest_len = mbedtls_md_get_size(digest_info); |
| 6936 | if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) { |
| 6937 | return -1; |
| 6938 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6939 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6940 | ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest); |
| 6941 | if (ret != 0) { |
| 6942 | return -1; |
| 6943 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6944 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6945 | return memcmp(tmp_digest, peer_cert_digest, digest_len); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6946 | } |
| 6947 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6948 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 6949 | |
| 6950 | /* |
| 6951 | * Once the certificate message is read, parse it into a cert chain and |
| 6952 | * perform basic checks, but leave actual verification to the caller |
| 6953 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6954 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6955 | static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl, |
| 6956 | mbedtls_x509_crt *chain) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6957 | { |
| 6958 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6959 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6960 | int crt_cnt = 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6961 | #endif |
| 6962 | size_t i, n; |
| 6963 | uint8_t alert; |
| 6964 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6965 | if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) { |
| 6966 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 6967 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6968 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
| 6969 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6970 | } |
| 6971 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6972 | if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) { |
| 6973 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6974 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
| 6975 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6976 | } |
| 6977 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6978 | if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) { |
| 6979 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 6980 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6981 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
| 6982 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6983 | } |
| 6984 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6985 | i = mbedtls_ssl_hs_hdr_len(ssl); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6986 | |
| 6987 | /* |
| 6988 | * Same message structure as in mbedtls_ssl_write_certificate() |
| 6989 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6990 | n = (ssl->in_msg[i+1] << 8) | ssl->in_msg[i+2]; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6991 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6992 | if (ssl->in_msg[i] != 0 || |
| 6993 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) { |
| 6994 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 6995 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6996 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
| 6997 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6998 | } |
| 6999 | |
| 7000 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 7001 | i += 3; |
| 7002 | |
| 7003 | /* Iterate through and parse the CRTs in the provided chain. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7004 | while (i < ssl->in_hslen) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7005 | /* Check that there's room for the next CRT's length fields. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7006 | if (i + 3 > ssl->in_hslen) { |
| 7007 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7008 | mbedtls_ssl_send_alert_message(ssl, |
| 7009 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7010 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
| 7011 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7012 | } |
| 7013 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 7014 | * anything beyond 2**16 ~ 64K. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7015 | if (ssl->in_msg[i] != 0) { |
| 7016 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7017 | mbedtls_ssl_send_alert_message(ssl, |
| 7018 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7019 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT); |
| 7020 | return MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7021 | } |
| 7022 | |
| 7023 | /* Read length of the next CRT in the chain. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7024 | n = ((unsigned int) ssl->in_msg[i + 1] << 8) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7025 | | (unsigned int) ssl->in_msg[i + 2]; |
| 7026 | i += 3; |
| 7027 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7028 | if (n < 128 || i + n > ssl->in_hslen) { |
| 7029 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 7030 | mbedtls_ssl_send_alert_message(ssl, |
| 7031 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7032 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
| 7033 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7034 | } |
| 7035 | |
| 7036 | /* Check if we're handling the first CRT in the chain. */ |
| 7037 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7038 | if (crt_cnt++ == 0 && |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7039 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7040 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7041 | /* During client-side renegotiation, check that the server's |
| 7042 | * end-CRTs hasn't changed compared to the initial handshake, |
| 7043 | * mitigating the triple handshake attack. On success, reuse |
| 7044 | * the original end-CRT instead of parsing it again. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7045 | MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation")); |
| 7046 | if (ssl_check_peer_crt_unchanged(ssl, |
| 7047 | &ssl->in_msg[i], |
| 7048 | n) != 0) { |
| 7049 | MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation")); |
| 7050 | mbedtls_ssl_send_alert_message(ssl, |
| 7051 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7052 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED); |
| 7053 | return MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7054 | } |
| 7055 | |
| 7056 | /* Now we can safely free the original chain. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7057 | ssl_clear_peer_cert(ssl->session); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7058 | } |
| 7059 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 7060 | |
| 7061 | /* Parse the next certificate in the chain. */ |
| 7062 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7063 | ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7064 | #else |
| 7065 | /* If we don't need to store the CRT chain permanently, parse |
| 7066 | * it in-place from the input buffer instead of making a copy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7067 | 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] | 7068 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7069 | switch (ret) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7070 | case 0: /*ok*/ |
| 7071 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 7072 | /* Ignore certificate with an unknown algorithm: maybe a |
| 7073 | prior certificate was already trusted. */ |
| 7074 | break; |
| 7075 | |
| 7076 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 7077 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 7078 | goto crt_parse_der_failed; |
| 7079 | |
| 7080 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 7081 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 7082 | goto crt_parse_der_failed; |
| 7083 | |
| 7084 | default: |
| 7085 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7086 | crt_parse_der_failed: |
| 7087 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert); |
| 7088 | MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret); |
| 7089 | return ret; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7090 | } |
| 7091 | |
| 7092 | i += n; |
| 7093 | } |
| 7094 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7095 | MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain); |
| 7096 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7097 | } |
| 7098 | |
| 7099 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7100 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7101 | 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] | 7102 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7103 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 7104 | return -1; |
| 7105 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7106 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7107 | if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) && |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7108 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 7109 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7110 | memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) { |
| 7111 | MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate")); |
| 7112 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7113 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7114 | return -1; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7115 | } |
| 7116 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7117 | |
| 7118 | /* Check if a certificate message is expected. |
| 7119 | * Return either |
| 7120 | * - SSL_CERTIFICATE_EXPECTED, or |
| 7121 | * - SSL_CERTIFICATE_SKIP |
| 7122 | * indicating whether a Certificate message is expected or not. |
| 7123 | */ |
| 7124 | #define SSL_CERTIFICATE_EXPECTED 0 |
| 7125 | #define SSL_CERTIFICATE_SKIP 1 |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7126 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7127 | static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl, |
| 7128 | int authmode) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7129 | { |
| 7130 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 7131 | ssl->handshake->ciphersuite_info; |
| 7132 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7133 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { |
| 7134 | return SSL_CERTIFICATE_SKIP; |
| 7135 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7136 | |
| 7137 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7138 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 7139 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) { |
| 7140 | return SSL_CERTIFICATE_SKIP; |
| 7141 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7142 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7143 | if (authmode == MBEDTLS_SSL_VERIFY_NONE) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7144 | ssl->session_negotiate->verify_result = |
| 7145 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7146 | return SSL_CERTIFICATE_SKIP; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7147 | } |
| 7148 | } |
| 7149 | #else |
| 7150 | ((void) authmode); |
| 7151 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7153 | return SSL_CERTIFICATE_EXPECTED; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7154 | } |
| 7155 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7156 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7157 | static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl, |
| 7158 | int authmode, |
| 7159 | mbedtls_x509_crt *chain, |
| 7160 | void *rs_ctx) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7161 | { |
| 7162 | int ret = 0; |
| 7163 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 7164 | ssl->handshake->ciphersuite_info; |
| 7165 | int have_ca_chain = 0; |
| 7166 | |
| 7167 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); |
| 7168 | void *p_vrfy; |
| 7169 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7170 | if (authmode == MBEDTLS_SSL_VERIFY_NONE) { |
| 7171 | return 0; |
| 7172 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7173 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7174 | if (ssl->f_vrfy != NULL) { |
| 7175 | MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7176 | f_vrfy = ssl->f_vrfy; |
| 7177 | p_vrfy = ssl->p_vrfy; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7178 | } else { |
| 7179 | MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7180 | f_vrfy = ssl->conf->f_vrfy; |
| 7181 | p_vrfy = ssl->conf->p_vrfy; |
| 7182 | } |
| 7183 | |
| 7184 | /* |
| 7185 | * Main check: verify certificate |
| 7186 | */ |
| 7187 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7188 | if (ssl->conf->f_ca_cb != NULL) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7189 | ((void) rs_ctx); |
| 7190 | have_ca_chain = 1; |
| 7191 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7192 | 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] | 7193 | ret = mbedtls_x509_crt_verify_with_ca_cb( |
| 7194 | chain, |
| 7195 | ssl->conf->f_ca_cb, |
| 7196 | ssl->conf->p_ca_cb, |
| 7197 | ssl->conf->cert_profile, |
| 7198 | ssl->hostname, |
| 7199 | &ssl->session_negotiate->verify_result, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7200 | f_vrfy, p_vrfy); |
| 7201 | } else |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7202 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 7203 | { |
| 7204 | mbedtls_x509_crt *ca_chain; |
| 7205 | mbedtls_x509_crl *ca_crl; |
| 7206 | |
| 7207 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7208 | if (ssl->handshake->sni_ca_chain != NULL) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7209 | ca_chain = ssl->handshake->sni_ca_chain; |
| 7210 | ca_crl = ssl->handshake->sni_ca_crl; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7211 | } else |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7212 | #endif |
| 7213 | { |
| 7214 | ca_chain = ssl->conf->ca_chain; |
| 7215 | ca_crl = ssl->conf->ca_crl; |
| 7216 | } |
| 7217 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7218 | if (ca_chain != NULL) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7219 | have_ca_chain = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7220 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7221 | |
| 7222 | ret = mbedtls_x509_crt_verify_restartable( |
| 7223 | chain, |
| 7224 | ca_chain, ca_crl, |
| 7225 | ssl->conf->cert_profile, |
| 7226 | ssl->hostname, |
| 7227 | &ssl->session_negotiate->verify_result, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7228 | f_vrfy, p_vrfy, rs_ctx); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7229 | } |
| 7230 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7231 | if (ret != 0) { |
| 7232 | MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7233 | } |
| 7234 | |
| 7235 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7236 | if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) { |
| 7237 | return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; |
| 7238 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7239 | #endif |
| 7240 | |
| 7241 | /* |
| 7242 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 7243 | */ |
| 7244 | |
| 7245 | #if defined(MBEDTLS_ECP_C) |
| 7246 | { |
| 7247 | const mbedtls_pk_context *pk = &chain->pk; |
| 7248 | |
Manuel Pégourié-Gonnard | 66b0d61 | 2022-06-17 10:49:29 +0200 | [diff] [blame] | 7249 | /* If certificate uses an EC key, make sure the curve is OK. |
| 7250 | * This is a public key, so it can't be opaque, so can_do() is a good |
| 7251 | * enough check to ensure pk_ec() is safe to use here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7252 | if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY)) { |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 7253 | /* and in the unlikely case the above assumption no longer holds |
| 7254 | * we are making sure that pk_ec() here does not return a NULL |
| 7255 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7256 | const mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*pk); |
| 7257 | if (ec == NULL) { |
| 7258 | MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_pk_ec() returned NULL")); |
| 7259 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 7260 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7261 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7262 | if (mbedtls_ssl_check_curve(ssl, ec->grp.id) != 0) { |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 7263 | ssl->session_negotiate->verify_result |= |
| 7264 | MBEDTLS_X509_BADCERT_BAD_KEY; |
| 7265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7266 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)")); |
| 7267 | if (ret == 0) { |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 7268 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7269 | } |
Leonid Rozenboim | 19e5973 | 2022-08-08 16:52:38 -0700 | [diff] [blame] | 7270 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7271 | } |
| 7272 | } |
| 7273 | #endif /* MBEDTLS_ECP_C */ |
| 7274 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7275 | if (mbedtls_ssl_check_cert_usage(chain, |
| 7276 | ciphersuite_info, |
| 7277 | !ssl->conf->endpoint, |
| 7278 | &ssl->session_negotiate->verify_result) != 0) { |
| 7279 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)")); |
| 7280 | if (ret == 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7281 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7282 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7283 | } |
| 7284 | |
| 7285 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a |
| 7286 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, |
| 7287 | * with details encoded in the verification flags. All other kinds |
| 7288 | * of error codes, including those from the user provided f_vrfy |
| 7289 | * functions, are treated as fatal and lead to a failure of |
| 7290 | * ssl_parse_certificate even if verification was optional. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7291 | if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
| 7292 | (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
| 7293 | ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7294 | ret = 0; |
| 7295 | } |
| 7296 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7297 | if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) { |
| 7298 | MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7299 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
| 7300 | } |
| 7301 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7302 | if (ret != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7303 | uint8_t alert; |
| 7304 | |
| 7305 | /* The certificate may have been rejected for several reasons. |
| 7306 | Pick one and send the corresponding alert. Which alert to send |
| 7307 | may be a subject of debate in some cases. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7308 | if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7309 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7310 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7311 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7312 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7313 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7314 | } 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] | 7315 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7316 | } 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] | 7317 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7318 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7319 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7320 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7321 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7322 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7323 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7324 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7325 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7326 | } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7327 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7328 | } else { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7329 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7330 | } |
| 7331 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7332 | alert); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7333 | } |
| 7334 | |
| 7335 | #if defined(MBEDTLS_DEBUG_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7336 | if (ssl->session_negotiate->verify_result != 0) { |
| 7337 | MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x", |
| 7338 | (unsigned int) ssl->session_negotiate->verify_result)); |
| 7339 | } else { |
| 7340 | MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7341 | } |
| 7342 | #endif /* MBEDTLS_DEBUG_C */ |
| 7343 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7344 | return ret; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7345 | } |
| 7346 | |
| 7347 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7348 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7349 | static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl, |
| 7350 | unsigned char *start, size_t len) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7351 | { |
| 7352 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7353 | /* Remember digest of the peer's end-CRT. */ |
| 7354 | ssl->session_negotiate->peer_cert_digest = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7355 | mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN); |
| 7356 | if (ssl->session_negotiate->peer_cert_digest == NULL) { |
| 7357 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed", |
| 7358 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN)); |
| 7359 | mbedtls_ssl_send_alert_message(ssl, |
| 7360 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7361 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7362 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7363 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7364 | } |
| 7365 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7366 | ret = mbedtls_md(mbedtls_md_info_from_type( |
| 7367 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE), |
| 7368 | start, len, |
| 7369 | ssl->session_negotiate->peer_cert_digest); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7370 | |
| 7371 | ssl->session_negotiate->peer_cert_digest_type = |
| 7372 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 7373 | ssl->session_negotiate->peer_cert_digest_len = |
| 7374 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 7375 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7376 | return ret; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7377 | } |
| 7378 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 7379 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7380 | static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl, |
| 7381 | unsigned char *start, size_t len) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7382 | { |
| 7383 | unsigned char *end = start + len; |
| 7384 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7385 | |
| 7386 | /* Make a copy of the peer's raw public key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7387 | mbedtls_pk_init(&ssl->handshake->peer_pubkey); |
| 7388 | ret = mbedtls_pk_parse_subpubkey(&start, end, |
| 7389 | &ssl->handshake->peer_pubkey); |
| 7390 | if (ret != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7391 | /* We should have parsed the public key before. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7392 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7393 | } |
| 7394 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7395 | return 0; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7396 | } |
| 7397 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7398 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7399 | int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl) |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7400 | { |
| 7401 | int ret = 0; |
| 7402 | int crt_expected; |
| 7403 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 7404 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 7405 | ? ssl->handshake->sni_authmode |
| 7406 | : ssl->conf->authmode; |
| 7407 | #else |
| 7408 | const int authmode = ssl->conf->authmode; |
| 7409 | #endif |
| 7410 | void *rs_ctx = NULL; |
| 7411 | mbedtls_x509_crt *chain = NULL; |
| 7412 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7413 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7414 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7415 | crt_expected = ssl_parse_certificate_coordinate(ssl, authmode); |
| 7416 | if (crt_expected == SSL_CERTIFICATE_SKIP) { |
| 7417 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7418 | goto exit; |
| 7419 | } |
| 7420 | |
| 7421 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7422 | if (ssl->handshake->ecrs_enabled && |
| 7423 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7424 | chain = ssl->handshake->ecrs_peer_cert; |
| 7425 | ssl->handshake->ecrs_peer_cert = NULL; |
| 7426 | goto crt_verify; |
| 7427 | } |
| 7428 | #endif |
| 7429 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7430 | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7431 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 7432 | let it decide whether to alert. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7433 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7434 | goto exit; |
| 7435 | } |
| 7436 | |
| 7437 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7438 | if (ssl_srv_check_client_no_crt_notification(ssl) == 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7439 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
| 7440 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7441 | if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7442 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7443 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7444 | |
| 7445 | goto exit; |
| 7446 | } |
| 7447 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7448 | |
| 7449 | /* Clear existing peer CRT structure in case we tried to |
| 7450 | * reuse a session but it failed, and allocate a new one. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7451 | ssl_clear_peer_cert(ssl->session_negotiate); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7452 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7453 | chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt)); |
| 7454 | if (chain == NULL) { |
| 7455 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", |
| 7456 | sizeof(mbedtls_x509_crt))); |
| 7457 | mbedtls_ssl_send_alert_message(ssl, |
| 7458 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7459 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7460 | |
| 7461 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 7462 | goto exit; |
| 7463 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7464 | mbedtls_x509_crt_init(chain); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7465 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7466 | ret = ssl_parse_certificate_chain(ssl, chain); |
| 7467 | if (ret != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7468 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7469 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7470 | |
| 7471 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7472 | if (ssl->handshake->ecrs_enabled) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7473 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7474 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7475 | |
| 7476 | crt_verify: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7477 | if (ssl->handshake->ecrs_enabled) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7478 | rs_ctx = &ssl->handshake->ecrs_ctx; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7479 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7480 | #endif |
| 7481 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7482 | ret = ssl_parse_certificate_verify(ssl, authmode, |
| 7483 | chain, rs_ctx); |
| 7484 | if (ret != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7485 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7486 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7487 | |
| 7488 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7489 | { |
| 7490 | unsigned char *crt_start, *pk_start; |
| 7491 | size_t crt_len, pk_len; |
| 7492 | |
| 7493 | /* We parse the CRT chain without copying, so |
| 7494 | * these pointers point into the input buffer, |
| 7495 | * and are hence still valid after freeing the |
| 7496 | * CRT chain. */ |
| 7497 | |
| 7498 | crt_start = chain->raw.p; |
| 7499 | crt_len = chain->raw.len; |
| 7500 | |
| 7501 | pk_start = chain->pk_raw.p; |
| 7502 | pk_len = chain->pk_raw.len; |
| 7503 | |
| 7504 | /* Free the CRT structures before computing |
| 7505 | * digest and copying the peer's public key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7506 | mbedtls_x509_crt_free(chain); |
| 7507 | mbedtls_free(chain); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7508 | chain = NULL; |
| 7509 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7510 | ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len); |
| 7511 | if (ret != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7512 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7513 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7514 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7515 | ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len); |
| 7516 | if (ret != 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7517 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7518 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7519 | } |
| 7520 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7521 | /* Pass ownership to session structure. */ |
| 7522 | ssl->session_negotiate->peer_cert = chain; |
| 7523 | chain = NULL; |
| 7524 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7525 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7526 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate")); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7527 | |
| 7528 | exit: |
| 7529 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7530 | if (ret == 0) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7531 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7532 | } |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7533 | |
| 7534 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7535 | if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) { |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7536 | ssl->handshake->ecrs_peer_cert = chain; |
| 7537 | chain = NULL; |
| 7538 | } |
| 7539 | #endif |
| 7540 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7541 | if (chain != NULL) { |
| 7542 | mbedtls_x509_crt_free(chain); |
| 7543 | mbedtls_free(chain); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7544 | } |
| 7545 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7546 | return ret; |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 7547 | } |
| 7548 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 7549 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7550 | #if defined(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] | 7551 | static void ssl_calc_finished_tls_sha256( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7552 | mbedtls_ssl_context *ssl, unsigned char *buf, int from) |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7553 | { |
| 7554 | int len = 12; |
| 7555 | const char *sender; |
| 7556 | unsigned char padbuf[32]; |
| 7557 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7558 | size_t hash_size; |
| 7559 | psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT; |
| 7560 | psa_status_t status; |
| 7561 | #else |
| 7562 | mbedtls_sha256_context sha256; |
| 7563 | #endif |
| 7564 | |
| 7565 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7566 | if (!session) { |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7567 | session = ssl->session; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7568 | } |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7569 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7570 | sender = (from == MBEDTLS_SSL_IS_CLIENT) |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7571 | ? "client finished" |
| 7572 | : "server finished"; |
| 7573 | |
| 7574 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7575 | sha256_psa = psa_hash_operation_init(); |
| 7576 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7577 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha256")); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7578 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7579 | status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa); |
| 7580 | if (status != PSA_SUCCESS) { |
| 7581 | MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed")); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7582 | return; |
| 7583 | } |
| 7584 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7585 | status = psa_hash_finish(&sha256_psa, padbuf, sizeof(padbuf), &hash_size); |
| 7586 | if (status != PSA_SUCCESS) { |
| 7587 | MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed")); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7588 | return; |
| 7589 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7590 | MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 32); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7591 | #else |
| 7592 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7593 | mbedtls_sha256_init(&sha256); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7594 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7595 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha256")); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7596 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7597 | mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7598 | |
| 7599 | /* |
| 7600 | * TLSv1.2: |
| 7601 | * hash = PRF( master, finished_label, |
| 7602 | * Hash( handshake ) )[0.11] |
| 7603 | */ |
| 7604 | |
| 7605 | #if !defined(MBEDTLS_SHA256_ALT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7606 | MBEDTLS_SSL_DEBUG_BUF(4, "finished sha2 state", (unsigned char *) |
| 7607 | sha256.state, sizeof(sha256.state)); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7608 | #endif |
| 7609 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7610 | mbedtls_sha256_finish(&sha256, padbuf); |
| 7611 | mbedtls_sha256_free(&sha256); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7612 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7613 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7614 | ssl->handshake->tls_prf(session->master, 48, sender, |
| 7615 | padbuf, 32, buf, len); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7616 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7617 | MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7618 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7619 | mbedtls_platform_zeroize(padbuf, sizeof(padbuf)); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7620 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7621 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished")); |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 7622 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 7623 | #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] | 7624 | |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7625 | |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 7626 | #if defined(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] | 7627 | static void ssl_calc_finished_tls_sha384( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7628 | mbedtls_ssl_context *ssl, unsigned char *buf, int from) |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7629 | { |
| 7630 | int len = 12; |
| 7631 | const char *sender; |
| 7632 | unsigned char padbuf[48]; |
| 7633 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7634 | size_t hash_size; |
| 7635 | psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT; |
| 7636 | psa_status_t status; |
| 7637 | #else |
| 7638 | mbedtls_sha512_context sha512; |
| 7639 | #endif |
| 7640 | |
| 7641 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7642 | if (!session) { |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7643 | session = ssl->session; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7644 | } |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7645 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7646 | sender = (from == MBEDTLS_SSL_IS_CLIENT) |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7647 | ? "client finished" |
| 7648 | : "server finished"; |
| 7649 | |
| 7650 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7651 | sha384_psa = psa_hash_operation_init(); |
| 7652 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7653 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha384")); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7654 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7655 | status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa); |
| 7656 | if (status != PSA_SUCCESS) { |
| 7657 | MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed")); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7658 | return; |
| 7659 | } |
| 7660 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7661 | status = psa_hash_finish(&sha384_psa, padbuf, sizeof(padbuf), &hash_size); |
| 7662 | if (status != PSA_SUCCESS) { |
| 7663 | MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed")); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7664 | return; |
| 7665 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7666 | MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 48); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7667 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7668 | mbedtls_sha512_init(&sha512); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7669 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7670 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha384")); |
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_sha512_clone(&sha512, &ssl->handshake->fin_sha384); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7673 | |
| 7674 | /* |
| 7675 | * TLSv1.2: |
| 7676 | * hash = PRF( master, finished_label, |
| 7677 | * Hash( handshake ) )[0.11] |
| 7678 | */ |
| 7679 | |
| 7680 | #if !defined(MBEDTLS_SHA512_ALT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7681 | MBEDTLS_SSL_DEBUG_BUF(4, "finished sha512 state", (unsigned char *) |
| 7682 | sha512.state, sizeof(sha512.state)); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7683 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7684 | mbedtls_sha512_finish(&sha512, padbuf); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7685 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7686 | mbedtls_sha512_free(&sha512); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7687 | #endif |
| 7688 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7689 | ssl->handshake->tls_prf(session->master, 48, sender, |
| 7690 | padbuf, 48, buf, len); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7691 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7692 | MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", 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_platform_zeroize(padbuf, sizeof(padbuf)); |
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_SSL_DEBUG_MSG(2, ("<= calc finished")); |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 7697 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 7698 | #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] | 7699 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7700 | void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl) |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7701 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7702 | MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free")); |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7703 | |
| 7704 | /* |
| 7705 | * Free our handshake params |
| 7706 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7707 | mbedtls_ssl_handshake_free(ssl); |
| 7708 | mbedtls_free(ssl->handshake); |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7709 | ssl->handshake = NULL; |
| 7710 | |
| 7711 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 7712 | * Free the previous transform and switch in the current one |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7713 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7714 | if (ssl->transform) { |
| 7715 | mbedtls_ssl_transform_free(ssl->transform); |
| 7716 | mbedtls_free(ssl->transform); |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7717 | } |
| 7718 | ssl->transform = ssl->transform_negotiate; |
| 7719 | ssl->transform_negotiate = NULL; |
| 7720 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7721 | MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free")); |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 7722 | } |
| 7723 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7724 | void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl) |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7725 | { |
| 7726 | int resume = ssl->handshake->resume; |
| 7727 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7728 | MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup")); |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7729 | |
| 7730 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7731 | if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) { |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7732 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
| 7733 | ssl->renego_records_seen = 0; |
| 7734 | } |
| 7735 | #endif |
| 7736 | |
| 7737 | /* |
| 7738 | * Free the previous session and switch in the current one |
| 7739 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7740 | if (ssl->session) { |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7741 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 7742 | /* RFC 7366 3.1: keep the EtM state */ |
| 7743 | ssl->session_negotiate->encrypt_then_mac = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7744 | ssl->session->encrypt_then_mac; |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7745 | #endif |
| 7746 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7747 | mbedtls_ssl_session_free(ssl->session); |
| 7748 | mbedtls_free(ssl->session); |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7749 | } |
| 7750 | ssl->session = ssl->session_negotiate; |
| 7751 | ssl->session_negotiate = NULL; |
| 7752 | |
| 7753 | /* |
| 7754 | * Add cache entry |
| 7755 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7756 | if (ssl->conf->f_set_cache != NULL && |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7757 | ssl->session->id_len != 0 && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7758 | resume == 0) { |
| 7759 | if (ssl->conf->f_set_cache(ssl->conf->p_cache, |
| 7760 | ssl->session->id, |
| 7761 | ssl->session->id_len, |
| 7762 | ssl->session) != 0) { |
| 7763 | MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session")); |
| 7764 | } |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7765 | } |
| 7766 | |
| 7767 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7768 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 7769 | ssl->handshake->flight != NULL) { |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7770 | /* Cancel handshake timer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7771 | mbedtls_ssl_set_timer(ssl, 0); |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7772 | |
| 7773 | /* Keep last flight around in case we need to resend it: |
| 7774 | * we need the handshake and transform structures for that */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7775 | MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform")); |
| 7776 | } else |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7777 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7778 | mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl); |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7779 | |
Jerry Yu | 5ed73ff | 2022-10-27 13:08:42 +0800 | [diff] [blame] | 7780 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7781 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7782 | MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup")); |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 7783 | } |
| 7784 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7785 | int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl) |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7786 | { |
| 7787 | int ret, hash_len; |
| 7788 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7789 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished")); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7790 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7791 | mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7792 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7793 | ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7794 | |
| 7795 | /* |
| 7796 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 7797 | * may define some other value. Currently (early 2016), no defined |
| 7798 | * ciphersuite does this (and this is unlikely to change as activity has |
| 7799 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 7800 | */ |
| 7801 | hash_len = 12; |
| 7802 | |
| 7803 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7804 | ssl->verify_data_len = hash_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7805 | memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7806 | #endif |
| 7807 | |
| 7808 | ssl->out_msglen = 4 + hash_len; |
| 7809 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 7810 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
| 7811 | |
| 7812 | /* |
| 7813 | * In case of session resuming, invert the client and server |
| 7814 | * ChangeCipherSpec messages order. |
| 7815 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7816 | if (ssl->handshake->resume != 0) { |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7817 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7818 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7819 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7820 | } |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7821 | #endif |
| 7822 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7823 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7824 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7825 | } |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7826 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7827 | } else { |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7828 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7829 | } |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7830 | |
| 7831 | /* |
| 7832 | * Switch to our negotiated transform and session parameters for outbound |
| 7833 | * data. |
| 7834 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7835 | 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] | 7836 | |
| 7837 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7838 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7839 | unsigned char i; |
| 7840 | |
| 7841 | /* Remember current epoch settings for resending */ |
| 7842 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7843 | memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, |
| 7844 | sizeof(ssl->handshake->alt_out_ctr)); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7845 | |
| 7846 | /* Set sequence_number to zero */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7847 | 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] | 7848 | |
| 7849 | |
| 7850 | /* Increment epoch */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7851 | for (i = 2; i > 0; i--) { |
| 7852 | if (++ssl->cur_out_ctr[i - 1] != 0) { |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7853 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7854 | } |
| 7855 | } |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7856 | |
| 7857 | /* The loop goes to its end iff the counter is wrapping */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7858 | if (i == 0) { |
| 7859 | MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap")); |
| 7860 | return MBEDTLS_ERR_SSL_COUNTER_WRAPPING; |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7861 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7862 | } else |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7863 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7864 | memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr)); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7865 | |
| 7866 | ssl->transform_out = ssl->transform_negotiate; |
| 7867 | ssl->session_out = ssl->session_negotiate; |
| 7868 | |
| 7869 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7870 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 7871 | mbedtls_ssl_send_flight_completed(ssl); |
| 7872 | } |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7873 | #endif |
| 7874 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7875 | if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { |
| 7876 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret); |
| 7877 | return ret; |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7878 | } |
| 7879 | |
| 7880 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7881 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 7882 | (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) { |
| 7883 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret); |
| 7884 | return ret; |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7885 | } |
| 7886 | #endif |
| 7887 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7888 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished")); |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7889 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7890 | return 0; |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 7891 | } |
| 7892 | |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7893 | #define SSL_MAX_HASH_LEN 12 |
| 7894 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7895 | int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl) |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7896 | { |
| 7897 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7898 | unsigned int hash_len = 12; |
| 7899 | unsigned char buf[SSL_MAX_HASH_LEN]; |
| 7900 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7901 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished")); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7902 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7903 | ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7904 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7905 | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { |
| 7906 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7907 | goto exit; |
| 7908 | } |
| 7909 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7910 | if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) { |
| 7911 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message")); |
| 7912 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7913 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7914 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 7915 | goto exit; |
| 7916 | } |
| 7917 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7918 | if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) { |
| 7919 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7920 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7921 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 7922 | goto exit; |
| 7923 | } |
| 7924 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7925 | if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) { |
| 7926 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message")); |
| 7927 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7928 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7929 | ret = MBEDTLS_ERR_SSL_DECODE_ERROR; |
| 7930 | goto exit; |
| 7931 | } |
| 7932 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7933 | if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), |
| 7934 | buf, hash_len) != 0) { |
| 7935 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message")); |
| 7936 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7937 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7938 | ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
| 7939 | goto exit; |
| 7940 | } |
| 7941 | |
| 7942 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7943 | ssl->verify_data_len = hash_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7944 | memcpy(ssl->peer_verify_data, buf, hash_len); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7945 | #endif |
| 7946 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7947 | if (ssl->handshake->resume != 0) { |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7948 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7949 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7950 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7951 | } |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7952 | #endif |
| 7953 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7954 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7955 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7956 | } |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7957 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7958 | } else { |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7959 | ssl->state++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7960 | } |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7961 | |
| 7962 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7963 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 7964 | mbedtls_ssl_recv_flight_completed(ssl); |
| 7965 | } |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7966 | #endif |
| 7967 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7968 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished")); |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7969 | |
| 7970 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7971 | mbedtls_platform_zeroize(buf, hash_len); |
| 7972 | return ret; |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 7973 | } |
| 7974 | |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7975 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 7976 | /* |
| 7977 | * Helper to get TLS 1.2 PRF from ciphersuite |
| 7978 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) |
| 7979 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7980 | static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id) |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7981 | { |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7982 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7983 | mbedtls_ssl_ciphersuite_from_id(ciphersuite_id); |
Andrzej Kurek | 6832774 | 2022-10-03 06:18:18 -0400 | [diff] [blame] | 7984 | #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] | 7985 | if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) { |
| 7986 | return tls_prf_sha384; |
| 7987 | } else |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7988 | #endif |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 7989 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 7990 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7991 | if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) { |
| 7992 | return tls_prf_sha256; |
| 7993 | } |
Andrzej Kurek | 894edde | 2022-09-29 06:31:14 -0400 | [diff] [blame] | 7994 | } |
| 7995 | #endif |
| 7996 | #if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 7997 | !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) |
| 7998 | (void) ciphersuite_info; |
| 7999 | #endif |
Andrzej Kurek | eabeb30 | 2022-10-17 07:52:51 -0400 | [diff] [blame] | 8000 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8001 | return NULL; |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 8002 | } |
| 8003 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 8004 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8005 | 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] | 8006 | { |
| 8007 | ((void) tls_prf); |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 8008 | #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] | 8009 | if (tls_prf == tls_prf_sha384) { |
| 8010 | return MBEDTLS_SSL_TLS_PRF_SHA384; |
| 8011 | } else |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 8012 | #endif |
Andrzej Kurek | 25f2715 | 2022-08-17 16:09:31 -0400 | [diff] [blame] | 8013 | #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] | 8014 | if (tls_prf == tls_prf_sha256) { |
| 8015 | return MBEDTLS_SSL_TLS_PRF_SHA256; |
| 8016 | } else |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 8017 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8018 | return MBEDTLS_SSL_TLS_PRF_NONE; |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 8019 | } |
| 8020 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8021 | /* |
| 8022 | * Populate a transform structure with session keys and all the other |
| 8023 | * necessary information. |
| 8024 | * |
| 8025 | * Parameters: |
| 8026 | * - [in/out]: transform: structure to populate |
| 8027 | * [in] must be just initialised with mbedtls_ssl_transform_init() |
| 8028 | * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf() |
| 8029 | * - [in] ciphersuite |
| 8030 | * - [in] master |
| 8031 | * - [in] encrypt_then_mac |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8032 | * - [in] tls_prf: pointer to PRF to use for key derivation |
| 8033 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 8034 | * - [in] tls_version: TLS version |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8035 | * - [in] endpoint: client or server |
| 8036 | * - [in] ssl: used for: |
| 8037 | * - ssl->conf->{f,p}_export_keys |
| 8038 | * [in] optionally used for: |
| 8039 | * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg |
| 8040 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 8041 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8042 | static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform, |
| 8043 | int ciphersuite, |
| 8044 | const unsigned char master[48], |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8045 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8046 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8047 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8048 | ssl_tls_prf_t tls_prf, |
| 8049 | const unsigned char randbytes[64], |
| 8050 | mbedtls_ssl_protocol_version tls_version, |
| 8051 | unsigned endpoint, |
| 8052 | const mbedtls_ssl_context *ssl) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8053 | { |
| 8054 | int ret = 0; |
| 8055 | unsigned char keyblk[256]; |
| 8056 | unsigned char *key1; |
| 8057 | unsigned char *key2; |
| 8058 | unsigned char *mac_enc; |
| 8059 | unsigned char *mac_dec; |
| 8060 | size_t mac_key_len = 0; |
| 8061 | size_t iv_copy_len; |
| 8062 | size_t keylen; |
| 8063 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8064 | mbedtls_ssl_mode_t ssl_mode; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8065 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8066 | const mbedtls_cipher_info_t *cipher_info; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8067 | const mbedtls_md_info_t *md_info; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8068 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8069 | |
| 8070 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8071 | psa_key_type_t key_type; |
| 8072 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8073 | psa_algorithm_t alg; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8074 | psa_algorithm_t mac_alg = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8075 | size_t key_bits; |
| 8076 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 8077 | #endif |
| 8078 | |
| 8079 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 8080 | !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8081 | if (ssl->f_export_keys == NULL) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8082 | ssl = NULL; /* make sure we don't use it except for these cases */ |
| 8083 | (void) ssl; |
| 8084 | } |
| 8085 | #endif |
| 8086 | |
| 8087 | /* |
| 8088 | * Some data just needs copying into the structure |
| 8089 | */ |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8090 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8091 | transform->encrypt_then_mac = encrypt_then_mac; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8092 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 8093 | transform->tls_version = tls_version; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8094 | |
| 8095 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8096 | memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes)); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8097 | #endif |
| 8098 | |
| 8099 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8100 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8101 | /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform |
| 8102 | * generation separate. This should never happen. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8103 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8104 | } |
| 8105 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 8106 | |
| 8107 | /* |
| 8108 | * Get various info structures |
| 8109 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8110 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite); |
| 8111 | if (ciphersuite_info == NULL) { |
| 8112 | MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found", |
| 8113 | ciphersuite)); |
| 8114 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8115 | } |
| 8116 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 8117 | ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8118 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8119 | encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 8120 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8121 | ciphersuite_info); |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8122 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8123 | if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) { |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8124 | transform->taglen = |
| 8125 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8126 | } |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8127 | |
| 8128 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8129 | if ((status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher, |
| 8130 | transform->taglen, |
| 8131 | &alg, |
| 8132 | &key_type, |
| 8133 | &key_bits)) != PSA_SUCCESS) { |
| 8134 | ret = psa_ssl_status_to_mbedtls(status); |
| 8135 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8136 | goto end; |
| 8137 | } |
| 8138 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8139 | cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher); |
| 8140 | if (cipher_info == NULL) { |
| 8141 | MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found", |
| 8142 | ciphersuite_info->cipher)); |
| 8143 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8144 | } |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8145 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8146 | |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8147 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8148 | mac_alg = mbedtls_hash_info_psa_from_md(ciphersuite_info->mac); |
| 8149 | if (mac_alg == 0) { |
| 8150 | MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_hash_info_psa_from_md for %u not found", |
| 8151 | (unsigned) ciphersuite_info->mac)); |
| 8152 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8153 | } |
| 8154 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8155 | md_info = mbedtls_md_info_from_type(ciphersuite_info->mac); |
| 8156 | if (md_info == NULL) { |
| 8157 | MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found", |
| 8158 | (unsigned) ciphersuite_info->mac)); |
| 8159 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8160 | } |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8161 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8162 | |
| 8163 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 8164 | /* Copy own and peer's CID if the use of the CID |
| 8165 | * extension has been negotiated. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8166 | if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) { |
| 8167 | MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform")); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8168 | |
| 8169 | transform->in_cid_len = ssl->own_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8170 | memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len); |
| 8171 | MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid, |
| 8172 | transform->in_cid_len); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8173 | |
| 8174 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8175 | memcpy(transform->out_cid, ssl->handshake->peer_cid, |
| 8176 | ssl->handshake->peer_cid_len); |
| 8177 | MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid, |
| 8178 | transform->out_cid_len); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8179 | } |
| 8180 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 8181 | |
| 8182 | /* |
| 8183 | * Compute key block using the PRF |
| 8184 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8185 | ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256); |
| 8186 | if (ret != 0) { |
| 8187 | MBEDTLS_SSL_DEBUG_RET(1, "prf", ret); |
| 8188 | return ret; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8189 | } |
| 8190 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8191 | MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s", |
| 8192 | mbedtls_ssl_get_ciphersuite_name(ciphersuite))); |
| 8193 | MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48); |
| 8194 | MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64); |
| 8195 | MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8196 | |
| 8197 | /* |
| 8198 | * Determine the appropriate key, IV and MAC length. |
| 8199 | */ |
| 8200 | |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8201 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8202 | keylen = PSA_BITS_TO_BYTES(key_bits); |
| 8203 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8204 | keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8205 | #endif |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8206 | |
| 8207 | #if defined(MBEDTLS_GCM_C) || \ |
| 8208 | defined(MBEDTLS_CCM_C) || \ |
| 8209 | defined(MBEDTLS_CHACHAPOLY_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8210 | if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8211 | size_t explicit_ivlen; |
| 8212 | |
| 8213 | transform->maclen = 0; |
| 8214 | mac_key_len = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8215 | |
| 8216 | /* All modes haves 96-bit IVs, but the length of the static parts vary |
| 8217 | * with mode and version: |
| 8218 | * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes |
| 8219 | * (to be concatenated with a dynamically chosen IV of 8 Bytes) |
| 8220 | * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's |
| 8221 | * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record |
| 8222 | * sequence number). |
| 8223 | */ |
| 8224 | transform->ivlen = 12; |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 8225 | |
| 8226 | int is_chachapoly = 0; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8227 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8228 | is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8229 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8230 | is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info) |
| 8231 | == MBEDTLS_MODE_CHACHAPOLY); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8232 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
David Horstmann | 3b2276a | 2022-10-06 14:49:08 +0100 | [diff] [blame] | 8233 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8234 | if (is_chachapoly) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8235 | transform->fixed_ivlen = 12; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8236 | } else { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8237 | transform->fixed_ivlen = 4; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8238 | } |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8239 | |
| 8240 | /* Minimum length of encrypted record */ |
| 8241 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
| 8242 | transform->minlen = explicit_ivlen + transform->taglen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8243 | } else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8244 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
| 8245 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8246 | if (ssl_mode == MBEDTLS_SSL_MODE_STREAM || |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 8247 | ssl_mode == MBEDTLS_SSL_MODE_CBC || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8248 | ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) { |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8249 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8250 | size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8251 | #else |
| 8252 | size_t block_size = cipher_info->block_size; |
| 8253 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8254 | |
| 8255 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8256 | /* Get MAC length */ |
| 8257 | mac_key_len = PSA_HASH_LENGTH(mac_alg); |
| 8258 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8259 | /* Initialize HMAC contexts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8260 | if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 || |
| 8261 | (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) { |
| 8262 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8263 | goto end; |
| 8264 | } |
| 8265 | |
| 8266 | /* Get MAC length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8267 | mac_key_len = mbedtls_md_get_size(md_info); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 8268 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8269 | transform->maclen = mac_key_len; |
| 8270 | |
| 8271 | /* IV length */ |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8272 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8273 | transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8274 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8275 | transform->ivlen = cipher_info->iv_size; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 8276 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8277 | |
| 8278 | /* Minimum length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8279 | if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8280 | transform->minlen = transform->maclen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8281 | } else { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8282 | /* |
| 8283 | * GenericBlockCipher: |
| 8284 | * 1. if EtM is in use: one block plus MAC |
| 8285 | * otherwise: * first multiple of blocklen greater than maclen |
| 8286 | * 2. IV |
| 8287 | */ |
| 8288 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8289 | if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8290 | transform->minlen = transform->maclen |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8291 | + block_size; |
| 8292 | } else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8293 | #endif |
| 8294 | { |
| 8295 | transform->minlen = transform->maclen |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8296 | + block_size |
| 8297 | - transform->maclen % block_size; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8298 | } |
| 8299 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8300 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8301 | transform->minlen += transform->ivlen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8302 | } else { |
| 8303 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8304 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 8305 | goto end; |
| 8306 | } |
| 8307 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8308 | } else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8309 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 8310 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8311 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 8312 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8313 | } |
| 8314 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8315 | MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 8316 | (unsigned) keylen, |
| 8317 | (unsigned) transform->minlen, |
| 8318 | (unsigned) transform->ivlen, |
| 8319 | (unsigned) transform->maclen)); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8320 | |
| 8321 | /* |
| 8322 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 8323 | */ |
| 8324 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8325 | if (endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8326 | key1 = keyblk + mac_key_len * 2; |
| 8327 | key2 = keyblk + mac_key_len * 2 + keylen; |
| 8328 | |
| 8329 | mac_enc = keyblk; |
| 8330 | mac_dec = keyblk + mac_key_len; |
| 8331 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8332 | iv_copy_len = (transform->fixed_ivlen) ? |
| 8333 | transform->fixed_ivlen : transform->ivlen; |
| 8334 | memcpy(transform->iv_enc, key2 + keylen, iv_copy_len); |
| 8335 | memcpy(transform->iv_dec, key2 + keylen + iv_copy_len, |
| 8336 | iv_copy_len); |
| 8337 | } else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8338 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 8339 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8340 | if (endpoint == MBEDTLS_SSL_IS_SERVER) { |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8341 | key1 = keyblk + mac_key_len * 2 + keylen; |
| 8342 | key2 = keyblk + mac_key_len * 2; |
| 8343 | |
| 8344 | mac_enc = keyblk + mac_key_len; |
| 8345 | mac_dec = keyblk; |
| 8346 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8347 | iv_copy_len = (transform->fixed_ivlen) ? |
| 8348 | transform->fixed_ivlen : transform->ivlen; |
| 8349 | memcpy(transform->iv_dec, key1 + keylen, iv_copy_len); |
| 8350 | memcpy(transform->iv_enc, key1 + keylen + iv_copy_len, |
| 8351 | iv_copy_len); |
| 8352 | } else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8353 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 8354 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8355 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8356 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 8357 | goto end; |
| 8358 | } |
| 8359 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8360 | if (ssl != NULL && ssl->f_export_keys != NULL) { |
| 8361 | ssl->f_export_keys(ssl->p_export_keys, |
| 8362 | MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET, |
| 8363 | master, 48, |
| 8364 | randbytes + 32, |
| 8365 | randbytes, |
| 8366 | tls_prf_get_type(tls_prf)); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8367 | } |
| 8368 | |
| 8369 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8370 | transform->psa_alg = alg; |
| 8371 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8372 | if (alg != MBEDTLS_SSL_NULL_CIPHER) { |
| 8373 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 8374 | psa_set_key_algorithm(&attributes, alg); |
| 8375 | psa_set_key_type(&attributes, key_type); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8377 | if ((status = psa_import_key(&attributes, |
| 8378 | key1, |
| 8379 | PSA_BITS_TO_BYTES(key_bits), |
| 8380 | &transform->psa_key_enc)) != PSA_SUCCESS) { |
| 8381 | MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status); |
| 8382 | ret = psa_ssl_status_to_mbedtls(status); |
| 8383 | MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8384 | goto end; |
| 8385 | } |
| 8386 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8387 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8389 | if ((status = psa_import_key(&attributes, |
| 8390 | key2, |
| 8391 | PSA_BITS_TO_BYTES(key_bits), |
| 8392 | &transform->psa_key_dec)) != PSA_SUCCESS) { |
| 8393 | ret = psa_ssl_status_to_mbedtls(status); |
| 8394 | MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8395 | goto end; |
| 8396 | } |
| 8397 | } |
| 8398 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8399 | if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc, |
| 8400 | cipher_info)) != 0) { |
| 8401 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8402 | goto end; |
| 8403 | } |
| 8404 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8405 | if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec, |
| 8406 | cipher_info)) != 0) { |
| 8407 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8408 | goto end; |
| 8409 | } |
| 8410 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8411 | if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1, |
| 8412 | (int) mbedtls_cipher_info_get_key_bitlen(cipher_info), |
| 8413 | MBEDTLS_ENCRYPT)) != 0) { |
| 8414 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8415 | goto end; |
| 8416 | } |
| 8417 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8418 | if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2, |
| 8419 | (int) mbedtls_cipher_info_get_key_bitlen(cipher_info), |
| 8420 | MBEDTLS_DECRYPT)) != 0) { |
| 8421 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8422 | goto end; |
| 8423 | } |
| 8424 | |
| 8425 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8426 | if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) { |
| 8427 | if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc, |
| 8428 | MBEDTLS_PADDING_NONE)) != 0) { |
| 8429 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8430 | goto end; |
| 8431 | } |
| 8432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8433 | if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec, |
| 8434 | MBEDTLS_PADDING_NONE)) != 0) { |
| 8435 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret); |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8436 | goto end; |
| 8437 | } |
| 8438 | } |
| 8439 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 8440 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8441 | |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8442 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 8443 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 8444 | For AEAD-based ciphersuites, there is nothing to do here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8445 | if (mac_key_len != 0) { |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8446 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8447 | transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8449 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 8450 | psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg)); |
| 8451 | psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8452 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8453 | if ((status = psa_import_key(&attributes, |
| 8454 | mac_enc, mac_key_len, |
| 8455 | &transform->psa_mac_enc)) != PSA_SUCCESS) { |
| 8456 | ret = psa_ssl_status_to_mbedtls(status); |
| 8457 | MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8458 | goto end; |
| 8459 | } |
| 8460 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8461 | if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) || |
| 8462 | ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING) |
Andrzej Kurek | 8c95ac4 | 2022-08-17 16:17:00 -0400 | [diff] [blame] | 8463 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8464 | && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED) |
Andrzej Kurek | 8c95ac4 | 2022-08-17 16:17:00 -0400 | [diff] [blame] | 8465 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8466 | )) { |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8467 | /* mbedtls_ct_hmac() requires the key to be exportable */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8468 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT | |
| 8469 | PSA_KEY_USAGE_VERIFY_HASH); |
| 8470 | } else { |
| 8471 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 8472 | } |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8473 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8474 | if ((status = psa_import_key(&attributes, |
| 8475 | mac_dec, mac_key_len, |
| 8476 | &transform->psa_mac_dec)) != PSA_SUCCESS) { |
| 8477 | ret = psa_ssl_status_to_mbedtls(status); |
| 8478 | MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8479 | goto end; |
| 8480 | } |
| 8481 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8482 | ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len); |
| 8483 | if (ret != 0) { |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8484 | goto end; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8485 | } |
| 8486 | ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len); |
| 8487 | if (ret != 0) { |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8488 | goto end; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8489 | } |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 8490 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8491 | } |
| 8492 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 8493 | |
| 8494 | ((void) mac_dec); |
| 8495 | ((void) mac_enc); |
| 8496 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8497 | end: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8498 | mbedtls_platform_zeroize(keyblk, sizeof(keyblk)); |
| 8499 | return ret; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 8500 | } |
| 8501 | |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8502 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \ |
| 8503 | defined(MBEDTLS_USE_PSA_CRYPTO) |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8504 | int mbedtls_psa_ecjpake_read_round( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8505 | psa_pake_operation_t *pake_ctx, |
| 8506 | const unsigned char *buf, |
| 8507 | size_t len, mbedtls_ecjpake_rounds_t round) |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8508 | { |
| 8509 | psa_status_t status; |
| 8510 | size_t input_offset = 0; |
Valerio Setti | 819de86 | 2022-11-17 18:05:19 +0100 | [diff] [blame] | 8511 | /* |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8512 | * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice |
| 8513 | * At round two perform a single cycle |
| 8514 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8515 | unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8516 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8517 | for (; remaining_steps > 0; remaining_steps--) { |
| 8518 | for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8519 | step <= PSA_PAKE_STEP_ZK_PROOF; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8520 | ++step) { |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8521 | /* Length is stored at the first byte */ |
| 8522 | size_t length = buf[input_offset]; |
| 8523 | input_offset += 1; |
| 8524 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8525 | if (input_offset + length > len) { |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8526 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
| 8527 | } |
| 8528 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8529 | status = psa_pake_input(pake_ctx, step, |
| 8530 | buf + input_offset, length); |
| 8531 | if (status != PSA_SUCCESS) { |
| 8532 | return psa_ssl_status_to_mbedtls(status); |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8533 | } |
| 8534 | |
| 8535 | input_offset += length; |
| 8536 | } |
| 8537 | } |
| 8538 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8539 | if (input_offset != len) { |
Valerio Setti | 61ea17d | 2022-11-18 12:11:00 +0100 | [diff] [blame] | 8540 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8541 | } |
Valerio Setti | 30ebe11 | 2022-11-17 16:23:34 +0100 | [diff] [blame] | 8542 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8543 | return 0; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8544 | } |
| 8545 | |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8546 | int mbedtls_psa_ecjpake_write_round( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8547 | psa_pake_operation_t *pake_ctx, |
| 8548 | unsigned char *buf, |
| 8549 | size_t len, size_t *olen, |
| 8550 | mbedtls_ecjpake_rounds_t round) |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8551 | { |
| 8552 | psa_status_t status; |
| 8553 | size_t output_offset = 0; |
| 8554 | size_t output_len; |
Valerio Setti | 819de86 | 2022-11-17 18:05:19 +0100 | [diff] [blame] | 8555 | /* |
Valerio Setti | 6b3dab0 | 2022-11-17 17:14:54 +0100 | [diff] [blame] | 8556 | * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice |
| 8557 | * At round two perform a single cycle |
| 8558 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8559 | unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8560 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8561 | for (; remaining_steps > 0; remaining_steps--) { |
| 8562 | for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE; |
| 8563 | step <= PSA_PAKE_STEP_ZK_PROOF; |
| 8564 | ++step) { |
Valerio Setti | 79f6b6b | 2022-11-21 14:17:03 +0100 | [diff] [blame] | 8565 | /* |
Valerio Setti | d4a9b1a | 2022-11-22 11:11:10 +0100 | [diff] [blame] | 8566 | * For each step, prepend 1 byte with the length of the data as |
| 8567 | * given by psa_pake_output(). |
Valerio Setti | 79f6b6b | 2022-11-21 14:17:03 +0100 | [diff] [blame] | 8568 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8569 | status = psa_pake_output(pake_ctx, step, |
| 8570 | buf + output_offset + 1, |
| 8571 | len - output_offset - 1, |
| 8572 | &output_len); |
| 8573 | if (status != PSA_SUCCESS) { |
| 8574 | return psa_ssl_status_to_mbedtls(status); |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8575 | } |
| 8576 | |
Valerio Setti | 99d88c1 | 2022-11-22 16:03:43 +0100 | [diff] [blame] | 8577 | *(buf + output_offset) = (uint8_t) output_len; |
Valerio Setti | 79f6b6b | 2022-11-21 14:17:03 +0100 | [diff] [blame] | 8578 | |
| 8579 | output_offset += output_len + 1; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8580 | } |
| 8581 | } |
| 8582 | |
| 8583 | *olen = output_offset; |
| 8584 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8585 | return 0; |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8586 | } |
Valerio Setti | a08b1a4 | 2022-11-17 15:10:02 +0100 | [diff] [blame] | 8587 | #endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO |
| 8588 | |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8589 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8590 | int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl, |
| 8591 | unsigned char *hash, size_t *hashlen, |
| 8592 | unsigned char *data, size_t data_len, |
| 8593 | mbedtls_md_type_t md_alg) |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8594 | { |
| 8595 | psa_status_t status; |
| 8596 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8597 | 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] | 8598 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8599 | 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] | 8600 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8601 | if ((status = psa_hash_setup(&hash_operation, |
| 8602 | hash_alg)) != PSA_SUCCESS) { |
| 8603 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8604 | goto exit; |
| 8605 | } |
| 8606 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8607 | if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes, |
| 8608 | 64)) != PSA_SUCCESS) { |
| 8609 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8610 | goto exit; |
| 8611 | } |
| 8612 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8613 | if ((status = psa_hash_update(&hash_operation, |
| 8614 | data, data_len)) != PSA_SUCCESS) { |
| 8615 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8616 | goto exit; |
| 8617 | } |
| 8618 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8619 | if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE, |
| 8620 | hashlen)) != PSA_SUCCESS) { |
| 8621 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status); |
| 8622 | goto exit; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8623 | } |
| 8624 | |
| 8625 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8626 | if (status != PSA_SUCCESS) { |
| 8627 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8628 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
| 8629 | switch (status) { |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8630 | case PSA_ERROR_NOT_SUPPORTED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8631 | return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8632 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ |
| 8633 | case PSA_ERROR_BUFFER_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8634 | return MBEDTLS_ERR_MD_BAD_INPUT_DATA; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8635 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8636 | return MBEDTLS_ERR_MD_ALLOC_FAILED; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8637 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8638 | return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8639 | } |
| 8640 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8641 | return 0; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8642 | } |
| 8643 | |
| 8644 | #else |
| 8645 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8646 | int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl, |
| 8647 | unsigned char *hash, size_t *hashlen, |
| 8648 | unsigned char *data, size_t data_len, |
| 8649 | mbedtls_md_type_t md_alg) |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8650 | { |
| 8651 | int ret = 0; |
| 8652 | mbedtls_md_context_t ctx; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8653 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg); |
| 8654 | *hashlen = mbedtls_md_get_size(md_info); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8655 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8656 | 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] | 8657 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8658 | mbedtls_md_init(&ctx); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8659 | |
| 8660 | /* |
| 8661 | * digitally-signed struct { |
| 8662 | * opaque client_random[32]; |
| 8663 | * opaque server_random[32]; |
| 8664 | * ServerDHParams params; |
| 8665 | * }; |
| 8666 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8667 | if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) { |
| 8668 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8669 | goto exit; |
| 8670 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8671 | if ((ret = mbedtls_md_starts(&ctx)) != 0) { |
| 8672 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8673 | goto exit; |
| 8674 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8675 | if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) { |
| 8676 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8677 | goto exit; |
| 8678 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8679 | if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) { |
| 8680 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8681 | goto exit; |
| 8682 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8683 | if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) { |
| 8684 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8685 | goto exit; |
| 8686 | } |
| 8687 | |
| 8688 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8689 | mbedtls_md_free(&ctx); |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8690 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8691 | if (ret != 0) { |
| 8692 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8693 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
| 8694 | } |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8695 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8696 | return ret; |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 8697 | } |
| 8698 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8699 | |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8700 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 8701 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8702 | /* Find the preferred hash for a given signature algorithm. */ |
| 8703 | unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8704 | mbedtls_ssl_context *ssl, |
| 8705 | unsigned int sig_alg) |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8706 | { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8707 | unsigned int i; |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 8708 | uint16_t *received_sig_algs = ssl->handshake->received_sig_algs; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8709 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8710 | if (sig_alg == MBEDTLS_SSL_SIG_ANON) { |
| 8711 | return MBEDTLS_SSL_HASH_NONE; |
| 8712 | } |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 8713 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8714 | for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) { |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8715 | unsigned int hash_alg_received = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8716 | MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG( |
| 8717 | received_sig_algs[i]); |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8718 | unsigned int sig_alg_received = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8719 | MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG( |
| 8720 | received_sig_algs[i]); |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8721 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8722 | if (sig_alg == sig_alg_received) { |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8723 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8724 | if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) { |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8725 | psa_algorithm_t psa_hash_alg = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8726 | mbedtls_hash_info_psa_from_md(hash_alg_received); |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8727 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8728 | if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA && |
| 8729 | !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key, |
| 8730 | PSA_ALG_ECDSA(psa_hash_alg), |
| 8731 | PSA_KEY_USAGE_SIGN_HASH)) { |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8732 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8733 | } |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8734 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8735 | if (sig_alg_received == MBEDTLS_SSL_SIG_RSA && |
| 8736 | !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key, |
| 8737 | PSA_ALG_RSA_PKCS1V15_SIGN( |
| 8738 | psa_hash_alg), |
| 8739 | PSA_KEY_USAGE_SIGN_HASH)) { |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8740 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8741 | } |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8742 | } |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8743 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 96eceb8 | 2022-06-30 18:05:05 +0200 | [diff] [blame] | 8744 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8745 | return hash_alg_received; |
Neil Armstrong | 9f1176a | 2022-06-24 18:19:19 +0200 | [diff] [blame] | 8746 | } |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8747 | } |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8748 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8749 | return MBEDTLS_SSL_HASH_NONE; |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 8750 | } |
| 8751 | |
| 8752 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 8753 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8754 | /* Serialization of TLS 1.2 sessions: |
| 8755 | * |
| 8756 | * struct { |
| 8757 | * uint64 start_time; |
| 8758 | * uint8 ciphersuite[2]; // defined by the standard |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8759 | * uint8 session_id_len; // at most 32 |
| 8760 | * opaque session_id[32]; |
| 8761 | * opaque master[48]; // fixed length in the standard |
| 8762 | * uint32 verify_result; |
| 8763 | * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert |
| 8764 | * opaque ticket<0..2^24-1>; // length 0 means no ticket |
| 8765 | * uint32 ticket_lifetime; |
| 8766 | * uint8 mfl_code; // up to 255 according to standard |
| 8767 | * uint8 encrypt_then_mac; // 0 or 1 |
| 8768 | * } serialized_session_tls12; |
| 8769 | * |
| 8770 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8771 | static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session, |
| 8772 | unsigned char *buf, |
| 8773 | size_t buf_len) |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8774 | { |
| 8775 | unsigned char *p = buf; |
| 8776 | size_t used = 0; |
| 8777 | |
| 8778 | #if defined(MBEDTLS_HAVE_TIME) |
| 8779 | uint64_t start; |
| 8780 | #endif |
| 8781 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8782 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8783 | size_t cert_len; |
| 8784 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8785 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8786 | |
| 8787 | /* |
| 8788 | * Time |
| 8789 | */ |
| 8790 | #if defined(MBEDTLS_HAVE_TIME) |
| 8791 | used += 8; |
| 8792 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8793 | if (used <= buf_len) { |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8794 | start = (uint64_t) session->start; |
| 8795 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8796 | MBEDTLS_PUT_UINT64_BE(start, p, 0); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8797 | p += 8; |
| 8798 | } |
| 8799 | #endif /* MBEDTLS_HAVE_TIME */ |
| 8800 | |
| 8801 | /* |
| 8802 | * Basic mandatory fields |
| 8803 | */ |
| 8804 | used += 2 /* ciphersuite */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8805 | + 1 /* id_len */ |
| 8806 | + sizeof(session->id) |
| 8807 | + sizeof(session->master) |
| 8808 | + 4; /* verify_result */ |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8809 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8810 | if (used <= buf_len) { |
| 8811 | MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8812 | p += 2; |
| 8813 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8814 | *p++ = MBEDTLS_BYTE_0(session->id_len); |
| 8815 | memcpy(p, session->id, 32); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8816 | p += 32; |
| 8817 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8818 | memcpy(p, session->master, 48); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8819 | p += 48; |
| 8820 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8821 | MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8822 | p += 4; |
| 8823 | } |
| 8824 | |
| 8825 | /* |
| 8826 | * Peer's end-entity certificate |
| 8827 | */ |
| 8828 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8829 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8830 | if (session->peer_cert == NULL) { |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8831 | cert_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8832 | } else { |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8833 | cert_len = session->peer_cert->raw.len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8834 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8835 | |
| 8836 | used += 3 + cert_len; |
| 8837 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8838 | if (used <= buf_len) { |
| 8839 | *p++ = MBEDTLS_BYTE_2(cert_len); |
| 8840 | *p++ = MBEDTLS_BYTE_1(cert_len); |
| 8841 | *p++ = MBEDTLS_BYTE_0(cert_len); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8842 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8843 | if (session->peer_cert != NULL) { |
| 8844 | memcpy(p, session->peer_cert->raw.p, cert_len); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8845 | p += cert_len; |
| 8846 | } |
| 8847 | } |
| 8848 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8849 | if (session->peer_cert_digest != NULL) { |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8850 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8851 | if (used <= buf_len) { |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8852 | *p++ = (unsigned char) session->peer_cert_digest_type; |
| 8853 | *p++ = (unsigned char) session->peer_cert_digest_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8854 | memcpy(p, session->peer_cert_digest, |
| 8855 | session->peer_cert_digest_len); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8856 | p += session->peer_cert_digest_len; |
| 8857 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8858 | } else { |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8859 | used += 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8860 | if (used <= buf_len) { |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8861 | *p++ = (unsigned char) MBEDTLS_MD_NONE; |
| 8862 | *p++ = 0; |
| 8863 | } |
| 8864 | } |
| 8865 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8866 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8867 | |
| 8868 | /* |
| 8869 | * Session ticket if any, plus associated data |
| 8870 | */ |
| 8871 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 8872 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ |
| 8873 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8874 | if (used <= buf_len) { |
| 8875 | *p++ = MBEDTLS_BYTE_2(session->ticket_len); |
| 8876 | *p++ = MBEDTLS_BYTE_1(session->ticket_len); |
| 8877 | *p++ = MBEDTLS_BYTE_0(session->ticket_len); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8878 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8879 | if (session->ticket != NULL) { |
| 8880 | memcpy(p, session->ticket, session->ticket_len); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8881 | p += session->ticket_len; |
| 8882 | } |
| 8883 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8884 | MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8885 | p += 4; |
| 8886 | } |
| 8887 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 8888 | |
| 8889 | /* |
| 8890 | * Misc extension-related info |
| 8891 | */ |
| 8892 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 8893 | used += 1; |
| 8894 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8895 | if (used <= buf_len) { |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8896 | *p++ = session->mfl_code; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8897 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8898 | #endif |
| 8899 | |
| 8900 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 8901 | used += 1; |
| 8902 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8903 | if (used <= buf_len) { |
| 8904 | *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac); |
| 8905 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8906 | #endif |
| 8907 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8908 | return used; |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8909 | } |
| 8910 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 8911 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8912 | static int ssl_tls12_session_load(mbedtls_ssl_session *session, |
| 8913 | const unsigned char *buf, |
| 8914 | size_t len) |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8915 | { |
| 8916 | #if defined(MBEDTLS_HAVE_TIME) |
| 8917 | uint64_t start; |
| 8918 | #endif |
| 8919 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8920 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8921 | size_t cert_len; |
| 8922 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8923 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8924 | |
| 8925 | const unsigned char *p = buf; |
| 8926 | const unsigned char * const end = buf + len; |
| 8927 | |
| 8928 | /* |
| 8929 | * Time |
| 8930 | */ |
| 8931 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8932 | if (8 > (size_t) (end - p)) { |
| 8933 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 8934 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8935 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8936 | start = ((uint64_t) p[0] << 56) | |
| 8937 | ((uint64_t) p[1] << 48) | |
| 8938 | ((uint64_t) p[2] << 40) | |
| 8939 | ((uint64_t) p[3] << 32) | |
| 8940 | ((uint64_t) p[4] << 24) | |
| 8941 | ((uint64_t) p[5] << 16) | |
| 8942 | ((uint64_t) p[6] << 8) | |
| 8943 | ((uint64_t) p[7]); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8944 | p += 8; |
| 8945 | |
| 8946 | session->start = (time_t) start; |
| 8947 | #endif /* MBEDTLS_HAVE_TIME */ |
| 8948 | |
| 8949 | /* |
| 8950 | * Basic mandatory fields |
| 8951 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8952 | if (2 + 1 + 32 + 48 + 4 > (size_t) (end - p)) { |
| 8953 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 8954 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8955 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8956 | session->ciphersuite = (p[0] << 8) | p[1]; |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8957 | p += 2; |
| 8958 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8959 | session->id_len = *p++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8960 | memcpy(session->id, p, 32); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8961 | p += 32; |
| 8962 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8963 | memcpy(session->master, p, 48); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8964 | p += 48; |
| 8965 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8966 | session->verify_result = ((uint32_t) p[0] << 24) | |
| 8967 | ((uint32_t) p[1] << 16) | |
| 8968 | ((uint32_t) p[2] << 8) | |
| 8969 | ((uint32_t) p[3]); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8970 | p += 4; |
| 8971 | |
| 8972 | /* Immediately clear invalid pointer values that have been read, in case |
| 8973 | * we exit early before we replaced them with valid ones. */ |
| 8974 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8975 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8976 | session->peer_cert = NULL; |
| 8977 | #else |
| 8978 | session->peer_cert_digest = NULL; |
| 8979 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8980 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8981 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 8982 | session->ticket = NULL; |
| 8983 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 8984 | |
| 8985 | /* |
| 8986 | * Peer certificate |
| 8987 | */ |
| 8988 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8989 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 8990 | /* Deserialize CRT from the end of the ticket. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8991 | if (3 > (size_t) (end - p)) { |
| 8992 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 8993 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8994 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8995 | cert_len = (p[0] << 16) | (p[1] << 8) | p[2]; |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8996 | p += 3; |
| 8997 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8998 | if (cert_len != 0) { |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 8999 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 9000 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9001 | if (cert_len > (size_t) (end - p)) { |
| 9002 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9003 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9004 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9005 | session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt)); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9006 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9007 | if (session->peer_cert == NULL) { |
| 9008 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 9009 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9010 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9011 | mbedtls_x509_crt_init(session->peer_cert); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9012 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9013 | if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert, |
| 9014 | p, cert_len)) != 0) { |
| 9015 | mbedtls_x509_crt_free(session->peer_cert); |
| 9016 | mbedtls_free(session->peer_cert); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9017 | session->peer_cert = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9018 | return ret; |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9019 | } |
| 9020 | |
| 9021 | p += cert_len; |
| 9022 | } |
| 9023 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 9024 | /* Deserialize CRT digest from the end of the ticket. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9025 | if (2 > (size_t) (end - p)) { |
| 9026 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9027 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9028 | |
| 9029 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; |
| 9030 | session->peer_cert_digest_len = (size_t) *p++; |
| 9031 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9032 | if (session->peer_cert_digest_len != 0) { |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9033 | const mbedtls_md_info_t *md_info = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9034 | mbedtls_md_info_from_type(session->peer_cert_digest_type); |
| 9035 | if (md_info == NULL) { |
| 9036 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9037 | } |
| 9038 | if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) { |
| 9039 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9040 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9041 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9042 | if (session->peer_cert_digest_len > (size_t) (end - p)) { |
| 9043 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9044 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9045 | |
| 9046 | session->peer_cert_digest = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9047 | mbedtls_calloc(1, session->peer_cert_digest_len); |
| 9048 | if (session->peer_cert_digest == NULL) { |
| 9049 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 9050 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9051 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9052 | memcpy(session->peer_cert_digest, p, |
| 9053 | session->peer_cert_digest_len); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9054 | p += session->peer_cert_digest_len; |
| 9055 | } |
| 9056 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 9057 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 9058 | |
| 9059 | /* |
| 9060 | * Session ticket and associated data |
| 9061 | */ |
| 9062 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9063 | if (3 > (size_t) (end - p)) { |
| 9064 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9065 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9066 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9067 | session->ticket_len = (p[0] << 16) | (p[1] << 8) | p[2]; |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9068 | p += 3; |
| 9069 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9070 | if (session->ticket_len != 0) { |
| 9071 | if (session->ticket_len > (size_t) (end - p)) { |
| 9072 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9073 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9074 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9075 | session->ticket = mbedtls_calloc(1, session->ticket_len); |
| 9076 | if (session->ticket == NULL) { |
| 9077 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 9078 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9079 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9080 | memcpy(session->ticket, p, session->ticket_len); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9081 | p += session->ticket_len; |
| 9082 | } |
| 9083 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9084 | if (4 > (size_t) (end - p)) { |
| 9085 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9086 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9087 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9088 | session->ticket_lifetime = ((uint32_t) p[0] << 24) | |
| 9089 | ((uint32_t) p[1] << 16) | |
| 9090 | ((uint32_t) p[2] << 8) | |
| 9091 | ((uint32_t) p[3]); |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9092 | p += 4; |
| 9093 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 9094 | |
| 9095 | /* |
| 9096 | * Misc extension-related info |
| 9097 | */ |
| 9098 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9099 | if (1 > (size_t) (end - p)) { |
| 9100 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9101 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9102 | |
| 9103 | session->mfl_code = *p++; |
| 9104 | #endif |
| 9105 | |
| 9106 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9107 | if (1 > (size_t) (end - p)) { |
| 9108 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9109 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9110 | |
| 9111 | session->encrypt_then_mac = *p++; |
| 9112 | #endif |
| 9113 | |
| 9114 | /* Done, should have consumed entire buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9115 | if (p != end) { |
| 9116 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9117 | } |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9118 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9119 | return 0; |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 9120 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 9121 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 9122 | |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9123 | int mbedtls_ssl_validate_ciphersuite( |
| 9124 | const mbedtls_ssl_context *ssl, |
| 9125 | const mbedtls_ssl_ciphersuite_t *suite_info, |
| 9126 | mbedtls_ssl_protocol_version min_tls_version, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9127 | mbedtls_ssl_protocol_version max_tls_version) |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9128 | { |
| 9129 | (void) ssl; |
| 9130 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9131 | if (suite_info == NULL) { |
| 9132 | return -1; |
| 9133 | } |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9135 | if ((suite_info->min_tls_version > max_tls_version) || |
| 9136 | (suite_info->max_tls_version < min_tls_version)) { |
| 9137 | return -1; |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9138 | } |
| 9139 | |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 9140 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C) |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9141 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 9142 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9143 | if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
| 9144 | ssl->handshake->psa_pake_ctx_is_ok != 1) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 9145 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9146 | if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
| 9147 | mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0) |
Neil Armstrong | ca7d506 | 2022-05-31 14:43:23 +0200 | [diff] [blame] | 9148 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9149 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9150 | return -1; |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9151 | } |
| 9152 | #endif |
| 9153 | |
| 9154 | /* Don't suggest PSK-based ciphersuite if no PSK is available. */ |
| 9155 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9156 | if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) && |
| 9157 | mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) { |
| 9158 | return -1; |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9159 | } |
| 9160 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
| 9161 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 9162 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9163 | return 0; |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 9164 | } |
| 9165 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 9166 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9167 | /* |
| 9168 | * Function for writing a signature algorithm extension. |
| 9169 | * |
| 9170 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 9171 | * value (TLS 1.3 RFC8446): |
| 9172 | * enum { |
| 9173 | * .... |
| 9174 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 9175 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 9176 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 9177 | * .... |
| 9178 | * } SignatureScheme; |
| 9179 | * |
| 9180 | * struct { |
| 9181 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 9182 | * } SignatureSchemeList; |
| 9183 | * |
| 9184 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 9185 | * value (TLS 1.2 RFC5246): |
| 9186 | * enum { |
| 9187 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 9188 | * sha512(6), (255) |
| 9189 | * } HashAlgorithm; |
| 9190 | * |
| 9191 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 9192 | * SignatureAlgorithm; |
| 9193 | * |
| 9194 | * struct { |
| 9195 | * HashAlgorithm hash; |
| 9196 | * SignatureAlgorithm signature; |
| 9197 | * } SignatureAndHashAlgorithm; |
| 9198 | * |
| 9199 | * SignatureAndHashAlgorithm |
| 9200 | * supported_signature_algorithms<2..2^16-2>; |
| 9201 | * |
| 9202 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 9203 | * generalization of the TLS 1.2 signature algorithm extension. |
| 9204 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 9205 | * `SignatureScheme` field of TLS 1.3 |
| 9206 | * |
| 9207 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9208 | int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf, |
| 9209 | const unsigned char *end, size_t *out_len) |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9210 | { |
| 9211 | unsigned char *p = buf; |
| 9212 | unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */ |
| 9213 | size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */ |
| 9214 | |
| 9215 | *out_len = 0; |
| 9216 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9217 | MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension")); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9218 | |
| 9219 | /* Check if we have space for header and length field: |
| 9220 | * - extension_type (2 bytes) |
| 9221 | * - extension_data_length (2 bytes) |
| 9222 | * - supported_signature_algorithms_length (2 bytes) |
| 9223 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9224 | MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9225 | p += 6; |
| 9226 | |
| 9227 | /* |
| 9228 | * Write supported_signature_algorithms |
| 9229 | */ |
| 9230 | supported_sig_alg = p; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9231 | const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl); |
| 9232 | if (sig_alg == NULL) { |
| 9233 | return MBEDTLS_ERR_SSL_BAD_CONFIG; |
| 9234 | } |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9235 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9236 | for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) { |
| 9237 | MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s", |
| 9238 | *sig_alg, |
| 9239 | mbedtls_ssl_sig_alg_to_str(*sig_alg))); |
| 9240 | if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) { |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9241 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9242 | } |
| 9243 | MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2); |
| 9244 | MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9245 | p += 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9246 | MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s", |
| 9247 | *sig_alg, |
| 9248 | mbedtls_ssl_sig_alg_to_str(*sig_alg))); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9249 | } |
| 9250 | |
| 9251 | /* Length of supported_signature_algorithms */ |
| 9252 | supported_sig_alg_len = p - supported_sig_alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9253 | if (supported_sig_alg_len == 0) { |
| 9254 | MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined.")); |
| 9255 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9256 | } |
| 9257 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9258 | MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0); |
| 9259 | MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2); |
| 9260 | MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9261 | |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9262 | *out_len = p - buf; |
| 9263 | |
| 9264 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9265 | 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] | 9266 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | 0c354a2 | 2022-08-29 15:25:36 +0800 | [diff] [blame] | 9267 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9268 | return 0; |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9269 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 9270 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 9271 | |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9272 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9273 | /* |
| 9274 | * mbedtls_ssl_parse_server_name_ext |
| 9275 | * |
| 9276 | * Structure of server_name extension: |
| 9277 | * |
| 9278 | * enum { |
| 9279 | * host_name(0), (255) |
| 9280 | * } NameType; |
| 9281 | * opaque HostName<1..2^16-1>; |
| 9282 | * |
| 9283 | * struct { |
| 9284 | * NameType name_type; |
| 9285 | * select (name_type) { |
| 9286 | * case host_name: HostName; |
| 9287 | * } name; |
| 9288 | * } ServerName; |
| 9289 | * struct { |
| 9290 | * ServerName server_name_list<1..2^16-1> |
| 9291 | * } ServerNameList; |
| 9292 | */ |
Ronald Cron | ce7d76e | 2022-07-08 18:56:49 +0200 | [diff] [blame] | 9293 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9294 | int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl, |
| 9295 | const unsigned char *buf, |
| 9296 | const unsigned char *end) |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9297 | { |
| 9298 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 9299 | const unsigned char *p = buf; |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9300 | size_t server_name_list_len, hostname_len; |
| 9301 | const unsigned char *server_name_list_end; |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9302 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9303 | MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension")); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9304 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9305 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2); |
| 9306 | server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9307 | p += 2; |
| 9308 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9309 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len); |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 9310 | server_name_list_end = p + server_name_list_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9311 | while (p < server_name_list_end) { |
| 9312 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3); |
| 9313 | hostname_len = MBEDTLS_GET_UINT16_BE(p, 1); |
| 9314 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, |
| 9315 | hostname_len + 3); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9316 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9317 | if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) { |
XiaokangQian | 75fe8c7 | 2022-06-15 09:42:45 +0000 | [diff] [blame] | 9318 | /* sni_name is intended to be used only during the parsing of the |
| 9319 | * ClientHello message (it is reset to NULL before the end of |
| 9320 | * the message parsing). Thus it is ok to just point to the |
| 9321 | * reception buffer and not make a copy of it. |
| 9322 | */ |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 9323 | ssl->handshake->sni_name = p + 3; |
| 9324 | ssl->handshake->sni_name_len = hostname_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9325 | if (ssl->conf->f_sni == NULL) { |
| 9326 | return 0; |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9327 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9328 | ret = ssl->conf->f_sni(ssl->conf->p_sni, |
| 9329 | ssl, p + 3, hostname_len); |
| 9330 | if (ret != 0) { |
| 9331 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret); |
| 9332 | MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME, |
| 9333 | MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME); |
| 9334 | return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME; |
| 9335 | } |
| 9336 | return 0; |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9337 | } |
| 9338 | |
| 9339 | p += hostname_len + 3; |
| 9340 | } |
| 9341 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9342 | return 0; |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 9343 | } |
| 9344 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 9345 | |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9346 | #if defined(MBEDTLS_SSL_ALPN) |
Ronald Cron | ce7d76e | 2022-07-08 18:56:49 +0200 | [diff] [blame] | 9347 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9348 | int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl, |
| 9349 | const unsigned char *buf, |
| 9350 | const unsigned char *end) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9351 | { |
| 9352 | const unsigned char *p = buf; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9353 | size_t protocol_name_list_len; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9354 | const unsigned char *protocol_name_list; |
| 9355 | const unsigned char *protocol_name_list_end; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9356 | size_t protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9357 | |
| 9358 | /* If ALPN not configured, just ignore the extension */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9359 | if (ssl->conf->alpn_list == NULL) { |
| 9360 | return 0; |
| 9361 | } |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9362 | |
| 9363 | /* |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9364 | * RFC7301, section 3.1 |
| 9365 | * opaque ProtocolName<1..2^8-1>; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9366 | * |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9367 | * struct { |
| 9368 | * ProtocolName protocol_name_list<2..2^16-1> |
| 9369 | * } ProtocolNameList; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9370 | */ |
| 9371 | |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9372 | /* |
XiaokangQian | 0b776e2 | 2022-06-24 09:04:59 +0000 | [diff] [blame] | 9373 | * protocol_name_list_len 2 bytes |
| 9374 | * protocol_name_len 1 bytes |
| 9375 | * protocol_name >=1 byte |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9376 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9377 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9378 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9379 | protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9380 | p += 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9381 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9382 | protocol_name_list = p; |
| 9383 | protocol_name_list_end = p + protocol_name_list_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9384 | |
| 9385 | /* Validate peer's list (lengths) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9386 | while (p < protocol_name_list_end) { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9387 | protocol_name_len = *p++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9388 | MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end, |
| 9389 | protocol_name_len); |
| 9390 | if (protocol_name_len == 0) { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9391 | MBEDTLS_SSL_PEND_FATAL_ALERT( |
| 9392 | MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9393 | MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER); |
| 9394 | return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9395 | } |
| 9396 | |
| 9397 | p += protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9398 | } |
| 9399 | |
| 9400 | /* Use our order of preference */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9401 | for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) { |
| 9402 | size_t const alpn_len = strlen(*alpn); |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9403 | p = protocol_name_list; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9404 | while (p < protocol_name_list_end) { |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9405 | protocol_name_len = *p++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9406 | if (protocol_name_len == alpn_len && |
| 9407 | memcmp(p, *alpn, alpn_len) == 0) { |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9408 | ssl->alpn_chosen = *alpn; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9409 | return 0; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9410 | } |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9411 | |
| 9412 | p += protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9413 | } |
| 9414 | } |
| 9415 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9416 | /* If we get here, no match was found */ |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9417 | MBEDTLS_SSL_PEND_FATAL_ALERT( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9418 | MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL, |
| 9419 | MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL); |
| 9420 | return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9421 | } |
| 9422 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9423 | int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl, |
| 9424 | unsigned char *buf, |
| 9425 | unsigned char *end, |
| 9426 | size_t *out_len) |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9427 | { |
| 9428 | unsigned char *p = buf; |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9429 | size_t protocol_name_len; |
XiaokangQian | c740345 | 2022-06-23 03:24:12 +0000 | [diff] [blame] | 9430 | *out_len = 0; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9431 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9432 | if (ssl->alpn_chosen == NULL) { |
| 9433 | return 0; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9434 | } |
| 9435 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9436 | protocol_name_len = strlen(ssl->alpn_chosen); |
| 9437 | MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9438 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9439 | MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension")); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9440 | /* |
| 9441 | * 0 . 1 ext identifier |
| 9442 | * 2 . 3 ext length |
| 9443 | * 4 . 5 protocol list length |
| 9444 | * 6 . 6 protocol name length |
| 9445 | * 7 . 7+n protocol name |
| 9446 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9447 | MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9448 | |
XiaokangQian | 95d5f54 | 2022-06-24 02:29:26 +0000 | [diff] [blame] | 9449 | *out_len = 7 + protocol_name_len; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9450 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9451 | MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2); |
| 9452 | MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4); |
XiaokangQian | 0b776e2 | 2022-06-24 09:04:59 +0000 | [diff] [blame] | 9453 | /* Note: the length of the chosen protocol has been checked to be less |
| 9454 | * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`. |
| 9455 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9456 | p[6] = MBEDTLS_BYTE_0(protocol_name_len); |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9457 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9458 | memcpy(p + 7, ssl->alpn_chosen, protocol_name_len); |
Jerry Yu | b95dd36 | 2022-11-08 21:19:34 +0800 | [diff] [blame] | 9459 | |
| 9460 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9461 | 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] | 9462 | #endif |
| 9463 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9464 | return 0; |
XiaokangQian | acb3992 | 2022-06-17 10:18:48 +0000 | [diff] [blame] | 9465 | } |
| 9466 | #endif /* MBEDTLS_SSL_ALPN */ |
| 9467 | |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9468 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \ |
Xiaokang Qian | 0340929 | 2022-10-12 02:49:52 +0000 | [diff] [blame] | 9469 | defined(MBEDTLS_SSL_SESSION_TICKETS) && \ |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 9470 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \ |
Xiaokang Qian | ed3afcd | 2022-10-12 08:31:11 +0000 | [diff] [blame] | 9471 | defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9472 | int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session, |
| 9473 | const char *hostname) |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9474 | { |
| 9475 | /* Initialize to suppress unnecessary compiler warning */ |
| 9476 | size_t hostname_len = 0; |
| 9477 | |
| 9478 | /* Check if new hostname is valid before |
| 9479 | * making any change to current one */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9480 | if (hostname != NULL) { |
| 9481 | hostname_len = strlen(hostname); |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9482 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9483 | if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) { |
| 9484 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 9485 | } |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9486 | } |
| 9487 | |
| 9488 | /* Now it's clear that we will overwrite the old hostname, |
| 9489 | * so we can free it safely */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9490 | if (session->hostname != NULL) { |
| 9491 | mbedtls_platform_zeroize(session->hostname, |
| 9492 | strlen(session->hostname)); |
| 9493 | mbedtls_free(session->hostname); |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9494 | } |
| 9495 | |
| 9496 | /* Passing NULL as hostname shall clear the old one */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9497 | if (hostname == NULL) { |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9498 | session->hostname = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9499 | } else { |
| 9500 | session->hostname = mbedtls_calloc(1, hostname_len + 1); |
| 9501 | if (session->hostname == NULL) { |
| 9502 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 9503 | } |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9504 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9505 | memcpy(session->hostname, hostname, hostname_len); |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9506 | } |
| 9507 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9508 | return 0; |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9509 | } |
Xiaokang Qian | 0340929 | 2022-10-12 02:49:52 +0000 | [diff] [blame] | 9510 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 && |
| 9511 | MBEDTLS_SSL_SESSION_TICKETS && |
Xiaokang Qian | ed0620c | 2022-10-12 06:58:13 +0000 | [diff] [blame] | 9512 | MBEDTLS_SSL_SERVER_NAME_INDICATION && |
Xiaokang Qian | ed3afcd | 2022-10-12 08:31:11 +0000 | [diff] [blame] | 9513 | MBEDTLS_SSL_CLI_C */ |
Xiaokang Qian | a3b451f | 2022-10-11 06:20:56 +0000 | [diff] [blame] | 9514 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9515 | #endif /* MBEDTLS_SSL_TLS_C */ |