blob: e71456e1a94623a3cda85f19ab326207f990c33e [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 Yu67eced02022-02-25 13:37:36 +080035#include "ssl_debug_helpers.h"
Jerry Yu65dd2cc2021-08-18 16:38:40 +080036
Xiaofei Bai746f9482021-11-12 08:53:56 +000037int mbedtls_ssl_tls13_fetch_handshake_msg( mbedtls_ssl_context *ssl,
38 unsigned hs_type,
39 unsigned char **buf,
Xiaofei Baieef15042021-11-18 07:29:56 +000040 size_t *buf_len )
XiaokangQian6b226b02021-09-24 07:51:16 +000041{
42 int ret;
43
44 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
45 {
46 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
47 goto cleanup;
48 }
49
XiaokangQian16c61aa2021-09-27 09:30:17 +000050 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
XiaokangQian6b226b02021-09-24 07:51:16 +000051 ssl->in_msg[0] != hs_type )
52 {
XiaokangQian16c61aa2021-09-27 09:30:17 +000053 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Receive unexpected handshake message." ) );
XiaokangQian6b226b02021-09-24 07:51:16 +000054 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
XiaokangQian05420b12021-09-29 08:46:37 +000055 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
XiaokangQian6b226b02021-09-24 07:51:16 +000056 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
57 goto cleanup;
58 }
59
XiaokangQian05420b12021-09-29 08:46:37 +000060 /*
61 * Jump handshake header (4 bytes, see Section 4 of RFC 8446).
62 * ...
63 * HandshakeType msg_type;
64 * uint24 length;
65 * ...
66 */
Xiaofei Baieef15042021-11-18 07:29:56 +000067 *buf = ssl->in_msg + 4;
68 *buf_len = ssl->in_hslen - 4;
XiaokangQian6b226b02021-09-24 07:51:16 +000069
XiaokangQian6b226b02021-09-24 07:51:16 +000070cleanup:
71
72 return( ret );
73}
74
Jerry Yuf4436812021-08-26 22:59:56 +080075int mbedtls_ssl_tls13_start_handshake_msg( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +080076 unsigned hs_type,
77 unsigned char **buf,
Jerry Yu0c63af62021-09-02 12:59:12 +080078 size_t *buf_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +080079{
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080080 /*
81 * Reserve 4 bytes for hanshake header. ( Section 4,RFC 8446 )
82 * ...
83 * HandshakeType msg_type;
84 * uint24 length;
85 * ...
86 */
Jerry Yuc8a392c2021-08-18 16:46:28 +080087 *buf = ssl->out_msg + 4;
Jerry Yu0c63af62021-09-02 12:59:12 +080088 *buf_len = MBEDTLS_SSL_OUT_CONTENT_LEN - 4;
Jerry Yuc8a392c2021-08-18 16:46:28 +080089
90 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
91 ssl->out_msg[0] = hs_type;
92
93 return( 0 );
Jerry Yu65dd2cc2021-08-18 16:38:40 +080094}
95
Jerry Yuf4436812021-08-26 22:59:56 +080096int mbedtls_ssl_tls13_finish_handshake_msg( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +080097 size_t buf_len,
98 size_t msg_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +080099{
Jerry Yuc8a392c2021-08-18 16:46:28 +0800100 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baieef15042021-11-18 07:29:56 +0000101 size_t msg_with_header_len;
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800102 ((void) buf_len);
Jerry Yuc8a392c2021-08-18 16:46:28 +0800103
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800104 /* Add reserved 4 bytes for handshake header */
Xiaofei Baieef15042021-11-18 07:29:56 +0000105 msg_with_header_len = msg_len + 4;
106 ssl->out_msglen = msg_with_header_len;
Ronald Cron66dbf912022-02-02 15:33:46 +0100107 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_handshake_msg_ext( ssl, 0, 0 ) );
Jerry Yuc8a392c2021-08-18 16:46:28 +0800108
109cleanup:
110 return( ret );
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800111}
112
Xiaofei Bai746f9482021-11-12 08:53:56 +0000113void mbedtls_ssl_tls13_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
114 unsigned hs_type,
115 unsigned char const *msg,
116 size_t msg_len )
Jerry Yu7bea4ba2021-09-09 15:06:18 +0800117{
118 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
119 ssl->handshake->update_checksum( ssl, msg, msg_len );
120}
121
Jerry Yuf4436812021-08-26 22:59:56 +0800122void mbedtls_ssl_tls13_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800123 unsigned hs_type,
124 size_t total_hs_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800125{
126 unsigned char hs_hdr[4];
127
128 /* Build HS header for checksum update. */
Jerry Yu2ac64192021-08-26 18:38:58 +0800129 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
130 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
131 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
132 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800133
134 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
135}
136
Jerry Yubc20bdd2021-08-24 15:59:48 +0800137#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000138/* mbedtls_ssl_tls13_parse_sig_alg_ext()
139 *
140 * enum {
141 * ....
142 * ecdsa_secp256r1_sha256( 0x0403 ),
143 * ecdsa_secp384r1_sha384( 0x0503 ),
144 * ecdsa_secp521r1_sha512( 0x0603 ),
145 * ....
146 * } SignatureScheme;
147 *
148 * struct {
149 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
150 * } SignatureSchemeList;
151 */
152int mbedtls_ssl_tls13_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
153 const unsigned char *buf,
154 const unsigned char *end )
155{
156 const unsigned char *p = buf;
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000157 size_t supported_sig_algs_len = 0;
158 const unsigned char *supported_sig_algs_end;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +0000159 uint16_t sig_alg;
Xiaofei Baic234ecf2022-02-08 09:59:23 +0000160 uint32_t common_idx = 0;
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000161
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000162 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000163 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000164 p += 2;
165
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000166 memset( ssl->handshake->received_sig_algs, 0,
Xiaofei Bai51f515a2022-02-08 07:28:04 +0000167 sizeof(ssl->handshake->received_sig_algs) );
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000168
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000169 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
170 supported_sig_algs_end = p + supported_sig_algs_len;
Xiaofei Bai51f515a2022-02-08 07:28:04 +0000171 while( p < supported_sig_algs_end )
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000172 {
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000173 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +0000174 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000175 p += 2;
176
177 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x",
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +0000178 sig_alg ) );
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000179
Xiaofei Bai51f515a2022-02-08 07:28:04 +0000180 if( ! mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ||
181 ! mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) )
182 continue;
183
184 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
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 {
Jerry Yu0c23fc32022-03-23 12:20:01 +0800341#if defined(MBEDTLS_ECDSA_C)
Xiaofei Bai746f9482021-11-12 08:53:56 +0000342 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Jerry Yu30b071c2021-09-12 20:16:03 +0800343 md_alg = MBEDTLS_MD_SHA256;
344 sig_alg = MBEDTLS_PK_ECDSA;
345 break;
Xiaofei Bai746f9482021-11-12 08:53:56 +0000346 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Jerry Yu30b071c2021-09-12 20:16:03 +0800347 md_alg = MBEDTLS_MD_SHA384;
348 sig_alg = MBEDTLS_PK_ECDSA;
349 break;
Xiaofei Bai746f9482021-11-12 08:53:56 +0000350 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Jerry Yu30b071c2021-09-12 20:16:03 +0800351 md_alg = MBEDTLS_MD_SHA512;
352 sig_alg = MBEDTLS_PK_ECDSA;
353 break;
Jerry Yu0c23fc32022-03-23 12:20:01 +0800354#endif /* MBEDTLS_ECDSA_C */
355
356#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Jerry Yu3a58b462022-02-22 16:42:29 +0800357#if defined(MBEDTLS_SHA256_C)
Xiaofei Bai6dc90da2021-11-26 08:11:40 +0000358 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
XiaokangQian82d34cc2021-11-03 08:51:56 +0000359 md_alg = MBEDTLS_MD_SHA256;
360 sig_alg = MBEDTLS_PK_RSASSA_PSS;
361 break;
Jerry Yu3a58b462022-02-22 16:42:29 +0800362#endif /* MBEDTLS_SHA256_C */
363
364#if defined(MBEDTLS_SHA384_C)
365 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
366 md_alg = MBEDTLS_MD_SHA384;
367 sig_alg = MBEDTLS_PK_RSASSA_PSS;
368 break;
369#endif /* MBEDTLS_SHA384_C */
370
371#if defined(MBEDTLS_SHA512_C)
372 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
373 md_alg = MBEDTLS_MD_SHA256;
374 sig_alg = MBEDTLS_PK_RSASSA_PSS;
375 break;
376#endif /* MBEDTLS_SHA512_C */
Jerry Yu0c23fc32022-03-23 12:20:01 +0800377#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800378 default:
379 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Certificate Verify: Unknown signature algorithm." ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800380 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800381 }
382
383 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate Verify: Signature algorithm ( %04x )",
384 ( unsigned int ) algorithm ) );
385
386 /*
387 * Check the certificate's key type matches the signature alg
388 */
389 if( !mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk, sig_alg ) )
390 {
391 MBEDTLS_SSL_DEBUG_MSG( 1, ( "signature algorithm doesn't match cert key" ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800392 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800393 }
394
395 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
396 signature_len = MBEDTLS_GET_UINT16_BE( p, 0 );
397 p += 2;
398 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, signature_len );
399
400 /* Hash verify buffer with indicated hash function */
401 switch( md_alg )
402 {
403#if defined(MBEDTLS_SHA256_C)
404 case MBEDTLS_MD_SHA256:
405 verify_hash_len = 32;
Jerry Yu133690c2021-10-25 14:01:13 +0800406 ret = mbedtls_sha256( verify_buffer, verify_buffer_len, verify_hash, 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800407 break;
408#endif /* MBEDTLS_SHA256_C */
409
410#if defined(MBEDTLS_SHA384_C)
411 case MBEDTLS_MD_SHA384:
412 verify_hash_len = 48;
Jerry Yu133690c2021-10-25 14:01:13 +0800413 ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 1 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800414 break;
415#endif /* MBEDTLS_SHA384_C */
416
417#if defined(MBEDTLS_SHA512_C)
418 case MBEDTLS_MD_SHA512:
419 verify_hash_len = 64;
Jerry Yu133690c2021-10-25 14:01:13 +0800420 ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800421 break;
422#endif /* MBEDTLS_SHA512_C */
423
Jerry Yu0b32c502021-10-28 13:41:59 +0800424 default:
425 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
426 break;
Jerry Yu30b071c2021-09-12 20:16:03 +0800427 }
428
Jerry Yu133690c2021-10-25 14:01:13 +0800429 if( ret != 0 )
430 {
431 MBEDTLS_SSL_DEBUG_RET( 1, "hash computation error", ret );
Jerry Yu6f87f252021-10-29 20:12:51 +0800432 goto error;
Jerry Yu133690c2021-10-25 14:01:13 +0800433 }
434
Jerry Yu30b071c2021-09-12 20:16:03 +0800435 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
XiaokangQian82d34cc2021-11-03 08:51:56 +0000436#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
437 if( sig_alg == MBEDTLS_PK_RSASSA_PSS )
438 {
439 const mbedtls_md_info_t* md_info;
Xiaofei Baid25fab62021-12-02 06:36:27 +0000440 rsassa_pss_options.mgf1_hash_id = md_alg;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000441 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
442 {
443 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
444 }
Xiaofei Baid25fab62021-12-02 06:36:27 +0000445 rsassa_pss_options.expected_salt_len = mbedtls_md_get_size( md_info );
446 options = (const void*) &rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000447 }
448#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800449
Xiaofei Baid25fab62021-12-02 06:36:27 +0000450 if( ( ret = mbedtls_pk_verify_ext( sig_alg, options,
Jerry Yu30b071c2021-09-12 20:16:03 +0800451 &ssl->session_negotiate->peer_cert->pk,
452 md_alg, verify_hash, verify_hash_len,
Jerry Yu6f87f252021-10-29 20:12:51 +0800453 p, signature_len ) ) == 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800454 {
Jerry Yu6f87f252021-10-29 20:12:51 +0800455 return( 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800456 }
Jerry Yu6f87f252021-10-29 20:12:51 +0800457 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify_ext", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800458
Jerry Yu6f87f252021-10-29 20:12:51 +0800459error:
460 /* RFC 8446 section 4.4.3
461 *
462 * If the verification fails, the receiver MUST terminate the handshake
463 * with a "decrypt_error" alert.
464 */
465 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
466 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
467 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
468
Jerry Yu30b071c2021-09-12 20:16:03 +0800469}
470#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
471
472int mbedtls_ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
473{
Jerry Yu30b071c2021-09-12 20:16:03 +0800474
Jerry Yuda8cdf22021-10-25 15:06:49 +0800475#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
476 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
477 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
478 size_t verify_buffer_len;
479 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
480 size_t transcript_len;
481 unsigned char *buf;
482 size_t buf_len;
483
Jerry Yu30b071c2021-09-12 20:16:03 +0800484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
485
Jerry Yuda8cdf22021-10-25 15:06:49 +0800486 MBEDTLS_SSL_PROC_CHK(
Xiaofei Bai746f9482021-11-12 08:53:56 +0000487 mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
Jerry Yuda8cdf22021-10-25 15:06:49 +0800488 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu30b071c2021-09-12 20:16:03 +0800489
Jerry Yuda8cdf22021-10-25 15:06:49 +0800490 /* Need to calculate the hash of the transcript first
Jerry Yu0b32c502021-10-28 13:41:59 +0800491 * before reading the message since otherwise it gets
492 * included in the transcript
493 */
Jerry Yuda8cdf22021-10-25 15:06:49 +0800494 ret = mbedtls_ssl_get_handshake_transcript( ssl,
495 ssl->handshake->ciphersuite_info->mac,
496 transcript, sizeof( transcript ),
497 &transcript_len );
498 if( ret != 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800499 {
Jerry Yuda8cdf22021-10-25 15:06:49 +0800500 MBEDTLS_SSL_PEND_FATAL_ALERT(
501 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
502 MBEDTLS_ERR_SSL_INTERNAL_ERROR );
503 return( ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800504 }
505
Jerry Yuda8cdf22021-10-25 15:06:49 +0800506 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash", transcript, transcript_len );
507
508 /* Create verify structure */
509 ssl_tls13_create_verify_structure( transcript,
Jerry Yu0b32c502021-10-28 13:41:59 +0800510 transcript_len,
511 verify_buffer,
512 &verify_buffer_len,
513 ( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) ?
514 MBEDTLS_SSL_IS_SERVER :
515 MBEDTLS_SSL_IS_CLIENT );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800516
517 /* Process the message contents */
Jerry Yu0b32c502021-10-28 13:41:59 +0800518 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_verify( ssl, buf,
519 buf + buf_len, verify_buffer, verify_buffer_len ) );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800520
Xiaofei Bai746f9482021-11-12 08:53:56 +0000521 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
Jerry Yuda8cdf22021-10-25 15:06:49 +0800522 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, buf, buf_len );
Jerry Yu30b071c2021-09-12 20:16:03 +0800523
524cleanup:
525
526 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
Jerry Yu5398c102021-11-05 13:32:38 +0800527 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_process_certificate_verify", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800528 return( ret );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800529#else
530 ((void) ssl);
531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
532 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
533#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800534}
535
536/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000537 *
538 * STATE HANDLING: Incoming Certificate, client-side only currently.
539 *
540 */
541
542/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000543 * Implementation
544 */
545
Xiaofei Bai947571e2021-09-29 09:12:03 +0000546#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
547#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
548/*
549 * Structure of Certificate message:
550 *
551 * enum {
552 * X509(0),
553 * RawPublicKey(2),
554 * (255)
555 * } CertificateType;
556 *
557 * struct {
558 * select (certificate_type) {
559 * case RawPublicKey:
560 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
561 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
562 * case X509:
563 * opaque cert_data<1..2^24-1>;
564 * };
565 * Extension extensions<0..2^16-1>;
566 * } CertificateEntry;
567 *
568 * struct {
569 * opaque certificate_request_context<0..2^8-1>;
570 * CertificateEntry certificate_list<0..2^24-1>;
571 * } Certificate;
572 *
573 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000574
575/* Parse certificate chain send by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000576static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
577 const unsigned char *buf,
578 const unsigned char *end )
579{
580 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
581 size_t certificate_request_context_len = 0;
582 size_t certificate_list_len = 0;
583 const unsigned char *p = buf;
584 const unsigned char *certificate_list_end;
585
586 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
587 certificate_request_context_len = p[0];
Jerry Yub640bf62021-10-29 10:05:32 +0800588 certificate_list_len = MBEDTLS_GET_UINT24_BE( p, 1 );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000589 p += 4;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000590
591 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
592 * support anything beyond 2^16 = 64K.
593 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000594 if( ( certificate_request_context_len != 0 ) ||
595 ( certificate_list_len >= 0x10000 ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000596 {
597 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
598 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
599 MBEDTLS_ERR_SSL_DECODE_ERROR );
600 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
601 }
602
603 /* In case we tried to reuse a session but it failed */
604 if( ssl->session_negotiate->peer_cert != NULL )
605 {
606 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
607 mbedtls_free( ssl->session_negotiate->peer_cert );
608 }
609
610 if( ( ssl->session_negotiate->peer_cert =
611 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
612 {
613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
614 sizeof( mbedtls_x509_crt ) ) );
615 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
616 MBEDTLS_ERR_SSL_ALLOC_FAILED );
617 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
618 }
619
620 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
621
Xiaofei Bai947571e2021-09-29 09:12:03 +0000622 certificate_list_end = p + certificate_list_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000623 while( p < certificate_list_end )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000624 {
625 size_t cert_data_len, extensions_len;
626
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000627 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 3 );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000628 cert_data_len = MBEDTLS_GET_UINT24_BE( p, 0 );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000629 p += 3;
630
631 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
632 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
633 * check that we have a minimum of 128 bytes of data, this is not
634 * clear why we need that though.
635 */
636 if( ( cert_data_len < 128 ) || ( cert_data_len >= 0x10000 ) )
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000637 {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
639 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
640 MBEDTLS_ERR_SSL_DECODE_ERROR );
641 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
642 }
643
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000644 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, cert_data_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000645 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
646 p, cert_data_len );
647
648 switch( ret )
649 {
650 case 0: /*ok*/
651 break;
652 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
653 /* Ignore certificate with an unknown algorithm: maybe a
654 prior certificate was already trusted. */
655 break;
656
657 case MBEDTLS_ERR_X509_ALLOC_FAILED:
658 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
659 MBEDTLS_ERR_X509_ALLOC_FAILED );
660 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
661 return( ret );
662
663 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
664 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
665 MBEDTLS_ERR_X509_UNKNOWN_VERSION );
666 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
667 return( ret );
668
669 default:
670 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
671 ret );
672 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
673 return( ret );
674 }
675
676 p += cert_data_len;
677
678 /* Certificate extensions length */
679 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 2 );
680 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
681 p += 2;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000682 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, extensions_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000683 p += extensions_len;
684 }
685
686 /* Check that all the message is consumed. */
687 if( p != end )
688 {
689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
690 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, \
691 MBEDTLS_ERR_SSL_DECODE_ERROR );
692 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
693 }
694
695 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
696
697 return( ret );
698}
699#else
700static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
701 const unsigned char *buf,
702 const unsigned char *end )
703{
704 ((void) ssl);
705 ((void) buf);
706 ((void) end);
707 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
708}
709#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
710#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
711
712#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
713#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000714/* Validate certificate chain sent by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000715static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
716{
717 int ret = 0;
718 mbedtls_x509_crt *ca_chain;
719 mbedtls_x509_crl *ca_crl;
Xiaofei Baiff456022021-10-28 06:50:17 +0000720 uint32_t verify_result = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000721
722#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
723 if( ssl->handshake->sni_ca_chain != NULL )
724 {
725 ca_chain = ssl->handshake->sni_ca_chain;
726 ca_crl = ssl->handshake->sni_ca_crl;
727 }
728 else
729#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
730 {
731 ca_chain = ssl->conf->ca_chain;
732 ca_crl = ssl->conf->ca_crl;
733 }
734
735 /*
736 * Main check: verify certificate
737 */
738 ret = mbedtls_x509_crt_verify_with_profile(
739 ssl->session_negotiate->peer_cert,
740 ca_chain, ca_crl,
741 ssl->conf->cert_profile,
742 ssl->hostname,
Xiaofei Baiff456022021-10-28 06:50:17 +0000743 &verify_result,
Xiaofei Bai947571e2021-09-29 09:12:03 +0000744 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
745
746 if( ret != 0 )
747 {
748 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
749 }
750
751 /*
752 * Secondary checks: always done, but change 'ret' only if it was 0
753 */
754
755#if defined(MBEDTLS_ECP_C)
756 {
757 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
758
759 /* If certificate uses an EC key, make sure the curve is OK */
760 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
761 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
762 {
Xiaofei Baiff456022021-10-28 06:50:17 +0000763 verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000764
765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( EC key curve )" ) );
766 if( ret == 0 )
767 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
768 }
769 }
770#endif /* MBEDTLS_ECP_C */
771
772 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
773 ssl->handshake->ciphersuite_info,
774 !ssl->conf->endpoint,
Xiaofei Baiff456022021-10-28 06:50:17 +0000775 &verify_result ) != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000776 {
777 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( usage extensions )" ) );
778 if( ret == 0 )
779 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
780 }
781
782
783 if( ca_chain == NULL )
784 {
785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
786 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
787 }
788
789 if( ret != 0 )
790 {
791 /* The certificate may have been rejected for several reasons.
792 Pick one and send the corresponding alert. Which alert to send
793 may be a subject of debate in some cases. */
Xiaofei Baiff456022021-10-28 06:50:17 +0000794 if( verify_result & MBEDTLS_X509_BADCERT_OTHER )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000795 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000796 else if( verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000797 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000798 else if( verify_result & ( MBEDTLS_X509_BADCERT_KEY_USAGE |
799 MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
800 MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
801 MBEDTLS_X509_BADCERT_BAD_PK |
802 MBEDTLS_X509_BADCERT_BAD_KEY ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000803 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000804 else if( verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000805 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000806 else if( verify_result & MBEDTLS_X509_BADCERT_REVOKED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000807 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000808 else if( verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000809 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret );
810 else
811 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret );
812 }
813
814#if defined(MBEDTLS_DEBUG_C)
Xiaofei Baiff456022021-10-28 06:50:17 +0000815 if( verify_result != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000816 {
817 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
Jerry Yu83bb1312021-10-28 22:16:33 +0800818 (unsigned int) verify_result ) );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000819 }
820 else
821 {
822 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
823 }
824#endif /* MBEDTLS_DEBUG_C */
825
Xiaofei Baiff456022021-10-28 06:50:17 +0000826 ssl->session_negotiate->verify_result = verify_result;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000827 return( ret );
828}
829#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
830static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
831{
832 ((void) ssl);
833 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
834}
835#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
836#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
837
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000838int mbedtls_ssl_tls13_process_certificate( mbedtls_ssl_context *ssl )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000839{
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000840 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
842
843#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
844 unsigned char *buf;
845 size_t buf_len;
846
Xiaofei Bai746f9482021-11-12 08:53:56 +0000847 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000848 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
849 &buf, &buf_len ) );
850
851 /* Parse the certificate chain sent by the peer. */
852 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate( ssl, buf, buf + buf_len ) );
853 /* Validate the certificate chain and set the verification results. */
854 MBEDTLS_SSL_PROC_CHK( ssl_tls13_validate_certificate( ssl ) );
855
Xiaofei Bai746f9482021-11-12 08:53:56 +0000856 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
857 buf, buf_len );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000858
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000859cleanup:
860
861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Xiaofei Bai10aeec02021-10-26 09:50:08 +0000862#else
863 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
864 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
865#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000866 return( ret );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000867}
Jerry Yu90f152d2022-01-29 22:12:42 +0800868#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7399d0d2022-01-30 17:54:19 +0800869/*
870 * enum {
871 * X509(0),
872 * RawPublicKey(2),
873 * (255)
874 * } CertificateType;
875 *
876 * struct {
877 * select (certificate_type) {
878 * case RawPublicKey:
879 * // From RFC 7250 ASN.1_subjectPublicKeyInfo
880 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
881 *
882 * case X509:
883 * opaque cert_data<1..2^24-1>;
884 * };
885 * Extension extensions<0..2^16-1>;
886 * } CertificateEntry;
887 *
888 * struct {
889 * opaque certificate_request_context<0..2^8-1>;
890 * CertificateEntry certificate_list<0..2^24-1>;
891 * } Certificate;
892 */
893static int ssl_tls13_write_certificate_body( mbedtls_ssl_context *ssl,
Jerry Yu3e536442022-02-15 11:05:59 +0800894 unsigned char *buf,
Jerry Yu7399d0d2022-01-30 17:54:19 +0800895 unsigned char *end,
Jerry Yu3e536442022-02-15 11:05:59 +0800896 size_t *out_len )
Jerry Yu5cc35062022-01-28 16:16:08 +0800897{
Jerry Yu5cc35062022-01-28 16:16:08 +0800898 const mbedtls_x509_crt *crt = mbedtls_ssl_own_cert( ssl );
Jerry Yu3e536442022-02-15 11:05:59 +0800899 unsigned char *p = buf;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800900 unsigned char *certificate_request_context =
901 ssl->handshake->certificate_request_context;
902 unsigned char certificate_request_context_len =
903 ssl->handshake->certificate_request_context_len;
904 unsigned char *p_certificate_list_len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800905
Jerry Yu5cc35062022-01-28 16:16:08 +0800906
Jerry Yu3391ac02022-02-16 11:21:37 +0800907 /* ...
908 * opaque certificate_request_context<0..2^8-1>;
909 * ...
910 */
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800911 MBEDTLS_SSL_CHK_BUF_PTR( p, end, certificate_request_context_len + 1 );
912 *p++ = certificate_request_context_len;
913 if( certificate_request_context_len > 0 )
Jerry Yu537530d2022-02-15 14:00:57 +0800914 {
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800915 memcpy( p, certificate_request_context, certificate_request_context_len );
916 p += certificate_request_context_len;
Jerry Yu537530d2022-02-15 14:00:57 +0800917 }
918
Jerry Yu3391ac02022-02-16 11:21:37 +0800919 /* ...
920 * CertificateEntry certificate_list<0..2^24-1>;
921 * ...
922 */
Jerry Yu3e536442022-02-15 11:05:59 +0800923 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 );
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800924 p_certificate_list_len = p;
Jerry Yu3e536442022-02-15 11:05:59 +0800925 p += 3;
926
Jerry Yu7399d0d2022-01-30 17:54:19 +0800927 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", crt );
Jerry Yu5cc35062022-01-28 16:16:08 +0800928
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800929 while( crt != NULL )
Jerry Yu5cc35062022-01-28 16:16:08 +0800930 {
Jerry Yu7399d0d2022-01-30 17:54:19 +0800931 size_t cert_data_len = crt->raw.len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800932
Jerry Yu7399d0d2022-01-30 17:54:19 +0800933 MBEDTLS_SSL_CHK_BUF_PTR( p, end, cert_data_len + 3 + 2 );
934 MBEDTLS_PUT_UINT24_BE( cert_data_len, p, 0 );
935 p += 3;
Jerry Yu5cc35062022-01-28 16:16:08 +0800936
Jerry Yu7399d0d2022-01-30 17:54:19 +0800937 memcpy( p, crt->raw.p, cert_data_len );
938 p += cert_data_len;
939 crt = crt->next;
Jerry Yu5cc35062022-01-28 16:16:08 +0800940
941 /* Currently, we don't have any certificate extensions defined.
942 * Hence, we are sending an empty extension with length zero.
943 */
Jerry Yu7399d0d2022-01-30 17:54:19 +0800944 MBEDTLS_PUT_UINT24_BE( 0, p, 0 );
945 p += 2;
Jerry Yu5cc35062022-01-28 16:16:08 +0800946 }
Jerry Yu5cc35062022-01-28 16:16:08 +0800947
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800948 MBEDTLS_PUT_UINT24_BE( p - p_certificate_list_len - 3,
949 p_certificate_list_len, 0 );
Jerry Yu7399d0d2022-01-30 17:54:19 +0800950
Jerry Yu3e536442022-02-15 11:05:59 +0800951 *out_len = p - buf;
Jerry Yu5cc35062022-01-28 16:16:08 +0800952
953 return( 0 );
954}
Jerry Yu5cc35062022-01-28 16:16:08 +0800955
Jerry Yu3e536442022-02-15 11:05:59 +0800956int mbedtls_ssl_tls13_write_certificate( mbedtls_ssl_context *ssl )
Jerry Yu5cc35062022-01-28 16:16:08 +0800957{
958 int ret;
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100959 unsigned char *buf;
960 size_t buf_len, msg_len;
961
Jerry Yu5cc35062022-01-28 16:16:08 +0800962 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
963
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100964 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg( ssl,
965 MBEDTLS_SSL_HS_CERTIFICATE, &buf, &buf_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800966
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100967 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_body( ssl,
968 buf,
969 buf + buf_len,
970 &msg_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800971
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100972 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
973 MBEDTLS_SSL_HS_CERTIFICATE,
974 buf,
975 msg_len );
Jerry Yu5cc35062022-01-28 16:16:08 +0800976
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100977 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg(
978 ssl, buf_len, msg_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800979cleanup:
980
981 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
982 return( ret );
983}
984
Jerry Yu3e536442022-02-15 11:05:59 +0800985/*
986 * STATE HANDLING: Output Certificate Verify
987 */
Jerry Yue91a51a2022-03-22 21:42:50 +0800988static int ssl_tls13_get_sig_alg_from_pk( mbedtls_ssl_context *ssl,
989 mbedtls_pk_context *own_key,
990 uint16_t *algorithm,
991 mbedtls_pk_type_t *pk_type,
992 mbedtls_md_type_t *md_alg)
Jerry Yu67eced02022-02-25 13:37:36 +0800993{
994 mbedtls_pk_type_t sig = mbedtls_ssl_sig_from_pk( own_key );
995 /* Determine the size of the key */
996 size_t own_key_size = mbedtls_pk_get_bitlen( own_key );
Jerry Yue91a51a2022-03-22 21:42:50 +0800997 *algorithm = MBEDTLS_TLS1_3_SIG_NONE;
Jerry Yu67eced02022-02-25 13:37:36 +0800998 ((void) own_key_size);
999
1000 switch( sig )
1001 {
1002#if defined(MBEDTLS_ECDSA_C)
1003 case MBEDTLS_SSL_SIG_ECDSA:
1004 switch( own_key_size )
1005 {
1006 case 256:
Jerry Yue91a51a2022-03-22 21:42:50 +08001007 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256;
Jerry Yudddf5a02022-03-22 15:47:19 +08001008 *md_alg = MBEDTLS_MD_SHA256;
1009 *pk_type = MBEDTLS_PK_ECDSA;
Jerry Yue91a51a2022-03-22 21:42:50 +08001010 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +08001011 case 384:
Jerry Yue91a51a2022-03-22 21:42:50 +08001012 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384;
Jerry Yudddf5a02022-03-22 15:47:19 +08001013 *md_alg = MBEDTLS_MD_SHA384;
1014 *pk_type = MBEDTLS_PK_ECDSA;
Jerry Yue91a51a2022-03-22 21:42:50 +08001015 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +08001016 case 521:
Jerry Yue91a51a2022-03-22 21:42:50 +08001017 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512;
Jerry Yudddf5a02022-03-22 15:47:19 +08001018 *md_alg = MBEDTLS_MD_SHA512;
1019 *pk_type = MBEDTLS_PK_ECDSA;
Jerry Yue91a51a2022-03-22 21:42:50 +08001020 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +08001021 default:
1022 MBEDTLS_SSL_DEBUG_MSG( 3,
1023 ( "unknown key size: %"
1024 MBEDTLS_PRINTF_SIZET " bits",
1025 own_key_size ) );
1026 break;
1027 }
1028 break;
1029#endif /* MBEDTLS_ECDSA_C */
1030
Jerry Yucef3f332022-03-22 23:00:13 +08001031#if defined(MBEDTLS_RSA_C)
Jerry Yu67eced02022-02-25 13:37:36 +08001032 case MBEDTLS_SSL_SIG_RSA:
Jerry Yucef3f332022-03-22 23:00:13 +08001033#if defined(MBEDTLS_PKCS1_V21)
1034#if defined(MBEDTLS_SHA256_C)
Jerry Yu67eced02022-02-25 13:37:36 +08001035 if( own_key_size <= 2048 &&
1036 mbedtls_ssl_sig_alg_is_received( ssl,
1037 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256 ) )
1038 {
Jerry Yue91a51a2022-03-22 21:42:50 +08001039 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256;
Jerry Yudddf5a02022-03-22 15:47:19 +08001040 *md_alg = MBEDTLS_MD_SHA256;
1041 *pk_type = MBEDTLS_PK_RSASSA_PSS;
Jerry Yue91a51a2022-03-22 21:42:50 +08001042 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +08001043 }
Jerry Yucef3f332022-03-22 23:00:13 +08001044 else
1045#endif /* MBEDTLS_SHA256_C */
1046#if defined(MBEDTLS_SHA384_C)
1047 if( own_key_size <= 3072 &&
1048 mbedtls_ssl_sig_alg_is_received( ssl,
Jerry Yu67eced02022-02-25 13:37:36 +08001049 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384 ) )
1050 {
Jerry Yue91a51a2022-03-22 21:42:50 +08001051 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384;
Jerry Yudddf5a02022-03-22 15:47:19 +08001052 *md_alg = MBEDTLS_MD_SHA384;
1053 *pk_type = MBEDTLS_PK_RSASSA_PSS;
Jerry Yue91a51a2022-03-22 21:42:50 +08001054 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +08001055 }
Jerry Yucef3f332022-03-22 23:00:13 +08001056 else
1057#endif /* MBEDTLS_SHA384_C */
1058#if defined(MBEDTLS_SHA512_C)
1059 if( own_key_size <= 4096 &&
1060 mbedtls_ssl_sig_alg_is_received( ssl,
Jerry Yu67eced02022-02-25 13:37:36 +08001061 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512 ) )
1062 {
Jerry Yue91a51a2022-03-22 21:42:50 +08001063 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512;
Jerry Yudddf5a02022-03-22 15:47:19 +08001064 *md_alg = MBEDTLS_MD_SHA512;
1065 *pk_type = MBEDTLS_PK_RSASSA_PSS;
Jerry Yue91a51a2022-03-22 21:42:50 +08001066 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +08001067 }
Jerry Yucef3f332022-03-22 23:00:13 +08001068 else
1069#endif /* MBEDTLS_SHA512_C */
1070#endif /* MBEDTLS_PKCS1_V21 */
1071#if defined(MBEDTLS_PKCS1_V15)
1072#if defined(MBEDTLS_SHA256_C)
1073 if( own_key_size <= 2048 &&
1074 mbedtls_ssl_sig_alg_is_received( ssl,
1075 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256 ) )
1076 {
1077 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256;
1078 *md_alg = MBEDTLS_MD_SHA256;
1079 *pk_type = MBEDTLS_PK_RSA;
1080 return( 0 );
1081 }
1082 else
1083#endif /* MBEDTLS_SHA256_C */
1084#if defined(MBEDTLS_SHA384_C)
1085 if( own_key_size <= 3072 &&
1086 mbedtls_ssl_sig_alg_is_received( ssl,
1087 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384 ) )
1088 {
1089 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384;
1090 *md_alg = MBEDTLS_MD_SHA384;
1091 *pk_type = MBEDTLS_PK_RSA;
1092 return( 0 );
1093 }
1094 else
1095#endif /* MBEDTLS_SHA384_C */
1096#if defined(MBEDTLS_SHA512_C)
1097 if( own_key_size <= 4096 &&
1098 mbedtls_ssl_sig_alg_is_received( ssl,
1099 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512 ) )
1100 {
1101 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512;
1102 *md_alg = MBEDTLS_MD_SHA512;
1103 *pk_type = MBEDTLS_PK_RSA;
1104 return( 0 );
1105 }
1106 else
1107#endif /* MBEDTLS_SHA512_C */
1108#endif /* MBEDTLS_PKCS1_V15 */
1109 {
1110 MBEDTLS_SSL_DEBUG_MSG( 3,
1111 ( "unknown key size: %"
1112 MBEDTLS_PRINTF_SIZET " bits",
1113 own_key_size ) );
1114 }
Jerry Yu67eced02022-02-25 13:37:36 +08001115 break;
Jerry Yucef3f332022-03-22 23:00:13 +08001116#endif /* MBEDTLS_RSA_C */
Jerry Yu67eced02022-02-25 13:37:36 +08001117 default:
1118 MBEDTLS_SSL_DEBUG_MSG( 1,
1119 ( "unkown signature type : %u", sig ) );
1120 break;
1121 }
Jerry Yue91a51a2022-03-22 21:42:50 +08001122 return( -1 );
Jerry Yu67eced02022-02-25 13:37:36 +08001123}
1124
Jerry Yu3e536442022-02-15 11:05:59 +08001125static int ssl_tls13_write_certificate_verify_body( mbedtls_ssl_context *ssl,
1126 unsigned char *buf,
1127 unsigned char *end,
1128 size_t *out_len )
Jerry Yu8511f122022-01-29 10:01:04 +08001129{
1130 int ret;
Jerry Yu3e536442022-02-15 11:05:59 +08001131 unsigned char *p = buf;
Jerry Yu8511f122022-01-29 10:01:04 +08001132 mbedtls_pk_context *own_key;
Jerry Yu3e536442022-02-15 11:05:59 +08001133
Jerry Yu8511f122022-01-29 10:01:04 +08001134 unsigned char handshake_hash[ MBEDTLS_TLS1_3_MD_MAX_SIZE ];
1135 size_t handshake_hash_len;
Jerry Yu3e536442022-02-15 11:05:59 +08001136 unsigned char verify_buffer[ SSL_VERIFY_STRUCT_MAX_SIZE ];
1137 size_t verify_buffer_len;
Jerry Yu67eced02022-02-25 13:37:36 +08001138 mbedtls_pk_type_t pk_type = MBEDTLS_PK_NONE;
Jerry Yu67eced02022-02-25 13:37:36 +08001139 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Jerry Yu78272072022-02-22 10:28:13 +08001140 uint16_t algorithm = MBEDTLS_TLS1_3_SIG_NONE;
Jerry Yu3e536442022-02-15 11:05:59 +08001141 size_t signature_len = 0;
Jerry Yu8511f122022-01-29 10:01:04 +08001142 const mbedtls_md_info_t *md_info;
Jerry Yu3391ac02022-02-16 11:21:37 +08001143 unsigned char verify_hash[ MBEDTLS_MD_MAX_SIZE ];
Jerry Yu3e536442022-02-15 11:05:59 +08001144 size_t verify_hash_len;
Jerry Yu8511f122022-01-29 10:01:04 +08001145
Jerry Yu0b7b1012022-02-23 12:23:05 +08001146 *out_len = 0;
1147
Jerry Yu3e536442022-02-15 11:05:59 +08001148 own_key = mbedtls_ssl_own_key( ssl );
1149 if( own_key == NULL )
Jerry Yu8511f122022-01-29 10:01:04 +08001150 {
Jerry Yu3e536442022-02-15 11:05:59 +08001151 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1152 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu8511f122022-01-29 10:01:04 +08001153 }
1154
Jerry Yu8511f122022-01-29 10:01:04 +08001155 ret = mbedtls_ssl_get_handshake_transcript( ssl,
1156 ssl->handshake->ciphersuite_info->mac,
1157 handshake_hash,
1158 sizeof( handshake_hash ),
1159 &handshake_hash_len );
1160 if( ret != 0 )
1161 return( ret );
1162
1163 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash",
1164 handshake_hash,
1165 handshake_hash_len);
1166
Jerry Yu8511f122022-01-29 10:01:04 +08001167 ssl_tls13_create_verify_structure( handshake_hash, handshake_hash_len,
1168 verify_buffer, &verify_buffer_len,
1169 ssl->conf->endpoint );
1170
1171 /*
1172 * struct {
1173 * SignatureScheme algorithm;
1174 * opaque signature<0..2^16-1>;
1175 * } CertificateVerify;
1176 */
Jerry Yue91a51a2022-03-22 21:42:50 +08001177 ret = ssl_tls13_get_sig_alg_from_pk( ssl, own_key, &algorithm,
1178 &pk_type, &md_alg );
1179 if( ret != 0 || ! mbedtls_ssl_sig_alg_is_received( ssl, algorithm ) )
Jerry Yu8511f122022-01-29 10:01:04 +08001180 {
Jerry Yud66409a2022-02-18 16:42:24 +08001181 MBEDTLS_SSL_DEBUG_MSG( 1,
Jerry Yu2124d052022-02-18 21:07:18 +08001182 ( "signature algorithm not in received or offered list." ) );
Jerry Yu67eced02022-02-25 13:37:36 +08001183
1184 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Signature algorithm is %s",
1185 mbedtls_ssl_sig_alg_to_str( algorithm ) ) );
1186
Jerry Yu71f36f12022-02-23 17:34:29 +08001187 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
Jerry Yud66409a2022-02-18 16:42:24 +08001188 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu3391ac02022-02-16 11:21:37 +08001189 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu8511f122022-01-29 10:01:04 +08001190 }
1191
Jerry Yu3391ac02022-02-16 11:21:37 +08001192 /* Check there is space for the algorithm identifier (2 bytes) and the
1193 * signature length (2 bytes).
1194 */
1195 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Jerry Yu3e536442022-02-15 11:05:59 +08001196 MBEDTLS_PUT_UINT16_BE( algorithm, p, 0 );
1197 p += 2;
Jerry Yu8511f122022-01-29 10:01:04 +08001198
1199 /* Hash verify buffer with indicated hash function */
1200 md_info = mbedtls_md_info_from_type( md_alg );
1201 if( md_info == NULL )
1202 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1203
1204 ret = mbedtls_md( md_info, verify_buffer, verify_buffer_len, verify_hash );
1205 if( ret != 0 )
1206 return( ret );
1207
1208 verify_hash_len = mbedtls_md_get_size( md_info );
1209 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
1210
Jerry Yu8beb9e12022-03-12 16:23:53 +08001211 if( ( ret = mbedtls_pk_sign_ext( pk_type, own_key,
1212 md_alg, verify_hash, verify_hash_len,
Jerry Yu67eced02022-02-25 13:37:36 +08001213 p + 2, (size_t)( end - ( p + 2 ) ), &signature_len,
1214 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Jerry Yu8511f122022-01-29 10:01:04 +08001215 {
1216 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
1217 return( ret );
1218 }
Jerry Yu8511f122022-01-29 10:01:04 +08001219
Jerry Yu3e536442022-02-15 11:05:59 +08001220 MBEDTLS_PUT_UINT16_BE( signature_len, p, 0 );
1221 p += 2 + signature_len;
Jerry Yu8511f122022-01-29 10:01:04 +08001222
Jerry Yu3e536442022-02-15 11:05:59 +08001223 *out_len = (size_t)( p - buf );
1224
Jerry Yu8511f122022-01-29 10:01:04 +08001225 return( ret );
1226}
Jerry Yu8511f122022-01-29 10:01:04 +08001227
Jerry Yu3e536442022-02-15 11:05:59 +08001228int mbedtls_ssl_tls13_write_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu8511f122022-01-29 10:01:04 +08001229{
1230 int ret = 0;
Jerry Yuca133a32022-02-15 14:22:05 +08001231 unsigned char *buf;
1232 size_t buf_len, msg_len;
1233
Jerry Yu8511f122022-01-29 10:01:04 +08001234 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
1235
Jerry Yuca133a32022-02-15 14:22:05 +08001236 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg( ssl,
1237 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001238
Jerry Yuca133a32022-02-15 14:22:05 +08001239 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_verify_body(
1240 ssl, buf, buf + buf_len, &msg_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001241
Jerry Yuca133a32022-02-15 14:22:05 +08001242 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
1243 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, buf, msg_len );
Jerry Yu8511f122022-01-29 10:01:04 +08001244
Jerry Yuca133a32022-02-15 14:22:05 +08001245 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg(
1246 ssl, buf_len, msg_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001247
1248cleanup:
1249
1250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
1251 return( ret );
1252}
1253
Jerry Yu90f152d2022-01-29 22:12:42 +08001254#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1255
Jerry Yu5cc35062022-01-28 16:16:08 +08001256/*
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001257 *
XiaokangQianc5c39d52021-11-09 11:55:10 +00001258 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001259 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001260/*
1261 * Implementation
1262 */
1263
XiaokangQianaaa0e192021-11-10 03:07:04 +00001264static int ssl_tls13_preprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001265{
1266 int ret;
1267
XiaokangQianc5c39d52021-11-09 11:55:10 +00001268 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001269 ssl->handshake->state_local.finished_in.digest,
1270 sizeof( ssl->handshake->state_local.finished_in.digest ),
1271 &ssl->handshake->state_local.finished_in.digest_len,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001272 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
XiaokangQianc13f9352021-11-11 06:13:22 +00001273 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001274 if( ret != 0 )
1275 {
XiaokangQianc5c39d52021-11-09 11:55:10 +00001276 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_calculate_verify_data", ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001277 return( ret );
1278 }
1279
1280 return( 0 );
1281}
1282
XiaokangQianc5c39d52021-11-09 11:55:10 +00001283static int ssl_tls13_parse_finished_message( mbedtls_ssl_context *ssl,
1284 const unsigned char *buf,
1285 const unsigned char *end )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001286{
XiaokangQian33062842021-11-11 03:37:45 +00001287 /*
1288 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +00001289 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +00001290 * } Finished;
1291 */
1292 const unsigned char *expected_verify_data =
1293 ssl->handshake->state_local.finished_in.digest;
1294 size_t expected_verify_data_len =
1295 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001296 /* Structural validation */
XiaokangQian33062842021-11-11 03:37:45 +00001297 if( (size_t)( end - buf ) != expected_verify_data_len )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001298 {
1299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1300
1301 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +00001302 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001303 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
1304 }
1305
XiaokangQianc5c39d52021-11-09 11:55:10 +00001306 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (self-computed):",
XiaokangQian33062842021-11-11 03:37:45 +00001307 expected_verify_data,
1308 expected_verify_data_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001309 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (received message):", buf,
XiaokangQian33062842021-11-11 03:37:45 +00001310 expected_verify_data_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001311
1312 /* Semantic validation */
Gabor Mezei685472b2021-11-24 11:17:36 +01001313 if( mbedtls_ct_memcmp( buf,
1314 expected_verify_data,
1315 expected_verify_data_len ) != 0 )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001316 {
1317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1318
XiaokangQian33062842021-11-11 03:37:45 +00001319 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +00001320 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001321 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1322 }
1323 return( 0 );
1324}
1325
XiaokangQianc13f9352021-11-11 06:13:22 +00001326#if defined(MBEDTLS_SSL_CLI_C)
XiaokangQianaaa0e192021-11-10 03:07:04 +00001327static int ssl_tls13_postprocess_server_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001328{
XiaokangQian33062842021-11-11 03:37:45 +00001329 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001330 mbedtls_ssl_key_set traffic_keys;
XiaokangQian1aef02e2021-10-28 09:54:34 +00001331 mbedtls_ssl_transform *transform_application = NULL;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001332
XiaokangQian4cab0242021-10-12 08:43:37 +00001333 ret = mbedtls_ssl_tls13_key_schedule_stage_application( ssl );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001334 if( ret != 0 )
1335 {
1336 MBEDTLS_SSL_DEBUG_RET( 1,
XiaokangQian4cab0242021-10-12 08:43:37 +00001337 "mbedtls_ssl_tls13_key_schedule_stage_application", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001338 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001339 }
1340
XiaokangQian33062842021-11-11 03:37:45 +00001341 ret = mbedtls_ssl_tls13_generate_application_keys( ssl, &traffic_keys );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001342 if( ret != 0 )
1343 {
1344 MBEDTLS_SSL_DEBUG_RET( 1,
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001345 "mbedtls_ssl_tls13_generate_application_keys", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001346 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001347 }
1348
1349 transform_application =
1350 mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
1351 if( transform_application == NULL )
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001352 {
1353 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1354 goto cleanup;
1355 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001356
1357 ret = mbedtls_ssl_tls13_populate_transform(
1358 transform_application,
1359 ssl->conf->endpoint,
1360 ssl->session_negotiate->ciphersuite,
1361 &traffic_keys,
1362 ssl );
1363 if( ret != 0 )
1364 {
1365 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001366 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001367 }
1368
1369 ssl->transform_application = transform_application;
1370
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001371cleanup:
1372
XiaokangQian33062842021-11-11 03:37:45 +00001373 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001374 if( ret != 0 )
XiaokangQian1aef02e2021-10-28 09:54:34 +00001375 {
1376 mbedtls_free( transform_application );
1377 MBEDTLS_SSL_PEND_FATAL_ALERT(
1378 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1379 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1380 }
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001381 return( ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001382}
XiaokangQianc13f9352021-11-11 06:13:22 +00001383#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001384
XiaokangQiancc90c942021-11-09 12:30:09 +00001385static int ssl_tls13_postprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001386{
1387
XiaokangQianc13f9352021-11-11 06:13:22 +00001388#if defined(MBEDTLS_SSL_CLI_C)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001389 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
1390 {
XiaokangQianaaa0e192021-11-10 03:07:04 +00001391 return( ssl_tls13_postprocess_server_finished_message( ssl ) );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001392 }
XiaokangQianc13f9352021-11-11 06:13:22 +00001393#else
1394 ((void) ssl);
1395#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001396
1397 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1398}
1399
XiaokangQianc5c39d52021-11-09 11:55:10 +00001400int mbedtls_ssl_tls13_process_finished_message( mbedtls_ssl_context *ssl )
1401{
XiaokangQian33062842021-11-11 03:37:45 +00001402 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001403 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +00001404 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001405
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001406 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001407
1408 /* Preprocessing step: Compute handshake digest */
XiaokangQianaaa0e192021-11-10 03:07:04 +00001409 MBEDTLS_SSL_PROC_CHK( ssl_tls13_preprocess_finished_message( ssl ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001410
Xiaofei Bai746f9482021-11-12 08:53:56 +00001411 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001412 MBEDTLS_SSL_HS_FINISHED,
Xiaofei Baieef15042021-11-18 07:29:56 +00001413 &buf, &buf_len ) );
1414 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_finished_message( ssl, buf, buf + buf_len ) );
Xiaofei Bai746f9482021-11-12 08:53:56 +00001415 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
Xiaofei Baieef15042021-11-18 07:29:56 +00001416 ssl, MBEDTLS_SSL_HS_FINISHED, buf, buf_len );
XiaokangQianaaa0e192021-11-10 03:07:04 +00001417 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_finished_message( ssl ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001418
1419cleanup:
1420
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001421 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001422 return( ret );
1423}
1424
XiaokangQian74af2a82021-09-22 07:40:30 +00001425/*
1426 *
XiaokangQiancc90c942021-11-09 12:30:09 +00001427 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +00001428 *
1429 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001430/*
XiaokangQian35dc6252021-11-11 08:16:19 +00001431 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +00001432 */
1433
XiaokangQian8773aa02021-11-10 07:33:09 +00001434static int ssl_tls13_prepare_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +00001435{
1436 int ret;
1437
1438 /* Compute transcript of handshake up to now. */
XiaokangQiancc90c942021-11-09 12:30:09 +00001439 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQian74af2a82021-09-22 07:40:30 +00001440 ssl->handshake->state_local.finished_out.digest,
1441 sizeof( ssl->handshake->state_local.finished_out.digest ),
1442 &ssl->handshake->state_local.finished_out.digest_len,
1443 ssl->conf->endpoint );
1444
1445 if( ret != 0 )
1446 {
Jerry Yu7ca30542021-12-08 15:57:57 +08001447 MBEDTLS_SSL_DEBUG_RET( 1, "calculate_verify_data failed", ret );
XiaokangQian74af2a82021-09-22 07:40:30 +00001448 return( ret );
1449 }
1450
1451 return( 0 );
1452}
1453
XiaokangQiancc90c942021-11-09 12:30:09 +00001454static int ssl_tls13_finalize_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +00001455{
XiaokangQian0fa66432021-11-15 03:33:57 +00001456 // TODO: Add back resumption keys calculation after MVP.
1457 ((void) ssl);
XiaokangQian74af2a82021-09-22 07:40:30 +00001458
1459 return( 0 );
1460}
1461
XiaokangQian8773aa02021-11-10 07:33:09 +00001462static int ssl_tls13_write_finished_message_body( mbedtls_ssl_context *ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001463 unsigned char *buf,
1464 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001465 size_t *out_len )
XiaokangQian74af2a82021-09-22 07:40:30 +00001466{
XiaokangQian8773aa02021-11-10 07:33:09 +00001467 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001468 /*
1469 * struct {
1470 * opaque verify_data[Hash.length];
1471 * } Finished;
1472 */
XiaokangQian8773aa02021-11-10 07:33:09 +00001473 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001474
1475 memcpy( buf, ssl->handshake->state_local.finished_out.digest,
XiaokangQian8773aa02021-11-10 07:33:09 +00001476 verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001477
Xiaofei Baid25fab62021-12-02 06:36:27 +00001478 *out_len = verify_data_len;
XiaokangQian74af2a82021-09-22 07:40:30 +00001479 return( 0 );
1480}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001481
XiaokangQian35dc6252021-11-11 08:16:19 +00001482/* Main entry point: orchestrates the other functions */
1483int mbedtls_ssl_tls13_write_finished_message( mbedtls_ssl_context *ssl )
1484{
1485 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1486 unsigned char *buf;
1487 size_t buf_len, msg_len;
1488
1489 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished message" ) );
1490
XiaokangQiandce82242021-11-15 06:01:26 +00001491 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_finished_message( ssl ) );
1492
XiaokangQian35dc6252021-11-11 08:16:19 +00001493 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg( ssl,
1494 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len ) );
1495
1496 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_finished_message_body(
1497 ssl, buf, buf + buf_len, &msg_len ) );
1498
Xiaofei Bai746f9482021-11-12 08:53:56 +00001499 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1500 buf, msg_len );
XiaokangQian35dc6252021-11-11 08:16:19 +00001501
1502 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_finished_message( ssl ) );
1503 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
1504 buf_len, msg_len ) );
XiaokangQian35dc6252021-11-11 08:16:19 +00001505
1506cleanup:
1507
1508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished message" ) );
1509 return( ret );
1510}
1511
Jerry Yu378254d2021-10-30 21:44:47 +08001512void mbedtls_ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
1513{
1514
1515 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
1516
1517 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001518 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001519 */
1520 if( ssl->session )
1521 {
Jerry Yu378254d2021-10-30 21:44:47 +08001522 mbedtls_ssl_session_free( ssl->session );
1523 mbedtls_free( ssl->session );
1524 }
1525 ssl->session = ssl->session_negotiate;
1526 ssl->session_negotiate = NULL;
1527
1528 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
1529}
1530
Ronald Cron49ad6192021-11-24 16:25:31 +01001531/*
1532 *
1533 * STATE HANDLING: Write ChangeCipherSpec
1534 *
1535 */
1536#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Ronald Cron49ad6192021-11-24 16:25:31 +01001537static int ssl_tls13_write_change_cipher_spec_body( mbedtls_ssl_context *ssl,
1538 unsigned char *buf,
1539 unsigned char *end,
1540 size_t *olen )
1541{
1542 ((void) ssl);
1543
1544 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 1 );
1545 buf[0] = 1;
1546 *olen = 1;
1547
1548 return( 0 );
1549}
1550
Ronald Cron49ad6192021-11-24 16:25:31 +01001551int mbedtls_ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1552{
1553 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1554
1555 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
1556
Ronald Cron49ad6192021-11-24 16:25:31 +01001557 /* Write CCS message */
1558 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_change_cipher_spec_body(
1559 ssl, ssl->out_msg,
1560 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1561 &ssl->out_msglen ) );
1562
1563 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1564
Ronald Cron49ad6192021-11-24 16:25:31 +01001565 /* Dispatch message */
Ronald Cron66dbf912022-02-02 15:33:46 +01001566 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_record( ssl, 0 ) );
Ronald Cron49ad6192021-11-24 16:25:31 +01001567
1568cleanup:
1569
1570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
1571 return( ret );
1572}
1573
1574#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1575
XiaokangQian78b1fa72022-01-19 06:56:30 +00001576/* Reset SSL context and update hash for handling HRR.
1577 *
1578 * Replace Transcript-Hash(X) by
1579 * Transcript-Hash( message_hash ||
1580 * 00 00 Hash.length ||
1581 * X )
1582 * A few states of the handshake are preserved, including:
1583 * - session ID
1584 * - session ticket
1585 * - negotiated ciphersuite
1586 */
1587int mbedtls_ssl_reset_transcript_for_hrr( mbedtls_ssl_context *ssl )
1588{
1589 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1590 unsigned char hash_transcript[ MBEDTLS_MD_MAX_SIZE + 4 ];
XiaokangQian0ece9982022-01-24 08:56:23 +00001591 size_t hash_len;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001592 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1593 uint16_t cipher_suite = ssl->session_negotiate->ciphersuite;
Przemyslaw Stekiel4b3fff42022-02-14 16:39:52 +01001594 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001595 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
1596
1597 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Reset SSL session for HRR" ) );
1598
XiaokangQian0ece9982022-01-24 08:56:23 +00001599 ret = mbedtls_ssl_get_handshake_transcript( ssl, ciphersuite_info->mac,
1600 hash_transcript + 4,
1601 MBEDTLS_MD_MAX_SIZE,
1602 &hash_len );
1603 if( ret != 0 )
1604 {
1605 MBEDTLS_SSL_DEBUG_RET( 4, "mbedtls_ssl_get_handshake_transcript", ret );
1606 return( ret );
1607 }
1608
1609 hash_transcript[0] = MBEDTLS_SSL_HS_MESSAGE_HASH;
1610 hash_transcript[1] = 0;
1611 hash_transcript[2] = 0;
1612 hash_transcript[3] = (unsigned char) hash_len;
1613
1614 hash_len += 4;
1615
XiaokangQian78b1fa72022-01-19 06:56:30 +00001616 if( ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
1617 {
1618#if defined(MBEDTLS_SHA256_C)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001619 MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-256 handshake transcript",
XiaokangQian0ece9982022-01-24 08:56:23 +00001620 hash_transcript, hash_len );
XiaokangQian78b1fa72022-01-19 06:56:30 +00001621
1622#if defined(MBEDTLS_USE_PSA_CRYPTO)
1623 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
1624 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
1625#else
1626 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
1627#endif
XiaokangQian78b1fa72022-01-19 06:56:30 +00001628#endif /* MBEDTLS_SHA256_C */
1629 }
1630 else if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1631 {
1632#if defined(MBEDTLS_SHA384_C)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001633 MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-384 handshake transcript",
XiaokangQian0ece9982022-01-24 08:56:23 +00001634 hash_transcript, hash_len );
XiaokangQian78b1fa72022-01-19 06:56:30 +00001635
1636#if defined(MBEDTLS_USE_PSA_CRYPTO)
1637 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
1638 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
1639#else
1640 mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 );
1641#endif
XiaokangQian78b1fa72022-01-19 06:56:30 +00001642#endif /* MBEDTLS_SHA384_C */
1643 }
1644
XiaokangQian0ece9982022-01-24 08:56:23 +00001645#if defined(MBEDTLS_SHA256_C) || defined(MBEDTLS_SHA384_C)
1646 ssl->handshake->update_checksum( ssl, hash_transcript, hash_len );
1647#endif /* MBEDTLS_SHA256_C || MBEDTLS_SHA384_C */
Przemyslaw Stekiel4b3fff42022-02-14 16:39:52 +01001648
1649 /* Destroy generated private key. */
1650 status = psa_destroy_key( ssl->handshake->ecdh_psa_privkey );
1651
1652 if( status != PSA_SUCCESS )
1653 {
1654 ret = psa_ssl_status_to_mbedtls( status );
1655 MBEDTLS_SSL_DEBUG_RET( 1, "psa_destroy_key", ret );
1656 return( ret );
1657 }
1658
1659 ssl->handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
1660
XiaokangQian78b1fa72022-01-19 06:56:30 +00001661 return( ret );
1662}
1663
Jerry Yufb4b6472022-01-27 15:03:26 +08001664#endif /* MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_PROTO_TLS1_3 */