blob: 1d31ce9218a7c3b0a9fc41cd70738aa3bb56c1ca [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 Yu08e2cea2021-12-22 10:53:23 +0800215 for( const uint16_t *sig_alg = mbedtls_ssl_conf_get_sig_algs( ssl->conf );
216 *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
Jerry Yu982d9e52021-10-14 15:59:37 +0800217 {
Jerry Yu2d0bd322022-01-12 12:58:00 +0800218 if( *sig_alg == proposed_sig_alg )
Jerry Yud0fc5852021-10-29 11:09:06 +0800219 return( 1 );
Jerry Yu982d9e52021-10-14 15:59:37 +0800220 }
Jerry Yud0fc5852021-10-29 11:09:06 +0800221 return( 0 );
Jerry Yu982d9e52021-10-14 15:59:37 +0800222}
223
Jerry Yu0b32c502021-10-28 13:41:59 +0800224static int ssl_tls13_parse_certificate_verify( mbedtls_ssl_context *ssl,
Jerry Yud0fc5852021-10-29 11:09:06 +0800225 const unsigned char *buf,
226 const unsigned char *end,
227 const unsigned char *verify_buffer,
228 size_t verify_buffer_len )
Jerry Yu30b071c2021-09-12 20:16:03 +0800229{
230 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
231 const unsigned char *p = buf;
232 uint16_t algorithm;
Jerry Yu30b071c2021-09-12 20:16:03 +0800233 size_t signature_len;
234 mbedtls_pk_type_t sig_alg;
235 mbedtls_md_type_t md_alg;
Jerry Yud0fc5852021-10-29 11:09:06 +0800236 unsigned char verify_hash[MBEDTLS_MD_MAX_SIZE];
Jerry Yu30b071c2021-09-12 20:16:03 +0800237 size_t verify_hash_len;
238
Xiaofei Baid25fab62021-12-02 06:36:27 +0000239 void const *options = NULL;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000240#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Xiaofei Baid25fab62021-12-02 06:36:27 +0000241 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000242#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
243
Jerry Yu30b071c2021-09-12 20:16:03 +0800244 /*
245 * struct {
246 * SignatureScheme algorithm;
247 * opaque signature<0..2^16-1>;
248 * } CertificateVerify;
249 */
250 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
251 algorithm = MBEDTLS_GET_UINT16_BE( p, 0 );
252 p += 2;
253
254 /* RFC 8446 section 4.4.3
255 *
256 * If the CertificateVerify message is sent by a server, the signature algorithm
257 * MUST be one offered in the client's "signature_algorithms" extension unless
258 * no valid certificate chain can be produced without unsupported algorithms
259 *
260 * RFC 8446 section 4.4.2.2
261 *
262 * If the client cannot construct an acceptable chain using the provided
263 * certificates and decides to abort the handshake, then it MUST abort the handshake
264 * with an appropriate certificate-related alert (by default, "unsupported_certificate").
265 *
Jerry Yu6f87f252021-10-29 20:12:51 +0800266 * Check if algorithm is an offered signature algorithm.
Jerry Yu30b071c2021-09-12 20:16:03 +0800267 */
Jerry Yu0b32c502021-10-28 13:41:59 +0800268 if( ! ssl_tls13_sig_alg_is_offered( ssl, algorithm ) )
Jerry Yu30b071c2021-09-12 20:16:03 +0800269 {
Jerry Yu982d9e52021-10-14 15:59:37 +0800270 /* algorithm not in offered signature algorithms list */
271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Received signature algorithm(%04x) is not "
272 "offered.",
273 ( unsigned int ) algorithm ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800274 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800275 }
276
277 /* We currently only support ECDSA-based signatures */
278 switch( algorithm )
279 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000280 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Jerry Yu30b071c2021-09-12 20:16:03 +0800281 md_alg = MBEDTLS_MD_SHA256;
282 sig_alg = MBEDTLS_PK_ECDSA;
283 break;
Xiaofei Bai746f9482021-11-12 08:53:56 +0000284 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Jerry Yu30b071c2021-09-12 20:16:03 +0800285 md_alg = MBEDTLS_MD_SHA384;
286 sig_alg = MBEDTLS_PK_ECDSA;
287 break;
Xiaofei Bai746f9482021-11-12 08:53:56 +0000288 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Jerry Yu30b071c2021-09-12 20:16:03 +0800289 md_alg = MBEDTLS_MD_SHA512;
290 sig_alg = MBEDTLS_PK_ECDSA;
291 break;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000292#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Xiaofei Bai6dc90da2021-11-26 08:11:40 +0000293 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
XiaokangQiana83014d2021-11-22 07:53:34 +0000294 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Certificate Verify: using RSA PSS" ) );
XiaokangQian82d34cc2021-11-03 08:51:56 +0000295 md_alg = MBEDTLS_MD_SHA256;
296 sig_alg = MBEDTLS_PK_RSASSA_PSS;
297 break;
298#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800299 default:
300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Certificate Verify: Unknown signature algorithm." ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800301 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800302 }
303
304 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate Verify: Signature algorithm ( %04x )",
305 ( unsigned int ) algorithm ) );
306
307 /*
308 * Check the certificate's key type matches the signature alg
309 */
310 if( !mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk, sig_alg ) )
311 {
312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "signature algorithm doesn't match cert key" ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800313 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800314 }
315
316 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
317 signature_len = MBEDTLS_GET_UINT16_BE( p, 0 );
318 p += 2;
319 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, signature_len );
320
321 /* Hash verify buffer with indicated hash function */
322 switch( md_alg )
323 {
324#if defined(MBEDTLS_SHA256_C)
325 case MBEDTLS_MD_SHA256:
326 verify_hash_len = 32;
Jerry Yu133690c2021-10-25 14:01:13 +0800327 ret = mbedtls_sha256( verify_buffer, verify_buffer_len, verify_hash, 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800328 break;
329#endif /* MBEDTLS_SHA256_C */
330
331#if defined(MBEDTLS_SHA384_C)
332 case MBEDTLS_MD_SHA384:
333 verify_hash_len = 48;
Jerry Yu133690c2021-10-25 14:01:13 +0800334 ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 1 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800335 break;
336#endif /* MBEDTLS_SHA384_C */
337
338#if defined(MBEDTLS_SHA512_C)
339 case MBEDTLS_MD_SHA512:
340 verify_hash_len = 64;
Jerry Yu133690c2021-10-25 14:01:13 +0800341 ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800342 break;
343#endif /* MBEDTLS_SHA512_C */
344
Jerry Yu0b32c502021-10-28 13:41:59 +0800345 default:
346 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
347 break;
Jerry Yu30b071c2021-09-12 20:16:03 +0800348 }
349
Jerry Yu133690c2021-10-25 14:01:13 +0800350 if( ret != 0 )
351 {
352 MBEDTLS_SSL_DEBUG_RET( 1, "hash computation error", ret );
Jerry Yu6f87f252021-10-29 20:12:51 +0800353 goto error;
Jerry Yu133690c2021-10-25 14:01:13 +0800354 }
355
Jerry Yu30b071c2021-09-12 20:16:03 +0800356 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
XiaokangQian82d34cc2021-11-03 08:51:56 +0000357#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
358 if( sig_alg == MBEDTLS_PK_RSASSA_PSS )
359 {
360 const mbedtls_md_info_t* md_info;
Xiaofei Baid25fab62021-12-02 06:36:27 +0000361 rsassa_pss_options.mgf1_hash_id = md_alg;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000362 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
363 {
364 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
365 }
Xiaofei Baid25fab62021-12-02 06:36:27 +0000366 rsassa_pss_options.expected_salt_len = mbedtls_md_get_size( md_info );
367 options = (const void*) &rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000368 }
369#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800370
Xiaofei Baid25fab62021-12-02 06:36:27 +0000371 if( ( ret = mbedtls_pk_verify_ext( sig_alg, options,
Jerry Yu30b071c2021-09-12 20:16:03 +0800372 &ssl->session_negotiate->peer_cert->pk,
373 md_alg, verify_hash, verify_hash_len,
Jerry Yu6f87f252021-10-29 20:12:51 +0800374 p, signature_len ) ) == 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800375 {
Jerry Yu6f87f252021-10-29 20:12:51 +0800376 return( 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800377 }
Jerry Yu6f87f252021-10-29 20:12:51 +0800378 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify_ext", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800379
Jerry Yu6f87f252021-10-29 20:12:51 +0800380error:
381 /* RFC 8446 section 4.4.3
382 *
383 * If the verification fails, the receiver MUST terminate the handshake
384 * with a "decrypt_error" alert.
385 */
386 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
387 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
388 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
389
Jerry Yu30b071c2021-09-12 20:16:03 +0800390}
391#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
392
393int mbedtls_ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
394{
Jerry Yu30b071c2021-09-12 20:16:03 +0800395
Jerry Yuda8cdf22021-10-25 15:06:49 +0800396#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
397 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
398 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
399 size_t verify_buffer_len;
400 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
401 size_t transcript_len;
402 unsigned char *buf;
403 size_t buf_len;
404
Jerry Yu30b071c2021-09-12 20:16:03 +0800405 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
406
Jerry Yuda8cdf22021-10-25 15:06:49 +0800407 MBEDTLS_SSL_PROC_CHK(
Xiaofei Bai746f9482021-11-12 08:53:56 +0000408 mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
Jerry Yuda8cdf22021-10-25 15:06:49 +0800409 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu30b071c2021-09-12 20:16:03 +0800410
Jerry Yuda8cdf22021-10-25 15:06:49 +0800411 /* Need to calculate the hash of the transcript first
Jerry Yu0b32c502021-10-28 13:41:59 +0800412 * before reading the message since otherwise it gets
413 * included in the transcript
414 */
Jerry Yuda8cdf22021-10-25 15:06:49 +0800415 ret = mbedtls_ssl_get_handshake_transcript( ssl,
416 ssl->handshake->ciphersuite_info->mac,
417 transcript, sizeof( transcript ),
418 &transcript_len );
419 if( ret != 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800420 {
Jerry Yuda8cdf22021-10-25 15:06:49 +0800421 MBEDTLS_SSL_PEND_FATAL_ALERT(
422 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
423 MBEDTLS_ERR_SSL_INTERNAL_ERROR );
424 return( ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800425 }
426
Jerry Yuda8cdf22021-10-25 15:06:49 +0800427 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash", transcript, transcript_len );
428
429 /* Create verify structure */
430 ssl_tls13_create_verify_structure( transcript,
Jerry Yu0b32c502021-10-28 13:41:59 +0800431 transcript_len,
432 verify_buffer,
433 &verify_buffer_len,
434 ( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) ?
435 MBEDTLS_SSL_IS_SERVER :
436 MBEDTLS_SSL_IS_CLIENT );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800437
438 /* Process the message contents */
Jerry Yu0b32c502021-10-28 13:41:59 +0800439 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_verify( ssl, buf,
440 buf + buf_len, verify_buffer, verify_buffer_len ) );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800441
Xiaofei Bai746f9482021-11-12 08:53:56 +0000442 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
Jerry Yuda8cdf22021-10-25 15:06:49 +0800443 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, buf, buf_len );
Jerry Yu30b071c2021-09-12 20:16:03 +0800444
445cleanup:
446
447 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
Jerry Yu5398c102021-11-05 13:32:38 +0800448 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_process_certificate_verify", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800449 return( ret );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800450#else
451 ((void) ssl);
452 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
453 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
454#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800455}
456
457/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000458 *
459 * STATE HANDLING: Incoming Certificate, client-side only currently.
460 *
461 */
462
463/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000464 * Implementation
465 */
466
Xiaofei Bai947571e2021-09-29 09:12:03 +0000467#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
468#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
469/*
470 * Structure of Certificate message:
471 *
472 * enum {
473 * X509(0),
474 * RawPublicKey(2),
475 * (255)
476 * } CertificateType;
477 *
478 * struct {
479 * select (certificate_type) {
480 * case RawPublicKey:
481 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
482 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
483 * case X509:
484 * opaque cert_data<1..2^24-1>;
485 * };
486 * Extension extensions<0..2^16-1>;
487 * } CertificateEntry;
488 *
489 * struct {
490 * opaque certificate_request_context<0..2^8-1>;
491 * CertificateEntry certificate_list<0..2^24-1>;
492 * } Certificate;
493 *
494 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000495
496/* Parse certificate chain send by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000497static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
498 const unsigned char *buf,
499 const unsigned char *end )
500{
501 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
502 size_t certificate_request_context_len = 0;
503 size_t certificate_list_len = 0;
504 const unsigned char *p = buf;
505 const unsigned char *certificate_list_end;
506
507 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
508 certificate_request_context_len = p[0];
Jerry Yub640bf62021-10-29 10:05:32 +0800509 certificate_list_len = MBEDTLS_GET_UINT24_BE( p, 1 );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000510 p += 4;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000511
512 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
513 * support anything beyond 2^16 = 64K.
514 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000515 if( ( certificate_request_context_len != 0 ) ||
516 ( certificate_list_len >= 0x10000 ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000517 {
518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
519 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
520 MBEDTLS_ERR_SSL_DECODE_ERROR );
521 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
522 }
523
524 /* In case we tried to reuse a session but it failed */
525 if( ssl->session_negotiate->peer_cert != NULL )
526 {
527 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
528 mbedtls_free( ssl->session_negotiate->peer_cert );
529 }
530
531 if( ( ssl->session_negotiate->peer_cert =
532 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
533 {
534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
535 sizeof( mbedtls_x509_crt ) ) );
536 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
537 MBEDTLS_ERR_SSL_ALLOC_FAILED );
538 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
539 }
540
541 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
542
Xiaofei Bai947571e2021-09-29 09:12:03 +0000543 certificate_list_end = p + certificate_list_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000544 while( p < certificate_list_end )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000545 {
546 size_t cert_data_len, extensions_len;
547
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000548 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 3 );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000549 cert_data_len = MBEDTLS_GET_UINT24_BE( p, 0 );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000550 p += 3;
551
552 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
553 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
554 * check that we have a minimum of 128 bytes of data, this is not
555 * clear why we need that though.
556 */
557 if( ( cert_data_len < 128 ) || ( cert_data_len >= 0x10000 ) )
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000558 {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000559 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
560 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
561 MBEDTLS_ERR_SSL_DECODE_ERROR );
562 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
563 }
564
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000565 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, cert_data_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000566 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
567 p, cert_data_len );
568
569 switch( ret )
570 {
571 case 0: /*ok*/
572 break;
573 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
574 /* Ignore certificate with an unknown algorithm: maybe a
575 prior certificate was already trusted. */
576 break;
577
578 case MBEDTLS_ERR_X509_ALLOC_FAILED:
579 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
580 MBEDTLS_ERR_X509_ALLOC_FAILED );
581 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
582 return( ret );
583
584 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
585 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
586 MBEDTLS_ERR_X509_UNKNOWN_VERSION );
587 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
588 return( ret );
589
590 default:
591 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
592 ret );
593 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
594 return( ret );
595 }
596
597 p += cert_data_len;
598
599 /* Certificate extensions length */
600 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 2 );
601 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
602 p += 2;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000603 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, extensions_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000604 p += extensions_len;
605 }
606
607 /* Check that all the message is consumed. */
608 if( p != end )
609 {
610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
611 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, \
612 MBEDTLS_ERR_SSL_DECODE_ERROR );
613 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
614 }
615
616 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
617
618 return( ret );
619}
620#else
621static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
622 const unsigned char *buf,
623 const unsigned char *end )
624{
625 ((void) ssl);
626 ((void) buf);
627 ((void) end);
628 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
629}
630#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
631#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
632
633#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
634#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000635/* Validate certificate chain sent by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000636static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
637{
638 int ret = 0;
639 mbedtls_x509_crt *ca_chain;
640 mbedtls_x509_crl *ca_crl;
Xiaofei Baiff456022021-10-28 06:50:17 +0000641 uint32_t verify_result = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000642
643#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
644 if( ssl->handshake->sni_ca_chain != NULL )
645 {
646 ca_chain = ssl->handshake->sni_ca_chain;
647 ca_crl = ssl->handshake->sni_ca_crl;
648 }
649 else
650#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
651 {
652 ca_chain = ssl->conf->ca_chain;
653 ca_crl = ssl->conf->ca_crl;
654 }
655
656 /*
657 * Main check: verify certificate
658 */
659 ret = mbedtls_x509_crt_verify_with_profile(
660 ssl->session_negotiate->peer_cert,
661 ca_chain, ca_crl,
662 ssl->conf->cert_profile,
663 ssl->hostname,
Xiaofei Baiff456022021-10-28 06:50:17 +0000664 &verify_result,
Xiaofei Bai947571e2021-09-29 09:12:03 +0000665 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
666
667 if( ret != 0 )
668 {
669 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
670 }
671
672 /*
673 * Secondary checks: always done, but change 'ret' only if it was 0
674 */
675
676#if defined(MBEDTLS_ECP_C)
677 {
678 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
679
680 /* If certificate uses an EC key, make sure the curve is OK */
681 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
682 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
683 {
Xiaofei Baiff456022021-10-28 06:50:17 +0000684 verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000685
686 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( EC key curve )" ) );
687 if( ret == 0 )
688 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
689 }
690 }
691#endif /* MBEDTLS_ECP_C */
692
693 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
694 ssl->handshake->ciphersuite_info,
695 !ssl->conf->endpoint,
Xiaofei Baiff456022021-10-28 06:50:17 +0000696 &verify_result ) != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000697 {
698 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( usage extensions )" ) );
699 if( ret == 0 )
700 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
701 }
702
703
704 if( ca_chain == NULL )
705 {
706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
707 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
708 }
709
710 if( ret != 0 )
711 {
712 /* The certificate may have been rejected for several reasons.
713 Pick one and send the corresponding alert. Which alert to send
714 may be a subject of debate in some cases. */
Xiaofei Baiff456022021-10-28 06:50:17 +0000715 if( verify_result & MBEDTLS_X509_BADCERT_OTHER )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000716 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000717 else if( verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000718 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000719 else if( verify_result & ( MBEDTLS_X509_BADCERT_KEY_USAGE |
720 MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
721 MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
722 MBEDTLS_X509_BADCERT_BAD_PK |
723 MBEDTLS_X509_BADCERT_BAD_KEY ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000724 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000725 else if( verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000726 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000727 else if( verify_result & MBEDTLS_X509_BADCERT_REVOKED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000728 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000729 else if( verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000730 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret );
731 else
732 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret );
733 }
734
735#if defined(MBEDTLS_DEBUG_C)
Xiaofei Baiff456022021-10-28 06:50:17 +0000736 if( verify_result != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000737 {
738 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
Jerry Yu83bb1312021-10-28 22:16:33 +0800739 (unsigned int) verify_result ) );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000740 }
741 else
742 {
743 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
744 }
745#endif /* MBEDTLS_DEBUG_C */
746
Xiaofei Baiff456022021-10-28 06:50:17 +0000747 ssl->session_negotiate->verify_result = verify_result;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000748 return( ret );
749}
750#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
751static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
752{
753 ((void) ssl);
754 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
755}
756#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
757#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
758
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000759int mbedtls_ssl_tls13_process_certificate( mbedtls_ssl_context *ssl )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000760{
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000761 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
762 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
763
764#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
765 unsigned char *buf;
766 size_t buf_len;
767
Xiaofei Bai746f9482021-11-12 08:53:56 +0000768 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000769 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
770 &buf, &buf_len ) );
771
772 /* Parse the certificate chain sent by the peer. */
773 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate( ssl, buf, buf + buf_len ) );
774 /* Validate the certificate chain and set the verification results. */
775 MBEDTLS_SSL_PROC_CHK( ssl_tls13_validate_certificate( ssl ) );
776
Xiaofei Bai746f9482021-11-12 08:53:56 +0000777 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
778 buf, buf_len );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000779
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000780cleanup:
781
782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Xiaofei Bai10aeec02021-10-26 09:50:08 +0000783#else
784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
785 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
786#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000787 return( ret );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000788}
789
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000790/*
791 *
XiaokangQianc5c39d52021-11-09 11:55:10 +0000792 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000793 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000794/*
795 * Implementation
796 */
797
XiaokangQianaaa0e192021-11-10 03:07:04 +0000798static int ssl_tls13_preprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000799{
800 int ret;
801
XiaokangQianc5c39d52021-11-09 11:55:10 +0000802 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000803 ssl->handshake->state_local.finished_in.digest,
804 sizeof( ssl->handshake->state_local.finished_in.digest ),
805 &ssl->handshake->state_local.finished_in.digest_len,
XiaokangQianc5c39d52021-11-09 11:55:10 +0000806 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
XiaokangQianc13f9352021-11-11 06:13:22 +0000807 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000808 if( ret != 0 )
809 {
XiaokangQianc5c39d52021-11-09 11:55:10 +0000810 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_calculate_verify_data", ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000811 return( ret );
812 }
813
814 return( 0 );
815}
816
XiaokangQianc5c39d52021-11-09 11:55:10 +0000817static int ssl_tls13_parse_finished_message( mbedtls_ssl_context *ssl,
818 const unsigned char *buf,
819 const unsigned char *end )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000820{
XiaokangQian33062842021-11-11 03:37:45 +0000821 /*
822 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +0000823 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +0000824 * } Finished;
825 */
826 const unsigned char *expected_verify_data =
827 ssl->handshake->state_local.finished_in.digest;
828 size_t expected_verify_data_len =
829 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000830 /* Structural validation */
XiaokangQian33062842021-11-11 03:37:45 +0000831 if( (size_t)( end - buf ) != expected_verify_data_len )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000832 {
833 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
834
835 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +0000836 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000837 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
838 }
839
XiaokangQianc5c39d52021-11-09 11:55:10 +0000840 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (self-computed):",
XiaokangQian33062842021-11-11 03:37:45 +0000841 expected_verify_data,
842 expected_verify_data_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +0000843 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (received message):", buf,
XiaokangQian33062842021-11-11 03:37:45 +0000844 expected_verify_data_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000845
846 /* Semantic validation */
Gabor Mezei685472b2021-11-24 11:17:36 +0100847 if( mbedtls_ct_memcmp( buf,
848 expected_verify_data,
849 expected_verify_data_len ) != 0 )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000850 {
851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
852
XiaokangQian33062842021-11-11 03:37:45 +0000853 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +0000854 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000855 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
856 }
857 return( 0 );
858}
859
XiaokangQianc13f9352021-11-11 06:13:22 +0000860#if defined(MBEDTLS_SSL_CLI_C)
XiaokangQianaaa0e192021-11-10 03:07:04 +0000861static int ssl_tls13_postprocess_server_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000862{
XiaokangQian33062842021-11-11 03:37:45 +0000863 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000864 mbedtls_ssl_key_set traffic_keys;
XiaokangQian1aef02e2021-10-28 09:54:34 +0000865 mbedtls_ssl_transform *transform_application = NULL;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000866
XiaokangQian4cab0242021-10-12 08:43:37 +0000867 ret = mbedtls_ssl_tls13_key_schedule_stage_application( ssl );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000868 if( ret != 0 )
869 {
870 MBEDTLS_SSL_DEBUG_RET( 1,
XiaokangQian4cab0242021-10-12 08:43:37 +0000871 "mbedtls_ssl_tls13_key_schedule_stage_application", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000872 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000873 }
874
XiaokangQian33062842021-11-11 03:37:45 +0000875 ret = mbedtls_ssl_tls13_generate_application_keys( ssl, &traffic_keys );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000876 if( ret != 0 )
877 {
878 MBEDTLS_SSL_DEBUG_RET( 1,
XiaokangQiand0aa3e92021-11-10 06:17:40 +0000879 "mbedtls_ssl_tls13_generate_application_keys", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000880 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000881 }
882
883 transform_application =
884 mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
885 if( transform_application == NULL )
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000886 {
887 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
888 goto cleanup;
889 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000890
891 ret = mbedtls_ssl_tls13_populate_transform(
892 transform_application,
893 ssl->conf->endpoint,
894 ssl->session_negotiate->ciphersuite,
895 &traffic_keys,
896 ssl );
897 if( ret != 0 )
898 {
899 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000900 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000901 }
902
903 ssl->transform_application = transform_application;
904
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000905cleanup:
906
XiaokangQian33062842021-11-11 03:37:45 +0000907 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +0000908 if( ret != 0 )
XiaokangQian1aef02e2021-10-28 09:54:34 +0000909 {
910 mbedtls_free( transform_application );
911 MBEDTLS_SSL_PEND_FATAL_ALERT(
912 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
913 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
914 }
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000915 return( ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000916}
XiaokangQianc13f9352021-11-11 06:13:22 +0000917#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000918
XiaokangQiancc90c942021-11-09 12:30:09 +0000919static int ssl_tls13_postprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000920{
921
XiaokangQianc13f9352021-11-11 06:13:22 +0000922#if defined(MBEDTLS_SSL_CLI_C)
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000923 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
924 {
XiaokangQianaaa0e192021-11-10 03:07:04 +0000925 return( ssl_tls13_postprocess_server_finished_message( ssl ) );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000926 }
XiaokangQianc13f9352021-11-11 06:13:22 +0000927#else
928 ((void) ssl);
929#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000930
931 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
932}
933
XiaokangQianc5c39d52021-11-09 11:55:10 +0000934int mbedtls_ssl_tls13_process_finished_message( mbedtls_ssl_context *ssl )
935{
XiaokangQian33062842021-11-11 03:37:45 +0000936 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +0000937 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000938 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +0000939
XiaokangQiand0aa3e92021-11-10 06:17:40 +0000940 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +0000941
942 /* Preprocessing step: Compute handshake digest */
XiaokangQianaaa0e192021-11-10 03:07:04 +0000943 MBEDTLS_SSL_PROC_CHK( ssl_tls13_preprocess_finished_message( ssl ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +0000944
Xiaofei Bai746f9482021-11-12 08:53:56 +0000945 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianc5c39d52021-11-09 11:55:10 +0000946 MBEDTLS_SSL_HS_FINISHED,
Xiaofei Baieef15042021-11-18 07:29:56 +0000947 &buf, &buf_len ) );
948 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_finished_message( ssl, buf, buf + buf_len ) );
Xiaofei Bai746f9482021-11-12 08:53:56 +0000949 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
Xiaofei Baieef15042021-11-18 07:29:56 +0000950 ssl, MBEDTLS_SSL_HS_FINISHED, buf, buf_len );
XiaokangQianaaa0e192021-11-10 03:07:04 +0000951 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_finished_message( ssl ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +0000952
953cleanup:
954
XiaokangQiand0aa3e92021-11-10 06:17:40 +0000955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +0000956 return( ret );
957}
958
XiaokangQian74af2a82021-09-22 07:40:30 +0000959/*
960 *
XiaokangQiancc90c942021-11-09 12:30:09 +0000961 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +0000962 *
963 */
XiaokangQian74af2a82021-09-22 07:40:30 +0000964/*
XiaokangQian35dc6252021-11-11 08:16:19 +0000965 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +0000966 */
967
XiaokangQian8773aa02021-11-10 07:33:09 +0000968static int ssl_tls13_prepare_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +0000969{
970 int ret;
971
972 /* Compute transcript of handshake up to now. */
XiaokangQiancc90c942021-11-09 12:30:09 +0000973 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQian74af2a82021-09-22 07:40:30 +0000974 ssl->handshake->state_local.finished_out.digest,
975 sizeof( ssl->handshake->state_local.finished_out.digest ),
976 &ssl->handshake->state_local.finished_out.digest_len,
977 ssl->conf->endpoint );
978
979 if( ret != 0 )
980 {
Jerry Yu7ca30542021-12-08 15:57:57 +0800981 MBEDTLS_SSL_DEBUG_RET( 1, "calculate_verify_data failed", ret );
XiaokangQian74af2a82021-09-22 07:40:30 +0000982 return( ret );
983 }
984
985 return( 0 );
986}
987
XiaokangQiancc90c942021-11-09 12:30:09 +0000988static int ssl_tls13_finalize_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +0000989{
XiaokangQian0fa66432021-11-15 03:33:57 +0000990 // TODO: Add back resumption keys calculation after MVP.
991 ((void) ssl);
XiaokangQian74af2a82021-09-22 07:40:30 +0000992
993 return( 0 );
994}
995
XiaokangQian8773aa02021-11-10 07:33:09 +0000996static int ssl_tls13_write_finished_message_body( mbedtls_ssl_context *ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +0000997 unsigned char *buf,
998 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000999 size_t *out_len )
XiaokangQian74af2a82021-09-22 07:40:30 +00001000{
XiaokangQian8773aa02021-11-10 07:33:09 +00001001 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001002 /*
1003 * struct {
1004 * opaque verify_data[Hash.length];
1005 * } Finished;
1006 */
XiaokangQian8773aa02021-11-10 07:33:09 +00001007 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001008
1009 memcpy( buf, ssl->handshake->state_local.finished_out.digest,
XiaokangQian8773aa02021-11-10 07:33:09 +00001010 verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001011
Xiaofei Baid25fab62021-12-02 06:36:27 +00001012 *out_len = verify_data_len;
XiaokangQian74af2a82021-09-22 07:40:30 +00001013 return( 0 );
1014}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001015
XiaokangQian35dc6252021-11-11 08:16:19 +00001016/* Main entry point: orchestrates the other functions */
1017int mbedtls_ssl_tls13_write_finished_message( mbedtls_ssl_context *ssl )
1018{
1019 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1020 unsigned char *buf;
1021 size_t buf_len, msg_len;
1022
1023 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished message" ) );
1024
XiaokangQiandce82242021-11-15 06:01:26 +00001025 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_finished_message( ssl ) );
1026
XiaokangQian35dc6252021-11-11 08:16:19 +00001027 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg( ssl,
1028 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len ) );
1029
1030 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_finished_message_body(
1031 ssl, buf, buf + buf_len, &msg_len ) );
1032
Xiaofei Bai746f9482021-11-12 08:53:56 +00001033 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1034 buf, msg_len );
XiaokangQian35dc6252021-11-11 08:16:19 +00001035
1036 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_finished_message( ssl ) );
1037 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
1038 buf_len, msg_len ) );
1039 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_flush_output( ssl ) );
1040
1041cleanup:
1042
1043 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished message" ) );
1044 return( ret );
1045}
1046
Jerry Yu378254d2021-10-30 21:44:47 +08001047void mbedtls_ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
1048{
1049
1050 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
1051
1052 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001053 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001054 */
1055 if( ssl->session )
1056 {
Jerry Yu378254d2021-10-30 21:44:47 +08001057 mbedtls_ssl_session_free( ssl->session );
1058 mbedtls_free( ssl->session );
1059 }
1060 ssl->session = ssl->session_negotiate;
1061 ssl->session_negotiate = NULL;
1062
1063 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
1064}
1065
Ronald Cron49ad6192021-11-24 16:25:31 +01001066/*
1067 *
1068 * STATE HANDLING: Write ChangeCipherSpec
1069 *
1070 */
1071#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1072
1073static int ssl_tls13_write_change_cipher_spec_body( mbedtls_ssl_context *ssl,
1074 unsigned char *buf,
1075 unsigned char *end,
1076 size_t *olen )
1077{
1078 ((void) ssl);
1079
1080 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 1 );
1081 buf[0] = 1;
1082 *olen = 1;
1083
1084 return( 0 );
1085}
1086
Ronald Cron49ad6192021-11-24 16:25:31 +01001087int mbedtls_ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1088{
1089 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1090
1091 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
1092
1093 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_flush_output( ssl ) );
1094
1095 /* Write CCS message */
1096 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_change_cipher_spec_body(
1097 ssl, ssl->out_msg,
1098 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1099 &ssl->out_msglen ) );
1100
1101 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1102
Ronald Cron49ad6192021-11-24 16:25:31 +01001103 /* Dispatch message */
1104 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_record( ssl, 1 ) );
1105
1106cleanup:
1107
1108 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
1109 return( ret );
1110}
1111
1112#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1113
Ronald Cron6f135e12021-12-08 16:57:54 +01001114#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001115
1116#endif /* MBEDTLS_SSL_TLS_C */