blob: e174d639848bd6307c92046571adf3c7a725ac3e [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 Bai69fcd392022-01-20 08:25:00 +0000161 uint32_t common_idx = 0; /* iterate through received signature schemes list */
162
163 /* skip 2 bytes of signature algorithms length */
164 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000165 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000166 p += 2;
167
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000168 memset( ssl->handshake->received_sig_algs, 0,
Xiaofei Bai51f515a2022-02-08 07:28:04 +0000169 sizeof(ssl->handshake->received_sig_algs) );
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000170
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000171 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
172 supported_sig_algs_end = p + supported_sig_algs_len;
Xiaofei Bai51f515a2022-02-08 07:28:04 +0000173 while( p < supported_sig_algs_end )
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000174 {
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000175 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +0000176 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000177 p += 2;
178
179 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x",
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +0000180 sig_alg ) );
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000181
Xiaofei Bai51f515a2022-02-08 07:28:04 +0000182 if( ! mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ||
183 ! mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) )
184 continue;
185
186 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000187 {
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000188 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +0000189 common_idx += 1;
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000190 }
191 }
192 /* Check that we consumed all the message. */
193 if( p != end )
194 {
195 MBEDTLS_SSL_DEBUG_MSG( 1,
196 ( "Signature algorithms extension length misaligned" ) );
197 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
198 MBEDTLS_ERR_SSL_DECODE_ERROR );
199 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
200 }
201
202 if( common_idx == 0 )
203 {
204 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
205 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
206 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
207 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
208 }
209
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000210 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS1_3_SIG_NONE;
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000211 return( 0 );
212}
213
Jerry Yu30b071c2021-09-12 20:16:03 +0800214/*
Jerry Yu30b071c2021-09-12 20:16:03 +0800215 * STATE HANDLING: Read CertificateVerify
216 */
Jerry Yud0fc5852021-10-29 11:09:06 +0800217/* Macro to express the maximum length of the verify structure.
Jerry Yu30b071c2021-09-12 20:16:03 +0800218 *
219 * The structure is computed per TLS 1.3 specification as:
220 * - 64 bytes of octet 32,
221 * - 33 bytes for the context string
222 * (which is either "TLS 1.3, client CertificateVerify"
223 * or "TLS 1.3, server CertificateVerify"),
Jerry Yud0fc5852021-10-29 11:09:06 +0800224 * - 1 byte for the octet 0x0, which serves as a separator,
Jerry Yu30b071c2021-09-12 20:16:03 +0800225 * - 32 or 48 bytes for the Transcript-Hash(Handshake Context, Certificate)
226 * (depending on the size of the transcript_hash)
227 *
228 * This results in a total size of
229 * - 130 bytes for a SHA256-based transcript hash, or
230 * (64 + 33 + 1 + 32 bytes)
231 * - 146 bytes for a SHA384-based transcript hash.
232 * (64 + 33 + 1 + 48 bytes)
233 *
234 */
Jerry Yu26c2d112021-10-25 12:42:58 +0800235#define SSL_VERIFY_STRUCT_MAX_SIZE ( 64 + \
236 33 + \
237 1 + \
238 MBEDTLS_TLS1_3_MD_MAX_SIZE \
Jerry Yu30b071c2021-09-12 20:16:03 +0800239 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800240
Jerry Yu0b32c502021-10-28 13:41:59 +0800241/*
242 * The ssl_tls13_create_verify_structure() creates the verify structure.
243 * As input, it requires the transcript hash.
244 *
245 * The caller has to ensure that the buffer has size at least
246 * SSL_VERIFY_STRUCT_MAX_SIZE bytes.
247 */
Jerry Yud0fc5852021-10-29 11:09:06 +0800248static void ssl_tls13_create_verify_structure( const unsigned char *transcript_hash,
Jerry Yu0b32c502021-10-28 13:41:59 +0800249 size_t transcript_hash_len,
250 unsigned char *verify_buffer,
251 size_t *verify_buffer_len,
252 int from )
253{
254 size_t idx;
Jerry Yu30b071c2021-09-12 20:16:03 +0800255
Jerry Yu0b32c502021-10-28 13:41:59 +0800256 /* RFC 8446, Section 4.4.3:
257 *
258 * The digital signature [in the CertificateVerify message] is then
259 * computed over the concatenation of:
260 * - A string that consists of octet 32 (0x20) repeated 64 times
261 * - The context string
262 * - A single 0 byte which serves as the separator
263 * - The content to be signed
264 */
265 memset( verify_buffer, 0x20, 64 );
266 idx = 64;
267
268 if( from == MBEDTLS_SSL_IS_CLIENT )
269 {
270 memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( client_cv ) );
271 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( client_cv );
272 }
273 else
274 { /* from == MBEDTLS_SSL_IS_SERVER */
275 memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( server_cv ) );
276 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( server_cv );
277 }
278
279 verify_buffer[idx++] = 0x0;
280
281 memcpy( verify_buffer + idx, transcript_hash, transcript_hash_len );
282 idx += transcript_hash_len;
283
284 *verify_buffer_len = idx;
285}
286
Jerry Yu0b32c502021-10-28 13:41:59 +0800287static int ssl_tls13_parse_certificate_verify( mbedtls_ssl_context *ssl,
Jerry Yud0fc5852021-10-29 11:09:06 +0800288 const unsigned char *buf,
289 const unsigned char *end,
290 const unsigned char *verify_buffer,
291 size_t verify_buffer_len )
Jerry Yu30b071c2021-09-12 20:16:03 +0800292{
293 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
294 const unsigned char *p = buf;
295 uint16_t algorithm;
Jerry Yu30b071c2021-09-12 20:16:03 +0800296 size_t signature_len;
297 mbedtls_pk_type_t sig_alg;
298 mbedtls_md_type_t md_alg;
Jerry Yud0fc5852021-10-29 11:09:06 +0800299 unsigned char verify_hash[MBEDTLS_MD_MAX_SIZE];
Jerry Yu30b071c2021-09-12 20:16:03 +0800300 size_t verify_hash_len;
301
Xiaofei Baid25fab62021-12-02 06:36:27 +0000302 void const *options = NULL;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000303#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Xiaofei Baid25fab62021-12-02 06:36:27 +0000304 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000305#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
306
Jerry Yu30b071c2021-09-12 20:16:03 +0800307 /*
308 * struct {
309 * SignatureScheme algorithm;
310 * opaque signature<0..2^16-1>;
311 * } CertificateVerify;
312 */
313 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
314 algorithm = MBEDTLS_GET_UINT16_BE( p, 0 );
315 p += 2;
316
317 /* RFC 8446 section 4.4.3
318 *
319 * If the CertificateVerify message is sent by a server, the signature algorithm
320 * MUST be one offered in the client's "signature_algorithms" extension unless
321 * no valid certificate chain can be produced without unsupported algorithms
322 *
323 * RFC 8446 section 4.4.2.2
324 *
325 * If the client cannot construct an acceptable chain using the provided
326 * certificates and decides to abort the handshake, then it MUST abort the handshake
327 * with an appropriate certificate-related alert (by default, "unsupported_certificate").
328 *
Jerry Yu6f87f252021-10-29 20:12:51 +0800329 * Check if algorithm is an offered signature algorithm.
Jerry Yu30b071c2021-09-12 20:16:03 +0800330 */
Jerry Yu24811fb2022-01-19 18:02:15 +0800331 if( ! mbedtls_ssl_sig_alg_is_offered( ssl, algorithm ) )
Jerry Yu30b071c2021-09-12 20:16:03 +0800332 {
Jerry Yu982d9e52021-10-14 15:59:37 +0800333 /* algorithm not in offered signature algorithms list */
334 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Received signature algorithm(%04x) is not "
335 "offered.",
336 ( unsigned int ) algorithm ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800337 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800338 }
339
340 /* We currently only support ECDSA-based signatures */
341 switch( algorithm )
342 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000343 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Jerry Yu30b071c2021-09-12 20:16:03 +0800344 md_alg = MBEDTLS_MD_SHA256;
345 sig_alg = MBEDTLS_PK_ECDSA;
346 break;
Xiaofei Bai746f9482021-11-12 08:53:56 +0000347 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Jerry Yu30b071c2021-09-12 20:16:03 +0800348 md_alg = MBEDTLS_MD_SHA384;
349 sig_alg = MBEDTLS_PK_ECDSA;
350 break;
Xiaofei Bai746f9482021-11-12 08:53:56 +0000351 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Jerry Yu30b071c2021-09-12 20:16:03 +0800352 md_alg = MBEDTLS_MD_SHA512;
353 sig_alg = MBEDTLS_PK_ECDSA;
354 break;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000355#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Xiaofei Bai6dc90da2021-11-26 08:11:40 +0000356 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
XiaokangQiana83014d2021-11-22 07:53:34 +0000357 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Certificate Verify: using RSA PSS" ) );
XiaokangQian82d34cc2021-11-03 08:51:56 +0000358 md_alg = MBEDTLS_MD_SHA256;
359 sig_alg = MBEDTLS_PK_RSASSA_PSS;
360 break;
361#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800362 default:
363 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Certificate Verify: Unknown signature algorithm." ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800364 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800365 }
366
367 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate Verify: Signature algorithm ( %04x )",
368 ( unsigned int ) algorithm ) );
369
370 /*
371 * Check the certificate's key type matches the signature alg
372 */
373 if( !mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk, sig_alg ) )
374 {
375 MBEDTLS_SSL_DEBUG_MSG( 1, ( "signature algorithm doesn't match cert key" ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800376 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800377 }
378
379 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
380 signature_len = MBEDTLS_GET_UINT16_BE( p, 0 );
381 p += 2;
382 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, signature_len );
383
384 /* Hash verify buffer with indicated hash function */
385 switch( md_alg )
386 {
387#if defined(MBEDTLS_SHA256_C)
388 case MBEDTLS_MD_SHA256:
389 verify_hash_len = 32;
Jerry Yu133690c2021-10-25 14:01:13 +0800390 ret = mbedtls_sha256( verify_buffer, verify_buffer_len, verify_hash, 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800391 break;
392#endif /* MBEDTLS_SHA256_C */
393
394#if defined(MBEDTLS_SHA384_C)
395 case MBEDTLS_MD_SHA384:
396 verify_hash_len = 48;
Jerry Yu133690c2021-10-25 14:01:13 +0800397 ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 1 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800398 break;
399#endif /* MBEDTLS_SHA384_C */
400
401#if defined(MBEDTLS_SHA512_C)
402 case MBEDTLS_MD_SHA512:
403 verify_hash_len = 64;
Jerry Yu133690c2021-10-25 14:01:13 +0800404 ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800405 break;
406#endif /* MBEDTLS_SHA512_C */
407
Jerry Yu0b32c502021-10-28 13:41:59 +0800408 default:
409 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
410 break;
Jerry Yu30b071c2021-09-12 20:16:03 +0800411 }
412
Jerry Yu133690c2021-10-25 14:01:13 +0800413 if( ret != 0 )
414 {
415 MBEDTLS_SSL_DEBUG_RET( 1, "hash computation error", ret );
Jerry Yu6f87f252021-10-29 20:12:51 +0800416 goto error;
Jerry Yu133690c2021-10-25 14:01:13 +0800417 }
418
Jerry Yu30b071c2021-09-12 20:16:03 +0800419 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
XiaokangQian82d34cc2021-11-03 08:51:56 +0000420#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
421 if( sig_alg == MBEDTLS_PK_RSASSA_PSS )
422 {
423 const mbedtls_md_info_t* md_info;
Xiaofei Baid25fab62021-12-02 06:36:27 +0000424 rsassa_pss_options.mgf1_hash_id = md_alg;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000425 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
426 {
427 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
428 }
Xiaofei Baid25fab62021-12-02 06:36:27 +0000429 rsassa_pss_options.expected_salt_len = mbedtls_md_get_size( md_info );
430 options = (const void*) &rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000431 }
432#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800433
Xiaofei Baid25fab62021-12-02 06:36:27 +0000434 if( ( ret = mbedtls_pk_verify_ext( sig_alg, options,
Jerry Yu30b071c2021-09-12 20:16:03 +0800435 &ssl->session_negotiate->peer_cert->pk,
436 md_alg, verify_hash, verify_hash_len,
Jerry Yu6f87f252021-10-29 20:12:51 +0800437 p, signature_len ) ) == 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800438 {
Jerry Yu6f87f252021-10-29 20:12:51 +0800439 return( 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800440 }
Jerry Yu6f87f252021-10-29 20:12:51 +0800441 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify_ext", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800442
Jerry Yu6f87f252021-10-29 20:12:51 +0800443error:
444 /* RFC 8446 section 4.4.3
445 *
446 * If the verification fails, the receiver MUST terminate the handshake
447 * with a "decrypt_error" alert.
448 */
449 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
450 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
451 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
452
Jerry Yu30b071c2021-09-12 20:16:03 +0800453}
454#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
455
456int mbedtls_ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
457{
Jerry Yu30b071c2021-09-12 20:16:03 +0800458
Jerry Yuda8cdf22021-10-25 15:06:49 +0800459#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
460 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
461 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
462 size_t verify_buffer_len;
463 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
464 size_t transcript_len;
465 unsigned char *buf;
466 size_t buf_len;
467
Jerry Yu30b071c2021-09-12 20:16:03 +0800468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
469
Jerry Yuda8cdf22021-10-25 15:06:49 +0800470 MBEDTLS_SSL_PROC_CHK(
Xiaofei Bai746f9482021-11-12 08:53:56 +0000471 mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
Jerry Yuda8cdf22021-10-25 15:06:49 +0800472 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu30b071c2021-09-12 20:16:03 +0800473
Jerry Yuda8cdf22021-10-25 15:06:49 +0800474 /* Need to calculate the hash of the transcript first
Jerry Yu0b32c502021-10-28 13:41:59 +0800475 * before reading the message since otherwise it gets
476 * included in the transcript
477 */
Jerry Yuda8cdf22021-10-25 15:06:49 +0800478 ret = mbedtls_ssl_get_handshake_transcript( ssl,
479 ssl->handshake->ciphersuite_info->mac,
480 transcript, sizeof( transcript ),
481 &transcript_len );
482 if( ret != 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800483 {
Jerry Yuda8cdf22021-10-25 15:06:49 +0800484 MBEDTLS_SSL_PEND_FATAL_ALERT(
485 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
486 MBEDTLS_ERR_SSL_INTERNAL_ERROR );
487 return( ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800488 }
489
Jerry Yuda8cdf22021-10-25 15:06:49 +0800490 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash", transcript, transcript_len );
491
492 /* Create verify structure */
493 ssl_tls13_create_verify_structure( transcript,
Jerry Yu0b32c502021-10-28 13:41:59 +0800494 transcript_len,
495 verify_buffer,
496 &verify_buffer_len,
497 ( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) ?
498 MBEDTLS_SSL_IS_SERVER :
499 MBEDTLS_SSL_IS_CLIENT );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800500
501 /* Process the message contents */
Jerry Yu0b32c502021-10-28 13:41:59 +0800502 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_verify( ssl, buf,
503 buf + buf_len, verify_buffer, verify_buffer_len ) );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800504
Xiaofei Bai746f9482021-11-12 08:53:56 +0000505 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
Jerry Yuda8cdf22021-10-25 15:06:49 +0800506 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, buf, buf_len );
Jerry Yu30b071c2021-09-12 20:16:03 +0800507
508cleanup:
509
510 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
Jerry Yu5398c102021-11-05 13:32:38 +0800511 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_process_certificate_verify", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800512 return( ret );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800513#else
514 ((void) ssl);
515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
516 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
517#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800518}
519
520/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000521 *
522 * STATE HANDLING: Incoming Certificate, client-side only currently.
523 *
524 */
525
526/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000527 * Implementation
528 */
529
Xiaofei Bai947571e2021-09-29 09:12:03 +0000530#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
531#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
532/*
533 * Structure of Certificate message:
534 *
535 * enum {
536 * X509(0),
537 * RawPublicKey(2),
538 * (255)
539 * } CertificateType;
540 *
541 * struct {
542 * select (certificate_type) {
543 * case RawPublicKey:
544 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
545 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
546 * case X509:
547 * opaque cert_data<1..2^24-1>;
548 * };
549 * Extension extensions<0..2^16-1>;
550 * } CertificateEntry;
551 *
552 * struct {
553 * opaque certificate_request_context<0..2^8-1>;
554 * CertificateEntry certificate_list<0..2^24-1>;
555 * } Certificate;
556 *
557 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000558
559/* Parse certificate chain send by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000560static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
561 const unsigned char *buf,
562 const unsigned char *end )
563{
564 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +0000565 size_t certificate_request_context_len = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000566 size_t certificate_list_len = 0;
567 const unsigned char *p = buf;
568 const unsigned char *certificate_list_end;
569
570 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
Xiaofei Baia0ab7772022-01-16 12:14:45 +0000571 certificate_request_context_len = p[0];
Jerry Yub640bf62021-10-29 10:05:32 +0800572 certificate_list_len = MBEDTLS_GET_UINT24_BE( p, 1 );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000573 p += 4;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000574
575 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
576 * support anything beyond 2^16 = 64K.
577 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +0000578 if( ( certificate_request_context_len != 0 ) ||
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000579 ( certificate_list_len >= 0x10000 ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000580 {
581 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
582 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
583 MBEDTLS_ERR_SSL_DECODE_ERROR );
584 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
585 }
586
587 /* In case we tried to reuse a session but it failed */
588 if( ssl->session_negotiate->peer_cert != NULL )
589 {
590 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
591 mbedtls_free( ssl->session_negotiate->peer_cert );
592 }
593
594 if( ( ssl->session_negotiate->peer_cert =
595 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
596 {
597 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
598 sizeof( mbedtls_x509_crt ) ) );
599 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
600 MBEDTLS_ERR_SSL_ALLOC_FAILED );
601 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
602 }
603
604 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
605
Xiaofei Bai947571e2021-09-29 09:12:03 +0000606 certificate_list_end = p + certificate_list_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000607 while( p < certificate_list_end )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000608 {
609 size_t cert_data_len, extensions_len;
610
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000611 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 3 );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000612 cert_data_len = MBEDTLS_GET_UINT24_BE( p, 0 );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000613 p += 3;
614
615 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
616 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
617 * check that we have a minimum of 128 bytes of data, this is not
618 * clear why we need that though.
619 */
620 if( ( cert_data_len < 128 ) || ( cert_data_len >= 0x10000 ) )
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000621 {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000622 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
623 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
624 MBEDTLS_ERR_SSL_DECODE_ERROR );
625 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
626 }
627
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000628 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, cert_data_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000629 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
630 p, cert_data_len );
631
632 switch( ret )
633 {
634 case 0: /*ok*/
635 break;
636 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
637 /* Ignore certificate with an unknown algorithm: maybe a
638 prior certificate was already trusted. */
639 break;
640
641 case MBEDTLS_ERR_X509_ALLOC_FAILED:
642 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
643 MBEDTLS_ERR_X509_ALLOC_FAILED );
644 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
645 return( ret );
646
647 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
648 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
649 MBEDTLS_ERR_X509_UNKNOWN_VERSION );
650 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
651 return( ret );
652
653 default:
654 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
655 ret );
656 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
657 return( ret );
658 }
659
660 p += cert_data_len;
661
662 /* Certificate extensions length */
663 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 2 );
664 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
665 p += 2;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000666 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, extensions_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000667 p += extensions_len;
668 }
669
670 /* Check that all the message is consumed. */
671 if( p != end )
672 {
673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
674 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, \
675 MBEDTLS_ERR_SSL_DECODE_ERROR );
676 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
677 }
678
679 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
680
681 return( ret );
682}
683#else
684static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
685 const unsigned char *buf,
686 const unsigned char *end )
687{
688 ((void) ssl);
689 ((void) buf);
690 ((void) end);
691 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
692}
693#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
694#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
695
696#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
697#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000698/* Validate certificate chain sent by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000699static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
700{
701 int ret = 0;
702 mbedtls_x509_crt *ca_chain;
703 mbedtls_x509_crl *ca_crl;
Xiaofei Baiff456022021-10-28 06:50:17 +0000704 uint32_t verify_result = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000705
706#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
707 if( ssl->handshake->sni_ca_chain != NULL )
708 {
709 ca_chain = ssl->handshake->sni_ca_chain;
710 ca_crl = ssl->handshake->sni_ca_crl;
711 }
712 else
713#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
714 {
715 ca_chain = ssl->conf->ca_chain;
716 ca_crl = ssl->conf->ca_crl;
717 }
718
719 /*
720 * Main check: verify certificate
721 */
722 ret = mbedtls_x509_crt_verify_with_profile(
723 ssl->session_negotiate->peer_cert,
724 ca_chain, ca_crl,
725 ssl->conf->cert_profile,
726 ssl->hostname,
Xiaofei Baiff456022021-10-28 06:50:17 +0000727 &verify_result,
Xiaofei Bai947571e2021-09-29 09:12:03 +0000728 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
729
730 if( ret != 0 )
731 {
732 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
733 }
734
735 /*
736 * Secondary checks: always done, but change 'ret' only if it was 0
737 */
738
739#if defined(MBEDTLS_ECP_C)
740 {
741 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
742
743 /* If certificate uses an EC key, make sure the curve is OK */
744 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
745 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
746 {
Xiaofei Baiff456022021-10-28 06:50:17 +0000747 verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000748
749 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( EC key curve )" ) );
750 if( ret == 0 )
751 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
752 }
753 }
754#endif /* MBEDTLS_ECP_C */
755
756 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
757 ssl->handshake->ciphersuite_info,
758 !ssl->conf->endpoint,
Xiaofei Baiff456022021-10-28 06:50:17 +0000759 &verify_result ) != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000760 {
761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( usage extensions )" ) );
762 if( ret == 0 )
763 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
764 }
765
766
767 if( ca_chain == NULL )
768 {
769 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
770 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
771 }
772
773 if( ret != 0 )
774 {
775 /* The certificate may have been rejected for several reasons.
776 Pick one and send the corresponding alert. Which alert to send
777 may be a subject of debate in some cases. */
Xiaofei Baiff456022021-10-28 06:50:17 +0000778 if( verify_result & MBEDTLS_X509_BADCERT_OTHER )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000779 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000780 else if( verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000781 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000782 else if( verify_result & ( MBEDTLS_X509_BADCERT_KEY_USAGE |
783 MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
784 MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
785 MBEDTLS_X509_BADCERT_BAD_PK |
786 MBEDTLS_X509_BADCERT_BAD_KEY ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000787 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000788 else if( verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000789 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000790 else if( verify_result & MBEDTLS_X509_BADCERT_REVOKED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000791 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000792 else if( verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000793 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret );
794 else
795 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret );
796 }
797
798#if defined(MBEDTLS_DEBUG_C)
Xiaofei Baiff456022021-10-28 06:50:17 +0000799 if( verify_result != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000800 {
801 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
Jerry Yu83bb1312021-10-28 22:16:33 +0800802 (unsigned int) verify_result ) );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000803 }
804 else
805 {
806 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
807 }
808#endif /* MBEDTLS_DEBUG_C */
809
Xiaofei Baiff456022021-10-28 06:50:17 +0000810 ssl->session_negotiate->verify_result = verify_result;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000811 return( ret );
812}
813#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
814static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
815{
816 ((void) ssl);
817 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
818}
819#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
820#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
821
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000822int mbedtls_ssl_tls13_process_certificate( mbedtls_ssl_context *ssl )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000823{
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000824 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
826
827#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
828 unsigned char *buf;
829 size_t buf_len;
830
Xiaofei Bai746f9482021-11-12 08:53:56 +0000831 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000832 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
833 &buf, &buf_len ) );
834
835 /* Parse the certificate chain sent by the peer. */
836 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate( ssl, buf, buf + buf_len ) );
837 /* Validate the certificate chain and set the verification results. */
838 MBEDTLS_SSL_PROC_CHK( ssl_tls13_validate_certificate( ssl ) );
839
Xiaofei Bai746f9482021-11-12 08:53:56 +0000840 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
841 buf, buf_len );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000842
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000843cleanup:
844
845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Xiaofei Bai10aeec02021-10-26 09:50:08 +0000846#else
847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
848 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
849#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000850 return( ret );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000851}
852
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000853/*
854 *
XiaokangQianc5c39d52021-11-09 11:55:10 +0000855 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000856 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000857/*
858 * Implementation
859 */
860
XiaokangQianaaa0e192021-11-10 03:07:04 +0000861static int ssl_tls13_preprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000862{
863 int ret;
864
XiaokangQianc5c39d52021-11-09 11:55:10 +0000865 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000866 ssl->handshake->state_local.finished_in.digest,
867 sizeof( ssl->handshake->state_local.finished_in.digest ),
868 &ssl->handshake->state_local.finished_in.digest_len,
XiaokangQianc5c39d52021-11-09 11:55:10 +0000869 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
XiaokangQianc13f9352021-11-11 06:13:22 +0000870 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000871 if( ret != 0 )
872 {
XiaokangQianc5c39d52021-11-09 11:55:10 +0000873 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_calculate_verify_data", ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000874 return( ret );
875 }
876
877 return( 0 );
878}
879
XiaokangQianc5c39d52021-11-09 11:55:10 +0000880static int ssl_tls13_parse_finished_message( mbedtls_ssl_context *ssl,
881 const unsigned char *buf,
882 const unsigned char *end )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000883{
XiaokangQian33062842021-11-11 03:37:45 +0000884 /*
885 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +0000886 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +0000887 * } Finished;
888 */
889 const unsigned char *expected_verify_data =
890 ssl->handshake->state_local.finished_in.digest;
891 size_t expected_verify_data_len =
892 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000893 /* Structural validation */
XiaokangQian33062842021-11-11 03:37:45 +0000894 if( (size_t)( end - buf ) != expected_verify_data_len )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000895 {
896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
897
898 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +0000899 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000900 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
901 }
902
XiaokangQianc5c39d52021-11-09 11:55:10 +0000903 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (self-computed):",
XiaokangQian33062842021-11-11 03:37:45 +0000904 expected_verify_data,
905 expected_verify_data_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +0000906 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (received message):", buf,
XiaokangQian33062842021-11-11 03:37:45 +0000907 expected_verify_data_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000908
909 /* Semantic validation */
Gabor Mezei685472b2021-11-24 11:17:36 +0100910 if( mbedtls_ct_memcmp( buf,
911 expected_verify_data,
912 expected_verify_data_len ) != 0 )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000913 {
914 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
915
XiaokangQian33062842021-11-11 03:37:45 +0000916 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +0000917 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000918 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
919 }
920 return( 0 );
921}
922
XiaokangQianc13f9352021-11-11 06:13:22 +0000923#if defined(MBEDTLS_SSL_CLI_C)
XiaokangQianaaa0e192021-11-10 03:07:04 +0000924static int ssl_tls13_postprocess_server_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000925{
XiaokangQian33062842021-11-11 03:37:45 +0000926 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000927 mbedtls_ssl_key_set traffic_keys;
XiaokangQian1aef02e2021-10-28 09:54:34 +0000928 mbedtls_ssl_transform *transform_application = NULL;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000929
XiaokangQian4cab0242021-10-12 08:43:37 +0000930 ret = mbedtls_ssl_tls13_key_schedule_stage_application( ssl );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000931 if( ret != 0 )
932 {
933 MBEDTLS_SSL_DEBUG_RET( 1,
XiaokangQian4cab0242021-10-12 08:43:37 +0000934 "mbedtls_ssl_tls13_key_schedule_stage_application", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000935 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000936 }
937
XiaokangQian33062842021-11-11 03:37:45 +0000938 ret = mbedtls_ssl_tls13_generate_application_keys( ssl, &traffic_keys );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000939 if( ret != 0 )
940 {
941 MBEDTLS_SSL_DEBUG_RET( 1,
XiaokangQiand0aa3e92021-11-10 06:17:40 +0000942 "mbedtls_ssl_tls13_generate_application_keys", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000943 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000944 }
945
946 transform_application =
947 mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
948 if( transform_application == NULL )
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000949 {
950 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
951 goto cleanup;
952 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000953
954 ret = mbedtls_ssl_tls13_populate_transform(
955 transform_application,
956 ssl->conf->endpoint,
957 ssl->session_negotiate->ciphersuite,
958 &traffic_keys,
959 ssl );
960 if( ret != 0 )
961 {
962 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000963 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000964 }
965
966 ssl->transform_application = transform_application;
967
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000968cleanup:
969
XiaokangQian33062842021-11-11 03:37:45 +0000970 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +0000971 if( ret != 0 )
XiaokangQian1aef02e2021-10-28 09:54:34 +0000972 {
973 mbedtls_free( transform_application );
974 MBEDTLS_SSL_PEND_FATAL_ALERT(
975 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
976 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
977 }
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000978 return( ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000979}
XiaokangQianc13f9352021-11-11 06:13:22 +0000980#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000981
XiaokangQiancc90c942021-11-09 12:30:09 +0000982static int ssl_tls13_postprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000983{
984
XiaokangQianc13f9352021-11-11 06:13:22 +0000985#if defined(MBEDTLS_SSL_CLI_C)
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000986 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
987 {
XiaokangQianaaa0e192021-11-10 03:07:04 +0000988 return( ssl_tls13_postprocess_server_finished_message( ssl ) );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000989 }
XiaokangQianc13f9352021-11-11 06:13:22 +0000990#else
991 ((void) ssl);
992#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000993
994 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
995}
996
XiaokangQianc5c39d52021-11-09 11:55:10 +0000997int mbedtls_ssl_tls13_process_finished_message( mbedtls_ssl_context *ssl )
998{
XiaokangQian33062842021-11-11 03:37:45 +0000999 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001000 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +00001001 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001002
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001003 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001004
1005 /* Preprocessing step: Compute handshake digest */
XiaokangQianaaa0e192021-11-10 03:07:04 +00001006 MBEDTLS_SSL_PROC_CHK( ssl_tls13_preprocess_finished_message( ssl ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001007
Xiaofei Bai746f9482021-11-12 08:53:56 +00001008 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001009 MBEDTLS_SSL_HS_FINISHED,
Xiaofei Baieef15042021-11-18 07:29:56 +00001010 &buf, &buf_len ) );
1011 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_finished_message( ssl, buf, buf + buf_len ) );
Xiaofei Bai746f9482021-11-12 08:53:56 +00001012 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
Xiaofei Baieef15042021-11-18 07:29:56 +00001013 ssl, MBEDTLS_SSL_HS_FINISHED, buf, buf_len );
XiaokangQianaaa0e192021-11-10 03:07:04 +00001014 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_finished_message( ssl ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001015
1016cleanup:
1017
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001018 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001019 return( ret );
1020}
1021
XiaokangQian74af2a82021-09-22 07:40:30 +00001022/*
1023 *
XiaokangQiancc90c942021-11-09 12:30:09 +00001024 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +00001025 *
1026 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001027/*
XiaokangQian35dc6252021-11-11 08:16:19 +00001028 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +00001029 */
1030
XiaokangQian8773aa02021-11-10 07:33:09 +00001031static int ssl_tls13_prepare_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +00001032{
1033 int ret;
1034
1035 /* Compute transcript of handshake up to now. */
XiaokangQiancc90c942021-11-09 12:30:09 +00001036 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQian74af2a82021-09-22 07:40:30 +00001037 ssl->handshake->state_local.finished_out.digest,
1038 sizeof( ssl->handshake->state_local.finished_out.digest ),
1039 &ssl->handshake->state_local.finished_out.digest_len,
1040 ssl->conf->endpoint );
1041
1042 if( ret != 0 )
1043 {
Jerry Yu7ca30542021-12-08 15:57:57 +08001044 MBEDTLS_SSL_DEBUG_RET( 1, "calculate_verify_data failed", ret );
XiaokangQian74af2a82021-09-22 07:40:30 +00001045 return( ret );
1046 }
1047
1048 return( 0 );
1049}
1050
XiaokangQiancc90c942021-11-09 12:30:09 +00001051static int ssl_tls13_finalize_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +00001052{
XiaokangQian0fa66432021-11-15 03:33:57 +00001053 // TODO: Add back resumption keys calculation after MVP.
1054 ((void) ssl);
XiaokangQian74af2a82021-09-22 07:40:30 +00001055
1056 return( 0 );
1057}
1058
XiaokangQian8773aa02021-11-10 07:33:09 +00001059static int ssl_tls13_write_finished_message_body( mbedtls_ssl_context *ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001060 unsigned char *buf,
1061 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001062 size_t *out_len )
XiaokangQian74af2a82021-09-22 07:40:30 +00001063{
XiaokangQian8773aa02021-11-10 07:33:09 +00001064 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001065 /*
1066 * struct {
1067 * opaque verify_data[Hash.length];
1068 * } Finished;
1069 */
XiaokangQian8773aa02021-11-10 07:33:09 +00001070 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001071
1072 memcpy( buf, ssl->handshake->state_local.finished_out.digest,
XiaokangQian8773aa02021-11-10 07:33:09 +00001073 verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001074
Xiaofei Baid25fab62021-12-02 06:36:27 +00001075 *out_len = verify_data_len;
XiaokangQian74af2a82021-09-22 07:40:30 +00001076 return( 0 );
1077}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001078
XiaokangQian35dc6252021-11-11 08:16:19 +00001079/* Main entry point: orchestrates the other functions */
1080int mbedtls_ssl_tls13_write_finished_message( mbedtls_ssl_context *ssl )
1081{
1082 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1083 unsigned char *buf;
1084 size_t buf_len, msg_len;
1085
1086 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished message" ) );
1087
XiaokangQiandce82242021-11-15 06:01:26 +00001088 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_finished_message( ssl ) );
1089
XiaokangQian35dc6252021-11-11 08:16:19 +00001090 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg( ssl,
1091 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len ) );
1092
1093 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_finished_message_body(
1094 ssl, buf, buf + buf_len, &msg_len ) );
1095
Xiaofei Bai746f9482021-11-12 08:53:56 +00001096 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1097 buf, msg_len );
XiaokangQian35dc6252021-11-11 08:16:19 +00001098
1099 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_finished_message( ssl ) );
1100 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
1101 buf_len, msg_len ) );
1102 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_flush_output( ssl ) );
1103
1104cleanup:
1105
1106 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished message" ) );
1107 return( ret );
1108}
1109
Jerry Yu378254d2021-10-30 21:44:47 +08001110void mbedtls_ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
1111{
1112
1113 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
1114
1115 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001116 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001117 */
1118 if( ssl->session )
1119 {
Jerry Yu378254d2021-10-30 21:44:47 +08001120 mbedtls_ssl_session_free( ssl->session );
1121 mbedtls_free( ssl->session );
1122 }
1123 ssl->session = ssl->session_negotiate;
1124 ssl->session_negotiate = NULL;
1125
1126 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
1127}
1128
Ronald Cron49ad6192021-11-24 16:25:31 +01001129/*
1130 *
1131 * STATE HANDLING: Write ChangeCipherSpec
1132 *
1133 */
1134#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1135
1136static int ssl_tls13_write_change_cipher_spec_body( mbedtls_ssl_context *ssl,
1137 unsigned char *buf,
1138 unsigned char *end,
1139 size_t *olen )
1140{
1141 ((void) ssl);
1142
1143 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 1 );
1144 buf[0] = 1;
1145 *olen = 1;
1146
1147 return( 0 );
1148}
1149
Ronald Cron49ad6192021-11-24 16:25:31 +01001150int mbedtls_ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1151{
1152 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1153
1154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
1155
1156 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_flush_output( ssl ) );
1157
1158 /* Write CCS message */
1159 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_change_cipher_spec_body(
1160 ssl, ssl->out_msg,
1161 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1162 &ssl->out_msglen ) );
1163
1164 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1165
Ronald Cron49ad6192021-11-24 16:25:31 +01001166 /* Dispatch message */
1167 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_record( ssl, 1 ) );
1168
1169cleanup:
1170
1171 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
1172 return( ret );
1173}
1174
1175#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1176
Ronald Cron6f135e12021-12-08 16:57:54 +01001177#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001178
1179#endif /* MBEDTLS_SSL_TLS_C */