Jerry Yu | 65dd2cc | 2021-08-18 16:38:40 +0800 | [diff] [blame] | 1 | /* |
| 2 | * TLS 1.3 functionality shared between client and server |
| 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 | |
| 20 | #include "common.h" |
| 21 | |
| 22 | #if defined(MBEDTLS_SSL_TLS_C) |
| 23 | |
| 24 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
| 25 | |
Jerry Yu | c8a392c | 2021-08-18 16:46:28 +0800 | [diff] [blame] | 26 | #include "mbedtls/error.h" |
Jerry Yu | 7533635 | 2021-09-01 15:59:36 +0800 | [diff] [blame] | 27 | #include "mbedtls/debug.h" |
Jerry Yu | c8a392c | 2021-08-18 16:46:28 +0800 | [diff] [blame] | 28 | |
Jerry Yu | 65dd2cc | 2021-08-18 16:38:40 +0800 | [diff] [blame] | 29 | #include "ssl_misc.h" |
| 30 | |
XiaokangQian | 6b226b0 | 2021-09-24 07:51:16 +0000 | [diff] [blame] | 31 | int mbedtls_ssl_tls1_3_fetch_handshake_msg( mbedtls_ssl_context *ssl, |
XiaokangQian | 16c61aa | 2021-09-27 09:30:17 +0000 | [diff] [blame] | 32 | unsigned hs_type, |
| 33 | unsigned char **buf, |
| 34 | size_t *buflen ) |
XiaokangQian | 6b226b0 | 2021-09-24 07:51:16 +0000 | [diff] [blame] | 35 | { |
| 36 | int ret; |
| 37 | |
| 38 | if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 ) |
| 39 | { |
| 40 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 41 | goto cleanup; |
| 42 | } |
| 43 | |
XiaokangQian | 16c61aa | 2021-09-27 09:30:17 +0000 | [diff] [blame] | 44 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE || |
XiaokangQian | 6b226b0 | 2021-09-24 07:51:16 +0000 | [diff] [blame] | 45 | ssl->in_msg[0] != hs_type ) |
| 46 | { |
XiaokangQian | 16c61aa | 2021-09-27 09:30:17 +0000 | [diff] [blame] | 47 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Receive unexpected handshake message." ) ); |
XiaokangQian | 6b226b0 | 2021-09-24 07:51:16 +0000 | [diff] [blame] | 48 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE, |
XiaokangQian | 05420b1 | 2021-09-29 08:46:37 +0000 | [diff] [blame^] | 49 | MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
XiaokangQian | 6b226b0 | 2021-09-24 07:51:16 +0000 | [diff] [blame] | 50 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 51 | goto cleanup; |
| 52 | } |
| 53 | |
XiaokangQian | 05420b1 | 2021-09-29 08:46:37 +0000 | [diff] [blame^] | 54 | /* |
| 55 | * Jump handshake header (4 bytes, see Section 4 of RFC 8446). |
| 56 | * ... |
| 57 | * HandshakeType msg_type; |
| 58 | * uint24 length; |
| 59 | * ... |
| 60 | */ |
XiaokangQian | 6b226b0 | 2021-09-24 07:51:16 +0000 | [diff] [blame] | 61 | *buf = ssl->in_msg + 4; |
| 62 | *buflen = ssl->in_hslen - 4; |
| 63 | |
XiaokangQian | 6b226b0 | 2021-09-24 07:51:16 +0000 | [diff] [blame] | 64 | cleanup: |
| 65 | |
| 66 | return( ret ); |
| 67 | } |
| 68 | |
Jerry Yu | f443681 | 2021-08-26 22:59:56 +0800 | [diff] [blame] | 69 | int mbedtls_ssl_tls13_start_handshake_msg( mbedtls_ssl_context *ssl, |
Jerry Yu | eecfbf0 | 2021-08-30 18:32:07 +0800 | [diff] [blame] | 70 | unsigned hs_type, |
| 71 | unsigned char **buf, |
Jerry Yu | 0c63af6 | 2021-09-02 12:59:12 +0800 | [diff] [blame] | 72 | size_t *buf_len ) |
Jerry Yu | 65dd2cc | 2021-08-18 16:38:40 +0800 | [diff] [blame] | 73 | { |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 74 | /* |
| 75 | * Reserve 4 bytes for hanshake header. ( Section 4,RFC 8446 ) |
| 76 | * ... |
| 77 | * HandshakeType msg_type; |
| 78 | * uint24 length; |
| 79 | * ... |
| 80 | */ |
Jerry Yu | c8a392c | 2021-08-18 16:46:28 +0800 | [diff] [blame] | 81 | *buf = ssl->out_msg + 4; |
Jerry Yu | 0c63af6 | 2021-09-02 12:59:12 +0800 | [diff] [blame] | 82 | *buf_len = MBEDTLS_SSL_OUT_CONTENT_LEN - 4; |
Jerry Yu | c8a392c | 2021-08-18 16:46:28 +0800 | [diff] [blame] | 83 | |
| 84 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 85 | ssl->out_msg[0] = hs_type; |
| 86 | |
| 87 | return( 0 ); |
Jerry Yu | 65dd2cc | 2021-08-18 16:38:40 +0800 | [diff] [blame] | 88 | } |
| 89 | |
Jerry Yu | f443681 | 2021-08-26 22:59:56 +0800 | [diff] [blame] | 90 | int mbedtls_ssl_tls13_finish_handshake_msg( mbedtls_ssl_context *ssl, |
Jerry Yu | eecfbf0 | 2021-08-30 18:32:07 +0800 | [diff] [blame] | 91 | size_t buf_len, |
| 92 | size_t msg_len ) |
Jerry Yu | 65dd2cc | 2021-08-18 16:38:40 +0800 | [diff] [blame] | 93 | { |
Jerry Yu | c8a392c | 2021-08-18 16:46:28 +0800 | [diff] [blame] | 94 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | dbfb7bd | 2021-09-04 09:58:58 +0800 | [diff] [blame] | 95 | size_t msg_len_with_header; |
Jerry Yu | 65dd2cc | 2021-08-18 16:38:40 +0800 | [diff] [blame] | 96 | ((void) buf_len); |
Jerry Yu | c8a392c | 2021-08-18 16:46:28 +0800 | [diff] [blame] | 97 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 98 | /* Add reserved 4 bytes for handshake header */ |
Jerry Yu | dbfb7bd | 2021-09-04 09:58:58 +0800 | [diff] [blame] | 99 | msg_len_with_header = msg_len + 4; |
| 100 | ssl->out_msglen = msg_len_with_header; |
Jerry Yu | 2c0fbf3 | 2021-09-02 13:53:46 +0800 | [diff] [blame] | 101 | MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_handshake_msg_ext( ssl, 0 ) ); |
Jerry Yu | c8a392c | 2021-08-18 16:46:28 +0800 | [diff] [blame] | 102 | |
| 103 | cleanup: |
| 104 | return( ret ); |
Jerry Yu | 65dd2cc | 2021-08-18 16:38:40 +0800 | [diff] [blame] | 105 | } |
| 106 | |
Jerry Yu | f443681 | 2021-08-26 22:59:56 +0800 | [diff] [blame] | 107 | void mbedtls_ssl_tls13_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl, |
Jerry Yu | eecfbf0 | 2021-08-30 18:32:07 +0800 | [diff] [blame] | 108 | unsigned hs_type, |
| 109 | size_t total_hs_len ) |
Jerry Yu | 65dd2cc | 2021-08-18 16:38:40 +0800 | [diff] [blame] | 110 | { |
| 111 | unsigned char hs_hdr[4]; |
| 112 | |
| 113 | /* Build HS header for checksum update. */ |
Jerry Yu | 2ac6419 | 2021-08-26 18:38:58 +0800 | [diff] [blame] | 114 | hs_hdr[0] = MBEDTLS_BYTE_0( hs_type ); |
| 115 | hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len ); |
| 116 | hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len ); |
| 117 | hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len ); |
Jerry Yu | 65dd2cc | 2021-08-18 16:38:40 +0800 | [diff] [blame] | 118 | |
| 119 | ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) ); |
| 120 | } |
| 121 | |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 122 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 123 | |
| 124 | /* |
Jerry Yu | e41dec0 | 2021-08-31 10:57:07 +0800 | [diff] [blame] | 125 | * mbedtls_ssl_tls13_write_sig_alg_ext( ) |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 126 | * |
| 127 | * enum { |
| 128 | * .... |
| 129 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 130 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 131 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 132 | * .... |
| 133 | * } SignatureScheme; |
| 134 | * |
| 135 | * struct { |
| 136 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 137 | * } SignatureSchemeList; |
| 138 | * |
| 139 | * Only if we handle at least one key exchange that needs signatures. |
| 140 | */ |
Jerry Yu | e41dec0 | 2021-08-31 10:57:07 +0800 | [diff] [blame] | 141 | int mbedtls_ssl_tls13_write_sig_alg_ext( mbedtls_ssl_context *ssl, |
| 142 | unsigned char *buf, |
| 143 | unsigned char *end, |
| 144 | size_t *olen ) |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 145 | { |
Jerry Yu | 7236994 | 2021-08-31 15:41:21 +0800 | [diff] [blame] | 146 | unsigned char *p = buf; |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 147 | unsigned char *supported_sig_alg_ptr; /* Start of supported_signature_algorithms */ |
| 148 | size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */ |
Jerry Yu | 7236994 | 2021-08-31 15:41:21 +0800 | [diff] [blame] | 149 | |
Jerry Yu | 7533635 | 2021-09-01 15:59:36 +0800 | [diff] [blame] | 150 | *olen = 0; |
Jerry Yu | 7236994 | 2021-08-31 15:41:21 +0800 | [diff] [blame] | 151 | |
| 152 | /* Skip the extension on the client if all allowed key exchanges |
| 153 | * are PSK-based. */ |
| 154 | #if defined(MBEDTLS_SSL_CLI_C) |
| 155 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 156 | !mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) ) |
| 157 | { |
| 158 | return( 0 ); |
| 159 | } |
| 160 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 161 | |
| 162 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) ); |
| 163 | |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 164 | /* Check if we have space for header and length field: |
| 165 | * - extension_type (2 bytes) |
| 166 | * - extension_data_length (2 bytes) |
| 167 | * - supported_signature_algorithms_length (2 bytes) |
| 168 | */ |
Jerry Yu | 7236994 | 2021-08-31 15:41:21 +0800 | [diff] [blame] | 169 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); |
| 170 | p += 6; |
| 171 | |
| 172 | /* |
| 173 | * Write supported_signature_algorithms |
| 174 | */ |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 175 | supported_sig_alg_ptr = p; |
Jerry Yu | 7236994 | 2021-08-31 15:41:21 +0800 | [diff] [blame] | 176 | for( const uint16_t *sig_alg = ssl->conf->tls13_sig_algs; |
| 177 | *sig_alg != MBEDTLS_TLS13_SIG_NONE; sig_alg++ ) |
| 178 | { |
| 179 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 180 | MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 ); |
| 181 | p += 2; |
| 182 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "signature scheme [%x]", *sig_alg ) ); |
| 183 | } |
| 184 | |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 185 | /* Length of supported_signature_algorithms */ |
| 186 | supported_sig_alg_len = p - supported_sig_alg_ptr; |
| 187 | if( supported_sig_alg_len == 0 ) |
Jerry Yu | 7236994 | 2021-08-31 15:41:21 +0800 | [diff] [blame] | 188 | { |
| 189 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) ); |
| 190 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 191 | } |
| 192 | |
| 193 | /* Write extension_type */ |
| 194 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 ); |
| 195 | /* Write extension_data_length */ |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 196 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 ); |
Jerry Yu | 7236994 | 2021-08-31 15:41:21 +0800 | [diff] [blame] | 197 | /* Write length of supported_signature_algorithms */ |
Jerry Yu | b60e3cf | 2021-09-08 16:41:02 +0800 | [diff] [blame] | 198 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 ); |
Jerry Yu | 7236994 | 2021-08-31 15:41:21 +0800 | [diff] [blame] | 199 | |
| 200 | /* Output the total length of signature algorithms extension. */ |
| 201 | *olen = p - buf; |
| 202 | |
| 203 | ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG; |
Jerry Yu | 7533635 | 2021-09-01 15:59:36 +0800 | [diff] [blame] | 204 | return( 0 ); |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 208 | |
Jerry Yu | 65dd2cc | 2021-08-18 16:38:40 +0800 | [diff] [blame] | 209 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
| 210 | |
| 211 | #endif /* MBEDTLS_SSL_TLS_C */ |