blob: 9de9d6d9c72aa5f7db0b0064b06250463c741399 [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
Jerry Yufbe3e642022-04-25 19:31:51 +080037const uint8_t mbedtls_ssl_tls13_hello_retry_request_magic[
38 MBEDTLS_SERVER_HELLO_RANDOM_LEN ] =
39 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
40 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
41 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
42 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C };
Jerry Yu93a13f22022-04-11 23:00:01 +080043
Xiaofei Bai746f9482021-11-12 08:53:56 +000044int mbedtls_ssl_tls13_fetch_handshake_msg( mbedtls_ssl_context *ssl,
45 unsigned hs_type,
46 unsigned char **buf,
Xiaofei Baieef15042021-11-18 07:29:56 +000047 size_t *buf_len )
XiaokangQian6b226b02021-09-24 07:51:16 +000048{
49 int ret;
50
51 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
52 {
53 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
54 goto cleanup;
55 }
56
XiaokangQian16c61aa2021-09-27 09:30:17 +000057 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
XiaokangQian6b226b02021-09-24 07:51:16 +000058 ssl->in_msg[0] != hs_type )
59 {
XiaokangQian16c61aa2021-09-27 09:30:17 +000060 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Receive unexpected handshake message." ) );
XiaokangQian6b226b02021-09-24 07:51:16 +000061 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
XiaokangQian05420b12021-09-29 08:46:37 +000062 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
XiaokangQian6b226b02021-09-24 07:51:16 +000063 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
64 goto cleanup;
65 }
66
XiaokangQian05420b12021-09-29 08:46:37 +000067 /*
68 * Jump handshake header (4 bytes, see Section 4 of RFC 8446).
69 * ...
70 * HandshakeType msg_type;
71 * uint24 length;
72 * ...
73 */
Xiaofei Baieef15042021-11-18 07:29:56 +000074 *buf = ssl->in_msg + 4;
75 *buf_len = ssl->in_hslen - 4;
XiaokangQian6b226b02021-09-24 07:51:16 +000076
XiaokangQian6b226b02021-09-24 07:51:16 +000077cleanup:
78
79 return( ret );
80}
81
Jerry Yubc20bdd2021-08-24 15:59:48 +080082#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai69fcd392022-01-20 08:25:00 +000083/* mbedtls_ssl_tls13_parse_sig_alg_ext()
84 *
85 * enum {
86 * ....
87 * ecdsa_secp256r1_sha256( 0x0403 ),
88 * ecdsa_secp384r1_sha384( 0x0503 ),
89 * ecdsa_secp521r1_sha512( 0x0603 ),
90 * ....
91 * } SignatureScheme;
92 *
93 * struct {
94 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
95 * } SignatureSchemeList;
96 */
97int mbedtls_ssl_tls13_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
98 const unsigned char *buf,
99 const unsigned char *end )
100{
101 const unsigned char *p = buf;
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000102 size_t supported_sig_algs_len = 0;
103 const unsigned char *supported_sig_algs_end;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +0000104 uint16_t sig_alg;
Xiaofei Baic234ecf2022-02-08 09:59:23 +0000105 uint32_t common_idx = 0;
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000106
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000107 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000108 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000109 p += 2;
110
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000111 memset( ssl->handshake->received_sig_algs, 0,
Xiaofei Bai51f515a2022-02-08 07:28:04 +0000112 sizeof(ssl->handshake->received_sig_algs) );
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000113
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000114 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
115 supported_sig_algs_end = p + supported_sig_algs_len;
Xiaofei Bai51f515a2022-02-08 07:28:04 +0000116 while( p < supported_sig_algs_end )
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000117 {
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000118 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +0000119 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000120 p += 2;
121
122 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x",
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +0000123 sig_alg ) );
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000124
XiaokangQian08037552022-04-20 07:16:41 +0000125 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) ||
126 ! mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) )
Xiaofei Bai51f515a2022-02-08 07:28:04 +0000127 continue;
128
129 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000130 {
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000131 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +0000132 common_idx += 1;
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000133 }
134 }
135 /* Check that we consumed all the message. */
136 if( p != end )
137 {
138 MBEDTLS_SSL_DEBUG_MSG( 1,
139 ( "Signature algorithms extension length misaligned" ) );
140 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
141 MBEDTLS_ERR_SSL_DECODE_ERROR );
142 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
143 }
144
145 if( common_idx == 0 )
146 {
147 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
148 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
149 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
150 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
151 }
152
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000153 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS1_3_SIG_NONE;
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000154 return( 0 );
155}
156
Jerry Yu30b071c2021-09-12 20:16:03 +0800157/*
Jerry Yu30b071c2021-09-12 20:16:03 +0800158 * STATE HANDLING: Read CertificateVerify
159 */
Jerry Yud0fc5852021-10-29 11:09:06 +0800160/* Macro to express the maximum length of the verify structure.
Jerry Yu30b071c2021-09-12 20:16:03 +0800161 *
162 * The structure is computed per TLS 1.3 specification as:
163 * - 64 bytes of octet 32,
164 * - 33 bytes for the context string
165 * (which is either "TLS 1.3, client CertificateVerify"
166 * or "TLS 1.3, server CertificateVerify"),
Jerry Yud0fc5852021-10-29 11:09:06 +0800167 * - 1 byte for the octet 0x0, which serves as a separator,
Jerry Yu30b071c2021-09-12 20:16:03 +0800168 * - 32 or 48 bytes for the Transcript-Hash(Handshake Context, Certificate)
169 * (depending on the size of the transcript_hash)
170 *
171 * This results in a total size of
172 * - 130 bytes for a SHA256-based transcript hash, or
173 * (64 + 33 + 1 + 32 bytes)
174 * - 146 bytes for a SHA384-based transcript hash.
175 * (64 + 33 + 1 + 48 bytes)
176 *
177 */
Jerry Yu26c2d112021-10-25 12:42:58 +0800178#define SSL_VERIFY_STRUCT_MAX_SIZE ( 64 + \
179 33 + \
180 1 + \
181 MBEDTLS_TLS1_3_MD_MAX_SIZE \
Jerry Yu30b071c2021-09-12 20:16:03 +0800182 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800183
Jerry Yu0b32c502021-10-28 13:41:59 +0800184/*
185 * The ssl_tls13_create_verify_structure() creates the verify structure.
186 * As input, it requires the transcript hash.
187 *
188 * The caller has to ensure that the buffer has size at least
189 * SSL_VERIFY_STRUCT_MAX_SIZE bytes.
190 */
Jerry Yud0fc5852021-10-29 11:09:06 +0800191static void ssl_tls13_create_verify_structure( const unsigned char *transcript_hash,
Jerry Yu0b32c502021-10-28 13:41:59 +0800192 size_t transcript_hash_len,
193 unsigned char *verify_buffer,
194 size_t *verify_buffer_len,
195 int from )
196{
197 size_t idx;
Jerry Yu30b071c2021-09-12 20:16:03 +0800198
Jerry Yu0b32c502021-10-28 13:41:59 +0800199 /* RFC 8446, Section 4.4.3:
200 *
201 * The digital signature [in the CertificateVerify message] is then
202 * computed over the concatenation of:
203 * - A string that consists of octet 32 (0x20) repeated 64 times
204 * - The context string
205 * - A single 0 byte which serves as the separator
206 * - The content to be signed
207 */
208 memset( verify_buffer, 0x20, 64 );
209 idx = 64;
210
211 if( from == MBEDTLS_SSL_IS_CLIENT )
212 {
213 memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( client_cv ) );
214 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( client_cv );
215 }
216 else
217 { /* from == MBEDTLS_SSL_IS_SERVER */
218 memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( server_cv ) );
219 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( server_cv );
220 }
221
222 verify_buffer[idx++] = 0x0;
223
224 memcpy( verify_buffer + idx, transcript_hash, transcript_hash_len );
225 idx += transcript_hash_len;
226
227 *verify_buffer_len = idx;
228}
229
Jerry Yu0b32c502021-10-28 13:41:59 +0800230static int ssl_tls13_parse_certificate_verify( mbedtls_ssl_context *ssl,
Jerry Yud0fc5852021-10-29 11:09:06 +0800231 const unsigned char *buf,
232 const unsigned char *end,
233 const unsigned char *verify_buffer,
234 size_t verify_buffer_len )
Jerry Yu30b071c2021-09-12 20:16:03 +0800235{
236 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
237 const unsigned char *p = buf;
238 uint16_t algorithm;
Jerry Yu30b071c2021-09-12 20:16:03 +0800239 size_t signature_len;
240 mbedtls_pk_type_t sig_alg;
241 mbedtls_md_type_t md_alg;
Jerry Yud0fc5852021-10-29 11:09:06 +0800242 unsigned char verify_hash[MBEDTLS_MD_MAX_SIZE];
Jerry Yu30b071c2021-09-12 20:16:03 +0800243 size_t verify_hash_len;
244
Xiaofei Baid25fab62021-12-02 06:36:27 +0000245 void const *options = NULL;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000246#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Xiaofei Baid25fab62021-12-02 06:36:27 +0000247 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000248#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
249
Jerry Yu30b071c2021-09-12 20:16:03 +0800250 /*
251 * struct {
252 * SignatureScheme algorithm;
253 * opaque signature<0..2^16-1>;
254 * } CertificateVerify;
255 */
256 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
257 algorithm = MBEDTLS_GET_UINT16_BE( p, 0 );
258 p += 2;
259
260 /* RFC 8446 section 4.4.3
261 *
262 * If the CertificateVerify message is sent by a server, the signature algorithm
263 * MUST be one offered in the client's "signature_algorithms" extension unless
264 * no valid certificate chain can be produced without unsupported algorithms
265 *
266 * RFC 8446 section 4.4.2.2
267 *
268 * If the client cannot construct an acceptable chain using the provided
269 * certificates and decides to abort the handshake, then it MUST abort the handshake
270 * with an appropriate certificate-related alert (by default, "unsupported_certificate").
271 *
Jerry Yu6f87f252021-10-29 20:12:51 +0800272 * Check if algorithm is an offered signature algorithm.
Jerry Yu30b071c2021-09-12 20:16:03 +0800273 */
Jerry Yu24811fb2022-01-19 18:02:15 +0800274 if( ! mbedtls_ssl_sig_alg_is_offered( ssl, algorithm ) )
Jerry Yu30b071c2021-09-12 20:16:03 +0800275 {
Jerry Yu982d9e52021-10-14 15:59:37 +0800276 /* algorithm not in offered signature algorithms list */
277 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Received signature algorithm(%04x) is not "
278 "offered.",
279 ( unsigned int ) algorithm ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800280 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800281 }
282
Jerry Yu8c338862022-03-23 13:34:04 +0800283 if( mbedtls_ssl_tls13_get_pk_type_and_md_alg_from_sig_alg(
Jerry Yuf8aa9a42022-03-23 20:40:28 +0800284 algorithm, &sig_alg, &md_alg ) != 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800285 {
Jerry Yu8c338862022-03-23 13:34:04 +0800286 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800287 }
288
289 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate Verify: Signature algorithm ( %04x )",
290 ( unsigned int ) algorithm ) );
291
292 /*
293 * Check the certificate's key type matches the signature alg
294 */
295 if( !mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk, sig_alg ) )
296 {
297 MBEDTLS_SSL_DEBUG_MSG( 1, ( "signature algorithm doesn't match cert key" ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800298 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800299 }
300
301 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
302 signature_len = MBEDTLS_GET_UINT16_BE( p, 0 );
303 p += 2;
304 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, signature_len );
305
306 /* Hash verify buffer with indicated hash function */
307 switch( md_alg )
308 {
309#if defined(MBEDTLS_SHA256_C)
310 case MBEDTLS_MD_SHA256:
311 verify_hash_len = 32;
Jerry Yu133690c2021-10-25 14:01:13 +0800312 ret = mbedtls_sha256( verify_buffer, verify_buffer_len, verify_hash, 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800313 break;
314#endif /* MBEDTLS_SHA256_C */
315
316#if defined(MBEDTLS_SHA384_C)
317 case MBEDTLS_MD_SHA384:
318 verify_hash_len = 48;
Jerry Yu133690c2021-10-25 14:01:13 +0800319 ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 1 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800320 break;
321#endif /* MBEDTLS_SHA384_C */
322
323#if defined(MBEDTLS_SHA512_C)
324 case MBEDTLS_MD_SHA512:
325 verify_hash_len = 64;
Jerry Yu133690c2021-10-25 14:01:13 +0800326 ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800327 break;
328#endif /* MBEDTLS_SHA512_C */
329
Jerry Yu0b32c502021-10-28 13:41:59 +0800330 default:
331 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
332 break;
Jerry Yu30b071c2021-09-12 20:16:03 +0800333 }
334
Jerry Yu133690c2021-10-25 14:01:13 +0800335 if( ret != 0 )
336 {
337 MBEDTLS_SSL_DEBUG_RET( 1, "hash computation error", ret );
Jerry Yu6f87f252021-10-29 20:12:51 +0800338 goto error;
Jerry Yu133690c2021-10-25 14:01:13 +0800339 }
340
Jerry Yu30b071c2021-09-12 20:16:03 +0800341 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
XiaokangQian82d34cc2021-11-03 08:51:56 +0000342#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
343 if( sig_alg == MBEDTLS_PK_RSASSA_PSS )
344 {
345 const mbedtls_md_info_t* md_info;
Xiaofei Baid25fab62021-12-02 06:36:27 +0000346 rsassa_pss_options.mgf1_hash_id = md_alg;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000347 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
348 {
349 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
350 }
Xiaofei Baid25fab62021-12-02 06:36:27 +0000351 rsassa_pss_options.expected_salt_len = mbedtls_md_get_size( md_info );
352 options = (const void*) &rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000353 }
354#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800355
Xiaofei Baid25fab62021-12-02 06:36:27 +0000356 if( ( ret = mbedtls_pk_verify_ext( sig_alg, options,
Jerry Yu30b071c2021-09-12 20:16:03 +0800357 &ssl->session_negotiate->peer_cert->pk,
358 md_alg, verify_hash, verify_hash_len,
Jerry Yu6f87f252021-10-29 20:12:51 +0800359 p, signature_len ) ) == 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800360 {
Jerry Yu6f87f252021-10-29 20:12:51 +0800361 return( 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800362 }
Jerry Yu6f87f252021-10-29 20:12:51 +0800363 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify_ext", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800364
Jerry Yu6f87f252021-10-29 20:12:51 +0800365error:
366 /* RFC 8446 section 4.4.3
367 *
368 * If the verification fails, the receiver MUST terminate the handshake
369 * with a "decrypt_error" alert.
370 */
371 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
372 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
373 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
374
Jerry Yu30b071c2021-09-12 20:16:03 +0800375}
376#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
377
378int mbedtls_ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
379{
Jerry Yu30b071c2021-09-12 20:16:03 +0800380
Jerry Yuda8cdf22021-10-25 15:06:49 +0800381#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
382 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
383 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
384 size_t verify_buffer_len;
385 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
386 size_t transcript_len;
387 unsigned char *buf;
388 size_t buf_len;
389
Jerry Yu30b071c2021-09-12 20:16:03 +0800390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
391
Jerry Yuda8cdf22021-10-25 15:06:49 +0800392 MBEDTLS_SSL_PROC_CHK(
Xiaofei Bai746f9482021-11-12 08:53:56 +0000393 mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
Jerry Yuda8cdf22021-10-25 15:06:49 +0800394 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu30b071c2021-09-12 20:16:03 +0800395
Jerry Yuda8cdf22021-10-25 15:06:49 +0800396 /* Need to calculate the hash of the transcript first
Jerry Yu0b32c502021-10-28 13:41:59 +0800397 * before reading the message since otherwise it gets
398 * included in the transcript
399 */
Jerry Yuda8cdf22021-10-25 15:06:49 +0800400 ret = mbedtls_ssl_get_handshake_transcript( ssl,
401 ssl->handshake->ciphersuite_info->mac,
402 transcript, sizeof( transcript ),
403 &transcript_len );
404 if( ret != 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800405 {
Jerry Yuda8cdf22021-10-25 15:06:49 +0800406 MBEDTLS_SSL_PEND_FATAL_ALERT(
407 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
408 MBEDTLS_ERR_SSL_INTERNAL_ERROR );
409 return( ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800410 }
411
Jerry Yuda8cdf22021-10-25 15:06:49 +0800412 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash", transcript, transcript_len );
413
414 /* Create verify structure */
415 ssl_tls13_create_verify_structure( transcript,
Jerry Yu0b32c502021-10-28 13:41:59 +0800416 transcript_len,
417 verify_buffer,
418 &verify_buffer_len,
419 ( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) ?
420 MBEDTLS_SSL_IS_SERVER :
421 MBEDTLS_SSL_IS_CLIENT );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800422
423 /* Process the message contents */
Jerry Yu0b32c502021-10-28 13:41:59 +0800424 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_verify( ssl, buf,
425 buf + buf_len, verify_buffer, verify_buffer_len ) );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800426
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100427 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
428 buf, buf_len );
Jerry Yu30b071c2021-09-12 20:16:03 +0800429
430cleanup:
431
432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
Jerry Yu5398c102021-11-05 13:32:38 +0800433 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_process_certificate_verify", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800434 return( ret );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800435#else
436 ((void) ssl);
437 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
438 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
439#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800440}
441
442/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000443 *
444 * STATE HANDLING: Incoming Certificate, client-side only currently.
445 *
446 */
447
448/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000449 * Implementation
450 */
451
Xiaofei Bai947571e2021-09-29 09:12:03 +0000452#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
453#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
454/*
455 * Structure of Certificate message:
456 *
457 * enum {
458 * X509(0),
459 * RawPublicKey(2),
460 * (255)
461 * } CertificateType;
462 *
463 * struct {
464 * select (certificate_type) {
465 * case RawPublicKey:
466 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
467 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
468 * case X509:
469 * opaque cert_data<1..2^24-1>;
470 * };
471 * Extension extensions<0..2^16-1>;
472 * } CertificateEntry;
473 *
474 * struct {
475 * opaque certificate_request_context<0..2^8-1>;
476 * CertificateEntry certificate_list<0..2^24-1>;
477 * } Certificate;
478 *
479 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000480
481/* Parse certificate chain send by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000482static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
483 const unsigned char *buf,
484 const unsigned char *end )
485{
486 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
487 size_t certificate_request_context_len = 0;
488 size_t certificate_list_len = 0;
489 const unsigned char *p = buf;
490 const unsigned char *certificate_list_end;
491
492 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
493 certificate_request_context_len = p[0];
Jerry Yub640bf62021-10-29 10:05:32 +0800494 certificate_list_len = MBEDTLS_GET_UINT24_BE( p, 1 );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000495 p += 4;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000496
497 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
498 * support anything beyond 2^16 = 64K.
499 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000500 if( ( certificate_request_context_len != 0 ) ||
501 ( certificate_list_len >= 0x10000 ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000502 {
503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
504 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
505 MBEDTLS_ERR_SSL_DECODE_ERROR );
506 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
507 }
508
509 /* In case we tried to reuse a session but it failed */
510 if( ssl->session_negotiate->peer_cert != NULL )
511 {
512 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
513 mbedtls_free( ssl->session_negotiate->peer_cert );
514 }
515
516 if( ( ssl->session_negotiate->peer_cert =
517 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
518 {
519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
520 sizeof( mbedtls_x509_crt ) ) );
521 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
522 MBEDTLS_ERR_SSL_ALLOC_FAILED );
523 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
524 }
525
526 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
527
Xiaofei Bai947571e2021-09-29 09:12:03 +0000528 certificate_list_end = p + certificate_list_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000529 while( p < certificate_list_end )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000530 {
531 size_t cert_data_len, extensions_len;
532
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000533 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 3 );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000534 cert_data_len = MBEDTLS_GET_UINT24_BE( p, 0 );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000535 p += 3;
536
537 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
538 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
539 * check that we have a minimum of 128 bytes of data, this is not
540 * clear why we need that though.
541 */
542 if( ( cert_data_len < 128 ) || ( cert_data_len >= 0x10000 ) )
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000543 {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000544 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
545 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
546 MBEDTLS_ERR_SSL_DECODE_ERROR );
547 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
548 }
549
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000550 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, cert_data_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000551 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
552 p, cert_data_len );
553
554 switch( ret )
555 {
556 case 0: /*ok*/
557 break;
558 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
559 /* Ignore certificate with an unknown algorithm: maybe a
560 prior certificate was already trusted. */
561 break;
562
563 case MBEDTLS_ERR_X509_ALLOC_FAILED:
564 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
565 MBEDTLS_ERR_X509_ALLOC_FAILED );
566 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
567 return( ret );
568
569 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
570 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
571 MBEDTLS_ERR_X509_UNKNOWN_VERSION );
572 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
573 return( ret );
574
575 default:
576 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
577 ret );
578 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
579 return( ret );
580 }
581
582 p += cert_data_len;
583
584 /* Certificate extensions length */
585 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 2 );
586 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
587 p += 2;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000588 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, extensions_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000589 p += extensions_len;
590 }
591
592 /* Check that all the message is consumed. */
593 if( p != end )
594 {
595 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
596 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, \
597 MBEDTLS_ERR_SSL_DECODE_ERROR );
598 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
599 }
600
601 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
602
603 return( ret );
604}
605#else
606static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
607 const unsigned char *buf,
608 const unsigned char *end )
609{
610 ((void) ssl);
611 ((void) buf);
612 ((void) end);
613 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
614}
615#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
616#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
617
618#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
619#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000620/* Validate certificate chain sent by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000621static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
622{
623 int ret = 0;
624 mbedtls_x509_crt *ca_chain;
625 mbedtls_x509_crl *ca_crl;
Xiaofei Baiff456022021-10-28 06:50:17 +0000626 uint32_t verify_result = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000627
628#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
629 if( ssl->handshake->sni_ca_chain != NULL )
630 {
631 ca_chain = ssl->handshake->sni_ca_chain;
632 ca_crl = ssl->handshake->sni_ca_crl;
633 }
634 else
635#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
636 {
637 ca_chain = ssl->conf->ca_chain;
638 ca_crl = ssl->conf->ca_crl;
639 }
640
641 /*
642 * Main check: verify certificate
643 */
644 ret = mbedtls_x509_crt_verify_with_profile(
645 ssl->session_negotiate->peer_cert,
646 ca_chain, ca_crl,
647 ssl->conf->cert_profile,
648 ssl->hostname,
Xiaofei Baiff456022021-10-28 06:50:17 +0000649 &verify_result,
Xiaofei Bai947571e2021-09-29 09:12:03 +0000650 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
651
652 if( ret != 0 )
653 {
654 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
655 }
656
657 /*
658 * Secondary checks: always done, but change 'ret' only if it was 0
659 */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000660 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
661 ssl->handshake->ciphersuite_info,
662 !ssl->conf->endpoint,
Xiaofei Baiff456022021-10-28 06:50:17 +0000663 &verify_result ) != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000664 {
665 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( usage extensions )" ) );
666 if( ret == 0 )
667 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
668 }
669
670
671 if( ca_chain == NULL )
672 {
673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
674 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
675 }
676
677 if( ret != 0 )
678 {
679 /* The certificate may have been rejected for several reasons.
680 Pick one and send the corresponding alert. Which alert to send
681 may be a subject of debate in some cases. */
Xiaofei Baiff456022021-10-28 06:50:17 +0000682 if( verify_result & MBEDTLS_X509_BADCERT_OTHER )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000683 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000684 else if( verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000685 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000686 else if( verify_result & ( MBEDTLS_X509_BADCERT_KEY_USAGE |
687 MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
688 MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
689 MBEDTLS_X509_BADCERT_BAD_PK |
690 MBEDTLS_X509_BADCERT_BAD_KEY ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000691 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000692 else if( verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000693 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000694 else if( verify_result & MBEDTLS_X509_BADCERT_REVOKED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000695 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000696 else if( verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000697 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret );
698 else
699 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret );
700 }
701
702#if defined(MBEDTLS_DEBUG_C)
Xiaofei Baiff456022021-10-28 06:50:17 +0000703 if( verify_result != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000704 {
705 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
Jerry Yu83bb1312021-10-28 22:16:33 +0800706 (unsigned int) verify_result ) );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000707 }
708 else
709 {
710 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
711 }
712#endif /* MBEDTLS_DEBUG_C */
713
Xiaofei Baiff456022021-10-28 06:50:17 +0000714 ssl->session_negotiate->verify_result = verify_result;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000715 return( ret );
716}
717#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
718static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
719{
720 ((void) ssl);
721 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
722}
723#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
724#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
725
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000726int mbedtls_ssl_tls13_process_certificate( mbedtls_ssl_context *ssl )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000727{
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000728 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
730
731#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
732 unsigned char *buf;
733 size_t buf_len;
734
Xiaofei Bai746f9482021-11-12 08:53:56 +0000735 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000736 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
737 &buf, &buf_len ) );
738
739 /* Parse the certificate chain sent by the peer. */
740 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate( ssl, buf, buf + buf_len ) );
741 /* Validate the certificate chain and set the verification results. */
742 MBEDTLS_SSL_PROC_CHK( ssl_tls13_validate_certificate( ssl ) );
743
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100744 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
745 buf, buf_len );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000746
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000747cleanup:
748
749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Xiaofei Bai10aeec02021-10-26 09:50:08 +0000750#else
751 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
752 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
753#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000754 return( ret );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000755}
Jerry Yu90f152d2022-01-29 22:12:42 +0800756#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7399d0d2022-01-30 17:54:19 +0800757/*
758 * enum {
759 * X509(0),
760 * RawPublicKey(2),
761 * (255)
762 * } CertificateType;
763 *
764 * struct {
765 * select (certificate_type) {
766 * case RawPublicKey:
767 * // From RFC 7250 ASN.1_subjectPublicKeyInfo
768 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
769 *
770 * case X509:
771 * opaque cert_data<1..2^24-1>;
772 * };
773 * Extension extensions<0..2^16-1>;
774 * } CertificateEntry;
775 *
776 * struct {
777 * opaque certificate_request_context<0..2^8-1>;
778 * CertificateEntry certificate_list<0..2^24-1>;
779 * } Certificate;
780 */
781static int ssl_tls13_write_certificate_body( mbedtls_ssl_context *ssl,
Jerry Yu3e536442022-02-15 11:05:59 +0800782 unsigned char *buf,
Jerry Yu7399d0d2022-01-30 17:54:19 +0800783 unsigned char *end,
Jerry Yu3e536442022-02-15 11:05:59 +0800784 size_t *out_len )
Jerry Yu5cc35062022-01-28 16:16:08 +0800785{
Jerry Yu5cc35062022-01-28 16:16:08 +0800786 const mbedtls_x509_crt *crt = mbedtls_ssl_own_cert( ssl );
Jerry Yu3e536442022-02-15 11:05:59 +0800787 unsigned char *p = buf;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800788 unsigned char *certificate_request_context =
789 ssl->handshake->certificate_request_context;
790 unsigned char certificate_request_context_len =
791 ssl->handshake->certificate_request_context_len;
792 unsigned char *p_certificate_list_len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800793
Jerry Yu5cc35062022-01-28 16:16:08 +0800794
Jerry Yu3391ac02022-02-16 11:21:37 +0800795 /* ...
796 * opaque certificate_request_context<0..2^8-1>;
797 * ...
798 */
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800799 MBEDTLS_SSL_CHK_BUF_PTR( p, end, certificate_request_context_len + 1 );
800 *p++ = certificate_request_context_len;
801 if( certificate_request_context_len > 0 )
Jerry Yu537530d2022-02-15 14:00:57 +0800802 {
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800803 memcpy( p, certificate_request_context, certificate_request_context_len );
804 p += certificate_request_context_len;
Jerry Yu537530d2022-02-15 14:00:57 +0800805 }
806
Jerry Yu3391ac02022-02-16 11:21:37 +0800807 /* ...
808 * CertificateEntry certificate_list<0..2^24-1>;
809 * ...
810 */
Jerry Yu3e536442022-02-15 11:05:59 +0800811 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 );
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800812 p_certificate_list_len = p;
Jerry Yu3e536442022-02-15 11:05:59 +0800813 p += 3;
814
Jerry Yu7399d0d2022-01-30 17:54:19 +0800815 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", crt );
Jerry Yu5cc35062022-01-28 16:16:08 +0800816
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800817 while( crt != NULL )
Jerry Yu5cc35062022-01-28 16:16:08 +0800818 {
Jerry Yu7399d0d2022-01-30 17:54:19 +0800819 size_t cert_data_len = crt->raw.len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800820
Jerry Yu7399d0d2022-01-30 17:54:19 +0800821 MBEDTLS_SSL_CHK_BUF_PTR( p, end, cert_data_len + 3 + 2 );
822 MBEDTLS_PUT_UINT24_BE( cert_data_len, p, 0 );
823 p += 3;
Jerry Yu5cc35062022-01-28 16:16:08 +0800824
Jerry Yu7399d0d2022-01-30 17:54:19 +0800825 memcpy( p, crt->raw.p, cert_data_len );
826 p += cert_data_len;
827 crt = crt->next;
Jerry Yu5cc35062022-01-28 16:16:08 +0800828
829 /* Currently, we don't have any certificate extensions defined.
830 * Hence, we are sending an empty extension with length zero.
831 */
Jerry Yu7399d0d2022-01-30 17:54:19 +0800832 MBEDTLS_PUT_UINT24_BE( 0, p, 0 );
833 p += 2;
Jerry Yu5cc35062022-01-28 16:16:08 +0800834 }
Jerry Yu5cc35062022-01-28 16:16:08 +0800835
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800836 MBEDTLS_PUT_UINT24_BE( p - p_certificate_list_len - 3,
837 p_certificate_list_len, 0 );
Jerry Yu7399d0d2022-01-30 17:54:19 +0800838
Jerry Yu3e536442022-02-15 11:05:59 +0800839 *out_len = p - buf;
Jerry Yu5cc35062022-01-28 16:16:08 +0800840
841 return( 0 );
842}
Jerry Yu5cc35062022-01-28 16:16:08 +0800843
Jerry Yu3e536442022-02-15 11:05:59 +0800844int mbedtls_ssl_tls13_write_certificate( mbedtls_ssl_context *ssl )
Jerry Yu5cc35062022-01-28 16:16:08 +0800845{
846 int ret;
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100847 unsigned char *buf;
848 size_t buf_len, msg_len;
849
Jerry Yu5cc35062022-01-28 16:16:08 +0800850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
851
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100852 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100853 MBEDTLS_SSL_HS_CERTIFICATE, &buf, &buf_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800854
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100855 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_body( ssl,
856 buf,
857 buf + buf_len,
858 &msg_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800859
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100860 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
861 buf, msg_len );
Jerry Yu5cc35062022-01-28 16:16:08 +0800862
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100863 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100864 ssl, buf_len, msg_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800865cleanup:
866
867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
868 return( ret );
869}
870
Jerry Yu3e536442022-02-15 11:05:59 +0800871/*
872 * STATE HANDLING: Output Certificate Verify
873 */
Jerry Yue91a51a2022-03-22 21:42:50 +0800874static int ssl_tls13_get_sig_alg_from_pk( mbedtls_ssl_context *ssl,
875 mbedtls_pk_context *own_key,
Jerry Yu8c338862022-03-23 13:34:04 +0800876 uint16_t *algorithm )
Jerry Yu67eced02022-02-25 13:37:36 +0800877{
878 mbedtls_pk_type_t sig = mbedtls_ssl_sig_from_pk( own_key );
879 /* Determine the size of the key */
880 size_t own_key_size = mbedtls_pk_get_bitlen( own_key );
Jerry Yue91a51a2022-03-22 21:42:50 +0800881 *algorithm = MBEDTLS_TLS1_3_SIG_NONE;
Jerry Yu67eced02022-02-25 13:37:36 +0800882 ((void) own_key_size);
883
884 switch( sig )
885 {
886#if defined(MBEDTLS_ECDSA_C)
887 case MBEDTLS_SSL_SIG_ECDSA:
888 switch( own_key_size )
889 {
890 case 256:
Jerry Yue91a51a2022-03-22 21:42:50 +0800891 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256;
Jerry Yue91a51a2022-03-22 21:42:50 +0800892 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800893 case 384:
Jerry Yue91a51a2022-03-22 21:42:50 +0800894 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384;
Jerry Yue91a51a2022-03-22 21:42:50 +0800895 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800896 case 521:
Jerry Yue91a51a2022-03-22 21:42:50 +0800897 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512;
Jerry Yue91a51a2022-03-22 21:42:50 +0800898 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800899 default:
900 MBEDTLS_SSL_DEBUG_MSG( 3,
901 ( "unknown key size: %"
902 MBEDTLS_PRINTF_SIZET " bits",
903 own_key_size ) );
904 break;
905 }
906 break;
907#endif /* MBEDTLS_ECDSA_C */
908
Jerry Yucef3f332022-03-22 23:00:13 +0800909#if defined(MBEDTLS_RSA_C)
Jerry Yu67eced02022-02-25 13:37:36 +0800910 case MBEDTLS_SSL_SIG_RSA:
Jerry Yucef3f332022-03-22 23:00:13 +0800911#if defined(MBEDTLS_PKCS1_V21)
912#if defined(MBEDTLS_SHA256_C)
Jerry Yu67eced02022-02-25 13:37:36 +0800913 if( own_key_size <= 2048 &&
914 mbedtls_ssl_sig_alg_is_received( ssl,
915 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256 ) )
916 {
Jerry Yue91a51a2022-03-22 21:42:50 +0800917 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256;
Jerry Yue91a51a2022-03-22 21:42:50 +0800918 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800919 }
Jerry Yucef3f332022-03-22 23:00:13 +0800920 else
921#endif /* MBEDTLS_SHA256_C */
922#if defined(MBEDTLS_SHA384_C)
923 if( own_key_size <= 3072 &&
924 mbedtls_ssl_sig_alg_is_received( ssl,
Jerry Yu67eced02022-02-25 13:37:36 +0800925 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384 ) )
926 {
Jerry Yue91a51a2022-03-22 21:42:50 +0800927 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384;
Jerry Yue91a51a2022-03-22 21:42:50 +0800928 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800929 }
Jerry Yucef3f332022-03-22 23:00:13 +0800930 else
931#endif /* MBEDTLS_SHA384_C */
932#if defined(MBEDTLS_SHA512_C)
933 if( own_key_size <= 4096 &&
934 mbedtls_ssl_sig_alg_is_received( ssl,
Jerry Yu67eced02022-02-25 13:37:36 +0800935 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512 ) )
936 {
Jerry Yue91a51a2022-03-22 21:42:50 +0800937 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512;
Jerry Yue91a51a2022-03-22 21:42:50 +0800938 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800939 }
Jerry Yucef3f332022-03-22 23:00:13 +0800940 else
941#endif /* MBEDTLS_SHA512_C */
942#endif /* MBEDTLS_PKCS1_V21 */
943#if defined(MBEDTLS_PKCS1_V15)
944#if defined(MBEDTLS_SHA256_C)
945 if( own_key_size <= 2048 &&
946 mbedtls_ssl_sig_alg_is_received( ssl,
947 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256 ) )
948 {
949 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256;
Jerry Yucef3f332022-03-22 23:00:13 +0800950 return( 0 );
951 }
952 else
953#endif /* MBEDTLS_SHA256_C */
954#if defined(MBEDTLS_SHA384_C)
955 if( own_key_size <= 3072 &&
956 mbedtls_ssl_sig_alg_is_received( ssl,
957 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384 ) )
958 {
959 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384;
Jerry Yucef3f332022-03-22 23:00:13 +0800960 return( 0 );
961 }
962 else
963#endif /* MBEDTLS_SHA384_C */
964#if defined(MBEDTLS_SHA512_C)
965 if( own_key_size <= 4096 &&
966 mbedtls_ssl_sig_alg_is_received( ssl,
967 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512 ) )
968 {
969 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512;
Jerry Yucef3f332022-03-22 23:00:13 +0800970 return( 0 );
971 }
972 else
973#endif /* MBEDTLS_SHA512_C */
974#endif /* MBEDTLS_PKCS1_V15 */
975 {
976 MBEDTLS_SSL_DEBUG_MSG( 3,
977 ( "unknown key size: %"
978 MBEDTLS_PRINTF_SIZET " bits",
979 own_key_size ) );
980 }
Jerry Yu67eced02022-02-25 13:37:36 +0800981 break;
Jerry Yucef3f332022-03-22 23:00:13 +0800982#endif /* MBEDTLS_RSA_C */
Jerry Yu67eced02022-02-25 13:37:36 +0800983 default:
984 MBEDTLS_SSL_DEBUG_MSG( 1,
Andrzej Kurek5c65c572022-04-13 14:28:52 -0400985 ( "unknown signature type : %u", sig ) );
Jerry Yu67eced02022-02-25 13:37:36 +0800986 break;
987 }
Jerry Yue91a51a2022-03-22 21:42:50 +0800988 return( -1 );
Jerry Yu67eced02022-02-25 13:37:36 +0800989}
990
Jerry Yu3e536442022-02-15 11:05:59 +0800991static int ssl_tls13_write_certificate_verify_body( mbedtls_ssl_context *ssl,
992 unsigned char *buf,
993 unsigned char *end,
994 size_t *out_len )
Jerry Yu8511f122022-01-29 10:01:04 +0800995{
996 int ret;
Jerry Yu3e536442022-02-15 11:05:59 +0800997 unsigned char *p = buf;
Jerry Yu8511f122022-01-29 10:01:04 +0800998 mbedtls_pk_context *own_key;
Jerry Yu3e536442022-02-15 11:05:59 +0800999
Jerry Yu8511f122022-01-29 10:01:04 +08001000 unsigned char handshake_hash[ MBEDTLS_TLS1_3_MD_MAX_SIZE ];
1001 size_t handshake_hash_len;
Jerry Yu3e536442022-02-15 11:05:59 +08001002 unsigned char verify_buffer[ SSL_VERIFY_STRUCT_MAX_SIZE ];
1003 size_t verify_buffer_len;
Jerry Yu67eced02022-02-25 13:37:36 +08001004 mbedtls_pk_type_t pk_type = MBEDTLS_PK_NONE;
Jerry Yu67eced02022-02-25 13:37:36 +08001005 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Jerry Yu78272072022-02-22 10:28:13 +08001006 uint16_t algorithm = MBEDTLS_TLS1_3_SIG_NONE;
Jerry Yu3e536442022-02-15 11:05:59 +08001007 size_t signature_len = 0;
Jerry Yu8511f122022-01-29 10:01:04 +08001008 const mbedtls_md_info_t *md_info;
Jerry Yu3391ac02022-02-16 11:21:37 +08001009 unsigned char verify_hash[ MBEDTLS_MD_MAX_SIZE ];
Jerry Yu3e536442022-02-15 11:05:59 +08001010 size_t verify_hash_len;
Jerry Yu8511f122022-01-29 10:01:04 +08001011
Jerry Yu0b7b1012022-02-23 12:23:05 +08001012 *out_len = 0;
1013
Jerry Yu3e536442022-02-15 11:05:59 +08001014 own_key = mbedtls_ssl_own_key( ssl );
1015 if( own_key == NULL )
Jerry Yu8511f122022-01-29 10:01:04 +08001016 {
Jerry Yu3e536442022-02-15 11:05:59 +08001017 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1018 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu8511f122022-01-29 10:01:04 +08001019 }
1020
Jerry Yu8511f122022-01-29 10:01:04 +08001021 ret = mbedtls_ssl_get_handshake_transcript( ssl,
1022 ssl->handshake->ciphersuite_info->mac,
1023 handshake_hash,
1024 sizeof( handshake_hash ),
1025 &handshake_hash_len );
1026 if( ret != 0 )
1027 return( ret );
1028
1029 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash",
1030 handshake_hash,
1031 handshake_hash_len);
1032
Jerry Yu8511f122022-01-29 10:01:04 +08001033 ssl_tls13_create_verify_structure( handshake_hash, handshake_hash_len,
1034 verify_buffer, &verify_buffer_len,
1035 ssl->conf->endpoint );
1036
1037 /*
1038 * struct {
1039 * SignatureScheme algorithm;
1040 * opaque signature<0..2^16-1>;
1041 * } CertificateVerify;
1042 */
Jerry Yu8c338862022-03-23 13:34:04 +08001043 ret = ssl_tls13_get_sig_alg_from_pk( ssl, own_key, &algorithm );
Jerry Yue91a51a2022-03-22 21:42:50 +08001044 if( ret != 0 || ! mbedtls_ssl_sig_alg_is_received( ssl, algorithm ) )
Jerry Yu8511f122022-01-29 10:01:04 +08001045 {
Jerry Yud66409a2022-02-18 16:42:24 +08001046 MBEDTLS_SSL_DEBUG_MSG( 1,
Jerry Yu2124d052022-02-18 21:07:18 +08001047 ( "signature algorithm not in received or offered list." ) );
Jerry Yu67eced02022-02-25 13:37:36 +08001048
1049 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Signature algorithm is %s",
1050 mbedtls_ssl_sig_alg_to_str( algorithm ) ) );
1051
Jerry Yu71f36f12022-02-23 17:34:29 +08001052 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
Jerry Yud66409a2022-02-18 16:42:24 +08001053 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu3391ac02022-02-16 11:21:37 +08001054 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu8511f122022-01-29 10:01:04 +08001055 }
1056
Jerry Yu6c6f1022022-03-25 11:09:50 +08001057 if( mbedtls_ssl_tls13_get_pk_type_and_md_alg_from_sig_alg(
1058 algorithm, &pk_type, &md_alg ) != 0 )
Jerry Yu8c338862022-03-23 13:34:04 +08001059 {
Jerry Yuf8aa9a42022-03-23 20:40:28 +08001060 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu8c338862022-03-23 13:34:04 +08001061 }
1062
Jerry Yu3391ac02022-02-16 11:21:37 +08001063 /* Check there is space for the algorithm identifier (2 bytes) and the
1064 * signature length (2 bytes).
1065 */
1066 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Jerry Yu3e536442022-02-15 11:05:59 +08001067 MBEDTLS_PUT_UINT16_BE( algorithm, p, 0 );
1068 p += 2;
Jerry Yu8511f122022-01-29 10:01:04 +08001069
1070 /* Hash verify buffer with indicated hash function */
1071 md_info = mbedtls_md_info_from_type( md_alg );
1072 if( md_info == NULL )
1073 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1074
1075 ret = mbedtls_md( md_info, verify_buffer, verify_buffer_len, verify_hash );
1076 if( ret != 0 )
1077 return( ret );
1078
1079 verify_hash_len = mbedtls_md_get_size( md_info );
1080 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
1081
Jerry Yu8beb9e12022-03-12 16:23:53 +08001082 if( ( ret = mbedtls_pk_sign_ext( pk_type, own_key,
1083 md_alg, verify_hash, verify_hash_len,
Jerry Yu67eced02022-02-25 13:37:36 +08001084 p + 2, (size_t)( end - ( p + 2 ) ), &signature_len,
1085 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Jerry Yu8511f122022-01-29 10:01:04 +08001086 {
1087 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
1088 return( ret );
1089 }
Jerry Yu8511f122022-01-29 10:01:04 +08001090
Jerry Yu3e536442022-02-15 11:05:59 +08001091 MBEDTLS_PUT_UINT16_BE( signature_len, p, 0 );
1092 p += 2 + signature_len;
Jerry Yu8511f122022-01-29 10:01:04 +08001093
Jerry Yu3e536442022-02-15 11:05:59 +08001094 *out_len = (size_t)( p - buf );
1095
Jerry Yu8511f122022-01-29 10:01:04 +08001096 return( ret );
1097}
Jerry Yu8511f122022-01-29 10:01:04 +08001098
Jerry Yu3e536442022-02-15 11:05:59 +08001099int mbedtls_ssl_tls13_write_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu8511f122022-01-29 10:01:04 +08001100{
1101 int ret = 0;
Jerry Yuca133a32022-02-15 14:22:05 +08001102 unsigned char *buf;
1103 size_t buf_len, msg_len;
1104
Jerry Yu8511f122022-01-29 10:01:04 +08001105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
1106
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001107 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Jerry Yuca133a32022-02-15 14:22:05 +08001108 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001109
Jerry Yuca133a32022-02-15 14:22:05 +08001110 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_verify_body(
1111 ssl, buf, buf + buf_len, &msg_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001112
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001113 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
1114 buf, msg_len );
Jerry Yu8511f122022-01-29 10:01:04 +08001115
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001116 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Jerry Yuca133a32022-02-15 14:22:05 +08001117 ssl, buf_len, msg_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001118
1119cleanup:
1120
1121 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
1122 return( ret );
1123}
1124
Jerry Yu90f152d2022-01-29 22:12:42 +08001125#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1126
Jerry Yu5cc35062022-01-28 16:16:08 +08001127/*
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001128 *
XiaokangQianc5c39d52021-11-09 11:55:10 +00001129 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001130 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001131/*
1132 * Implementation
1133 */
1134
XiaokangQianaaa0e192021-11-10 03:07:04 +00001135static int ssl_tls13_preprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001136{
1137 int ret;
1138
XiaokangQianc5c39d52021-11-09 11:55:10 +00001139 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001140 ssl->handshake->state_local.finished_in.digest,
1141 sizeof( ssl->handshake->state_local.finished_in.digest ),
1142 &ssl->handshake->state_local.finished_in.digest_len,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001143 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
XiaokangQianc13f9352021-11-11 06:13:22 +00001144 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001145 if( ret != 0 )
1146 {
XiaokangQianc5c39d52021-11-09 11:55:10 +00001147 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_calculate_verify_data", ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001148 return( ret );
1149 }
1150
1151 return( 0 );
1152}
1153
XiaokangQianc5c39d52021-11-09 11:55:10 +00001154static int ssl_tls13_parse_finished_message( mbedtls_ssl_context *ssl,
1155 const unsigned char *buf,
1156 const unsigned char *end )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001157{
XiaokangQian33062842021-11-11 03:37:45 +00001158 /*
1159 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +00001160 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +00001161 * } Finished;
1162 */
1163 const unsigned char *expected_verify_data =
1164 ssl->handshake->state_local.finished_in.digest;
1165 size_t expected_verify_data_len =
1166 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001167 /* Structural validation */
XiaokangQian33062842021-11-11 03:37:45 +00001168 if( (size_t)( end - buf ) != expected_verify_data_len )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001169 {
1170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1171
1172 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +00001173 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001174 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
1175 }
1176
XiaokangQianc5c39d52021-11-09 11:55:10 +00001177 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (self-computed):",
XiaokangQian33062842021-11-11 03:37:45 +00001178 expected_verify_data,
1179 expected_verify_data_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001180 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (received message):", buf,
XiaokangQian33062842021-11-11 03:37:45 +00001181 expected_verify_data_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001182
1183 /* Semantic validation */
Gabor Mezei685472b2021-11-24 11:17:36 +01001184 if( mbedtls_ct_memcmp( buf,
1185 expected_verify_data,
1186 expected_verify_data_len ) != 0 )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001187 {
1188 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1189
XiaokangQian33062842021-11-11 03:37:45 +00001190 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +00001191 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001192 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1193 }
1194 return( 0 );
1195}
1196
XiaokangQianc13f9352021-11-11 06:13:22 +00001197#if defined(MBEDTLS_SSL_CLI_C)
XiaokangQianaaa0e192021-11-10 03:07:04 +00001198static int ssl_tls13_postprocess_server_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001199{
XiaokangQian33062842021-11-11 03:37:45 +00001200 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001201 mbedtls_ssl_key_set traffic_keys;
XiaokangQian1aef02e2021-10-28 09:54:34 +00001202 mbedtls_ssl_transform *transform_application = NULL;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001203
XiaokangQian4cab0242021-10-12 08:43:37 +00001204 ret = mbedtls_ssl_tls13_key_schedule_stage_application( ssl );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001205 if( ret != 0 )
1206 {
1207 MBEDTLS_SSL_DEBUG_RET( 1,
XiaokangQian4cab0242021-10-12 08:43:37 +00001208 "mbedtls_ssl_tls13_key_schedule_stage_application", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001209 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001210 }
1211
XiaokangQian33062842021-11-11 03:37:45 +00001212 ret = mbedtls_ssl_tls13_generate_application_keys( ssl, &traffic_keys );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001213 if( ret != 0 )
1214 {
1215 MBEDTLS_SSL_DEBUG_RET( 1,
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001216 "mbedtls_ssl_tls13_generate_application_keys", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001217 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001218 }
1219
1220 transform_application =
1221 mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
1222 if( transform_application == NULL )
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001223 {
1224 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1225 goto cleanup;
1226 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001227
1228 ret = mbedtls_ssl_tls13_populate_transform(
1229 transform_application,
1230 ssl->conf->endpoint,
1231 ssl->session_negotiate->ciphersuite,
1232 &traffic_keys,
1233 ssl );
1234 if( ret != 0 )
1235 {
1236 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001237 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001238 }
1239
1240 ssl->transform_application = transform_application;
1241
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001242cleanup:
1243
XiaokangQian33062842021-11-11 03:37:45 +00001244 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001245 if( ret != 0 )
XiaokangQian1aef02e2021-10-28 09:54:34 +00001246 {
1247 mbedtls_free( transform_application );
1248 MBEDTLS_SSL_PEND_FATAL_ALERT(
1249 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1250 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1251 }
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001252 return( ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001253}
XiaokangQianc13f9352021-11-11 06:13:22 +00001254#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001255
XiaokangQiancc90c942021-11-09 12:30:09 +00001256static int ssl_tls13_postprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001257{
1258
XiaokangQianc13f9352021-11-11 06:13:22 +00001259#if defined(MBEDTLS_SSL_CLI_C)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001260 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
1261 {
XiaokangQianaaa0e192021-11-10 03:07:04 +00001262 return( ssl_tls13_postprocess_server_finished_message( ssl ) );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001263 }
XiaokangQianc13f9352021-11-11 06:13:22 +00001264#else
1265 ((void) ssl);
1266#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001267
1268 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1269}
1270
XiaokangQianc5c39d52021-11-09 11:55:10 +00001271int mbedtls_ssl_tls13_process_finished_message( mbedtls_ssl_context *ssl )
1272{
XiaokangQian33062842021-11-11 03:37:45 +00001273 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001274 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +00001275 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001276
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001277 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001278
1279 /* Preprocessing step: Compute handshake digest */
XiaokangQianaaa0e192021-11-10 03:07:04 +00001280 MBEDTLS_SSL_PROC_CHK( ssl_tls13_preprocess_finished_message( ssl ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001281
Xiaofei Bai746f9482021-11-12 08:53:56 +00001282 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001283 MBEDTLS_SSL_HS_FINISHED,
Xiaofei Baieef15042021-11-18 07:29:56 +00001284 &buf, &buf_len ) );
1285 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_finished_message( ssl, buf, buf + buf_len ) );
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001286 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1287 buf, buf_len );
XiaokangQianaaa0e192021-11-10 03:07:04 +00001288 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_finished_message( ssl ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001289
1290cleanup:
1291
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001292 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001293 return( ret );
1294}
1295
XiaokangQian74af2a82021-09-22 07:40:30 +00001296/*
1297 *
XiaokangQiancc90c942021-11-09 12:30:09 +00001298 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +00001299 *
1300 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001301/*
XiaokangQian35dc6252021-11-11 08:16:19 +00001302 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +00001303 */
1304
XiaokangQian8773aa02021-11-10 07:33:09 +00001305static int ssl_tls13_prepare_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +00001306{
1307 int ret;
1308
1309 /* Compute transcript of handshake up to now. */
XiaokangQiancc90c942021-11-09 12:30:09 +00001310 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQian74af2a82021-09-22 07:40:30 +00001311 ssl->handshake->state_local.finished_out.digest,
1312 sizeof( ssl->handshake->state_local.finished_out.digest ),
1313 &ssl->handshake->state_local.finished_out.digest_len,
1314 ssl->conf->endpoint );
1315
1316 if( ret != 0 )
1317 {
Jerry Yu7ca30542021-12-08 15:57:57 +08001318 MBEDTLS_SSL_DEBUG_RET( 1, "calculate_verify_data failed", ret );
XiaokangQian74af2a82021-09-22 07:40:30 +00001319 return( ret );
1320 }
1321
1322 return( 0 );
1323}
1324
XiaokangQiancc90c942021-11-09 12:30:09 +00001325static int ssl_tls13_finalize_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +00001326{
XiaokangQian0fa66432021-11-15 03:33:57 +00001327 // TODO: Add back resumption keys calculation after MVP.
1328 ((void) ssl);
XiaokangQian74af2a82021-09-22 07:40:30 +00001329
1330 return( 0 );
1331}
1332
XiaokangQian8773aa02021-11-10 07:33:09 +00001333static int ssl_tls13_write_finished_message_body( mbedtls_ssl_context *ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001334 unsigned char *buf,
1335 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001336 size_t *out_len )
XiaokangQian74af2a82021-09-22 07:40:30 +00001337{
XiaokangQian8773aa02021-11-10 07:33:09 +00001338 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001339 /*
1340 * struct {
1341 * opaque verify_data[Hash.length];
1342 * } Finished;
1343 */
XiaokangQian8773aa02021-11-10 07:33:09 +00001344 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001345
1346 memcpy( buf, ssl->handshake->state_local.finished_out.digest,
XiaokangQian8773aa02021-11-10 07:33:09 +00001347 verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001348
Xiaofei Baid25fab62021-12-02 06:36:27 +00001349 *out_len = verify_data_len;
XiaokangQian74af2a82021-09-22 07:40:30 +00001350 return( 0 );
1351}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001352
XiaokangQian35dc6252021-11-11 08:16:19 +00001353/* Main entry point: orchestrates the other functions */
1354int mbedtls_ssl_tls13_write_finished_message( mbedtls_ssl_context *ssl )
1355{
1356 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1357 unsigned char *buf;
1358 size_t buf_len, msg_len;
1359
1360 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished message" ) );
1361
XiaokangQiandce82242021-11-15 06:01:26 +00001362 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_finished_message( ssl ) );
1363
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001364 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001365 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len ) );
1366
1367 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_finished_message_body(
1368 ssl, buf, buf + buf_len, &msg_len ) );
1369
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001370 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1371 buf, msg_len );
XiaokangQian35dc6252021-11-11 08:16:19 +00001372
1373 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_finished_message( ssl ) );
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001374 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
1375 ssl, buf_len, msg_len ) );
XiaokangQian35dc6252021-11-11 08:16:19 +00001376cleanup:
1377
1378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished message" ) );
1379 return( ret );
1380}
1381
Jerry Yu378254d2021-10-30 21:44:47 +08001382void mbedtls_ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
1383{
1384
1385 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
1386
1387 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001388 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001389 */
1390 if( ssl->session )
1391 {
Jerry Yu378254d2021-10-30 21:44:47 +08001392 mbedtls_ssl_session_free( ssl->session );
1393 mbedtls_free( ssl->session );
1394 }
1395 ssl->session = ssl->session_negotiate;
1396 ssl->session_negotiate = NULL;
1397
1398 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
1399}
1400
Ronald Cron49ad6192021-11-24 16:25:31 +01001401/*
1402 *
1403 * STATE HANDLING: Write ChangeCipherSpec
1404 *
1405 */
1406#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Ronald Cron49ad6192021-11-24 16:25:31 +01001407static int ssl_tls13_write_change_cipher_spec_body( mbedtls_ssl_context *ssl,
1408 unsigned char *buf,
1409 unsigned char *end,
1410 size_t *olen )
1411{
1412 ((void) ssl);
1413
1414 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 1 );
1415 buf[0] = 1;
1416 *olen = 1;
1417
1418 return( 0 );
1419}
1420
Ronald Cron49ad6192021-11-24 16:25:31 +01001421int mbedtls_ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1422{
1423 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1424
1425 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
1426
Ronald Cron49ad6192021-11-24 16:25:31 +01001427 /* Write CCS message */
1428 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_change_cipher_spec_body(
1429 ssl, ssl->out_msg,
1430 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1431 &ssl->out_msglen ) );
1432
1433 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1434
Ronald Cron49ad6192021-11-24 16:25:31 +01001435 /* Dispatch message */
Ronald Cron66dbf912022-02-02 15:33:46 +01001436 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_record( ssl, 0 ) );
Ronald Cron49ad6192021-11-24 16:25:31 +01001437
1438cleanup:
1439
1440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
1441 return( ret );
1442}
1443
1444#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1445
XiaokangQian78b1fa72022-01-19 06:56:30 +00001446/* Reset SSL context and update hash for handling HRR.
1447 *
1448 * Replace Transcript-Hash(X) by
1449 * Transcript-Hash( message_hash ||
1450 * 00 00 Hash.length ||
1451 * X )
1452 * A few states of the handshake are preserved, including:
1453 * - session ID
1454 * - session ticket
1455 * - negotiated ciphersuite
1456 */
1457int mbedtls_ssl_reset_transcript_for_hrr( mbedtls_ssl_context *ssl )
1458{
1459 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1460 unsigned char hash_transcript[ MBEDTLS_MD_MAX_SIZE + 4 ];
XiaokangQian0ece9982022-01-24 08:56:23 +00001461 size_t hash_len;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001462 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1463 uint16_t cipher_suite = ssl->session_negotiate->ciphersuite;
1464 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
1465
1466 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Reset SSL session for HRR" ) );
1467
XiaokangQian0ece9982022-01-24 08:56:23 +00001468 ret = mbedtls_ssl_get_handshake_transcript( ssl, ciphersuite_info->mac,
1469 hash_transcript + 4,
1470 MBEDTLS_MD_MAX_SIZE,
1471 &hash_len );
1472 if( ret != 0 )
1473 {
1474 MBEDTLS_SSL_DEBUG_RET( 4, "mbedtls_ssl_get_handshake_transcript", ret );
1475 return( ret );
1476 }
1477
1478 hash_transcript[0] = MBEDTLS_SSL_HS_MESSAGE_HASH;
1479 hash_transcript[1] = 0;
1480 hash_transcript[2] = 0;
1481 hash_transcript[3] = (unsigned char) hash_len;
1482
1483 hash_len += 4;
1484
XiaokangQian78b1fa72022-01-19 06:56:30 +00001485 if( ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
1486 {
1487#if defined(MBEDTLS_SHA256_C)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001488 MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-256 handshake transcript",
XiaokangQian0ece9982022-01-24 08:56:23 +00001489 hash_transcript, hash_len );
XiaokangQian78b1fa72022-01-19 06:56:30 +00001490
1491#if defined(MBEDTLS_USE_PSA_CRYPTO)
1492 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
1493 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
1494#else
1495 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
1496#endif
XiaokangQian78b1fa72022-01-19 06:56:30 +00001497#endif /* MBEDTLS_SHA256_C */
1498 }
1499 else if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1500 {
1501#if defined(MBEDTLS_SHA384_C)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001502 MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-384 handshake transcript",
XiaokangQian0ece9982022-01-24 08:56:23 +00001503 hash_transcript, hash_len );
XiaokangQian78b1fa72022-01-19 06:56:30 +00001504
1505#if defined(MBEDTLS_USE_PSA_CRYPTO)
1506 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
1507 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
1508#else
1509 mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 );
1510#endif
XiaokangQian78b1fa72022-01-19 06:56:30 +00001511#endif /* MBEDTLS_SHA384_C */
1512 }
1513
XiaokangQian0ece9982022-01-24 08:56:23 +00001514#if defined(MBEDTLS_SHA256_C) || defined(MBEDTLS_SHA384_C)
1515 ssl->handshake->update_checksum( ssl, hash_transcript, hash_len );
1516#endif /* MBEDTLS_SHA256_C || MBEDTLS_SHA384_C */
Przemyslaw Stekiel4b3fff42022-02-14 16:39:52 +01001517
XiaokangQian78b1fa72022-01-19 06:56:30 +00001518 return( ret );
1519}
1520
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001521#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001522
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001523int mbedtls_ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
1524 const unsigned char *buf,
1525 size_t buf_len )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001526{
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001527 uint8_t *p = (uint8_t*)buf;
XiaokangQiancfd925f2022-04-14 07:10:37 +00001528 const uint8_t *end = buf + buf_len;
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001529 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001530
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001531 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
XiaokangQiancfd925f2022-04-14 07:10:37 +00001532 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001533 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1534 p += 2;
XiaokangQian3207a322022-02-23 03:15:27 +00001535
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001536 /* Check if key size is consistent with given buffer length. */
XiaokangQiancfd925f2022-04-14 07:10:37 +00001537 MBEDTLS_SSL_CHK_BUF_PTR( p, end, peerkey_len );
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001538
1539 /* Store peer's ECDH public key. */
1540 memcpy( handshake->ecdh_psa_peerkey, p, peerkey_len );
1541 handshake->ecdh_psa_peerkey_len = peerkey_len;
1542
XiaokangQian3207a322022-02-23 03:15:27 +00001543 return( 0 );
1544}
Jerry Yu89e103c2022-03-30 22:43:29 +08001545
1546int mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
1547 mbedtls_ssl_context *ssl,
1548 uint16_t named_group,
1549 unsigned char *buf,
1550 unsigned char *end,
1551 size_t *out_len )
1552{
1553 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1554 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1555 psa_key_attributes_t key_attributes;
1556 size_t own_pubkey_len;
1557 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1558 size_t ecdh_bits = 0;
1559
1560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
1561
1562 /* Convert EC group to PSA key type. */
1563 if( ( handshake->ecdh_psa_type =
1564 mbedtls_psa_parse_tls_ecc_group( named_group, &ecdh_bits ) ) == 0 )
1565 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1566
1567 ssl->handshake->ecdh_bits = ecdh_bits;
1568
1569 key_attributes = psa_key_attributes_init();
1570 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1571 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
1572 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
1573 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
1574
1575 /* Generate ECDH private key. */
1576 status = psa_generate_key( &key_attributes,
1577 &handshake->ecdh_psa_privkey );
1578 if( status != PSA_SUCCESS )
1579 {
1580 ret = psa_ssl_status_to_mbedtls( status );
1581 MBEDTLS_SSL_DEBUG_RET( 1, "psa_generate_key", ret );
1582 return( ret );
1583
1584 }
1585
1586 /* Export the public part of the ECDH private key from PSA. */
1587 status = psa_export_public_key( handshake->ecdh_psa_privkey,
1588 buf, (size_t)( end - buf ),
1589 &own_pubkey_len );
1590 if( status != PSA_SUCCESS )
1591 {
1592 ret = psa_ssl_status_to_mbedtls( status );
1593 MBEDTLS_SSL_DEBUG_RET( 1, "psa_export_public_key", ret );
1594 return( ret );
1595
1596 }
1597
1598 *out_len = own_pubkey_len;
1599
1600 return( 0 );
1601}
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001602#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001603
Jerry Yufb4b6472022-01-27 15:03:26 +08001604#endif /* MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_PROTO_TLS1_3 */