Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 1 | /* |
| 2 | * TLS 1.3 key schedule |
| 3 | * |
| 4 | * Copyright The Mbed TLS Contributors |
| 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. |
| 18 | */ |
| 19 | |
Hanno Becker | 58c5cea | 2020-09-08 10:31:33 +0100 | [diff] [blame] | 20 | #include "common.h" |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 21 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 22 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 23 | |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 24 | #include <stdint.h> |
| 25 | #include <string.h> |
| 26 | |
Jerry Yu | e3131ef | 2021-09-16 13:14:15 +0800 | [diff] [blame] | 27 | #include "mbedtls/hkdf.h" |
| 28 | #include "mbedtls/debug.h" |
| 29 | #include "mbedtls/error.h" |
Jerry Yu | e110d25 | 2022-05-05 10:19:22 +0800 | [diff] [blame] | 30 | #include "mbedtls/platform.h" |
Jerry Yu | e3131ef | 2021-09-16 13:14:15 +0800 | [diff] [blame] | 31 | |
| 32 | #include "ssl_misc.h" |
| 33 | #include "ssl_tls13_keys.h" |
Gabor Mezei | a3eecd2 | 2022-02-09 16:57:26 +0100 | [diff] [blame] | 34 | #include "ssl_tls13_invasive.h" |
| 35 | |
| 36 | #include "psa/crypto.h" |
Jerry Yu | e3131ef | 2021-09-16 13:14:15 +0800 | [diff] [blame] | 37 | |
Hanno Becker | 1413bd8 | 2020-09-09 12:46:09 +0100 | [diff] [blame] | 38 | #define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \ |
Hanno Becker | e4435ea | 2020-09-08 10:43:52 +0100 | [diff] [blame] | 39 | .name = string, |
| 40 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 41 | struct mbedtls_ssl_tls13_labels_struct const mbedtls_ssl_tls13_labels = |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 42 | { |
| 43 | /* This seems to work in C, despite the string literal being one |
| 44 | * character too long due to the 0-termination. */ |
Hanno Becker | e4435ea | 2020-09-08 10:43:52 +0100 | [diff] [blame] | 45 | MBEDTLS_SSL_TLS1_3_LABEL_LIST |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 46 | }; |
| 47 | |
Hanno Becker | a3a5a4e | 2020-09-08 11:33:48 +0100 | [diff] [blame] | 48 | #undef MBEDTLS_SSL_TLS1_3_LABEL |
Hanno Becker | e4435ea | 2020-09-08 10:43:52 +0100 | [diff] [blame] | 49 | |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 50 | /* |
| 51 | * This function creates a HkdfLabel structure used in the TLS 1.3 key schedule. |
| 52 | * |
| 53 | * The HkdfLabel is specified in RFC 8446 as follows: |
| 54 | * |
| 55 | * struct HkdfLabel { |
| 56 | * uint16 length; // Length of expanded key material |
| 57 | * opaque label<7..255>; // Always prefixed by "tls13 " |
| 58 | * opaque context<0..255>; // Usually a communication transcript hash |
| 59 | * }; |
| 60 | * |
| 61 | * Parameters: |
| 62 | * - desired_length: Length of expanded key material |
| 63 | * Even though the standard allows expansion to up to |
| 64 | * 2**16 Bytes, TLS 1.3 never uses expansion to more than |
| 65 | * 255 Bytes, so we require `desired_length` to be at most |
| 66 | * 255. This allows us to save a few Bytes of code by |
| 67 | * hardcoding the writing of the high bytes. |
Xiaofei Bai | feecbbb | 2021-11-23 07:24:58 +0000 | [diff] [blame] | 68 | * - (label, label_len): label + label length, without "tls13 " prefix |
| 69 | * The label length MUST be less than or equal to |
| 70 | * MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN |
| 71 | * It is the caller's responsibility to ensure this. |
| 72 | * All (label, label length) pairs used in TLS 1.3 |
| 73 | * can be obtained via MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(). |
| 74 | * - (ctx, ctx_len): context + context length |
| 75 | * The context length MUST be less than or equal to |
| 76 | * MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN |
| 77 | * It is the caller's responsibility to ensure this. |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 78 | * - dst: Target buffer for HkdfLabel structure, |
| 79 | * This MUST be a writable buffer of size |
| 80 | * at least SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN Bytes. |
Xiaofei Bai | feecbbb | 2021-11-23 07:24:58 +0000 | [diff] [blame] | 81 | * - dst_len: Pointer at which to store the actual length of |
| 82 | * the HkdfLabel structure on success. |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 83 | */ |
| 84 | |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 85 | static const char tls13_label_prefix[6] = "tls13 "; |
Hanno Becker | 2dfe132 | 2020-09-10 09:23:12 +0100 | [diff] [blame] | 86 | |
Hanno Becker | 9cb0a14 | 2020-09-08 10:48:14 +0100 | [diff] [blame] | 87 | #define SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( label_len, context_len ) \ |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 88 | ( 2 /* expansion length */ \ |
| 89 | + 1 /* label length */ \ |
Hanno Becker | 9cb0a14 | 2020-09-08 10:48:14 +0100 | [diff] [blame] | 90 | + label_len \ |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 91 | + 1 /* context length */ \ |
Hanno Becker | 9cb0a14 | 2020-09-08 10:48:14 +0100 | [diff] [blame] | 92 | + context_len ) |
| 93 | |
| 94 | #define SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN \ |
| 95 | SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( \ |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 96 | sizeof(tls13_label_prefix) + \ |
Hanno Becker | 9cb0a14 | 2020-09-08 10:48:14 +0100 | [diff] [blame] | 97 | MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN, \ |
| 98 | MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN ) |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 99 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 100 | static void ssl_tls13_hkdf_encode_label( |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 101 | size_t desired_length, |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 102 | const unsigned char *label, size_t label_len, |
| 103 | const unsigned char *ctx, size_t ctx_len, |
| 104 | unsigned char *dst, size_t *dst_len ) |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 105 | { |
Hanno Becker | 2dfe132 | 2020-09-10 09:23:12 +0100 | [diff] [blame] | 106 | size_t total_label_len = |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 107 | sizeof(tls13_label_prefix) + label_len; |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 108 | size_t total_hkdf_lbl_len = |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 109 | SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( total_label_len, ctx_len ); |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 110 | |
| 111 | unsigned char *p = dst; |
| 112 | |
Hanno Becker | 531fe30 | 2020-09-16 09:45:27 +0100 | [diff] [blame] | 113 | /* Add the size of the expanded key material. |
| 114 | * We're hardcoding the high byte to 0 here assuming that we never use |
| 115 | * TLS 1.3 HKDF key expansion to more than 255 Bytes. */ |
| 116 | #if MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN > 255 |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 117 | #error "The implementation of ssl_tls13_hkdf_encode_label() is not fit for the \ |
Hanno Becker | 531fe30 | 2020-09-16 09:45:27 +0100 | [diff] [blame] | 118 | value of MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN" |
| 119 | #endif |
| 120 | |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 121 | *p++ = 0; |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 122 | *p++ = MBEDTLS_BYTE_0( desired_length ); |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 123 | |
| 124 | /* Add label incl. prefix */ |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 125 | *p++ = MBEDTLS_BYTE_0( total_label_len ); |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 126 | memcpy( p, tls13_label_prefix, sizeof(tls13_label_prefix) ); |
| 127 | p += sizeof(tls13_label_prefix); |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 128 | memcpy( p, label, label_len ); |
| 129 | p += label_len; |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 130 | |
| 131 | /* Add context value */ |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 132 | *p++ = MBEDTLS_BYTE_0( ctx_len ); |
| 133 | if( ctx_len != 0 ) |
| 134 | memcpy( p, ctx, ctx_len ); |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 135 | |
| 136 | /* Return total length to the caller. */ |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 137 | *dst_len = total_hkdf_lbl_len; |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 138 | } |
| 139 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 140 | int mbedtls_ssl_tls13_hkdf_expand_label( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 141 | psa_algorithm_t hash_alg, |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 142 | const unsigned char *secret, size_t secret_len, |
| 143 | const unsigned char *label, size_t label_len, |
| 144 | const unsigned char *ctx, size_t ctx_len, |
| 145 | unsigned char *buf, size_t buf_len ) |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 146 | { |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 147 | unsigned char hkdf_label[ SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN ]; |
| 148 | size_t hkdf_label_len; |
Przemek Stekiel | d5ae365 | 2022-05-13 12:10:08 +0200 | [diff] [blame^] | 149 | psa_status_t status = PSA_SUCCESS; |
| 150 | psa_key_derivation_operation_t operation = |
| 151 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 152 | |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 153 | if( label_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN ) |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 154 | { |
| 155 | /* Should never happen since this is an internal |
| 156 | * function, and we know statically which labels |
| 157 | * are allowed. */ |
| 158 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 159 | } |
| 160 | |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 161 | if( ctx_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN ) |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 162 | { |
| 163 | /* Should not happen, as above. */ |
| 164 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 165 | } |
| 166 | |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 167 | if( buf_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN ) |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 168 | { |
| 169 | /* Should not happen, as above. */ |
| 170 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 171 | } |
| 172 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 173 | if( ! PSA_ALG_IS_HASH( hash_alg ) ) |
Gabor Mezei | 58db653 | 2022-03-21 12:12:37 +0100 | [diff] [blame] | 174 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 175 | |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 176 | ssl_tls13_hkdf_encode_label( buf_len, |
| 177 | label, label_len, |
| 178 | ctx, ctx_len, |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 179 | hkdf_label, |
| 180 | &hkdf_label_len ); |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 181 | |
Przemek Stekiel | d5ae365 | 2022-05-13 12:10:08 +0200 | [diff] [blame^] | 182 | status = psa_key_derivation_setup( &operation, PSA_ALG_HKDF_EXPAND( hash_alg ) ); |
| 183 | if (status == PSA_SUCCESS) |
| 184 | status |= psa_key_derivation_input_bytes( &operation, |
| 185 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 186 | secret, |
| 187 | secret_len ); |
| 188 | if (status == PSA_SUCCESS) |
| 189 | status |= psa_key_derivation_input_bytes( &operation, |
| 190 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 191 | hkdf_label, |
| 192 | hkdf_label_len ); |
| 193 | if (status == PSA_SUCCESS) |
| 194 | status |= psa_key_derivation_output_bytes( &operation, |
| 195 | buf, |
| 196 | buf_len ); |
| 197 | if (status == PSA_SUCCESS) |
| 198 | status |= psa_key_derivation_abort( &operation ); |
| 199 | |
| 200 | return( psa_ssl_status_to_mbedtls ( status ) ); |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 201 | } |
| 202 | |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 203 | /* |
| 204 | * The traffic keying material is generated from the following inputs: |
| 205 | * |
| 206 | * - One secret value per sender. |
| 207 | * - A purpose value indicating the specific value being generated |
| 208 | * - The desired lengths of key and IV. |
| 209 | * |
| 210 | * The expansion itself is based on HKDF: |
| 211 | * |
| 212 | * [sender]_write_key = HKDF-Expand-Label( Secret, "key", "", key_length ) |
| 213 | * [sender]_write_iv = HKDF-Expand-Label( Secret, "iv" , "", iv_length ) |
| 214 | * |
| 215 | * [sender] denotes the sending side and the Secret value is provided |
| 216 | * by the function caller. Note that we generate server and client side |
| 217 | * keys in a single function call. |
| 218 | */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 219 | int mbedtls_ssl_tls13_make_traffic_keys( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 220 | psa_algorithm_t hash_alg, |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 221 | const unsigned char *client_secret, |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 222 | const unsigned char *server_secret, size_t secret_len, |
| 223 | size_t key_len, size_t iv_len, |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 224 | mbedtls_ssl_key_set *keys ) |
| 225 | { |
| 226 | int ret = 0; |
| 227 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 228 | ret = mbedtls_ssl_tls13_hkdf_expand_label( hash_alg, |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 229 | client_secret, secret_len, |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 230 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( key ), |
| 231 | NULL, 0, |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 232 | keys->client_write_key, key_len ); |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 233 | if( ret != 0 ) |
| 234 | return( ret ); |
| 235 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 236 | ret = mbedtls_ssl_tls13_hkdf_expand_label( hash_alg, |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 237 | server_secret, secret_len, |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 238 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( key ), |
| 239 | NULL, 0, |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 240 | keys->server_write_key, key_len ); |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 241 | if( ret != 0 ) |
| 242 | return( ret ); |
| 243 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 244 | ret = mbedtls_ssl_tls13_hkdf_expand_label( hash_alg, |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 245 | client_secret, secret_len, |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 246 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( iv ), |
| 247 | NULL, 0, |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 248 | keys->client_write_iv, iv_len ); |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 249 | if( ret != 0 ) |
| 250 | return( ret ); |
| 251 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 252 | ret = mbedtls_ssl_tls13_hkdf_expand_label( hash_alg, |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 253 | server_secret, secret_len, |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 254 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( iv ), |
| 255 | NULL, 0, |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 256 | keys->server_write_iv, iv_len ); |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 257 | if( ret != 0 ) |
| 258 | return( ret ); |
| 259 | |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 260 | keys->key_len = key_len; |
| 261 | keys->iv_len = iv_len; |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 262 | |
| 263 | return( 0 ); |
| 264 | } |
| 265 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 266 | int mbedtls_ssl_tls13_derive_secret( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 267 | psa_algorithm_t hash_alg, |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 268 | const unsigned char *secret, size_t secret_len, |
| 269 | const unsigned char *label, size_t label_len, |
| 270 | const unsigned char *ctx, size_t ctx_len, |
Hanno Becker | 0c42fd9 | 2020-09-09 12:58:29 +0100 | [diff] [blame] | 271 | int ctx_hashed, |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 272 | unsigned char *dstbuf, size_t dstbuf_len ) |
Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 273 | { |
| 274 | int ret; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 275 | unsigned char hashed_context[ PSA_HASH_MAX_SIZE ]; |
Hanno Becker | 0c42fd9 | 2020-09-09 12:58:29 +0100 | [diff] [blame] | 276 | if( ctx_hashed == MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED ) |
Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 277 | { |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 278 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 279 | |
| 280 | status = psa_hash_compute( hash_alg, ctx, ctx_len, hashed_context, |
| 281 | PSA_HASH_LENGTH( hash_alg ), &ctx_len ); |
| 282 | if( status != PSA_SUCCESS ) |
| 283 | { |
| 284 | ret = psa_ssl_status_to_mbedtls( status ); |
| 285 | return ret; |
| 286 | } |
Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 287 | } |
| 288 | else |
| 289 | { |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 290 | if( ctx_len > sizeof(hashed_context) ) |
Hanno Becker | 97a2156 | 2020-09-09 12:57:16 +0100 | [diff] [blame] | 291 | { |
| 292 | /* This should never happen since this function is internal |
Hanno Becker | 0c42fd9 | 2020-09-09 12:58:29 +0100 | [diff] [blame] | 293 | * and the code sets `ctx_hashed` correctly. |
Hanno Becker | 97a2156 | 2020-09-09 12:57:16 +0100 | [diff] [blame] | 294 | * Let's double-check nonetheless to not run at the risk |
| 295 | * of getting a stack overflow. */ |
Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 296 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Hanno Becker | 97a2156 | 2020-09-09 12:57:16 +0100 | [diff] [blame] | 297 | } |
Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 298 | |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 299 | memcpy( hashed_context, ctx, ctx_len ); |
Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 300 | } |
| 301 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 302 | return( mbedtls_ssl_tls13_hkdf_expand_label( hash_alg, |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 303 | secret, secret_len, |
| 304 | label, label_len, |
| 305 | hashed_context, ctx_len, |
| 306 | dstbuf, dstbuf_len ) ); |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 307 | |
Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 308 | } |
| 309 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 310 | int mbedtls_ssl_tls13_evolve_secret( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 311 | psa_algorithm_t hash_alg, |
Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 312 | const unsigned char *secret_old, |
| 313 | const unsigned char *input, size_t input_len, |
| 314 | unsigned char *secret_new ) |
| 315 | { |
| 316 | int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Przemek Stekiel | d5ae365 | 2022-05-13 12:10:08 +0200 | [diff] [blame^] | 317 | psa_status_t status = PSA_SUCCESS; |
Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 318 | size_t hlen, ilen; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 319 | unsigned char tmp_secret[ PSA_MAC_MAX_SIZE ] = { 0 }; |
Jerry Yu | 6eaa41c | 2021-11-22 18:16:45 +0800 | [diff] [blame] | 320 | unsigned char tmp_input [ MBEDTLS_ECP_MAX_BYTES ] = { 0 }; |
Przemek Stekiel | d5ae365 | 2022-05-13 12:10:08 +0200 | [diff] [blame^] | 321 | psa_key_derivation_operation_t operation = |
| 322 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 323 | |
| 324 | if( ! PSA_ALG_IS_HASH( hash_alg ) ) |
Gabor Mezei | 58db653 | 2022-03-21 12:12:37 +0100 | [diff] [blame] | 325 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 326 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 327 | hlen = PSA_HASH_LENGTH( hash_alg ); |
Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 328 | |
| 329 | /* For non-initial runs, call Derive-Secret( ., "derived", "") |
Hanno Becker | 61baae7 | 2020-09-16 09:24:14 +0100 | [diff] [blame] | 330 | * on the old secret. */ |
Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 331 | if( secret_old != NULL ) |
| 332 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 333 | ret = mbedtls_ssl_tls13_derive_secret( |
Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 334 | hash_alg, |
| 335 | secret_old, hlen, |
| 336 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( derived ), |
| 337 | NULL, 0, /* context */ |
| 338 | MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED, |
Hanno Becker | 59b50a1 | 2020-09-09 10:56:56 +0100 | [diff] [blame] | 339 | tmp_secret, hlen ); |
Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 340 | if( ret != 0 ) |
| 341 | goto cleanup; |
| 342 | } |
| 343 | |
| 344 | if( input != NULL ) |
| 345 | { |
Hanno Becker | 59b50a1 | 2020-09-09 10:56:56 +0100 | [diff] [blame] | 346 | memcpy( tmp_input, input, input_len ); |
Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 347 | ilen = input_len; |
| 348 | } |
| 349 | else |
| 350 | { |
| 351 | ilen = hlen; |
| 352 | } |
| 353 | |
Przemek Stekiel | d5ae365 | 2022-05-13 12:10:08 +0200 | [diff] [blame^] | 354 | status = psa_key_derivation_setup( &operation, |
| 355 | PSA_ALG_HKDF_EXTRACT( hash_alg ) ); |
| 356 | if (status == PSA_SUCCESS) |
| 357 | status |= psa_key_derivation_input_bytes( &operation, |
| 358 | PSA_KEY_DERIVATION_INPUT_SALT, |
| 359 | tmp_secret, |
| 360 | hlen ); |
| 361 | if (status == PSA_SUCCESS) |
| 362 | status |= psa_key_derivation_input_bytes( &operation, |
| 363 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 364 | tmp_input, |
| 365 | ilen ); |
| 366 | if (status == PSA_SUCCESS) |
| 367 | status |= psa_key_derivation_output_bytes( &operation, |
| 368 | secret_new, |
| 369 | PSA_HASH_LENGTH( hash_alg ) ); |
| 370 | if (status == PSA_SUCCESS) |
| 371 | status |= psa_key_derivation_abort( &operation ); |
Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 372 | |
Przemek Stekiel | d5ae365 | 2022-05-13 12:10:08 +0200 | [diff] [blame^] | 373 | ret = psa_ssl_status_to_mbedtls ( status ); |
Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 374 | cleanup: |
| 375 | |
Hanno Becker | 59b50a1 | 2020-09-09 10:56:56 +0100 | [diff] [blame] | 376 | mbedtls_platform_zeroize( tmp_secret, sizeof(tmp_secret) ); |
| 377 | mbedtls_platform_zeroize( tmp_input, sizeof(tmp_input) ); |
Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 378 | return( ret ); |
| 379 | } |
| 380 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 381 | int mbedtls_ssl_tls13_derive_early_secrets( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 382 | psa_algorithm_t hash_alg, |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 383 | unsigned char const *early_secret, |
| 384 | unsigned char const *transcript, size_t transcript_len, |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 385 | mbedtls_ssl_tls13_early_secrets *derived ) |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 386 | { |
| 387 | int ret; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 388 | size_t const hash_len = PSA_HASH_LENGTH( hash_alg ); |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 389 | |
| 390 | /* We should never call this function with an unknown hash, |
| 391 | * but add an assertion anyway. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 392 | if( ! PSA_ALG_IS_HASH( hash_alg ) ) |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 393 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 394 | |
| 395 | /* |
| 396 | * 0 |
| 397 | * | |
| 398 | * v |
| 399 | * PSK -> HKDF-Extract = Early Secret |
| 400 | * | |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 401 | * +-----> Derive-Secret(., "c e traffic", ClientHello) |
| 402 | * | = client_early_traffic_secret |
| 403 | * | |
| 404 | * +-----> Derive-Secret(., "e exp master", ClientHello) |
| 405 | * | = early_exporter_master_secret |
| 406 | * v |
| 407 | */ |
| 408 | |
| 409 | /* Create client_early_traffic_secret */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 410 | ret = mbedtls_ssl_tls13_derive_secret( hash_alg, |
| 411 | early_secret, hash_len, |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 412 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( c_e_traffic ), |
| 413 | transcript, transcript_len, |
| 414 | MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED, |
| 415 | derived->client_early_traffic_secret, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 416 | hash_len ); |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 417 | if( ret != 0 ) |
| 418 | return( ret ); |
| 419 | |
| 420 | /* Create early exporter */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 421 | ret = mbedtls_ssl_tls13_derive_secret( hash_alg, |
| 422 | early_secret, hash_len, |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 423 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( e_exp_master ), |
| 424 | transcript, transcript_len, |
| 425 | MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED, |
| 426 | derived->early_exporter_master_secret, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 427 | hash_len ); |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 428 | if( ret != 0 ) |
| 429 | return( ret ); |
| 430 | |
| 431 | return( 0 ); |
| 432 | } |
| 433 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 434 | int mbedtls_ssl_tls13_derive_handshake_secrets( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 435 | psa_algorithm_t hash_alg, |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 436 | unsigned char const *handshake_secret, |
| 437 | unsigned char const *transcript, size_t transcript_len, |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 438 | mbedtls_ssl_tls13_handshake_secrets *derived ) |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 439 | { |
| 440 | int ret; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 441 | size_t const hash_len = PSA_HASH_LENGTH( hash_alg ); |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 442 | |
| 443 | /* We should never call this function with an unknown hash, |
| 444 | * but add an assertion anyway. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 445 | if( ! PSA_ALG_IS_HASH( hash_alg ) ) |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 446 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 447 | |
| 448 | /* |
| 449 | * |
| 450 | * Handshake Secret |
| 451 | * | |
| 452 | * +-----> Derive-Secret( ., "c hs traffic", |
| 453 | * | ClientHello...ServerHello ) |
| 454 | * | = client_handshake_traffic_secret |
| 455 | * | |
| 456 | * +-----> Derive-Secret( ., "s hs traffic", |
| 457 | * | ClientHello...ServerHello ) |
| 458 | * | = server_handshake_traffic_secret |
| 459 | * |
| 460 | */ |
| 461 | |
| 462 | /* |
| 463 | * Compute client_handshake_traffic_secret with |
| 464 | * Derive-Secret( ., "c hs traffic", ClientHello...ServerHello ) |
| 465 | */ |
| 466 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 467 | ret = mbedtls_ssl_tls13_derive_secret( hash_alg, |
| 468 | handshake_secret, hash_len, |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 469 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( c_hs_traffic ), |
| 470 | transcript, transcript_len, |
| 471 | MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED, |
| 472 | derived->client_handshake_traffic_secret, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 473 | hash_len ); |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 474 | if( ret != 0 ) |
| 475 | return( ret ); |
| 476 | |
| 477 | /* |
| 478 | * Compute server_handshake_traffic_secret with |
| 479 | * Derive-Secret( ., "s hs traffic", ClientHello...ServerHello ) |
| 480 | */ |
| 481 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 482 | ret = mbedtls_ssl_tls13_derive_secret( hash_alg, |
| 483 | handshake_secret, hash_len, |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 484 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( s_hs_traffic ), |
| 485 | transcript, transcript_len, |
| 486 | MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED, |
| 487 | derived->server_handshake_traffic_secret, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 488 | hash_len ); |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 489 | if( ret != 0 ) |
| 490 | return( ret ); |
| 491 | |
| 492 | return( 0 ); |
| 493 | } |
| 494 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 495 | int mbedtls_ssl_tls13_derive_application_secrets( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 496 | psa_algorithm_t hash_alg, |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 497 | unsigned char const *application_secret, |
| 498 | unsigned char const *transcript, size_t transcript_len, |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 499 | mbedtls_ssl_tls13_application_secrets *derived ) |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 500 | { |
| 501 | int ret; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 502 | size_t const hash_len = PSA_HASH_LENGTH( hash_alg ); |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 503 | |
| 504 | /* We should never call this function with an unknown hash, |
| 505 | * but add an assertion anyway. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 506 | if( ! PSA_ALG_IS_HASH( hash_alg ) ) |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 507 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 508 | |
| 509 | /* Generate {client,server}_application_traffic_secret_0 |
| 510 | * |
| 511 | * Master Secret |
| 512 | * | |
| 513 | * +-----> Derive-Secret( ., "c ap traffic", |
| 514 | * | ClientHello...server Finished ) |
| 515 | * | = client_application_traffic_secret_0 |
| 516 | * | |
| 517 | * +-----> Derive-Secret( ., "s ap traffic", |
| 518 | * | ClientHello...Server Finished ) |
| 519 | * | = server_application_traffic_secret_0 |
| 520 | * | |
| 521 | * +-----> Derive-Secret( ., "exp master", |
| 522 | * | ClientHello...server Finished) |
| 523 | * | = exporter_master_secret |
| 524 | * |
| 525 | */ |
| 526 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 527 | ret = mbedtls_ssl_tls13_derive_secret( hash_alg, |
| 528 | application_secret, hash_len, |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 529 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( c_ap_traffic ), |
| 530 | transcript, transcript_len, |
| 531 | MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED, |
| 532 | derived->client_application_traffic_secret_N, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 533 | hash_len ); |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 534 | if( ret != 0 ) |
| 535 | return( ret ); |
| 536 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 537 | ret = mbedtls_ssl_tls13_derive_secret( hash_alg, |
| 538 | application_secret, hash_len, |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 539 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( s_ap_traffic ), |
| 540 | transcript, transcript_len, |
| 541 | MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED, |
| 542 | derived->server_application_traffic_secret_N, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 543 | hash_len ); |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 544 | if( ret != 0 ) |
| 545 | return( ret ); |
| 546 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 547 | ret = mbedtls_ssl_tls13_derive_secret( hash_alg, |
| 548 | application_secret, hash_len, |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 549 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( exp_master ), |
| 550 | transcript, transcript_len, |
| 551 | MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED, |
| 552 | derived->exporter_master_secret, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 553 | hash_len ); |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 554 | if( ret != 0 ) |
| 555 | return( ret ); |
| 556 | |
| 557 | return( 0 ); |
| 558 | } |
| 559 | |
| 560 | /* Generate resumption_master_secret for use with the ticket exchange. |
| 561 | * |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 562 | * This is not integrated with mbedtls_ssl_tls13_derive_application_secrets() |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 563 | * because it uses the transcript hash up to and including ClientFinished. */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 564 | int mbedtls_ssl_tls13_derive_resumption_master_secret( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 565 | psa_algorithm_t hash_alg, |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 566 | unsigned char const *application_secret, |
| 567 | unsigned char const *transcript, size_t transcript_len, |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 568 | mbedtls_ssl_tls13_application_secrets *derived ) |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 569 | { |
| 570 | int ret; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 571 | size_t const hash_len = PSA_HASH_LENGTH( hash_alg ); |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 572 | |
| 573 | /* We should never call this function with an unknown hash, |
| 574 | * but add an assertion anyway. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 575 | if( ! PSA_ALG_IS_HASH( hash_alg ) ) |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 576 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 577 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 578 | ret = mbedtls_ssl_tls13_derive_secret( hash_alg, |
| 579 | application_secret, hash_len, |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 580 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( res_master ), |
| 581 | transcript, transcript_len, |
| 582 | MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED, |
| 583 | derived->resumption_master_secret, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 584 | hash_len ); |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 585 | |
| 586 | if( ret != 0 ) |
| 587 | return( ret ); |
| 588 | |
| 589 | return( 0 ); |
| 590 | } |
| 591 | |
XiaokangQian | a4c99f2 | 2021-11-11 06:46:35 +0000 | [diff] [blame] | 592 | int mbedtls_ssl_tls13_key_schedule_stage_application( mbedtls_ssl_context *ssl ) |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 593 | { |
XiaokangQian | 61bdbbc | 2021-10-28 08:03:38 +0000 | [diff] [blame] | 594 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
XiaokangQian | 3306284 | 2021-11-11 03:37:45 +0000 | [diff] [blame] | 595 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 596 | psa_algorithm_t const hash_alg = mbedtls_psa_translate_md( |
| 597 | handshake->ciphersuite_info->mac ); |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 598 | |
| 599 | /* |
| 600 | * Compute MasterSecret |
| 601 | */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 602 | ret = mbedtls_ssl_tls13_evolve_secret( hash_alg, |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 603 | handshake->tls13_master_secrets.handshake, |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 604 | NULL, 0, |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 605 | handshake->tls13_master_secrets.app ); |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 606 | if( ret != 0 ) |
| 607 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 608 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret ); |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 609 | return( ret ); |
| 610 | } |
| 611 | |
| 612 | MBEDTLS_SSL_DEBUG_BUF( 4, "Master secret", |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 613 | handshake->tls13_master_secrets.app, PSA_HASH_LENGTH( hash_alg ) ); |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 614 | |
| 615 | return( 0 ); |
| 616 | } |
| 617 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 618 | static int ssl_tls13_calc_finished_core( psa_algorithm_t hash_alg, |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 619 | unsigned char const *base_key, |
| 620 | unsigned char const *transcript, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 621 | unsigned char *dst, |
| 622 | size_t *dst_len ) |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 623 | { |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 624 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 625 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 626 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 627 | size_t hash_len = PSA_HASH_LENGTH( hash_alg ); |
| 628 | unsigned char finished_key[PSA_MAC_MAX_SIZE]; |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 629 | int ret; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 630 | psa_algorithm_t alg; |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 631 | |
| 632 | /* We should never call this function with an unknown hash, |
| 633 | * but add an assertion anyway. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 634 | if( ! PSA_ALG_IS_HASH( hash_alg ) ) |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 635 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 636 | |
| 637 | /* TLS 1.3 Finished message |
| 638 | * |
| 639 | * struct { |
| 640 | * opaque verify_data[Hash.length]; |
| 641 | * } Finished; |
| 642 | * |
| 643 | * verify_data = |
| 644 | * HMAC( finished_key, |
| 645 | * Hash( Handshake Context + |
| 646 | * Certificate* + |
| 647 | * CertificateVerify* ) |
| 648 | * ) |
| 649 | * |
| 650 | * finished_key = |
| 651 | * HKDF-Expand-Label( BaseKey, "finished", "", Hash.length ) |
| 652 | */ |
| 653 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 654 | ret = mbedtls_ssl_tls13_hkdf_expand_label( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 655 | hash_alg, base_key, hash_len, |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 656 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( finished ), |
| 657 | NULL, 0, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 658 | finished_key, hash_len ); |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 659 | if( ret != 0 ) |
| 660 | goto exit; |
| 661 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 662 | alg = PSA_ALG_HMAC( hash_alg ); |
| 663 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 664 | psa_set_key_algorithm( &attributes, alg ); |
| 665 | psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC ); |
| 666 | |
| 667 | status = psa_import_key( &attributes, finished_key, hash_len, &key ); |
| 668 | if( status != PSA_SUCCESS ) |
| 669 | { |
| 670 | ret = psa_ssl_status_to_mbedtls( status ); |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 671 | goto exit; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | status = psa_mac_compute( key, alg, transcript, hash_len, |
| 675 | dst, hash_len, dst_len ); |
| 676 | ret = psa_ssl_status_to_mbedtls( status ); |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 677 | |
| 678 | exit: |
| 679 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 680 | status = psa_destroy_key( key ); |
| 681 | if( ret == 0 ) |
| 682 | ret = psa_ssl_status_to_mbedtls( status ); |
| 683 | |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 684 | mbedtls_platform_zeroize( finished_key, sizeof( finished_key ) ); |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 685 | |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 686 | return( ret ); |
| 687 | } |
| 688 | |
XiaokangQian | c5c39d5 | 2021-11-09 11:55:10 +0000 | [diff] [blame] | 689 | int mbedtls_ssl_tls13_calculate_verify_data( mbedtls_ssl_context* ssl, |
XiaokangQian | aaa0e19 | 2021-11-10 03:07:04 +0000 | [diff] [blame] | 690 | unsigned char* dst, |
| 691 | size_t dst_len, |
| 692 | size_t *actual_len, |
| 693 | int from ) |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 694 | { |
XiaokangQian | a763498 | 2021-10-22 06:32:32 +0000 | [diff] [blame] | 695 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 696 | |
XiaokangQian | aaa0e19 | 2021-11-10 03:07:04 +0000 | [diff] [blame] | 697 | unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE]; |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 698 | size_t transcript_len; |
| 699 | |
Jerry Yu | 4a2fa5d | 2021-12-10 10:19:34 +0800 | [diff] [blame] | 700 | unsigned char *base_key = NULL; |
Jerry Yu | b737f6a | 2021-12-10 17:55:23 +0800 | [diff] [blame] | 701 | size_t base_key_len = 0; |
Jerry Yu | 9c07473 | 2021-12-10 17:12:43 +0800 | [diff] [blame] | 702 | mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets = |
| 703 | &ssl->handshake->tls13_hs_secrets; |
Jerry Yu | a5563f6 | 2021-12-10 18:14:36 +0800 | [diff] [blame] | 704 | |
| 705 | mbedtls_md_type_t const md_type = ssl->handshake->ciphersuite_info->mac; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 706 | |
| 707 | psa_algorithm_t hash_alg = mbedtls_psa_translate_md( |
| 708 | ssl->handshake->ciphersuite_info->mac ); |
Gabor Mezei | 29e7ca8 | 2022-03-29 17:08:49 +0200 | [diff] [blame] | 709 | size_t const hash_len = PSA_HASH_LENGTH( hash_alg ); |
Jerry Yu | a5563f6 | 2021-12-10 18:14:36 +0800 | [diff] [blame] | 710 | |
| 711 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_tls13_calculate_verify_data" ) ); |
| 712 | |
Jerry Yu | b737f6a | 2021-12-10 17:55:23 +0800 | [diff] [blame] | 713 | if( from == MBEDTLS_SSL_IS_CLIENT ) |
| 714 | { |
| 715 | base_key = tls13_hs_secrets->client_handshake_traffic_secret; |
| 716 | base_key_len = sizeof( tls13_hs_secrets->client_handshake_traffic_secret ); |
| 717 | } |
| 718 | else |
| 719 | { |
| 720 | base_key = tls13_hs_secrets->server_handshake_traffic_secret; |
| 721 | base_key_len = sizeof( tls13_hs_secrets->server_handshake_traffic_secret ); |
| 722 | } |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 723 | |
Gabor Mezei | 29e7ca8 | 2022-03-29 17:08:49 +0200 | [diff] [blame] | 724 | if( dst_len < hash_len ) |
Jerry Yu | 9c07473 | 2021-12-10 17:12:43 +0800 | [diff] [blame] | 725 | { |
| 726 | ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 727 | goto exit; |
| 728 | } |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 729 | |
| 730 | ret = mbedtls_ssl_get_handshake_transcript( ssl, md_type, |
| 731 | transcript, sizeof( transcript ), |
| 732 | &transcript_len ); |
| 733 | if( ret != 0 ) |
| 734 | { |
| 735 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_handshake_transcript", ret ); |
XiaokangQian | 61bdbbc | 2021-10-28 08:03:38 +0000 | [diff] [blame] | 736 | goto exit; |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 737 | } |
| 738 | MBEDTLS_SSL_DEBUG_BUF( 4, "handshake hash", transcript, transcript_len ); |
| 739 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 740 | ret = ssl_tls13_calc_finished_core( hash_alg, base_key, transcript, dst, actual_len ); |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 741 | if( ret != 0 ) |
XiaokangQian | 61bdbbc | 2021-10-28 08:03:38 +0000 | [diff] [blame] | 742 | goto exit; |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 743 | |
Gabor Mezei | 29e7ca8 | 2022-03-29 17:08:49 +0200 | [diff] [blame] | 744 | MBEDTLS_SSL_DEBUG_BUF( 3, "verify_data for finished message", dst, hash_len ); |
XiaokangQian | c5c39d5 | 2021-11-09 11:55:10 +0000 | [diff] [blame] | 745 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_tls13_calculate_verify_data" ) ); |
XiaokangQian | 61bdbbc | 2021-10-28 08:03:38 +0000 | [diff] [blame] | 746 | |
| 747 | exit: |
Jerry Yu | 4a2fa5d | 2021-12-10 10:19:34 +0800 | [diff] [blame] | 748 | /* Erase handshake secrets */ |
Jerry Yu | b737f6a | 2021-12-10 17:55:23 +0800 | [diff] [blame] | 749 | mbedtls_platform_zeroize( base_key, base_key_len ); |
XiaokangQian | 3306284 | 2021-11-11 03:37:45 +0000 | [diff] [blame] | 750 | mbedtls_platform_zeroize( transcript, sizeof( transcript ) ); |
XiaokangQian | 61bdbbc | 2021-10-28 08:03:38 +0000 | [diff] [blame] | 751 | return( ret ); |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 754 | int mbedtls_ssl_tls13_create_psk_binder( mbedtls_ssl_context *ssl, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 755 | const psa_algorithm_t hash_alg, |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 756 | unsigned char const *psk, size_t psk_len, |
| 757 | int psk_type, |
| 758 | unsigned char const *transcript, |
| 759 | unsigned char *result ) |
| 760 | { |
| 761 | int ret = 0; |
Gabor Mezei | ed6d658 | 2022-03-26 17:28:06 +0100 | [diff] [blame] | 762 | unsigned char binder_key[PSA_MAC_MAX_SIZE]; |
| 763 | unsigned char early_secret[PSA_MAC_MAX_SIZE]; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 764 | size_t const hash_len = PSA_HASH_LENGTH( hash_alg ); |
| 765 | size_t actual_len; |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 766 | |
Hanno Becker | 28e5f1e | 2021-05-26 09:29:49 +0100 | [diff] [blame] | 767 | #if !defined(MBEDTLS_DEBUG_C) |
| 768 | ssl = NULL; /* make sure we don't use it except for debug */ |
| 769 | ((void) ssl); |
| 770 | #endif |
| 771 | |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 772 | /* We should never call this function with an unknown hash, |
| 773 | * but add an assertion anyway. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 774 | if( ! PSA_ALG_IS_HASH( hash_alg ) ) |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 775 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 776 | |
| 777 | /* |
| 778 | * 0 |
| 779 | * | |
| 780 | * v |
| 781 | * PSK -> HKDF-Extract = Early Secret |
| 782 | * | |
| 783 | * +-----> Derive-Secret(., "ext binder" | "res binder", "") |
| 784 | * | = binder_key |
| 785 | * v |
| 786 | */ |
| 787 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 788 | ret = mbedtls_ssl_tls13_evolve_secret( hash_alg, |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 789 | NULL, /* Old secret */ |
| 790 | psk, psk_len, /* Input */ |
| 791 | early_secret ); |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 792 | if( ret != 0 ) |
| 793 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 794 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret ); |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 795 | goto exit; |
| 796 | } |
| 797 | |
| 798 | if( psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION ) |
| 799 | { |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 800 | ret = mbedtls_ssl_tls13_derive_secret( hash_alg, |
| 801 | early_secret, hash_len, |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 802 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( res_binder ), |
| 803 | NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 804 | binder_key, hash_len ); |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 805 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "Derive Early Secret with 'res binder'" ) ); |
| 806 | } |
| 807 | else |
| 808 | { |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 809 | ret = mbedtls_ssl_tls13_derive_secret( hash_alg, |
| 810 | early_secret, hash_len, |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 811 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( ext_binder ), |
| 812 | NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 813 | binder_key, hash_len ); |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 814 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "Derive Early Secret with 'ext binder'" ) ); |
| 815 | } |
| 816 | |
| 817 | if( ret != 0 ) |
| 818 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 819 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_secret", ret ); |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 820 | goto exit; |
| 821 | } |
| 822 | |
| 823 | /* |
| 824 | * The binding_value is computed in the same way as the Finished message |
| 825 | * but with the BaseKey being the binder_key. |
| 826 | */ |
| 827 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 828 | ret = ssl_tls13_calc_finished_core( hash_alg, binder_key, transcript, |
| 829 | result, &actual_len ); |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 830 | if( ret != 0 ) |
| 831 | goto exit; |
| 832 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 833 | MBEDTLS_SSL_DEBUG_BUF( 3, "psk binder", result, actual_len ); |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 834 | |
| 835 | exit: |
| 836 | |
| 837 | mbedtls_platform_zeroize( early_secret, sizeof( early_secret ) ); |
| 838 | mbedtls_platform_zeroize( binder_key, sizeof( binder_key ) ); |
| 839 | return( ret ); |
| 840 | } |
| 841 | |
Hanno Becker | c94060c | 2021-03-22 07:50:44 +0000 | [diff] [blame] | 842 | int mbedtls_ssl_tls13_populate_transform( mbedtls_ssl_transform *transform, |
| 843 | int endpoint, |
| 844 | int ciphersuite, |
| 845 | mbedtls_ssl_key_set const *traffic_keys, |
| 846 | mbedtls_ssl_context *ssl /* DEBUG ONLY */ ) |
| 847 | { |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 848 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | c94060c | 2021-03-22 07:50:44 +0000 | [diff] [blame] | 849 | int ret; |
| 850 | mbedtls_cipher_info_t const *cipher_info; |
Neil Armstrong | a8093f5 | 2022-05-04 17:44:05 +0200 | [diff] [blame] | 851 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | c94060c | 2021-03-22 07:50:44 +0000 | [diff] [blame] | 852 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
| 853 | unsigned char const *key_enc; |
| 854 | unsigned char const *iv_enc; |
| 855 | unsigned char const *key_dec; |
| 856 | unsigned char const *iv_dec; |
| 857 | |
Przemyslaw Stekiel | ae77b0a | 2022-01-12 10:29:03 +0100 | [diff] [blame] | 858 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 859 | psa_key_type_t key_type; |
| 860 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 861 | psa_algorithm_t alg; |
| 862 | size_t key_bits; |
| 863 | psa_status_t status = PSA_SUCCESS; |
| 864 | #endif |
| 865 | |
Hanno Becker | c94060c | 2021-03-22 07:50:44 +0000 | [diff] [blame] | 866 | #if !defined(MBEDTLS_DEBUG_C) |
| 867 | ssl = NULL; /* make sure we don't use it except for those cases */ |
| 868 | (void) ssl; |
| 869 | #endif |
| 870 | |
| 871 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite ); |
Hanno Becker | 7887a77 | 2021-04-20 05:27:57 +0100 | [diff] [blame] | 872 | if( ciphersuite_info == NULL ) |
| 873 | { |
| 874 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found", |
| 875 | ciphersuite ) ); |
| 876 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 877 | } |
Hanno Becker | c94060c | 2021-03-22 07:50:44 +0000 | [diff] [blame] | 878 | |
Neil Armstrong | a8093f5 | 2022-05-04 17:44:05 +0200 | [diff] [blame] | 879 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | c94060c | 2021-03-22 07:50:44 +0000 | [diff] [blame] | 880 | cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher ); |
| 881 | if( cipher_info == NULL ) |
| 882 | { |
Hanno Becker | 7887a77 | 2021-04-20 05:27:57 +0100 | [diff] [blame] | 883 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found", |
| 884 | ciphersuite_info->cipher ) ); |
Hanno Becker | f62a730 | 2021-04-21 05:21:28 +0100 | [diff] [blame] | 885 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | c94060c | 2021-03-22 07:50:44 +0000 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | /* |
| 889 | * Setup cipher contexts in target transform |
| 890 | */ |
Hanno Becker | c94060c | 2021-03-22 07:50:44 +0000 | [diff] [blame] | 891 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, |
| 892 | cipher_info ) ) != 0 ) |
| 893 | { |
| 894 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 895 | return( ret ); |
| 896 | } |
| 897 | |
| 898 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, |
| 899 | cipher_info ) ) != 0 ) |
| 900 | { |
| 901 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 902 | return( ret ); |
| 903 | } |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 904 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | c94060c | 2021-03-22 07:50:44 +0000 | [diff] [blame] | 905 | |
| 906 | #if defined(MBEDTLS_SSL_SRV_C) |
| 907 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 908 | { |
| 909 | key_enc = traffic_keys->server_write_key; |
| 910 | key_dec = traffic_keys->client_write_key; |
| 911 | iv_enc = traffic_keys->server_write_iv; |
| 912 | iv_dec = traffic_keys->client_write_iv; |
| 913 | } |
| 914 | else |
| 915 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 916 | #if defined(MBEDTLS_SSL_CLI_C) |
| 917 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 918 | { |
| 919 | key_enc = traffic_keys->client_write_key; |
| 920 | key_dec = traffic_keys->server_write_key; |
| 921 | iv_enc = traffic_keys->client_write_iv; |
| 922 | iv_dec = traffic_keys->server_write_iv; |
| 923 | } |
| 924 | else |
| 925 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 926 | { |
| 927 | /* should not happen */ |
| 928 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 929 | } |
| 930 | |
| 931 | memcpy( transform->iv_enc, iv_enc, traffic_keys->iv_len ); |
| 932 | memcpy( transform->iv_dec, iv_dec, traffic_keys->iv_len ); |
| 933 | |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 934 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | c94060c | 2021-03-22 07:50:44 +0000 | [diff] [blame] | 935 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, |
| 936 | key_enc, cipher_info->key_bitlen, |
| 937 | MBEDTLS_ENCRYPT ) ) != 0 ) |
| 938 | { |
| 939 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 940 | return( ret ); |
| 941 | } |
| 942 | |
| 943 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, |
| 944 | key_dec, cipher_info->key_bitlen, |
| 945 | MBEDTLS_DECRYPT ) ) != 0 ) |
| 946 | { |
| 947 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 948 | return( ret ); |
| 949 | } |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 950 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | c94060c | 2021-03-22 07:50:44 +0000 | [diff] [blame] | 951 | |
Przemyslaw Stekiel | dd7b501 | 2022-01-17 15:28:57 +0100 | [diff] [blame] | 952 | /* |
| 953 | * Setup other fields in SSL transform |
| 954 | */ |
| 955 | |
| 956 | if( ( ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ) != 0 ) |
| 957 | transform->taglen = 8; |
| 958 | else |
| 959 | transform->taglen = 16; |
| 960 | |
| 961 | transform->ivlen = traffic_keys->iv_len; |
| 962 | transform->maclen = 0; |
| 963 | transform->fixed_ivlen = transform->ivlen; |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 964 | transform->tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
Przemyslaw Stekiel | dd7b501 | 2022-01-17 15:28:57 +0100 | [diff] [blame] | 965 | |
| 966 | /* We add the true record content type (1 Byte) to the plaintext and |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 967 | * then pad to the configured granularity. The minimum length of the |
Przemyslaw Stekiel | dd7b501 | 2022-01-17 15:28:57 +0100 | [diff] [blame] | 968 | * type-extended and padded plaintext is therefore the padding |
| 969 | * granularity. */ |
| 970 | transform->minlen = |
| 971 | transform->taglen + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY; |
| 972 | |
Przemyslaw Stekiel | ae77b0a | 2022-01-12 10:29:03 +0100 | [diff] [blame] | 973 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 974 | /* |
| 975 | * Setup psa keys and alg |
| 976 | */ |
Neil Armstrong | a8093f5 | 2022-05-04 17:44:05 +0200 | [diff] [blame] | 977 | if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher, |
Przemyslaw Stekiel | ae77b0a | 2022-01-12 10:29:03 +0100 | [diff] [blame] | 978 | transform->taglen, |
| 979 | &alg, |
| 980 | &key_type, |
| 981 | &key_bits ) ) != PSA_SUCCESS ) |
| 982 | { |
Przemyslaw Stekiel | 77aec8d | 2022-01-31 20:22:53 +0100 | [diff] [blame] | 983 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", psa_ssl_status_to_mbedtls( status ) ); |
| 984 | return( psa_ssl_status_to_mbedtls( status ) ); |
Przemyslaw Stekiel | ae77b0a | 2022-01-12 10:29:03 +0100 | [diff] [blame] | 985 | } |
| 986 | |
Przemyslaw Stekiel | ae77b0a | 2022-01-12 10:29:03 +0100 | [diff] [blame] | 987 | transform->psa_alg = alg; |
| 988 | |
Przemyslaw Stekiel | f9cd608 | 2022-02-01 11:25:55 +0100 | [diff] [blame] | 989 | if ( alg != MBEDTLS_SSL_NULL_CIPHER ) |
Przemyslaw Stekiel | ae77b0a | 2022-01-12 10:29:03 +0100 | [diff] [blame] | 990 | { |
Przemyslaw Stekiel | f9cd608 | 2022-02-01 11:25:55 +0100 | [diff] [blame] | 991 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 992 | psa_set_key_algorithm( &attributes, alg ); |
| 993 | psa_set_key_type( &attributes, key_type ); |
Przemyslaw Stekiel | fe7397d | 2022-01-17 15:47:07 +0100 | [diff] [blame] | 994 | |
Przemyslaw Stekiel | f9cd608 | 2022-02-01 11:25:55 +0100 | [diff] [blame] | 995 | if( ( status = psa_import_key( &attributes, |
| 996 | key_enc, |
| 997 | PSA_BITS_TO_BYTES( key_bits ), |
| 998 | &transform->psa_key_enc ) ) != PSA_SUCCESS ) |
| 999 | { |
| 1000 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", psa_ssl_status_to_mbedtls( status ) ); |
| 1001 | return( psa_ssl_status_to_mbedtls( status ) ); |
| 1002 | } |
Przemyslaw Stekiel | fe7397d | 2022-01-17 15:47:07 +0100 | [diff] [blame] | 1003 | |
Przemyslaw Stekiel | f9cd608 | 2022-02-01 11:25:55 +0100 | [diff] [blame] | 1004 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 1005 | |
| 1006 | if( ( status = psa_import_key( &attributes, |
| 1007 | key_dec, |
| 1008 | PSA_BITS_TO_BYTES( key_bits ), |
| 1009 | &transform->psa_key_dec ) ) != PSA_SUCCESS ) |
| 1010 | { |
| 1011 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", psa_ssl_status_to_mbedtls( status ) ); |
| 1012 | return( psa_ssl_status_to_mbedtls( status ) ); |
| 1013 | } |
Przemyslaw Stekiel | ae77b0a | 2022-01-12 10:29:03 +0100 | [diff] [blame] | 1014 | } |
| 1015 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1016 | |
Hanno Becker | c94060c | 2021-03-22 07:50:44 +0000 | [diff] [blame] | 1017 | return( 0 ); |
| 1018 | } |
| 1019 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1020 | int mbedtls_ssl_tls13_key_schedule_stage_early( mbedtls_ssl_context *ssl ) |
Jerry Yu | 89ea321 | 2021-09-09 14:31:24 +0800 | [diff] [blame] | 1021 | { |
Jerry Yu | e3131ef | 2021-09-16 13:14:15 +0800 | [diff] [blame] | 1022 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 1023 | psa_algorithm_t hash_alg; |
Jerry Yu | 5ccfcd4 | 2021-10-11 16:39:29 +0800 | [diff] [blame] | 1024 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
Jerry Yu | 6ca7c7f | 2021-09-28 18:51:40 +0800 | [diff] [blame] | 1025 | |
Jerry Yu | 5ccfcd4 | 2021-10-11 16:39:29 +0800 | [diff] [blame] | 1026 | if( handshake->ciphersuite_info == NULL ) |
Jerry Yu | 89ea321 | 2021-09-09 14:31:24 +0800 | [diff] [blame] | 1027 | { |
| 1028 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher suite info not found" ) ); |
| 1029 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 1030 | } |
Jerry Yu | e3131ef | 2021-09-16 13:14:15 +0800 | [diff] [blame] | 1031 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 1032 | hash_alg = mbedtls_psa_translate_md( handshake->ciphersuite_info->mac ); |
Jerry Yu | e06f453 | 2021-09-23 18:35:07 +0800 | [diff] [blame] | 1033 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 1034 | ret = mbedtls_ssl_tls13_evolve_secret( hash_alg, NULL, NULL, 0, |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 1035 | handshake->tls13_master_secrets.early ); |
Jerry Yu | 89ea321 | 2021-09-09 14:31:24 +0800 | [diff] [blame] | 1036 | if( ret != 0 ) |
| 1037 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1038 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret ); |
Jerry Yu | 89ea321 | 2021-09-09 14:31:24 +0800 | [diff] [blame] | 1039 | return( ret ); |
| 1040 | } |
| 1041 | |
| 1042 | return( 0 ); |
| 1043 | } |
| 1044 | |
Neil Armstrong | b818e16 | 2022-05-17 09:24:52 +0200 | [diff] [blame] | 1045 | static int mbedtls_ssl_tls13_get_cipher_key_info( |
| 1046 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info, |
| 1047 | size_t *key_len, size_t *iv_len ) |
| 1048 | { |
| 1049 | psa_key_type_t key_type; |
| 1050 | psa_algorithm_t alg; |
| 1051 | size_t taglen; |
| 1052 | size_t key_bits; |
| 1053 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1054 | |
| 1055 | if( ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ) |
| 1056 | taglen = 8; |
| 1057 | else |
| 1058 | taglen = 16; |
| 1059 | |
| 1060 | status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher, taglen, |
| 1061 | &alg, &key_type, &key_bits ); |
| 1062 | if( status != PSA_SUCCESS ) |
| 1063 | return psa_ssl_status_to_mbedtls( status ); |
| 1064 | |
| 1065 | *key_len = PSA_BITS_TO_BYTES( key_bits ); |
| 1066 | |
Neil Armstrong | 0fa8ce3 | 2022-05-17 14:42:57 +0200 | [diff] [blame] | 1067 | /* TLS 1.3 only have AEAD ciphers, IV length is unconditionally 12 bytes */ |
| 1068 | *iv_len = 12; |
Neil Armstrong | b818e16 | 2022-05-17 09:24:52 +0200 | [diff] [blame] | 1069 | |
| 1070 | return 0; |
| 1071 | } |
| 1072 | |
Jerry Yu | c068b66 | 2021-10-11 22:30:19 +0800 | [diff] [blame] | 1073 | /* mbedtls_ssl_tls13_generate_handshake_keys() generates keys necessary for |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1074 | * protecting the handshake messages, as described in Section 7 of TLS 1.3. */ |
Jerry Yu | c068b66 | 2021-10-11 22:30:19 +0800 | [diff] [blame] | 1075 | int mbedtls_ssl_tls13_generate_handshake_keys( mbedtls_ssl_context *ssl, |
| 1076 | mbedtls_ssl_key_set *traffic_keys ) |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1077 | { |
| 1078 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1079 | |
| 1080 | mbedtls_md_type_t md_type; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 1081 | |
| 1082 | psa_algorithm_t hash_alg; |
| 1083 | size_t hash_len; |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1084 | |
Jerry Yu | 435208a | 2021-10-13 11:22:16 +0800 | [diff] [blame] | 1085 | unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE]; |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1086 | size_t transcript_len; |
| 1087 | |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 1088 | size_t key_len, iv_len; |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1089 | |
Jerry Yu | 435208a | 2021-10-13 11:22:16 +0800 | [diff] [blame] | 1090 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 1091 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = handshake->ciphersuite_info; |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1092 | mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets = &handshake->tls13_hs_secrets; |
Jerry Yu | 435208a | 2021-10-13 11:22:16 +0800 | [diff] [blame] | 1093 | |
Jerry Yu | c068b66 | 2021-10-11 22:30:19 +0800 | [diff] [blame] | 1094 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_tls13_generate_handshake_keys" ) ); |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1095 | |
Neil Armstrong | b818e16 | 2022-05-17 09:24:52 +0200 | [diff] [blame] | 1096 | ret = mbedtls_ssl_tls13_get_cipher_key_info( ciphersuite_info, |
| 1097 | &key_len, &iv_len ); |
| 1098 | if( ret != 0 ) |
Neil Armstrong | 4f4f271 | 2022-05-05 15:34:39 +0200 | [diff] [blame] | 1099 | { |
Neil Armstrong | b818e16 | 2022-05-17 09:24:52 +0200 | [diff] [blame] | 1100 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_get_cipher_key_info", ret ); |
Neil Armstrong | 4f4f271 | 2022-05-05 15:34:39 +0200 | [diff] [blame] | 1101 | return ret; |
| 1102 | } |
| 1103 | |
Jerry Yu | 435208a | 2021-10-13 11:22:16 +0800 | [diff] [blame] | 1104 | md_type = ciphersuite_info->mac; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 1105 | |
| 1106 | hash_alg = mbedtls_psa_translate_md( ciphersuite_info->mac ); |
| 1107 | hash_len = PSA_HASH_LENGTH( hash_alg ); |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1108 | |
| 1109 | ret = mbedtls_ssl_get_handshake_transcript( ssl, md_type, |
| 1110 | transcript, |
| 1111 | sizeof( transcript ), |
| 1112 | &transcript_len ); |
| 1113 | if( ret != 0 ) |
| 1114 | { |
| 1115 | MBEDTLS_SSL_DEBUG_RET( 1, |
| 1116 | "mbedtls_ssl_get_handshake_transcript", |
| 1117 | ret ); |
| 1118 | return( ret ); |
| 1119 | } |
| 1120 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 1121 | ret = mbedtls_ssl_tls13_derive_handshake_secrets( hash_alg, |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 1122 | handshake->tls13_master_secrets.handshake, |
Jerry Yu | 435208a | 2021-10-13 11:22:16 +0800 | [diff] [blame] | 1123 | transcript, transcript_len, tls13_hs_secrets ); |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1124 | if( ret != 0 ) |
| 1125 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1126 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_handshake_secrets", |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1127 | ret ); |
| 1128 | return( ret ); |
| 1129 | } |
| 1130 | |
| 1131 | MBEDTLS_SSL_DEBUG_BUF( 4, "Client handshake traffic secret", |
Jerry Yu | 435208a | 2021-10-13 11:22:16 +0800 | [diff] [blame] | 1132 | tls13_hs_secrets->client_handshake_traffic_secret, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 1133 | hash_len ); |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1134 | MBEDTLS_SSL_DEBUG_BUF( 4, "Server handshake traffic secret", |
Jerry Yu | 435208a | 2021-10-13 11:22:16 +0800 | [diff] [blame] | 1135 | tls13_hs_secrets->server_handshake_traffic_secret, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 1136 | hash_len ); |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1137 | |
| 1138 | /* |
| 1139 | * Export client handshake traffic secret |
| 1140 | */ |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1141 | if( ssl->f_export_keys != NULL ) |
| 1142 | { |
| 1143 | ssl->f_export_keys( ssl->p_export_keys, |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1144 | MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_HANDSHAKE_TRAFFIC_SECRET, |
Jerry Yu | 435208a | 2021-10-13 11:22:16 +0800 | [diff] [blame] | 1145 | tls13_hs_secrets->client_handshake_traffic_secret, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 1146 | hash_len, |
Jerry Yu | 435208a | 2021-10-13 11:22:16 +0800 | [diff] [blame] | 1147 | handshake->randbytes, |
lhuang04 | a3890a3 | 2022-01-04 09:47:20 -0800 | [diff] [blame] | 1148 | handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN, |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1149 | MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */ ); |
| 1150 | |
| 1151 | ssl->f_export_keys( ssl->p_export_keys, |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1152 | MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_HANDSHAKE_TRAFFIC_SECRET, |
Jerry Yu | 435208a | 2021-10-13 11:22:16 +0800 | [diff] [blame] | 1153 | tls13_hs_secrets->server_handshake_traffic_secret, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 1154 | hash_len, |
Jerry Yu | 435208a | 2021-10-13 11:22:16 +0800 | [diff] [blame] | 1155 | handshake->randbytes, |
lhuang04 | a3890a3 | 2022-01-04 09:47:20 -0800 | [diff] [blame] | 1156 | handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN, |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1157 | MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */ ); |
| 1158 | } |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1159 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 1160 | ret = mbedtls_ssl_tls13_make_traffic_keys( hash_alg, |
Jerry Yu | 435208a | 2021-10-13 11:22:16 +0800 | [diff] [blame] | 1161 | tls13_hs_secrets->client_handshake_traffic_secret, |
| 1162 | tls13_hs_secrets->server_handshake_traffic_secret, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 1163 | hash_len, key_len, iv_len, traffic_keys ); |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1164 | if( ret != 0 ) |
| 1165 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1166 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_make_traffic_keys", ret ); |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1167 | goto exit; |
| 1168 | } |
| 1169 | |
| 1170 | MBEDTLS_SSL_DEBUG_BUF( 4, "client_handshake write_key", |
| 1171 | traffic_keys->client_write_key, |
| 1172 | traffic_keys->key_len); |
| 1173 | |
| 1174 | MBEDTLS_SSL_DEBUG_BUF( 4, "server_handshake write_key", |
| 1175 | traffic_keys->server_write_key, |
| 1176 | traffic_keys->key_len); |
| 1177 | |
| 1178 | MBEDTLS_SSL_DEBUG_BUF( 4, "client_handshake write_iv", |
| 1179 | traffic_keys->client_write_iv, |
| 1180 | traffic_keys->iv_len); |
| 1181 | |
| 1182 | MBEDTLS_SSL_DEBUG_BUF( 4, "server_handshake write_iv", |
| 1183 | traffic_keys->server_write_iv, |
| 1184 | traffic_keys->iv_len); |
| 1185 | |
Jerry Yu | c068b66 | 2021-10-11 22:30:19 +0800 | [diff] [blame] | 1186 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_tls13_generate_handshake_keys" ) ); |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1187 | |
| 1188 | exit: |
| 1189 | |
| 1190 | return( ret ); |
| 1191 | } |
| 1192 | |
Jerry Yu | f0ac235 | 2021-10-11 17:47:07 +0800 | [diff] [blame] | 1193 | int mbedtls_ssl_tls13_key_schedule_stage_handshake( mbedtls_ssl_context *ssl ) |
Jerry Yu | a0650eb | 2021-09-09 17:14:45 +0800 | [diff] [blame] | 1194 | { |
Jerry Yu | f0ac235 | 2021-10-11 17:47:07 +0800 | [diff] [blame] | 1195 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | c0824bf | 2022-02-10 10:37:15 +0100 | [diff] [blame] | 1196 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED) && defined(MBEDTLS_ECDH_C) |
| 1197 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 1198 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED && MBEDTLS_ECDH_C */ |
Jerry Yu | 5ccfcd4 | 2021-10-11 16:39:29 +0800 | [diff] [blame] | 1199 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 1200 | psa_algorithm_t const hash_alg = mbedtls_psa_translate_md( |
| 1201 | handshake->ciphersuite_info->mac ); |
Jerry Yu | a0650eb | 2021-09-09 17:14:45 +0800 | [diff] [blame] | 1202 | |
Jerry Yu | f0ac235 | 2021-10-11 17:47:07 +0800 | [diff] [blame] | 1203 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED) |
| 1204 | /* |
| 1205 | * Compute ECDHE secret used to compute the handshake secret from which |
| 1206 | * client_handshake_traffic_secret and server_handshake_traffic_secret |
| 1207 | * are derived in the handshake secret derivation stage. |
| 1208 | */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1209 | if( mbedtls_ssl_tls13_ephemeral_enabled( ssl ) ) |
Jerry Yu | f0ac235 | 2021-10-11 17:47:07 +0800 | [diff] [blame] | 1210 | { |
| 1211 | if( mbedtls_ssl_tls13_named_group_is_ecdhe( handshake->offered_group_id ) ) |
| 1212 | { |
| 1213 | #if defined(MBEDTLS_ECDH_C) |
Przemyslaw Stekiel | c0824bf | 2022-02-10 10:37:15 +0100 | [diff] [blame] | 1214 | /* Compute ECDH shared secret. */ |
| 1215 | status = psa_raw_key_agreement( |
| 1216 | PSA_ALG_ECDH, handshake->ecdh_psa_privkey, |
| 1217 | handshake->ecdh_psa_peerkey, handshake->ecdh_psa_peerkey_len, |
| 1218 | handshake->premaster, sizeof( handshake->premaster ), |
| 1219 | &handshake->pmslen ); |
| 1220 | if( status != PSA_SUCCESS ) |
| 1221 | { |
| 1222 | ret = psa_ssl_status_to_mbedtls( status ); |
| 1223 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_raw_key_agreement", ret ); |
| 1224 | return( ret ); |
| 1225 | } |
| 1226 | |
| 1227 | status = psa_destroy_key( handshake->ecdh_psa_privkey ); |
| 1228 | if( status != PSA_SUCCESS ) |
| 1229 | { |
| 1230 | ret = psa_ssl_status_to_mbedtls( status ); |
| 1231 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_destroy_key", ret ); |
| 1232 | return( ret ); |
| 1233 | } |
| 1234 | |
| 1235 | handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT; |
Jerry Yu | f0ac235 | 2021-10-11 17:47:07 +0800 | [diff] [blame] | 1236 | #endif /* MBEDTLS_ECDH_C */ |
| 1237 | } |
| 1238 | else if( mbedtls_ssl_tls13_named_group_is_dhe( handshake->offered_group_id ) ) |
| 1239 | { |
Jerry Yu | f0ac235 | 2021-10-11 17:47:07 +0800 | [diff] [blame] | 1240 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHE not supported." ) ); |
| 1241 | return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ); |
| 1242 | } |
| 1243 | } |
| 1244 | #else |
| 1245 | return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ); |
| 1246 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */ |
Jerry Yu | a0650eb | 2021-09-09 17:14:45 +0800 | [diff] [blame] | 1247 | |
| 1248 | /* |
Jerry Yu | f0ac235 | 2021-10-11 17:47:07 +0800 | [diff] [blame] | 1249 | * Compute the Handshake Secret |
Jerry Yu | a0650eb | 2021-09-09 17:14:45 +0800 | [diff] [blame] | 1250 | */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 1251 | ret = mbedtls_ssl_tls13_evolve_secret( hash_alg, |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 1252 | handshake->tls13_master_secrets.early, |
Przemyslaw Stekiel | c0824bf | 2022-02-10 10:37:15 +0100 | [diff] [blame] | 1253 | handshake->premaster, handshake->pmslen, |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 1254 | handshake->tls13_master_secrets.handshake ); |
Jerry Yu | a0650eb | 2021-09-09 17:14:45 +0800 | [diff] [blame] | 1255 | if( ret != 0 ) |
| 1256 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1257 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret ); |
Jerry Yu | a0650eb | 2021-09-09 17:14:45 +0800 | [diff] [blame] | 1258 | return( ret ); |
| 1259 | } |
| 1260 | |
| 1261 | MBEDTLS_SSL_DEBUG_BUF( 4, "Handshake secret", |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 1262 | handshake->tls13_master_secrets.handshake, |
| 1263 | PSA_HASH_LENGTH( hash_alg ) ); |
Jerry Yu | a0650eb | 2021-09-09 17:14:45 +0800 | [diff] [blame] | 1264 | |
| 1265 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED) |
Przemyslaw Stekiel | c0824bf | 2022-02-10 10:37:15 +0100 | [diff] [blame] | 1266 | mbedtls_platform_zeroize( handshake->premaster, sizeof( handshake->premaster ) ); |
Jerry Yu | a0650eb | 2021-09-09 17:14:45 +0800 | [diff] [blame] | 1267 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */ |
| 1268 | return( 0 ); |
| 1269 | } |
| 1270 | |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1271 | /* Generate application traffic keys since any records following a 1-RTT Finished message |
| 1272 | * MUST be encrypted under the application traffic key. |
| 1273 | */ |
XiaokangQian | d0aa3e9 | 2021-11-10 06:17:40 +0000 | [diff] [blame] | 1274 | int mbedtls_ssl_tls13_generate_application_keys( |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1275 | mbedtls_ssl_context *ssl, |
| 1276 | mbedtls_ssl_key_set *traffic_keys ) |
| 1277 | { |
XiaokangQian | a763498 | 2021-10-22 06:32:32 +0000 | [diff] [blame] | 1278 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
XiaokangQian | 3306284 | 2021-11-11 03:37:45 +0000 | [diff] [blame] | 1279 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1280 | |
| 1281 | /* Address at which to store the application secrets */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1282 | mbedtls_ssl_tls13_application_secrets * const app_secrets = |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1283 | &ssl->session_negotiate->app_secrets; |
| 1284 | |
| 1285 | /* Holding the transcript up to and including the ServerFinished */ |
XiaokangQian | 3306284 | 2021-11-11 03:37:45 +0000 | [diff] [blame] | 1286 | unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE]; |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1287 | size_t transcript_len; |
| 1288 | |
| 1289 | /* Variables relating to the hash for the chosen ciphersuite. */ |
| 1290 | mbedtls_md_type_t md_type; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 1291 | |
| 1292 | psa_algorithm_t hash_alg; |
| 1293 | size_t hash_len; |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1294 | |
| 1295 | /* Variables relating to the cipher for the chosen ciphersuite. */ |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 1296 | size_t key_len, iv_len; |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1297 | |
| 1298 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive application traffic keys" ) ); |
| 1299 | |
| 1300 | /* Extract basic information about hash and ciphersuite */ |
| 1301 | |
Neil Armstrong | b818e16 | 2022-05-17 09:24:52 +0200 | [diff] [blame] | 1302 | ret = mbedtls_ssl_tls13_get_cipher_key_info( handshake->ciphersuite_info, |
| 1303 | &key_len, &iv_len ); |
| 1304 | if( ret != 0 ) |
Neil Armstrong | 4f4f271 | 2022-05-05 15:34:39 +0200 | [diff] [blame] | 1305 | { |
Neil Armstrong | b818e16 | 2022-05-17 09:24:52 +0200 | [diff] [blame] | 1306 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_get_cipher_key_info", ret ); |
Neil Armstrong | 4f4f271 | 2022-05-05 15:34:39 +0200 | [diff] [blame] | 1307 | goto cleanup; |
| 1308 | } |
| 1309 | |
XiaokangQian | 3306284 | 2021-11-11 03:37:45 +0000 | [diff] [blame] | 1310 | md_type = handshake->ciphersuite_info->mac; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 1311 | |
| 1312 | hash_alg = mbedtls_psa_translate_md( handshake->ciphersuite_info->mac ); |
| 1313 | hash_len = PSA_HASH_LENGTH( hash_alg ); |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1314 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1315 | /* Compute current handshake transcript. It's the caller's responsibility |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1316 | * to call this at the right time, that is, after the ServerFinished. */ |
| 1317 | |
| 1318 | ret = mbedtls_ssl_get_handshake_transcript( ssl, md_type, |
| 1319 | transcript, sizeof( transcript ), |
| 1320 | &transcript_len ); |
| 1321 | if( ret != 0 ) |
XiaokangQian | 4cab024 | 2021-10-12 08:43:37 +0000 | [diff] [blame] | 1322 | goto cleanup; |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1323 | |
| 1324 | /* Compute application secrets from master secret and transcript hash. */ |
| 1325 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 1326 | ret = mbedtls_ssl_tls13_derive_application_secrets( hash_alg, |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 1327 | handshake->tls13_master_secrets.app, |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1328 | transcript, transcript_len, |
| 1329 | app_secrets ); |
| 1330 | if( ret != 0 ) |
| 1331 | { |
| 1332 | MBEDTLS_SSL_DEBUG_RET( 1, |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1333 | "mbedtls_ssl_tls13_derive_application_secrets", ret ); |
XiaokangQian | 4cab024 | 2021-10-12 08:43:37 +0000 | [diff] [blame] | 1334 | goto cleanup; |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | /* Derive first epoch of IV + Key for application traffic. */ |
| 1338 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 1339 | ret = mbedtls_ssl_tls13_make_traffic_keys( hash_alg, |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1340 | app_secrets->client_application_traffic_secret_N, |
| 1341 | app_secrets->server_application_traffic_secret_N, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 1342 | hash_len, key_len, iv_len, traffic_keys ); |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1343 | if( ret != 0 ) |
| 1344 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1345 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_make_traffic_keys", ret ); |
XiaokangQian | 4cab024 | 2021-10-12 08:43:37 +0000 | [diff] [blame] | 1346 | goto cleanup; |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1347 | } |
| 1348 | |
| 1349 | MBEDTLS_SSL_DEBUG_BUF( 4, "Client application traffic secret", |
| 1350 | app_secrets->client_application_traffic_secret_N, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 1351 | hash_len ); |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1352 | |
| 1353 | MBEDTLS_SSL_DEBUG_BUF( 4, "Server application traffic secret", |
| 1354 | app_secrets->server_application_traffic_secret_N, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 1355 | hash_len ); |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1356 | |
XiaokangQian | ac0385c | 2021-11-03 06:40:11 +0000 | [diff] [blame] | 1357 | /* |
| 1358 | * Export client/server application traffic secret 0 |
| 1359 | */ |
| 1360 | if( ssl->f_export_keys != NULL ) |
| 1361 | { |
| 1362 | ssl->f_export_keys( ssl->p_export_keys, |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1363 | MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_APPLICATION_TRAFFIC_SECRET, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 1364 | app_secrets->client_application_traffic_secret_N, hash_len, |
XiaokangQian | 3306284 | 2021-11-11 03:37:45 +0000 | [diff] [blame] | 1365 | handshake->randbytes, |
lhuang04 | a3890a3 | 2022-01-04 09:47:20 -0800 | [diff] [blame] | 1366 | handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN, |
XiaokangQian | b51f884 | 2021-11-04 03:02:47 +0000 | [diff] [blame] | 1367 | MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by |
| 1368 | a new constant for TLS 1.3! */ ); |
XiaokangQian | ac0385c | 2021-11-03 06:40:11 +0000 | [diff] [blame] | 1369 | |
| 1370 | ssl->f_export_keys( ssl->p_export_keys, |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1371 | MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_APPLICATION_TRAFFIC_SECRET, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 1372 | app_secrets->server_application_traffic_secret_N, hash_len, |
XiaokangQian | 3306284 | 2021-11-11 03:37:45 +0000 | [diff] [blame] | 1373 | handshake->randbytes, |
lhuang04 | a3890a3 | 2022-01-04 09:47:20 -0800 | [diff] [blame] | 1374 | handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN, |
XiaokangQian | b51f884 | 2021-11-04 03:02:47 +0000 | [diff] [blame] | 1375 | MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by |
| 1376 | a new constant for TLS 1.3! */ ); |
XiaokangQian | ac0385c | 2021-11-03 06:40:11 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1379 | MBEDTLS_SSL_DEBUG_BUF( 4, "client application_write_key:", |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 1380 | traffic_keys->client_write_key, key_len ); |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1381 | MBEDTLS_SSL_DEBUG_BUF( 4, "server application write key", |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 1382 | traffic_keys->server_write_key, key_len ); |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1383 | MBEDTLS_SSL_DEBUG_BUF( 4, "client application write IV", |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 1384 | traffic_keys->client_write_iv, iv_len ); |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1385 | MBEDTLS_SSL_DEBUG_BUF( 4, "server application write IV", |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 1386 | traffic_keys->server_write_iv, iv_len ); |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1387 | |
| 1388 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive application traffic keys" ) ); |
XiaokangQian | 4cab024 | 2021-10-12 08:43:37 +0000 | [diff] [blame] | 1389 | |
| 1390 | cleanup: |
Jerry Yu | 2c70a39 | 2021-12-08 13:28:49 +0800 | [diff] [blame] | 1391 | /* randbytes is not used again */ |
| 1392 | mbedtls_platform_zeroize( ssl->handshake->randbytes, |
| 1393 | sizeof( ssl->handshake->randbytes ) ); |
Jerry Yu | ef2b98a | 2022-05-06 16:40:05 +0800 | [diff] [blame] | 1394 | |
XiaokangQian | 3306284 | 2021-11-11 03:37:45 +0000 | [diff] [blame] | 1395 | mbedtls_platform_zeroize( transcript, sizeof( transcript ) ); |
XiaokangQian | 4cab024 | 2021-10-12 08:43:37 +0000 | [diff] [blame] | 1396 | return( ret ); |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
Jerry Yu | f86eb75 | 2022-05-06 11:16:55 +0800 | [diff] [blame] | 1399 | int mbedtls_ssl_tls13_compute_handshake_transform( mbedtls_ssl_context *ssl ) |
Jerry Yu | e110d25 | 2022-05-05 10:19:22 +0800 | [diff] [blame] | 1400 | { |
Jerry Yu | ef2b98a | 2022-05-06 16:40:05 +0800 | [diff] [blame] | 1401 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | e110d25 | 2022-05-05 10:19:22 +0800 | [diff] [blame] | 1402 | mbedtls_ssl_key_set traffic_keys; |
| 1403 | mbedtls_ssl_transform *transform_handshake = NULL; |
| 1404 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 1405 | |
| 1406 | /* Compute handshake secret */ |
| 1407 | ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl ); |
| 1408 | if( ret != 0 ) |
| 1409 | { |
| 1410 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret ); |
| 1411 | goto cleanup; |
| 1412 | } |
| 1413 | |
| 1414 | /* Next evolution in key schedule: Establish handshake secret and |
| 1415 | * key material. */ |
| 1416 | ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys ); |
| 1417 | if( ret != 0 ) |
| 1418 | { |
| 1419 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys", |
| 1420 | ret ); |
| 1421 | goto cleanup; |
| 1422 | } |
| 1423 | |
| 1424 | transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) ); |
| 1425 | if( transform_handshake == NULL ) |
| 1426 | { |
| 1427 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 1428 | goto cleanup; |
| 1429 | } |
| 1430 | |
Jerry Yu | ef2b98a | 2022-05-06 16:40:05 +0800 | [diff] [blame] | 1431 | ret = mbedtls_ssl_tls13_populate_transform( |
| 1432 | transform_handshake, |
| 1433 | ssl->conf->endpoint, |
| 1434 | ssl->session_negotiate->ciphersuite, |
| 1435 | &traffic_keys, |
| 1436 | ssl ); |
Jerry Yu | e110d25 | 2022-05-05 10:19:22 +0800 | [diff] [blame] | 1437 | if( ret != 0 ) |
| 1438 | { |
| 1439 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret ); |
| 1440 | goto cleanup; |
| 1441 | } |
| 1442 | handshake->transform_handshake = transform_handshake; |
| 1443 | |
| 1444 | cleanup: |
| 1445 | mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) ); |
| 1446 | if( ret != 0 ) |
Jerry Yu | e110d25 | 2022-05-05 10:19:22 +0800 | [diff] [blame] | 1447 | mbedtls_free( transform_handshake ); |
Jerry Yu | e110d25 | 2022-05-05 10:19:22 +0800 | [diff] [blame] | 1448 | |
| 1449 | return( ret ); |
| 1450 | } |
| 1451 | |
Jerry Yu | ff22698 | 2022-04-16 16:52:57 +0800 | [diff] [blame] | 1452 | int mbedtls_ssl_tls13_generate_resumption_master_secret( |
| 1453 | mbedtls_ssl_context *ssl ) |
| 1454 | { |
| 1455 | /* Erase master secrets */ |
| 1456 | mbedtls_platform_zeroize( &ssl->handshake->tls13_master_secrets, |
| 1457 | sizeof( ssl->handshake->tls13_master_secrets ) ); |
| 1458 | return( 0 ); |
| 1459 | } |
| 1460 | |
Jerry Yu | fd5ea04 | 2022-05-19 14:29:48 +0800 | [diff] [blame] | 1461 | int mbedtls_ssl_tls13_compute_application_transform( mbedtls_ssl_context *ssl ) |
| 1462 | { |
| 1463 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1464 | mbedtls_ssl_key_set traffic_keys; |
| 1465 | mbedtls_ssl_transform *transform_application = NULL; |
| 1466 | |
| 1467 | ret = mbedtls_ssl_tls13_key_schedule_stage_application( ssl ); |
| 1468 | if( ret != 0 ) |
| 1469 | { |
| 1470 | MBEDTLS_SSL_DEBUG_RET( 1, |
| 1471 | "mbedtls_ssl_tls13_key_schedule_stage_application", ret ); |
| 1472 | goto cleanup; |
| 1473 | } |
| 1474 | |
| 1475 | ret = mbedtls_ssl_tls13_generate_application_keys( ssl, &traffic_keys ); |
| 1476 | if( ret != 0 ) |
| 1477 | { |
| 1478 | MBEDTLS_SSL_DEBUG_RET( 1, |
| 1479 | "mbedtls_ssl_tls13_generate_application_keys", ret ); |
| 1480 | goto cleanup; |
| 1481 | } |
| 1482 | |
| 1483 | transform_application = |
| 1484 | mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) ); |
| 1485 | if( transform_application == NULL ) |
| 1486 | { |
| 1487 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 1488 | goto cleanup; |
| 1489 | } |
| 1490 | |
| 1491 | ret = mbedtls_ssl_tls13_populate_transform( |
| 1492 | transform_application, |
| 1493 | ssl->conf->endpoint, |
| 1494 | ssl->session_negotiate->ciphersuite, |
| 1495 | &traffic_keys, |
| 1496 | ssl ); |
| 1497 | if( ret != 0 ) |
| 1498 | { |
| 1499 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret ); |
| 1500 | goto cleanup; |
| 1501 | } |
| 1502 | |
| 1503 | ssl->transform_application = transform_application; |
| 1504 | |
| 1505 | cleanup: |
| 1506 | |
| 1507 | mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) ); |
| 1508 | if( ret != 0 ) |
| 1509 | { |
| 1510 | mbedtls_free( transform_application ); |
| 1511 | } |
| 1512 | return( ret ); |
| 1513 | } |
| 1514 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1515 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |