1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #include "api/s2n.h"
17 #include "tls/s2n_tls.h"
18 #include "tls/s2n_tls13.h"
19 #include "crypto/s2n_rsa_pss.h"
20 #include "crypto/s2n_rsa_signing.h"
21 
22 bool s2n_use_default_tls13_config_flag = false;
23 
s2n_use_default_tls13_config()24 bool s2n_use_default_tls13_config()
25 {
26     return s2n_use_default_tls13_config_flag;
27 }
28 
s2n_is_tls13_fully_supported()29 bool s2n_is_tls13_fully_supported() {
30     /* Older versions of Openssl (eg 1.0.2) do not support RSA PSS, which is required for TLS 1.3. */
31     return s2n_is_rsa_pss_signing_supported() && s2n_is_rsa_pss_certs_supported();
32 }
33 
s2n_get_highest_fully_supported_tls_version()34 int s2n_get_highest_fully_supported_tls_version() {
35     return s2n_is_tls13_fully_supported() ? S2N_TLS13 : S2N_TLS12;
36 }
37 
38 /* Allow TLS1.3 to be negotiated, and use the default TLS1.3 security policy.
39  * This is NOT the default behavior, and this method is deprecated.
40  *
41  * Please consider using the default behavior and configuring
42  * TLS1.2/TLS1.3 via explicit security policy instead.
43  */
s2n_enable_tls13()44 int s2n_enable_tls13()
45 {
46     s2n_highest_protocol_version = S2N_TLS13;
47     s2n_use_default_tls13_config_flag = true;
48     return S2N_SUCCESS;
49 }
50 
51 /* Do NOT allow TLS1.3 to be negotiated, regardless of security policy.
52  * This is NOT the default behavior, and this method is deprecated.
53  *
54  * Please consider using the default behavior and configuring
55  * TLS1.2/TLS1.3 via explicit security policy instead.
56  */
s2n_disable_tls13()57 int s2n_disable_tls13()
58 {
59     POSIX_ENSURE(s2n_in_unit_test(), S2N_ERR_NOT_IN_UNIT_TEST);
60     s2n_highest_protocol_version = S2N_TLS12;
61     s2n_use_default_tls13_config_flag = false;
62     return S2N_SUCCESS;
63 }
64 
65 /* Reset S2N to the default protocol version behavior.
66  *
67  * This method is intended for use in existing unit tests when the APIs
68  * to enable/disable TLS1.3 have already been called.
69  */
s2n_reset_tls13()70 int s2n_reset_tls13()
71 {
72     POSIX_ENSURE(s2n_in_unit_test(), S2N_ERR_NOT_IN_UNIT_TEST);
73     s2n_highest_protocol_version = S2N_TLS13;
74     s2n_use_default_tls13_config_flag = false;
75     return S2N_SUCCESS;
76 }
77 
78 /* Returns whether a uint16 iana value is a valid TLS 1.3 cipher suite */
s2n_is_valid_tls13_cipher(const uint8_t version[2])79 bool s2n_is_valid_tls13_cipher(const uint8_t version[2]) {
80     /* Valid TLS 1.3 Ciphers are
81      * 0x1301, 0x1302, 0x1303, 0x1304, 0x1305.
82      * (https://tools.ietf.org/html/rfc8446#appendix-B.4)
83      */
84     return version[0] == 0x13 && version[1] >= 0x01 && version[1] <= 0x05;
85 }
86 
87 /* Use middlebox compatibility mode for TLS1.3 by default.
88  * For now, only disable it when QUIC support is enabled.
89  */
s2n_is_middlebox_compat_enabled(struct s2n_connection * conn)90 bool s2n_is_middlebox_compat_enabled(struct s2n_connection *conn)
91 {
92     return s2n_connection_get_protocol_version(conn) >= S2N_TLS13
93             && !s2n_connection_is_quic_enabled(conn);
94 }
95