Jerry Yu | 65dd2cc | 2021-08-18 16:38:40 +0800 | [diff] [blame] | 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_EXPERIMENTAL) |
| 25 | |
Jerry Yu | c8a392c | 2021-08-18 16:46:28 +0800 | [diff] [blame] | 26 | #include "mbedtls/error.h" |
Jerry Yu | 7533635 | 2021-09-01 15:59:36 +0800 | [diff] [blame^] | 27 | #include "mbedtls/debug.h" |
Jerry Yu | c8a392c | 2021-08-18 16:46:28 +0800 | [diff] [blame] | 28 | |
Jerry Yu | 65dd2cc | 2021-08-18 16:38:40 +0800 | [diff] [blame] | 29 | #include "ssl_misc.h" |
| 30 | |
Jerry Yu | 7533635 | 2021-09-01 15:59:36 +0800 | [diff] [blame^] | 31 | |
Jerry Yu | f443681 | 2021-08-26 22:59:56 +0800 | [diff] [blame] | 32 | int mbedtls_ssl_tls13_start_handshake_msg( mbedtls_ssl_context *ssl, |
Jerry Yu | eecfbf0 | 2021-08-30 18:32:07 +0800 | [diff] [blame] | 33 | unsigned hs_type, |
| 34 | unsigned char **buf, |
Jerry Yu | 0c63af6 | 2021-09-02 12:59:12 +0800 | [diff] [blame] | 35 | size_t *buf_len ) |
Jerry Yu | 65dd2cc | 2021-08-18 16:38:40 +0800 | [diff] [blame] | 36 | { |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 37 | /* |
| 38 | * Reserve 4 bytes for hanshake header. ( Section 4,RFC 8446 ) |
| 39 | * ... |
| 40 | * HandshakeType msg_type; |
| 41 | * uint24 length; |
| 42 | * ... |
| 43 | */ |
Jerry Yu | c8a392c | 2021-08-18 16:46:28 +0800 | [diff] [blame] | 44 | *buf = ssl->out_msg + 4; |
Jerry Yu | 0c63af6 | 2021-09-02 12:59:12 +0800 | [diff] [blame] | 45 | *buf_len = MBEDTLS_SSL_OUT_CONTENT_LEN - 4; |
Jerry Yu | c8a392c | 2021-08-18 16:46:28 +0800 | [diff] [blame] | 46 | |
| 47 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 48 | ssl->out_msg[0] = hs_type; |
| 49 | |
| 50 | return( 0 ); |
Jerry Yu | 65dd2cc | 2021-08-18 16:38:40 +0800 | [diff] [blame] | 51 | } |
| 52 | |
Jerry Yu | f443681 | 2021-08-26 22:59:56 +0800 | [diff] [blame] | 53 | int mbedtls_ssl_tls13_finish_handshake_msg( mbedtls_ssl_context *ssl, |
Jerry Yu | eecfbf0 | 2021-08-30 18:32:07 +0800 | [diff] [blame] | 54 | size_t buf_len, |
| 55 | size_t msg_len ) |
Jerry Yu | 65dd2cc | 2021-08-18 16:38:40 +0800 | [diff] [blame] | 56 | { |
Jerry Yu | c8a392c | 2021-08-18 16:46:28 +0800 | [diff] [blame] | 57 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | dbfb7bd | 2021-09-04 09:58:58 +0800 | [diff] [blame] | 58 | size_t msg_len_with_header; |
Jerry Yu | 65dd2cc | 2021-08-18 16:38:40 +0800 | [diff] [blame] | 59 | ((void) buf_len); |
Jerry Yu | c8a392c | 2021-08-18 16:46:28 +0800 | [diff] [blame] | 60 | |
Jerry Yu | 1bc2c1f | 2021-09-01 12:57:29 +0800 | [diff] [blame] | 61 | /* Add reserved 4 bytes for handshake header */ |
Jerry Yu | dbfb7bd | 2021-09-04 09:58:58 +0800 | [diff] [blame] | 62 | msg_len_with_header = msg_len + 4; |
| 63 | ssl->out_msglen = msg_len_with_header; |
Jerry Yu | 2c0fbf3 | 2021-09-02 13:53:46 +0800 | [diff] [blame] | 64 | MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_handshake_msg_ext( ssl, 0 ) ); |
Jerry Yu | c8a392c | 2021-08-18 16:46:28 +0800 | [diff] [blame] | 65 | |
| 66 | cleanup: |
| 67 | return( ret ); |
Jerry Yu | 65dd2cc | 2021-08-18 16:38:40 +0800 | [diff] [blame] | 68 | } |
| 69 | |
Jerry Yu | f443681 | 2021-08-26 22:59:56 +0800 | [diff] [blame] | 70 | void mbedtls_ssl_tls13_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl, |
Jerry Yu | eecfbf0 | 2021-08-30 18:32:07 +0800 | [diff] [blame] | 71 | unsigned hs_type, |
| 72 | size_t total_hs_len ) |
Jerry Yu | 65dd2cc | 2021-08-18 16:38:40 +0800 | [diff] [blame] | 73 | { |
| 74 | unsigned char hs_hdr[4]; |
| 75 | |
| 76 | /* Build HS header for checksum update. */ |
Jerry Yu | 2ac6419 | 2021-08-26 18:38:58 +0800 | [diff] [blame] | 77 | hs_hdr[0] = MBEDTLS_BYTE_0( hs_type ); |
| 78 | hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len ); |
| 79 | hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len ); |
| 80 | hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len ); |
Jerry Yu | 65dd2cc | 2021-08-18 16:38:40 +0800 | [diff] [blame] | 81 | |
| 82 | ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) ); |
| 83 | } |
| 84 | |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 85 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 86 | |
| 87 | /* |
Jerry Yu | e41dec0 | 2021-08-31 10:57:07 +0800 | [diff] [blame] | 88 | * mbedtls_ssl_tls13_write_sig_alg_ext( ) |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 89 | * |
| 90 | * enum { |
| 91 | * .... |
| 92 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 93 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 94 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 95 | * .... |
| 96 | * } SignatureScheme; |
| 97 | * |
| 98 | * struct { |
| 99 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 100 | * } SignatureSchemeList; |
| 101 | * |
| 102 | * Only if we handle at least one key exchange that needs signatures. |
| 103 | */ |
| 104 | |
Jerry Yu | e41dec0 | 2021-08-31 10:57:07 +0800 | [diff] [blame] | 105 | int mbedtls_ssl_tls13_write_sig_alg_ext( mbedtls_ssl_context *ssl, |
| 106 | unsigned char *buf, |
| 107 | unsigned char *end, |
| 108 | size_t *olen ) |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 109 | { |
| 110 | ((void) ssl); |
| 111 | ((void) buf); |
| 112 | ((void) end); |
Jerry Yu | 7533635 | 2021-09-01 15:59:36 +0800 | [diff] [blame^] | 113 | *olen = 0; |
| 114 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "signature_algorithm extension is not available" ) ); |
| 115 | return( 0 ); |
Jerry Yu | bc20bdd | 2021-08-24 15:59:48 +0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 119 | |
Jerry Yu | 65dd2cc | 2021-08-18 16:38:40 +0800 | [diff] [blame] | 120 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
| 121 | |
| 122 | #endif /* MBEDTLS_SSL_TLS_C */ |