1 /*
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
24 #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
25
26 #include <string.h>
27
28 #include "mbedtls/error.h"
29 #include "mbedtls/debug.h"
30 #include "mbedtls/oid.h"
31 #include "mbedtls/platform.h"
32 #include "mbedtls/constant_time.h"
33 #include <string.h>
34
35 #include "ssl_misc.h"
36 #include "ssl_tls13_keys.h"
37
mbedtls_ssl_tls13_fetch_handshake_msg(mbedtls_ssl_context * ssl,unsigned hs_type,unsigned char ** buf,size_t * buf_len)38 int mbedtls_ssl_tls13_fetch_handshake_msg( mbedtls_ssl_context *ssl,
39 unsigned hs_type,
40 unsigned char **buf,
41 size_t *buf_len )
42 {
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
51 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
52 ssl->in_msg[0] != hs_type )
53 {
54 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Receive unexpected handshake message." ) );
55 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
56 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
57 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
58 goto cleanup;
59 }
60
61 /*
62 * Jump handshake header (4 bytes, see Section 4 of RFC 8446).
63 * ...
64 * HandshakeType msg_type;
65 * uint24 length;
66 * ...
67 */
68 *buf = ssl->in_msg + 4;
69 *buf_len = ssl->in_hslen - 4;
70
71 cleanup:
72
73 return( ret );
74 }
75
mbedtls_ssl_tls13_start_handshake_msg(mbedtls_ssl_context * ssl,unsigned hs_type,unsigned char ** buf,size_t * buf_len)76 int mbedtls_ssl_tls13_start_handshake_msg( mbedtls_ssl_context *ssl,
77 unsigned hs_type,
78 unsigned char **buf,
79 size_t *buf_len )
80 {
81 /*
82 * Reserve 4 bytes for hanshake header. ( Section 4,RFC 8446 )
83 * ...
84 * HandshakeType msg_type;
85 * uint24 length;
86 * ...
87 */
88 *buf = ssl->out_msg + 4;
89 *buf_len = MBEDTLS_SSL_OUT_CONTENT_LEN - 4;
90
91 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
92 ssl->out_msg[0] = hs_type;
93
94 return( 0 );
95 }
96
mbedtls_ssl_tls13_finish_handshake_msg(mbedtls_ssl_context * ssl,size_t buf_len,size_t msg_len)97 int mbedtls_ssl_tls13_finish_handshake_msg( mbedtls_ssl_context *ssl,
98 size_t buf_len,
99 size_t msg_len )
100 {
101 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
102 size_t msg_with_header_len;
103 ((void) buf_len);
104
105 /* Add reserved 4 bytes for handshake header */
106 msg_with_header_len = msg_len + 4;
107 ssl->out_msglen = msg_with_header_len;
108 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_handshake_msg_ext( ssl, 0 ) );
109
110 cleanup:
111 return( ret );
112 }
113
mbedtls_ssl_tls13_add_hs_msg_to_checksum(mbedtls_ssl_context * ssl,unsigned hs_type,unsigned char const * msg,size_t msg_len)114 void 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 )
118 {
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
mbedtls_ssl_tls13_add_hs_hdr_to_checksum(mbedtls_ssl_context * ssl,unsigned hs_type,size_t total_hs_len)123 void mbedtls_ssl_tls13_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
124 unsigned hs_type,
125 size_t total_hs_len )
126 {
127 unsigned char hs_hdr[4];
128
129 /* Build HS header for checksum update. */
130 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 );
134
135 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
136 }
137
138 #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
139
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 */
mbedtls_ssl_tls13_write_sig_alg_ext(mbedtls_ssl_context * ssl,unsigned char * buf,unsigned char * end,size_t * out_len)157 int 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 /*
224 * STATE HANDLING: Read CertificateVerify
225 */
226 /* Macro to express the maximum length of the verify structure.
227 *
228 * The structure is computed per TLS 1.3 specification as:
229 * - 64 bytes of octet 32,
230 * - 33 bytes for the context string
231 * (which is either "TLS 1.3, client CertificateVerify"
232 * or "TLS 1.3, server CertificateVerify"),
233 * - 1 byte for the octet 0x0, which serves as a separator,
234 * - 32 or 48 bytes for the Transcript-Hash(Handshake Context, Certificate)
235 * (depending on the size of the transcript_hash)
236 *
237 * This results in a total size of
238 * - 130 bytes for a SHA256-based transcript hash, or
239 * (64 + 33 + 1 + 32 bytes)
240 * - 146 bytes for a SHA384-based transcript hash.
241 * (64 + 33 + 1 + 48 bytes)
242 *
243 */
244 #define SSL_VERIFY_STRUCT_MAX_SIZE ( 64 + \
245 33 + \
246 1 + \
247 MBEDTLS_TLS1_3_MD_MAX_SIZE \
248 )
249
250 /*
251 * The ssl_tls13_create_verify_structure() creates the verify structure.
252 * As input, it requires the transcript hash.
253 *
254 * The caller has to ensure that the buffer has size at least
255 * SSL_VERIFY_STRUCT_MAX_SIZE bytes.
256 */
ssl_tls13_create_verify_structure(const unsigned char * transcript_hash,size_t transcript_hash_len,unsigned char * verify_buffer,size_t * verify_buffer_len,int from)257 static void ssl_tls13_create_verify_structure( const unsigned char *transcript_hash,
258 size_t transcript_hash_len,
259 unsigned char *verify_buffer,
260 size_t *verify_buffer_len,
261 int from )
262 {
263 size_t idx;
264
265 /* RFC 8446, Section 4.4.3:
266 *
267 * The digital signature [in the CertificateVerify message] is then
268 * computed over the concatenation of:
269 * - A string that consists of octet 32 (0x20) repeated 64 times
270 * - The context string
271 * - A single 0 byte which serves as the separator
272 * - The content to be signed
273 */
274 memset( verify_buffer, 0x20, 64 );
275 idx = 64;
276
277 if( from == MBEDTLS_SSL_IS_CLIENT )
278 {
279 memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( client_cv ) );
280 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( client_cv );
281 }
282 else
283 { /* from == MBEDTLS_SSL_IS_SERVER */
284 memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( server_cv ) );
285 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( server_cv );
286 }
287
288 verify_buffer[idx++] = 0x0;
289
290 memcpy( verify_buffer + idx, transcript_hash, transcript_hash_len );
291 idx += transcript_hash_len;
292
293 *verify_buffer_len = idx;
294 }
295
ssl_tls13_sig_alg_is_offered(const mbedtls_ssl_context * ssl,uint16_t sig_alg)296 static int ssl_tls13_sig_alg_is_offered( const mbedtls_ssl_context *ssl,
297 uint16_t sig_alg )
298 {
299 const uint16_t *tls13_sig_alg = ssl->conf->tls13_sig_algs;
300
301 for( ; *tls13_sig_alg != MBEDTLS_TLS1_3_SIG_NONE ; tls13_sig_alg++ )
302 {
303 if( *tls13_sig_alg == sig_alg )
304 return( 1 );
305 }
306 return( 0 );
307 }
308
ssl_tls13_parse_certificate_verify(mbedtls_ssl_context * ssl,const unsigned char * buf,const unsigned char * end,const unsigned char * verify_buffer,size_t verify_buffer_len)309 static int ssl_tls13_parse_certificate_verify( mbedtls_ssl_context *ssl,
310 const unsigned char *buf,
311 const unsigned char *end,
312 const unsigned char *verify_buffer,
313 size_t verify_buffer_len )
314 {
315 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
316 const unsigned char *p = buf;
317 uint16_t algorithm;
318 size_t signature_len;
319 mbedtls_pk_type_t sig_alg;
320 mbedtls_md_type_t md_alg;
321 unsigned char verify_hash[MBEDTLS_MD_MAX_SIZE];
322 size_t verify_hash_len;
323
324 void const *options = NULL;
325 #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
326 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
327 #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
328
329 /*
330 * struct {
331 * SignatureScheme algorithm;
332 * opaque signature<0..2^16-1>;
333 * } CertificateVerify;
334 */
335 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
336 algorithm = MBEDTLS_GET_UINT16_BE( p, 0 );
337 p += 2;
338
339 /* RFC 8446 section 4.4.3
340 *
341 * If the CertificateVerify message is sent by a server, the signature algorithm
342 * MUST be one offered in the client's "signature_algorithms" extension unless
343 * no valid certificate chain can be produced without unsupported algorithms
344 *
345 * RFC 8446 section 4.4.2.2
346 *
347 * If the client cannot construct an acceptable chain using the provided
348 * certificates and decides to abort the handshake, then it MUST abort the handshake
349 * with an appropriate certificate-related alert (by default, "unsupported_certificate").
350 *
351 * Check if algorithm is an offered signature algorithm.
352 */
353 if( ! ssl_tls13_sig_alg_is_offered( ssl, algorithm ) )
354 {
355 /* algorithm not in offered signature algorithms list */
356 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Received signature algorithm(%04x) is not "
357 "offered.",
358 ( unsigned int ) algorithm ) );
359 goto error;
360 }
361
362 /* We currently only support ECDSA-based signatures */
363 switch( algorithm )
364 {
365 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
366 md_alg = MBEDTLS_MD_SHA256;
367 sig_alg = MBEDTLS_PK_ECDSA;
368 break;
369 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
370 md_alg = MBEDTLS_MD_SHA384;
371 sig_alg = MBEDTLS_PK_ECDSA;
372 break;
373 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
374 md_alg = MBEDTLS_MD_SHA512;
375 sig_alg = MBEDTLS_PK_ECDSA;
376 break;
377 #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
378 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
379 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Certificate Verify: using RSA PSS" ) );
380 md_alg = MBEDTLS_MD_SHA256;
381 sig_alg = MBEDTLS_PK_RSASSA_PSS;
382 break;
383 #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
384 default:
385 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Certificate Verify: Unknown signature algorithm." ) );
386 goto error;
387 }
388
389 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate Verify: Signature algorithm ( %04x )",
390 ( unsigned int ) algorithm ) );
391
392 /*
393 * Check the certificate's key type matches the signature alg
394 */
395 if( !mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk, sig_alg ) )
396 {
397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "signature algorithm doesn't match cert key" ) );
398 goto error;
399 }
400
401 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
402 signature_len = MBEDTLS_GET_UINT16_BE( p, 0 );
403 p += 2;
404 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, signature_len );
405
406 /* Hash verify buffer with indicated hash function */
407 switch( md_alg )
408 {
409 #if defined(MBEDTLS_SHA256_C)
410 case MBEDTLS_MD_SHA256:
411 verify_hash_len = 32;
412 ret = mbedtls_sha256( verify_buffer, verify_buffer_len, verify_hash, 0 );
413 break;
414 #endif /* MBEDTLS_SHA256_C */
415
416 #if defined(MBEDTLS_SHA384_C)
417 case MBEDTLS_MD_SHA384:
418 verify_hash_len = 48;
419 ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 1 );
420 break;
421 #endif /* MBEDTLS_SHA384_C */
422
423 #if defined(MBEDTLS_SHA512_C)
424 case MBEDTLS_MD_SHA512:
425 verify_hash_len = 64;
426 ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 0 );
427 break;
428 #endif /* MBEDTLS_SHA512_C */
429
430 default:
431 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
432 break;
433 }
434
435 if( ret != 0 )
436 {
437 MBEDTLS_SSL_DEBUG_RET( 1, "hash computation error", ret );
438 goto error;
439 }
440
441 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
442 #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
443 if( sig_alg == MBEDTLS_PK_RSASSA_PSS )
444 {
445 const mbedtls_md_info_t* md_info;
446 rsassa_pss_options.mgf1_hash_id = md_alg;
447 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
448 {
449 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
450 }
451 rsassa_pss_options.expected_salt_len = mbedtls_md_get_size( md_info );
452 options = (const void*) &rsassa_pss_options;
453 }
454 #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
455
456 if( ( ret = mbedtls_pk_verify_ext( sig_alg, options,
457 &ssl->session_negotiate->peer_cert->pk,
458 md_alg, verify_hash, verify_hash_len,
459 p, signature_len ) ) == 0 )
460 {
461 return( 0 );
462 }
463 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify_ext", ret );
464
465 error:
466 /* RFC 8446 section 4.4.3
467 *
468 * If the verification fails, the receiver MUST terminate the handshake
469 * with a "decrypt_error" alert.
470 */
471 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
472 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
473 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
474
475 }
476 #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
477
mbedtls_ssl_tls13_process_certificate_verify(mbedtls_ssl_context * ssl)478 int mbedtls_ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
479 {
480
481 #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
482 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
483 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
484 size_t verify_buffer_len;
485 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
486 size_t transcript_len;
487 unsigned char *buf;
488 size_t buf_len;
489
490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
491
492 MBEDTLS_SSL_PROC_CHK(
493 mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
494 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
495
496 /* Need to calculate the hash of the transcript first
497 * before reading the message since otherwise it gets
498 * included in the transcript
499 */
500 ret = mbedtls_ssl_get_handshake_transcript( ssl,
501 ssl->handshake->ciphersuite_info->mac,
502 transcript, sizeof( transcript ),
503 &transcript_len );
504 if( ret != 0 )
505 {
506 MBEDTLS_SSL_PEND_FATAL_ALERT(
507 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
508 MBEDTLS_ERR_SSL_INTERNAL_ERROR );
509 return( ret );
510 }
511
512 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash", transcript, transcript_len );
513
514 /* Create verify structure */
515 ssl_tls13_create_verify_structure( transcript,
516 transcript_len,
517 verify_buffer,
518 &verify_buffer_len,
519 ( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) ?
520 MBEDTLS_SSL_IS_SERVER :
521 MBEDTLS_SSL_IS_CLIENT );
522
523 /* Process the message contents */
524 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_verify( ssl, buf,
525 buf + buf_len, verify_buffer, verify_buffer_len ) );
526
527 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl,
528 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, buf, buf_len );
529
530 cleanup:
531
532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
533 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_process_certificate_verify", ret );
534 return( ret );
535 #else
536 ((void) ssl);
537 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
538 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
539 #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
540 }
541
542 /*
543 *
544 * STATE HANDLING: Incoming Certificate, client-side only currently.
545 *
546 */
547
548 /*
549 * Implementation
550 */
551
552 #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
553 #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
554 /*
555 * Structure of Certificate message:
556 *
557 * enum {
558 * X509(0),
559 * RawPublicKey(2),
560 * (255)
561 * } CertificateType;
562 *
563 * struct {
564 * select (certificate_type) {
565 * case RawPublicKey:
566 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
567 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
568 * case X509:
569 * opaque cert_data<1..2^24-1>;
570 * };
571 * Extension extensions<0..2^16-1>;
572 * } CertificateEntry;
573 *
574 * struct {
575 * opaque certificate_request_context<0..2^8-1>;
576 * CertificateEntry certificate_list<0..2^24-1>;
577 * } Certificate;
578 *
579 */
580
581 /* Parse certificate chain send by the server. */
ssl_tls13_parse_certificate(mbedtls_ssl_context * ssl,const unsigned char * buf,const unsigned char * end)582 static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
583 const unsigned char *buf,
584 const unsigned char *end )
585 {
586 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
587 size_t certificate_request_context_len = 0;
588 size_t certificate_list_len = 0;
589 const unsigned char *p = buf;
590 const unsigned char *certificate_list_end;
591
592 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
593 certificate_request_context_len = p[0];
594 certificate_list_len = MBEDTLS_GET_UINT24_BE( p, 1 );
595 p += 4;
596
597 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
598 * support anything beyond 2^16 = 64K.
599 */
600 if( ( certificate_request_context_len != 0 ) ||
601 ( certificate_list_len >= 0x10000 ) )
602 {
603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
604 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
605 MBEDTLS_ERR_SSL_DECODE_ERROR );
606 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
607 }
608
609 /* In case we tried to reuse a session but it failed */
610 if( ssl->session_negotiate->peer_cert != NULL )
611 {
612 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
613 mbedtls_free( ssl->session_negotiate->peer_cert );
614 }
615
616 if( ( ssl->session_negotiate->peer_cert =
617 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
618 {
619 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
620 sizeof( mbedtls_x509_crt ) ) );
621 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
622 MBEDTLS_ERR_SSL_ALLOC_FAILED );
623 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
624 }
625
626 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
627
628 certificate_list_end = p + certificate_list_len;
629 while( p < certificate_list_end )
630 {
631 size_t cert_data_len, extensions_len;
632
633 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 3 );
634 cert_data_len = MBEDTLS_GET_UINT24_BE( p, 0 );
635 p += 3;
636
637 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
638 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
639 * check that we have a minimum of 128 bytes of data, this is not
640 * clear why we need that though.
641 */
642 if( ( cert_data_len < 128 ) || ( cert_data_len >= 0x10000 ) )
643 {
644 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
645 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
646 MBEDTLS_ERR_SSL_DECODE_ERROR );
647 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
648 }
649
650 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, cert_data_len );
651 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
652 p, cert_data_len );
653
654 switch( ret )
655 {
656 case 0: /*ok*/
657 break;
658 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
659 /* Ignore certificate with an unknown algorithm: maybe a
660 prior certificate was already trusted. */
661 break;
662
663 case MBEDTLS_ERR_X509_ALLOC_FAILED:
664 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
665 MBEDTLS_ERR_X509_ALLOC_FAILED );
666 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
667 return( ret );
668
669 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
670 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
671 MBEDTLS_ERR_X509_UNKNOWN_VERSION );
672 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
673 return( ret );
674
675 default:
676 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
677 ret );
678 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
679 return( ret );
680 }
681
682 p += cert_data_len;
683
684 /* Certificate extensions length */
685 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 2 );
686 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
687 p += 2;
688 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, extensions_len );
689 p += extensions_len;
690 }
691
692 /* Check that all the message is consumed. */
693 if( p != end )
694 {
695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
696 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, \
697 MBEDTLS_ERR_SSL_DECODE_ERROR );
698 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
699 }
700
701 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
702
703 return( ret );
704 }
705 #else
ssl_tls13_parse_certificate(mbedtls_ssl_context * ssl,const unsigned char * buf,const unsigned char * end)706 static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
707 const unsigned char *buf,
708 const unsigned char *end )
709 {
710 ((void) ssl);
711 ((void) buf);
712 ((void) end);
713 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
714 }
715 #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
716 #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
717
718 #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
719 #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
720 /* Validate certificate chain sent by the server. */
ssl_tls13_validate_certificate(mbedtls_ssl_context * ssl)721 static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
722 {
723 int ret = 0;
724 mbedtls_x509_crt *ca_chain;
725 mbedtls_x509_crl *ca_crl;
726 uint32_t verify_result = 0;
727
728 #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
729 if( ssl->handshake->sni_ca_chain != NULL )
730 {
731 ca_chain = ssl->handshake->sni_ca_chain;
732 ca_crl = ssl->handshake->sni_ca_crl;
733 }
734 else
735 #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
736 {
737 ca_chain = ssl->conf->ca_chain;
738 ca_crl = ssl->conf->ca_crl;
739 }
740
741 /*
742 * Main check: verify certificate
743 */
744 ret = mbedtls_x509_crt_verify_with_profile(
745 ssl->session_negotiate->peer_cert,
746 ca_chain, ca_crl,
747 ssl->conf->cert_profile,
748 ssl->hostname,
749 &verify_result,
750 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
751
752 if( ret != 0 )
753 {
754 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
755 }
756
757 /*
758 * Secondary checks: always done, but change 'ret' only if it was 0
759 */
760
761 #if defined(MBEDTLS_ECP_C)
762 {
763 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
764
765 /* If certificate uses an EC key, make sure the curve is OK */
766 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
767 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
768 {
769 verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
770
771 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( EC key curve )" ) );
772 if( ret == 0 )
773 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
774 }
775 }
776 #endif /* MBEDTLS_ECP_C */
777
778 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
779 ssl->handshake->ciphersuite_info,
780 !ssl->conf->endpoint,
781 &verify_result ) != 0 )
782 {
783 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( usage extensions )" ) );
784 if( ret == 0 )
785 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
786 }
787
788
789 if( ca_chain == NULL )
790 {
791 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
792 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
793 }
794
795 if( ret != 0 )
796 {
797 /* The certificate may have been rejected for several reasons.
798 Pick one and send the corresponding alert. Which alert to send
799 may be a subject of debate in some cases. */
800 if( verify_result & MBEDTLS_X509_BADCERT_OTHER )
801 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret );
802 else if( verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
803 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret );
804 else if( verify_result & ( MBEDTLS_X509_BADCERT_KEY_USAGE |
805 MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
806 MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
807 MBEDTLS_X509_BADCERT_BAD_PK |
808 MBEDTLS_X509_BADCERT_BAD_KEY ) )
809 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret );
810 else if( verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
811 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret );
812 else if( verify_result & MBEDTLS_X509_BADCERT_REVOKED )
813 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret );
814 else if( verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
815 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret );
816 else
817 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret );
818 }
819
820 #if defined(MBEDTLS_DEBUG_C)
821 if( verify_result != 0 )
822 {
823 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
824 (unsigned int) verify_result ) );
825 }
826 else
827 {
828 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
829 }
830 #endif /* MBEDTLS_DEBUG_C */
831
832 ssl->session_negotiate->verify_result = verify_result;
833 return( ret );
834 }
835 #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
ssl_tls13_validate_certificate(mbedtls_ssl_context * ssl)836 static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
837 {
838 ((void) ssl);
839 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
840 }
841 #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
842 #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
843
mbedtls_ssl_tls13_process_certificate(mbedtls_ssl_context * ssl)844 int mbedtls_ssl_tls13_process_certificate( mbedtls_ssl_context *ssl )
845 {
846 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
847 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
848
849 #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
850 unsigned char *buf;
851 size_t buf_len;
852
853 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
854 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
855 &buf, &buf_len ) );
856
857 /* Parse the certificate chain sent by the peer. */
858 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate( ssl, buf, buf + buf_len ) );
859 /* Validate the certificate chain and set the verification results. */
860 MBEDTLS_SSL_PROC_CHK( ssl_tls13_validate_certificate( ssl ) );
861
862 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
863 buf, buf_len );
864
865 cleanup:
866
867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
868 #else
869 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
870 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
871 #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
872 return( ret );
873 }
874
875 /*
876 *
877 * STATE HANDLING: Incoming Finished message.
878 */
879 /*
880 * Implementation
881 */
882
ssl_tls13_preprocess_finished_message(mbedtls_ssl_context * ssl)883 static int ssl_tls13_preprocess_finished_message( mbedtls_ssl_context *ssl )
884 {
885 int ret;
886
887 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
888 ssl->handshake->state_local.finished_in.digest,
889 sizeof( ssl->handshake->state_local.finished_in.digest ),
890 &ssl->handshake->state_local.finished_in.digest_len,
891 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
892 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT );
893 if( ret != 0 )
894 {
895 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_calculate_verify_data", ret );
896 return( ret );
897 }
898
899 return( 0 );
900 }
901
ssl_tls13_parse_finished_message(mbedtls_ssl_context * ssl,const unsigned char * buf,const unsigned char * end)902 static int ssl_tls13_parse_finished_message( mbedtls_ssl_context *ssl,
903 const unsigned char *buf,
904 const unsigned char *end )
905 {
906 /*
907 * struct {
908 * opaque verify_data[Hash.length];
909 * } Finished;
910 */
911 const unsigned char *expected_verify_data =
912 ssl->handshake->state_local.finished_in.digest;
913 size_t expected_verify_data_len =
914 ssl->handshake->state_local.finished_in.digest_len;
915 /* Structural validation */
916 if( (size_t)( end - buf ) != expected_verify_data_len )
917 {
918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
919
920 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
921 MBEDTLS_ERR_SSL_DECODE_ERROR );
922 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
923 }
924
925 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (self-computed):",
926 expected_verify_data,
927 expected_verify_data_len );
928 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (received message):", buf,
929 expected_verify_data_len );
930
931 /* Semantic validation */
932 if( mbedtls_ct_memcmp( buf,
933 expected_verify_data,
934 expected_verify_data_len ) != 0 )
935 {
936 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
937
938 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
939 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
940 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
941 }
942 return( 0 );
943 }
944
945 #if defined(MBEDTLS_SSL_CLI_C)
ssl_tls13_postprocess_server_finished_message(mbedtls_ssl_context * ssl)946 static int ssl_tls13_postprocess_server_finished_message( mbedtls_ssl_context *ssl )
947 {
948 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
949 mbedtls_ssl_key_set traffic_keys;
950 mbedtls_ssl_transform *transform_application = NULL;
951
952 ret = mbedtls_ssl_tls13_key_schedule_stage_application( ssl );
953 if( ret != 0 )
954 {
955 MBEDTLS_SSL_DEBUG_RET( 1,
956 "mbedtls_ssl_tls13_key_schedule_stage_application", ret );
957 goto cleanup;
958 }
959
960 ret = mbedtls_ssl_tls13_generate_application_keys( ssl, &traffic_keys );
961 if( ret != 0 )
962 {
963 MBEDTLS_SSL_DEBUG_RET( 1,
964 "mbedtls_ssl_tls13_generate_application_keys", ret );
965 goto cleanup;
966 }
967
968 transform_application =
969 mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
970 if( transform_application == NULL )
971 {
972 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
973 goto cleanup;
974 }
975
976 ret = mbedtls_ssl_tls13_populate_transform(
977 transform_application,
978 ssl->conf->endpoint,
979 ssl->session_negotiate->ciphersuite,
980 &traffic_keys,
981 ssl );
982 if( ret != 0 )
983 {
984 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
985 goto cleanup;
986 }
987
988 ssl->transform_application = transform_application;
989
990 cleanup:
991
992 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
993 if( ret != 0 )
994 {
995 mbedtls_free( transform_application );
996 MBEDTLS_SSL_PEND_FATAL_ALERT(
997 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
998 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
999 }
1000 return( ret );
1001 }
1002 #endif /* MBEDTLS_SSL_CLI_C */
1003
ssl_tls13_postprocess_finished_message(mbedtls_ssl_context * ssl)1004 static int ssl_tls13_postprocess_finished_message( mbedtls_ssl_context *ssl )
1005 {
1006
1007 #if defined(MBEDTLS_SSL_CLI_C)
1008 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
1009 {
1010 return( ssl_tls13_postprocess_server_finished_message( ssl ) );
1011 }
1012 #else
1013 ((void) ssl);
1014 #endif /* MBEDTLS_SSL_CLI_C */
1015
1016 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1017 }
1018
mbedtls_ssl_tls13_process_finished_message(mbedtls_ssl_context * ssl)1019 int mbedtls_ssl_tls13_process_finished_message( mbedtls_ssl_context *ssl )
1020 {
1021 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1022 unsigned char *buf;
1023 size_t buf_len;
1024
1025 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished message" ) );
1026
1027 /* Preprocessing step: Compute handshake digest */
1028 MBEDTLS_SSL_PROC_CHK( ssl_tls13_preprocess_finished_message( ssl ) );
1029
1030 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
1031 MBEDTLS_SSL_HS_FINISHED,
1032 &buf, &buf_len ) );
1033 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_finished_message( ssl, buf, buf + buf_len ) );
1034 mbedtls_ssl_tls13_add_hs_msg_to_checksum(
1035 ssl, MBEDTLS_SSL_HS_FINISHED, buf, buf_len );
1036 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_finished_message( ssl ) );
1037
1038 cleanup:
1039
1040 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished message" ) );
1041 return( ret );
1042 }
1043
1044 /*
1045 *
1046 * STATE HANDLING: Write and send Finished message.
1047 *
1048 */
1049 /*
1050 * Implement
1051 */
1052
ssl_tls13_prepare_finished_message(mbedtls_ssl_context * ssl)1053 static int ssl_tls13_prepare_finished_message( mbedtls_ssl_context *ssl )
1054 {
1055 int ret;
1056
1057 /* Compute transcript of handshake up to now. */
1058 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
1059 ssl->handshake->state_local.finished_out.digest,
1060 sizeof( ssl->handshake->state_local.finished_out.digest ),
1061 &ssl->handshake->state_local.finished_out.digest_len,
1062 ssl->conf->endpoint );
1063
1064 if( ret != 0 )
1065 {
1066 MBEDTLS_SSL_DEBUG_RET( 1, "calculate_verify_data failed", ret );
1067 return( ret );
1068 }
1069
1070 return( 0 );
1071 }
1072
ssl_tls13_finalize_finished_message(mbedtls_ssl_context * ssl)1073 static int ssl_tls13_finalize_finished_message( mbedtls_ssl_context *ssl )
1074 {
1075 // TODO: Add back resumption keys calculation after MVP.
1076 ((void) ssl);
1077
1078 return( 0 );
1079 }
1080
ssl_tls13_write_finished_message_body(mbedtls_ssl_context * ssl,unsigned char * buf,unsigned char * end,size_t * out_len)1081 static int ssl_tls13_write_finished_message_body( mbedtls_ssl_context *ssl,
1082 unsigned char *buf,
1083 unsigned char *end,
1084 size_t *out_len )
1085 {
1086 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
1087 /*
1088 * struct {
1089 * opaque verify_data[Hash.length];
1090 * } Finished;
1091 */
1092 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, verify_data_len );
1093
1094 memcpy( buf, ssl->handshake->state_local.finished_out.digest,
1095 verify_data_len );
1096
1097 *out_len = verify_data_len;
1098 return( 0 );
1099 }
1100
1101 /* Main entry point: orchestrates the other functions */
mbedtls_ssl_tls13_write_finished_message(mbedtls_ssl_context * ssl)1102 int mbedtls_ssl_tls13_write_finished_message( mbedtls_ssl_context *ssl )
1103 {
1104 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1105 unsigned char *buf;
1106 size_t buf_len, msg_len;
1107
1108 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished message" ) );
1109
1110 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_finished_message( ssl ) );
1111
1112 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg( ssl,
1113 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len ) );
1114
1115 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_finished_message_body(
1116 ssl, buf, buf + buf_len, &msg_len ) );
1117
1118 mbedtls_ssl_tls13_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1119 buf, msg_len );
1120
1121 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_finished_message( ssl ) );
1122 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
1123 buf_len, msg_len ) );
1124 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_flush_output( ssl ) );
1125
1126 cleanup:
1127
1128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished message" ) );
1129 return( ret );
1130 }
1131
mbedtls_ssl_tls13_handshake_wrapup(mbedtls_ssl_context * ssl)1132 void mbedtls_ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
1133 {
1134
1135 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
1136
1137 /*
1138 * Free the previous session and switch to the current one.
1139 */
1140 if( ssl->session )
1141 {
1142 mbedtls_ssl_session_free( ssl->session );
1143 mbedtls_free( ssl->session );
1144 }
1145 ssl->session = ssl->session_negotiate;
1146 ssl->session_negotiate = NULL;
1147
1148 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
1149 }
1150
1151 /*
1152 *
1153 * STATE HANDLING: Write ChangeCipherSpec
1154 *
1155 */
1156 #if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1157
ssl_tls13_write_change_cipher_spec_body(mbedtls_ssl_context * ssl,unsigned char * buf,unsigned char * end,size_t * olen)1158 static int ssl_tls13_write_change_cipher_spec_body( mbedtls_ssl_context *ssl,
1159 unsigned char *buf,
1160 unsigned char *end,
1161 size_t *olen )
1162 {
1163 ((void) ssl);
1164
1165 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 1 );
1166 buf[0] = 1;
1167 *olen = 1;
1168
1169 return( 0 );
1170 }
1171
mbedtls_ssl_tls13_write_change_cipher_spec(mbedtls_ssl_context * ssl)1172 int mbedtls_ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1173 {
1174 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1175
1176 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
1177
1178 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_flush_output( ssl ) );
1179
1180 /* Write CCS message */
1181 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_change_cipher_spec_body(
1182 ssl, ssl->out_msg,
1183 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1184 &ssl->out_msglen ) );
1185
1186 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1187
1188 /* Dispatch message */
1189 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_record( ssl, 1 ) );
1190
1191 cleanup:
1192
1193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
1194 return( ret );
1195 }
1196
1197 #endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1198
1199 #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1200
1201 #endif /* MBEDTLS_SSL_TLS_C */
1202