blob: cea381102865e933378f942c6f71c8dbceb2c17b [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
Jerry Yufb4b6472022-01-27 15:03:26 +080022#if defined(MBEDTLS_SSL_TLS_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu65dd2cc2021-08-18 16:38:40 +080023
Jerry Yu30b071c2021-09-12 20:16:03 +080024#include <string.h>
25
Jerry Yuc8a392c2021-08-18 16:46:28 +080026#include "mbedtls/error.h"
Jerry Yu75336352021-09-01 15:59:36 +080027#include "mbedtls/debug.h"
Jerry Yu30b071c2021-09-12 20:16:03 +080028#include "mbedtls/oid.h"
29#include "mbedtls/platform.h"
Gabor Mezei685472b2021-11-24 11:17:36 +010030#include "mbedtls/constant_time.h"
XiaokangQian74af2a82021-09-22 07:40:30 +000031#include <string.h>
Jerry Yuc8a392c2021-08-18 16:46:28 +080032
Jerry Yu65dd2cc2021-08-18 16:38:40 +080033#include "ssl_misc.h"
Jerry Yu30b071c2021-09-12 20:16:03 +080034#include "ssl_tls13_keys.h"
Jerry Yu65dd2cc2021-08-18 16:38:40 +080035
Xiaofei Bai746f9482021-11-12 08:53:56 +000036int mbedtls_ssl_tls13_fetch_handshake_msg( mbedtls_ssl_context *ssl,
37 unsigned hs_type,
38 unsigned char **buf,
Xiaofei Baieef15042021-11-18 07:29:56 +000039 size_t *buf_len )
XiaokangQian6b226b02021-09-24 07:51:16 +000040{
41 int ret;
42
43 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
44 {
45 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
46 goto cleanup;
47 }
48
XiaokangQian16c61aa2021-09-27 09:30:17 +000049 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
XiaokangQian6b226b02021-09-24 07:51:16 +000050 ssl->in_msg[0] != hs_type )
51 {
XiaokangQian16c61aa2021-09-27 09:30:17 +000052 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Receive unexpected handshake message." ) );
XiaokangQian6b226b02021-09-24 07:51:16 +000053 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
XiaokangQian05420b12021-09-29 08:46:37 +000054 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
XiaokangQian6b226b02021-09-24 07:51:16 +000055 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
56 goto cleanup;
57 }
58
XiaokangQian05420b12021-09-29 08:46:37 +000059 /*
60 * Jump handshake header (4 bytes, see Section 4 of RFC 8446).
61 * ...
62 * HandshakeType msg_type;
63 * uint24 length;
64 * ...
65 */
Xiaofei Baieef15042021-11-18 07:29:56 +000066 *buf = ssl->in_msg + 4;
67 *buf_len = ssl->in_hslen - 4;
XiaokangQian6b226b02021-09-24 07:51:16 +000068
XiaokangQian6b226b02021-09-24 07:51:16 +000069cleanup:
70
71 return( ret );
72}
73
Jerry Yuf4436812021-08-26 22:59:56 +080074int mbedtls_ssl_tls13_start_handshake_msg( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +080075 unsigned hs_type,
76 unsigned char **buf,
Jerry Yu0c63af62021-09-02 12:59:12 +080077 size_t *buf_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +080078{
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080079 /*
80 * Reserve 4 bytes for hanshake header. ( Section 4,RFC 8446 )
81 * ...
82 * HandshakeType msg_type;
83 * uint24 length;
84 * ...
85 */
Jerry Yuc8a392c2021-08-18 16:46:28 +080086 *buf = ssl->out_msg + 4;
Jerry Yu0c63af62021-09-02 12:59:12 +080087 *buf_len = MBEDTLS_SSL_OUT_CONTENT_LEN - 4;
Jerry Yuc8a392c2021-08-18 16:46:28 +080088
89 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
90 ssl->out_msg[0] = hs_type;
91
92 return( 0 );
Jerry Yu65dd2cc2021-08-18 16:38:40 +080093}
94
Jerry Yuf4436812021-08-26 22:59:56 +080095int mbedtls_ssl_tls13_finish_handshake_msg( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +080096 size_t buf_len,
97 size_t msg_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +080098{
Jerry Yuc8a392c2021-08-18 16:46:28 +080099 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baieef15042021-11-18 07:29:56 +0000100 size_t msg_with_header_len;
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800101 ((void) buf_len);
Jerry Yuc8a392c2021-08-18 16:46:28 +0800102
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800103 /* Add reserved 4 bytes for handshake header */
Xiaofei Baieef15042021-11-18 07:29:56 +0000104 msg_with_header_len = msg_len + 4;
105 ssl->out_msglen = msg_with_header_len;
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800106 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_handshake_msg_ext( ssl, 0 ) );
Jerry Yuc8a392c2021-08-18 16:46:28 +0800107
108cleanup:
109 return( ret );
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800110}
111
Xiaofei Bai746f9482021-11-12 08:53:56 +0000112void mbedtls_ssl_tls13_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
113 unsigned hs_type,
114 unsigned char const *msg,
115 size_t msg_len )
Jerry Yu7bea4ba2021-09-09 15:06:18 +0800116{
117 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
118 ssl->handshake->update_checksum( ssl, msg, msg_len );
119}
120
Jerry Yuf4436812021-08-26 22:59:56 +0800121void mbedtls_ssl_tls13_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800122 unsigned hs_type,
123 size_t total_hs_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800124{
125 unsigned char hs_hdr[4];
126
127 /* Build HS header for checksum update. */
Jerry Yu2ac64192021-08-26 18:38:58 +0800128 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
129 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
130 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
131 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800132
133 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
134}
135
Jerry Yubc20bdd2021-08-24 15:59:48 +0800136#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000137/* mbedtls_ssl_tls13_parse_sig_alg_ext()
138 *
139 * enum {
140 * ....
141 * ecdsa_secp256r1_sha256( 0x0403 ),
142 * ecdsa_secp384r1_sha384( 0x0503 ),
143 * ecdsa_secp521r1_sha512( 0x0603 ),
144 * ....
145 * } SignatureScheme;
146 *
147 * struct {
148 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
149 * } SignatureSchemeList;
150 */
151int mbedtls_ssl_tls13_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
152 const unsigned char *buf,
153 const unsigned char *end )
154{
155 const unsigned char *p = buf;
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000156 size_t supported_sig_algs_len = 0;
157 const unsigned char *supported_sig_algs_end;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +0000158 uint16_t sig_alg;
Xiaofei Baic234ecf2022-02-08 09:59:23 +0000159 uint32_t common_idx = 0;
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000160
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000161 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000162 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000163 p += 2;
164
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000165 memset( ssl->handshake->received_sig_algs, 0,
Xiaofei Bai51f515a2022-02-08 07:28:04 +0000166 sizeof(ssl->handshake->received_sig_algs) );
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000167
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000168 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
169 supported_sig_algs_end = p + supported_sig_algs_len;
Xiaofei Bai51f515a2022-02-08 07:28:04 +0000170 while( p < supported_sig_algs_end )
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000171 {
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000172 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +0000173 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000174 p += 2;
175
176 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x",
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +0000177 sig_alg ) );
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000178
Xiaofei Bai51f515a2022-02-08 07:28:04 +0000179 if( ! mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ||
180 ! mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) )
181 continue;
182
183 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000184 {
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000185 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +0000186 common_idx += 1;
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000187 }
188 }
189 /* Check that we consumed all the message. */
190 if( p != end )
191 {
192 MBEDTLS_SSL_DEBUG_MSG( 1,
193 ( "Signature algorithms extension length misaligned" ) );
194 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
195 MBEDTLS_ERR_SSL_DECODE_ERROR );
196 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
197 }
198
199 if( common_idx == 0 )
200 {
201 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
202 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
203 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
204 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
205 }
206
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000207 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS1_3_SIG_NONE;
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000208 return( 0 );
209}
210
Jerry Yu30b071c2021-09-12 20:16:03 +0800211/*
Jerry Yu30b071c2021-09-12 20:16:03 +0800212 * STATE HANDLING: Read CertificateVerify
213 */
Jerry Yud0fc5852021-10-29 11:09:06 +0800214/* Macro to express the maximum length of the verify structure.
Jerry Yu30b071c2021-09-12 20:16:03 +0800215 *
216 * The structure is computed per TLS 1.3 specification as:
217 * - 64 bytes of octet 32,
218 * - 33 bytes for the context string
219 * (which is either "TLS 1.3, client CertificateVerify"
220 * or "TLS 1.3, server CertificateVerify"),
Jerry Yud0fc5852021-10-29 11:09:06 +0800221 * - 1 byte for the octet 0x0, which serves as a separator,
Jerry Yu30b071c2021-09-12 20:16:03 +0800222 * - 32 or 48 bytes for the Transcript-Hash(Handshake Context, Certificate)
223 * (depending on the size of the transcript_hash)
224 *
225 * This results in a total size of
226 * - 130 bytes for a SHA256-based transcript hash, or
227 * (64 + 33 + 1 + 32 bytes)
228 * - 146 bytes for a SHA384-based transcript hash.
229 * (64 + 33 + 1 + 48 bytes)
230 *
231 */
Jerry Yu26c2d112021-10-25 12:42:58 +0800232#define SSL_VERIFY_STRUCT_MAX_SIZE ( 64 + \
233 33 + \
234 1 + \
235 MBEDTLS_TLS1_3_MD_MAX_SIZE \
Jerry Yu30b071c2021-09-12 20:16:03 +0800236 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800237
Jerry Yu0b32c502021-10-28 13:41:59 +0800238/*
239 * The ssl_tls13_create_verify_structure() creates the verify structure.
240 * As input, it requires the transcript hash.
241 *
242 * The caller has to ensure that the buffer has size at least
243 * SSL_VERIFY_STRUCT_MAX_SIZE bytes.
244 */
Jerry Yud0fc5852021-10-29 11:09:06 +0800245static void ssl_tls13_create_verify_structure( const unsigned char *transcript_hash,
Jerry Yu0b32c502021-10-28 13:41:59 +0800246 size_t transcript_hash_len,
247 unsigned char *verify_buffer,
248 size_t *verify_buffer_len,
249 int from )
250{
251 size_t idx;
Jerry Yu30b071c2021-09-12 20:16:03 +0800252
Jerry Yu0b32c502021-10-28 13:41:59 +0800253 /* RFC 8446, Section 4.4.3:
254 *
255 * The digital signature [in the CertificateVerify message] is then
256 * computed over the concatenation of:
257 * - A string that consists of octet 32 (0x20) repeated 64 times
258 * - The context string
259 * - A single 0 byte which serves as the separator
260 * - The content to be signed
261 */
262 memset( verify_buffer, 0x20, 64 );
263 idx = 64;
264
265 if( from == MBEDTLS_SSL_IS_CLIENT )
266 {
267 memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( client_cv ) );
268 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( client_cv );
269 }
270 else
271 { /* from == MBEDTLS_SSL_IS_SERVER */
272 memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( server_cv ) );
273 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( server_cv );
274 }
275
276 verify_buffer[idx++] = 0x0;
277
278 memcpy( verify_buffer + idx, transcript_hash, transcript_hash_len );
279 idx += transcript_hash_len;
280
281 *verify_buffer_len = idx;
282}
283
Jerry Yu0b32c502021-10-28 13:41:59 +0800284static int ssl_tls13_parse_certificate_verify( mbedtls_ssl_context *ssl,
Jerry Yud0fc5852021-10-29 11:09:06 +0800285 const unsigned char *buf,
286 const unsigned char *end,
287 const unsigned char *verify_buffer,
288 size_t verify_buffer_len )
Jerry Yu30b071c2021-09-12 20:16:03 +0800289{
290 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
291 const unsigned char *p = buf;
292 uint16_t algorithm;
Jerry Yu30b071c2021-09-12 20:16:03 +0800293 size_t signature_len;
294 mbedtls_pk_type_t sig_alg;
295 mbedtls_md_type_t md_alg;
Jerry Yud0fc5852021-10-29 11:09:06 +0800296 unsigned char verify_hash[MBEDTLS_MD_MAX_SIZE];
Jerry Yu30b071c2021-09-12 20:16:03 +0800297 size_t verify_hash_len;
298
Xiaofei Baid25fab62021-12-02 06:36:27 +0000299 void const *options = NULL;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000300#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Xiaofei Baid25fab62021-12-02 06:36:27 +0000301 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000302#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
303
Jerry Yu30b071c2021-09-12 20:16:03 +0800304 /*
305 * struct {
306 * SignatureScheme algorithm;
307 * opaque signature<0..2^16-1>;
308 * } CertificateVerify;
309 */
310 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
311 algorithm = MBEDTLS_GET_UINT16_BE( p, 0 );
312 p += 2;
313
314 /* RFC 8446 section 4.4.3
315 *
316 * If the CertificateVerify message is sent by a server, the signature algorithm
317 * MUST be one offered in the client's "signature_algorithms" extension unless
318 * no valid certificate chain can be produced without unsupported algorithms
319 *
320 * RFC 8446 section 4.4.2.2
321 *
322 * If the client cannot construct an acceptable chain using the provided
323 * certificates and decides to abort the handshake, then it MUST abort the handshake
324 * with an appropriate certificate-related alert (by default, "unsupported_certificate").
325 *
Jerry Yu6f87f252021-10-29 20:12:51 +0800326 * Check if algorithm is an offered signature algorithm.
Jerry Yu30b071c2021-09-12 20:16:03 +0800327 */
Jerry Yu24811fb2022-01-19 18:02:15 +0800328 if( ! mbedtls_ssl_sig_alg_is_offered( ssl, algorithm ) )
Jerry Yu30b071c2021-09-12 20:16:03 +0800329 {
Jerry Yu982d9e52021-10-14 15:59:37 +0800330 /* algorithm not in offered signature algorithms list */
331 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Received signature algorithm(%04x) is not "
332 "offered.",
333 ( unsigned int ) algorithm ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800334 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800335 }
336
337 /* We currently only support ECDSA-based signatures */
338 switch( algorithm )
339 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000340 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Jerry Yu30b071c2021-09-12 20:16:03 +0800341 md_alg = MBEDTLS_MD_SHA256;
342 sig_alg = MBEDTLS_PK_ECDSA;
343 break;
Xiaofei Bai746f9482021-11-12 08:53:56 +0000344 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Jerry Yu30b071c2021-09-12 20:16:03 +0800345 md_alg = MBEDTLS_MD_SHA384;
346 sig_alg = MBEDTLS_PK_ECDSA;
347 break;
Xiaofei Bai746f9482021-11-12 08:53:56 +0000348 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Jerry Yu30b071c2021-09-12 20:16:03 +0800349 md_alg = MBEDTLS_MD_SHA512;
350 sig_alg = MBEDTLS_PK_ECDSA;
351 break;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000352#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Xiaofei Bai6dc90da2021-11-26 08:11:40 +0000353 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
XiaokangQiana83014d2021-11-22 07:53:34 +0000354 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Certificate Verify: using RSA PSS" ) );
XiaokangQian82d34cc2021-11-03 08:51:56 +0000355 md_alg = MBEDTLS_MD_SHA256;
356 sig_alg = MBEDTLS_PK_RSASSA_PSS;
357 break;
358#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800359 default:
360 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Certificate Verify: Unknown signature algorithm." ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800361 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800362 }
363
364 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate Verify: Signature algorithm ( %04x )",
365 ( unsigned int ) algorithm ) );
366
367 /*
368 * Check the certificate's key type matches the signature alg
369 */
370 if( !mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk, sig_alg ) )
371 {
372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "signature algorithm doesn't match cert key" ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800373 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800374 }
375
376 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
377 signature_len = MBEDTLS_GET_UINT16_BE( p, 0 );
378 p += 2;
379 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, signature_len );
380
381 /* Hash verify buffer with indicated hash function */
382 switch( md_alg )
383 {
384#if defined(MBEDTLS_SHA256_C)
385 case MBEDTLS_MD_SHA256:
386 verify_hash_len = 32;
Jerry Yu133690c2021-10-25 14:01:13 +0800387 ret = mbedtls_sha256( verify_buffer, verify_buffer_len, verify_hash, 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800388 break;
389#endif /* MBEDTLS_SHA256_C */
390
391#if defined(MBEDTLS_SHA384_C)
392 case MBEDTLS_MD_SHA384:
393 verify_hash_len = 48;
Jerry Yu133690c2021-10-25 14:01:13 +0800394 ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 1 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800395 break;
396#endif /* MBEDTLS_SHA384_C */
397
398#if defined(MBEDTLS_SHA512_C)
399 case MBEDTLS_MD_SHA512:
400 verify_hash_len = 64;
Jerry Yu133690c2021-10-25 14:01:13 +0800401 ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800402 break;
403#endif /* MBEDTLS_SHA512_C */
404
Jerry Yu0b32c502021-10-28 13:41:59 +0800405 default:
406 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
407 break;
Jerry Yu30b071c2021-09-12 20:16:03 +0800408 }
409
Jerry Yu133690c2021-10-25 14:01:13 +0800410 if( ret != 0 )
411 {
412 MBEDTLS_SSL_DEBUG_RET( 1, "hash computation error", ret );
Jerry Yu6f87f252021-10-29 20:12:51 +0800413 goto error;
Jerry Yu133690c2021-10-25 14:01:13 +0800414 }
415
Jerry Yu30b071c2021-09-12 20:16:03 +0800416 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
XiaokangQian82d34cc2021-11-03 08:51:56 +0000417#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
418 if( sig_alg == MBEDTLS_PK_RSASSA_PSS )
419 {
420 const mbedtls_md_info_t* md_info;
Xiaofei Baid25fab62021-12-02 06:36:27 +0000421 rsassa_pss_options.mgf1_hash_id = md_alg;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000422 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
423 {
424 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
425 }
Xiaofei Baid25fab62021-12-02 06:36:27 +0000426 rsassa_pss_options.expected_salt_len = mbedtls_md_get_size( md_info );
427 options = (const void*) &rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000428 }
429#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800430
Xiaofei Baid25fab62021-12-02 06:36:27 +0000431 if( ( ret = mbedtls_pk_verify_ext( sig_alg, options,
Jerry Yu30b071c2021-09-12 20:16:03 +0800432 &ssl->session_negotiate->peer_cert->pk,
433 md_alg, verify_hash, verify_hash_len,
Jerry Yu6f87f252021-10-29 20:12:51 +0800434 p, signature_len ) ) == 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800435 {
Jerry Yu6f87f252021-10-29 20:12:51 +0800436 return( 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800437 }
Jerry Yu6f87f252021-10-29 20:12:51 +0800438 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify_ext", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800439
Jerry Yu6f87f252021-10-29 20:12:51 +0800440error:
441 /* RFC 8446 section 4.4.3
442 *
443 * If the verification fails, the receiver MUST terminate the handshake
444 * with a "decrypt_error" alert.
445 */
446 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
447 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
448 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
449
Jerry Yu30b071c2021-09-12 20:16:03 +0800450}
451#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
452
453int mbedtls_ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
454{
Jerry Yu30b071c2021-09-12 20:16:03 +0800455
Jerry Yuda8cdf22021-10-25 15:06:49 +0800456#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
457 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
458 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
459 size_t verify_buffer_len;
460 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
461 size_t transcript_len;
462 unsigned char *buf;
463 size_t buf_len;
464
Jerry Yu30b071c2021-09-12 20:16:03 +0800465 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
466
Jerry Yuda8cdf22021-10-25 15:06:49 +0800467 MBEDTLS_SSL_PROC_CHK(
Xiaofei Bai746f9482021-11-12 08:53:56 +0000468 mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
Jerry Yuda8cdf22021-10-25 15:06:49 +0800469 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu30b071c2021-09-12 20:16:03 +0800470
Jerry Yuda8cdf22021-10-25 15:06:49 +0800471 /* Need to calculate the hash of the transcript first
Jerry Yu0b32c502021-10-28 13:41:59 +0800472 * before reading the message since otherwise it gets
473 * included in the transcript
474 */
Jerry Yuda8cdf22021-10-25 15:06:49 +0800475 ret = mbedtls_ssl_get_handshake_transcript( ssl,
476 ssl->handshake->ciphersuite_info->mac,
477 transcript, sizeof( transcript ),
478 &transcript_len );
479 if( ret != 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800480 {
Jerry Yuda8cdf22021-10-25 15:06:49 +0800481 MBEDTLS_SSL_PEND_FATAL_ALERT(
482 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
483 MBEDTLS_ERR_SSL_INTERNAL_ERROR );
484 return( ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800485 }
486
Jerry Yuda8cdf22021-10-25 15:06:49 +0800487 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash", transcript, transcript_len );
488
489 /* Create verify structure */
490 ssl_tls13_create_verify_structure( transcript,
Jerry Yu0b32c502021-10-28 13:41:59 +0800491 transcript_len,
492 verify_buffer,
493 &verify_buffer_len,
494 ( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) ?
495 MBEDTLS_SSL_IS_SERVER :
496 MBEDTLS_SSL_IS_CLIENT );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800497
498 /* Process the message contents */
Jerry Yu0b32c502021-10-28 13:41:59 +0800499 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_verify( ssl, buf,
500 buf + buf_len, verify_buffer, verify_buffer_len ) );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800501
Xiaofei Bai746f9482021-11-12 08:53:56 +0000502 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
Jerry Yuda8cdf22021-10-25 15:06:49 +0800503 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, buf, buf_len );
Jerry Yu30b071c2021-09-12 20:16:03 +0800504
505cleanup:
506
507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
Jerry Yu5398c102021-11-05 13:32:38 +0800508 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_process_certificate_verify", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800509 return( ret );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800510#else
511 ((void) ssl);
512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
513 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
514#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800515}
516
517/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000518 *
519 * STATE HANDLING: Incoming Certificate, client-side only currently.
520 *
521 */
522
523/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000524 * Implementation
525 */
526
Xiaofei Bai947571e2021-09-29 09:12:03 +0000527#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
528#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
529/*
530 * Structure of Certificate message:
531 *
532 * enum {
533 * X509(0),
534 * RawPublicKey(2),
535 * (255)
536 * } CertificateType;
537 *
538 * struct {
539 * select (certificate_type) {
540 * case RawPublicKey:
541 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
542 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
543 * case X509:
544 * opaque cert_data<1..2^24-1>;
545 * };
546 * Extension extensions<0..2^16-1>;
547 * } CertificateEntry;
548 *
549 * struct {
550 * opaque certificate_request_context<0..2^8-1>;
551 * CertificateEntry certificate_list<0..2^24-1>;
552 * } Certificate;
553 *
554 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000555
556/* Parse certificate chain send by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000557static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
558 const unsigned char *buf,
559 const unsigned char *end )
560{
561 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
562 size_t certificate_request_context_len = 0;
563 size_t certificate_list_len = 0;
564 const unsigned char *p = buf;
565 const unsigned char *certificate_list_end;
566
567 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
568 certificate_request_context_len = p[0];
Jerry Yub640bf62021-10-29 10:05:32 +0800569 certificate_list_len = MBEDTLS_GET_UINT24_BE( p, 1 );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000570 p += 4;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000571
572 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
573 * support anything beyond 2^16 = 64K.
574 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000575 if( ( certificate_request_context_len != 0 ) ||
576 ( certificate_list_len >= 0x10000 ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000577 {
578 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
579 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
580 MBEDTLS_ERR_SSL_DECODE_ERROR );
581 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
582 }
583
584 /* In case we tried to reuse a session but it failed */
585 if( ssl->session_negotiate->peer_cert != NULL )
586 {
587 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
588 mbedtls_free( ssl->session_negotiate->peer_cert );
589 }
590
591 if( ( ssl->session_negotiate->peer_cert =
592 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
593 {
594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
595 sizeof( mbedtls_x509_crt ) ) );
596 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
597 MBEDTLS_ERR_SSL_ALLOC_FAILED );
598 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
599 }
600
601 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
602
Xiaofei Bai947571e2021-09-29 09:12:03 +0000603 certificate_list_end = p + certificate_list_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000604 while( p < certificate_list_end )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000605 {
606 size_t cert_data_len, extensions_len;
607
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000608 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 3 );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000609 cert_data_len = MBEDTLS_GET_UINT24_BE( p, 0 );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000610 p += 3;
611
612 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
613 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
614 * check that we have a minimum of 128 bytes of data, this is not
615 * clear why we need that though.
616 */
617 if( ( cert_data_len < 128 ) || ( cert_data_len >= 0x10000 ) )
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000618 {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000619 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
620 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
621 MBEDTLS_ERR_SSL_DECODE_ERROR );
622 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
623 }
624
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000625 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, cert_data_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000626 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
627 p, cert_data_len );
628
629 switch( ret )
630 {
631 case 0: /*ok*/
632 break;
633 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
634 /* Ignore certificate with an unknown algorithm: maybe a
635 prior certificate was already trusted. */
636 break;
637
638 case MBEDTLS_ERR_X509_ALLOC_FAILED:
639 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
640 MBEDTLS_ERR_X509_ALLOC_FAILED );
641 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
642 return( ret );
643
644 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
645 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
646 MBEDTLS_ERR_X509_UNKNOWN_VERSION );
647 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
648 return( ret );
649
650 default:
651 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
652 ret );
653 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
654 return( ret );
655 }
656
657 p += cert_data_len;
658
659 /* Certificate extensions length */
660 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 2 );
661 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
662 p += 2;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000663 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, extensions_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000664 p += extensions_len;
665 }
666
667 /* Check that all the message is consumed. */
668 if( p != end )
669 {
670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
671 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, \
672 MBEDTLS_ERR_SSL_DECODE_ERROR );
673 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
674 }
675
676 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
677
678 return( ret );
679}
680#else
681static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
682 const unsigned char *buf,
683 const unsigned char *end )
684{
685 ((void) ssl);
686 ((void) buf);
687 ((void) end);
688 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
689}
690#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
691#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
692
693#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
694#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000695/* Validate certificate chain sent by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000696static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
697{
698 int ret = 0;
699 mbedtls_x509_crt *ca_chain;
700 mbedtls_x509_crl *ca_crl;
Xiaofei Baiff456022021-10-28 06:50:17 +0000701 uint32_t verify_result = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000702
703#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
704 if( ssl->handshake->sni_ca_chain != NULL )
705 {
706 ca_chain = ssl->handshake->sni_ca_chain;
707 ca_crl = ssl->handshake->sni_ca_crl;
708 }
709 else
710#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
711 {
712 ca_chain = ssl->conf->ca_chain;
713 ca_crl = ssl->conf->ca_crl;
714 }
715
716 /*
717 * Main check: verify certificate
718 */
719 ret = mbedtls_x509_crt_verify_with_profile(
720 ssl->session_negotiate->peer_cert,
721 ca_chain, ca_crl,
722 ssl->conf->cert_profile,
723 ssl->hostname,
Xiaofei Baiff456022021-10-28 06:50:17 +0000724 &verify_result,
Xiaofei Bai947571e2021-09-29 09:12:03 +0000725 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
726
727 if( ret != 0 )
728 {
729 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
730 }
731
732 /*
733 * Secondary checks: always done, but change 'ret' only if it was 0
734 */
735
736#if defined(MBEDTLS_ECP_C)
737 {
738 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
739
740 /* If certificate uses an EC key, make sure the curve is OK */
741 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
742 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
743 {
Xiaofei Baiff456022021-10-28 06:50:17 +0000744 verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000745
746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( EC key curve )" ) );
747 if( ret == 0 )
748 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
749 }
750 }
751#endif /* MBEDTLS_ECP_C */
752
753 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
754 ssl->handshake->ciphersuite_info,
755 !ssl->conf->endpoint,
Xiaofei Baiff456022021-10-28 06:50:17 +0000756 &verify_result ) != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000757 {
758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( usage extensions )" ) );
759 if( ret == 0 )
760 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
761 }
762
763
764 if( ca_chain == NULL )
765 {
766 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
767 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
768 }
769
770 if( ret != 0 )
771 {
772 /* The certificate may have been rejected for several reasons.
773 Pick one and send the corresponding alert. Which alert to send
774 may be a subject of debate in some cases. */
Xiaofei Baiff456022021-10-28 06:50:17 +0000775 if( verify_result & MBEDTLS_X509_BADCERT_OTHER )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000776 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000777 else if( verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000778 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000779 else if( verify_result & ( MBEDTLS_X509_BADCERT_KEY_USAGE |
780 MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
781 MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
782 MBEDTLS_X509_BADCERT_BAD_PK |
783 MBEDTLS_X509_BADCERT_BAD_KEY ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000784 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000785 else if( verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000786 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000787 else if( verify_result & MBEDTLS_X509_BADCERT_REVOKED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000788 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000789 else if( verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000790 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret );
791 else
792 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret );
793 }
794
795#if defined(MBEDTLS_DEBUG_C)
Xiaofei Baiff456022021-10-28 06:50:17 +0000796 if( verify_result != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000797 {
798 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
Jerry Yu83bb1312021-10-28 22:16:33 +0800799 (unsigned int) verify_result ) );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000800 }
801 else
802 {
803 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
804 }
805#endif /* MBEDTLS_DEBUG_C */
806
Xiaofei Baiff456022021-10-28 06:50:17 +0000807 ssl->session_negotiate->verify_result = verify_result;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000808 return( ret );
809}
810#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
811static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
812{
813 ((void) ssl);
814 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
815}
816#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
817#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
818
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000819int mbedtls_ssl_tls13_process_certificate( mbedtls_ssl_context *ssl )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000820{
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000821 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
823
824#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
825 unsigned char *buf;
826 size_t buf_len;
827
Xiaofei Bai746f9482021-11-12 08:53:56 +0000828 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000829 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
830 &buf, &buf_len ) );
831
832 /* Parse the certificate chain sent by the peer. */
833 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate( ssl, buf, buf + buf_len ) );
834 /* Validate the certificate chain and set the verification results. */
835 MBEDTLS_SSL_PROC_CHK( ssl_tls13_validate_certificate( ssl ) );
836
Xiaofei Bai746f9482021-11-12 08:53:56 +0000837 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
838 buf, buf_len );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000839
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000840cleanup:
841
842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Xiaofei Bai10aeec02021-10-26 09:50:08 +0000843#else
844 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
845 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
846#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000847 return( ret );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000848}
Jerry Yu90f152d2022-01-29 22:12:42 +0800849#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000850
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000851/*
Jerry Yu5cc35062022-01-28 16:16:08 +0800852 * STATE HANDLING: Output Certificate
853 */
854/* Check if a certificate should be written, and if yes,
855 * if it is available.
856 * Returns a negative error code on failure ( such as no certificate
857 * being available on the server ), and otherwise
Jerry Yu3e536442022-02-15 11:05:59 +0800858 * SSL_WRITE_CERTIFICATE_SEND or
Jerry Yu5cc35062022-01-28 16:16:08 +0800859 * SSL_WRITE_CERTIFICATE_SKIP
860 * indicating that a Certificate message should be written based
861 * on the configured certificate, or whether it should be silently skipped.
862 */
Jerry Yu3391ac02022-02-16 11:21:37 +0800863#define SSL_WRITE_CERTIFICATE_SEND 0
864#define SSL_WRITE_CERTIFICATE_SKIP 1
Jerry Yu5cc35062022-01-28 16:16:08 +0800865
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800866static int ssl_tls13_write_certificate_coordinate( mbedtls_ssl_context *ssl )
Jerry Yu5cc35062022-01-28 16:16:08 +0800867{
868
869 /* For PSK and ECDHE-PSK ciphersuites there is no certificate to exchange. */
870 if( mbedtls_ssl_tls13_some_psk_enabled( ssl ) )
871 {
872 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
873 return( SSL_WRITE_CERTIFICATE_SKIP );
874 }
875
Jerry Yu5cc35062022-01-28 16:16:08 +0800876#if defined(MBEDTLS_SSL_CLI_C)
877 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
878 {
879 /* The client MUST send a Certificate message if and only
880 * if the server has requested client authentication via a
881 * CertificateRequest message.
882 *
883 * client_auth indicates whether the server had requested
884 * client authentication.
885 */
886 if( ssl->handshake->client_auth == 0 )
887 {
888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
889 return( SSL_WRITE_CERTIFICATE_SKIP );
890 }
891 }
892#endif /* MBEDTLS_SSL_CLI_C */
893
Jerry Yu3e536442022-02-15 11:05:59 +0800894 return( SSL_WRITE_CERTIFICATE_SEND );
Jerry Yu90f152d2022-01-29 22:12:42 +0800895
Jerry Yu5cc35062022-01-28 16:16:08 +0800896}
897
Jerry Yu7399d0d2022-01-30 17:54:19 +0800898/*
899 * enum {
900 * X509(0),
901 * RawPublicKey(2),
902 * (255)
903 * } CertificateType;
904 *
905 * struct {
906 * select (certificate_type) {
907 * case RawPublicKey:
908 * // From RFC 7250 ASN.1_subjectPublicKeyInfo
909 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
910 *
911 * case X509:
912 * opaque cert_data<1..2^24-1>;
913 * };
914 * Extension extensions<0..2^16-1>;
915 * } CertificateEntry;
916 *
917 * struct {
918 * opaque certificate_request_context<0..2^8-1>;
919 * CertificateEntry certificate_list<0..2^24-1>;
920 * } Certificate;
921 */
922static int ssl_tls13_write_certificate_body( mbedtls_ssl_context *ssl,
Jerry Yu3e536442022-02-15 11:05:59 +0800923 unsigned char *buf,
Jerry Yu7399d0d2022-01-30 17:54:19 +0800924 unsigned char *end,
Jerry Yu3e536442022-02-15 11:05:59 +0800925 size_t *out_len )
Jerry Yu5cc35062022-01-28 16:16:08 +0800926{
Jerry Yu5cc35062022-01-28 16:16:08 +0800927 const mbedtls_x509_crt *crt = mbedtls_ssl_own_cert( ssl );
Jerry Yu3e536442022-02-15 11:05:59 +0800928 unsigned char *p = buf;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800929 unsigned char *certificate_request_context =
930 ssl->handshake->certificate_request_context;
931 unsigned char certificate_request_context_len =
932 ssl->handshake->certificate_request_context_len;
933 unsigned char *p_certificate_list_len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800934
Jerry Yu5cc35062022-01-28 16:16:08 +0800935
Jerry Yu3391ac02022-02-16 11:21:37 +0800936 /* ...
937 * opaque certificate_request_context<0..2^8-1>;
938 * ...
939 */
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800940 MBEDTLS_SSL_CHK_BUF_PTR( p, end, certificate_request_context_len + 1 );
941 *p++ = certificate_request_context_len;
942 if( certificate_request_context_len > 0 )
Jerry Yu537530d2022-02-15 14:00:57 +0800943 {
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800944 memcpy( p, certificate_request_context, certificate_request_context_len );
945 p += certificate_request_context_len;
Jerry Yu537530d2022-02-15 14:00:57 +0800946 }
947
Jerry Yu3391ac02022-02-16 11:21:37 +0800948 /* ...
949 * CertificateEntry certificate_list<0..2^24-1>;
950 * ...
951 */
Jerry Yu3e536442022-02-15 11:05:59 +0800952 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 );
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800953 p_certificate_list_len = p;
Jerry Yu3e536442022-02-15 11:05:59 +0800954 p += 3;
955
Jerry Yu7399d0d2022-01-30 17:54:19 +0800956 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", crt );
Jerry Yu5cc35062022-01-28 16:16:08 +0800957
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800958 while( crt != NULL )
Jerry Yu5cc35062022-01-28 16:16:08 +0800959 {
Jerry Yu7399d0d2022-01-30 17:54:19 +0800960 size_t cert_data_len = crt->raw.len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800961
Jerry Yu7399d0d2022-01-30 17:54:19 +0800962 MBEDTLS_SSL_CHK_BUF_PTR( p, end, cert_data_len + 3 + 2 );
963 MBEDTLS_PUT_UINT24_BE( cert_data_len, p, 0 );
964 p += 3;
Jerry Yu5cc35062022-01-28 16:16:08 +0800965
Jerry Yu7399d0d2022-01-30 17:54:19 +0800966 memcpy( p, crt->raw.p, cert_data_len );
967 p += cert_data_len;
968 crt = crt->next;
Jerry Yu5cc35062022-01-28 16:16:08 +0800969
970 /* Currently, we don't have any certificate extensions defined.
971 * Hence, we are sending an empty extension with length zero.
972 */
Jerry Yu7399d0d2022-01-30 17:54:19 +0800973 MBEDTLS_PUT_UINT24_BE( 0, p, 0 );
974 p += 2;
Jerry Yu5cc35062022-01-28 16:16:08 +0800975 }
Jerry Yu5cc35062022-01-28 16:16:08 +0800976
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800977 MBEDTLS_PUT_UINT24_BE( p - p_certificate_list_len - 3,
978 p_certificate_list_len, 0 );
Jerry Yu7399d0d2022-01-30 17:54:19 +0800979
Jerry Yu3e536442022-02-15 11:05:59 +0800980 *out_len = p - buf;
Jerry Yu5cc35062022-01-28 16:16:08 +0800981
982 return( 0 );
983}
Jerry Yu5cc35062022-01-28 16:16:08 +0800984
Jerry Yu3e536442022-02-15 11:05:59 +0800985static int ssl_tls13_finalize_write_certificate( mbedtls_ssl_context *ssl )
Jerry Yu5cc35062022-01-28 16:16:08 +0800986{
987#if defined(MBEDTLS_SSL_CLI_C)
988 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
989 {
Jerry Yuca133a32022-02-15 14:22:05 +0800990 const mbedtls_x509_crt *crt = mbedtls_ssl_own_cert( ssl );
991 if( ssl->handshake->client_auth && crt != NULL )
992 {
993 mbedtls_ssl_handshake_set_state( ssl,
994 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY );
995 }
996 else
997 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
Jerry Yu5cc35062022-01-28 16:16:08 +0800998 return( 0 );
999 }
1000 else
1001#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yu32e0c2d2022-01-29 22:28:16 +08001002 ((void) ssl);
Jerry Yu5cc35062022-01-28 16:16:08 +08001003 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1004}
1005
Jerry Yu3e536442022-02-15 11:05:59 +08001006int mbedtls_ssl_tls13_write_certificate( mbedtls_ssl_context *ssl )
Jerry Yu5cc35062022-01-28 16:16:08 +08001007{
1008 int ret;
1009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
1010
1011 /* Coordination: Check if we need to send a certificate. */
1012 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_write_certificate_coordinate( ssl ) );
1013
Jerry Yu3e536442022-02-15 11:05:59 +08001014 if( ret == SSL_WRITE_CERTIFICATE_SEND )
Jerry Yu5cc35062022-01-28 16:16:08 +08001015 {
1016 unsigned char *buf;
1017 size_t buf_len, msg_len;
1018
1019 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg( ssl,
1020 MBEDTLS_SSL_HS_CERTIFICATE, &buf, &buf_len ) );
1021
Jerry Yu7399d0d2022-01-30 17:54:19 +08001022 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_body( ssl,
1023 buf,
1024 buf + buf_len,
1025 &msg_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +08001026
1027 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
1028 MBEDTLS_SSL_HS_CERTIFICATE,
1029 buf,
1030 msg_len );
1031
1032 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_write_certificate( ssl ) );
1033 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg(
1034 ssl, buf_len, msg_len ) );
1035 }
1036 else
Jerry Yu5cc35062022-01-28 16:16:08 +08001037 {
1038 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
1039 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_write_certificate( ssl ) );
1040 }
1041
1042cleanup:
1043
1044 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
1045 return( ret );
1046}
1047
Jerry Yu3e536442022-02-15 11:05:59 +08001048/*
1049 * STATE HANDLING: Output Certificate Verify
1050 */
Jerry Yu3e536442022-02-15 11:05:59 +08001051static int ssl_tls13_write_certificate_verify_body( mbedtls_ssl_context *ssl,
1052 unsigned char *buf,
1053 unsigned char *end,
1054 size_t *out_len )
Jerry Yu8511f122022-01-29 10:01:04 +08001055{
1056 int ret;
Jerry Yu3e536442022-02-15 11:05:59 +08001057 unsigned char *p = buf;
Jerry Yu8511f122022-01-29 10:01:04 +08001058 mbedtls_pk_context *own_key;
Jerry Yu3e536442022-02-15 11:05:59 +08001059
Jerry Yu8511f122022-01-29 10:01:04 +08001060 unsigned char handshake_hash[ MBEDTLS_TLS1_3_MD_MAX_SIZE ];
1061 size_t handshake_hash_len;
Jerry Yu3e536442022-02-15 11:05:59 +08001062 unsigned char verify_buffer[ SSL_VERIFY_STRUCT_MAX_SIZE ];
1063 size_t verify_buffer_len;
Jerry Yu7db5b8f2022-02-17 18:30:13 +08001064 unsigned char signature_type;
Jerry Yu3e536442022-02-15 11:05:59 +08001065 size_t own_key_size;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +08001066 mbedtls_md_type_t md_alg;
Jerry Yu78272072022-02-22 10:28:13 +08001067 uint16_t algorithm = MBEDTLS_TLS1_3_SIG_NONE;
Jerry Yu3e536442022-02-15 11:05:59 +08001068 size_t signature_len = 0;
Jerry Yu8511f122022-01-29 10:01:04 +08001069 const mbedtls_md_info_t *md_info;
Jerry Yu3391ac02022-02-16 11:21:37 +08001070 unsigned char verify_hash[ MBEDTLS_MD_MAX_SIZE ];
Jerry Yu3e536442022-02-15 11:05:59 +08001071 size_t verify_hash_len;
Jerry Yu8511f122022-01-29 10:01:04 +08001072
Jerry Yu3e536442022-02-15 11:05:59 +08001073 own_key = mbedtls_ssl_own_key( ssl );
1074 if( own_key == NULL )
Jerry Yu8511f122022-01-29 10:01:04 +08001075 {
Jerry Yu3e536442022-02-15 11:05:59 +08001076 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1077 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu8511f122022-01-29 10:01:04 +08001078 }
1079
Jerry Yu8511f122022-01-29 10:01:04 +08001080 ret = mbedtls_ssl_get_handshake_transcript( ssl,
1081 ssl->handshake->ciphersuite_info->mac,
1082 handshake_hash,
1083 sizeof( handshake_hash ),
1084 &handshake_hash_len );
1085 if( ret != 0 )
1086 return( ret );
1087
1088 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash",
1089 handshake_hash,
1090 handshake_hash_len);
1091
Jerry Yu8511f122022-01-29 10:01:04 +08001092 ssl_tls13_create_verify_structure( handshake_hash, handshake_hash_len,
1093 verify_buffer, &verify_buffer_len,
1094 ssl->conf->endpoint );
1095
1096 /*
1097 * struct {
1098 * SignatureScheme algorithm;
1099 * opaque signature<0..2^16-1>;
1100 * } CertificateVerify;
1101 */
Jerry Yu7db5b8f2022-02-17 18:30:13 +08001102 signature_type = mbedtls_ssl_sig_from_pk( own_key );
Jerry Yu78272072022-02-22 10:28:13 +08001103 /* Determine the size of the key */
1104 own_key_size = mbedtls_pk_get_bitlen( own_key );
Jerry Yu7db5b8f2022-02-17 18:30:13 +08001105 switch( signature_type )
Jerry Yu8511f122022-01-29 10:01:04 +08001106 {
Jerry Yu7db5b8f2022-02-17 18:30:13 +08001107#if defined(MBEDTLS_ECDSA_C)
1108 case MBEDTLS_SSL_SIG_ECDSA:
Jerry Yu7db5b8f2022-02-17 18:30:13 +08001109 switch( own_key_size )
1110 {
1111 case 256:
1112 md_alg = MBEDTLS_MD_SHA256;
1113 algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256;
1114 break;
1115 case 384:
1116 md_alg = MBEDTLS_MD_SHA384;
1117 algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384;
1118 break;
1119 case 521:
1120 md_alg = MBEDTLS_MD_SHA512;
1121 algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512;
1122 break;
1123 default:
1124 MBEDTLS_SSL_DEBUG_MSG( 3,
1125 ( "unknown key size: %"
1126 MBEDTLS_PRINTF_SIZET " bits",
1127 own_key_size ) );
Jerry Yu78272072022-02-22 10:28:13 +08001128 break;
Jerry Yu7db5b8f2022-02-17 18:30:13 +08001129 }
Jerry Yu3e536442022-02-15 11:05:59 +08001130 break;
Jerry Yu7db5b8f2022-02-17 18:30:13 +08001131#endif /* MBEDTLS_ECDSA_C */
Jerry Yu3e536442022-02-15 11:05:59 +08001132
Jerry Yu7db5b8f2022-02-17 18:30:13 +08001133#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
1134 case MBEDTLS_SSL_SIG_RSA:
Jerry Yu2124d052022-02-18 21:07:18 +08001135 if( own_key_size <= 2048 &&
1136 mbedtls_ssl_sig_alg_is_received( ssl,
1137 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256 ) )
Jerry Yu7db5b8f2022-02-17 18:30:13 +08001138 {
Jerry Yu2124d052022-02-18 21:07:18 +08001139 md_alg = MBEDTLS_MD_SHA256;
1140 algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256;
Jerry Yu7db5b8f2022-02-17 18:30:13 +08001141 }
Jerry Yu2124d052022-02-18 21:07:18 +08001142 else if( own_key_size <= 3072 &&
1143 mbedtls_ssl_sig_alg_is_received( ssl,
1144 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384 ) )
1145 {
1146 md_alg = MBEDTLS_MD_SHA384;
1147 algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384;
1148 }
1149 else if( own_key_size <= 4096 &&
1150 mbedtls_ssl_sig_alg_is_received( ssl,
1151 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512 ) )
1152 {
1153 md_alg = MBEDTLS_MD_SHA512;
1154 algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512;
1155 }
1156 else
1157 {
1158 MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown key size: %"
1159 MBEDTLS_PRINTF_SIZET " bits",
1160 own_key_size ) );
Jerry Yu78272072022-02-22 10:28:13 +08001161 break;
Jerry Yu2124d052022-02-18 21:07:18 +08001162 }
1163
Jerry Yu7db5b8f2022-02-17 18:30:13 +08001164 if( mbedtls_rsa_set_padding( mbedtls_pk_rsa( *own_key ),
1165 MBEDTLS_RSA_PKCS_V21,
1166 md_alg ) != 0 )
1167 {
1168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Set RSA padding Fail" ) );
Jerry Yu2124d052022-02-18 21:07:18 +08001169 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu7db5b8f2022-02-17 18:30:13 +08001170 }
1171 break;
1172#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
1173 default:
1174 MBEDTLS_SSL_DEBUG_MSG( 1,
1175 ( "unkown pk type : %d", signature_type ) );
1176 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1177 }
Jerry Yu3e536442022-02-15 11:05:59 +08001178
Jerry Yu78272072022-02-22 10:28:13 +08001179 if( algorithm == MBEDTLS_TLS1_3_SIG_NONE ||
1180 ! mbedtls_ssl_sig_alg_is_received( ssl, algorithm ) )
Jerry Yu8511f122022-01-29 10:01:04 +08001181 {
Jerry Yud66409a2022-02-18 16:42:24 +08001182 MBEDTLS_SSL_DEBUG_MSG( 1,
Jerry Yu2124d052022-02-18 21:07:18 +08001183 ( "signature algorithm not in received or offered list." ) );
Jerry Yud66409a2022-02-18 16:42:24 +08001184 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1185 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu3391ac02022-02-16 11:21:37 +08001186 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu8511f122022-01-29 10:01:04 +08001187 }
1188
Jerry Yu3391ac02022-02-16 11:21:37 +08001189 /* Check there is space for the algorithm identifier (2 bytes) and the
1190 * signature length (2 bytes).
1191 */
1192 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Jerry Yu3e536442022-02-15 11:05:59 +08001193 MBEDTLS_PUT_UINT16_BE( algorithm, p, 0 );
1194 p += 2;
Jerry Yu8511f122022-01-29 10:01:04 +08001195
1196 /* Hash verify buffer with indicated hash function */
1197 md_info = mbedtls_md_info_from_type( md_alg );
1198 if( md_info == NULL )
1199 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1200
1201 ret = mbedtls_md( md_info, verify_buffer, verify_buffer_len, verify_hash );
1202 if( ret != 0 )
1203 return( ret );
1204
1205 verify_hash_len = mbedtls_md_get_size( md_info );
1206 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
1207
1208 if( ( ret = mbedtls_pk_sign( own_key, md_alg,
1209 verify_hash, verify_hash_len,
Jerry Yu3e536442022-02-15 11:05:59 +08001210 p + 2, (size_t)( end - ( p + 2 ) ), &signature_len,
Jerry Yu8511f122022-01-29 10:01:04 +08001211 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
1212 {
1213 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
1214 return( ret );
1215 }
Jerry Yu8511f122022-01-29 10:01:04 +08001216
Jerry Yu3e536442022-02-15 11:05:59 +08001217 MBEDTLS_PUT_UINT16_BE( signature_len, p, 0 );
1218 p += 2 + signature_len;
Jerry Yu8511f122022-01-29 10:01:04 +08001219
Jerry Yu3e536442022-02-15 11:05:59 +08001220 *out_len = (size_t)( p - buf );
1221
Jerry Yu8511f122022-01-29 10:01:04 +08001222 return( ret );
1223}
Jerry Yu8511f122022-01-29 10:01:04 +08001224
Jerry Yu3e536442022-02-15 11:05:59 +08001225static int ssl_tls13_finalize_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu8511f122022-01-29 10:01:04 +08001226{
1227#if defined(MBEDTLS_SSL_CLI_C)
1228 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
1229 {
1230 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
1231 }
1232 else
1233#endif /* MBEDTLS_SSL_CLI_C */
1234 {
1235 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
1236 }
1237
1238 return( 0 );
1239}
1240
Jerry Yu3e536442022-02-15 11:05:59 +08001241int mbedtls_ssl_tls13_write_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu8511f122022-01-29 10:01:04 +08001242{
1243 int ret = 0;
Jerry Yuca133a32022-02-15 14:22:05 +08001244 unsigned char *buf;
1245 size_t buf_len, msg_len;
1246
Jerry Yu8511f122022-01-29 10:01:04 +08001247 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
1248
Jerry Yuca133a32022-02-15 14:22:05 +08001249 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg( ssl,
1250 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001251
Jerry Yuca133a32022-02-15 14:22:05 +08001252 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_verify_body(
1253 ssl, buf, buf + buf_len, &msg_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001254
Jerry Yuca133a32022-02-15 14:22:05 +08001255 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
1256 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, buf, msg_len );
1257 /* Update state */
1258 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_certificate_verify( ssl ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001259
Jerry Yuca133a32022-02-15 14:22:05 +08001260 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg(
1261 ssl, buf_len, msg_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001262
1263cleanup:
1264
1265 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
1266 return( ret );
1267}
1268
Jerry Yu90f152d2022-01-29 22:12:42 +08001269#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1270
Jerry Yu5cc35062022-01-28 16:16:08 +08001271/*
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001272 *
XiaokangQianc5c39d52021-11-09 11:55:10 +00001273 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001274 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001275/*
1276 * Implementation
1277 */
1278
XiaokangQianaaa0e192021-11-10 03:07:04 +00001279static int ssl_tls13_preprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001280{
1281 int ret;
1282
XiaokangQianc5c39d52021-11-09 11:55:10 +00001283 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001284 ssl->handshake->state_local.finished_in.digest,
1285 sizeof( ssl->handshake->state_local.finished_in.digest ),
1286 &ssl->handshake->state_local.finished_in.digest_len,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001287 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
XiaokangQianc13f9352021-11-11 06:13:22 +00001288 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001289 if( ret != 0 )
1290 {
XiaokangQianc5c39d52021-11-09 11:55:10 +00001291 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_calculate_verify_data", ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001292 return( ret );
1293 }
1294
1295 return( 0 );
1296}
1297
XiaokangQianc5c39d52021-11-09 11:55:10 +00001298static int ssl_tls13_parse_finished_message( mbedtls_ssl_context *ssl,
1299 const unsigned char *buf,
1300 const unsigned char *end )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001301{
XiaokangQian33062842021-11-11 03:37:45 +00001302 /*
1303 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +00001304 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +00001305 * } Finished;
1306 */
1307 const unsigned char *expected_verify_data =
1308 ssl->handshake->state_local.finished_in.digest;
1309 size_t expected_verify_data_len =
1310 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001311 /* Structural validation */
XiaokangQian33062842021-11-11 03:37:45 +00001312 if( (size_t)( end - buf ) != expected_verify_data_len )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001313 {
1314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1315
1316 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +00001317 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001318 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
1319 }
1320
XiaokangQianc5c39d52021-11-09 11:55:10 +00001321 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (self-computed):",
XiaokangQian33062842021-11-11 03:37:45 +00001322 expected_verify_data,
1323 expected_verify_data_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001324 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (received message):", buf,
XiaokangQian33062842021-11-11 03:37:45 +00001325 expected_verify_data_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001326
1327 /* Semantic validation */
Gabor Mezei685472b2021-11-24 11:17:36 +01001328 if( mbedtls_ct_memcmp( buf,
1329 expected_verify_data,
1330 expected_verify_data_len ) != 0 )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001331 {
1332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1333
XiaokangQian33062842021-11-11 03:37:45 +00001334 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +00001335 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001336 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1337 }
1338 return( 0 );
1339}
1340
XiaokangQianc13f9352021-11-11 06:13:22 +00001341#if defined(MBEDTLS_SSL_CLI_C)
XiaokangQianaaa0e192021-11-10 03:07:04 +00001342static int ssl_tls13_postprocess_server_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001343{
XiaokangQian33062842021-11-11 03:37:45 +00001344 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001345 mbedtls_ssl_key_set traffic_keys;
XiaokangQian1aef02e2021-10-28 09:54:34 +00001346 mbedtls_ssl_transform *transform_application = NULL;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001347
XiaokangQian4cab0242021-10-12 08:43:37 +00001348 ret = mbedtls_ssl_tls13_key_schedule_stage_application( ssl );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001349 if( ret != 0 )
1350 {
1351 MBEDTLS_SSL_DEBUG_RET( 1,
XiaokangQian4cab0242021-10-12 08:43:37 +00001352 "mbedtls_ssl_tls13_key_schedule_stage_application", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001353 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001354 }
1355
XiaokangQian33062842021-11-11 03:37:45 +00001356 ret = mbedtls_ssl_tls13_generate_application_keys( ssl, &traffic_keys );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001357 if( ret != 0 )
1358 {
1359 MBEDTLS_SSL_DEBUG_RET( 1,
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001360 "mbedtls_ssl_tls13_generate_application_keys", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001361 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001362 }
1363
1364 transform_application =
1365 mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
1366 if( transform_application == NULL )
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001367 {
1368 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1369 goto cleanup;
1370 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001371
1372 ret = mbedtls_ssl_tls13_populate_transform(
1373 transform_application,
1374 ssl->conf->endpoint,
1375 ssl->session_negotiate->ciphersuite,
1376 &traffic_keys,
1377 ssl );
1378 if( ret != 0 )
1379 {
1380 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001381 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001382 }
1383
1384 ssl->transform_application = transform_application;
1385
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001386cleanup:
1387
XiaokangQian33062842021-11-11 03:37:45 +00001388 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001389 if( ret != 0 )
XiaokangQian1aef02e2021-10-28 09:54:34 +00001390 {
1391 mbedtls_free( transform_application );
1392 MBEDTLS_SSL_PEND_FATAL_ALERT(
1393 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1394 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1395 }
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001396 return( ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001397}
XiaokangQianc13f9352021-11-11 06:13:22 +00001398#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001399
XiaokangQiancc90c942021-11-09 12:30:09 +00001400static int ssl_tls13_postprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001401{
1402
XiaokangQianc13f9352021-11-11 06:13:22 +00001403#if defined(MBEDTLS_SSL_CLI_C)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001404 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
1405 {
XiaokangQianaaa0e192021-11-10 03:07:04 +00001406 return( ssl_tls13_postprocess_server_finished_message( ssl ) );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001407 }
XiaokangQianc13f9352021-11-11 06:13:22 +00001408#else
1409 ((void) ssl);
1410#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001411
1412 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1413}
1414
XiaokangQianc5c39d52021-11-09 11:55:10 +00001415int mbedtls_ssl_tls13_process_finished_message( mbedtls_ssl_context *ssl )
1416{
XiaokangQian33062842021-11-11 03:37:45 +00001417 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001418 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +00001419 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001420
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001421 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001422
1423 /* Preprocessing step: Compute handshake digest */
XiaokangQianaaa0e192021-11-10 03:07:04 +00001424 MBEDTLS_SSL_PROC_CHK( ssl_tls13_preprocess_finished_message( ssl ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001425
Xiaofei Bai746f9482021-11-12 08:53:56 +00001426 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001427 MBEDTLS_SSL_HS_FINISHED,
Xiaofei Baieef15042021-11-18 07:29:56 +00001428 &buf, &buf_len ) );
1429 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_finished_message( ssl, buf, buf + buf_len ) );
Xiaofei Bai746f9482021-11-12 08:53:56 +00001430 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
Xiaofei Baieef15042021-11-18 07:29:56 +00001431 ssl, MBEDTLS_SSL_HS_FINISHED, buf, buf_len );
XiaokangQianaaa0e192021-11-10 03:07:04 +00001432 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_finished_message( ssl ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001433
1434cleanup:
1435
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001437 return( ret );
1438}
1439
XiaokangQian74af2a82021-09-22 07:40:30 +00001440/*
1441 *
XiaokangQiancc90c942021-11-09 12:30:09 +00001442 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +00001443 *
1444 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001445/*
XiaokangQian35dc6252021-11-11 08:16:19 +00001446 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +00001447 */
1448
XiaokangQian8773aa02021-11-10 07:33:09 +00001449static int ssl_tls13_prepare_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +00001450{
1451 int ret;
1452
1453 /* Compute transcript of handshake up to now. */
XiaokangQiancc90c942021-11-09 12:30:09 +00001454 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQian74af2a82021-09-22 07:40:30 +00001455 ssl->handshake->state_local.finished_out.digest,
1456 sizeof( ssl->handshake->state_local.finished_out.digest ),
1457 &ssl->handshake->state_local.finished_out.digest_len,
1458 ssl->conf->endpoint );
1459
1460 if( ret != 0 )
1461 {
Jerry Yu7ca30542021-12-08 15:57:57 +08001462 MBEDTLS_SSL_DEBUG_RET( 1, "calculate_verify_data failed", ret );
XiaokangQian74af2a82021-09-22 07:40:30 +00001463 return( ret );
1464 }
1465
1466 return( 0 );
1467}
1468
XiaokangQiancc90c942021-11-09 12:30:09 +00001469static int ssl_tls13_finalize_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +00001470{
XiaokangQian0fa66432021-11-15 03:33:57 +00001471 // TODO: Add back resumption keys calculation after MVP.
1472 ((void) ssl);
XiaokangQian74af2a82021-09-22 07:40:30 +00001473
1474 return( 0 );
1475}
1476
XiaokangQian8773aa02021-11-10 07:33:09 +00001477static int ssl_tls13_write_finished_message_body( mbedtls_ssl_context *ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001478 unsigned char *buf,
1479 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001480 size_t *out_len )
XiaokangQian74af2a82021-09-22 07:40:30 +00001481{
XiaokangQian8773aa02021-11-10 07:33:09 +00001482 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001483 /*
1484 * struct {
1485 * opaque verify_data[Hash.length];
1486 * } Finished;
1487 */
XiaokangQian8773aa02021-11-10 07:33:09 +00001488 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001489
1490 memcpy( buf, ssl->handshake->state_local.finished_out.digest,
XiaokangQian8773aa02021-11-10 07:33:09 +00001491 verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001492
Xiaofei Baid25fab62021-12-02 06:36:27 +00001493 *out_len = verify_data_len;
XiaokangQian74af2a82021-09-22 07:40:30 +00001494 return( 0 );
1495}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001496
XiaokangQian35dc6252021-11-11 08:16:19 +00001497/* Main entry point: orchestrates the other functions */
1498int mbedtls_ssl_tls13_write_finished_message( mbedtls_ssl_context *ssl )
1499{
1500 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1501 unsigned char *buf;
1502 size_t buf_len, msg_len;
1503
1504 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished message" ) );
1505
XiaokangQiandce82242021-11-15 06:01:26 +00001506 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_finished_message( ssl ) );
1507
XiaokangQian35dc6252021-11-11 08:16:19 +00001508 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg( ssl,
1509 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len ) );
1510
1511 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_finished_message_body(
1512 ssl, buf, buf + buf_len, &msg_len ) );
1513
Xiaofei Bai746f9482021-11-12 08:53:56 +00001514 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1515 buf, msg_len );
XiaokangQian35dc6252021-11-11 08:16:19 +00001516
1517 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_finished_message( ssl ) );
1518 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
1519 buf_len, msg_len ) );
1520 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_flush_output( ssl ) );
1521
1522cleanup:
1523
1524 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished message" ) );
1525 return( ret );
1526}
1527
Jerry Yu378254d2021-10-30 21:44:47 +08001528void mbedtls_ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
1529{
1530
1531 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
1532
1533 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001534 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001535 */
1536 if( ssl->session )
1537 {
Jerry Yu378254d2021-10-30 21:44:47 +08001538 mbedtls_ssl_session_free( ssl->session );
1539 mbedtls_free( ssl->session );
1540 }
1541 ssl->session = ssl->session_negotiate;
1542 ssl->session_negotiate = NULL;
1543
1544 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
1545}
1546
Ronald Cron49ad6192021-11-24 16:25:31 +01001547/*
1548 *
1549 * STATE HANDLING: Write ChangeCipherSpec
1550 *
1551 */
1552#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1553
XiaokangQian355e09a2022-01-20 11:14:50 +00001554static int ssl_tls13_finalize_change_cipher_spec( mbedtls_ssl_context* ssl )
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001555{
1556
1557#if defined(MBEDTLS_SSL_CLI_C)
1558 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
1559 {
1560 switch( ssl->state )
1561 {
1562 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
1563 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
1564 break;
1565 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
Jerry Yu566c7812022-01-26 15:41:22 +08001566#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yuca133a32022-02-15 14:22:05 +08001567 mbedtls_ssl_handshake_set_state( ssl,
Jerry Yu566c7812022-01-26 15:41:22 +08001568 MBEDTLS_SSL_CLIENT_CERTIFICATE );
Jerry Yuca133a32022-02-15 14:22:05 +08001569#else
1570 mbedtls_ssl_handshake_set_state( ssl,
1571 MBEDTLS_SSL_CLIENT_FINISHED );
Jerry Yu566c7812022-01-26 15:41:22 +08001572#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yuca133a32022-02-15 14:22:05 +08001573
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001574 break;
1575 default:
1576 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1577 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1578 }
1579 }
1580#else
1581 ((void) ssl);
1582#endif /* MBEDTLS_SSL_CLI_C */
1583
1584 return( 0 );
1585}
1586
Ronald Cron49ad6192021-11-24 16:25:31 +01001587static int ssl_tls13_write_change_cipher_spec_body( mbedtls_ssl_context *ssl,
1588 unsigned char *buf,
1589 unsigned char *end,
1590 size_t *olen )
1591{
1592 ((void) ssl);
1593
1594 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 1 );
1595 buf[0] = 1;
1596 *olen = 1;
1597
1598 return( 0 );
1599}
1600
Ronald Cron49ad6192021-11-24 16:25:31 +01001601int mbedtls_ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1602{
1603 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1604
1605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
1606
1607 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_flush_output( ssl ) );
1608
1609 /* Write CCS message */
1610 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_change_cipher_spec_body(
1611 ssl, ssl->out_msg,
1612 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1613 &ssl->out_msglen ) );
1614
1615 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1616
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001617 /* Update state */
XiaokangQian355e09a2022-01-20 11:14:50 +00001618 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_change_cipher_spec( ssl ) );
XiaokangQian0b56a8f2021-12-22 02:39:32 +00001619
Ronald Cron49ad6192021-11-24 16:25:31 +01001620 /* Dispatch message */
1621 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_record( ssl, 1 ) );
1622
1623cleanup:
1624
1625 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
1626 return( ret );
1627}
1628
1629#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1630
XiaokangQian78b1fa72022-01-19 06:56:30 +00001631/* Reset SSL context and update hash for handling HRR.
1632 *
1633 * Replace Transcript-Hash(X) by
1634 * Transcript-Hash( message_hash ||
1635 * 00 00 Hash.length ||
1636 * X )
1637 * A few states of the handshake are preserved, including:
1638 * - session ID
1639 * - session ticket
1640 * - negotiated ciphersuite
1641 */
1642int mbedtls_ssl_reset_transcript_for_hrr( mbedtls_ssl_context *ssl )
1643{
1644 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1645 unsigned char hash_transcript[ MBEDTLS_MD_MAX_SIZE + 4 ];
XiaokangQian0ece9982022-01-24 08:56:23 +00001646 size_t hash_len;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001647 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1648 uint16_t cipher_suite = ssl->session_negotiate->ciphersuite;
1649 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
1650
1651 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Reset SSL session for HRR" ) );
1652
XiaokangQian0ece9982022-01-24 08:56:23 +00001653 ret = mbedtls_ssl_get_handshake_transcript( ssl, ciphersuite_info->mac,
1654 hash_transcript + 4,
1655 MBEDTLS_MD_MAX_SIZE,
1656 &hash_len );
1657 if( ret != 0 )
1658 {
1659 MBEDTLS_SSL_DEBUG_RET( 4, "mbedtls_ssl_get_handshake_transcript", ret );
1660 return( ret );
1661 }
1662
1663 hash_transcript[0] = MBEDTLS_SSL_HS_MESSAGE_HASH;
1664 hash_transcript[1] = 0;
1665 hash_transcript[2] = 0;
1666 hash_transcript[3] = (unsigned char) hash_len;
1667
1668 hash_len += 4;
1669
XiaokangQian78b1fa72022-01-19 06:56:30 +00001670 if( ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
1671 {
1672#if defined(MBEDTLS_SHA256_C)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001673 MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-256 handshake transcript",
XiaokangQian0ece9982022-01-24 08:56:23 +00001674 hash_transcript, hash_len );
XiaokangQian78b1fa72022-01-19 06:56:30 +00001675
1676#if defined(MBEDTLS_USE_PSA_CRYPTO)
1677 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
1678 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
1679#else
1680 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
1681#endif
XiaokangQian78b1fa72022-01-19 06:56:30 +00001682#endif /* MBEDTLS_SHA256_C */
1683 }
1684 else if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1685 {
1686#if defined(MBEDTLS_SHA384_C)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001687 MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-384 handshake transcript",
XiaokangQian0ece9982022-01-24 08:56:23 +00001688 hash_transcript, hash_len );
XiaokangQian78b1fa72022-01-19 06:56:30 +00001689
1690#if defined(MBEDTLS_USE_PSA_CRYPTO)
1691 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
1692 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
1693#else
1694 mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 );
1695#endif
XiaokangQian78b1fa72022-01-19 06:56:30 +00001696#endif /* MBEDTLS_SHA384_C */
1697 }
1698
XiaokangQian0ece9982022-01-24 08:56:23 +00001699#if defined(MBEDTLS_SHA256_C) || defined(MBEDTLS_SHA384_C)
1700 ssl->handshake->update_checksum( ssl, hash_transcript, hash_len );
1701#endif /* MBEDTLS_SHA256_C || MBEDTLS_SHA384_C */
XiaokangQian78b1fa72022-01-19 06:56:30 +00001702 return( ret );
1703}
1704
Jerry Yufb4b6472022-01-27 15:03:26 +08001705#endif /* MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_PROTO_TLS1_3 */