blob: aeefdc77f9511b91399cb1c559d8bc650db05430 [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 Baie1e34422021-12-23 12:09:05 +0000139
140/*
141 * mbedtls_ssl_tls13_write_sig_alg_ext( )
142 *
143 * enum {
144 * ....
145 * ecdsa_secp256r1_sha256( 0x0403 ),
146 * ecdsa_secp384r1_sha384( 0x0503 ),
147 * ecdsa_secp521r1_sha512( 0x0603 ),
148 * ....
149 * } SignatureScheme;
150 *
151 * struct {
152 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
153 * } SignatureSchemeList;
154 *
155 * Only if we handle at least one key exchange that needs signatures.
156 */
157int mbedtls_ssl_tls13_write_sig_alg_ext( mbedtls_ssl_context *ssl,
158 unsigned char *buf,
159 unsigned char *end,
160 size_t *out_len )
161{
162 unsigned char *p = buf;
163 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
164 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
165
166 *out_len = 0;
167
168 /* Skip the extension on the client if all allowed key exchanges
169 * are PSK-based. */
170#if defined(MBEDTLS_SSL_CLI_C)
171 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
172 !mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
173 {
174 return( 0 );
175 }
176#endif /* MBEDTLS_SSL_CLI_C */
177
178 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
179
180 /* Check if we have space for header and length field:
181 * - extension_type (2 bytes)
182 * - extension_data_length (2 bytes)
183 * - supported_signature_algorithms_length (2 bytes)
184 */
185 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
186 p += 6;
187
188 /*
189 * Write supported_signature_algorithms
190 */
191 supported_sig_alg = p;
192 for( const uint16_t *sig_alg = ssl->conf->tls13_sig_algs;
193 *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
194 {
195 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
196 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
197 p += 2;
198 MBEDTLS_SSL_DEBUG_MSG( 3, ( "signature scheme [%x]", *sig_alg ) );
199 }
200
201 /* Length of supported_signature_algorithms */
202 supported_sig_alg_len = p - supported_sig_alg;
203 if( supported_sig_alg_len == 0 )
204 {
205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
206 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
207 }
208
209 /* Write extension_type */
210 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
211 /* Write extension_data_length */
212 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
213 /* Write length of supported_signature_algorithms */
214 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
215
216 /* Output the total length of signature algorithms extension. */
217 *out_len = p - buf;
218
219 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
220 return( 0 );
221}
222
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000223/* mbedtls_ssl_tls13_parse_sig_alg_ext()
224 *
225 * enum {
226 * ....
227 * ecdsa_secp256r1_sha256( 0x0403 ),
228 * ecdsa_secp384r1_sha384( 0x0503 ),
229 * ecdsa_secp521r1_sha512( 0x0603 ),
230 * ....
231 * } SignatureScheme;
232 *
233 * struct {
234 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
235 * } SignatureSchemeList;
236 */
237int mbedtls_ssl_tls13_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
238 const unsigned char *buf,
239 const unsigned char *end )
240{
241 const unsigned char *p = buf;
242 const uint16_t *sig_alg;
243 unsigned int signature_scheme; /* store received signature algorithm scheme */
244 uint32_t common_idx = 0; /* iterate through received signature schemes list */
245
246 /* skip 2 bytes of signature algorithms length */
247 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
248 p += 2;
249
250 memset( ssl->handshake->sig_algs, 0, sizeof( ssl->handshake->sig_algs ) );
251
252 while( p < end && common_idx + 1 < MBEDTLS_PK_SIGNATURE_MAX_SIZE )
253 {
254 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
255 signature_scheme = MBEDTLS_GET_UINT16_BE( p, 0 );
256 p += 2;
257
258 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x",
259 signature_scheme ) );
260
261 for( sig_alg = ssl->conf->tls13_sig_algs;
262 *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
263 {
264 if( *sig_alg == signature_scheme )
265 {
266 ssl->handshake->sig_algs[common_idx] = signature_scheme;
267 common_idx++;
268 break;
269 }
270 }
271 }
272 /* Check that we consumed all the message. */
273 if( p != end )
274 {
275 MBEDTLS_SSL_DEBUG_MSG( 1,
276 ( "Signature algorithms extension length misaligned" ) );
277 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
278 MBEDTLS_ERR_SSL_DECODE_ERROR );
279 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
280 }
281
282 if( common_idx == 0 )
283 {
284 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
285 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
286 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
287 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
288 }
289
290 ssl->handshake->sig_algs[common_idx] = MBEDTLS_TLS1_3_SIG_NONE;
291
292 return( 0 );
293}
294
Jerry Yu30b071c2021-09-12 20:16:03 +0800295/*
Jerry Yu30b071c2021-09-12 20:16:03 +0800296 * STATE HANDLING: Read CertificateVerify
297 */
Jerry Yud0fc5852021-10-29 11:09:06 +0800298/* Macro to express the maximum length of the verify structure.
Jerry Yu30b071c2021-09-12 20:16:03 +0800299 *
300 * The structure is computed per TLS 1.3 specification as:
301 * - 64 bytes of octet 32,
302 * - 33 bytes for the context string
303 * (which is either "TLS 1.3, client CertificateVerify"
304 * or "TLS 1.3, server CertificateVerify"),
Jerry Yud0fc5852021-10-29 11:09:06 +0800305 * - 1 byte for the octet 0x0, which serves as a separator,
Jerry Yu30b071c2021-09-12 20:16:03 +0800306 * - 32 or 48 bytes for the Transcript-Hash(Handshake Context, Certificate)
307 * (depending on the size of the transcript_hash)
308 *
309 * This results in a total size of
310 * - 130 bytes for a SHA256-based transcript hash, or
311 * (64 + 33 + 1 + 32 bytes)
312 * - 146 bytes for a SHA384-based transcript hash.
313 * (64 + 33 + 1 + 48 bytes)
314 *
315 */
Jerry Yu26c2d112021-10-25 12:42:58 +0800316#define SSL_VERIFY_STRUCT_MAX_SIZE ( 64 + \
317 33 + \
318 1 + \
319 MBEDTLS_TLS1_3_MD_MAX_SIZE \
Jerry Yu30b071c2021-09-12 20:16:03 +0800320 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800321
Jerry Yu0b32c502021-10-28 13:41:59 +0800322/*
323 * The ssl_tls13_create_verify_structure() creates the verify structure.
324 * As input, it requires the transcript hash.
325 *
326 * The caller has to ensure that the buffer has size at least
327 * SSL_VERIFY_STRUCT_MAX_SIZE bytes.
328 */
Jerry Yud0fc5852021-10-29 11:09:06 +0800329static void ssl_tls13_create_verify_structure( const unsigned char *transcript_hash,
Jerry Yu0b32c502021-10-28 13:41:59 +0800330 size_t transcript_hash_len,
331 unsigned char *verify_buffer,
332 size_t *verify_buffer_len,
333 int from )
334{
335 size_t idx;
Jerry Yu30b071c2021-09-12 20:16:03 +0800336
Jerry Yu0b32c502021-10-28 13:41:59 +0800337 /* RFC 8446, Section 4.4.3:
338 *
339 * The digital signature [in the CertificateVerify message] is then
340 * computed over the concatenation of:
341 * - A string that consists of octet 32 (0x20) repeated 64 times
342 * - The context string
343 * - A single 0 byte which serves as the separator
344 * - The content to be signed
345 */
346 memset( verify_buffer, 0x20, 64 );
347 idx = 64;
348
349 if( from == MBEDTLS_SSL_IS_CLIENT )
350 {
351 memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( client_cv ) );
352 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( client_cv );
353 }
354 else
355 { /* from == MBEDTLS_SSL_IS_SERVER */
356 memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( server_cv ) );
357 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( server_cv );
358 }
359
360 verify_buffer[idx++] = 0x0;
361
362 memcpy( verify_buffer + idx, transcript_hash, transcript_hash_len );
363 idx += transcript_hash_len;
364
365 *verify_buffer_len = idx;
366}
367
Jerry Yu0b32c502021-10-28 13:41:59 +0800368static int ssl_tls13_parse_certificate_verify( mbedtls_ssl_context *ssl,
Jerry Yud0fc5852021-10-29 11:09:06 +0800369 const unsigned char *buf,
370 const unsigned char *end,
371 const unsigned char *verify_buffer,
372 size_t verify_buffer_len )
Jerry Yu30b071c2021-09-12 20:16:03 +0800373{
374 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
375 const unsigned char *p = buf;
376 uint16_t algorithm;
Jerry Yu30b071c2021-09-12 20:16:03 +0800377 size_t signature_len;
378 mbedtls_pk_type_t sig_alg;
379 mbedtls_md_type_t md_alg;
Jerry Yud0fc5852021-10-29 11:09:06 +0800380 unsigned char verify_hash[MBEDTLS_MD_MAX_SIZE];
Jerry Yu30b071c2021-09-12 20:16:03 +0800381 size_t verify_hash_len;
382
Xiaofei Baid25fab62021-12-02 06:36:27 +0000383 void const *options = NULL;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000384#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Xiaofei Baid25fab62021-12-02 06:36:27 +0000385 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000386#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
387
Jerry Yu30b071c2021-09-12 20:16:03 +0800388 /*
389 * struct {
390 * SignatureScheme algorithm;
391 * opaque signature<0..2^16-1>;
392 * } CertificateVerify;
393 */
394 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
395 algorithm = MBEDTLS_GET_UINT16_BE( p, 0 );
396 p += 2;
397
398 /* RFC 8446 section 4.4.3
399 *
400 * If the CertificateVerify message is sent by a server, the signature algorithm
401 * MUST be one offered in the client's "signature_algorithms" extension unless
402 * no valid certificate chain can be produced without unsupported algorithms
403 *
404 * RFC 8446 section 4.4.2.2
405 *
406 * If the client cannot construct an acceptable chain using the provided
407 * certificates and decides to abort the handshake, then it MUST abort the handshake
408 * with an appropriate certificate-related alert (by default, "unsupported_certificate").
409 *
Jerry Yu6f87f252021-10-29 20:12:51 +0800410 * Check if algorithm is an offered signature algorithm.
Jerry Yu30b071c2021-09-12 20:16:03 +0800411 */
Jerry Yu24811fb2022-01-19 18:02:15 +0800412 if( ! mbedtls_ssl_sig_alg_is_offered( ssl, algorithm ) )
Jerry Yu30b071c2021-09-12 20:16:03 +0800413 {
Jerry Yu982d9e52021-10-14 15:59:37 +0800414 /* algorithm not in offered signature algorithms list */
415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Received signature algorithm(%04x) is not "
416 "offered.",
417 ( unsigned int ) algorithm ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800418 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800419 }
420
421 /* We currently only support ECDSA-based signatures */
422 switch( algorithm )
423 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000424 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Jerry Yu30b071c2021-09-12 20:16:03 +0800425 md_alg = MBEDTLS_MD_SHA256;
426 sig_alg = MBEDTLS_PK_ECDSA;
427 break;
Xiaofei Bai746f9482021-11-12 08:53:56 +0000428 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Jerry Yu30b071c2021-09-12 20:16:03 +0800429 md_alg = MBEDTLS_MD_SHA384;
430 sig_alg = MBEDTLS_PK_ECDSA;
431 break;
Xiaofei Bai746f9482021-11-12 08:53:56 +0000432 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Jerry Yu30b071c2021-09-12 20:16:03 +0800433 md_alg = MBEDTLS_MD_SHA512;
434 sig_alg = MBEDTLS_PK_ECDSA;
435 break;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000436#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Xiaofei Bai6dc90da2021-11-26 08:11:40 +0000437 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
XiaokangQiana83014d2021-11-22 07:53:34 +0000438 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Certificate Verify: using RSA PSS" ) );
XiaokangQian82d34cc2021-11-03 08:51:56 +0000439 md_alg = MBEDTLS_MD_SHA256;
440 sig_alg = MBEDTLS_PK_RSASSA_PSS;
441 break;
442#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800443 default:
444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Certificate Verify: Unknown signature algorithm." ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800445 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800446 }
447
448 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate Verify: Signature algorithm ( %04x )",
449 ( unsigned int ) algorithm ) );
450
451 /*
452 * Check the certificate's key type matches the signature alg
453 */
454 if( !mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk, sig_alg ) )
455 {
456 MBEDTLS_SSL_DEBUG_MSG( 1, ( "signature algorithm doesn't match cert key" ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800457 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800458 }
459
460 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
461 signature_len = MBEDTLS_GET_UINT16_BE( p, 0 );
462 p += 2;
463 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, signature_len );
464
465 /* Hash verify buffer with indicated hash function */
466 switch( md_alg )
467 {
468#if defined(MBEDTLS_SHA256_C)
469 case MBEDTLS_MD_SHA256:
470 verify_hash_len = 32;
Jerry Yu133690c2021-10-25 14:01:13 +0800471 ret = mbedtls_sha256( verify_buffer, verify_buffer_len, verify_hash, 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800472 break;
473#endif /* MBEDTLS_SHA256_C */
474
475#if defined(MBEDTLS_SHA384_C)
476 case MBEDTLS_MD_SHA384:
477 verify_hash_len = 48;
Jerry Yu133690c2021-10-25 14:01:13 +0800478 ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 1 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800479 break;
480#endif /* MBEDTLS_SHA384_C */
481
482#if defined(MBEDTLS_SHA512_C)
483 case MBEDTLS_MD_SHA512:
484 verify_hash_len = 64;
Jerry Yu133690c2021-10-25 14:01:13 +0800485 ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800486 break;
487#endif /* MBEDTLS_SHA512_C */
488
Jerry Yu0b32c502021-10-28 13:41:59 +0800489 default:
490 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
491 break;
Jerry Yu30b071c2021-09-12 20:16:03 +0800492 }
493
Jerry Yu133690c2021-10-25 14:01:13 +0800494 if( ret != 0 )
495 {
496 MBEDTLS_SSL_DEBUG_RET( 1, "hash computation error", ret );
Jerry Yu6f87f252021-10-29 20:12:51 +0800497 goto error;
Jerry Yu133690c2021-10-25 14:01:13 +0800498 }
499
Jerry Yu30b071c2021-09-12 20:16:03 +0800500 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
XiaokangQian82d34cc2021-11-03 08:51:56 +0000501#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
502 if( sig_alg == MBEDTLS_PK_RSASSA_PSS )
503 {
504 const mbedtls_md_info_t* md_info;
Xiaofei Baid25fab62021-12-02 06:36:27 +0000505 rsassa_pss_options.mgf1_hash_id = md_alg;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000506 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
507 {
508 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
509 }
Xiaofei Baid25fab62021-12-02 06:36:27 +0000510 rsassa_pss_options.expected_salt_len = mbedtls_md_get_size( md_info );
511 options = (const void*) &rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000512 }
513#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800514
Xiaofei Baid25fab62021-12-02 06:36:27 +0000515 if( ( ret = mbedtls_pk_verify_ext( sig_alg, options,
Jerry Yu30b071c2021-09-12 20:16:03 +0800516 &ssl->session_negotiate->peer_cert->pk,
517 md_alg, verify_hash, verify_hash_len,
Jerry Yu6f87f252021-10-29 20:12:51 +0800518 p, signature_len ) ) == 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800519 {
Jerry Yu6f87f252021-10-29 20:12:51 +0800520 return( 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800521 }
Jerry Yu6f87f252021-10-29 20:12:51 +0800522 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify_ext", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800523
Jerry Yu6f87f252021-10-29 20:12:51 +0800524error:
525 /* RFC 8446 section 4.4.3
526 *
527 * If the verification fails, the receiver MUST terminate the handshake
528 * with a "decrypt_error" alert.
529 */
530 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
531 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
532 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
533
Jerry Yu30b071c2021-09-12 20:16:03 +0800534}
535#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
536
537int mbedtls_ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
538{
Jerry Yu30b071c2021-09-12 20:16:03 +0800539
Jerry Yuda8cdf22021-10-25 15:06:49 +0800540#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
541 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
542 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
543 size_t verify_buffer_len;
544 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
545 size_t transcript_len;
546 unsigned char *buf;
547 size_t buf_len;
548
Jerry Yu30b071c2021-09-12 20:16:03 +0800549 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
550
Jerry Yuda8cdf22021-10-25 15:06:49 +0800551 MBEDTLS_SSL_PROC_CHK(
Xiaofei Bai746f9482021-11-12 08:53:56 +0000552 mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
Jerry Yuda8cdf22021-10-25 15:06:49 +0800553 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu30b071c2021-09-12 20:16:03 +0800554
Jerry Yuda8cdf22021-10-25 15:06:49 +0800555 /* Need to calculate the hash of the transcript first
Jerry Yu0b32c502021-10-28 13:41:59 +0800556 * before reading the message since otherwise it gets
557 * included in the transcript
558 */
Jerry Yuda8cdf22021-10-25 15:06:49 +0800559 ret = mbedtls_ssl_get_handshake_transcript( ssl,
560 ssl->handshake->ciphersuite_info->mac,
561 transcript, sizeof( transcript ),
562 &transcript_len );
563 if( ret != 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800564 {
Jerry Yuda8cdf22021-10-25 15:06:49 +0800565 MBEDTLS_SSL_PEND_FATAL_ALERT(
566 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
567 MBEDTLS_ERR_SSL_INTERNAL_ERROR );
568 return( ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800569 }
570
Jerry Yuda8cdf22021-10-25 15:06:49 +0800571 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash", transcript, transcript_len );
572
573 /* Create verify structure */
574 ssl_tls13_create_verify_structure( transcript,
Jerry Yu0b32c502021-10-28 13:41:59 +0800575 transcript_len,
576 verify_buffer,
577 &verify_buffer_len,
578 ( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) ?
579 MBEDTLS_SSL_IS_SERVER :
580 MBEDTLS_SSL_IS_CLIENT );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800581
582 /* Process the message contents */
Jerry Yu0b32c502021-10-28 13:41:59 +0800583 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_verify( ssl, buf,
584 buf + buf_len, verify_buffer, verify_buffer_len ) );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800585
Xiaofei Bai746f9482021-11-12 08:53:56 +0000586 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
Jerry Yuda8cdf22021-10-25 15:06:49 +0800587 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, buf, buf_len );
Jerry Yu30b071c2021-09-12 20:16:03 +0800588
589cleanup:
590
591 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
Jerry Yu5398c102021-11-05 13:32:38 +0800592 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_process_certificate_verify", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800593 return( ret );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800594#else
595 ((void) ssl);
596 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
597 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
598#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800599}
600
601/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000602 *
603 * STATE HANDLING: Incoming Certificate, client-side only currently.
604 *
605 */
606
607/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000608 * Implementation
609 */
610
Xiaofei Bai947571e2021-09-29 09:12:03 +0000611#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
612#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
613/*
614 * Structure of Certificate message:
615 *
616 * enum {
617 * X509(0),
618 * RawPublicKey(2),
619 * (255)
620 * } CertificateType;
621 *
622 * struct {
623 * select (certificate_type) {
624 * case RawPublicKey:
625 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
626 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
627 * case X509:
628 * opaque cert_data<1..2^24-1>;
629 * };
630 * Extension extensions<0..2^16-1>;
631 * } CertificateEntry;
632 *
633 * struct {
634 * opaque certificate_request_context<0..2^8-1>;
635 * CertificateEntry certificate_list<0..2^24-1>;
636 * } Certificate;
637 *
638 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000639
640/* Parse certificate chain send by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000641static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
642 const unsigned char *buf,
643 const unsigned char *end )
644{
645 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +0000646 size_t certificate_request_context_len = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000647 size_t certificate_list_len = 0;
648 const unsigned char *p = buf;
649 const unsigned char *certificate_list_end;
650
651 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
Xiaofei Baia0ab7772022-01-16 12:14:45 +0000652 certificate_request_context_len = p[0];
Jerry Yub640bf62021-10-29 10:05:32 +0800653 certificate_list_len = MBEDTLS_GET_UINT24_BE( p, 1 );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000654 p += 4;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000655
656 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
657 * support anything beyond 2^16 = 64K.
658 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +0000659 if( ( certificate_request_context_len != 0 ) ||
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000660 ( certificate_list_len >= 0x10000 ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000661 {
662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
663 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
664 MBEDTLS_ERR_SSL_DECODE_ERROR );
665 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
666 }
667
668 /* In case we tried to reuse a session but it failed */
669 if( ssl->session_negotiate->peer_cert != NULL )
670 {
671 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
672 mbedtls_free( ssl->session_negotiate->peer_cert );
673 }
674
675 if( ( ssl->session_negotiate->peer_cert =
676 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
677 {
678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
679 sizeof( mbedtls_x509_crt ) ) );
680 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
681 MBEDTLS_ERR_SSL_ALLOC_FAILED );
682 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
683 }
684
685 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
686
Xiaofei Bai947571e2021-09-29 09:12:03 +0000687 certificate_list_end = p + certificate_list_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000688 while( p < certificate_list_end )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000689 {
690 size_t cert_data_len, extensions_len;
691
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000692 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 3 );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000693 cert_data_len = MBEDTLS_GET_UINT24_BE( p, 0 );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000694 p += 3;
695
696 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
697 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
698 * check that we have a minimum of 128 bytes of data, this is not
699 * clear why we need that though.
700 */
701 if( ( cert_data_len < 128 ) || ( cert_data_len >= 0x10000 ) )
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000702 {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000703 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
704 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
705 MBEDTLS_ERR_SSL_DECODE_ERROR );
706 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
707 }
708
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000709 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, cert_data_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000710 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
711 p, cert_data_len );
712
713 switch( ret )
714 {
715 case 0: /*ok*/
716 break;
717 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
718 /* Ignore certificate with an unknown algorithm: maybe a
719 prior certificate was already trusted. */
720 break;
721
722 case MBEDTLS_ERR_X509_ALLOC_FAILED:
723 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
724 MBEDTLS_ERR_X509_ALLOC_FAILED );
725 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
726 return( ret );
727
728 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
729 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
730 MBEDTLS_ERR_X509_UNKNOWN_VERSION );
731 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
732 return( ret );
733
734 default:
735 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
736 ret );
737 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
738 return( ret );
739 }
740
741 p += cert_data_len;
742
743 /* Certificate extensions length */
744 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 2 );
745 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
746 p += 2;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000747 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, extensions_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000748 p += extensions_len;
749 }
750
751 /* Check that all the message is consumed. */
752 if( p != end )
753 {
754 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
755 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, \
756 MBEDTLS_ERR_SSL_DECODE_ERROR );
757 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
758 }
759
760 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
761
762 return( ret );
763}
764#else
765static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
766 const unsigned char *buf,
767 const unsigned char *end )
768{
769 ((void) ssl);
770 ((void) buf);
771 ((void) end);
772 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
773}
774#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
775#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
776
777#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
778#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000779/* Validate certificate chain sent by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000780static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
781{
782 int ret = 0;
783 mbedtls_x509_crt *ca_chain;
784 mbedtls_x509_crl *ca_crl;
Xiaofei Baiff456022021-10-28 06:50:17 +0000785 uint32_t verify_result = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000786
787#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
788 if( ssl->handshake->sni_ca_chain != NULL )
789 {
790 ca_chain = ssl->handshake->sni_ca_chain;
791 ca_crl = ssl->handshake->sni_ca_crl;
792 }
793 else
794#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
795 {
796 ca_chain = ssl->conf->ca_chain;
797 ca_crl = ssl->conf->ca_crl;
798 }
799
800 /*
801 * Main check: verify certificate
802 */
803 ret = mbedtls_x509_crt_verify_with_profile(
804 ssl->session_negotiate->peer_cert,
805 ca_chain, ca_crl,
806 ssl->conf->cert_profile,
807 ssl->hostname,
Xiaofei Baiff456022021-10-28 06:50:17 +0000808 &verify_result,
Xiaofei Bai947571e2021-09-29 09:12:03 +0000809 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
810
811 if( ret != 0 )
812 {
813 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
814 }
815
816 /*
817 * Secondary checks: always done, but change 'ret' only if it was 0
818 */
819
820#if defined(MBEDTLS_ECP_C)
821 {
822 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
823
824 /* If certificate uses an EC key, make sure the curve is OK */
825 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
826 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
827 {
Xiaofei Baiff456022021-10-28 06:50:17 +0000828 verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000829
830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( EC key curve )" ) );
831 if( ret == 0 )
832 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
833 }
834 }
835#endif /* MBEDTLS_ECP_C */
836
837 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
838 ssl->handshake->ciphersuite_info,
839 !ssl->conf->endpoint,
Xiaofei Baiff456022021-10-28 06:50:17 +0000840 &verify_result ) != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000841 {
842 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( usage extensions )" ) );
843 if( ret == 0 )
844 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
845 }
846
847
848 if( ca_chain == NULL )
849 {
850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
851 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
852 }
853
854 if( ret != 0 )
855 {
856 /* The certificate may have been rejected for several reasons.
857 Pick one and send the corresponding alert. Which alert to send
858 may be a subject of debate in some cases. */
Xiaofei Baiff456022021-10-28 06:50:17 +0000859 if( verify_result & MBEDTLS_X509_BADCERT_OTHER )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000860 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000861 else if( verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000862 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000863 else if( verify_result & ( MBEDTLS_X509_BADCERT_KEY_USAGE |
864 MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
865 MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
866 MBEDTLS_X509_BADCERT_BAD_PK |
867 MBEDTLS_X509_BADCERT_BAD_KEY ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000868 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000869 else if( verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000870 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000871 else if( verify_result & MBEDTLS_X509_BADCERT_REVOKED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000872 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000873 else if( verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000874 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret );
875 else
876 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret );
877 }
878
879#if defined(MBEDTLS_DEBUG_C)
Xiaofei Baiff456022021-10-28 06:50:17 +0000880 if( verify_result != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000881 {
882 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
Jerry Yu83bb1312021-10-28 22:16:33 +0800883 (unsigned int) verify_result ) );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000884 }
885 else
886 {
887 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
888 }
889#endif /* MBEDTLS_DEBUG_C */
890
Xiaofei Baiff456022021-10-28 06:50:17 +0000891 ssl->session_negotiate->verify_result = verify_result;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000892 return( ret );
893}
894#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
895static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
896{
897 ((void) ssl);
898 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
899}
900#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
901#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
902
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000903int mbedtls_ssl_tls13_process_certificate( mbedtls_ssl_context *ssl )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000904{
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000905 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
906 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
907
908#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
909 unsigned char *buf;
910 size_t buf_len;
911
Xiaofei Bai746f9482021-11-12 08:53:56 +0000912 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000913 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
914 &buf, &buf_len ) );
915
916 /* Parse the certificate chain sent by the peer. */
917 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate( ssl, buf, buf + buf_len ) );
918 /* Validate the certificate chain and set the verification results. */
919 MBEDTLS_SSL_PROC_CHK( ssl_tls13_validate_certificate( ssl ) );
920
Xiaofei Bai746f9482021-11-12 08:53:56 +0000921 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
922 buf, buf_len );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000923
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000924cleanup:
925
926 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Xiaofei Bai10aeec02021-10-26 09:50:08 +0000927#else
928 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
929 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
930#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000931 return( ret );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000932}
933
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000934/*
935 *
XiaokangQianc5c39d52021-11-09 11:55:10 +0000936 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000937 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000938/*
939 * Implementation
940 */
941
XiaokangQianaaa0e192021-11-10 03:07:04 +0000942static int ssl_tls13_preprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000943{
944 int ret;
945
XiaokangQianc5c39d52021-11-09 11:55:10 +0000946 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000947 ssl->handshake->state_local.finished_in.digest,
948 sizeof( ssl->handshake->state_local.finished_in.digest ),
949 &ssl->handshake->state_local.finished_in.digest_len,
XiaokangQianc5c39d52021-11-09 11:55:10 +0000950 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
XiaokangQianc13f9352021-11-11 06:13:22 +0000951 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000952 if( ret != 0 )
953 {
XiaokangQianc5c39d52021-11-09 11:55:10 +0000954 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_calculate_verify_data", ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000955 return( ret );
956 }
957
958 return( 0 );
959}
960
XiaokangQianc5c39d52021-11-09 11:55:10 +0000961static int ssl_tls13_parse_finished_message( mbedtls_ssl_context *ssl,
962 const unsigned char *buf,
963 const unsigned char *end )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000964{
XiaokangQian33062842021-11-11 03:37:45 +0000965 /*
966 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +0000967 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +0000968 * } Finished;
969 */
970 const unsigned char *expected_verify_data =
971 ssl->handshake->state_local.finished_in.digest;
972 size_t expected_verify_data_len =
973 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000974 /* Structural validation */
XiaokangQian33062842021-11-11 03:37:45 +0000975 if( (size_t)( end - buf ) != expected_verify_data_len )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000976 {
977 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
978
979 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +0000980 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000981 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
982 }
983
XiaokangQianc5c39d52021-11-09 11:55:10 +0000984 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (self-computed):",
XiaokangQian33062842021-11-11 03:37:45 +0000985 expected_verify_data,
986 expected_verify_data_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +0000987 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (received message):", buf,
XiaokangQian33062842021-11-11 03:37:45 +0000988 expected_verify_data_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000989
990 /* Semantic validation */
Gabor Mezei685472b2021-11-24 11:17:36 +0100991 if( mbedtls_ct_memcmp( buf,
992 expected_verify_data,
993 expected_verify_data_len ) != 0 )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000994 {
995 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
996
XiaokangQian33062842021-11-11 03:37:45 +0000997 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +0000998 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000999 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1000 }
1001 return( 0 );
1002}
1003
XiaokangQianc13f9352021-11-11 06:13:22 +00001004#if defined(MBEDTLS_SSL_CLI_C)
XiaokangQianaaa0e192021-11-10 03:07:04 +00001005static int ssl_tls13_postprocess_server_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001006{
XiaokangQian33062842021-11-11 03:37:45 +00001007 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001008 mbedtls_ssl_key_set traffic_keys;
XiaokangQian1aef02e2021-10-28 09:54:34 +00001009 mbedtls_ssl_transform *transform_application = NULL;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001010
XiaokangQian4cab0242021-10-12 08:43:37 +00001011 ret = mbedtls_ssl_tls13_key_schedule_stage_application( ssl );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001012 if( ret != 0 )
1013 {
1014 MBEDTLS_SSL_DEBUG_RET( 1,
XiaokangQian4cab0242021-10-12 08:43:37 +00001015 "mbedtls_ssl_tls13_key_schedule_stage_application", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001016 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001017 }
1018
XiaokangQian33062842021-11-11 03:37:45 +00001019 ret = mbedtls_ssl_tls13_generate_application_keys( ssl, &traffic_keys );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001020 if( ret != 0 )
1021 {
1022 MBEDTLS_SSL_DEBUG_RET( 1,
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001023 "mbedtls_ssl_tls13_generate_application_keys", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001024 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001025 }
1026
1027 transform_application =
1028 mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
1029 if( transform_application == NULL )
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001030 {
1031 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1032 goto cleanup;
1033 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001034
1035 ret = mbedtls_ssl_tls13_populate_transform(
1036 transform_application,
1037 ssl->conf->endpoint,
1038 ssl->session_negotiate->ciphersuite,
1039 &traffic_keys,
1040 ssl );
1041 if( ret != 0 )
1042 {
1043 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001044 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001045 }
1046
1047 ssl->transform_application = transform_application;
1048
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001049cleanup:
1050
XiaokangQian33062842021-11-11 03:37:45 +00001051 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001052 if( ret != 0 )
XiaokangQian1aef02e2021-10-28 09:54:34 +00001053 {
1054 mbedtls_free( transform_application );
1055 MBEDTLS_SSL_PEND_FATAL_ALERT(
1056 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1057 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1058 }
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001059 return( ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001060}
XiaokangQianc13f9352021-11-11 06:13:22 +00001061#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001062
XiaokangQiancc90c942021-11-09 12:30:09 +00001063static int ssl_tls13_postprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001064{
1065
XiaokangQianc13f9352021-11-11 06:13:22 +00001066#if defined(MBEDTLS_SSL_CLI_C)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001067 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
1068 {
XiaokangQianaaa0e192021-11-10 03:07:04 +00001069 return( ssl_tls13_postprocess_server_finished_message( ssl ) );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001070 }
XiaokangQianc13f9352021-11-11 06:13:22 +00001071#else
1072 ((void) ssl);
1073#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001074
1075 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1076}
1077
XiaokangQianc5c39d52021-11-09 11:55:10 +00001078int mbedtls_ssl_tls13_process_finished_message( mbedtls_ssl_context *ssl )
1079{
XiaokangQian33062842021-11-11 03:37:45 +00001080 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001081 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +00001082 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001083
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001084 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001085
1086 /* Preprocessing step: Compute handshake digest */
XiaokangQianaaa0e192021-11-10 03:07:04 +00001087 MBEDTLS_SSL_PROC_CHK( ssl_tls13_preprocess_finished_message( ssl ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001088
Xiaofei Bai746f9482021-11-12 08:53:56 +00001089 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001090 MBEDTLS_SSL_HS_FINISHED,
Xiaofei Baieef15042021-11-18 07:29:56 +00001091 &buf, &buf_len ) );
1092 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_finished_message( ssl, buf, buf + buf_len ) );
Xiaofei Bai746f9482021-11-12 08:53:56 +00001093 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
Xiaofei Baieef15042021-11-18 07:29:56 +00001094 ssl, MBEDTLS_SSL_HS_FINISHED, buf, buf_len );
XiaokangQianaaa0e192021-11-10 03:07:04 +00001095 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_finished_message( ssl ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001096
1097cleanup:
1098
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001099 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001100 return( ret );
1101}
1102
XiaokangQian74af2a82021-09-22 07:40:30 +00001103/*
1104 *
XiaokangQiancc90c942021-11-09 12:30:09 +00001105 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +00001106 *
1107 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001108/*
XiaokangQian35dc6252021-11-11 08:16:19 +00001109 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +00001110 */
1111
XiaokangQian8773aa02021-11-10 07:33:09 +00001112static int ssl_tls13_prepare_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +00001113{
1114 int ret;
1115
1116 /* Compute transcript of handshake up to now. */
XiaokangQiancc90c942021-11-09 12:30:09 +00001117 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQian74af2a82021-09-22 07:40:30 +00001118 ssl->handshake->state_local.finished_out.digest,
1119 sizeof( ssl->handshake->state_local.finished_out.digest ),
1120 &ssl->handshake->state_local.finished_out.digest_len,
1121 ssl->conf->endpoint );
1122
1123 if( ret != 0 )
1124 {
Jerry Yu7ca30542021-12-08 15:57:57 +08001125 MBEDTLS_SSL_DEBUG_RET( 1, "calculate_verify_data failed", ret );
XiaokangQian74af2a82021-09-22 07:40:30 +00001126 return( ret );
1127 }
1128
1129 return( 0 );
1130}
1131
XiaokangQiancc90c942021-11-09 12:30:09 +00001132static int ssl_tls13_finalize_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +00001133{
XiaokangQian0fa66432021-11-15 03:33:57 +00001134 // TODO: Add back resumption keys calculation after MVP.
1135 ((void) ssl);
XiaokangQian74af2a82021-09-22 07:40:30 +00001136
1137 return( 0 );
1138}
1139
XiaokangQian8773aa02021-11-10 07:33:09 +00001140static int ssl_tls13_write_finished_message_body( mbedtls_ssl_context *ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001141 unsigned char *buf,
1142 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001143 size_t *out_len )
XiaokangQian74af2a82021-09-22 07:40:30 +00001144{
XiaokangQian8773aa02021-11-10 07:33:09 +00001145 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001146 /*
1147 * struct {
1148 * opaque verify_data[Hash.length];
1149 * } Finished;
1150 */
XiaokangQian8773aa02021-11-10 07:33:09 +00001151 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001152
1153 memcpy( buf, ssl->handshake->state_local.finished_out.digest,
XiaokangQian8773aa02021-11-10 07:33:09 +00001154 verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001155
Xiaofei Baid25fab62021-12-02 06:36:27 +00001156 *out_len = verify_data_len;
XiaokangQian74af2a82021-09-22 07:40:30 +00001157 return( 0 );
1158}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001159
XiaokangQian35dc6252021-11-11 08:16:19 +00001160/* Main entry point: orchestrates the other functions */
1161int mbedtls_ssl_tls13_write_finished_message( mbedtls_ssl_context *ssl )
1162{
1163 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1164 unsigned char *buf;
1165 size_t buf_len, msg_len;
1166
1167 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished message" ) );
1168
XiaokangQiandce82242021-11-15 06:01:26 +00001169 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_finished_message( ssl ) );
1170
XiaokangQian35dc6252021-11-11 08:16:19 +00001171 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg( ssl,
1172 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len ) );
1173
1174 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_finished_message_body(
1175 ssl, buf, buf + buf_len, &msg_len ) );
1176
Xiaofei Bai746f9482021-11-12 08:53:56 +00001177 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1178 buf, msg_len );
XiaokangQian35dc6252021-11-11 08:16:19 +00001179
1180 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_finished_message( ssl ) );
1181 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
1182 buf_len, msg_len ) );
1183 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_flush_output( ssl ) );
1184
1185cleanup:
1186
1187 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished message" ) );
1188 return( ret );
1189}
1190
Jerry Yu378254d2021-10-30 21:44:47 +08001191void mbedtls_ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
1192{
1193
1194 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
1195
1196 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001197 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001198 */
1199 if( ssl->session )
1200 {
Jerry Yu378254d2021-10-30 21:44:47 +08001201 mbedtls_ssl_session_free( ssl->session );
1202 mbedtls_free( ssl->session );
1203 }
1204 ssl->session = ssl->session_negotiate;
1205 ssl->session_negotiate = NULL;
1206
1207 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
1208}
1209
Ronald Cron49ad6192021-11-24 16:25:31 +01001210/*
1211 *
1212 * STATE HANDLING: Write ChangeCipherSpec
1213 *
1214 */
1215#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1216
1217static int ssl_tls13_write_change_cipher_spec_body( mbedtls_ssl_context *ssl,
1218 unsigned char *buf,
1219 unsigned char *end,
1220 size_t *olen )
1221{
1222 ((void) ssl);
1223
1224 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 1 );
1225 buf[0] = 1;
1226 *olen = 1;
1227
1228 return( 0 );
1229}
1230
Ronald Cron49ad6192021-11-24 16:25:31 +01001231int mbedtls_ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1232{
1233 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1234
1235 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
1236
1237 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_flush_output( ssl ) );
1238
1239 /* Write CCS message */
1240 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_change_cipher_spec_body(
1241 ssl, ssl->out_msg,
1242 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1243 &ssl->out_msglen ) );
1244
1245 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1246
Ronald Cron49ad6192021-11-24 16:25:31 +01001247 /* Dispatch message */
1248 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_record( ssl, 1 ) );
1249
1250cleanup:
1251
1252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
1253 return( ret );
1254}
1255
1256#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1257
Ronald Cron6f135e12021-12-08 16:57:54 +01001258#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001259
1260#endif /* MBEDTLS_SSL_TLS_C */