blob: 226f8e33f714462d66e1599bf210b0ecc277b3b7 [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
Ronald Cron6f135e12021-12-08 16:57:54 +010024#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu65dd2cc2021-08-18 16:38:40 +080025
Jerry Yu30b071c2021-09-12 20:16:03 +080026#include <string.h>
27
Jerry Yuc8a392c2021-08-18 16:46:28 +080028#include "mbedtls/error.h"
Jerry Yu75336352021-09-01 15:59:36 +080029#include "mbedtls/debug.h"
Jerry Yu30b071c2021-09-12 20:16:03 +080030#include "mbedtls/oid.h"
31#include "mbedtls/platform.h"
Gabor Mezei685472b2021-11-24 11:17:36 +010032#include "mbedtls/constant_time.h"
XiaokangQian74af2a82021-09-22 07:40:30 +000033#include <string.h>
Jerry Yuc8a392c2021-08-18 16:46:28 +080034
Jerry Yu65dd2cc2021-08-18 16:38:40 +080035#include "ssl_misc.h"
Jerry Yu30b071c2021-09-12 20:16:03 +080036#include "ssl_tls13_keys.h"
Jerry Yu65dd2cc2021-08-18 16:38:40 +080037
Xiaofei Bai746f9482021-11-12 08:53:56 +000038int mbedtls_ssl_tls13_fetch_handshake_msg( mbedtls_ssl_context *ssl,
39 unsigned hs_type,
40 unsigned char **buf,
Xiaofei Baieef15042021-11-18 07:29:56 +000041 size_t *buf_len )
XiaokangQian6b226b02021-09-24 07:51:16 +000042{
43 int ret;
44
45 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
46 {
47 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
48 goto cleanup;
49 }
50
XiaokangQian16c61aa2021-09-27 09:30:17 +000051 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
XiaokangQian6b226b02021-09-24 07:51:16 +000052 ssl->in_msg[0] != hs_type )
53 {
XiaokangQian16c61aa2021-09-27 09:30:17 +000054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Receive unexpected handshake message." ) );
XiaokangQian6b226b02021-09-24 07:51:16 +000055 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
XiaokangQian05420b12021-09-29 08:46:37 +000056 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
XiaokangQian6b226b02021-09-24 07:51:16 +000057 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
58 goto cleanup;
59 }
60
XiaokangQian05420b12021-09-29 08:46:37 +000061 /*
62 * Jump handshake header (4 bytes, see Section 4 of RFC 8446).
63 * ...
64 * HandshakeType msg_type;
65 * uint24 length;
66 * ...
67 */
Xiaofei Baieef15042021-11-18 07:29:56 +000068 *buf = ssl->in_msg + 4;
69 *buf_len = ssl->in_hslen - 4;
XiaokangQian6b226b02021-09-24 07:51:16 +000070
XiaokangQian6b226b02021-09-24 07:51:16 +000071cleanup:
72
73 return( ret );
74}
75
Jerry Yuf4436812021-08-26 22:59:56 +080076int mbedtls_ssl_tls13_start_handshake_msg( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +080077 unsigned hs_type,
78 unsigned char **buf,
Jerry Yu0c63af62021-09-02 12:59:12 +080079 size_t *buf_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +080080{
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080081 /*
82 * Reserve 4 bytes for hanshake header. ( Section 4,RFC 8446 )
83 * ...
84 * HandshakeType msg_type;
85 * uint24 length;
86 * ...
87 */
Jerry Yuc8a392c2021-08-18 16:46:28 +080088 *buf = ssl->out_msg + 4;
Jerry Yu0c63af62021-09-02 12:59:12 +080089 *buf_len = MBEDTLS_SSL_OUT_CONTENT_LEN - 4;
Jerry Yuc8a392c2021-08-18 16:46:28 +080090
91 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
92 ssl->out_msg[0] = hs_type;
93
94 return( 0 );
Jerry Yu65dd2cc2021-08-18 16:38:40 +080095}
96
Jerry Yuf4436812021-08-26 22:59:56 +080097int mbedtls_ssl_tls13_finish_handshake_msg( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +080098 size_t buf_len,
99 size_t msg_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800100{
Jerry Yuc8a392c2021-08-18 16:46:28 +0800101 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baieef15042021-11-18 07:29:56 +0000102 size_t msg_with_header_len;
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800103 ((void) buf_len);
Jerry Yuc8a392c2021-08-18 16:46:28 +0800104
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800105 /* Add reserved 4 bytes for handshake header */
Xiaofei Baieef15042021-11-18 07:29:56 +0000106 msg_with_header_len = msg_len + 4;
107 ssl->out_msglen = msg_with_header_len;
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800108 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_handshake_msg_ext( ssl, 0 ) );
Jerry Yuc8a392c2021-08-18 16:46:28 +0800109
110cleanup:
111 return( ret );
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800112}
113
Xiaofei Bai746f9482021-11-12 08:53:56 +0000114void mbedtls_ssl_tls13_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
115 unsigned hs_type,
116 unsigned char const *msg,
117 size_t msg_len )
Jerry Yu7bea4ba2021-09-09 15:06:18 +0800118{
119 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
120 ssl->handshake->update_checksum( ssl, msg, msg_len );
121}
122
Jerry Yuf4436812021-08-26 22:59:56 +0800123void mbedtls_ssl_tls13_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800124 unsigned hs_type,
125 size_t total_hs_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800126{
127 unsigned char hs_hdr[4];
128
129 /* Build HS header for checksum update. */
Jerry Yu2ac64192021-08-26 18:38:58 +0800130 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
131 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
132 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
133 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800134
135 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
136}
137
Jerry Yubc20bdd2021-08-24 15:59:48 +0800138#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu30b071c2021-09-12 20:16:03 +0800139/*
Jerry Yu30b071c2021-09-12 20:16:03 +0800140 * STATE HANDLING: Read CertificateVerify
141 */
Jerry Yud0fc5852021-10-29 11:09:06 +0800142/* Macro to express the maximum length of the verify structure.
Jerry Yu30b071c2021-09-12 20:16:03 +0800143 *
144 * The structure is computed per TLS 1.3 specification as:
145 * - 64 bytes of octet 32,
146 * - 33 bytes for the context string
147 * (which is either "TLS 1.3, client CertificateVerify"
148 * or "TLS 1.3, server CertificateVerify"),
Jerry Yud0fc5852021-10-29 11:09:06 +0800149 * - 1 byte for the octet 0x0, which serves as a separator,
Jerry Yu30b071c2021-09-12 20:16:03 +0800150 * - 32 or 48 bytes for the Transcript-Hash(Handshake Context, Certificate)
151 * (depending on the size of the transcript_hash)
152 *
153 * This results in a total size of
154 * - 130 bytes for a SHA256-based transcript hash, or
155 * (64 + 33 + 1 + 32 bytes)
156 * - 146 bytes for a SHA384-based transcript hash.
157 * (64 + 33 + 1 + 48 bytes)
158 *
159 */
Jerry Yu26c2d112021-10-25 12:42:58 +0800160#define SSL_VERIFY_STRUCT_MAX_SIZE ( 64 + \
161 33 + \
162 1 + \
163 MBEDTLS_TLS1_3_MD_MAX_SIZE \
Jerry Yu30b071c2021-09-12 20:16:03 +0800164 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800165
Jerry Yu0b32c502021-10-28 13:41:59 +0800166/*
167 * The ssl_tls13_create_verify_structure() creates the verify structure.
168 * As input, it requires the transcript hash.
169 *
170 * The caller has to ensure that the buffer has size at least
171 * SSL_VERIFY_STRUCT_MAX_SIZE bytes.
172 */
Jerry Yud0fc5852021-10-29 11:09:06 +0800173static void ssl_tls13_create_verify_structure( const unsigned char *transcript_hash,
Jerry Yu0b32c502021-10-28 13:41:59 +0800174 size_t transcript_hash_len,
175 unsigned char *verify_buffer,
176 size_t *verify_buffer_len,
177 int from )
178{
179 size_t idx;
Jerry Yu30b071c2021-09-12 20:16:03 +0800180
Jerry Yu0b32c502021-10-28 13:41:59 +0800181 /* RFC 8446, Section 4.4.3:
182 *
183 * The digital signature [in the CertificateVerify message] is then
184 * computed over the concatenation of:
185 * - A string that consists of octet 32 (0x20) repeated 64 times
186 * - The context string
187 * - A single 0 byte which serves as the separator
188 * - The content to be signed
189 */
190 memset( verify_buffer, 0x20, 64 );
191 idx = 64;
192
193 if( from == MBEDTLS_SSL_IS_CLIENT )
194 {
195 memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( client_cv ) );
196 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( client_cv );
197 }
198 else
199 { /* from == MBEDTLS_SSL_IS_SERVER */
200 memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( server_cv ) );
201 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( server_cv );
202 }
203
204 verify_buffer[idx++] = 0x0;
205
206 memcpy( verify_buffer + idx, transcript_hash, transcript_hash_len );
207 idx += transcript_hash_len;
208
209 *verify_buffer_len = idx;
210}
211
Jerry Yud0fc5852021-10-29 11:09:06 +0800212static int ssl_tls13_sig_alg_is_offered( const mbedtls_ssl_context *ssl,
Jerry Yu2d0bd322022-01-12 12:58:00 +0800213 uint16_t proposed_sig_alg )
Jerry Yu982d9e52021-10-14 15:59:37 +0800214{
Jerry Yu6106fdc2022-01-12 16:36:14 +0800215 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl );
216 if( sig_alg == NULL )
217 return( 0 );
218
219 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
Jerry Yu982d9e52021-10-14 15:59:37 +0800220 {
Jerry Yu2d0bd322022-01-12 12:58:00 +0800221 if( *sig_alg == proposed_sig_alg )
Jerry Yud0fc5852021-10-29 11:09:06 +0800222 return( 1 );
Jerry Yu982d9e52021-10-14 15:59:37 +0800223 }
Jerry Yud0fc5852021-10-29 11:09:06 +0800224 return( 0 );
Jerry Yu982d9e52021-10-14 15:59:37 +0800225}
226
Jerry Yu0b32c502021-10-28 13:41:59 +0800227static int ssl_tls13_parse_certificate_verify( mbedtls_ssl_context *ssl,
Jerry Yud0fc5852021-10-29 11:09:06 +0800228 const unsigned char *buf,
229 const unsigned char *end,
230 const unsigned char *verify_buffer,
231 size_t verify_buffer_len )
Jerry Yu30b071c2021-09-12 20:16:03 +0800232{
233 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
234 const unsigned char *p = buf;
235 uint16_t algorithm;
Jerry Yu30b071c2021-09-12 20:16:03 +0800236 size_t signature_len;
237 mbedtls_pk_type_t sig_alg;
238 mbedtls_md_type_t md_alg;
Jerry Yud0fc5852021-10-29 11:09:06 +0800239 unsigned char verify_hash[MBEDTLS_MD_MAX_SIZE];
Jerry Yu30b071c2021-09-12 20:16:03 +0800240 size_t verify_hash_len;
241
Xiaofei Baid25fab62021-12-02 06:36:27 +0000242 void const *options = NULL;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000243#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Xiaofei Baid25fab62021-12-02 06:36:27 +0000244 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000245#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
246
Jerry Yu30b071c2021-09-12 20:16:03 +0800247 /*
248 * struct {
249 * SignatureScheme algorithm;
250 * opaque signature<0..2^16-1>;
251 * } CertificateVerify;
252 */
253 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
254 algorithm = MBEDTLS_GET_UINT16_BE( p, 0 );
255 p += 2;
256
257 /* RFC 8446 section 4.4.3
258 *
259 * If the CertificateVerify message is sent by a server, the signature algorithm
260 * MUST be one offered in the client's "signature_algorithms" extension unless
261 * no valid certificate chain can be produced without unsupported algorithms
262 *
263 * RFC 8446 section 4.4.2.2
264 *
265 * If the client cannot construct an acceptable chain using the provided
266 * certificates and decides to abort the handshake, then it MUST abort the handshake
267 * with an appropriate certificate-related alert (by default, "unsupported_certificate").
268 *
Jerry Yu6f87f252021-10-29 20:12:51 +0800269 * Check if algorithm is an offered signature algorithm.
Jerry Yu30b071c2021-09-12 20:16:03 +0800270 */
Jerry Yu0b32c502021-10-28 13:41:59 +0800271 if( ! ssl_tls13_sig_alg_is_offered( ssl, algorithm ) )
Jerry Yu30b071c2021-09-12 20:16:03 +0800272 {
Jerry Yu982d9e52021-10-14 15:59:37 +0800273 /* algorithm not in offered signature algorithms list */
274 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Received signature algorithm(%04x) is not "
275 "offered.",
276 ( unsigned int ) algorithm ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800277 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800278 }
279
280 /* We currently only support ECDSA-based signatures */
281 switch( algorithm )
282 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000283 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Jerry Yu30b071c2021-09-12 20:16:03 +0800284 md_alg = MBEDTLS_MD_SHA256;
285 sig_alg = MBEDTLS_PK_ECDSA;
286 break;
Xiaofei Bai746f9482021-11-12 08:53:56 +0000287 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Jerry Yu30b071c2021-09-12 20:16:03 +0800288 md_alg = MBEDTLS_MD_SHA384;
289 sig_alg = MBEDTLS_PK_ECDSA;
290 break;
Xiaofei Bai746f9482021-11-12 08:53:56 +0000291 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Jerry Yu30b071c2021-09-12 20:16:03 +0800292 md_alg = MBEDTLS_MD_SHA512;
293 sig_alg = MBEDTLS_PK_ECDSA;
294 break;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000295#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Xiaofei Bai6dc90da2021-11-26 08:11:40 +0000296 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
XiaokangQiana83014d2021-11-22 07:53:34 +0000297 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Certificate Verify: using RSA PSS" ) );
XiaokangQian82d34cc2021-11-03 08:51:56 +0000298 md_alg = MBEDTLS_MD_SHA256;
299 sig_alg = MBEDTLS_PK_RSASSA_PSS;
300 break;
301#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800302 default:
303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Certificate Verify: Unknown signature algorithm." ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800304 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800305 }
306
307 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate Verify: Signature algorithm ( %04x )",
308 ( unsigned int ) algorithm ) );
309
310 /*
311 * Check the certificate's key type matches the signature alg
312 */
313 if( !mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk, sig_alg ) )
314 {
315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "signature algorithm doesn't match cert key" ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800316 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800317 }
318
319 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
320 signature_len = MBEDTLS_GET_UINT16_BE( p, 0 );
321 p += 2;
322 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, signature_len );
323
324 /* Hash verify buffer with indicated hash function */
325 switch( md_alg )
326 {
327#if defined(MBEDTLS_SHA256_C)
328 case MBEDTLS_MD_SHA256:
329 verify_hash_len = 32;
Jerry Yu133690c2021-10-25 14:01:13 +0800330 ret = mbedtls_sha256( verify_buffer, verify_buffer_len, verify_hash, 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800331 break;
332#endif /* MBEDTLS_SHA256_C */
333
334#if defined(MBEDTLS_SHA384_C)
335 case MBEDTLS_MD_SHA384:
336 verify_hash_len = 48;
Jerry Yu133690c2021-10-25 14:01:13 +0800337 ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 1 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800338 break;
339#endif /* MBEDTLS_SHA384_C */
340
341#if defined(MBEDTLS_SHA512_C)
342 case MBEDTLS_MD_SHA512:
343 verify_hash_len = 64;
Jerry Yu133690c2021-10-25 14:01:13 +0800344 ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800345 break;
346#endif /* MBEDTLS_SHA512_C */
347
Jerry Yu0b32c502021-10-28 13:41:59 +0800348 default:
349 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
350 break;
Jerry Yu30b071c2021-09-12 20:16:03 +0800351 }
352
Jerry Yu133690c2021-10-25 14:01:13 +0800353 if( ret != 0 )
354 {
355 MBEDTLS_SSL_DEBUG_RET( 1, "hash computation error", ret );
Jerry Yu6f87f252021-10-29 20:12:51 +0800356 goto error;
Jerry Yu133690c2021-10-25 14:01:13 +0800357 }
358
Jerry Yu30b071c2021-09-12 20:16:03 +0800359 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
XiaokangQian82d34cc2021-11-03 08:51:56 +0000360#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
361 if( sig_alg == MBEDTLS_PK_RSASSA_PSS )
362 {
363 const mbedtls_md_info_t* md_info;
Xiaofei Baid25fab62021-12-02 06:36:27 +0000364 rsassa_pss_options.mgf1_hash_id = md_alg;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000365 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
366 {
367 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
368 }
Xiaofei Baid25fab62021-12-02 06:36:27 +0000369 rsassa_pss_options.expected_salt_len = mbedtls_md_get_size( md_info );
370 options = (const void*) &rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000371 }
372#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800373
Xiaofei Baid25fab62021-12-02 06:36:27 +0000374 if( ( ret = mbedtls_pk_verify_ext( sig_alg, options,
Jerry Yu30b071c2021-09-12 20:16:03 +0800375 &ssl->session_negotiate->peer_cert->pk,
376 md_alg, verify_hash, verify_hash_len,
Jerry Yu6f87f252021-10-29 20:12:51 +0800377 p, signature_len ) ) == 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800378 {
Jerry Yu6f87f252021-10-29 20:12:51 +0800379 return( 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800380 }
Jerry Yu6f87f252021-10-29 20:12:51 +0800381 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify_ext", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800382
Jerry Yu6f87f252021-10-29 20:12:51 +0800383error:
384 /* RFC 8446 section 4.4.3
385 *
386 * If the verification fails, the receiver MUST terminate the handshake
387 * with a "decrypt_error" alert.
388 */
389 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
390 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
391 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
392
Jerry Yu30b071c2021-09-12 20:16:03 +0800393}
394#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
395
396int mbedtls_ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
397{
Jerry Yu30b071c2021-09-12 20:16:03 +0800398
Jerry Yuda8cdf22021-10-25 15:06:49 +0800399#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
400 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
401 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
402 size_t verify_buffer_len;
403 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
404 size_t transcript_len;
405 unsigned char *buf;
406 size_t buf_len;
407
Jerry Yu30b071c2021-09-12 20:16:03 +0800408 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
409
Jerry Yuda8cdf22021-10-25 15:06:49 +0800410 MBEDTLS_SSL_PROC_CHK(
Xiaofei Bai746f9482021-11-12 08:53:56 +0000411 mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
Jerry Yuda8cdf22021-10-25 15:06:49 +0800412 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu30b071c2021-09-12 20:16:03 +0800413
Jerry Yuda8cdf22021-10-25 15:06:49 +0800414 /* Need to calculate the hash of the transcript first
Jerry Yu0b32c502021-10-28 13:41:59 +0800415 * before reading the message since otherwise it gets
416 * included in the transcript
417 */
Jerry Yuda8cdf22021-10-25 15:06:49 +0800418 ret = mbedtls_ssl_get_handshake_transcript( ssl,
419 ssl->handshake->ciphersuite_info->mac,
420 transcript, sizeof( transcript ),
421 &transcript_len );
422 if( ret != 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800423 {
Jerry Yuda8cdf22021-10-25 15:06:49 +0800424 MBEDTLS_SSL_PEND_FATAL_ALERT(
425 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
426 MBEDTLS_ERR_SSL_INTERNAL_ERROR );
427 return( ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800428 }
429
Jerry Yuda8cdf22021-10-25 15:06:49 +0800430 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash", transcript, transcript_len );
431
432 /* Create verify structure */
433 ssl_tls13_create_verify_structure( transcript,
Jerry Yu0b32c502021-10-28 13:41:59 +0800434 transcript_len,
435 verify_buffer,
436 &verify_buffer_len,
437 ( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) ?
438 MBEDTLS_SSL_IS_SERVER :
439 MBEDTLS_SSL_IS_CLIENT );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800440
441 /* Process the message contents */
Jerry Yu0b32c502021-10-28 13:41:59 +0800442 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_verify( ssl, buf,
443 buf + buf_len, verify_buffer, verify_buffer_len ) );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800444
Xiaofei Bai746f9482021-11-12 08:53:56 +0000445 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
Jerry Yuda8cdf22021-10-25 15:06:49 +0800446 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, buf, buf_len );
Jerry Yu30b071c2021-09-12 20:16:03 +0800447
448cleanup:
449
450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
Jerry Yu5398c102021-11-05 13:32:38 +0800451 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_process_certificate_verify", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800452 return( ret );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800453#else
454 ((void) ssl);
455 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
456 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
457#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800458}
459
460/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000461 *
462 * STATE HANDLING: Incoming Certificate, client-side only currently.
463 *
464 */
465
466/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000467 * Implementation
468 */
469
Xiaofei Bai947571e2021-09-29 09:12:03 +0000470#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
471#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
472/*
473 * Structure of Certificate message:
474 *
475 * enum {
476 * X509(0),
477 * RawPublicKey(2),
478 * (255)
479 * } CertificateType;
480 *
481 * struct {
482 * select (certificate_type) {
483 * case RawPublicKey:
484 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
485 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
486 * case X509:
487 * opaque cert_data<1..2^24-1>;
488 * };
489 * Extension extensions<0..2^16-1>;
490 * } CertificateEntry;
491 *
492 * struct {
493 * opaque certificate_request_context<0..2^8-1>;
494 * CertificateEntry certificate_list<0..2^24-1>;
495 * } Certificate;
496 *
497 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000498
499/* Parse certificate chain send by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000500static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
501 const unsigned char *buf,
502 const unsigned char *end )
503{
504 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
505 size_t certificate_request_context_len = 0;
506 size_t certificate_list_len = 0;
507 const unsigned char *p = buf;
508 const unsigned char *certificate_list_end;
509
510 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
511 certificate_request_context_len = p[0];
Jerry Yub640bf62021-10-29 10:05:32 +0800512 certificate_list_len = MBEDTLS_GET_UINT24_BE( p, 1 );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000513 p += 4;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000514
515 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
516 * support anything beyond 2^16 = 64K.
517 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000518 if( ( certificate_request_context_len != 0 ) ||
519 ( certificate_list_len >= 0x10000 ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000520 {
521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
522 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
523 MBEDTLS_ERR_SSL_DECODE_ERROR );
524 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
525 }
526
527 /* In case we tried to reuse a session but it failed */
528 if( ssl->session_negotiate->peer_cert != NULL )
529 {
530 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
531 mbedtls_free( ssl->session_negotiate->peer_cert );
532 }
533
534 if( ( ssl->session_negotiate->peer_cert =
535 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
536 {
537 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
538 sizeof( mbedtls_x509_crt ) ) );
539 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
540 MBEDTLS_ERR_SSL_ALLOC_FAILED );
541 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
542 }
543
544 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
545
Xiaofei Bai947571e2021-09-29 09:12:03 +0000546 certificate_list_end = p + certificate_list_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000547 while( p < certificate_list_end )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000548 {
549 size_t cert_data_len, extensions_len;
550
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000551 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 3 );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000552 cert_data_len = MBEDTLS_GET_UINT24_BE( p, 0 );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000553 p += 3;
554
555 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
556 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
557 * check that we have a minimum of 128 bytes of data, this is not
558 * clear why we need that though.
559 */
560 if( ( cert_data_len < 128 ) || ( cert_data_len >= 0x10000 ) )
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000561 {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
563 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
564 MBEDTLS_ERR_SSL_DECODE_ERROR );
565 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
566 }
567
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000568 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, cert_data_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000569 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
570 p, cert_data_len );
571
572 switch( ret )
573 {
574 case 0: /*ok*/
575 break;
576 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
577 /* Ignore certificate with an unknown algorithm: maybe a
578 prior certificate was already trusted. */
579 break;
580
581 case MBEDTLS_ERR_X509_ALLOC_FAILED:
582 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
583 MBEDTLS_ERR_X509_ALLOC_FAILED );
584 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
585 return( ret );
586
587 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
588 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
589 MBEDTLS_ERR_X509_UNKNOWN_VERSION );
590 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
591 return( ret );
592
593 default:
594 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
595 ret );
596 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
597 return( ret );
598 }
599
600 p += cert_data_len;
601
602 /* Certificate extensions length */
603 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 2 );
604 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
605 p += 2;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000606 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, extensions_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000607 p += extensions_len;
608 }
609
610 /* Check that all the message is consumed. */
611 if( p != end )
612 {
613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
614 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, \
615 MBEDTLS_ERR_SSL_DECODE_ERROR );
616 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
617 }
618
619 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
620
621 return( ret );
622}
623#else
624static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
625 const unsigned char *buf,
626 const unsigned char *end )
627{
628 ((void) ssl);
629 ((void) buf);
630 ((void) end);
631 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
632}
633#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
634#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
635
636#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
637#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000638/* Validate certificate chain sent by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000639static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
640{
641 int ret = 0;
642 mbedtls_x509_crt *ca_chain;
643 mbedtls_x509_crl *ca_crl;
Xiaofei Baiff456022021-10-28 06:50:17 +0000644 uint32_t verify_result = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000645
646#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
647 if( ssl->handshake->sni_ca_chain != NULL )
648 {
649 ca_chain = ssl->handshake->sni_ca_chain;
650 ca_crl = ssl->handshake->sni_ca_crl;
651 }
652 else
653#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
654 {
655 ca_chain = ssl->conf->ca_chain;
656 ca_crl = ssl->conf->ca_crl;
657 }
658
659 /*
660 * Main check: verify certificate
661 */
662 ret = mbedtls_x509_crt_verify_with_profile(
663 ssl->session_negotiate->peer_cert,
664 ca_chain, ca_crl,
665 ssl->conf->cert_profile,
666 ssl->hostname,
Xiaofei Baiff456022021-10-28 06:50:17 +0000667 &verify_result,
Xiaofei Bai947571e2021-09-29 09:12:03 +0000668 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
669
670 if( ret != 0 )
671 {
672 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
673 }
674
675 /*
676 * Secondary checks: always done, but change 'ret' only if it was 0
677 */
678
679#if defined(MBEDTLS_ECP_C)
680 {
681 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
682
683 /* If certificate uses an EC key, make sure the curve is OK */
684 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
685 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
686 {
Xiaofei Baiff456022021-10-28 06:50:17 +0000687 verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000688
689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( EC key curve )" ) );
690 if( ret == 0 )
691 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
692 }
693 }
694#endif /* MBEDTLS_ECP_C */
695
696 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
697 ssl->handshake->ciphersuite_info,
698 !ssl->conf->endpoint,
Xiaofei Baiff456022021-10-28 06:50:17 +0000699 &verify_result ) != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000700 {
701 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( usage extensions )" ) );
702 if( ret == 0 )
703 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
704 }
705
706
707 if( ca_chain == NULL )
708 {
709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
710 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
711 }
712
713 if( ret != 0 )
714 {
715 /* The certificate may have been rejected for several reasons.
716 Pick one and send the corresponding alert. Which alert to send
717 may be a subject of debate in some cases. */
Xiaofei Baiff456022021-10-28 06:50:17 +0000718 if( verify_result & MBEDTLS_X509_BADCERT_OTHER )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000719 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000720 else if( verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000721 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000722 else if( verify_result & ( MBEDTLS_X509_BADCERT_KEY_USAGE |
723 MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
724 MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
725 MBEDTLS_X509_BADCERT_BAD_PK |
726 MBEDTLS_X509_BADCERT_BAD_KEY ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000727 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000728 else if( verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000729 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000730 else if( verify_result & MBEDTLS_X509_BADCERT_REVOKED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000731 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000732 else if( verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000733 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret );
734 else
735 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret );
736 }
737
738#if defined(MBEDTLS_DEBUG_C)
Xiaofei Baiff456022021-10-28 06:50:17 +0000739 if( verify_result != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000740 {
741 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
Jerry Yu83bb1312021-10-28 22:16:33 +0800742 (unsigned int) verify_result ) );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000743 }
744 else
745 {
746 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
747 }
748#endif /* MBEDTLS_DEBUG_C */
749
Xiaofei Baiff456022021-10-28 06:50:17 +0000750 ssl->session_negotiate->verify_result = verify_result;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000751 return( ret );
752}
753#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
754static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
755{
756 ((void) ssl);
757 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
758}
759#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
760#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
761
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000762int mbedtls_ssl_tls13_process_certificate( mbedtls_ssl_context *ssl )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000763{
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000764 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
766
767#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
768 unsigned char *buf;
769 size_t buf_len;
770
Xiaofei Bai746f9482021-11-12 08:53:56 +0000771 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000772 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
773 &buf, &buf_len ) );
774
775 /* Parse the certificate chain sent by the peer. */
776 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate( ssl, buf, buf + buf_len ) );
777 /* Validate the certificate chain and set the verification results. */
778 MBEDTLS_SSL_PROC_CHK( ssl_tls13_validate_certificate( ssl ) );
779
Xiaofei Bai746f9482021-11-12 08:53:56 +0000780 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
781 buf, buf_len );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000782
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000783cleanup:
784
785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Xiaofei Bai10aeec02021-10-26 09:50:08 +0000786#else
787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
788 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
789#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000790 return( ret );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000791}
792
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000793/*
794 *
XiaokangQianc5c39d52021-11-09 11:55:10 +0000795 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000796 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000797/*
798 * Implementation
799 */
800
XiaokangQianaaa0e192021-11-10 03:07:04 +0000801static int ssl_tls13_preprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000802{
803 int ret;
804
XiaokangQianc5c39d52021-11-09 11:55:10 +0000805 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000806 ssl->handshake->state_local.finished_in.digest,
807 sizeof( ssl->handshake->state_local.finished_in.digest ),
808 &ssl->handshake->state_local.finished_in.digest_len,
XiaokangQianc5c39d52021-11-09 11:55:10 +0000809 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
XiaokangQianc13f9352021-11-11 06:13:22 +0000810 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000811 if( ret != 0 )
812 {
XiaokangQianc5c39d52021-11-09 11:55:10 +0000813 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_calculate_verify_data", ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000814 return( ret );
815 }
816
817 return( 0 );
818}
819
XiaokangQianc5c39d52021-11-09 11:55:10 +0000820static int ssl_tls13_parse_finished_message( mbedtls_ssl_context *ssl,
821 const unsigned char *buf,
822 const unsigned char *end )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000823{
XiaokangQian33062842021-11-11 03:37:45 +0000824 /*
825 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +0000826 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +0000827 * } Finished;
828 */
829 const unsigned char *expected_verify_data =
830 ssl->handshake->state_local.finished_in.digest;
831 size_t expected_verify_data_len =
832 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000833 /* Structural validation */
XiaokangQian33062842021-11-11 03:37:45 +0000834 if( (size_t)( end - buf ) != expected_verify_data_len )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000835 {
836 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
837
838 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +0000839 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000840 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
841 }
842
XiaokangQianc5c39d52021-11-09 11:55:10 +0000843 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (self-computed):",
XiaokangQian33062842021-11-11 03:37:45 +0000844 expected_verify_data,
845 expected_verify_data_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +0000846 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (received message):", buf,
XiaokangQian33062842021-11-11 03:37:45 +0000847 expected_verify_data_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000848
849 /* Semantic validation */
Gabor Mezei685472b2021-11-24 11:17:36 +0100850 if( mbedtls_ct_memcmp( buf,
851 expected_verify_data,
852 expected_verify_data_len ) != 0 )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000853 {
854 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
855
XiaokangQian33062842021-11-11 03:37:45 +0000856 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +0000857 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000858 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
859 }
860 return( 0 );
861}
862
XiaokangQianc13f9352021-11-11 06:13:22 +0000863#if defined(MBEDTLS_SSL_CLI_C)
XiaokangQianaaa0e192021-11-10 03:07:04 +0000864static int ssl_tls13_postprocess_server_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000865{
XiaokangQian33062842021-11-11 03:37:45 +0000866 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000867 mbedtls_ssl_key_set traffic_keys;
XiaokangQian1aef02e2021-10-28 09:54:34 +0000868 mbedtls_ssl_transform *transform_application = NULL;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000869
XiaokangQian4cab0242021-10-12 08:43:37 +0000870 ret = mbedtls_ssl_tls13_key_schedule_stage_application( ssl );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000871 if( ret != 0 )
872 {
873 MBEDTLS_SSL_DEBUG_RET( 1,
XiaokangQian4cab0242021-10-12 08:43:37 +0000874 "mbedtls_ssl_tls13_key_schedule_stage_application", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000875 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000876 }
877
XiaokangQian33062842021-11-11 03:37:45 +0000878 ret = mbedtls_ssl_tls13_generate_application_keys( ssl, &traffic_keys );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000879 if( ret != 0 )
880 {
881 MBEDTLS_SSL_DEBUG_RET( 1,
XiaokangQiand0aa3e92021-11-10 06:17:40 +0000882 "mbedtls_ssl_tls13_generate_application_keys", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000883 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000884 }
885
886 transform_application =
887 mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
888 if( transform_application == NULL )
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000889 {
890 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
891 goto cleanup;
892 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000893
894 ret = mbedtls_ssl_tls13_populate_transform(
895 transform_application,
896 ssl->conf->endpoint,
897 ssl->session_negotiate->ciphersuite,
898 &traffic_keys,
899 ssl );
900 if( ret != 0 )
901 {
902 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000903 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000904 }
905
906 ssl->transform_application = transform_application;
907
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000908cleanup:
909
XiaokangQian33062842021-11-11 03:37:45 +0000910 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +0000911 if( ret != 0 )
XiaokangQian1aef02e2021-10-28 09:54:34 +0000912 {
913 mbedtls_free( transform_application );
914 MBEDTLS_SSL_PEND_FATAL_ALERT(
915 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
916 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
917 }
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000918 return( ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000919}
XiaokangQianc13f9352021-11-11 06:13:22 +0000920#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000921
XiaokangQiancc90c942021-11-09 12:30:09 +0000922static int ssl_tls13_postprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000923{
924
XiaokangQianc13f9352021-11-11 06:13:22 +0000925#if defined(MBEDTLS_SSL_CLI_C)
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000926 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
927 {
XiaokangQianaaa0e192021-11-10 03:07:04 +0000928 return( ssl_tls13_postprocess_server_finished_message( ssl ) );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000929 }
XiaokangQianc13f9352021-11-11 06:13:22 +0000930#else
931 ((void) ssl);
932#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000933
934 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
935}
936
XiaokangQianc5c39d52021-11-09 11:55:10 +0000937int mbedtls_ssl_tls13_process_finished_message( mbedtls_ssl_context *ssl )
938{
XiaokangQian33062842021-11-11 03:37:45 +0000939 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +0000940 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000941 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +0000942
XiaokangQiand0aa3e92021-11-10 06:17:40 +0000943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +0000944
945 /* Preprocessing step: Compute handshake digest */
XiaokangQianaaa0e192021-11-10 03:07:04 +0000946 MBEDTLS_SSL_PROC_CHK( ssl_tls13_preprocess_finished_message( ssl ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +0000947
Xiaofei Bai746f9482021-11-12 08:53:56 +0000948 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianc5c39d52021-11-09 11:55:10 +0000949 MBEDTLS_SSL_HS_FINISHED,
Xiaofei Baieef15042021-11-18 07:29:56 +0000950 &buf, &buf_len ) );
951 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_finished_message( ssl, buf, buf + buf_len ) );
Xiaofei Bai746f9482021-11-12 08:53:56 +0000952 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
Xiaofei Baieef15042021-11-18 07:29:56 +0000953 ssl, MBEDTLS_SSL_HS_FINISHED, buf, buf_len );
XiaokangQianaaa0e192021-11-10 03:07:04 +0000954 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_finished_message( ssl ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +0000955
956cleanup:
957
XiaokangQiand0aa3e92021-11-10 06:17:40 +0000958 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +0000959 return( ret );
960}
961
XiaokangQian74af2a82021-09-22 07:40:30 +0000962/*
963 *
XiaokangQiancc90c942021-11-09 12:30:09 +0000964 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +0000965 *
966 */
XiaokangQian74af2a82021-09-22 07:40:30 +0000967/*
XiaokangQian35dc6252021-11-11 08:16:19 +0000968 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +0000969 */
970
XiaokangQian8773aa02021-11-10 07:33:09 +0000971static int ssl_tls13_prepare_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +0000972{
973 int ret;
974
975 /* Compute transcript of handshake up to now. */
XiaokangQiancc90c942021-11-09 12:30:09 +0000976 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQian74af2a82021-09-22 07:40:30 +0000977 ssl->handshake->state_local.finished_out.digest,
978 sizeof( ssl->handshake->state_local.finished_out.digest ),
979 &ssl->handshake->state_local.finished_out.digest_len,
980 ssl->conf->endpoint );
981
982 if( ret != 0 )
983 {
Jerry Yu7ca30542021-12-08 15:57:57 +0800984 MBEDTLS_SSL_DEBUG_RET( 1, "calculate_verify_data failed", ret );
XiaokangQian74af2a82021-09-22 07:40:30 +0000985 return( ret );
986 }
987
988 return( 0 );
989}
990
XiaokangQiancc90c942021-11-09 12:30:09 +0000991static int ssl_tls13_finalize_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +0000992{
XiaokangQian0fa66432021-11-15 03:33:57 +0000993 // TODO: Add back resumption keys calculation after MVP.
994 ((void) ssl);
XiaokangQian74af2a82021-09-22 07:40:30 +0000995
996 return( 0 );
997}
998
XiaokangQian8773aa02021-11-10 07:33:09 +0000999static int ssl_tls13_write_finished_message_body( mbedtls_ssl_context *ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001000 unsigned char *buf,
1001 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001002 size_t *out_len )
XiaokangQian74af2a82021-09-22 07:40:30 +00001003{
XiaokangQian8773aa02021-11-10 07:33:09 +00001004 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001005 /*
1006 * struct {
1007 * opaque verify_data[Hash.length];
1008 * } Finished;
1009 */
XiaokangQian8773aa02021-11-10 07:33:09 +00001010 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001011
1012 memcpy( buf, ssl->handshake->state_local.finished_out.digest,
XiaokangQian8773aa02021-11-10 07:33:09 +00001013 verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001014
Xiaofei Baid25fab62021-12-02 06:36:27 +00001015 *out_len = verify_data_len;
XiaokangQian74af2a82021-09-22 07:40:30 +00001016 return( 0 );
1017}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001018
XiaokangQian35dc6252021-11-11 08:16:19 +00001019/* Main entry point: orchestrates the other functions */
1020int mbedtls_ssl_tls13_write_finished_message( mbedtls_ssl_context *ssl )
1021{
1022 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1023 unsigned char *buf;
1024 size_t buf_len, msg_len;
1025
1026 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished message" ) );
1027
XiaokangQiandce82242021-11-15 06:01:26 +00001028 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_finished_message( ssl ) );
1029
XiaokangQian35dc6252021-11-11 08:16:19 +00001030 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg( ssl,
1031 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len ) );
1032
1033 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_finished_message_body(
1034 ssl, buf, buf + buf_len, &msg_len ) );
1035
Xiaofei Bai746f9482021-11-12 08:53:56 +00001036 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1037 buf, msg_len );
XiaokangQian35dc6252021-11-11 08:16:19 +00001038
1039 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_finished_message( ssl ) );
1040 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
1041 buf_len, msg_len ) );
1042 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_flush_output( ssl ) );
1043
1044cleanup:
1045
1046 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished message" ) );
1047 return( ret );
1048}
1049
Jerry Yu378254d2021-10-30 21:44:47 +08001050void mbedtls_ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
1051{
1052
1053 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
1054
1055 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001056 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001057 */
1058 if( ssl->session )
1059 {
Jerry Yu378254d2021-10-30 21:44:47 +08001060 mbedtls_ssl_session_free( ssl->session );
1061 mbedtls_free( ssl->session );
1062 }
1063 ssl->session = ssl->session_negotiate;
1064 ssl->session_negotiate = NULL;
1065
1066 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
1067}
1068
Ronald Cron49ad6192021-11-24 16:25:31 +01001069/*
1070 *
1071 * STATE HANDLING: Write ChangeCipherSpec
1072 *
1073 */
1074#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1075
1076static int ssl_tls13_write_change_cipher_spec_body( mbedtls_ssl_context *ssl,
1077 unsigned char *buf,
1078 unsigned char *end,
1079 size_t *olen )
1080{
1081 ((void) ssl);
1082
1083 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 1 );
1084 buf[0] = 1;
1085 *olen = 1;
1086
1087 return( 0 );
1088}
1089
Ronald Cron49ad6192021-11-24 16:25:31 +01001090int mbedtls_ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1091{
1092 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1093
1094 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
1095
1096 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_flush_output( ssl ) );
1097
1098 /* Write CCS message */
1099 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_change_cipher_spec_body(
1100 ssl, ssl->out_msg,
1101 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1102 &ssl->out_msglen ) );
1103
1104 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1105
Ronald Cron49ad6192021-11-24 16:25:31 +01001106 /* Dispatch message */
1107 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_record( ssl, 1 ) );
1108
1109cleanup:
1110
1111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
1112 return( ret );
1113}
1114
1115#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1116
Ronald Cron6f135e12021-12-08 16:57:54 +01001117#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001118
1119#endif /* MBEDTLS_SSL_TLS_C */