blob: 90c20bde92d6f7415e6624068e978b5e57bf7d19 [file] [log] [blame]
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001/*
2 * TLS 1.3 functionality shared between client and server
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#include "common.h"
21
22#if defined(MBEDTLS_SSL_TLS_C)
23
Ronald Cron6f135e12021-12-08 16:57:54 +010024#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu65dd2cc2021-08-18 16:38:40 +080025
Jerry Yu30b071c2021-09-12 20:16:03 +080026#include <string.h>
27
Jerry Yuc8a392c2021-08-18 16:46:28 +080028#include "mbedtls/error.h"
Jerry Yu75336352021-09-01 15:59:36 +080029#include "mbedtls/debug.h"
Jerry Yu30b071c2021-09-12 20:16:03 +080030#include "mbedtls/oid.h"
31#include "mbedtls/platform.h"
Gabor Mezei685472b2021-11-24 11:17:36 +010032#include "mbedtls/constant_time.h"
XiaokangQian74af2a82021-09-22 07:40:30 +000033#include <string.h>
Jerry Yuc8a392c2021-08-18 16:46:28 +080034
Jerry Yu65dd2cc2021-08-18 16:38:40 +080035#include "ssl_misc.h"
Jerry Yu30b071c2021-09-12 20:16:03 +080036#include "ssl_tls13_keys.h"
Jerry Yu65dd2cc2021-08-18 16:38:40 +080037
Xiaofei Bai746f9482021-11-12 08:53:56 +000038int mbedtls_ssl_tls13_fetch_handshake_msg( mbedtls_ssl_context *ssl,
39 unsigned hs_type,
40 unsigned char **buf,
Xiaofei Baieef15042021-11-18 07:29:56 +000041 size_t *buf_len )
XiaokangQian6b226b02021-09-24 07:51:16 +000042{
43 int ret;
44
45 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
46 {
47 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
48 goto cleanup;
49 }
50
XiaokangQian16c61aa2021-09-27 09:30:17 +000051 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
XiaokangQian6b226b02021-09-24 07:51:16 +000052 ssl->in_msg[0] != hs_type )
53 {
XiaokangQian16c61aa2021-09-27 09:30:17 +000054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Receive unexpected handshake message." ) );
XiaokangQian6b226b02021-09-24 07:51:16 +000055 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
XiaokangQian05420b12021-09-29 08:46:37 +000056 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
XiaokangQian6b226b02021-09-24 07:51:16 +000057 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
58 goto cleanup;
59 }
60
XiaokangQian05420b12021-09-29 08:46:37 +000061 /*
62 * Jump handshake header (4 bytes, see Section 4 of RFC 8446).
63 * ...
64 * HandshakeType msg_type;
65 * uint24 length;
66 * ...
67 */
Xiaofei Baieef15042021-11-18 07:29:56 +000068 *buf = ssl->in_msg + 4;
69 *buf_len = ssl->in_hslen - 4;
XiaokangQian6b226b02021-09-24 07:51:16 +000070
XiaokangQian6b226b02021-09-24 07:51:16 +000071cleanup:
72
73 return( ret );
74}
75
Jerry Yuf4436812021-08-26 22:59:56 +080076int mbedtls_ssl_tls13_start_handshake_msg( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +080077 unsigned hs_type,
78 unsigned char **buf,
Jerry Yu0c63af62021-09-02 12:59:12 +080079 size_t *buf_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +080080{
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080081 /*
82 * Reserve 4 bytes for hanshake header. ( Section 4,RFC 8446 )
83 * ...
84 * HandshakeType msg_type;
85 * uint24 length;
86 * ...
87 */
Jerry Yuc8a392c2021-08-18 16:46:28 +080088 *buf = ssl->out_msg + 4;
Jerry Yu0c63af62021-09-02 12:59:12 +080089 *buf_len = MBEDTLS_SSL_OUT_CONTENT_LEN - 4;
Jerry Yuc8a392c2021-08-18 16:46:28 +080090
91 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
92 ssl->out_msg[0] = hs_type;
93
94 return( 0 );
Jerry Yu65dd2cc2021-08-18 16:38:40 +080095}
96
Jerry Yuf4436812021-08-26 22:59:56 +080097int mbedtls_ssl_tls13_finish_handshake_msg( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +080098 size_t buf_len,
99 size_t msg_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800100{
Jerry Yuc8a392c2021-08-18 16:46:28 +0800101 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baieef15042021-11-18 07:29:56 +0000102 size_t msg_with_header_len;
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800103 ((void) buf_len);
Jerry Yuc8a392c2021-08-18 16:46:28 +0800104
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800105 /* Add reserved 4 bytes for handshake header */
Xiaofei Baieef15042021-11-18 07:29:56 +0000106 msg_with_header_len = msg_len + 4;
107 ssl->out_msglen = msg_with_header_len;
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800108 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_handshake_msg_ext( ssl, 0 ) );
Jerry Yuc8a392c2021-08-18 16:46:28 +0800109
110cleanup:
111 return( ret );
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800112}
113
Xiaofei Bai746f9482021-11-12 08:53:56 +0000114void mbedtls_ssl_tls13_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
115 unsigned hs_type,
116 unsigned char const *msg,
117 size_t msg_len )
Jerry Yu7bea4ba2021-09-09 15:06:18 +0800118{
119 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
120 ssl->handshake->update_checksum( ssl, msg, msg_len );
121}
122
Jerry Yuf4436812021-08-26 22:59:56 +0800123void mbedtls_ssl_tls13_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800124 unsigned hs_type,
125 size_t total_hs_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800126{
127 unsigned char hs_hdr[4];
128
129 /* Build HS header for checksum update. */
Jerry Yu2ac64192021-08-26 18:38:58 +0800130 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
131 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
132 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
133 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800134
135 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
136}
137
Jerry Yubc20bdd2021-08-24 15:59:48 +0800138#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Xiaofei Baie1e34422021-12-23 12:09:05 +0000139
140/*
141 * mbedtls_ssl_tls13_write_sig_alg_ext( )
142 *
143 * enum {
144 * ....
145 * ecdsa_secp256r1_sha256( 0x0403 ),
146 * ecdsa_secp384r1_sha384( 0x0503 ),
147 * ecdsa_secp521r1_sha512( 0x0603 ),
148 * ....
149 * } SignatureScheme;
150 *
151 * struct {
152 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
153 * } SignatureSchemeList;
154 *
155 * Only if we handle at least one key exchange that needs signatures.
156 */
157int mbedtls_ssl_tls13_write_sig_alg_ext( mbedtls_ssl_context *ssl,
158 unsigned char *buf,
159 unsigned char *end,
160 size_t *out_len )
161{
162 unsigned char *p = buf;
163 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
164 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
165
166 *out_len = 0;
167
168 /* Skip the extension on the client if all allowed key exchanges
169 * are PSK-based. */
170#if defined(MBEDTLS_SSL_CLI_C)
171 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
172 !mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
173 {
174 return( 0 );
175 }
176#endif /* MBEDTLS_SSL_CLI_C */
177
178 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
179
180 /* Check if we have space for header and length field:
181 * - extension_type (2 bytes)
182 * - extension_data_length (2 bytes)
183 * - supported_signature_algorithms_length (2 bytes)
184 */
185 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
186 p += 6;
187
188 /*
189 * Write supported_signature_algorithms
190 */
191 supported_sig_alg = p;
192 for( const uint16_t *sig_alg = ssl->conf->tls13_sig_algs;
193 *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
194 {
195 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
196 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
197 p += 2;
198 MBEDTLS_SSL_DEBUG_MSG( 3, ( "signature scheme [%x]", *sig_alg ) );
199 }
200
201 /* Length of supported_signature_algorithms */
202 supported_sig_alg_len = p - supported_sig_alg;
203 if( supported_sig_alg_len == 0 )
204 {
205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
206 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
207 }
208
209 /* Write extension_type */
210 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
211 /* Write extension_data_length */
212 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
213 /* Write length of supported_signature_algorithms */
214 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
215
216 /* Output the total length of signature algorithms extension. */
217 *out_len = p - buf;
218
219 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
220 return( 0 );
221}
222
223#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
224static int ssl_tls13_parse_signature_algorithms_ext( mbedtls_ssl_context *ssl,
225 const unsigned char *buf,
226 const unsigned char *end )
227{
228 size_t sig_alg_list_size; /* size of receive signature algorithms list */
229 const unsigned char *p; /* pointer to individual signature algorithm */
230 const uint16_t *sig_alg; /* iterate through configured signature schemes */
231 unsigned int signature_scheme; /* store received signature algorithm scheme */
232 uint32_t common_idx = 0; /* iterate through received_signature_schemes_list */
233 size_t buf_len = end - buf;
234
235 MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 );
236
237 sig_alg_list_size = MBEDTLS_GET_UINT16_BE( buf, 0 );
238 if( sig_alg_list_size + 2 != buf_len ||
239 sig_alg_list_size % 2 != 0 )
240 {
241 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad signature_algorithms extension" ) );
242 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
243 }
244 memset( ssl->handshake->recv_sig_schemes_list, 0,
245 sizeof( ssl->handshake->recv_sig_schemes_list ) );
246
247 for( p = buf + 2; p < end && common_idx + 1 < MBEDTLS_PK_SIGNATURE_MAX_SIZE; p += 2 )
248 {
249 signature_scheme = MBEDTLS_GET_UINT16_BE( p, 0 );
250
251 MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x",
252 signature_scheme ) );
253
254 for( sig_alg = ssl->conf->tls13_sig_algs;
255 *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ )
256 {
257 if( *sig_alg == signature_scheme )
258 {
259 ssl->handshake->recv_sig_schemes_list[common_idx] = signature_scheme;
260 common_idx++;
261 break;
262 }
263 }
264 }
265
266 if( common_idx == 0 )
267 {
268 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) );
269 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
270 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
271 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
272 }
273
274 ssl->handshake->recv_sig_schemes_list[common_idx] = MBEDTLS_TLS1_3_SIG_NONE;
275
276 return( 0 );
277}
278#endif
279
280/*
281 *
282 * STATE HANDLING: CertificateRequest
283 *
284 */
285
286/*
287 * Overview
288 */
289
290/* Coordination:
291 * Deals with the ambiguity of not knowing if a CertificateRequest
292 * will be sent. Returns a negative code on failure, or
293 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
294 * - SSL_CERTIFICATE_REQUEST_SKIP
295 * indicating if a Certificate Request is expected or not.
296 */
297#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
298#define SSL_CERTIFICATE_REQUEST_SKIP 1
299
300/*
301 * Implementation
302 */
303
304static int ssl_tls13_certificate_request_coordinate( mbedtls_ssl_context *ssl )
305{
306 int ret;
307
308 if( mbedtls_ssl_tls13_psk_enabled( ssl ) )
309 {
310 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= skip parse certificate request" ) );
311 return( SSL_CERTIFICATE_REQUEST_SKIP );
312 }
313
314 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
315 {
316 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
317 return( ret );
318 }
319 ssl->keep_current_message = 1;
320
321 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
322 {
323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
324 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
325 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
326 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
327 }
328
329 return( SSL_CERTIFICATE_REQUEST_SKIP );
330}
331
332#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
333static int ssl_tls13_parse_certificate_request( mbedtls_ssl_context *ssl,
334 const unsigned char *buf,
335 const unsigned char *end )
336{
337 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
338 const unsigned char *p = buf;
339 const unsigned char *ext = NULL;
340 size_t ext_len = 0;
341 size_t context_len = 0;
342
343
344 /*
345 * struct {
346 * opaque certificate_request_context<0..2^8-1>;
347 * Extension extensions<2..2^16-1>;
348 * } CertificateRequest;
349 */
350
351 /*
352 * Parse certificate_request_context
353 */
354
355 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
356 context_len = (size_t) p[0];
357 /* skip context_len */
358 p++;
359
360 /* Fixed length fields are:
361 * - 1 for length of context
362 * - 2 for length of extensions
363 * -----
364 * 3 bytes
365 */
366 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 3 );
367
368 /* store context ( if necessary ) */
369 if( context_len > 0 )
370 {
371 MBEDTLS_SSL_DEBUG_BUF( 3, "Certificate Request Context",
372 p, context_len );
373
374 ssl->handshake->cert_req_ctx = mbedtls_calloc( context_len, 1 );
375 if( ssl->handshake->cert_req_ctx == NULL )
376 {
377 MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
378 return ( MBEDTLS_ERR_SSL_ALLOC_FAILED );
379 }
380 memcpy( ssl->handshake->cert_req_ctx, p, context_len );
381
382 /* jump over certificate_request_context */
383 p += context_len;
384 }
385
386 /*
387 * Parse extensions
388 */
389 ext_len = MBEDTLS_GET_UINT16_BE( p, 0 );
390
391 /* At least one extension needs to be present,
392 * namely signature_algorithms ext. */
393 if( ext_len < 4 )
394 {
395 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
396 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
397 MBEDTLS_ERR_SSL_DECODE_ERROR );
398 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
399 }
400
401 /* skip total extension length */
402 p += 2;
403
404 ext = p; /* jump to extensions */
405 while( ext_len )
406 {
407 size_t ext_id = MBEDTLS_GET_UINT16_BE( ext, 0 );
408 size_t ext_size = MBEDTLS_GET_UINT16_BE( ext, 1 );
409
410 if( ext_size + 4 > ext_len )
411 {
412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
413 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
414 MBEDTLS_ERR_SSL_DECODE_ERROR );
415 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
416 }
417
418 switch( ext_id )
419 {
420 case MBEDTLS_TLS_EXT_SIG_ALG:
421 MBEDTLS_SSL_DEBUG_MSG( 3,
422 ( "found signature_algorithms extension" ) );
423
424 if( ( ret = ssl_tls13_parse_signature_algorithms_ext( ssl,
425 ext + 4, ext + 4 + ext_size ) ) != 0 )
426 {
427 MBEDTLS_SSL_DEBUG_RET( 1,
428 "ssl_tls13_parse_signature_algorithms_ext", ret );
429 MBEDTLS_SSL_PEND_FATAL_ALERT(
430 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, ret );
431 return( ret );
432 }
433 break;
434
435 default:
436 MBEDTLS_SSL_DEBUG_MSG( 3,
437 ( "unknown extension found: %" MBEDTLS_PRINTF_SIZET " ( ignoring )",
438 ext_id ) );
439 break;
440 }
441
442 ext_len -= 4 + ext_size;
443 ext += 4 + ext_size;
444
445 if( ( ext_len > 0 ) && ( ext_len < 4 ) )
446 {
447 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
448 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
449 MBEDTLS_ERR_SSL_DECODE_ERROR );
450 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
451 }
452 }
453
454 ssl->client_auth = 1;
455 return( 0 );
456}
457#endif /* ( MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ) */
458
459/* Main entry point; orchestrates the other functions */
460int mbedtls_ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
461{
462 int ret = 0;
463
464 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
465
466 /* Coordination step
467 * - Fetch record
468 * - Make sure it's either a CertificateRequest or a ServerHelloDone
469 */
470 MBEDTLS_SSL_PROC_CHK_NEG( ssl_tls13_certificate_request_coordinate( ssl ) );
471
472#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
473 if( ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST )
474 {
475 unsigned char *buf;
476 size_t buf_len;
477
478 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
479 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
480 &buf, &buf_len ) );
481
482 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_request( ssl,
483 buf, buf + buf_len ) );
484
485 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
486 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, buf, buf_len );
487 }
488 else
489#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
490 if( ret == SSL_CERTIFICATE_REQUEST_SKIP )
491 {
492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
493 }
494 else
495 {
496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
497 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
498 }
499
500 MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
501 ssl->client_auth ? "a" : "no" ) );
502
503cleanup:
504
505 /* In the MPS one would close the read-port here to
506 * ensure there's no overlap of reading and writing. */
507
508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
509 return( ret );
510}
511
Jerry Yu30b071c2021-09-12 20:16:03 +0800512/*
Jerry Yu30b071c2021-09-12 20:16:03 +0800513 * STATE HANDLING: Read CertificateVerify
514 */
Jerry Yud0fc5852021-10-29 11:09:06 +0800515/* Macro to express the maximum length of the verify structure.
Jerry Yu30b071c2021-09-12 20:16:03 +0800516 *
517 * The structure is computed per TLS 1.3 specification as:
518 * - 64 bytes of octet 32,
519 * - 33 bytes for the context string
520 * (which is either "TLS 1.3, client CertificateVerify"
521 * or "TLS 1.3, server CertificateVerify"),
Jerry Yud0fc5852021-10-29 11:09:06 +0800522 * - 1 byte for the octet 0x0, which serves as a separator,
Jerry Yu30b071c2021-09-12 20:16:03 +0800523 * - 32 or 48 bytes for the Transcript-Hash(Handshake Context, Certificate)
524 * (depending on the size of the transcript_hash)
525 *
526 * This results in a total size of
527 * - 130 bytes for a SHA256-based transcript hash, or
528 * (64 + 33 + 1 + 32 bytes)
529 * - 146 bytes for a SHA384-based transcript hash.
530 * (64 + 33 + 1 + 48 bytes)
531 *
532 */
Jerry Yu26c2d112021-10-25 12:42:58 +0800533#define SSL_VERIFY_STRUCT_MAX_SIZE ( 64 + \
534 33 + \
535 1 + \
536 MBEDTLS_TLS1_3_MD_MAX_SIZE \
Jerry Yu30b071c2021-09-12 20:16:03 +0800537 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800538
Jerry Yu0b32c502021-10-28 13:41:59 +0800539/*
540 * The ssl_tls13_create_verify_structure() creates the verify structure.
541 * As input, it requires the transcript hash.
542 *
543 * The caller has to ensure that the buffer has size at least
544 * SSL_VERIFY_STRUCT_MAX_SIZE bytes.
545 */
Jerry Yud0fc5852021-10-29 11:09:06 +0800546static void ssl_tls13_create_verify_structure( const unsigned char *transcript_hash,
Jerry Yu0b32c502021-10-28 13:41:59 +0800547 size_t transcript_hash_len,
548 unsigned char *verify_buffer,
549 size_t *verify_buffer_len,
550 int from )
551{
552 size_t idx;
Jerry Yu30b071c2021-09-12 20:16:03 +0800553
Jerry Yu0b32c502021-10-28 13:41:59 +0800554 /* RFC 8446, Section 4.4.3:
555 *
556 * The digital signature [in the CertificateVerify message] is then
557 * computed over the concatenation of:
558 * - A string that consists of octet 32 (0x20) repeated 64 times
559 * - The context string
560 * - A single 0 byte which serves as the separator
561 * - The content to be signed
562 */
563 memset( verify_buffer, 0x20, 64 );
564 idx = 64;
565
566 if( from == MBEDTLS_SSL_IS_CLIENT )
567 {
568 memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( client_cv ) );
569 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( client_cv );
570 }
571 else
572 { /* from == MBEDTLS_SSL_IS_SERVER */
573 memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( server_cv ) );
574 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( server_cv );
575 }
576
577 verify_buffer[idx++] = 0x0;
578
579 memcpy( verify_buffer + idx, transcript_hash, transcript_hash_len );
580 idx += transcript_hash_len;
581
582 *verify_buffer_len = idx;
583}
584
Jerry Yu0b32c502021-10-28 13:41:59 +0800585static int ssl_tls13_parse_certificate_verify( mbedtls_ssl_context *ssl,
Jerry Yud0fc5852021-10-29 11:09:06 +0800586 const unsigned char *buf,
587 const unsigned char *end,
588 const unsigned char *verify_buffer,
589 size_t verify_buffer_len )
Jerry Yu30b071c2021-09-12 20:16:03 +0800590{
591 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
592 const unsigned char *p = buf;
593 uint16_t algorithm;
Jerry Yu30b071c2021-09-12 20:16:03 +0800594 size_t signature_len;
595 mbedtls_pk_type_t sig_alg;
596 mbedtls_md_type_t md_alg;
Jerry Yud0fc5852021-10-29 11:09:06 +0800597 unsigned char verify_hash[MBEDTLS_MD_MAX_SIZE];
Jerry Yu30b071c2021-09-12 20:16:03 +0800598 size_t verify_hash_len;
599
Xiaofei Baid25fab62021-12-02 06:36:27 +0000600 void const *options = NULL;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000601#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Xiaofei Baid25fab62021-12-02 06:36:27 +0000602 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000603#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
604
Jerry Yu30b071c2021-09-12 20:16:03 +0800605 /*
606 * struct {
607 * SignatureScheme algorithm;
608 * opaque signature<0..2^16-1>;
609 * } CertificateVerify;
610 */
611 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
612 algorithm = MBEDTLS_GET_UINT16_BE( p, 0 );
613 p += 2;
614
615 /* RFC 8446 section 4.4.3
616 *
617 * If the CertificateVerify message is sent by a server, the signature algorithm
618 * MUST be one offered in the client's "signature_algorithms" extension unless
619 * no valid certificate chain can be produced without unsupported algorithms
620 *
621 * RFC 8446 section 4.4.2.2
622 *
623 * If the client cannot construct an acceptable chain using the provided
624 * certificates and decides to abort the handshake, then it MUST abort the handshake
625 * with an appropriate certificate-related alert (by default, "unsupported_certificate").
626 *
Jerry Yu6f87f252021-10-29 20:12:51 +0800627 * Check if algorithm is an offered signature algorithm.
Jerry Yu30b071c2021-09-12 20:16:03 +0800628 */
Jerry Yu24811fb2022-01-19 18:02:15 +0800629 if( ! mbedtls_ssl_sig_alg_is_offered( ssl, algorithm ) )
Jerry Yu30b071c2021-09-12 20:16:03 +0800630 {
Jerry Yu982d9e52021-10-14 15:59:37 +0800631 /* algorithm not in offered signature algorithms list */
632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Received signature algorithm(%04x) is not "
633 "offered.",
634 ( unsigned int ) algorithm ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800635 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800636 }
637
638 /* We currently only support ECDSA-based signatures */
639 switch( algorithm )
640 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000641 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Jerry Yu30b071c2021-09-12 20:16:03 +0800642 md_alg = MBEDTLS_MD_SHA256;
643 sig_alg = MBEDTLS_PK_ECDSA;
644 break;
Xiaofei Bai746f9482021-11-12 08:53:56 +0000645 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Jerry Yu30b071c2021-09-12 20:16:03 +0800646 md_alg = MBEDTLS_MD_SHA384;
647 sig_alg = MBEDTLS_PK_ECDSA;
648 break;
Xiaofei Bai746f9482021-11-12 08:53:56 +0000649 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Jerry Yu30b071c2021-09-12 20:16:03 +0800650 md_alg = MBEDTLS_MD_SHA512;
651 sig_alg = MBEDTLS_PK_ECDSA;
652 break;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000653#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Xiaofei Bai6dc90da2021-11-26 08:11:40 +0000654 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
XiaokangQiana83014d2021-11-22 07:53:34 +0000655 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Certificate Verify: using RSA PSS" ) );
XiaokangQian82d34cc2021-11-03 08:51:56 +0000656 md_alg = MBEDTLS_MD_SHA256;
657 sig_alg = MBEDTLS_PK_RSASSA_PSS;
658 break;
659#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800660 default:
661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Certificate Verify: Unknown signature algorithm." ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800662 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800663 }
664
665 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate Verify: Signature algorithm ( %04x )",
666 ( unsigned int ) algorithm ) );
667
668 /*
669 * Check the certificate's key type matches the signature alg
670 */
671 if( !mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk, sig_alg ) )
672 {
673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "signature algorithm doesn't match cert key" ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800674 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800675 }
676
677 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
678 signature_len = MBEDTLS_GET_UINT16_BE( p, 0 );
679 p += 2;
680 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, signature_len );
681
682 /* Hash verify buffer with indicated hash function */
683 switch( md_alg )
684 {
685#if defined(MBEDTLS_SHA256_C)
686 case MBEDTLS_MD_SHA256:
687 verify_hash_len = 32;
Jerry Yu133690c2021-10-25 14:01:13 +0800688 ret = mbedtls_sha256( verify_buffer, verify_buffer_len, verify_hash, 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800689 break;
690#endif /* MBEDTLS_SHA256_C */
691
692#if defined(MBEDTLS_SHA384_C)
693 case MBEDTLS_MD_SHA384:
694 verify_hash_len = 48;
Jerry Yu133690c2021-10-25 14:01:13 +0800695 ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 1 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800696 break;
697#endif /* MBEDTLS_SHA384_C */
698
699#if defined(MBEDTLS_SHA512_C)
700 case MBEDTLS_MD_SHA512:
701 verify_hash_len = 64;
Jerry Yu133690c2021-10-25 14:01:13 +0800702 ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800703 break;
704#endif /* MBEDTLS_SHA512_C */
705
Jerry Yu0b32c502021-10-28 13:41:59 +0800706 default:
707 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
708 break;
Jerry Yu30b071c2021-09-12 20:16:03 +0800709 }
710
Jerry Yu133690c2021-10-25 14:01:13 +0800711 if( ret != 0 )
712 {
713 MBEDTLS_SSL_DEBUG_RET( 1, "hash computation error", ret );
Jerry Yu6f87f252021-10-29 20:12:51 +0800714 goto error;
Jerry Yu133690c2021-10-25 14:01:13 +0800715 }
716
Jerry Yu30b071c2021-09-12 20:16:03 +0800717 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
XiaokangQian82d34cc2021-11-03 08:51:56 +0000718#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
719 if( sig_alg == MBEDTLS_PK_RSASSA_PSS )
720 {
721 const mbedtls_md_info_t* md_info;
Xiaofei Baid25fab62021-12-02 06:36:27 +0000722 rsassa_pss_options.mgf1_hash_id = md_alg;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000723 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
724 {
725 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
726 }
Xiaofei Baid25fab62021-12-02 06:36:27 +0000727 rsassa_pss_options.expected_salt_len = mbedtls_md_get_size( md_info );
728 options = (const void*) &rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000729 }
730#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800731
Xiaofei Baid25fab62021-12-02 06:36:27 +0000732 if( ( ret = mbedtls_pk_verify_ext( sig_alg, options,
Jerry Yu30b071c2021-09-12 20:16:03 +0800733 &ssl->session_negotiate->peer_cert->pk,
734 md_alg, verify_hash, verify_hash_len,
Jerry Yu6f87f252021-10-29 20:12:51 +0800735 p, signature_len ) ) == 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800736 {
Jerry Yu6f87f252021-10-29 20:12:51 +0800737 return( 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800738 }
Jerry Yu6f87f252021-10-29 20:12:51 +0800739 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify_ext", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800740
Jerry Yu6f87f252021-10-29 20:12:51 +0800741error:
742 /* RFC 8446 section 4.4.3
743 *
744 * If the verification fails, the receiver MUST terminate the handshake
745 * with a "decrypt_error" alert.
746 */
747 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
748 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
749 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
750
Jerry Yu30b071c2021-09-12 20:16:03 +0800751}
752#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
753
754int mbedtls_ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
755{
Jerry Yu30b071c2021-09-12 20:16:03 +0800756
Jerry Yuda8cdf22021-10-25 15:06:49 +0800757#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
758 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
759 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
760 size_t verify_buffer_len;
761 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
762 size_t transcript_len;
763 unsigned char *buf;
764 size_t buf_len;
765
Jerry Yu30b071c2021-09-12 20:16:03 +0800766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
767
Jerry Yuda8cdf22021-10-25 15:06:49 +0800768 MBEDTLS_SSL_PROC_CHK(
Xiaofei Bai746f9482021-11-12 08:53:56 +0000769 mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
Jerry Yuda8cdf22021-10-25 15:06:49 +0800770 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu30b071c2021-09-12 20:16:03 +0800771
Jerry Yuda8cdf22021-10-25 15:06:49 +0800772 /* Need to calculate the hash of the transcript first
Jerry Yu0b32c502021-10-28 13:41:59 +0800773 * before reading the message since otherwise it gets
774 * included in the transcript
775 */
Jerry Yuda8cdf22021-10-25 15:06:49 +0800776 ret = mbedtls_ssl_get_handshake_transcript( ssl,
777 ssl->handshake->ciphersuite_info->mac,
778 transcript, sizeof( transcript ),
779 &transcript_len );
780 if( ret != 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800781 {
Jerry Yuda8cdf22021-10-25 15:06:49 +0800782 MBEDTLS_SSL_PEND_FATAL_ALERT(
783 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
784 MBEDTLS_ERR_SSL_INTERNAL_ERROR );
785 return( ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800786 }
787
Jerry Yuda8cdf22021-10-25 15:06:49 +0800788 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash", transcript, transcript_len );
789
790 /* Create verify structure */
791 ssl_tls13_create_verify_structure( transcript,
Jerry Yu0b32c502021-10-28 13:41:59 +0800792 transcript_len,
793 verify_buffer,
794 &verify_buffer_len,
795 ( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) ?
796 MBEDTLS_SSL_IS_SERVER :
797 MBEDTLS_SSL_IS_CLIENT );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800798
799 /* Process the message contents */
Jerry Yu0b32c502021-10-28 13:41:59 +0800800 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_verify( ssl, buf,
801 buf + buf_len, verify_buffer, verify_buffer_len ) );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800802
Xiaofei Bai746f9482021-11-12 08:53:56 +0000803 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
Jerry Yuda8cdf22021-10-25 15:06:49 +0800804 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, buf, buf_len );
Jerry Yu30b071c2021-09-12 20:16:03 +0800805
806cleanup:
807
808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
Jerry Yu5398c102021-11-05 13:32:38 +0800809 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_process_certificate_verify", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800810 return( ret );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800811#else
812 ((void) ssl);
813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
814 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
815#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800816}
817
818/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000819 *
820 * STATE HANDLING: Incoming Certificate, client-side only currently.
821 *
822 */
823
824/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000825 * Implementation
826 */
827
Xiaofei Bai947571e2021-09-29 09:12:03 +0000828#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
829#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
830/*
831 * Structure of Certificate message:
832 *
833 * enum {
834 * X509(0),
835 * RawPublicKey(2),
836 * (255)
837 * } CertificateType;
838 *
839 * struct {
840 * select (certificate_type) {
841 * case RawPublicKey:
842 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
843 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
844 * case X509:
845 * opaque cert_data<1..2^24-1>;
846 * };
847 * Extension extensions<0..2^16-1>;
848 * } CertificateEntry;
849 *
850 * struct {
851 * opaque certificate_request_context<0..2^8-1>;
852 * CertificateEntry certificate_list<0..2^24-1>;
853 * } Certificate;
854 *
855 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000856
857/* Parse certificate chain send by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000858static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
859 const unsigned char *buf,
860 const unsigned char *end )
861{
862 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Xiaofei Baie1e34422021-12-23 12:09:05 +0000863 size_t cert_req_ctx_len = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000864 size_t certificate_list_len = 0;
865 const unsigned char *p = buf;
866 const unsigned char *certificate_list_end;
867
868 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
Xiaofei Baie1e34422021-12-23 12:09:05 +0000869 cert_req_ctx_len = p[0];
Jerry Yub640bf62021-10-29 10:05:32 +0800870 certificate_list_len = MBEDTLS_GET_UINT24_BE( p, 1 );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000871 p += 4;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000872
873 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
874 * support anything beyond 2^16 = 64K.
875 */
Xiaofei Baie1e34422021-12-23 12:09:05 +0000876 if( ( cert_req_ctx_len != 0 ) ||
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000877 ( certificate_list_len >= 0x10000 ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000878 {
879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
880 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
881 MBEDTLS_ERR_SSL_DECODE_ERROR );
882 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
883 }
884
885 /* In case we tried to reuse a session but it failed */
886 if( ssl->session_negotiate->peer_cert != NULL )
887 {
888 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
889 mbedtls_free( ssl->session_negotiate->peer_cert );
890 }
891
892 if( ( ssl->session_negotiate->peer_cert =
893 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
894 {
895 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
896 sizeof( mbedtls_x509_crt ) ) );
897 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
898 MBEDTLS_ERR_SSL_ALLOC_FAILED );
899 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
900 }
901
902 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
903
Xiaofei Bai947571e2021-09-29 09:12:03 +0000904 certificate_list_end = p + certificate_list_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000905 while( p < certificate_list_end )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000906 {
907 size_t cert_data_len, extensions_len;
908
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000909 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 3 );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000910 cert_data_len = MBEDTLS_GET_UINT24_BE( p, 0 );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000911 p += 3;
912
913 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
914 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
915 * check that we have a minimum of 128 bytes of data, this is not
916 * clear why we need that though.
917 */
918 if( ( cert_data_len < 128 ) || ( cert_data_len >= 0x10000 ) )
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000919 {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
921 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
922 MBEDTLS_ERR_SSL_DECODE_ERROR );
923 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
924 }
925
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000926 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, cert_data_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000927 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
928 p, cert_data_len );
929
930 switch( ret )
931 {
932 case 0: /*ok*/
933 break;
934 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
935 /* Ignore certificate with an unknown algorithm: maybe a
936 prior certificate was already trusted. */
937 break;
938
939 case MBEDTLS_ERR_X509_ALLOC_FAILED:
940 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
941 MBEDTLS_ERR_X509_ALLOC_FAILED );
942 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
943 return( ret );
944
945 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
946 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
947 MBEDTLS_ERR_X509_UNKNOWN_VERSION );
948 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
949 return( ret );
950
951 default:
952 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
953 ret );
954 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
955 return( ret );
956 }
957
958 p += cert_data_len;
959
960 /* Certificate extensions length */
961 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 2 );
962 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
963 p += 2;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000964 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, extensions_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000965 p += extensions_len;
966 }
967
968 /* Check that all the message is consumed. */
969 if( p != end )
970 {
971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
972 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, \
973 MBEDTLS_ERR_SSL_DECODE_ERROR );
974 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
975 }
976
977 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
978
979 return( ret );
980}
981#else
982static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
983 const unsigned char *buf,
984 const unsigned char *end )
985{
986 ((void) ssl);
987 ((void) buf);
988 ((void) end);
989 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
990}
991#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
992#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
993
994#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
995#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000996/* Validate certificate chain sent by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000997static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
998{
999 int ret = 0;
1000 mbedtls_x509_crt *ca_chain;
1001 mbedtls_x509_crl *ca_crl;
Xiaofei Baiff456022021-10-28 06:50:17 +00001002 uint32_t verify_result = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +00001003
1004#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1005 if( ssl->handshake->sni_ca_chain != NULL )
1006 {
1007 ca_chain = ssl->handshake->sni_ca_chain;
1008 ca_crl = ssl->handshake->sni_ca_crl;
1009 }
1010 else
1011#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1012 {
1013 ca_chain = ssl->conf->ca_chain;
1014 ca_crl = ssl->conf->ca_crl;
1015 }
1016
1017 /*
1018 * Main check: verify certificate
1019 */
1020 ret = mbedtls_x509_crt_verify_with_profile(
1021 ssl->session_negotiate->peer_cert,
1022 ca_chain, ca_crl,
1023 ssl->conf->cert_profile,
1024 ssl->hostname,
Xiaofei Baiff456022021-10-28 06:50:17 +00001025 &verify_result,
Xiaofei Bai947571e2021-09-29 09:12:03 +00001026 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
1027
1028 if( ret != 0 )
1029 {
1030 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
1031 }
1032
1033 /*
1034 * Secondary checks: always done, but change 'ret' only if it was 0
1035 */
1036
1037#if defined(MBEDTLS_ECP_C)
1038 {
1039 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
1040
1041 /* If certificate uses an EC key, make sure the curve is OK */
1042 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
1043 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
1044 {
Xiaofei Baiff456022021-10-28 06:50:17 +00001045 verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
Xiaofei Bai947571e2021-09-29 09:12:03 +00001046
1047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( EC key curve )" ) );
1048 if( ret == 0 )
1049 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
1050 }
1051 }
1052#endif /* MBEDTLS_ECP_C */
1053
1054 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
1055 ssl->handshake->ciphersuite_info,
1056 !ssl->conf->endpoint,
Xiaofei Baiff456022021-10-28 06:50:17 +00001057 &verify_result ) != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001058 {
1059 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( usage extensions )" ) );
1060 if( ret == 0 )
1061 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
1062 }
1063
1064
1065 if( ca_chain == NULL )
1066 {
1067 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
1068 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
1069 }
1070
1071 if( ret != 0 )
1072 {
1073 /* The certificate may have been rejected for several reasons.
1074 Pick one and send the corresponding alert. Which alert to send
1075 may be a subject of debate in some cases. */
Xiaofei Baiff456022021-10-28 06:50:17 +00001076 if( verify_result & MBEDTLS_X509_BADCERT_OTHER )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001077 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +00001078 else if( verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001079 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret );
Xiaofei Baif93cbd22021-10-29 02:39:30 +00001080 else if( verify_result & ( MBEDTLS_X509_BADCERT_KEY_USAGE |
1081 MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
1082 MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
1083 MBEDTLS_X509_BADCERT_BAD_PK |
1084 MBEDTLS_X509_BADCERT_BAD_KEY ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001085 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +00001086 else if( verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001087 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +00001088 else if( verify_result & MBEDTLS_X509_BADCERT_REVOKED )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001089 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +00001090 else if( verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001091 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret );
1092 else
1093 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret );
1094 }
1095
1096#if defined(MBEDTLS_DEBUG_C)
Xiaofei Baiff456022021-10-28 06:50:17 +00001097 if( verify_result != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001098 {
1099 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
Jerry Yu83bb1312021-10-28 22:16:33 +08001100 (unsigned int) verify_result ) );
Xiaofei Bai947571e2021-09-29 09:12:03 +00001101 }
1102 else
1103 {
1104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
1105 }
1106#endif /* MBEDTLS_DEBUG_C */
1107
Xiaofei Baiff456022021-10-28 06:50:17 +00001108 ssl->session_negotiate->verify_result = verify_result;
Xiaofei Bai947571e2021-09-29 09:12:03 +00001109 return( ret );
1110}
1111#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1112static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
1113{
1114 ((void) ssl);
1115 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
1116}
1117#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1118#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
1119
Xiaofei Bai79595ac2021-10-26 07:16:45 +00001120int mbedtls_ssl_tls13_process_certificate( mbedtls_ssl_context *ssl )
Xiaofei Bai947571e2021-09-29 09:12:03 +00001121{
Xiaofei Bai79595ac2021-10-26 07:16:45 +00001122 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1123 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
1124
1125#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1126 unsigned char *buf;
1127 size_t buf_len;
1128
Xiaofei Bai746f9482021-11-12 08:53:56 +00001129 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
Xiaofei Bai79595ac2021-10-26 07:16:45 +00001130 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
1131 &buf, &buf_len ) );
1132
1133 /* Parse the certificate chain sent by the peer. */
1134 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate( ssl, buf, buf + buf_len ) );
1135 /* Validate the certificate chain and set the verification results. */
1136 MBEDTLS_SSL_PROC_CHK( ssl_tls13_validate_certificate( ssl ) );
1137
Xiaofei Bai746f9482021-11-12 08:53:56 +00001138 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
1139 buf, buf_len );
Xiaofei Bai79595ac2021-10-26 07:16:45 +00001140
Xiaofei Bai79595ac2021-10-26 07:16:45 +00001141cleanup:
1142
1143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Xiaofei Bai10aeec02021-10-26 09:50:08 +00001144#else
1145 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1146 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1147#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +00001148 return( ret );
Xiaofei Bai947571e2021-09-29 09:12:03 +00001149}
1150
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001151/*
1152 *
XiaokangQianc5c39d52021-11-09 11:55:10 +00001153 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001154 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001155/*
1156 * Implementation
1157 */
1158
XiaokangQianaaa0e192021-11-10 03:07:04 +00001159static int ssl_tls13_preprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001160{
1161 int ret;
1162
XiaokangQianc5c39d52021-11-09 11:55:10 +00001163 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001164 ssl->handshake->state_local.finished_in.digest,
1165 sizeof( ssl->handshake->state_local.finished_in.digest ),
1166 &ssl->handshake->state_local.finished_in.digest_len,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001167 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
XiaokangQianc13f9352021-11-11 06:13:22 +00001168 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001169 if( ret != 0 )
1170 {
XiaokangQianc5c39d52021-11-09 11:55:10 +00001171 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_calculate_verify_data", ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001172 return( ret );
1173 }
1174
1175 return( 0 );
1176}
1177
XiaokangQianc5c39d52021-11-09 11:55:10 +00001178static int ssl_tls13_parse_finished_message( mbedtls_ssl_context *ssl,
1179 const unsigned char *buf,
1180 const unsigned char *end )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001181{
XiaokangQian33062842021-11-11 03:37:45 +00001182 /*
1183 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +00001184 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +00001185 * } Finished;
1186 */
1187 const unsigned char *expected_verify_data =
1188 ssl->handshake->state_local.finished_in.digest;
1189 size_t expected_verify_data_len =
1190 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001191 /* Structural validation */
XiaokangQian33062842021-11-11 03:37:45 +00001192 if( (size_t)( end - buf ) != expected_verify_data_len )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001193 {
1194 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1195
1196 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +00001197 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001198 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
1199 }
1200
XiaokangQianc5c39d52021-11-09 11:55:10 +00001201 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (self-computed):",
XiaokangQian33062842021-11-11 03:37:45 +00001202 expected_verify_data,
1203 expected_verify_data_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001204 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (received message):", buf,
XiaokangQian33062842021-11-11 03:37:45 +00001205 expected_verify_data_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001206
1207 /* Semantic validation */
Gabor Mezei685472b2021-11-24 11:17:36 +01001208 if( mbedtls_ct_memcmp( buf,
1209 expected_verify_data,
1210 expected_verify_data_len ) != 0 )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001211 {
1212 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1213
XiaokangQian33062842021-11-11 03:37:45 +00001214 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +00001215 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001216 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1217 }
1218 return( 0 );
1219}
1220
XiaokangQianc13f9352021-11-11 06:13:22 +00001221#if defined(MBEDTLS_SSL_CLI_C)
XiaokangQianaaa0e192021-11-10 03:07:04 +00001222static int ssl_tls13_postprocess_server_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001223{
XiaokangQian33062842021-11-11 03:37:45 +00001224 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001225 mbedtls_ssl_key_set traffic_keys;
XiaokangQian1aef02e2021-10-28 09:54:34 +00001226 mbedtls_ssl_transform *transform_application = NULL;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001227
XiaokangQian4cab0242021-10-12 08:43:37 +00001228 ret = mbedtls_ssl_tls13_key_schedule_stage_application( ssl );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001229 if( ret != 0 )
1230 {
1231 MBEDTLS_SSL_DEBUG_RET( 1,
XiaokangQian4cab0242021-10-12 08:43:37 +00001232 "mbedtls_ssl_tls13_key_schedule_stage_application", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001233 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001234 }
1235
XiaokangQian33062842021-11-11 03:37:45 +00001236 ret = mbedtls_ssl_tls13_generate_application_keys( ssl, &traffic_keys );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001237 if( ret != 0 )
1238 {
1239 MBEDTLS_SSL_DEBUG_RET( 1,
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001240 "mbedtls_ssl_tls13_generate_application_keys", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001241 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001242 }
1243
1244 transform_application =
1245 mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
1246 if( transform_application == NULL )
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001247 {
1248 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1249 goto cleanup;
1250 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001251
1252 ret = mbedtls_ssl_tls13_populate_transform(
1253 transform_application,
1254 ssl->conf->endpoint,
1255 ssl->session_negotiate->ciphersuite,
1256 &traffic_keys,
1257 ssl );
1258 if( ret != 0 )
1259 {
1260 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001261 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001262 }
1263
1264 ssl->transform_application = transform_application;
1265
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001266cleanup:
1267
XiaokangQian33062842021-11-11 03:37:45 +00001268 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001269 if( ret != 0 )
XiaokangQian1aef02e2021-10-28 09:54:34 +00001270 {
1271 mbedtls_free( transform_application );
1272 MBEDTLS_SSL_PEND_FATAL_ALERT(
1273 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1274 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1275 }
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001276 return( ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001277}
XiaokangQianc13f9352021-11-11 06:13:22 +00001278#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001279
XiaokangQiancc90c942021-11-09 12:30:09 +00001280static int ssl_tls13_postprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001281{
1282
XiaokangQianc13f9352021-11-11 06:13:22 +00001283#if defined(MBEDTLS_SSL_CLI_C)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001284 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
1285 {
XiaokangQianaaa0e192021-11-10 03:07:04 +00001286 return( ssl_tls13_postprocess_server_finished_message( ssl ) );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001287 }
XiaokangQianc13f9352021-11-11 06:13:22 +00001288#else
1289 ((void) ssl);
1290#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001291
1292 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1293}
1294
XiaokangQianc5c39d52021-11-09 11:55:10 +00001295int mbedtls_ssl_tls13_process_finished_message( mbedtls_ssl_context *ssl )
1296{
XiaokangQian33062842021-11-11 03:37:45 +00001297 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001298 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +00001299 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001300
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001302
1303 /* Preprocessing step: Compute handshake digest */
XiaokangQianaaa0e192021-11-10 03:07:04 +00001304 MBEDTLS_SSL_PROC_CHK( ssl_tls13_preprocess_finished_message( ssl ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001305
Xiaofei Bai746f9482021-11-12 08:53:56 +00001306 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001307 MBEDTLS_SSL_HS_FINISHED,
Xiaofei Baieef15042021-11-18 07:29:56 +00001308 &buf, &buf_len ) );
1309 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_finished_message( ssl, buf, buf + buf_len ) );
Xiaofei Bai746f9482021-11-12 08:53:56 +00001310 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
Xiaofei Baieef15042021-11-18 07:29:56 +00001311 ssl, MBEDTLS_SSL_HS_FINISHED, buf, buf_len );
XiaokangQianaaa0e192021-11-10 03:07:04 +00001312 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_finished_message( ssl ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001313
1314cleanup:
1315
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001316 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001317 return( ret );
1318}
1319
XiaokangQian74af2a82021-09-22 07:40:30 +00001320/*
1321 *
XiaokangQiancc90c942021-11-09 12:30:09 +00001322 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +00001323 *
1324 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001325/*
XiaokangQian35dc6252021-11-11 08:16:19 +00001326 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +00001327 */
1328
XiaokangQian8773aa02021-11-10 07:33:09 +00001329static int ssl_tls13_prepare_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +00001330{
1331 int ret;
1332
1333 /* Compute transcript of handshake up to now. */
XiaokangQiancc90c942021-11-09 12:30:09 +00001334 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQian74af2a82021-09-22 07:40:30 +00001335 ssl->handshake->state_local.finished_out.digest,
1336 sizeof( ssl->handshake->state_local.finished_out.digest ),
1337 &ssl->handshake->state_local.finished_out.digest_len,
1338 ssl->conf->endpoint );
1339
1340 if( ret != 0 )
1341 {
Jerry Yu7ca30542021-12-08 15:57:57 +08001342 MBEDTLS_SSL_DEBUG_RET( 1, "calculate_verify_data failed", ret );
XiaokangQian74af2a82021-09-22 07:40:30 +00001343 return( ret );
1344 }
1345
1346 return( 0 );
1347}
1348
XiaokangQiancc90c942021-11-09 12:30:09 +00001349static int ssl_tls13_finalize_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +00001350{
XiaokangQian0fa66432021-11-15 03:33:57 +00001351 // TODO: Add back resumption keys calculation after MVP.
1352 ((void) ssl);
XiaokangQian74af2a82021-09-22 07:40:30 +00001353
1354 return( 0 );
1355}
1356
XiaokangQian8773aa02021-11-10 07:33:09 +00001357static int ssl_tls13_write_finished_message_body( mbedtls_ssl_context *ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001358 unsigned char *buf,
1359 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001360 size_t *out_len )
XiaokangQian74af2a82021-09-22 07:40:30 +00001361{
XiaokangQian8773aa02021-11-10 07:33:09 +00001362 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001363 /*
1364 * struct {
1365 * opaque verify_data[Hash.length];
1366 * } Finished;
1367 */
XiaokangQian8773aa02021-11-10 07:33:09 +00001368 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001369
1370 memcpy( buf, ssl->handshake->state_local.finished_out.digest,
XiaokangQian8773aa02021-11-10 07:33:09 +00001371 verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001372
Xiaofei Baid25fab62021-12-02 06:36:27 +00001373 *out_len = verify_data_len;
XiaokangQian74af2a82021-09-22 07:40:30 +00001374 return( 0 );
1375}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001376
XiaokangQian35dc6252021-11-11 08:16:19 +00001377/* Main entry point: orchestrates the other functions */
1378int mbedtls_ssl_tls13_write_finished_message( mbedtls_ssl_context *ssl )
1379{
1380 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1381 unsigned char *buf;
1382 size_t buf_len, msg_len;
1383
1384 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished message" ) );
1385
XiaokangQiandce82242021-11-15 06:01:26 +00001386 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_finished_message( ssl ) );
1387
XiaokangQian35dc6252021-11-11 08:16:19 +00001388 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg( ssl,
1389 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len ) );
1390
1391 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_finished_message_body(
1392 ssl, buf, buf + buf_len, &msg_len ) );
1393
Xiaofei Bai746f9482021-11-12 08:53:56 +00001394 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1395 buf, msg_len );
XiaokangQian35dc6252021-11-11 08:16:19 +00001396
1397 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_finished_message( ssl ) );
1398 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
1399 buf_len, msg_len ) );
1400 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_flush_output( ssl ) );
1401
1402cleanup:
1403
1404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished message" ) );
1405 return( ret );
1406}
1407
Jerry Yu378254d2021-10-30 21:44:47 +08001408void mbedtls_ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
1409{
1410
1411 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
1412
1413 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001414 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001415 */
1416 if( ssl->session )
1417 {
Jerry Yu378254d2021-10-30 21:44:47 +08001418 mbedtls_ssl_session_free( ssl->session );
1419 mbedtls_free( ssl->session );
1420 }
1421 ssl->session = ssl->session_negotiate;
1422 ssl->session_negotiate = NULL;
1423
1424 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
1425}
1426
Ronald Cron49ad6192021-11-24 16:25:31 +01001427/*
1428 *
1429 * STATE HANDLING: Write ChangeCipherSpec
1430 *
1431 */
1432#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1433
1434static int ssl_tls13_write_change_cipher_spec_body( mbedtls_ssl_context *ssl,
1435 unsigned char *buf,
1436 unsigned char *end,
1437 size_t *olen )
1438{
1439 ((void) ssl);
1440
1441 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 1 );
1442 buf[0] = 1;
1443 *olen = 1;
1444
1445 return( 0 );
1446}
1447
Ronald Cron49ad6192021-11-24 16:25:31 +01001448int mbedtls_ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1449{
1450 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1451
1452 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
1453
1454 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_flush_output( ssl ) );
1455
1456 /* Write CCS message */
1457 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_change_cipher_spec_body(
1458 ssl, ssl->out_msg,
1459 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1460 &ssl->out_msglen ) );
1461
1462 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1463
Ronald Cron49ad6192021-11-24 16:25:31 +01001464 /* Dispatch message */
1465 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_record( ssl, 1 ) );
1466
1467cleanup:
1468
1469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
1470 return( ret );
1471}
1472
1473#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1474
Ronald Cron6f135e12021-12-08 16:57:54 +01001475#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001476
1477#endif /* MBEDTLS_SSL_TLS_C */