blob: 86699d9596a134bcf179389bad233bad1b9d03f5 [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,
169 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;
173 while( p < supported_sig_algs_end &&
174 common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000175 {
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000176 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +0000177 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000178 p += 2;
179
180 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x",
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +0000181 sig_alg ) );
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000182
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +0000183 if( mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) &&
184 mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) )
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000185 {
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000186 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +0000187 common_idx += 1;
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000188 }
189 }
190 /* Check that we consumed all the message. */
191 if( p != end )
192 {
193 MBEDTLS_SSL_DEBUG_MSG( 1,
194 ( "Signature algorithms extension length misaligned" ) );
195 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
196 MBEDTLS_ERR_SSL_DECODE_ERROR );
197 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
198 }
199
200 if( common_idx == 0 )
201 {
202 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
203 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
204 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
205 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
206 }
207
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000208 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS1_3_SIG_NONE;
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000209 return( 0 );
210}
211
Jerry Yu30b071c2021-09-12 20:16:03 +0800212/*
Jerry Yu30b071c2021-09-12 20:16:03 +0800213 * STATE HANDLING: Read CertificateVerify
214 */
Jerry Yud0fc5852021-10-29 11:09:06 +0800215/* Macro to express the maximum length of the verify structure.
Jerry Yu30b071c2021-09-12 20:16:03 +0800216 *
217 * The structure is computed per TLS 1.3 specification as:
218 * - 64 bytes of octet 32,
219 * - 33 bytes for the context string
220 * (which is either "TLS 1.3, client CertificateVerify"
221 * or "TLS 1.3, server CertificateVerify"),
Jerry Yud0fc5852021-10-29 11:09:06 +0800222 * - 1 byte for the octet 0x0, which serves as a separator,
Jerry Yu30b071c2021-09-12 20:16:03 +0800223 * - 32 or 48 bytes for the Transcript-Hash(Handshake Context, Certificate)
224 * (depending on the size of the transcript_hash)
225 *
226 * This results in a total size of
227 * - 130 bytes for a SHA256-based transcript hash, or
228 * (64 + 33 + 1 + 32 bytes)
229 * - 146 bytes for a SHA384-based transcript hash.
230 * (64 + 33 + 1 + 48 bytes)
231 *
232 */
Jerry Yu26c2d112021-10-25 12:42:58 +0800233#define SSL_VERIFY_STRUCT_MAX_SIZE ( 64 + \
234 33 + \
235 1 + \
236 MBEDTLS_TLS1_3_MD_MAX_SIZE \
Jerry Yu30b071c2021-09-12 20:16:03 +0800237 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800238
Jerry Yu0b32c502021-10-28 13:41:59 +0800239/*
240 * The ssl_tls13_create_verify_structure() creates the verify structure.
241 * As input, it requires the transcript hash.
242 *
243 * The caller has to ensure that the buffer has size at least
244 * SSL_VERIFY_STRUCT_MAX_SIZE bytes.
245 */
Jerry Yud0fc5852021-10-29 11:09:06 +0800246static void ssl_tls13_create_verify_structure( const unsigned char *transcript_hash,
Jerry Yu0b32c502021-10-28 13:41:59 +0800247 size_t transcript_hash_len,
248 unsigned char *verify_buffer,
249 size_t *verify_buffer_len,
250 int from )
251{
252 size_t idx;
Jerry Yu30b071c2021-09-12 20:16:03 +0800253
Jerry Yu0b32c502021-10-28 13:41:59 +0800254 /* RFC 8446, Section 4.4.3:
255 *
256 * The digital signature [in the CertificateVerify message] is then
257 * computed over the concatenation of:
258 * - A string that consists of octet 32 (0x20) repeated 64 times
259 * - The context string
260 * - A single 0 byte which serves as the separator
261 * - The content to be signed
262 */
263 memset( verify_buffer, 0x20, 64 );
264 idx = 64;
265
266 if( from == MBEDTLS_SSL_IS_CLIENT )
267 {
268 memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( client_cv ) );
269 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( client_cv );
270 }
271 else
272 { /* from == MBEDTLS_SSL_IS_SERVER */
273 memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( server_cv ) );
274 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( server_cv );
275 }
276
277 verify_buffer[idx++] = 0x0;
278
279 memcpy( verify_buffer + idx, transcript_hash, transcript_hash_len );
280 idx += transcript_hash_len;
281
282 *verify_buffer_len = idx;
283}
284
Jerry Yu0b32c502021-10-28 13:41:59 +0800285static int ssl_tls13_parse_certificate_verify( mbedtls_ssl_context *ssl,
Jerry Yud0fc5852021-10-29 11:09:06 +0800286 const unsigned char *buf,
287 const unsigned char *end,
288 const unsigned char *verify_buffer,
289 size_t verify_buffer_len )
Jerry Yu30b071c2021-09-12 20:16:03 +0800290{
291 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
292 const unsigned char *p = buf;
293 uint16_t algorithm;
Jerry Yu30b071c2021-09-12 20:16:03 +0800294 size_t signature_len;
295 mbedtls_pk_type_t sig_alg;
296 mbedtls_md_type_t md_alg;
Jerry Yud0fc5852021-10-29 11:09:06 +0800297 unsigned char verify_hash[MBEDTLS_MD_MAX_SIZE];
Jerry Yu30b071c2021-09-12 20:16:03 +0800298 size_t verify_hash_len;
299
Xiaofei Baid25fab62021-12-02 06:36:27 +0000300 void const *options = NULL;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000301#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Xiaofei Baid25fab62021-12-02 06:36:27 +0000302 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000303#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
304
Jerry Yu30b071c2021-09-12 20:16:03 +0800305 /*
306 * struct {
307 * SignatureScheme algorithm;
308 * opaque signature<0..2^16-1>;
309 * } CertificateVerify;
310 */
311 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
312 algorithm = MBEDTLS_GET_UINT16_BE( p, 0 );
313 p += 2;
314
315 /* RFC 8446 section 4.4.3
316 *
317 * If the CertificateVerify message is sent by a server, the signature algorithm
318 * MUST be one offered in the client's "signature_algorithms" extension unless
319 * no valid certificate chain can be produced without unsupported algorithms
320 *
321 * RFC 8446 section 4.4.2.2
322 *
323 * If the client cannot construct an acceptable chain using the provided
324 * certificates and decides to abort the handshake, then it MUST abort the handshake
325 * with an appropriate certificate-related alert (by default, "unsupported_certificate").
326 *
Jerry Yu6f87f252021-10-29 20:12:51 +0800327 * Check if algorithm is an offered signature algorithm.
Jerry Yu30b071c2021-09-12 20:16:03 +0800328 */
Jerry Yu24811fb2022-01-19 18:02:15 +0800329 if( ! mbedtls_ssl_sig_alg_is_offered( ssl, algorithm ) )
Jerry Yu30b071c2021-09-12 20:16:03 +0800330 {
Jerry Yu982d9e52021-10-14 15:59:37 +0800331 /* algorithm not in offered signature algorithms list */
332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Received signature algorithm(%04x) is not "
333 "offered.",
334 ( unsigned int ) algorithm ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800335 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800336 }
337
338 /* We currently only support ECDSA-based signatures */
339 switch( algorithm )
340 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000341 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Jerry Yu30b071c2021-09-12 20:16:03 +0800342 md_alg = MBEDTLS_MD_SHA256;
343 sig_alg = MBEDTLS_PK_ECDSA;
344 break;
Xiaofei Bai746f9482021-11-12 08:53:56 +0000345 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Jerry Yu30b071c2021-09-12 20:16:03 +0800346 md_alg = MBEDTLS_MD_SHA384;
347 sig_alg = MBEDTLS_PK_ECDSA;
348 break;
Xiaofei Bai746f9482021-11-12 08:53:56 +0000349 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Jerry Yu30b071c2021-09-12 20:16:03 +0800350 md_alg = MBEDTLS_MD_SHA512;
351 sig_alg = MBEDTLS_PK_ECDSA;
352 break;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000353#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Xiaofei Bai6dc90da2021-11-26 08:11:40 +0000354 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
XiaokangQiana83014d2021-11-22 07:53:34 +0000355 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Certificate Verify: using RSA PSS" ) );
XiaokangQian82d34cc2021-11-03 08:51:56 +0000356 md_alg = MBEDTLS_MD_SHA256;
357 sig_alg = MBEDTLS_PK_RSASSA_PSS;
358 break;
359#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800360 default:
361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Certificate Verify: Unknown signature algorithm." ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800362 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800363 }
364
365 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate Verify: Signature algorithm ( %04x )",
366 ( unsigned int ) algorithm ) );
367
368 /*
369 * Check the certificate's key type matches the signature alg
370 */
371 if( !mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk, sig_alg ) )
372 {
373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "signature algorithm doesn't match cert key" ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800374 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800375 }
376
377 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
378 signature_len = MBEDTLS_GET_UINT16_BE( p, 0 );
379 p += 2;
380 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, signature_len );
381
382 /* Hash verify buffer with indicated hash function */
383 switch( md_alg )
384 {
385#if defined(MBEDTLS_SHA256_C)
386 case MBEDTLS_MD_SHA256:
387 verify_hash_len = 32;
Jerry Yu133690c2021-10-25 14:01:13 +0800388 ret = mbedtls_sha256( verify_buffer, verify_buffer_len, verify_hash, 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800389 break;
390#endif /* MBEDTLS_SHA256_C */
391
392#if defined(MBEDTLS_SHA384_C)
393 case MBEDTLS_MD_SHA384:
394 verify_hash_len = 48;
Jerry Yu133690c2021-10-25 14:01:13 +0800395 ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 1 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800396 break;
397#endif /* MBEDTLS_SHA384_C */
398
399#if defined(MBEDTLS_SHA512_C)
400 case MBEDTLS_MD_SHA512:
401 verify_hash_len = 64;
Jerry Yu133690c2021-10-25 14:01:13 +0800402 ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800403 break;
404#endif /* MBEDTLS_SHA512_C */
405
Jerry Yu0b32c502021-10-28 13:41:59 +0800406 default:
407 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
408 break;
Jerry Yu30b071c2021-09-12 20:16:03 +0800409 }
410
Jerry Yu133690c2021-10-25 14:01:13 +0800411 if( ret != 0 )
412 {
413 MBEDTLS_SSL_DEBUG_RET( 1, "hash computation error", ret );
Jerry Yu6f87f252021-10-29 20:12:51 +0800414 goto error;
Jerry Yu133690c2021-10-25 14:01:13 +0800415 }
416
Jerry Yu30b071c2021-09-12 20:16:03 +0800417 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
XiaokangQian82d34cc2021-11-03 08:51:56 +0000418#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
419 if( sig_alg == MBEDTLS_PK_RSASSA_PSS )
420 {
421 const mbedtls_md_info_t* md_info;
Xiaofei Baid25fab62021-12-02 06:36:27 +0000422 rsassa_pss_options.mgf1_hash_id = md_alg;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000423 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
424 {
425 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
426 }
Xiaofei Baid25fab62021-12-02 06:36:27 +0000427 rsassa_pss_options.expected_salt_len = mbedtls_md_get_size( md_info );
428 options = (const void*) &rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000429 }
430#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800431
Xiaofei Baid25fab62021-12-02 06:36:27 +0000432 if( ( ret = mbedtls_pk_verify_ext( sig_alg, options,
Jerry Yu30b071c2021-09-12 20:16:03 +0800433 &ssl->session_negotiate->peer_cert->pk,
434 md_alg, verify_hash, verify_hash_len,
Jerry Yu6f87f252021-10-29 20:12:51 +0800435 p, signature_len ) ) == 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800436 {
Jerry Yu6f87f252021-10-29 20:12:51 +0800437 return( 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800438 }
Jerry Yu6f87f252021-10-29 20:12:51 +0800439 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify_ext", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800440
Jerry Yu6f87f252021-10-29 20:12:51 +0800441error:
442 /* RFC 8446 section 4.4.3
443 *
444 * If the verification fails, the receiver MUST terminate the handshake
445 * with a "decrypt_error" alert.
446 */
447 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
448 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
449 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
450
Jerry Yu30b071c2021-09-12 20:16:03 +0800451}
452#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
453
454int mbedtls_ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
455{
Jerry Yu30b071c2021-09-12 20:16:03 +0800456
Jerry Yuda8cdf22021-10-25 15:06:49 +0800457#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
458 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
459 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
460 size_t verify_buffer_len;
461 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
462 size_t transcript_len;
463 unsigned char *buf;
464 size_t buf_len;
465
Jerry Yu30b071c2021-09-12 20:16:03 +0800466 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
467
Jerry Yuda8cdf22021-10-25 15:06:49 +0800468 MBEDTLS_SSL_PROC_CHK(
Xiaofei Bai746f9482021-11-12 08:53:56 +0000469 mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
Jerry Yuda8cdf22021-10-25 15:06:49 +0800470 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu30b071c2021-09-12 20:16:03 +0800471
Jerry Yuda8cdf22021-10-25 15:06:49 +0800472 /* Need to calculate the hash of the transcript first
Jerry Yu0b32c502021-10-28 13:41:59 +0800473 * before reading the message since otherwise it gets
474 * included in the transcript
475 */
Jerry Yuda8cdf22021-10-25 15:06:49 +0800476 ret = mbedtls_ssl_get_handshake_transcript( ssl,
477 ssl->handshake->ciphersuite_info->mac,
478 transcript, sizeof( transcript ),
479 &transcript_len );
480 if( ret != 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800481 {
Jerry Yuda8cdf22021-10-25 15:06:49 +0800482 MBEDTLS_SSL_PEND_FATAL_ALERT(
483 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
484 MBEDTLS_ERR_SSL_INTERNAL_ERROR );
485 return( ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800486 }
487
Jerry Yuda8cdf22021-10-25 15:06:49 +0800488 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash", transcript, transcript_len );
489
490 /* Create verify structure */
491 ssl_tls13_create_verify_structure( transcript,
Jerry Yu0b32c502021-10-28 13:41:59 +0800492 transcript_len,
493 verify_buffer,
494 &verify_buffer_len,
495 ( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) ?
496 MBEDTLS_SSL_IS_SERVER :
497 MBEDTLS_SSL_IS_CLIENT );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800498
499 /* Process the message contents */
Jerry Yu0b32c502021-10-28 13:41:59 +0800500 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_verify( ssl, buf,
501 buf + buf_len, verify_buffer, verify_buffer_len ) );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800502
Xiaofei Bai746f9482021-11-12 08:53:56 +0000503 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
Jerry Yuda8cdf22021-10-25 15:06:49 +0800504 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, buf, buf_len );
Jerry Yu30b071c2021-09-12 20:16:03 +0800505
506cleanup:
507
508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
Jerry Yu5398c102021-11-05 13:32:38 +0800509 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_process_certificate_verify", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800510 return( ret );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800511#else
512 ((void) ssl);
513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
514 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
515#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800516}
517
518/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000519 *
520 * STATE HANDLING: Incoming Certificate, client-side only currently.
521 *
522 */
523
524/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000525 * Implementation
526 */
527
Xiaofei Bai947571e2021-09-29 09:12:03 +0000528#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
529#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
530/*
531 * Structure of Certificate message:
532 *
533 * enum {
534 * X509(0),
535 * RawPublicKey(2),
536 * (255)
537 * } CertificateType;
538 *
539 * struct {
540 * select (certificate_type) {
541 * case RawPublicKey:
542 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
543 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
544 * case X509:
545 * opaque cert_data<1..2^24-1>;
546 * };
547 * Extension extensions<0..2^16-1>;
548 * } CertificateEntry;
549 *
550 * struct {
551 * opaque certificate_request_context<0..2^8-1>;
552 * CertificateEntry certificate_list<0..2^24-1>;
553 * } Certificate;
554 *
555 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000556
557/* Parse certificate chain send by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000558static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
559 const unsigned char *buf,
560 const unsigned char *end )
561{
562 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baia0ab7772022-01-16 12:14:45 +0000563 size_t certificate_request_context_len = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000564 size_t certificate_list_len = 0;
565 const unsigned char *p = buf;
566 const unsigned char *certificate_list_end;
567
568 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
Xiaofei Baia0ab7772022-01-16 12:14:45 +0000569 certificate_request_context_len = p[0];
Jerry Yub640bf62021-10-29 10:05:32 +0800570 certificate_list_len = MBEDTLS_GET_UINT24_BE( p, 1 );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000571 p += 4;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000572
573 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
574 * support anything beyond 2^16 = 64K.
575 */
Xiaofei Baia0ab7772022-01-16 12:14:45 +0000576 if( ( certificate_request_context_len != 0 ) ||
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000577 ( certificate_list_len >= 0x10000 ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000578 {
579 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
580 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
581 MBEDTLS_ERR_SSL_DECODE_ERROR );
582 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
583 }
584
585 /* In case we tried to reuse a session but it failed */
586 if( ssl->session_negotiate->peer_cert != NULL )
587 {
588 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
589 mbedtls_free( ssl->session_negotiate->peer_cert );
590 }
591
592 if( ( ssl->session_negotiate->peer_cert =
593 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
594 {
595 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
596 sizeof( mbedtls_x509_crt ) ) );
597 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
598 MBEDTLS_ERR_SSL_ALLOC_FAILED );
599 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
600 }
601
602 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
603
Xiaofei Bai947571e2021-09-29 09:12:03 +0000604 certificate_list_end = p + certificate_list_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000605 while( p < certificate_list_end )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000606 {
607 size_t cert_data_len, extensions_len;
608
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000609 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 3 );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000610 cert_data_len = MBEDTLS_GET_UINT24_BE( p, 0 );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000611 p += 3;
612
613 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
614 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
615 * check that we have a minimum of 128 bytes of data, this is not
616 * clear why we need that though.
617 */
618 if( ( cert_data_len < 128 ) || ( cert_data_len >= 0x10000 ) )
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000619 {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000620 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
621 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
622 MBEDTLS_ERR_SSL_DECODE_ERROR );
623 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
624 }
625
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000626 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, cert_data_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000627 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
628 p, cert_data_len );
629
630 switch( ret )
631 {
632 case 0: /*ok*/
633 break;
634 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
635 /* Ignore certificate with an unknown algorithm: maybe a
636 prior certificate was already trusted. */
637 break;
638
639 case MBEDTLS_ERR_X509_ALLOC_FAILED:
640 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
641 MBEDTLS_ERR_X509_ALLOC_FAILED );
642 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
643 return( ret );
644
645 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
646 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
647 MBEDTLS_ERR_X509_UNKNOWN_VERSION );
648 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
649 return( ret );
650
651 default:
652 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
653 ret );
654 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
655 return( ret );
656 }
657
658 p += cert_data_len;
659
660 /* Certificate extensions length */
661 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 2 );
662 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
663 p += 2;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000664 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, extensions_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000665 p += extensions_len;
666 }
667
668 /* Check that all the message is consumed. */
669 if( p != end )
670 {
671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
672 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, \
673 MBEDTLS_ERR_SSL_DECODE_ERROR );
674 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
675 }
676
677 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
678
679 return( ret );
680}
681#else
682static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
683 const unsigned char *buf,
684 const unsigned char *end )
685{
686 ((void) ssl);
687 ((void) buf);
688 ((void) end);
689 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
690}
691#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
692#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
693
694#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
695#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000696/* Validate certificate chain sent by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000697static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
698{
699 int ret = 0;
700 mbedtls_x509_crt *ca_chain;
701 mbedtls_x509_crl *ca_crl;
Xiaofei Baiff456022021-10-28 06:50:17 +0000702 uint32_t verify_result = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000703
704#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
705 if( ssl->handshake->sni_ca_chain != NULL )
706 {
707 ca_chain = ssl->handshake->sni_ca_chain;
708 ca_crl = ssl->handshake->sni_ca_crl;
709 }
710 else
711#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
712 {
713 ca_chain = ssl->conf->ca_chain;
714 ca_crl = ssl->conf->ca_crl;
715 }
716
717 /*
718 * Main check: verify certificate
719 */
720 ret = mbedtls_x509_crt_verify_with_profile(
721 ssl->session_negotiate->peer_cert,
722 ca_chain, ca_crl,
723 ssl->conf->cert_profile,
724 ssl->hostname,
Xiaofei Baiff456022021-10-28 06:50:17 +0000725 &verify_result,
Xiaofei Bai947571e2021-09-29 09:12:03 +0000726 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
727
728 if( ret != 0 )
729 {
730 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
731 }
732
733 /*
734 * Secondary checks: always done, but change 'ret' only if it was 0
735 */
736
737#if defined(MBEDTLS_ECP_C)
738 {
739 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
740
741 /* If certificate uses an EC key, make sure the curve is OK */
742 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
743 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
744 {
Xiaofei Baiff456022021-10-28 06:50:17 +0000745 verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000746
747 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( EC key curve )" ) );
748 if( ret == 0 )
749 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
750 }
751 }
752#endif /* MBEDTLS_ECP_C */
753
754 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
755 ssl->handshake->ciphersuite_info,
756 !ssl->conf->endpoint,
Xiaofei Baiff456022021-10-28 06:50:17 +0000757 &verify_result ) != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000758 {
759 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( usage extensions )" ) );
760 if( ret == 0 )
761 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
762 }
763
764
765 if( ca_chain == NULL )
766 {
767 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
768 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
769 }
770
771 if( ret != 0 )
772 {
773 /* The certificate may have been rejected for several reasons.
774 Pick one and send the corresponding alert. Which alert to send
775 may be a subject of debate in some cases. */
Xiaofei Baiff456022021-10-28 06:50:17 +0000776 if( verify_result & MBEDTLS_X509_BADCERT_OTHER )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000777 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000778 else if( verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000779 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000780 else if( verify_result & ( MBEDTLS_X509_BADCERT_KEY_USAGE |
781 MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
782 MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
783 MBEDTLS_X509_BADCERT_BAD_PK |
784 MBEDTLS_X509_BADCERT_BAD_KEY ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000785 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000786 else if( verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000787 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000788 else if( verify_result & MBEDTLS_X509_BADCERT_REVOKED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000789 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000790 else if( verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000791 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret );
792 else
793 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret );
794 }
795
796#if defined(MBEDTLS_DEBUG_C)
Xiaofei Baiff456022021-10-28 06:50:17 +0000797 if( verify_result != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000798 {
799 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
Jerry Yu83bb1312021-10-28 22:16:33 +0800800 (unsigned int) verify_result ) );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000801 }
802 else
803 {
804 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
805 }
806#endif /* MBEDTLS_DEBUG_C */
807
Xiaofei Baiff456022021-10-28 06:50:17 +0000808 ssl->session_negotiate->verify_result = verify_result;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000809 return( ret );
810}
811#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
812static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
813{
814 ((void) ssl);
815 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
816}
817#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
818#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
819
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000820int mbedtls_ssl_tls13_process_certificate( mbedtls_ssl_context *ssl )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000821{
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000822 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
824
825#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
826 unsigned char *buf;
827 size_t buf_len;
828
Xiaofei Bai746f9482021-11-12 08:53:56 +0000829 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000830 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
831 &buf, &buf_len ) );
832
833 /* Parse the certificate chain sent by the peer. */
834 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate( ssl, buf, buf + buf_len ) );
835 /* Validate the certificate chain and set the verification results. */
836 MBEDTLS_SSL_PROC_CHK( ssl_tls13_validate_certificate( ssl ) );
837
Xiaofei Bai746f9482021-11-12 08:53:56 +0000838 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
839 buf, buf_len );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000840
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000841cleanup:
842
843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Xiaofei Bai10aeec02021-10-26 09:50:08 +0000844#else
845 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
846 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
847#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000848 return( ret );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000849}
850
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000851/*
852 *
XiaokangQianc5c39d52021-11-09 11:55:10 +0000853 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000854 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000855/*
856 * Implementation
857 */
858
XiaokangQianaaa0e192021-11-10 03:07:04 +0000859static int ssl_tls13_preprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000860{
861 int ret;
862
XiaokangQianc5c39d52021-11-09 11:55:10 +0000863 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000864 ssl->handshake->state_local.finished_in.digest,
865 sizeof( ssl->handshake->state_local.finished_in.digest ),
866 &ssl->handshake->state_local.finished_in.digest_len,
XiaokangQianc5c39d52021-11-09 11:55:10 +0000867 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
XiaokangQianc13f9352021-11-11 06:13:22 +0000868 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000869 if( ret != 0 )
870 {
XiaokangQianc5c39d52021-11-09 11:55:10 +0000871 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_calculate_verify_data", ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000872 return( ret );
873 }
874
875 return( 0 );
876}
877
XiaokangQianc5c39d52021-11-09 11:55:10 +0000878static int ssl_tls13_parse_finished_message( mbedtls_ssl_context *ssl,
879 const unsigned char *buf,
880 const unsigned char *end )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000881{
XiaokangQian33062842021-11-11 03:37:45 +0000882 /*
883 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +0000884 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +0000885 * } Finished;
886 */
887 const unsigned char *expected_verify_data =
888 ssl->handshake->state_local.finished_in.digest;
889 size_t expected_verify_data_len =
890 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000891 /* Structural validation */
XiaokangQian33062842021-11-11 03:37:45 +0000892 if( (size_t)( end - buf ) != expected_verify_data_len )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000893 {
894 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
895
896 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +0000897 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000898 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
899 }
900
XiaokangQianc5c39d52021-11-09 11:55:10 +0000901 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (self-computed):",
XiaokangQian33062842021-11-11 03:37:45 +0000902 expected_verify_data,
903 expected_verify_data_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +0000904 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (received message):", buf,
XiaokangQian33062842021-11-11 03:37:45 +0000905 expected_verify_data_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000906
907 /* Semantic validation */
Gabor Mezei685472b2021-11-24 11:17:36 +0100908 if( mbedtls_ct_memcmp( buf,
909 expected_verify_data,
910 expected_verify_data_len ) != 0 )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000911 {
912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
913
XiaokangQian33062842021-11-11 03:37:45 +0000914 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +0000915 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000916 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
917 }
918 return( 0 );
919}
920
XiaokangQianc13f9352021-11-11 06:13:22 +0000921#if defined(MBEDTLS_SSL_CLI_C)
XiaokangQianaaa0e192021-11-10 03:07:04 +0000922static int ssl_tls13_postprocess_server_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000923{
XiaokangQian33062842021-11-11 03:37:45 +0000924 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000925 mbedtls_ssl_key_set traffic_keys;
XiaokangQian1aef02e2021-10-28 09:54:34 +0000926 mbedtls_ssl_transform *transform_application = NULL;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000927
XiaokangQian4cab0242021-10-12 08:43:37 +0000928 ret = mbedtls_ssl_tls13_key_schedule_stage_application( ssl );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000929 if( ret != 0 )
930 {
931 MBEDTLS_SSL_DEBUG_RET( 1,
XiaokangQian4cab0242021-10-12 08:43:37 +0000932 "mbedtls_ssl_tls13_key_schedule_stage_application", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000933 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000934 }
935
XiaokangQian33062842021-11-11 03:37:45 +0000936 ret = mbedtls_ssl_tls13_generate_application_keys( ssl, &traffic_keys );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000937 if( ret != 0 )
938 {
939 MBEDTLS_SSL_DEBUG_RET( 1,
XiaokangQiand0aa3e92021-11-10 06:17:40 +0000940 "mbedtls_ssl_tls13_generate_application_keys", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000941 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000942 }
943
944 transform_application =
945 mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
946 if( transform_application == NULL )
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000947 {
948 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
949 goto cleanup;
950 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000951
952 ret = mbedtls_ssl_tls13_populate_transform(
953 transform_application,
954 ssl->conf->endpoint,
955 ssl->session_negotiate->ciphersuite,
956 &traffic_keys,
957 ssl );
958 if( ret != 0 )
959 {
960 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000961 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000962 }
963
964 ssl->transform_application = transform_application;
965
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000966cleanup:
967
XiaokangQian33062842021-11-11 03:37:45 +0000968 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +0000969 if( ret != 0 )
XiaokangQian1aef02e2021-10-28 09:54:34 +0000970 {
971 mbedtls_free( transform_application );
972 MBEDTLS_SSL_PEND_FATAL_ALERT(
973 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
974 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
975 }
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000976 return( ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000977}
XiaokangQianc13f9352021-11-11 06:13:22 +0000978#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000979
XiaokangQiancc90c942021-11-09 12:30:09 +0000980static int ssl_tls13_postprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000981{
982
XiaokangQianc13f9352021-11-11 06:13:22 +0000983#if defined(MBEDTLS_SSL_CLI_C)
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000984 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
985 {
XiaokangQianaaa0e192021-11-10 03:07:04 +0000986 return( ssl_tls13_postprocess_server_finished_message( ssl ) );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000987 }
XiaokangQianc13f9352021-11-11 06:13:22 +0000988#else
989 ((void) ssl);
990#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000991
992 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
993}
994
XiaokangQianc5c39d52021-11-09 11:55:10 +0000995int mbedtls_ssl_tls13_process_finished_message( mbedtls_ssl_context *ssl )
996{
XiaokangQian33062842021-11-11 03:37:45 +0000997 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +0000998 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +0000999 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001000
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001001 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001002
1003 /* Preprocessing step: Compute handshake digest */
XiaokangQianaaa0e192021-11-10 03:07:04 +00001004 MBEDTLS_SSL_PROC_CHK( ssl_tls13_preprocess_finished_message( ssl ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001005
Xiaofei Bai746f9482021-11-12 08:53:56 +00001006 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001007 MBEDTLS_SSL_HS_FINISHED,
Xiaofei Baieef15042021-11-18 07:29:56 +00001008 &buf, &buf_len ) );
1009 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_finished_message( ssl, buf, buf + buf_len ) );
Xiaofei Bai746f9482021-11-12 08:53:56 +00001010 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
Xiaofei Baieef15042021-11-18 07:29:56 +00001011 ssl, MBEDTLS_SSL_HS_FINISHED, buf, buf_len );
XiaokangQianaaa0e192021-11-10 03:07:04 +00001012 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_finished_message( ssl ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001013
1014cleanup:
1015
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001016 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001017 return( ret );
1018}
1019
XiaokangQian74af2a82021-09-22 07:40:30 +00001020/*
1021 *
XiaokangQiancc90c942021-11-09 12:30:09 +00001022 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +00001023 *
1024 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001025/*
XiaokangQian35dc6252021-11-11 08:16:19 +00001026 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +00001027 */
1028
XiaokangQian8773aa02021-11-10 07:33:09 +00001029static int ssl_tls13_prepare_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +00001030{
1031 int ret;
1032
1033 /* Compute transcript of handshake up to now. */
XiaokangQiancc90c942021-11-09 12:30:09 +00001034 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQian74af2a82021-09-22 07:40:30 +00001035 ssl->handshake->state_local.finished_out.digest,
1036 sizeof( ssl->handshake->state_local.finished_out.digest ),
1037 &ssl->handshake->state_local.finished_out.digest_len,
1038 ssl->conf->endpoint );
1039
1040 if( ret != 0 )
1041 {
Jerry Yu7ca30542021-12-08 15:57:57 +08001042 MBEDTLS_SSL_DEBUG_RET( 1, "calculate_verify_data failed", ret );
XiaokangQian74af2a82021-09-22 07:40:30 +00001043 return( ret );
1044 }
1045
1046 return( 0 );
1047}
1048
XiaokangQiancc90c942021-11-09 12:30:09 +00001049static int ssl_tls13_finalize_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +00001050{
XiaokangQian0fa66432021-11-15 03:33:57 +00001051 // TODO: Add back resumption keys calculation after MVP.
1052 ((void) ssl);
XiaokangQian74af2a82021-09-22 07:40:30 +00001053
1054 return( 0 );
1055}
1056
XiaokangQian8773aa02021-11-10 07:33:09 +00001057static int ssl_tls13_write_finished_message_body( mbedtls_ssl_context *ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001058 unsigned char *buf,
1059 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001060 size_t *out_len )
XiaokangQian74af2a82021-09-22 07:40:30 +00001061{
XiaokangQian8773aa02021-11-10 07:33:09 +00001062 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001063 /*
1064 * struct {
1065 * opaque verify_data[Hash.length];
1066 * } Finished;
1067 */
XiaokangQian8773aa02021-11-10 07:33:09 +00001068 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001069
1070 memcpy( buf, ssl->handshake->state_local.finished_out.digest,
XiaokangQian8773aa02021-11-10 07:33:09 +00001071 verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001072
Xiaofei Baid25fab62021-12-02 06:36:27 +00001073 *out_len = verify_data_len;
XiaokangQian74af2a82021-09-22 07:40:30 +00001074 return( 0 );
1075}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001076
XiaokangQian35dc6252021-11-11 08:16:19 +00001077/* Main entry point: orchestrates the other functions */
1078int mbedtls_ssl_tls13_write_finished_message( mbedtls_ssl_context *ssl )
1079{
1080 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1081 unsigned char *buf;
1082 size_t buf_len, msg_len;
1083
1084 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished message" ) );
1085
XiaokangQiandce82242021-11-15 06:01:26 +00001086 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_finished_message( ssl ) );
1087
XiaokangQian35dc6252021-11-11 08:16:19 +00001088 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg( ssl,
1089 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len ) );
1090
1091 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_finished_message_body(
1092 ssl, buf, buf + buf_len, &msg_len ) );
1093
Xiaofei Bai746f9482021-11-12 08:53:56 +00001094 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1095 buf, msg_len );
XiaokangQian35dc6252021-11-11 08:16:19 +00001096
1097 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_finished_message( ssl ) );
1098 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
1099 buf_len, msg_len ) );
1100 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_flush_output( ssl ) );
1101
1102cleanup:
1103
1104 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished message" ) );
1105 return( ret );
1106}
1107
Jerry Yu378254d2021-10-30 21:44:47 +08001108void mbedtls_ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
1109{
1110
1111 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
1112
1113 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001114 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001115 */
1116 if( ssl->session )
1117 {
Jerry Yu378254d2021-10-30 21:44:47 +08001118 mbedtls_ssl_session_free( ssl->session );
1119 mbedtls_free( ssl->session );
1120 }
1121 ssl->session = ssl->session_negotiate;
1122 ssl->session_negotiate = NULL;
1123
1124 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
1125}
1126
Ronald Cron49ad6192021-11-24 16:25:31 +01001127/*
1128 *
1129 * STATE HANDLING: Write ChangeCipherSpec
1130 *
1131 */
1132#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1133
1134static int ssl_tls13_write_change_cipher_spec_body( mbedtls_ssl_context *ssl,
1135 unsigned char *buf,
1136 unsigned char *end,
1137 size_t *olen )
1138{
1139 ((void) ssl);
1140
1141 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 1 );
1142 buf[0] = 1;
1143 *olen = 1;
1144
1145 return( 0 );
1146}
1147
Ronald Cron49ad6192021-11-24 16:25:31 +01001148int mbedtls_ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1149{
1150 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1151
1152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
1153
1154 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_flush_output( ssl ) );
1155
1156 /* Write CCS message */
1157 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_change_cipher_spec_body(
1158 ssl, ssl->out_msg,
1159 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1160 &ssl->out_msglen ) );
1161
1162 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1163
Ronald Cron49ad6192021-11-24 16:25:31 +01001164 /* Dispatch message */
1165 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_record( ssl, 1 ) );
1166
1167cleanup:
1168
1169 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
1170 return( ret );
1171}
1172
1173#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1174
Ronald Cron6f135e12021-12-08 16:57:54 +01001175#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001176
1177#endif /* MBEDTLS_SSL_TLS_C */