Debian Codegraph @

crt/s2n/tls/extensions/s2n_client_supported_groups.c

raw symbols SCIP
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 "tls/extensions/s2n_client_supported_groups.h"
17
18 #include <stdint.h>
19
20 #include "crypto/s2n_pq.h"
21 #include "tls/extensions/s2n_ec_point_format.h"
22 #include "tls/s2n_security_policies.h"
23 #include "tls/s2n_tls.h"
24 #include "tls/s2n_tls13.h"
25 #include "tls/s2n_tls_parameters.h"
26 #include "utils/s2n_safety.h"
27
28 static int s2n_client_supported_groups_send(struct s2n_connection *conn, struct s2n_stuffer *out);
29 static int s2n_client_supported_groups_recv(struct s2n_connection *conn, struct s2n_stuffer *extension);
30
31 const s2n_extension_type s2n_client_supported_groups_extension = {
32 .iana_value = TLS_EXTENSION_SUPPORTED_GROUPS,
33 .is_response = false,
34 .send = s2n_client_supported_groups_send,
35 .recv = s2n_client_supported_groups_recv,
36 .should_send = s2n_extension_should_send_if_ecc_enabled,
37 .if_missing = s2n_extension_noop_if_missing,
38 };
39
40 bool s2n_extension_should_send_if_ecc_enabled(struct s2n_connection *conn)
41 {
42 const struct s2n_security_policy *security_policy = NULL;
43 return s2n_connection_get_security_policy(conn, &security_policy) == S2N_SUCCESS
44 && s2n_ecc_is_extension_required(security_policy);
45 }
46
47 static int s2n_client_supported_groups_send(struct s2n_connection *conn, struct s2n_stuffer *out)
48 {
49 POSIX_ENSURE_REF(conn);
50
51 const struct s2n_ecc_preferences *ecc_pref = NULL;
52 POSIX_GUARD(s2n_connection_get_ecc_preferences(conn, &ecc_pref));
53 POSIX_ENSURE_REF(ecc_pref);
54
55 const struct s2n_kem_preferences *kem_pref = NULL;
56 POSIX_GUARD(s2n_connection_get_kem_preferences(conn, &kem_pref));
57 POSIX_ENSURE_REF(kem_pref);
58
59 /* Group list len */
60 struct s2n_stuffer_reservation group_list_len = { 0 };
61 POSIX_GUARD(s2n_stuffer_reserve_uint16(out, &group_list_len));
62
63 /* Send KEM groups list first */
64 if (s2n_connection_get_protocol_version(conn) >= S2N_TLS13 && s2n_pq_is_enabled()) {
65 for (size_t i = 0; i < kem_pref->tls13_kem_group_count; i++) {
66 if (!s2n_kem_group_is_available(kem_pref->tls13_kem_groups[i])) {
67 continue;
68 }
69 POSIX_GUARD(s2n_stuffer_write_uint16(out, kem_pref->tls13_kem_groups[i]->iana_id));
70 }
71 }
72
73 /* Then send curve list */
74 for (size_t i = 0; i < ecc_pref->count; i++) {
75 POSIX_GUARD(s2n_stuffer_write_uint16(out, ecc_pref->ecc_curves[i]->iana_id));
76 }
77
78 POSIX_GUARD(s2n_stuffer_write_vector_size(&group_list_len));
79
80 return S2N_SUCCESS;
81 }
82
83 S2N_RESULT s2n_supported_groups_parse_count(struct s2n_stuffer *extension, uint16_t *count)
84 {
85 RESULT_ENSURE_REF(count);
86 *count = 0;
87 RESULT_ENSURE_REF(extension);
88
89 uint16_t supported_groups_list_size = 0;
90 RESULT_GUARD_POSIX(s2n_stuffer_read_uint16(extension, &supported_groups_list_size));
91
92 RESULT_ENSURE(supported_groups_list_size <= s2n_stuffer_data_available(extension),
93 S2N_ERR_INVALID_PARSED_EXTENSIONS);
94 RESULT_ENSURE(supported_groups_list_size % S2N_SUPPORTED_GROUP_SIZE == 0,
95 S2N_ERR_INVALID_PARSED_EXTENSIONS);
96
97 *count = supported_groups_list_size / S2N_SUPPORTED_GROUP_SIZE;
98
99 return S2N_RESULT_OK;
100 }
101
102 /* Populates the appropriate index of either the mutually_supported_curves or
103 * mutually_supported_kem_groups array based on the received IANA ID. Will
104 * ignore unrecognized IANA IDs (and return success). */
105 static int s2n_client_supported_groups_recv_iana_id(struct s2n_connection *conn, uint16_t iana_id)
106 {
107 POSIX_ENSURE_REF(conn);
108
109 const struct s2n_ecc_preferences *ecc_pref = NULL;
110 POSIX_GUARD(s2n_connection_get_ecc_preferences(conn, &ecc_pref));
111 POSIX_ENSURE_REF(ecc_pref);
112
113 for (size_t i = 0; i < ecc_pref->count; i++) {
114 const struct s2n_ecc_named_curve *supported_curve = ecc_pref->ecc_curves[i];
115 if (iana_id == supported_curve->iana_id) {
116 conn->kex_params.mutually_supported_curves[i] = supported_curve;
117 return S2N_SUCCESS;
118 }
119 }
120
121 /* Return early if PQ is disabled, or if TLS version is less than 1.3, so as to ignore PQ IDs */
122 if (!s2n_pq_is_enabled() || s2n_connection_get_protocol_version(conn) < S2N_TLS13) {
123 return S2N_SUCCESS;
124 }
125
126 const struct s2n_kem_preferences *kem_pref = NULL;
127 POSIX_GUARD(s2n_connection_get_kem_preferences(conn, &kem_pref));
128 POSIX_ENSURE_REF(kem_pref);
129
130 for (size_t i = 0; i < kem_pref->tls13_kem_group_count; i++) {
131 const struct s2n_kem_group *supported_kem_group = kem_pref->tls13_kem_groups[i];
132 if (s2n_kem_group_is_available(supported_kem_group) && iana_id == supported_kem_group->iana_id) {
133 conn->kex_params.mutually_supported_kem_groups[i] = supported_kem_group;
134 return S2N_SUCCESS;
135 }
136 }
137
138 return S2N_SUCCESS;
139 }
140
141 static int s2n_choose_supported_group(struct s2n_connection *conn)
142 {
143 POSIX_ENSURE_REF(conn);
144
145 const struct s2n_ecc_preferences *ecc_pref = NULL;
146 POSIX_GUARD(s2n_connection_get_ecc_preferences(conn, &ecc_pref));
147 POSIX_ENSURE_REF(ecc_pref);
148
149 const struct s2n_kem_preferences *kem_pref = NULL;
150 POSIX_GUARD(s2n_connection_get_kem_preferences(conn, &kem_pref));
151 POSIX_ENSURE_REF(kem_pref);
152
153 /* Ensure that only the intended group will be non-NULL (if no group is chosen, everything
154 * should be NULL). */
155 conn->kex_params.server_kem_group_params.kem_group = NULL;
156 conn->kex_params.server_kem_group_params.ecc_params.negotiated_curve = NULL;
157 conn->kex_params.server_kem_group_params.kem_params.kem = NULL;
158 conn->kex_params.server_ecc_evp_params.negotiated_curve = NULL;
159
160 /* Prefer to negotiate hybrid PQ over ECC. If PQ is disabled, we will never choose a
161 * PQ group because the mutually_supported_kem_groups array will not have been
162 * populated with anything. */
163 for (size_t i = 0; i < kem_pref->tls13_kem_group_count; i++) {
164 const struct s2n_kem_group *candidate_kem_group = conn->kex_params.mutually_supported_kem_groups[i];
165 if (candidate_kem_group != NULL && s2n_kem_group_is_available(candidate_kem_group)) {
166 conn->kex_params.server_kem_group_params.kem_group = candidate_kem_group;
167 conn->kex_params.server_kem_group_params.ecc_params.negotiated_curve = candidate_kem_group->curve;
168 conn->kex_params.server_kem_group_params.kem_params.kem = candidate_kem_group->kem;
169 return S2N_SUCCESS;
170 }
171 }
172
173 for (size_t i = 0; i < ecc_pref->count; i++) {
174 const struct s2n_ecc_named_curve *candidate_curve = conn->kex_params.mutually_supported_curves[i];
175 if (candidate_curve != NULL) {
176 conn->kex_params.server_ecc_evp_params.negotiated_curve = candidate_curve;
177 return S2N_SUCCESS;
178 }
179 }
180
181 return S2N_SUCCESS;
182 }
183
184 static int s2n_client_supported_groups_recv(struct s2n_connection *conn, struct s2n_stuffer *extension)
185 {
186 POSIX_ENSURE_REF(conn);
187 POSIX_ENSURE_REF(extension);
188
189 uint16_t supported_groups_count = 0;
190 if (s2n_result_is_error(s2n_supported_groups_parse_count(extension, &supported_groups_count))) {
191 /* Malformed length, ignore the extension */
192 return S2N_SUCCESS;
193 }
194
195 for (size_t i = 0; i < supported_groups_count; i++) {
196 uint16_t iana_id = 0;
197 POSIX_GUARD(s2n_stuffer_read_uint16(extension, &iana_id));
198 POSIX_GUARD(s2n_client_supported_groups_recv_iana_id(conn, iana_id));
199 }
200
201 POSIX_GUARD(s2n_choose_supported_group(conn));
202
203 return S2N_SUCCESS;
204 }
205
206 /* Old-style extension functions -- remove after extensions refactor is complete */
207
208 int s2n_recv_client_supported_groups(struct s2n_connection *conn, struct s2n_stuffer *extension)
209 {
210 return s2n_extension_recv(&s2n_client_supported_groups_extension, conn, extension);
211 }