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