blob: 4332a1d0c0372a24970f3f1362f69e1a1bf367f2 [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 Yu93a13f22022-04-11 23:00:01 +080037const uint8_t mbedtls_ssl_tls13_hello_retry_request_magic[32] =
38 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11, 0xBE,
39 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91, 0xC2, 0xA2,
40 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E, 0x07, 0x9E, 0x09,
41 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
42
Xiaofei Bai746f9482021-11-12 08:53:56 +000043int mbedtls_ssl_tls13_fetch_handshake_msg( mbedtls_ssl_context *ssl,
44 unsigned hs_type,
45 unsigned char **buf,
Xiaofei Baieef15042021-11-18 07:29:56 +000046 size_t *buf_len )
XiaokangQian6b226b02021-09-24 07:51:16 +000047{
48 int ret;
49
50 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
51 {
52 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
53 goto cleanup;
54 }
55
XiaokangQian16c61aa2021-09-27 09:30:17 +000056 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
XiaokangQian6b226b02021-09-24 07:51:16 +000057 ssl->in_msg[0] != hs_type )
58 {
XiaokangQian16c61aa2021-09-27 09:30:17 +000059 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Receive unexpected handshake message." ) );
XiaokangQian6b226b02021-09-24 07:51:16 +000060 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
XiaokangQian05420b12021-09-29 08:46:37 +000061 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
XiaokangQian6b226b02021-09-24 07:51:16 +000062 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
63 goto cleanup;
64 }
65
XiaokangQian05420b12021-09-29 08:46:37 +000066 /*
67 * Jump handshake header (4 bytes, see Section 4 of RFC 8446).
68 * ...
69 * HandshakeType msg_type;
70 * uint24 length;
71 * ...
72 */
Xiaofei Baieef15042021-11-18 07:29:56 +000073 *buf = ssl->in_msg + 4;
74 *buf_len = ssl->in_hslen - 4;
XiaokangQian6b226b02021-09-24 07:51:16 +000075
XiaokangQian6b226b02021-09-24 07:51:16 +000076cleanup:
77
78 return( ret );
79}
80
Jerry Yubc20bdd2021-08-24 15:59:48 +080081#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Bai69fcd392022-01-20 08:25:00 +000082/* mbedtls_ssl_tls13_parse_sig_alg_ext()
83 *
84 * enum {
85 * ....
86 * ecdsa_secp256r1_sha256( 0x0403 ),
87 * ecdsa_secp384r1_sha384( 0x0503 ),
88 * ecdsa_secp521r1_sha512( 0x0603 ),
89 * ....
90 * } SignatureScheme;
91 *
92 * struct {
93 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
94 * } SignatureSchemeList;
95 */
96int mbedtls_ssl_tls13_parse_sig_alg_ext( mbedtls_ssl_context *ssl,
97 const unsigned char *buf,
98 const unsigned char *end )
99{
100 const unsigned char *p = buf;
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000101 size_t supported_sig_algs_len = 0;
102 const unsigned char *supported_sig_algs_end;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +0000103 uint16_t sig_alg;
Xiaofei Baic234ecf2022-02-08 09:59:23 +0000104 uint32_t common_idx = 0;
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000105
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000106 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000107 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 );
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000108 p += 2;
109
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000110 memset( ssl->handshake->received_sig_algs, 0,
Xiaofei Bai51f515a2022-02-08 07:28:04 +0000111 sizeof(ssl->handshake->received_sig_algs) );
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000112
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000113 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len );
114 supported_sig_algs_end = p + supported_sig_algs_len;
Xiaofei Bai51f515a2022-02-08 07:28:04 +0000115 while( p < supported_sig_algs_end )
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000116 {
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000117 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 );
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +0000118 sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 );
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000119 p += 2;
120
121 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x",
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +0000122 sig_alg ) );
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000123
XiaokangQian08037552022-04-20 07:16:41 +0000124 if( ! mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) ||
125 ! mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) )
Xiaofei Bai51f515a2022-02-08 07:28:04 +0000126 continue;
127
128 if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE )
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000129 {
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000130 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
Xiaofei Bai82f0a9a2022-01-26 09:21:54 +0000131 common_idx += 1;
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000132 }
133 }
134 /* Check that we consumed all the message. */
135 if( p != end )
136 {
137 MBEDTLS_SSL_DEBUG_MSG( 1,
138 ( "Signature algorithms extension length misaligned" ) );
139 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
140 MBEDTLS_ERR_SSL_DECODE_ERROR );
141 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
142 }
143
144 if( common_idx == 0 )
145 {
146 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
147 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
148 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
149 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
150 }
151
Xiaofei Baif5b4d252022-01-28 06:37:15 +0000152 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS1_3_SIG_NONE;
Xiaofei Bai69fcd392022-01-20 08:25:00 +0000153 return( 0 );
154}
155
Jerry Yu30b071c2021-09-12 20:16:03 +0800156/*
Jerry Yu30b071c2021-09-12 20:16:03 +0800157 * STATE HANDLING: Read CertificateVerify
158 */
Jerry Yud0fc5852021-10-29 11:09:06 +0800159/* Macro to express the maximum length of the verify structure.
Jerry Yu30b071c2021-09-12 20:16:03 +0800160 *
161 * The structure is computed per TLS 1.3 specification as:
162 * - 64 bytes of octet 32,
163 * - 33 bytes for the context string
164 * (which is either "TLS 1.3, client CertificateVerify"
165 * or "TLS 1.3, server CertificateVerify"),
Jerry Yud0fc5852021-10-29 11:09:06 +0800166 * - 1 byte for the octet 0x0, which serves as a separator,
Jerry Yu30b071c2021-09-12 20:16:03 +0800167 * - 32 or 48 bytes for the Transcript-Hash(Handshake Context, Certificate)
168 * (depending on the size of the transcript_hash)
169 *
170 * This results in a total size of
171 * - 130 bytes for a SHA256-based transcript hash, or
172 * (64 + 33 + 1 + 32 bytes)
173 * - 146 bytes for a SHA384-based transcript hash.
174 * (64 + 33 + 1 + 48 bytes)
175 *
176 */
Jerry Yu26c2d112021-10-25 12:42:58 +0800177#define SSL_VERIFY_STRUCT_MAX_SIZE ( 64 + \
178 33 + \
179 1 + \
180 MBEDTLS_TLS1_3_MD_MAX_SIZE \
Jerry Yu30b071c2021-09-12 20:16:03 +0800181 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800182
Jerry Yu0b32c502021-10-28 13:41:59 +0800183/*
184 * The ssl_tls13_create_verify_structure() creates the verify structure.
185 * As input, it requires the transcript hash.
186 *
187 * The caller has to ensure that the buffer has size at least
188 * SSL_VERIFY_STRUCT_MAX_SIZE bytes.
189 */
Jerry Yud0fc5852021-10-29 11:09:06 +0800190static void ssl_tls13_create_verify_structure( const unsigned char *transcript_hash,
Jerry Yu0b32c502021-10-28 13:41:59 +0800191 size_t transcript_hash_len,
192 unsigned char *verify_buffer,
193 size_t *verify_buffer_len,
194 int from )
195{
196 size_t idx;
Jerry Yu30b071c2021-09-12 20:16:03 +0800197
Jerry Yu0b32c502021-10-28 13:41:59 +0800198 /* RFC 8446, Section 4.4.3:
199 *
200 * The digital signature [in the CertificateVerify message] is then
201 * computed over the concatenation of:
202 * - A string that consists of octet 32 (0x20) repeated 64 times
203 * - The context string
204 * - A single 0 byte which serves as the separator
205 * - The content to be signed
206 */
207 memset( verify_buffer, 0x20, 64 );
208 idx = 64;
209
210 if( from == MBEDTLS_SSL_IS_CLIENT )
211 {
212 memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( client_cv ) );
213 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( client_cv );
214 }
215 else
216 { /* from == MBEDTLS_SSL_IS_SERVER */
217 memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( server_cv ) );
218 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( server_cv );
219 }
220
221 verify_buffer[idx++] = 0x0;
222
223 memcpy( verify_buffer + idx, transcript_hash, transcript_hash_len );
224 idx += transcript_hash_len;
225
226 *verify_buffer_len = idx;
227}
228
Jerry Yu0b32c502021-10-28 13:41:59 +0800229static int ssl_tls13_parse_certificate_verify( mbedtls_ssl_context *ssl,
Jerry Yud0fc5852021-10-29 11:09:06 +0800230 const unsigned char *buf,
231 const unsigned char *end,
232 const unsigned char *verify_buffer,
233 size_t verify_buffer_len )
Jerry Yu30b071c2021-09-12 20:16:03 +0800234{
235 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
236 const unsigned char *p = buf;
237 uint16_t algorithm;
Jerry Yu30b071c2021-09-12 20:16:03 +0800238 size_t signature_len;
239 mbedtls_pk_type_t sig_alg;
240 mbedtls_md_type_t md_alg;
Jerry Yud0fc5852021-10-29 11:09:06 +0800241 unsigned char verify_hash[MBEDTLS_MD_MAX_SIZE];
Jerry Yu30b071c2021-09-12 20:16:03 +0800242 size_t verify_hash_len;
243
Xiaofei Baid25fab62021-12-02 06:36:27 +0000244 void const *options = NULL;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000245#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Xiaofei Baid25fab62021-12-02 06:36:27 +0000246 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000247#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
248
Jerry Yu30b071c2021-09-12 20:16:03 +0800249 /*
250 * struct {
251 * SignatureScheme algorithm;
252 * opaque signature<0..2^16-1>;
253 * } CertificateVerify;
254 */
255 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
256 algorithm = MBEDTLS_GET_UINT16_BE( p, 0 );
257 p += 2;
258
259 /* RFC 8446 section 4.4.3
260 *
261 * If the CertificateVerify message is sent by a server, the signature algorithm
262 * MUST be one offered in the client's "signature_algorithms" extension unless
263 * no valid certificate chain can be produced without unsupported algorithms
264 *
265 * RFC 8446 section 4.4.2.2
266 *
267 * If the client cannot construct an acceptable chain using the provided
268 * certificates and decides to abort the handshake, then it MUST abort the handshake
269 * with an appropriate certificate-related alert (by default, "unsupported_certificate").
270 *
Jerry Yu6f87f252021-10-29 20:12:51 +0800271 * Check if algorithm is an offered signature algorithm.
Jerry Yu30b071c2021-09-12 20:16:03 +0800272 */
Jerry Yu24811fb2022-01-19 18:02:15 +0800273 if( ! mbedtls_ssl_sig_alg_is_offered( ssl, algorithm ) )
Jerry Yu30b071c2021-09-12 20:16:03 +0800274 {
Jerry Yu982d9e52021-10-14 15:59:37 +0800275 /* algorithm not in offered signature algorithms list */
276 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Received signature algorithm(%04x) is not "
277 "offered.",
278 ( unsigned int ) algorithm ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800279 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800280 }
281
Jerry Yu8c338862022-03-23 13:34:04 +0800282 if( mbedtls_ssl_tls13_get_pk_type_and_md_alg_from_sig_alg(
Jerry Yuf8aa9a42022-03-23 20:40:28 +0800283 algorithm, &sig_alg, &md_alg ) != 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800284 {
Jerry Yu8c338862022-03-23 13:34:04 +0800285 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800286 }
287
288 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate Verify: Signature algorithm ( %04x )",
289 ( unsigned int ) algorithm ) );
290
291 /*
292 * Check the certificate's key type matches the signature alg
293 */
294 if( !mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk, sig_alg ) )
295 {
296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "signature algorithm doesn't match cert key" ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800297 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800298 }
299
300 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
301 signature_len = MBEDTLS_GET_UINT16_BE( p, 0 );
302 p += 2;
303 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, signature_len );
304
305 /* Hash verify buffer with indicated hash function */
306 switch( md_alg )
307 {
308#if defined(MBEDTLS_SHA256_C)
309 case MBEDTLS_MD_SHA256:
310 verify_hash_len = 32;
Jerry Yu133690c2021-10-25 14:01:13 +0800311 ret = mbedtls_sha256( verify_buffer, verify_buffer_len, verify_hash, 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800312 break;
313#endif /* MBEDTLS_SHA256_C */
314
315#if defined(MBEDTLS_SHA384_C)
316 case MBEDTLS_MD_SHA384:
317 verify_hash_len = 48;
Jerry Yu133690c2021-10-25 14:01:13 +0800318 ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 1 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800319 break;
320#endif /* MBEDTLS_SHA384_C */
321
322#if defined(MBEDTLS_SHA512_C)
323 case MBEDTLS_MD_SHA512:
324 verify_hash_len = 64;
Jerry Yu133690c2021-10-25 14:01:13 +0800325 ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800326 break;
327#endif /* MBEDTLS_SHA512_C */
328
Jerry Yu0b32c502021-10-28 13:41:59 +0800329 default:
330 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
331 break;
Jerry Yu30b071c2021-09-12 20:16:03 +0800332 }
333
Jerry Yu133690c2021-10-25 14:01:13 +0800334 if( ret != 0 )
335 {
336 MBEDTLS_SSL_DEBUG_RET( 1, "hash computation error", ret );
Jerry Yu6f87f252021-10-29 20:12:51 +0800337 goto error;
Jerry Yu133690c2021-10-25 14:01:13 +0800338 }
339
Jerry Yu30b071c2021-09-12 20:16:03 +0800340 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
XiaokangQian82d34cc2021-11-03 08:51:56 +0000341#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
342 if( sig_alg == MBEDTLS_PK_RSASSA_PSS )
343 {
344 const mbedtls_md_info_t* md_info;
Xiaofei Baid25fab62021-12-02 06:36:27 +0000345 rsassa_pss_options.mgf1_hash_id = md_alg;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000346 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
347 {
348 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
349 }
Xiaofei Baid25fab62021-12-02 06:36:27 +0000350 rsassa_pss_options.expected_salt_len = mbedtls_md_get_size( md_info );
351 options = (const void*) &rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000352 }
353#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800354
Xiaofei Baid25fab62021-12-02 06:36:27 +0000355 if( ( ret = mbedtls_pk_verify_ext( sig_alg, options,
Jerry Yu30b071c2021-09-12 20:16:03 +0800356 &ssl->session_negotiate->peer_cert->pk,
357 md_alg, verify_hash, verify_hash_len,
Jerry Yu6f87f252021-10-29 20:12:51 +0800358 p, signature_len ) ) == 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800359 {
Jerry Yu6f87f252021-10-29 20:12:51 +0800360 return( 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800361 }
Jerry Yu6f87f252021-10-29 20:12:51 +0800362 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify_ext", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800363
Jerry Yu6f87f252021-10-29 20:12:51 +0800364error:
365 /* RFC 8446 section 4.4.3
366 *
367 * If the verification fails, the receiver MUST terminate the handshake
368 * with a "decrypt_error" alert.
369 */
370 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
371 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
372 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
373
Jerry Yu30b071c2021-09-12 20:16:03 +0800374}
375#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
376
377int mbedtls_ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
378{
Jerry Yu30b071c2021-09-12 20:16:03 +0800379
Jerry Yuda8cdf22021-10-25 15:06:49 +0800380#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
381 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
382 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
383 size_t verify_buffer_len;
384 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
385 size_t transcript_len;
386 unsigned char *buf;
387 size_t buf_len;
388
Jerry Yu30b071c2021-09-12 20:16:03 +0800389 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
390
Jerry Yuda8cdf22021-10-25 15:06:49 +0800391 MBEDTLS_SSL_PROC_CHK(
Xiaofei Bai746f9482021-11-12 08:53:56 +0000392 mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
Jerry Yuda8cdf22021-10-25 15:06:49 +0800393 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu30b071c2021-09-12 20:16:03 +0800394
Jerry Yuda8cdf22021-10-25 15:06:49 +0800395 /* Need to calculate the hash of the transcript first
Jerry Yu0b32c502021-10-28 13:41:59 +0800396 * before reading the message since otherwise it gets
397 * included in the transcript
398 */
Jerry Yuda8cdf22021-10-25 15:06:49 +0800399 ret = mbedtls_ssl_get_handshake_transcript( ssl,
400 ssl->handshake->ciphersuite_info->mac,
401 transcript, sizeof( transcript ),
402 &transcript_len );
403 if( ret != 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800404 {
Jerry Yuda8cdf22021-10-25 15:06:49 +0800405 MBEDTLS_SSL_PEND_FATAL_ALERT(
406 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
407 MBEDTLS_ERR_SSL_INTERNAL_ERROR );
408 return( ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800409 }
410
Jerry Yuda8cdf22021-10-25 15:06:49 +0800411 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash", transcript, transcript_len );
412
413 /* Create verify structure */
414 ssl_tls13_create_verify_structure( transcript,
Jerry Yu0b32c502021-10-28 13:41:59 +0800415 transcript_len,
416 verify_buffer,
417 &verify_buffer_len,
418 ( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) ?
419 MBEDTLS_SSL_IS_SERVER :
420 MBEDTLS_SSL_IS_CLIENT );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800421
422 /* Process the message contents */
Jerry Yu0b32c502021-10-28 13:41:59 +0800423 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_verify( ssl, buf,
424 buf + buf_len, verify_buffer, verify_buffer_len ) );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800425
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100426 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
427 buf, buf_len );
Jerry Yu30b071c2021-09-12 20:16:03 +0800428
429cleanup:
430
431 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
Jerry Yu5398c102021-11-05 13:32:38 +0800432 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_process_certificate_verify", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800433 return( ret );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800434#else
435 ((void) ssl);
436 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
437 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
438#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800439}
440
441/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000442 *
443 * STATE HANDLING: Incoming Certificate, client-side only currently.
444 *
445 */
446
447/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000448 * Implementation
449 */
450
Xiaofei Bai947571e2021-09-29 09:12:03 +0000451#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
452#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
453/*
454 * Structure of Certificate message:
455 *
456 * enum {
457 * X509(0),
458 * RawPublicKey(2),
459 * (255)
460 * } CertificateType;
461 *
462 * struct {
463 * select (certificate_type) {
464 * case RawPublicKey:
465 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
466 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
467 * case X509:
468 * opaque cert_data<1..2^24-1>;
469 * };
470 * Extension extensions<0..2^16-1>;
471 * } CertificateEntry;
472 *
473 * struct {
474 * opaque certificate_request_context<0..2^8-1>;
475 * CertificateEntry certificate_list<0..2^24-1>;
476 * } Certificate;
477 *
478 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000479
480/* Parse certificate chain send by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000481static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
482 const unsigned char *buf,
483 const unsigned char *end )
484{
485 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
486 size_t certificate_request_context_len = 0;
487 size_t certificate_list_len = 0;
488 const unsigned char *p = buf;
489 const unsigned char *certificate_list_end;
490
491 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
492 certificate_request_context_len = p[0];
Jerry Yub640bf62021-10-29 10:05:32 +0800493 certificate_list_len = MBEDTLS_GET_UINT24_BE( p, 1 );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000494 p += 4;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000495
496 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
497 * support anything beyond 2^16 = 64K.
498 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000499 if( ( certificate_request_context_len != 0 ) ||
500 ( certificate_list_len >= 0x10000 ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000501 {
502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
503 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
504 MBEDTLS_ERR_SSL_DECODE_ERROR );
505 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
506 }
507
508 /* In case we tried to reuse a session but it failed */
509 if( ssl->session_negotiate->peer_cert != NULL )
510 {
511 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
512 mbedtls_free( ssl->session_negotiate->peer_cert );
513 }
514
515 if( ( ssl->session_negotiate->peer_cert =
516 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
517 {
518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
519 sizeof( mbedtls_x509_crt ) ) );
520 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
521 MBEDTLS_ERR_SSL_ALLOC_FAILED );
522 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
523 }
524
525 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
526
Xiaofei Bai947571e2021-09-29 09:12:03 +0000527 certificate_list_end = p + certificate_list_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000528 while( p < certificate_list_end )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000529 {
530 size_t cert_data_len, extensions_len;
531
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000532 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 3 );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000533 cert_data_len = MBEDTLS_GET_UINT24_BE( p, 0 );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000534 p += 3;
535
536 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
537 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
538 * check that we have a minimum of 128 bytes of data, this is not
539 * clear why we need that though.
540 */
541 if( ( cert_data_len < 128 ) || ( cert_data_len >= 0x10000 ) )
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000542 {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
544 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
545 MBEDTLS_ERR_SSL_DECODE_ERROR );
546 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
547 }
548
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000549 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, cert_data_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000550 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
551 p, cert_data_len );
552
553 switch( ret )
554 {
555 case 0: /*ok*/
556 break;
557 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
558 /* Ignore certificate with an unknown algorithm: maybe a
559 prior certificate was already trusted. */
560 break;
561
562 case MBEDTLS_ERR_X509_ALLOC_FAILED:
563 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
564 MBEDTLS_ERR_X509_ALLOC_FAILED );
565 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
566 return( ret );
567
568 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
569 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
570 MBEDTLS_ERR_X509_UNKNOWN_VERSION );
571 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
572 return( ret );
573
574 default:
575 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
576 ret );
577 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
578 return( ret );
579 }
580
581 p += cert_data_len;
582
583 /* Certificate extensions length */
584 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 2 );
585 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
586 p += 2;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000587 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, extensions_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000588 p += extensions_len;
589 }
590
591 /* Check that all the message is consumed. */
592 if( p != end )
593 {
594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
595 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, \
596 MBEDTLS_ERR_SSL_DECODE_ERROR );
597 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
598 }
599
600 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
601
602 return( ret );
603}
604#else
605static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
606 const unsigned char *buf,
607 const unsigned char *end )
608{
609 ((void) ssl);
610 ((void) buf);
611 ((void) end);
612 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
613}
614#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
615#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
616
617#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
618#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000619/* Validate certificate chain sent by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000620static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
621{
622 int ret = 0;
623 mbedtls_x509_crt *ca_chain;
624 mbedtls_x509_crl *ca_crl;
Xiaofei Baiff456022021-10-28 06:50:17 +0000625 uint32_t verify_result = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000626
627#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
628 if( ssl->handshake->sni_ca_chain != NULL )
629 {
630 ca_chain = ssl->handshake->sni_ca_chain;
631 ca_crl = ssl->handshake->sni_ca_crl;
632 }
633 else
634#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
635 {
636 ca_chain = ssl->conf->ca_chain;
637 ca_crl = ssl->conf->ca_crl;
638 }
639
640 /*
641 * Main check: verify certificate
642 */
643 ret = mbedtls_x509_crt_verify_with_profile(
644 ssl->session_negotiate->peer_cert,
645 ca_chain, ca_crl,
646 ssl->conf->cert_profile,
647 ssl->hostname,
Xiaofei Baiff456022021-10-28 06:50:17 +0000648 &verify_result,
Xiaofei Bai947571e2021-09-29 09:12:03 +0000649 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
650
651 if( ret != 0 )
652 {
653 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
654 }
655
656 /*
657 * Secondary checks: always done, but change 'ret' only if it was 0
658 */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000659 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
660 ssl->handshake->ciphersuite_info,
661 !ssl->conf->endpoint,
Xiaofei Baiff456022021-10-28 06:50:17 +0000662 &verify_result ) != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000663 {
664 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( usage extensions )" ) );
665 if( ret == 0 )
666 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
667 }
668
669
670 if( ca_chain == NULL )
671 {
672 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
673 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
674 }
675
676 if( ret != 0 )
677 {
678 /* The certificate may have been rejected for several reasons.
679 Pick one and send the corresponding alert. Which alert to send
680 may be a subject of debate in some cases. */
Xiaofei Baiff456022021-10-28 06:50:17 +0000681 if( verify_result & MBEDTLS_X509_BADCERT_OTHER )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000682 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000683 else if( verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000684 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000685 else if( verify_result & ( MBEDTLS_X509_BADCERT_KEY_USAGE |
686 MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
687 MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
688 MBEDTLS_X509_BADCERT_BAD_PK |
689 MBEDTLS_X509_BADCERT_BAD_KEY ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000690 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000691 else if( verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000692 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000693 else if( verify_result & MBEDTLS_X509_BADCERT_REVOKED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000694 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000695 else if( verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000696 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret );
697 else
698 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret );
699 }
700
701#if defined(MBEDTLS_DEBUG_C)
Xiaofei Baiff456022021-10-28 06:50:17 +0000702 if( verify_result != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000703 {
704 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
Jerry Yu83bb1312021-10-28 22:16:33 +0800705 (unsigned int) verify_result ) );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000706 }
707 else
708 {
709 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
710 }
711#endif /* MBEDTLS_DEBUG_C */
712
Xiaofei Baiff456022021-10-28 06:50:17 +0000713 ssl->session_negotiate->verify_result = verify_result;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000714 return( ret );
715}
716#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
717static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
718{
719 ((void) ssl);
720 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
721}
722#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
723#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
724
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000725int mbedtls_ssl_tls13_process_certificate( mbedtls_ssl_context *ssl )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000726{
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000727 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
729
730#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
731 unsigned char *buf;
732 size_t buf_len;
733
Xiaofei Bai746f9482021-11-12 08:53:56 +0000734 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000735 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
736 &buf, &buf_len ) );
737
738 /* Parse the certificate chain sent by the peer. */
739 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate( ssl, buf, buf + buf_len ) );
740 /* Validate the certificate chain and set the verification results. */
741 MBEDTLS_SSL_PROC_CHK( ssl_tls13_validate_certificate( ssl ) );
742
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100743 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
744 buf, buf_len );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000745
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000746cleanup:
747
748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Xiaofei Bai10aeec02021-10-26 09:50:08 +0000749#else
750 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
751 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
752#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000753 return( ret );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000754}
Jerry Yu90f152d2022-01-29 22:12:42 +0800755#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7399d0d2022-01-30 17:54:19 +0800756/*
757 * enum {
758 * X509(0),
759 * RawPublicKey(2),
760 * (255)
761 * } CertificateType;
762 *
763 * struct {
764 * select (certificate_type) {
765 * case RawPublicKey:
766 * // From RFC 7250 ASN.1_subjectPublicKeyInfo
767 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
768 *
769 * case X509:
770 * opaque cert_data<1..2^24-1>;
771 * };
772 * Extension extensions<0..2^16-1>;
773 * } CertificateEntry;
774 *
775 * struct {
776 * opaque certificate_request_context<0..2^8-1>;
777 * CertificateEntry certificate_list<0..2^24-1>;
778 * } Certificate;
779 */
780static int ssl_tls13_write_certificate_body( mbedtls_ssl_context *ssl,
Jerry Yu3e536442022-02-15 11:05:59 +0800781 unsigned char *buf,
Jerry Yu7399d0d2022-01-30 17:54:19 +0800782 unsigned char *end,
Jerry Yu3e536442022-02-15 11:05:59 +0800783 size_t *out_len )
Jerry Yu5cc35062022-01-28 16:16:08 +0800784{
Jerry Yu5cc35062022-01-28 16:16:08 +0800785 const mbedtls_x509_crt *crt = mbedtls_ssl_own_cert( ssl );
Jerry Yu3e536442022-02-15 11:05:59 +0800786 unsigned char *p = buf;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800787 unsigned char *certificate_request_context =
788 ssl->handshake->certificate_request_context;
789 unsigned char certificate_request_context_len =
790 ssl->handshake->certificate_request_context_len;
791 unsigned char *p_certificate_list_len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800792
Jerry Yu5cc35062022-01-28 16:16:08 +0800793
Jerry Yu3391ac02022-02-16 11:21:37 +0800794 /* ...
795 * opaque certificate_request_context<0..2^8-1>;
796 * ...
797 */
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800798 MBEDTLS_SSL_CHK_BUF_PTR( p, end, certificate_request_context_len + 1 );
799 *p++ = certificate_request_context_len;
800 if( certificate_request_context_len > 0 )
Jerry Yu537530d2022-02-15 14:00:57 +0800801 {
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800802 memcpy( p, certificate_request_context, certificate_request_context_len );
803 p += certificate_request_context_len;
Jerry Yu537530d2022-02-15 14:00:57 +0800804 }
805
Jerry Yu3391ac02022-02-16 11:21:37 +0800806 /* ...
807 * CertificateEntry certificate_list<0..2^24-1>;
808 * ...
809 */
Jerry Yu3e536442022-02-15 11:05:59 +0800810 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 );
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800811 p_certificate_list_len = p;
Jerry Yu3e536442022-02-15 11:05:59 +0800812 p += 3;
813
Jerry Yu7399d0d2022-01-30 17:54:19 +0800814 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", crt );
Jerry Yu5cc35062022-01-28 16:16:08 +0800815
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800816 while( crt != NULL )
Jerry Yu5cc35062022-01-28 16:16:08 +0800817 {
Jerry Yu7399d0d2022-01-30 17:54:19 +0800818 size_t cert_data_len = crt->raw.len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800819
Jerry Yu7399d0d2022-01-30 17:54:19 +0800820 MBEDTLS_SSL_CHK_BUF_PTR( p, end, cert_data_len + 3 + 2 );
821 MBEDTLS_PUT_UINT24_BE( cert_data_len, p, 0 );
822 p += 3;
Jerry Yu5cc35062022-01-28 16:16:08 +0800823
Jerry Yu7399d0d2022-01-30 17:54:19 +0800824 memcpy( p, crt->raw.p, cert_data_len );
825 p += cert_data_len;
826 crt = crt->next;
Jerry Yu5cc35062022-01-28 16:16:08 +0800827
828 /* Currently, we don't have any certificate extensions defined.
829 * Hence, we are sending an empty extension with length zero.
830 */
Jerry Yu7399d0d2022-01-30 17:54:19 +0800831 MBEDTLS_PUT_UINT24_BE( 0, p, 0 );
832 p += 2;
Jerry Yu5cc35062022-01-28 16:16:08 +0800833 }
Jerry Yu5cc35062022-01-28 16:16:08 +0800834
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800835 MBEDTLS_PUT_UINT24_BE( p - p_certificate_list_len - 3,
836 p_certificate_list_len, 0 );
Jerry Yu7399d0d2022-01-30 17:54:19 +0800837
Jerry Yu3e536442022-02-15 11:05:59 +0800838 *out_len = p - buf;
Jerry Yu5cc35062022-01-28 16:16:08 +0800839
840 return( 0 );
841}
Jerry Yu5cc35062022-01-28 16:16:08 +0800842
Jerry Yu3e536442022-02-15 11:05:59 +0800843int mbedtls_ssl_tls13_write_certificate( mbedtls_ssl_context *ssl )
Jerry Yu5cc35062022-01-28 16:16:08 +0800844{
845 int ret;
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100846 unsigned char *buf;
847 size_t buf_len, msg_len;
848
Jerry Yu5cc35062022-01-28 16:16:08 +0800849 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
850
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100851 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100852 MBEDTLS_SSL_HS_CERTIFICATE, &buf, &buf_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800853
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100854 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_body( ssl,
855 buf,
856 buf + buf_len,
857 &msg_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800858
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100859 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
860 buf, msg_len );
Jerry Yu5cc35062022-01-28 16:16:08 +0800861
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100862 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100863 ssl, buf_len, msg_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800864cleanup:
865
866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
867 return( ret );
868}
869
Jerry Yu3e536442022-02-15 11:05:59 +0800870/*
871 * STATE HANDLING: Output Certificate Verify
872 */
Jerry Yue91a51a2022-03-22 21:42:50 +0800873static int ssl_tls13_get_sig_alg_from_pk( mbedtls_ssl_context *ssl,
874 mbedtls_pk_context *own_key,
Jerry Yu8c338862022-03-23 13:34:04 +0800875 uint16_t *algorithm )
Jerry Yu67eced02022-02-25 13:37:36 +0800876{
877 mbedtls_pk_type_t sig = mbedtls_ssl_sig_from_pk( own_key );
878 /* Determine the size of the key */
879 size_t own_key_size = mbedtls_pk_get_bitlen( own_key );
Jerry Yue91a51a2022-03-22 21:42:50 +0800880 *algorithm = MBEDTLS_TLS1_3_SIG_NONE;
Jerry Yu67eced02022-02-25 13:37:36 +0800881 ((void) own_key_size);
882
883 switch( sig )
884 {
885#if defined(MBEDTLS_ECDSA_C)
886 case MBEDTLS_SSL_SIG_ECDSA:
887 switch( own_key_size )
888 {
889 case 256:
Jerry Yue91a51a2022-03-22 21:42:50 +0800890 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256;
Jerry Yue91a51a2022-03-22 21:42:50 +0800891 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800892 case 384:
Jerry Yue91a51a2022-03-22 21:42:50 +0800893 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384;
Jerry Yue91a51a2022-03-22 21:42:50 +0800894 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800895 case 521:
Jerry Yue91a51a2022-03-22 21:42:50 +0800896 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512;
Jerry Yue91a51a2022-03-22 21:42:50 +0800897 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800898 default:
899 MBEDTLS_SSL_DEBUG_MSG( 3,
900 ( "unknown key size: %"
901 MBEDTLS_PRINTF_SIZET " bits",
902 own_key_size ) );
903 break;
904 }
905 break;
906#endif /* MBEDTLS_ECDSA_C */
907
Jerry Yucef3f332022-03-22 23:00:13 +0800908#if defined(MBEDTLS_RSA_C)
Jerry Yu67eced02022-02-25 13:37:36 +0800909 case MBEDTLS_SSL_SIG_RSA:
Jerry Yucef3f332022-03-22 23:00:13 +0800910#if defined(MBEDTLS_PKCS1_V21)
911#if defined(MBEDTLS_SHA256_C)
Jerry Yu67eced02022-02-25 13:37:36 +0800912 if( own_key_size <= 2048 &&
913 mbedtls_ssl_sig_alg_is_received( ssl,
914 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256 ) )
915 {
Jerry Yue91a51a2022-03-22 21:42:50 +0800916 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256;
Jerry Yue91a51a2022-03-22 21:42:50 +0800917 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800918 }
Jerry Yucef3f332022-03-22 23:00:13 +0800919 else
920#endif /* MBEDTLS_SHA256_C */
921#if defined(MBEDTLS_SHA384_C)
922 if( own_key_size <= 3072 &&
923 mbedtls_ssl_sig_alg_is_received( ssl,
Jerry Yu67eced02022-02-25 13:37:36 +0800924 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384 ) )
925 {
Jerry Yue91a51a2022-03-22 21:42:50 +0800926 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384;
Jerry Yue91a51a2022-03-22 21:42:50 +0800927 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800928 }
Jerry Yucef3f332022-03-22 23:00:13 +0800929 else
930#endif /* MBEDTLS_SHA384_C */
931#if defined(MBEDTLS_SHA512_C)
932 if( own_key_size <= 4096 &&
933 mbedtls_ssl_sig_alg_is_received( ssl,
Jerry Yu67eced02022-02-25 13:37:36 +0800934 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512 ) )
935 {
Jerry Yue91a51a2022-03-22 21:42:50 +0800936 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512;
Jerry Yue91a51a2022-03-22 21:42:50 +0800937 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800938 }
Jerry Yucef3f332022-03-22 23:00:13 +0800939 else
940#endif /* MBEDTLS_SHA512_C */
941#endif /* MBEDTLS_PKCS1_V21 */
942#if defined(MBEDTLS_PKCS1_V15)
943#if defined(MBEDTLS_SHA256_C)
944 if( own_key_size <= 2048 &&
945 mbedtls_ssl_sig_alg_is_received( ssl,
946 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256 ) )
947 {
948 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256;
Jerry Yucef3f332022-03-22 23:00:13 +0800949 return( 0 );
950 }
951 else
952#endif /* MBEDTLS_SHA256_C */
953#if defined(MBEDTLS_SHA384_C)
954 if( own_key_size <= 3072 &&
955 mbedtls_ssl_sig_alg_is_received( ssl,
956 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384 ) )
957 {
958 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384;
Jerry Yucef3f332022-03-22 23:00:13 +0800959 return( 0 );
960 }
961 else
962#endif /* MBEDTLS_SHA384_C */
963#if defined(MBEDTLS_SHA512_C)
964 if( own_key_size <= 4096 &&
965 mbedtls_ssl_sig_alg_is_received( ssl,
966 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512 ) )
967 {
968 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512;
Jerry Yucef3f332022-03-22 23:00:13 +0800969 return( 0 );
970 }
971 else
972#endif /* MBEDTLS_SHA512_C */
973#endif /* MBEDTLS_PKCS1_V15 */
974 {
975 MBEDTLS_SSL_DEBUG_MSG( 3,
976 ( "unknown key size: %"
977 MBEDTLS_PRINTF_SIZET " bits",
978 own_key_size ) );
979 }
Jerry Yu67eced02022-02-25 13:37:36 +0800980 break;
Jerry Yucef3f332022-03-22 23:00:13 +0800981#endif /* MBEDTLS_RSA_C */
Jerry Yu67eced02022-02-25 13:37:36 +0800982 default:
983 MBEDTLS_SSL_DEBUG_MSG( 1,
984 ( "unkown signature type : %u", sig ) );
985 break;
986 }
Jerry Yue91a51a2022-03-22 21:42:50 +0800987 return( -1 );
Jerry Yu67eced02022-02-25 13:37:36 +0800988}
989
Jerry Yu3e536442022-02-15 11:05:59 +0800990static int ssl_tls13_write_certificate_verify_body( mbedtls_ssl_context *ssl,
991 unsigned char *buf,
992 unsigned char *end,
993 size_t *out_len )
Jerry Yu8511f122022-01-29 10:01:04 +0800994{
995 int ret;
Jerry Yu3e536442022-02-15 11:05:59 +0800996 unsigned char *p = buf;
Jerry Yu8511f122022-01-29 10:01:04 +0800997 mbedtls_pk_context *own_key;
Jerry Yu3e536442022-02-15 11:05:59 +0800998
Jerry Yu8511f122022-01-29 10:01:04 +0800999 unsigned char handshake_hash[ MBEDTLS_TLS1_3_MD_MAX_SIZE ];
1000 size_t handshake_hash_len;
Jerry Yu3e536442022-02-15 11:05:59 +08001001 unsigned char verify_buffer[ SSL_VERIFY_STRUCT_MAX_SIZE ];
1002 size_t verify_buffer_len;
Jerry Yu67eced02022-02-25 13:37:36 +08001003 mbedtls_pk_type_t pk_type = MBEDTLS_PK_NONE;
Jerry Yu67eced02022-02-25 13:37:36 +08001004 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Jerry Yu78272072022-02-22 10:28:13 +08001005 uint16_t algorithm = MBEDTLS_TLS1_3_SIG_NONE;
Jerry Yu3e536442022-02-15 11:05:59 +08001006 size_t signature_len = 0;
Jerry Yu8511f122022-01-29 10:01:04 +08001007 const mbedtls_md_info_t *md_info;
Jerry Yu3391ac02022-02-16 11:21:37 +08001008 unsigned char verify_hash[ MBEDTLS_MD_MAX_SIZE ];
Jerry Yu3e536442022-02-15 11:05:59 +08001009 size_t verify_hash_len;
Jerry Yu8511f122022-01-29 10:01:04 +08001010
Jerry Yu0b7b1012022-02-23 12:23:05 +08001011 *out_len = 0;
1012
Jerry Yu3e536442022-02-15 11:05:59 +08001013 own_key = mbedtls_ssl_own_key( ssl );
1014 if( own_key == NULL )
Jerry Yu8511f122022-01-29 10:01:04 +08001015 {
Jerry Yu3e536442022-02-15 11:05:59 +08001016 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1017 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu8511f122022-01-29 10:01:04 +08001018 }
1019
Jerry Yu8511f122022-01-29 10:01:04 +08001020 ret = mbedtls_ssl_get_handshake_transcript( ssl,
1021 ssl->handshake->ciphersuite_info->mac,
1022 handshake_hash,
1023 sizeof( handshake_hash ),
1024 &handshake_hash_len );
1025 if( ret != 0 )
1026 return( ret );
1027
1028 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash",
1029 handshake_hash,
1030 handshake_hash_len);
1031
Jerry Yu8511f122022-01-29 10:01:04 +08001032 ssl_tls13_create_verify_structure( handshake_hash, handshake_hash_len,
1033 verify_buffer, &verify_buffer_len,
1034 ssl->conf->endpoint );
1035
1036 /*
1037 * struct {
1038 * SignatureScheme algorithm;
1039 * opaque signature<0..2^16-1>;
1040 * } CertificateVerify;
1041 */
Jerry Yu8c338862022-03-23 13:34:04 +08001042 ret = ssl_tls13_get_sig_alg_from_pk( ssl, own_key, &algorithm );
Jerry Yue91a51a2022-03-22 21:42:50 +08001043 if( ret != 0 || ! mbedtls_ssl_sig_alg_is_received( ssl, algorithm ) )
Jerry Yu8511f122022-01-29 10:01:04 +08001044 {
Jerry Yud66409a2022-02-18 16:42:24 +08001045 MBEDTLS_SSL_DEBUG_MSG( 1,
Jerry Yu2124d052022-02-18 21:07:18 +08001046 ( "signature algorithm not in received or offered list." ) );
Jerry Yu67eced02022-02-25 13:37:36 +08001047
1048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Signature algorithm is %s",
1049 mbedtls_ssl_sig_alg_to_str( algorithm ) ) );
1050
Jerry Yu71f36f12022-02-23 17:34:29 +08001051 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
Jerry Yud66409a2022-02-18 16:42:24 +08001052 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu3391ac02022-02-16 11:21:37 +08001053 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu8511f122022-01-29 10:01:04 +08001054 }
1055
Jerry Yu6c6f1022022-03-25 11:09:50 +08001056 if( mbedtls_ssl_tls13_get_pk_type_and_md_alg_from_sig_alg(
1057 algorithm, &pk_type, &md_alg ) != 0 )
Jerry Yu8c338862022-03-23 13:34:04 +08001058 {
Jerry Yuf8aa9a42022-03-23 20:40:28 +08001059 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu8c338862022-03-23 13:34:04 +08001060 }
1061
Jerry Yu3391ac02022-02-16 11:21:37 +08001062 /* Check there is space for the algorithm identifier (2 bytes) and the
1063 * signature length (2 bytes).
1064 */
1065 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Jerry Yu3e536442022-02-15 11:05:59 +08001066 MBEDTLS_PUT_UINT16_BE( algorithm, p, 0 );
1067 p += 2;
Jerry Yu8511f122022-01-29 10:01:04 +08001068
1069 /* Hash verify buffer with indicated hash function */
1070 md_info = mbedtls_md_info_from_type( md_alg );
1071 if( md_info == NULL )
1072 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1073
1074 ret = mbedtls_md( md_info, verify_buffer, verify_buffer_len, verify_hash );
1075 if( ret != 0 )
1076 return( ret );
1077
1078 verify_hash_len = mbedtls_md_get_size( md_info );
1079 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
1080
Jerry Yu8beb9e12022-03-12 16:23:53 +08001081 if( ( ret = mbedtls_pk_sign_ext( pk_type, own_key,
1082 md_alg, verify_hash, verify_hash_len,
Jerry Yu67eced02022-02-25 13:37:36 +08001083 p + 2, (size_t)( end - ( p + 2 ) ), &signature_len,
1084 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Jerry Yu8511f122022-01-29 10:01:04 +08001085 {
1086 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
1087 return( ret );
1088 }
Jerry Yu8511f122022-01-29 10:01:04 +08001089
Jerry Yu3e536442022-02-15 11:05:59 +08001090 MBEDTLS_PUT_UINT16_BE( signature_len, p, 0 );
1091 p += 2 + signature_len;
Jerry Yu8511f122022-01-29 10:01:04 +08001092
Jerry Yu3e536442022-02-15 11:05:59 +08001093 *out_len = (size_t)( p - buf );
1094
Jerry Yu8511f122022-01-29 10:01:04 +08001095 return( ret );
1096}
Jerry Yu8511f122022-01-29 10:01:04 +08001097
Jerry Yu3e536442022-02-15 11:05:59 +08001098int mbedtls_ssl_tls13_write_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu8511f122022-01-29 10:01:04 +08001099{
1100 int ret = 0;
Jerry Yuca133a32022-02-15 14:22:05 +08001101 unsigned char *buf;
1102 size_t buf_len, msg_len;
1103
Jerry Yu8511f122022-01-29 10:01:04 +08001104 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
1105
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001106 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Jerry Yuca133a32022-02-15 14:22:05 +08001107 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001108
Jerry Yuca133a32022-02-15 14:22:05 +08001109 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_verify_body(
1110 ssl, buf, buf + buf_len, &msg_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001111
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001112 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
1113 buf, msg_len );
Jerry Yu8511f122022-01-29 10:01:04 +08001114
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001115 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Jerry Yuca133a32022-02-15 14:22:05 +08001116 ssl, buf_len, msg_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001117
1118cleanup:
1119
1120 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
1121 return( ret );
1122}
1123
Jerry Yu90f152d2022-01-29 22:12:42 +08001124#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1125
Jerry Yu5cc35062022-01-28 16:16:08 +08001126/*
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001127 *
XiaokangQianc5c39d52021-11-09 11:55:10 +00001128 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001129 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001130/*
1131 * Implementation
1132 */
1133
XiaokangQianaaa0e192021-11-10 03:07:04 +00001134static int ssl_tls13_preprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001135{
1136 int ret;
1137
XiaokangQianc5c39d52021-11-09 11:55:10 +00001138 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001139 ssl->handshake->state_local.finished_in.digest,
1140 sizeof( ssl->handshake->state_local.finished_in.digest ),
1141 &ssl->handshake->state_local.finished_in.digest_len,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001142 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
XiaokangQianc13f9352021-11-11 06:13:22 +00001143 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001144 if( ret != 0 )
1145 {
XiaokangQianc5c39d52021-11-09 11:55:10 +00001146 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_calculate_verify_data", ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001147 return( ret );
1148 }
1149
1150 return( 0 );
1151}
1152
XiaokangQianc5c39d52021-11-09 11:55:10 +00001153static int ssl_tls13_parse_finished_message( mbedtls_ssl_context *ssl,
1154 const unsigned char *buf,
1155 const unsigned char *end )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001156{
XiaokangQian33062842021-11-11 03:37:45 +00001157 /*
1158 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +00001159 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +00001160 * } Finished;
1161 */
1162 const unsigned char *expected_verify_data =
1163 ssl->handshake->state_local.finished_in.digest;
1164 size_t expected_verify_data_len =
1165 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001166 /* Structural validation */
XiaokangQian33062842021-11-11 03:37:45 +00001167 if( (size_t)( end - buf ) != expected_verify_data_len )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001168 {
1169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1170
1171 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +00001172 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001173 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
1174 }
1175
XiaokangQianc5c39d52021-11-09 11:55:10 +00001176 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (self-computed):",
XiaokangQian33062842021-11-11 03:37:45 +00001177 expected_verify_data,
1178 expected_verify_data_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001179 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (received message):", buf,
XiaokangQian33062842021-11-11 03:37:45 +00001180 expected_verify_data_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001181
1182 /* Semantic validation */
Gabor Mezei685472b2021-11-24 11:17:36 +01001183 if( mbedtls_ct_memcmp( buf,
1184 expected_verify_data,
1185 expected_verify_data_len ) != 0 )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001186 {
1187 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1188
XiaokangQian33062842021-11-11 03:37:45 +00001189 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +00001190 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001191 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1192 }
1193 return( 0 );
1194}
1195
XiaokangQianc13f9352021-11-11 06:13:22 +00001196#if defined(MBEDTLS_SSL_CLI_C)
XiaokangQianaaa0e192021-11-10 03:07:04 +00001197static int ssl_tls13_postprocess_server_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001198{
XiaokangQian33062842021-11-11 03:37:45 +00001199 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001200 mbedtls_ssl_key_set traffic_keys;
XiaokangQian1aef02e2021-10-28 09:54:34 +00001201 mbedtls_ssl_transform *transform_application = NULL;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001202
XiaokangQian4cab0242021-10-12 08:43:37 +00001203 ret = mbedtls_ssl_tls13_key_schedule_stage_application( ssl );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001204 if( ret != 0 )
1205 {
1206 MBEDTLS_SSL_DEBUG_RET( 1,
XiaokangQian4cab0242021-10-12 08:43:37 +00001207 "mbedtls_ssl_tls13_key_schedule_stage_application", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001208 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001209 }
1210
XiaokangQian33062842021-11-11 03:37:45 +00001211 ret = mbedtls_ssl_tls13_generate_application_keys( ssl, &traffic_keys );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001212 if( ret != 0 )
1213 {
1214 MBEDTLS_SSL_DEBUG_RET( 1,
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001215 "mbedtls_ssl_tls13_generate_application_keys", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001216 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001217 }
1218
1219 transform_application =
1220 mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
1221 if( transform_application == NULL )
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001222 {
1223 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1224 goto cleanup;
1225 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001226
1227 ret = mbedtls_ssl_tls13_populate_transform(
1228 transform_application,
1229 ssl->conf->endpoint,
1230 ssl->session_negotiate->ciphersuite,
1231 &traffic_keys,
1232 ssl );
1233 if( ret != 0 )
1234 {
1235 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001236 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001237 }
1238
1239 ssl->transform_application = transform_application;
1240
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001241cleanup:
1242
XiaokangQian33062842021-11-11 03:37:45 +00001243 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001244 if( ret != 0 )
XiaokangQian1aef02e2021-10-28 09:54:34 +00001245 {
1246 mbedtls_free( transform_application );
1247 MBEDTLS_SSL_PEND_FATAL_ALERT(
1248 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1249 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1250 }
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001251 return( ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001252}
XiaokangQianc13f9352021-11-11 06:13:22 +00001253#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001254
XiaokangQiancc90c942021-11-09 12:30:09 +00001255static int ssl_tls13_postprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001256{
1257
XiaokangQianc13f9352021-11-11 06:13:22 +00001258#if defined(MBEDTLS_SSL_CLI_C)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001259 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
1260 {
XiaokangQianaaa0e192021-11-10 03:07:04 +00001261 return( ssl_tls13_postprocess_server_finished_message( ssl ) );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001262 }
XiaokangQianc13f9352021-11-11 06:13:22 +00001263#else
1264 ((void) ssl);
1265#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001266
1267 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1268}
1269
XiaokangQianc5c39d52021-11-09 11:55:10 +00001270int mbedtls_ssl_tls13_process_finished_message( mbedtls_ssl_context *ssl )
1271{
XiaokangQian33062842021-11-11 03:37:45 +00001272 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001273 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +00001274 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001275
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001276 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001277
1278 /* Preprocessing step: Compute handshake digest */
XiaokangQianaaa0e192021-11-10 03:07:04 +00001279 MBEDTLS_SSL_PROC_CHK( ssl_tls13_preprocess_finished_message( ssl ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001280
Xiaofei Bai746f9482021-11-12 08:53:56 +00001281 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001282 MBEDTLS_SSL_HS_FINISHED,
Xiaofei Baieef15042021-11-18 07:29:56 +00001283 &buf, &buf_len ) );
1284 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_finished_message( ssl, buf, buf + buf_len ) );
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001285 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1286 buf, buf_len );
XiaokangQianaaa0e192021-11-10 03:07:04 +00001287 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_finished_message( ssl ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001288
1289cleanup:
1290
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001291 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001292 return( ret );
1293}
1294
XiaokangQian74af2a82021-09-22 07:40:30 +00001295/*
1296 *
XiaokangQiancc90c942021-11-09 12:30:09 +00001297 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +00001298 *
1299 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001300/*
XiaokangQian35dc6252021-11-11 08:16:19 +00001301 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +00001302 */
1303
XiaokangQian8773aa02021-11-10 07:33:09 +00001304static int ssl_tls13_prepare_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +00001305{
1306 int ret;
1307
1308 /* Compute transcript of handshake up to now. */
XiaokangQiancc90c942021-11-09 12:30:09 +00001309 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQian74af2a82021-09-22 07:40:30 +00001310 ssl->handshake->state_local.finished_out.digest,
1311 sizeof( ssl->handshake->state_local.finished_out.digest ),
1312 &ssl->handshake->state_local.finished_out.digest_len,
1313 ssl->conf->endpoint );
1314
1315 if( ret != 0 )
1316 {
Jerry Yu7ca30542021-12-08 15:57:57 +08001317 MBEDTLS_SSL_DEBUG_RET( 1, "calculate_verify_data failed", ret );
XiaokangQian74af2a82021-09-22 07:40:30 +00001318 return( ret );
1319 }
1320
1321 return( 0 );
1322}
1323
XiaokangQiancc90c942021-11-09 12:30:09 +00001324static int ssl_tls13_finalize_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +00001325{
XiaokangQian0fa66432021-11-15 03:33:57 +00001326 // TODO: Add back resumption keys calculation after MVP.
1327 ((void) ssl);
XiaokangQian74af2a82021-09-22 07:40:30 +00001328
1329 return( 0 );
1330}
1331
XiaokangQian8773aa02021-11-10 07:33:09 +00001332static int ssl_tls13_write_finished_message_body( mbedtls_ssl_context *ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001333 unsigned char *buf,
1334 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001335 size_t *out_len )
XiaokangQian74af2a82021-09-22 07:40:30 +00001336{
XiaokangQian8773aa02021-11-10 07:33:09 +00001337 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001338 /*
1339 * struct {
1340 * opaque verify_data[Hash.length];
1341 * } Finished;
1342 */
XiaokangQian8773aa02021-11-10 07:33:09 +00001343 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001344
1345 memcpy( buf, ssl->handshake->state_local.finished_out.digest,
XiaokangQian8773aa02021-11-10 07:33:09 +00001346 verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001347
Xiaofei Baid25fab62021-12-02 06:36:27 +00001348 *out_len = verify_data_len;
XiaokangQian74af2a82021-09-22 07:40:30 +00001349 return( 0 );
1350}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001351
XiaokangQian35dc6252021-11-11 08:16:19 +00001352/* Main entry point: orchestrates the other functions */
1353int mbedtls_ssl_tls13_write_finished_message( mbedtls_ssl_context *ssl )
1354{
1355 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1356 unsigned char *buf;
1357 size_t buf_len, msg_len;
1358
1359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished message" ) );
1360
XiaokangQiandce82242021-11-15 06:01:26 +00001361 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_finished_message( ssl ) );
1362
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001363 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001364 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len ) );
1365
1366 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_finished_message_body(
1367 ssl, buf, buf + buf_len, &msg_len ) );
1368
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001369 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1370 buf, msg_len );
XiaokangQian35dc6252021-11-11 08:16:19 +00001371
1372 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_finished_message( ssl ) );
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001373 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
1374 ssl, buf_len, msg_len ) );
XiaokangQian35dc6252021-11-11 08:16:19 +00001375cleanup:
1376
1377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished message" ) );
1378 return( ret );
1379}
1380
Jerry Yu378254d2021-10-30 21:44:47 +08001381void mbedtls_ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
1382{
1383
1384 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
1385
1386 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001387 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001388 */
1389 if( ssl->session )
1390 {
Jerry Yu378254d2021-10-30 21:44:47 +08001391 mbedtls_ssl_session_free( ssl->session );
1392 mbedtls_free( ssl->session );
1393 }
1394 ssl->session = ssl->session_negotiate;
1395 ssl->session_negotiate = NULL;
1396
1397 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
1398}
1399
Ronald Cron49ad6192021-11-24 16:25:31 +01001400/*
1401 *
1402 * STATE HANDLING: Write ChangeCipherSpec
1403 *
1404 */
1405#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Ronald Cron49ad6192021-11-24 16:25:31 +01001406static int ssl_tls13_write_change_cipher_spec_body( mbedtls_ssl_context *ssl,
1407 unsigned char *buf,
1408 unsigned char *end,
1409 size_t *olen )
1410{
1411 ((void) ssl);
1412
1413 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 1 );
1414 buf[0] = 1;
1415 *olen = 1;
1416
1417 return( 0 );
1418}
1419
Ronald Cron49ad6192021-11-24 16:25:31 +01001420int mbedtls_ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1421{
1422 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1423
1424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
1425
Ronald Cron49ad6192021-11-24 16:25:31 +01001426 /* Write CCS message */
1427 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_change_cipher_spec_body(
1428 ssl, ssl->out_msg,
1429 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1430 &ssl->out_msglen ) );
1431
1432 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1433
Ronald Cron49ad6192021-11-24 16:25:31 +01001434 /* Dispatch message */
Ronald Cron66dbf912022-02-02 15:33:46 +01001435 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_record( ssl, 0 ) );
Ronald Cron49ad6192021-11-24 16:25:31 +01001436
1437cleanup:
1438
1439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
1440 return( ret );
1441}
1442
1443#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1444
XiaokangQian78b1fa72022-01-19 06:56:30 +00001445/* Reset SSL context and update hash for handling HRR.
1446 *
1447 * Replace Transcript-Hash(X) by
1448 * Transcript-Hash( message_hash ||
1449 * 00 00 Hash.length ||
1450 * X )
1451 * A few states of the handshake are preserved, including:
1452 * - session ID
1453 * - session ticket
1454 * - negotiated ciphersuite
1455 */
1456int mbedtls_ssl_reset_transcript_for_hrr( mbedtls_ssl_context *ssl )
1457{
1458 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1459 unsigned char hash_transcript[ MBEDTLS_MD_MAX_SIZE + 4 ];
XiaokangQian0ece9982022-01-24 08:56:23 +00001460 size_t hash_len;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001461 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1462 uint16_t cipher_suite = ssl->session_negotiate->ciphersuite;
1463 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
1464
1465 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Reset SSL session for HRR" ) );
1466
XiaokangQian0ece9982022-01-24 08:56:23 +00001467 ret = mbedtls_ssl_get_handshake_transcript( ssl, ciphersuite_info->mac,
1468 hash_transcript + 4,
1469 MBEDTLS_MD_MAX_SIZE,
1470 &hash_len );
1471 if( ret != 0 )
1472 {
1473 MBEDTLS_SSL_DEBUG_RET( 4, "mbedtls_ssl_get_handshake_transcript", ret );
1474 return( ret );
1475 }
1476
1477 hash_transcript[0] = MBEDTLS_SSL_HS_MESSAGE_HASH;
1478 hash_transcript[1] = 0;
1479 hash_transcript[2] = 0;
1480 hash_transcript[3] = (unsigned char) hash_len;
1481
1482 hash_len += 4;
1483
XiaokangQian78b1fa72022-01-19 06:56:30 +00001484 if( ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
1485 {
1486#if defined(MBEDTLS_SHA256_C)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001487 MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-256 handshake transcript",
XiaokangQian0ece9982022-01-24 08:56:23 +00001488 hash_transcript, hash_len );
XiaokangQian78b1fa72022-01-19 06:56:30 +00001489
1490#if defined(MBEDTLS_USE_PSA_CRYPTO)
1491 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
1492 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
1493#else
1494 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
1495#endif
XiaokangQian78b1fa72022-01-19 06:56:30 +00001496#endif /* MBEDTLS_SHA256_C */
1497 }
1498 else if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1499 {
1500#if defined(MBEDTLS_SHA384_C)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001501 MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-384 handshake transcript",
XiaokangQian0ece9982022-01-24 08:56:23 +00001502 hash_transcript, hash_len );
XiaokangQian78b1fa72022-01-19 06:56:30 +00001503
1504#if defined(MBEDTLS_USE_PSA_CRYPTO)
1505 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
1506 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
1507#else
1508 mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 );
1509#endif
XiaokangQian78b1fa72022-01-19 06:56:30 +00001510#endif /* MBEDTLS_SHA384_C */
1511 }
1512
XiaokangQian0ece9982022-01-24 08:56:23 +00001513#if defined(MBEDTLS_SHA256_C) || defined(MBEDTLS_SHA384_C)
1514 ssl->handshake->update_checksum( ssl, hash_transcript, hash_len );
1515#endif /* MBEDTLS_SHA256_C || MBEDTLS_SHA384_C */
Przemyslaw Stekiel4b3fff42022-02-14 16:39:52 +01001516
XiaokangQian78b1fa72022-01-19 06:56:30 +00001517 return( ret );
1518}
1519
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001520#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001521
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001522int mbedtls_ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
1523 const unsigned char *buf,
1524 size_t buf_len )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001525{
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001526 uint8_t *p = (uint8_t*)buf;
XiaokangQiancfd925f2022-04-14 07:10:37 +00001527 const uint8_t *end = buf + buf_len;
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001528 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001529
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001530 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
XiaokangQiancfd925f2022-04-14 07:10:37 +00001531 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001532 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1533 p += 2;
XiaokangQian3207a322022-02-23 03:15:27 +00001534
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001535 /* Check if key size is consistent with given buffer length. */
XiaokangQiancfd925f2022-04-14 07:10:37 +00001536 MBEDTLS_SSL_CHK_BUF_PTR( p, end, peerkey_len );
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001537
1538 /* Store peer's ECDH public key. */
1539 memcpy( handshake->ecdh_psa_peerkey, p, peerkey_len );
1540 handshake->ecdh_psa_peerkey_len = peerkey_len;
1541
XiaokangQian3207a322022-02-23 03:15:27 +00001542 return( 0 );
1543}
Jerry Yu89e103c2022-03-30 22:43:29 +08001544
1545int mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
1546 mbedtls_ssl_context *ssl,
1547 uint16_t named_group,
1548 unsigned char *buf,
1549 unsigned char *end,
1550 size_t *out_len )
1551{
1552 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1553 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1554 psa_key_attributes_t key_attributes;
1555 size_t own_pubkey_len;
1556 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1557 size_t ecdh_bits = 0;
1558
1559 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
1560
1561 /* Convert EC group to PSA key type. */
1562 if( ( handshake->ecdh_psa_type =
1563 mbedtls_psa_parse_tls_ecc_group( named_group, &ecdh_bits ) ) == 0 )
1564 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1565
1566 ssl->handshake->ecdh_bits = ecdh_bits;
1567
1568 key_attributes = psa_key_attributes_init();
1569 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1570 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
1571 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
1572 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
1573
1574 /* Generate ECDH private key. */
1575 status = psa_generate_key( &key_attributes,
1576 &handshake->ecdh_psa_privkey );
1577 if( status != PSA_SUCCESS )
1578 {
1579 ret = psa_ssl_status_to_mbedtls( status );
1580 MBEDTLS_SSL_DEBUG_RET( 1, "psa_generate_key", ret );
1581 return( ret );
1582
1583 }
1584
1585 /* Export the public part of the ECDH private key from PSA. */
1586 status = psa_export_public_key( handshake->ecdh_psa_privkey,
1587 buf, (size_t)( end - buf ),
1588 &own_pubkey_len );
1589 if( status != PSA_SUCCESS )
1590 {
1591 ret = psa_ssl_status_to_mbedtls( status );
1592 MBEDTLS_SSL_DEBUG_RET( 1, "psa_export_public_key", ret );
1593 return( ret );
1594
1595 }
1596
1597 *out_len = own_pubkey_len;
1598
1599 return( 0 );
1600}
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001601#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001602
Jerry Yufb4b6472022-01-27 15:03:26 +08001603#endif /* MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_PROTO_TLS1_3 */