blob: 1ad03a9b1a0677b15c00351ec48458e1dd18c69d [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)
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000139/* mbedtls_ssl_tls13_parse_sig_alg_ext()
140 *
141 * enum {
142 * ....
143 * ecdsa_secp256r1_sha256( 0x0403 ),
144 * ecdsa_secp384r1_sha384( 0x0503 ),
145 * ecdsa_secp521r1_sha512( 0x0603 ),
146 * ....
147 * } SignatureScheme;
148 *
149 * struct {
150 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
151 * } SignatureSchemeList;
152 */
153int mbedtls_ssl_tls13_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
154 const unsigned char *buf,
155 const unsigned char *end )
156{
157 const unsigned char *p = buf;
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000158 size_t supported_sig_algs_len = 0;
159 const unsigned char *supported_sig_algs_end;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +0000160 uint16_t sig_alg;
Xiaofei Baic234ecf2022-02-08 09:59:23 +0000161 uint32_t common_idx = 0;
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000162
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000163 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000164 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000165 p += 2;
166
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000167 memset( ssl->handshake->received_sig_algs, 0,
Xiaofei Bai51f515a2022-02-08 07:28:04 +0000168 sizeof(ssl->handshake->received_sig_algs) );
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000169
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000170 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
171 supported_sig_algs_end = p + supported_sig_algs_len;
Xiaofei Bai51f515a2022-02-08 07:28:04 +0000172 while( p < supported_sig_algs_end )
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000173 {
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000174 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +0000175 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000176 p += 2;
177
178 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x",
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +0000179 sig_alg ) );
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000180
Xiaofei Bai51f515a2022-02-08 07:28:04 +0000181 if( ! mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ||
182 ! mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) )
183 continue;
184
185 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000186 {
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000187 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +0000188 common_idx += 1;
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000189 }
190 }
191 /* Check that we consumed all the message. */
192 if( p != end )
193 {
194 MBEDTLS_SSL_DEBUG_MSG( 1,
195 ( "Signature algorithms extension length misaligned" ) );
196 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
197 MBEDTLS_ERR_SSL_DECODE_ERROR );
198 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
199 }
200
201 if( common_idx == 0 )
202 {
203 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
204 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
205 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
206 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
207 }
208
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000209 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS1_3_SIG_NONE;
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000210 return( 0 );
211}
212
Jerry Yu30b071c2021-09-12 20:16:03 +0800213/*
Jerry Yu30b071c2021-09-12 20:16:03 +0800214 * STATE HANDLING: Read CertificateVerify
215 */
Jerry Yud0fc5852021-10-29 11:09:06 +0800216/* Macro to express the maximum length of the verify structure.
Jerry Yu30b071c2021-09-12 20:16:03 +0800217 *
218 * The structure is computed per TLS 1.3 specification as:
219 * - 64 bytes of octet 32,
220 * - 33 bytes for the context string
221 * (which is either "TLS 1.3, client CertificateVerify"
222 * or "TLS 1.3, server CertificateVerify"),
Jerry Yud0fc5852021-10-29 11:09:06 +0800223 * - 1 byte for the octet 0x0, which serves as a separator,
Jerry Yu30b071c2021-09-12 20:16:03 +0800224 * - 32 or 48 bytes for the Transcript-Hash(Handshake Context, Certificate)
225 * (depending on the size of the transcript_hash)
226 *
227 * This results in a total size of
228 * - 130 bytes for a SHA256-based transcript hash, or
229 * (64 + 33 + 1 + 32 bytes)
230 * - 146 bytes for a SHA384-based transcript hash.
231 * (64 + 33 + 1 + 48 bytes)
232 *
233 */
Jerry Yu26c2d112021-10-25 12:42:58 +0800234#define SSL_VERIFY_STRUCT_MAX_SIZE ( 64 + \
235 33 + \
236 1 + \
237 MBEDTLS_TLS1_3_MD_MAX_SIZE \
Jerry Yu30b071c2021-09-12 20:16:03 +0800238 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800239
Jerry Yu0b32c502021-10-28 13:41:59 +0800240/*
241 * The ssl_tls13_create_verify_structure() creates the verify structure.
242 * As input, it requires the transcript hash.
243 *
244 * The caller has to ensure that the buffer has size at least
245 * SSL_VERIFY_STRUCT_MAX_SIZE bytes.
246 */
Jerry Yud0fc5852021-10-29 11:09:06 +0800247static void ssl_tls13_create_verify_structure( const unsigned char *transcript_hash,
Jerry Yu0b32c502021-10-28 13:41:59 +0800248 size_t transcript_hash_len,
249 unsigned char *verify_buffer,
250 size_t *verify_buffer_len,
251 int from )
252{
253 size_t idx;
Jerry Yu30b071c2021-09-12 20:16:03 +0800254
Jerry Yu0b32c502021-10-28 13:41:59 +0800255 /* RFC 8446, Section 4.4.3:
256 *
257 * The digital signature [in the CertificateVerify message] is then
258 * computed over the concatenation of:
259 * - A string that consists of octet 32 (0x20) repeated 64 times
260 * - The context string
261 * - A single 0 byte which serves as the separator
262 * - The content to be signed
263 */
264 memset( verify_buffer, 0x20, 64 );
265 idx = 64;
266
267 if( from == MBEDTLS_SSL_IS_CLIENT )
268 {
269 memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( client_cv ) );
270 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( client_cv );
271 }
272 else
273 { /* from == MBEDTLS_SSL_IS_SERVER */
274 memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( server_cv ) );
275 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( server_cv );
276 }
277
278 verify_buffer[idx++] = 0x0;
279
280 memcpy( verify_buffer + idx, transcript_hash, transcript_hash_len );
281 idx += transcript_hash_len;
282
283 *verify_buffer_len = idx;
284}
285
Jerry Yu0b32c502021-10-28 13:41:59 +0800286static int ssl_tls13_parse_certificate_verify( mbedtls_ssl_context *ssl,
Jerry Yud0fc5852021-10-29 11:09:06 +0800287 const unsigned char *buf,
288 const unsigned char *end,
289 const unsigned char *verify_buffer,
290 size_t verify_buffer_len )
Jerry Yu30b071c2021-09-12 20:16:03 +0800291{
292 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
293 const unsigned char *p = buf;
294 uint16_t algorithm;
Jerry Yu30b071c2021-09-12 20:16:03 +0800295 size_t signature_len;
296 mbedtls_pk_type_t sig_alg;
297 mbedtls_md_type_t md_alg;
Jerry Yud0fc5852021-10-29 11:09:06 +0800298 unsigned char verify_hash[MBEDTLS_MD_MAX_SIZE];
Jerry Yu30b071c2021-09-12 20:16:03 +0800299 size_t verify_hash_len;
300
Xiaofei Baid25fab62021-12-02 06:36:27 +0000301 void const *options = NULL;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000302#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Xiaofei Baid25fab62021-12-02 06:36:27 +0000303 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000304#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
305
Jerry Yu30b071c2021-09-12 20:16:03 +0800306 /*
307 * struct {
308 * SignatureScheme algorithm;
309 * opaque signature<0..2^16-1>;
310 * } CertificateVerify;
311 */
312 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
313 algorithm = MBEDTLS_GET_UINT16_BE( p, 0 );
314 p += 2;
315
316 /* RFC 8446 section 4.4.3
317 *
318 * If the CertificateVerify message is sent by a server, the signature algorithm
319 * MUST be one offered in the client's "signature_algorithms" extension unless
320 * no valid certificate chain can be produced without unsupported algorithms
321 *
322 * RFC 8446 section 4.4.2.2
323 *
324 * If the client cannot construct an acceptable chain using the provided
325 * certificates and decides to abort the handshake, then it MUST abort the handshake
326 * with an appropriate certificate-related alert (by default, "unsupported_certificate").
327 *
Jerry Yu6f87f252021-10-29 20:12:51 +0800328 * Check if algorithm is an offered signature algorithm.
Jerry Yu30b071c2021-09-12 20:16:03 +0800329 */
Jerry Yu24811fb2022-01-19 18:02:15 +0800330 if( ! mbedtls_ssl_sig_alg_is_offered( ssl, algorithm ) )
Jerry Yu30b071c2021-09-12 20:16:03 +0800331 {
Jerry Yu982d9e52021-10-14 15:59:37 +0800332 /* algorithm not in offered signature algorithms list */
333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Received signature algorithm(%04x) is not "
334 "offered.",
335 ( unsigned int ) algorithm ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800336 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800337 }
338
339 /* We currently only support ECDSA-based signatures */
340 switch( algorithm )
341 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000342 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Jerry Yu30b071c2021-09-12 20:16:03 +0800343 md_alg = MBEDTLS_MD_SHA256;
344 sig_alg = MBEDTLS_PK_ECDSA;
345 break;
Xiaofei Bai746f9482021-11-12 08:53:56 +0000346 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Jerry Yu30b071c2021-09-12 20:16:03 +0800347 md_alg = MBEDTLS_MD_SHA384;
348 sig_alg = MBEDTLS_PK_ECDSA;
349 break;
Xiaofei Bai746f9482021-11-12 08:53:56 +0000350 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Jerry Yu30b071c2021-09-12 20:16:03 +0800351 md_alg = MBEDTLS_MD_SHA512;
352 sig_alg = MBEDTLS_PK_ECDSA;
353 break;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000354#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Xiaofei Bai6dc90da2021-11-26 08:11:40 +0000355 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
XiaokangQiana83014d2021-11-22 07:53:34 +0000356 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Certificate Verify: using RSA PSS" ) );
XiaokangQian82d34cc2021-11-03 08:51:56 +0000357 md_alg = MBEDTLS_MD_SHA256;
358 sig_alg = MBEDTLS_PK_RSASSA_PSS;
359 break;
360#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800361 default:
362 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Certificate Verify: Unknown signature algorithm." ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800363 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800364 }
365
366 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate Verify: Signature algorithm ( %04x )",
367 ( unsigned int ) algorithm ) );
368
369 /*
370 * Check the certificate's key type matches the signature alg
371 */
372 if( !mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk, sig_alg ) )
373 {
374 MBEDTLS_SSL_DEBUG_MSG( 1, ( "signature algorithm doesn't match cert key" ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800375 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800376 }
377
378 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
379 signature_len = MBEDTLS_GET_UINT16_BE( p, 0 );
380 p += 2;
381 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, signature_len );
382
383 /* Hash verify buffer with indicated hash function */
384 switch( md_alg )
385 {
386#if defined(MBEDTLS_SHA256_C)
387 case MBEDTLS_MD_SHA256:
388 verify_hash_len = 32;
Jerry Yu133690c2021-10-25 14:01:13 +0800389 ret = mbedtls_sha256( verify_buffer, verify_buffer_len, verify_hash, 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800390 break;
391#endif /* MBEDTLS_SHA256_C */
392
393#if defined(MBEDTLS_SHA384_C)
394 case MBEDTLS_MD_SHA384:
395 verify_hash_len = 48;
Jerry Yu133690c2021-10-25 14:01:13 +0800396 ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 1 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800397 break;
398#endif /* MBEDTLS_SHA384_C */
399
400#if defined(MBEDTLS_SHA512_C)
401 case MBEDTLS_MD_SHA512:
402 verify_hash_len = 64;
Jerry Yu133690c2021-10-25 14:01:13 +0800403 ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800404 break;
405#endif /* MBEDTLS_SHA512_C */
406
Jerry Yu0b32c502021-10-28 13:41:59 +0800407 default:
408 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
409 break;
Jerry Yu30b071c2021-09-12 20:16:03 +0800410 }
411
Jerry Yu133690c2021-10-25 14:01:13 +0800412 if( ret != 0 )
413 {
414 MBEDTLS_SSL_DEBUG_RET( 1, "hash computation error", ret );
Jerry Yu6f87f252021-10-29 20:12:51 +0800415 goto error;
Jerry Yu133690c2021-10-25 14:01:13 +0800416 }
417
Jerry Yu30b071c2021-09-12 20:16:03 +0800418 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
XiaokangQian82d34cc2021-11-03 08:51:56 +0000419#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
420 if( sig_alg == MBEDTLS_PK_RSASSA_PSS )
421 {
422 const mbedtls_md_info_t* md_info;
Xiaofei Baid25fab62021-12-02 06:36:27 +0000423 rsassa_pss_options.mgf1_hash_id = md_alg;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000424 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
425 {
426 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
427 }
Xiaofei Baid25fab62021-12-02 06:36:27 +0000428 rsassa_pss_options.expected_salt_len = mbedtls_md_get_size( md_info );
429 options = (const void*) &rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000430 }
431#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800432
Xiaofei Baid25fab62021-12-02 06:36:27 +0000433 if( ( ret = mbedtls_pk_verify_ext( sig_alg, options,
Jerry Yu30b071c2021-09-12 20:16:03 +0800434 &ssl->session_negotiate->peer_cert->pk,
435 md_alg, verify_hash, verify_hash_len,
Jerry Yu6f87f252021-10-29 20:12:51 +0800436 p, signature_len ) ) == 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800437 {
Jerry Yu6f87f252021-10-29 20:12:51 +0800438 return( 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800439 }
Jerry Yu6f87f252021-10-29 20:12:51 +0800440 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify_ext", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800441
Jerry Yu6f87f252021-10-29 20:12:51 +0800442error:
443 /* RFC 8446 section 4.4.3
444 *
445 * If the verification fails, the receiver MUST terminate the handshake
446 * with a "decrypt_error" alert.
447 */
448 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
449 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
450 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
451
Jerry Yu30b071c2021-09-12 20:16:03 +0800452}
453#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
454
455int mbedtls_ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
456{
Jerry Yu30b071c2021-09-12 20:16:03 +0800457
Jerry Yuda8cdf22021-10-25 15:06:49 +0800458#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
459 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
460 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
461 size_t verify_buffer_len;
462 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
463 size_t transcript_len;
464 unsigned char *buf;
465 size_t buf_len;
466
Jerry Yu30b071c2021-09-12 20:16:03 +0800467 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
468
Jerry Yuda8cdf22021-10-25 15:06:49 +0800469 MBEDTLS_SSL_PROC_CHK(
Xiaofei Bai746f9482021-11-12 08:53:56 +0000470 mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
Jerry Yuda8cdf22021-10-25 15:06:49 +0800471 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu30b071c2021-09-12 20:16:03 +0800472
Jerry Yuda8cdf22021-10-25 15:06:49 +0800473 /* Need to calculate the hash of the transcript first
Jerry Yu0b32c502021-10-28 13:41:59 +0800474 * before reading the message since otherwise it gets
475 * included in the transcript
476 */
Jerry Yuda8cdf22021-10-25 15:06:49 +0800477 ret = mbedtls_ssl_get_handshake_transcript( ssl,
478 ssl->handshake->ciphersuite_info->mac,
479 transcript, sizeof( transcript ),
480 &transcript_len );
481 if( ret != 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800482 {
Jerry Yuda8cdf22021-10-25 15:06:49 +0800483 MBEDTLS_SSL_PEND_FATAL_ALERT(
484 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
485 MBEDTLS_ERR_SSL_INTERNAL_ERROR );
486 return( ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800487 }
488
Jerry Yuda8cdf22021-10-25 15:06:49 +0800489 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash", transcript, transcript_len );
490
491 /* Create verify structure */
492 ssl_tls13_create_verify_structure( transcript,
Jerry Yu0b32c502021-10-28 13:41:59 +0800493 transcript_len,
494 verify_buffer,
495 &verify_buffer_len,
496 ( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) ?
497 MBEDTLS_SSL_IS_SERVER :
498 MBEDTLS_SSL_IS_CLIENT );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800499
500 /* Process the message contents */
Jerry Yu0b32c502021-10-28 13:41:59 +0800501 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_verify( ssl, buf,
502 buf + buf_len, verify_buffer, verify_buffer_len ) );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800503
Xiaofei Bai746f9482021-11-12 08:53:56 +0000504 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
Jerry Yuda8cdf22021-10-25 15:06:49 +0800505 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, buf, buf_len );
Jerry Yu30b071c2021-09-12 20:16:03 +0800506
507cleanup:
508
509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
Jerry Yu5398c102021-11-05 13:32:38 +0800510 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_process_certificate_verify", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800511 return( ret );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800512#else
513 ((void) ssl);
514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
515 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
516#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800517}
518
519/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000520 *
521 * STATE HANDLING: Incoming Certificate, client-side only currently.
522 *
523 */
524
525/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000526 * Implementation
527 */
528
Xiaofei Bai947571e2021-09-29 09:12:03 +0000529#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
530#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
531/*
532 * Structure of Certificate message:
533 *
534 * enum {
535 * X509(0),
536 * RawPublicKey(2),
537 * (255)
538 * } CertificateType;
539 *
540 * struct {
541 * select (certificate_type) {
542 * case RawPublicKey:
543 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
544 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
545 * case X509:
546 * opaque cert_data<1..2^24-1>;
547 * };
548 * Extension extensions<0..2^16-1>;
549 * } CertificateEntry;
550 *
551 * struct {
552 * opaque certificate_request_context<0..2^8-1>;
553 * CertificateEntry certificate_list<0..2^24-1>;
554 * } Certificate;
555 *
556 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000557
558/* Parse certificate chain send by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000559static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
560 const unsigned char *buf,
561 const unsigned char *end )
562{
563 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
564 size_t certificate_request_context_len = 0;
565 size_t certificate_list_len = 0;
566 const unsigned char *p = buf;
567 const unsigned char *certificate_list_end;
568
569 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
570 certificate_request_context_len = p[0];
Jerry Yub640bf62021-10-29 10:05:32 +0800571 certificate_list_len = MBEDTLS_GET_UINT24_BE( p, 1 );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000572 p += 4;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000573
574 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
575 * support anything beyond 2^16 = 64K.
576 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000577 if( ( certificate_request_context_len != 0 ) ||
578 ( certificate_list_len >= 0x10000 ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000579 {
580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
581 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
582 MBEDTLS_ERR_SSL_DECODE_ERROR );
583 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
584 }
585
586 /* In case we tried to reuse a session but it failed */
587 if( ssl->session_negotiate->peer_cert != NULL )
588 {
589 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
590 mbedtls_free( ssl->session_negotiate->peer_cert );
591 }
592
593 if( ( ssl->session_negotiate->peer_cert =
594 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
595 {
596 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
597 sizeof( mbedtls_x509_crt ) ) );
598 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
599 MBEDTLS_ERR_SSL_ALLOC_FAILED );
600 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
601 }
602
603 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
604
Xiaofei Bai947571e2021-09-29 09:12:03 +0000605 certificate_list_end = p + certificate_list_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000606 while( p < certificate_list_end )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000607 {
608 size_t cert_data_len, extensions_len;
609
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000610 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 3 );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000611 cert_data_len = MBEDTLS_GET_UINT24_BE( p, 0 );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000612 p += 3;
613
614 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
615 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
616 * check that we have a minimum of 128 bytes of data, this is not
617 * clear why we need that though.
618 */
619 if( ( cert_data_len < 128 ) || ( cert_data_len >= 0x10000 ) )
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000620 {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000621 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
622 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
623 MBEDTLS_ERR_SSL_DECODE_ERROR );
624 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
625 }
626
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000627 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, cert_data_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000628 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
629 p, cert_data_len );
630
631 switch( ret )
632 {
633 case 0: /*ok*/
634 break;
635 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
636 /* Ignore certificate with an unknown algorithm: maybe a
637 prior certificate was already trusted. */
638 break;
639
640 case MBEDTLS_ERR_X509_ALLOC_FAILED:
641 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
642 MBEDTLS_ERR_X509_ALLOC_FAILED );
643 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
644 return( ret );
645
646 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
647 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
648 MBEDTLS_ERR_X509_UNKNOWN_VERSION );
649 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
650 return( ret );
651
652 default:
653 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
654 ret );
655 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
656 return( ret );
657 }
658
659 p += cert_data_len;
660
661 /* Certificate extensions length */
662 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 2 );
663 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
664 p += 2;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000665 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, extensions_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000666 p += extensions_len;
667 }
668
669 /* Check that all the message is consumed. */
670 if( p != end )
671 {
672 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
673 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, \
674 MBEDTLS_ERR_SSL_DECODE_ERROR );
675 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
676 }
677
678 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
679
680 return( ret );
681}
682#else
683static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
684 const unsigned char *buf,
685 const unsigned char *end )
686{
687 ((void) ssl);
688 ((void) buf);
689 ((void) end);
690 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
691}
692#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
693#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
694
695#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
696#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000697/* Validate certificate chain sent by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000698static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
699{
700 int ret = 0;
701 mbedtls_x509_crt *ca_chain;
702 mbedtls_x509_crl *ca_crl;
Xiaofei Baiff456022021-10-28 06:50:17 +0000703 uint32_t verify_result = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000704
705#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
706 if( ssl->handshake->sni_ca_chain != NULL )
707 {
708 ca_chain = ssl->handshake->sni_ca_chain;
709 ca_crl = ssl->handshake->sni_ca_crl;
710 }
711 else
712#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
713 {
714 ca_chain = ssl->conf->ca_chain;
715 ca_crl = ssl->conf->ca_crl;
716 }
717
718 /*
719 * Main check: verify certificate
720 */
721 ret = mbedtls_x509_crt_verify_with_profile(
722 ssl->session_negotiate->peer_cert,
723 ca_chain, ca_crl,
724 ssl->conf->cert_profile,
725 ssl->hostname,
Xiaofei Baiff456022021-10-28 06:50:17 +0000726 &verify_result,
Xiaofei Bai947571e2021-09-29 09:12:03 +0000727 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
728
729 if( ret != 0 )
730 {
731 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
732 }
733
734 /*
735 * Secondary checks: always done, but change 'ret' only if it was 0
736 */
737
738#if defined(MBEDTLS_ECP_C)
739 {
740 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
741
742 /* If certificate uses an EC key, make sure the curve is OK */
743 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
744 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
745 {
Xiaofei Baiff456022021-10-28 06:50:17 +0000746 verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000747
748 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( EC key curve )" ) );
749 if( ret == 0 )
750 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
751 }
752 }
753#endif /* MBEDTLS_ECP_C */
754
755 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
756 ssl->handshake->ciphersuite_info,
757 !ssl->conf->endpoint,
Xiaofei Baiff456022021-10-28 06:50:17 +0000758 &verify_result ) != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000759 {
760 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( usage extensions )" ) );
761 if( ret == 0 )
762 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
763 }
764
765
766 if( ca_chain == NULL )
767 {
768 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
769 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
770 }
771
772 if( ret != 0 )
773 {
774 /* The certificate may have been rejected for several reasons.
775 Pick one and send the corresponding alert. Which alert to send
776 may be a subject of debate in some cases. */
Xiaofei Baiff456022021-10-28 06:50:17 +0000777 if( verify_result & MBEDTLS_X509_BADCERT_OTHER )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000778 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000779 else if( verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000780 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000781 else if( verify_result & ( MBEDTLS_X509_BADCERT_KEY_USAGE |
782 MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
783 MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
784 MBEDTLS_X509_BADCERT_BAD_PK |
785 MBEDTLS_X509_BADCERT_BAD_KEY ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000786 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000787 else if( verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000788 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000789 else if( verify_result & MBEDTLS_X509_BADCERT_REVOKED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000790 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000791 else if( verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000792 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret );
793 else
794 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret );
795 }
796
797#if defined(MBEDTLS_DEBUG_C)
Xiaofei Baiff456022021-10-28 06:50:17 +0000798 if( verify_result != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000799 {
800 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
Jerry Yu83bb1312021-10-28 22:16:33 +0800801 (unsigned int) verify_result ) );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000802 }
803 else
804 {
805 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
806 }
807#endif /* MBEDTLS_DEBUG_C */
808
Xiaofei Baiff456022021-10-28 06:50:17 +0000809 ssl->session_negotiate->verify_result = verify_result;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000810 return( ret );
811}
812#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
813static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
814{
815 ((void) ssl);
816 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
817}
818#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
819#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
820
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000821int mbedtls_ssl_tls13_process_certificate( mbedtls_ssl_context *ssl )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000822{
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000823 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
825
826#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
827 unsigned char *buf;
828 size_t buf_len;
829
Xiaofei Bai746f9482021-11-12 08:53:56 +0000830 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000831 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
832 &buf, &buf_len ) );
833
834 /* Parse the certificate chain sent by the peer. */
835 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate( ssl, buf, buf + buf_len ) );
836 /* Validate the certificate chain and set the verification results. */
837 MBEDTLS_SSL_PROC_CHK( ssl_tls13_validate_certificate( ssl ) );
838
Xiaofei Bai746f9482021-11-12 08:53:56 +0000839 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
840 buf, buf_len );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000841
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000842cleanup:
843
844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Xiaofei Bai10aeec02021-10-26 09:50:08 +0000845#else
846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
847 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
848#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000849 return( ret );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000850}
851
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000852/*
853 *
XiaokangQianc5c39d52021-11-09 11:55:10 +0000854 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000855 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000856/*
857 * Implementation
858 */
859
XiaokangQianaaa0e192021-11-10 03:07:04 +0000860static int ssl_tls13_preprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000861{
862 int ret;
863
XiaokangQianc5c39d52021-11-09 11:55:10 +0000864 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000865 ssl->handshake->state_local.finished_in.digest,
866 sizeof( ssl->handshake->state_local.finished_in.digest ),
867 &ssl->handshake->state_local.finished_in.digest_len,
XiaokangQianc5c39d52021-11-09 11:55:10 +0000868 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
XiaokangQianc13f9352021-11-11 06:13:22 +0000869 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000870 if( ret != 0 )
871 {
XiaokangQianc5c39d52021-11-09 11:55:10 +0000872 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_calculate_verify_data", ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000873 return( ret );
874 }
875
876 return( 0 );
877}
878
XiaokangQianc5c39d52021-11-09 11:55:10 +0000879static int ssl_tls13_parse_finished_message( mbedtls_ssl_context *ssl,
880 const unsigned char *buf,
881 const unsigned char *end )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000882{
XiaokangQian33062842021-11-11 03:37:45 +0000883 /*
884 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +0000885 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +0000886 * } Finished;
887 */
888 const unsigned char *expected_verify_data =
889 ssl->handshake->state_local.finished_in.digest;
890 size_t expected_verify_data_len =
891 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000892 /* Structural validation */
XiaokangQian33062842021-11-11 03:37:45 +0000893 if( (size_t)( end - buf ) != expected_verify_data_len )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000894 {
895 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
896
897 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +0000898 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000899 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
900 }
901
XiaokangQianc5c39d52021-11-09 11:55:10 +0000902 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (self-computed):",
XiaokangQian33062842021-11-11 03:37:45 +0000903 expected_verify_data,
904 expected_verify_data_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +0000905 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (received message):", buf,
XiaokangQian33062842021-11-11 03:37:45 +0000906 expected_verify_data_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000907
908 /* Semantic validation */
Gabor Mezei685472b2021-11-24 11:17:36 +0100909 if( mbedtls_ct_memcmp( buf,
910 expected_verify_data,
911 expected_verify_data_len ) != 0 )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000912 {
913 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
914
XiaokangQian33062842021-11-11 03:37:45 +0000915 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +0000916 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000917 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
918 }
919 return( 0 );
920}
921
XiaokangQianc13f9352021-11-11 06:13:22 +0000922#if defined(MBEDTLS_SSL_CLI_C)
XiaokangQianaaa0e192021-11-10 03:07:04 +0000923static int ssl_tls13_postprocess_server_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000924{
XiaokangQian33062842021-11-11 03:37:45 +0000925 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000926 mbedtls_ssl_key_set traffic_keys;
XiaokangQian1aef02e2021-10-28 09:54:34 +0000927 mbedtls_ssl_transform *transform_application = NULL;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000928
XiaokangQian4cab0242021-10-12 08:43:37 +0000929 ret = mbedtls_ssl_tls13_key_schedule_stage_application( ssl );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000930 if( ret != 0 )
931 {
932 MBEDTLS_SSL_DEBUG_RET( 1,
XiaokangQian4cab0242021-10-12 08:43:37 +0000933 "mbedtls_ssl_tls13_key_schedule_stage_application", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000934 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000935 }
936
XiaokangQian33062842021-11-11 03:37:45 +0000937 ret = mbedtls_ssl_tls13_generate_application_keys( ssl, &traffic_keys );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000938 if( ret != 0 )
939 {
940 MBEDTLS_SSL_DEBUG_RET( 1,
XiaokangQiand0aa3e92021-11-10 06:17:40 +0000941 "mbedtls_ssl_tls13_generate_application_keys", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000942 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000943 }
944
945 transform_application =
946 mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
947 if( transform_application == NULL )
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000948 {
949 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
950 goto cleanup;
951 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000952
953 ret = mbedtls_ssl_tls13_populate_transform(
954 transform_application,
955 ssl->conf->endpoint,
956 ssl->session_negotiate->ciphersuite,
957 &traffic_keys,
958 ssl );
959 if( ret != 0 )
960 {
961 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000962 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000963 }
964
965 ssl->transform_application = transform_application;
966
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000967cleanup:
968
XiaokangQian33062842021-11-11 03:37:45 +0000969 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +0000970 if( ret != 0 )
XiaokangQian1aef02e2021-10-28 09:54:34 +0000971 {
972 mbedtls_free( transform_application );
973 MBEDTLS_SSL_PEND_FATAL_ALERT(
974 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
975 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
976 }
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000977 return( ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000978}
XiaokangQianc13f9352021-11-11 06:13:22 +0000979#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000980
XiaokangQiancc90c942021-11-09 12:30:09 +0000981static int ssl_tls13_postprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000982{
983
XiaokangQianc13f9352021-11-11 06:13:22 +0000984#if defined(MBEDTLS_SSL_CLI_C)
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000985 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
986 {
XiaokangQianaaa0e192021-11-10 03:07:04 +0000987 return( ssl_tls13_postprocess_server_finished_message( ssl ) );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000988 }
XiaokangQianc13f9352021-11-11 06:13:22 +0000989#else
990 ((void) ssl);
991#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000992
993 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
994}
995
XiaokangQianc5c39d52021-11-09 11:55:10 +0000996int mbedtls_ssl_tls13_process_finished_message( mbedtls_ssl_context *ssl )
997{
XiaokangQian33062842021-11-11 03:37:45 +0000998 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +0000999 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +00001000 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001001
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001003
1004 /* Preprocessing step: Compute handshake digest */
XiaokangQianaaa0e192021-11-10 03:07:04 +00001005 MBEDTLS_SSL_PROC_CHK( ssl_tls13_preprocess_finished_message( ssl ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001006
Xiaofei Bai746f9482021-11-12 08:53:56 +00001007 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001008 MBEDTLS_SSL_HS_FINISHED,
Xiaofei Baieef15042021-11-18 07:29:56 +00001009 &buf, &buf_len ) );
1010 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_finished_message( ssl, buf, buf + buf_len ) );
Xiaofei Bai746f9482021-11-12 08:53:56 +00001011 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
Xiaofei Baieef15042021-11-18 07:29:56 +00001012 ssl, MBEDTLS_SSL_HS_FINISHED, buf, buf_len );
XiaokangQianaaa0e192021-11-10 03:07:04 +00001013 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_finished_message( ssl ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001014
1015cleanup:
1016
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001017 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001018 return( ret );
1019}
1020
XiaokangQian74af2a82021-09-22 07:40:30 +00001021/*
1022 *
XiaokangQiancc90c942021-11-09 12:30:09 +00001023 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +00001024 *
1025 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001026/*
XiaokangQian35dc6252021-11-11 08:16:19 +00001027 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +00001028 */
1029
XiaokangQian8773aa02021-11-10 07:33:09 +00001030static int ssl_tls13_prepare_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +00001031{
1032 int ret;
1033
1034 /* Compute transcript of handshake up to now. */
XiaokangQiancc90c942021-11-09 12:30:09 +00001035 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQian74af2a82021-09-22 07:40:30 +00001036 ssl->handshake->state_local.finished_out.digest,
1037 sizeof( ssl->handshake->state_local.finished_out.digest ),
1038 &ssl->handshake->state_local.finished_out.digest_len,
1039 ssl->conf->endpoint );
1040
1041 if( ret != 0 )
1042 {
Jerry Yu7ca30542021-12-08 15:57:57 +08001043 MBEDTLS_SSL_DEBUG_RET( 1, "calculate_verify_data failed", ret );
XiaokangQian74af2a82021-09-22 07:40:30 +00001044 return( ret );
1045 }
1046
1047 return( 0 );
1048}
1049
XiaokangQiancc90c942021-11-09 12:30:09 +00001050static int ssl_tls13_finalize_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +00001051{
XiaokangQian0fa66432021-11-15 03:33:57 +00001052 // TODO: Add back resumption keys calculation after MVP.
1053 ((void) ssl);
XiaokangQian74af2a82021-09-22 07:40:30 +00001054
1055 return( 0 );
1056}
1057
XiaokangQian8773aa02021-11-10 07:33:09 +00001058static int ssl_tls13_write_finished_message_body( mbedtls_ssl_context *ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001059 unsigned char *buf,
1060 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001061 size_t *out_len )
XiaokangQian74af2a82021-09-22 07:40:30 +00001062{
XiaokangQian8773aa02021-11-10 07:33:09 +00001063 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001064 /*
1065 * struct {
1066 * opaque verify_data[Hash.length];
1067 * } Finished;
1068 */
XiaokangQian8773aa02021-11-10 07:33:09 +00001069 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001070
1071 memcpy( buf, ssl->handshake->state_local.finished_out.digest,
XiaokangQian8773aa02021-11-10 07:33:09 +00001072 verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001073
Xiaofei Baid25fab62021-12-02 06:36:27 +00001074 *out_len = verify_data_len;
XiaokangQian74af2a82021-09-22 07:40:30 +00001075 return( 0 );
1076}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001077
XiaokangQian35dc6252021-11-11 08:16:19 +00001078/* Main entry point: orchestrates the other functions */
1079int mbedtls_ssl_tls13_write_finished_message( mbedtls_ssl_context *ssl )
1080{
1081 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1082 unsigned char *buf;
1083 size_t buf_len, msg_len;
1084
1085 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished message" ) );
1086
XiaokangQiandce82242021-11-15 06:01:26 +00001087 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_finished_message( ssl ) );
1088
XiaokangQian35dc6252021-11-11 08:16:19 +00001089 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg( ssl,
1090 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len ) );
1091
1092 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_finished_message_body(
1093 ssl, buf, buf + buf_len, &msg_len ) );
1094
Xiaofei Bai746f9482021-11-12 08:53:56 +00001095 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1096 buf, msg_len );
XiaokangQian35dc6252021-11-11 08:16:19 +00001097
1098 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_finished_message( ssl ) );
1099 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
1100 buf_len, msg_len ) );
1101 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_flush_output( ssl ) );
1102
1103cleanup:
1104
1105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished message" ) );
1106 return( ret );
1107}
1108
Jerry Yu378254d2021-10-30 21:44:47 +08001109void mbedtls_ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
1110{
1111
1112 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
1113
1114 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001115 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001116 */
1117 if( ssl->session )
1118 {
Jerry Yu378254d2021-10-30 21:44:47 +08001119 mbedtls_ssl_session_free( ssl->session );
1120 mbedtls_free( ssl->session );
1121 }
1122 ssl->session = ssl->session_negotiate;
1123 ssl->session_negotiate = NULL;
1124
1125 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
1126}
1127
Ronald Cron49ad6192021-11-24 16:25:31 +01001128/*
1129 *
1130 * STATE HANDLING: Write ChangeCipherSpec
1131 *
1132 */
1133#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1134
XiaokangQian355e09a2022-01-20 11:14:50 +00001135static int ssl_tls13_finalize_change_cipher_spec( mbedtls_ssl_context* ssl )
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001136{
1137
1138#if defined(MBEDTLS_SSL_CLI_C)
1139 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
1140 {
1141 switch( ssl->state )
1142 {
1143 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
1144 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1145 break;
1146 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
1147 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1148 break;
1149 default:
1150 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1151 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1152 }
1153 }
1154#else
1155 ((void) ssl);
1156#endif /* MBEDTLS_SSL_CLI_C */
1157
1158 return( 0 );
1159}
1160
Ronald Cron49ad6192021-11-24 16:25:31 +01001161static int ssl_tls13_write_change_cipher_spec_body( mbedtls_ssl_context *ssl,
1162 unsigned char *buf,
1163 unsigned char *end,
1164 size_t *olen )
1165{
1166 ((void) ssl);
1167
1168 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 1 );
1169 buf[0] = 1;
1170 *olen = 1;
1171
1172 return( 0 );
1173}
1174
Ronald Cron49ad6192021-11-24 16:25:31 +01001175int mbedtls_ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1176{
1177 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1178
1179 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
1180
1181 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_flush_output( ssl ) );
1182
1183 /* Write CCS message */
1184 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_change_cipher_spec_body(
1185 ssl, ssl->out_msg,
1186 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1187 &ssl->out_msglen ) );
1188
1189 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1190
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001191 /* Update state */
XiaokangQian355e09a2022-01-20 11:14:50 +00001192 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_change_cipher_spec( ssl ) );
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001193
Ronald Cron49ad6192021-11-24 16:25:31 +01001194 /* Dispatch message */
1195 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_record( ssl, 1 ) );
1196
1197cleanup:
1198
1199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
1200 return( ret );
1201}
1202
1203#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1204
XiaokangQian78b1fa72022-01-19 06:56:30 +00001205/* Reset SSL context and update hash for handling HRR.
1206 *
1207 * Replace Transcript-Hash(X) by
1208 * Transcript-Hash( message_hash ||
1209 * 00 00 Hash.length ||
1210 * X )
1211 * A few states of the handshake are preserved, including:
1212 * - session ID
1213 * - session ticket
1214 * - negotiated ciphersuite
1215 */
1216int mbedtls_ssl_reset_transcript_for_hrr( mbedtls_ssl_context *ssl )
1217{
1218 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1219 unsigned char hash_transcript[ MBEDTLS_MD_MAX_SIZE + 4 ];
XiaokangQian0ece9982022-01-24 08:56:23 +00001220 size_t hash_len;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001221 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1222 uint16_t cipher_suite = ssl->session_negotiate->ciphersuite;
1223 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
1224
1225 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Reset SSL session for HRR" ) );
1226
XiaokangQian0ece9982022-01-24 08:56:23 +00001227 ret = mbedtls_ssl_get_handshake_transcript( ssl, ciphersuite_info->mac,
1228 hash_transcript + 4,
1229 MBEDTLS_MD_MAX_SIZE,
1230 &hash_len );
1231 if( ret != 0 )
1232 {
1233 MBEDTLS_SSL_DEBUG_RET( 4, "mbedtls_ssl_get_handshake_transcript", ret );
1234 return( ret );
1235 }
1236
1237 hash_transcript[0] = MBEDTLS_SSL_HS_MESSAGE_HASH;
1238 hash_transcript[1] = 0;
1239 hash_transcript[2] = 0;
1240 hash_transcript[3] = (unsigned char) hash_len;
1241
1242 hash_len += 4;
1243
XiaokangQian78b1fa72022-01-19 06:56:30 +00001244 if( ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
1245 {
1246#if defined(MBEDTLS_SHA256_C)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001247 MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-256 handshake transcript",
XiaokangQian0ece9982022-01-24 08:56:23 +00001248 hash_transcript, hash_len );
XiaokangQian78b1fa72022-01-19 06:56:30 +00001249
1250#if defined(MBEDTLS_USE_PSA_CRYPTO)
1251 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
1252 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
1253#else
1254 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
1255#endif
XiaokangQian78b1fa72022-01-19 06:56:30 +00001256#endif /* MBEDTLS_SHA256_C */
1257 }
1258 else if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1259 {
1260#if defined(MBEDTLS_SHA384_C)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001261 MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-384 handshake transcript",
XiaokangQian0ece9982022-01-24 08:56:23 +00001262 hash_transcript, hash_len );
XiaokangQian78b1fa72022-01-19 06:56:30 +00001263
1264#if defined(MBEDTLS_USE_PSA_CRYPTO)
1265 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
1266 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
1267#else
1268 mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 );
1269#endif
XiaokangQian78b1fa72022-01-19 06:56:30 +00001270#endif /* MBEDTLS_SHA384_C */
1271 }
1272
XiaokangQian0ece9982022-01-24 08:56:23 +00001273#if defined(MBEDTLS_SHA256_C) || defined(MBEDTLS_SHA384_C)
1274 ssl->handshake->update_checksum( ssl, hash_transcript, hash_len );
1275#endif /* MBEDTLS_SHA256_C || MBEDTLS_SHA384_C */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001276 return( ret );
1277}
1278
Ronald Cron6f135e12021-12-08 16:57:54 +01001279#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001280
1281#endif /* MBEDTLS_SSL_TLS_C */