blob: 949fa747410f2c0f7fcbfe1a4c7c3b6bf10e638e [file] [log] [blame]
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001/*
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 Yuc8a392c2021-08-18 16:46:28 +080026#include "mbedtls/error.h"
Jerry Yu75336352021-09-01 15:59:36 +080027#include "mbedtls/debug.h"
Jerry Yuc8a392c2021-08-18 16:46:28 +080028
Jerry Yu65dd2cc2021-08-18 16:38:40 +080029#include "ssl_misc.h"
XiaokangQian2d5c72b2021-09-13 07:30:09 +000030#include <mbedtls/debug.h>
31
32int mbedtls_ssl_tls13_fetch_handshake_msg( mbedtls_ssl_context *ssl,
33 unsigned hs_type,
34 unsigned char **buf,
35 size_t *buflen )
36{
37 int ret;
38
39 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
40 {
41 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
42 goto cleanup;
43 }
44
45 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
46 ssl->in_msg[0] != hs_type )
47 {
48 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
49 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
50 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
51 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
52 goto cleanup;
53 }
54
55 *buf = ssl->in_msg + 4;
56 *buflen = ssl->in_hslen - 4;
57
58
59cleanup:
60
61 return( ret );
62}
Jerry Yu65dd2cc2021-08-18 16:38:40 +080063
XiaokangQian6b226b02021-09-24 07:51:16 +000064int mbedtls_ssl_tls1_3_fetch_handshake_msg( mbedtls_ssl_context *ssl,
XiaokangQian16c61aa2021-09-27 09:30:17 +000065 unsigned hs_type,
66 unsigned char **buf,
67 size_t *buflen )
XiaokangQian6b226b02021-09-24 07:51:16 +000068{
69 int ret;
70
71 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
72 {
73 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
74 goto cleanup;
75 }
76
XiaokangQian16c61aa2021-09-27 09:30:17 +000077 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
XiaokangQian6b226b02021-09-24 07:51:16 +000078 ssl->in_msg[0] != hs_type )
79 {
XiaokangQian16c61aa2021-09-27 09:30:17 +000080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Receive unexpected handshake message." ) );
XiaokangQian6b226b02021-09-24 07:51:16 +000081 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
XiaokangQian05420b12021-09-29 08:46:37 +000082 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
XiaokangQian6b226b02021-09-24 07:51:16 +000083 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
84 goto cleanup;
85 }
86
XiaokangQian05420b12021-09-29 08:46:37 +000087 /*
88 * Jump handshake header (4 bytes, see Section 4 of RFC 8446).
89 * ...
90 * HandshakeType msg_type;
91 * uint24 length;
92 * ...
93 */
XiaokangQian6b226b02021-09-24 07:51:16 +000094 *buf = ssl->in_msg + 4;
95 *buflen = ssl->in_hslen - 4;
96
XiaokangQian6b226b02021-09-24 07:51:16 +000097cleanup:
98
99 return( ret );
100}
101
Jerry Yuf4436812021-08-26 22:59:56 +0800102int mbedtls_ssl_tls13_start_handshake_msg( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800103 unsigned hs_type,
104 unsigned char **buf,
Jerry Yu0c63af62021-09-02 12:59:12 +0800105 size_t *buf_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800106{
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800107 /*
108 * Reserve 4 bytes for hanshake header. ( Section 4,RFC 8446 )
109 * ...
110 * HandshakeType msg_type;
111 * uint24 length;
112 * ...
113 */
Jerry Yuc8a392c2021-08-18 16:46:28 +0800114 *buf = ssl->out_msg + 4;
Jerry Yu0c63af62021-09-02 12:59:12 +0800115 *buf_len = MBEDTLS_SSL_OUT_CONTENT_LEN - 4;
Jerry Yuc8a392c2021-08-18 16:46:28 +0800116
117 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
118 ssl->out_msg[0] = hs_type;
119
120 return( 0 );
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800121}
122
Jerry Yuf4436812021-08-26 22:59:56 +0800123int mbedtls_ssl_tls13_finish_handshake_msg( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800124 size_t buf_len,
125 size_t msg_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800126{
Jerry Yuc8a392c2021-08-18 16:46:28 +0800127 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800128 size_t msg_len_with_header;
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800129 ((void) buf_len);
Jerry Yuc8a392c2021-08-18 16:46:28 +0800130
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800131 /* Add reserved 4 bytes for handshake header */
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800132 msg_len_with_header = msg_len + 4;
133 ssl->out_msglen = msg_len_with_header;
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800134 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_handshake_msg_ext( ssl, 0 ) );
Jerry Yuc8a392c2021-08-18 16:46:28 +0800135
136cleanup:
137 return( ret );
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800138}
139
Jerry Yu48369522021-09-18 16:09:01 +0800140void mbedtls_ssl_tls1_3_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
141 unsigned hs_type,
142 unsigned char const *msg,
143 size_t msg_len )
Jerry Yu7bea4ba2021-09-09 15:06:18 +0800144{
145 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
146 ssl->handshake->update_checksum( ssl, msg, msg_len );
147}
148
Jerry Yuf4436812021-08-26 22:59:56 +0800149void mbedtls_ssl_tls13_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800150 unsigned hs_type,
151 size_t total_hs_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800152{
153 unsigned char hs_hdr[4];
154
155 /* Build HS header for checksum update. */
Jerry Yu2ac64192021-08-26 18:38:58 +0800156 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
157 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
158 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
159 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800160
161 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
162}
163
Jerry Yubc20bdd2021-08-24 15:59:48 +0800164#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
165
166/*
Jerry Yue41dec02021-08-31 10:57:07 +0800167 * mbedtls_ssl_tls13_write_sig_alg_ext( )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800168 *
169 * enum {
170 * ....
171 * ecdsa_secp256r1_sha256( 0x0403 ),
172 * ecdsa_secp384r1_sha384( 0x0503 ),
173 * ecdsa_secp521r1_sha512( 0x0603 ),
174 * ....
175 * } SignatureScheme;
176 *
177 * struct {
178 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
179 * } SignatureSchemeList;
180 *
181 * Only if we handle at least one key exchange that needs signatures.
182 */
Jerry Yue41dec02021-08-31 10:57:07 +0800183int mbedtls_ssl_tls13_write_sig_alg_ext( mbedtls_ssl_context *ssl,
184 unsigned char *buf,
185 unsigned char *end,
186 size_t *olen )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800187{
Jerry Yu72369942021-08-31 15:41:21 +0800188 unsigned char *p = buf;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800189 unsigned char *supported_sig_alg_ptr; /* Start of supported_signature_algorithms */
190 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
Jerry Yu72369942021-08-31 15:41:21 +0800191
Jerry Yu75336352021-09-01 15:59:36 +0800192 *olen = 0;
Jerry Yu72369942021-08-31 15:41:21 +0800193
194 /* Skip the extension on the client if all allowed key exchanges
195 * are PSK-based. */
196#if defined(MBEDTLS_SSL_CLI_C)
197 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
198 !mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
199 {
200 return( 0 );
201 }
202#endif /* MBEDTLS_SSL_CLI_C */
203
204 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
205
Jerry Yub60e3cf2021-09-08 16:41:02 +0800206 /* Check if we have space for header and length field:
207 * - extension_type (2 bytes)
208 * - extension_data_length (2 bytes)
209 * - supported_signature_algorithms_length (2 bytes)
210 */
Jerry Yu72369942021-08-31 15:41:21 +0800211 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
212 p += 6;
213
214 /*
215 * Write supported_signature_algorithms
216 */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800217 supported_sig_alg_ptr = p;
Jerry Yu72369942021-08-31 15:41:21 +0800218 for( const uint16_t *sig_alg = ssl->conf->tls13_sig_algs;
219 *sig_alg != MBEDTLS_TLS13_SIG_NONE; sig_alg++ )
220 {
221 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
222 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
223 p += 2;
224 MBEDTLS_SSL_DEBUG_MSG( 3, ( "signature scheme [%x]", *sig_alg ) );
225 }
226
Jerry Yub60e3cf2021-09-08 16:41:02 +0800227 /* Length of supported_signature_algorithms */
228 supported_sig_alg_len = p - supported_sig_alg_ptr;
229 if( supported_sig_alg_len == 0 )
Jerry Yu72369942021-08-31 15:41:21 +0800230 {
231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
232 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
233 }
234
235 /* Write extension_type */
236 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
237 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800238 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
Jerry Yu72369942021-08-31 15:41:21 +0800239 /* Write length of supported_signature_algorithms */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800240 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
Jerry Yu72369942021-08-31 15:41:21 +0800241
242 /* Output the total length of signature algorithms extension. */
243 *olen = p - buf;
244
245 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
Jerry Yu75336352021-09-01 15:59:36 +0800246 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800247}
248
249#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
250
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800251#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
252
253#endif /* MBEDTLS_SSL_TLS_C */