Debian Codegraph @

crt/s2n/tls/extensions/s2n_server_key_share.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_server_key_share.h"
17
18 #include "crypto/s2n_pq.h"
19 #include "tls/s2n_security_policies.h"
20 #include "tls/s2n_supported_group_preferences.h"
21 #include "tls/s2n_tls.h"
22 #include "tls/s2n_tls13.h"
23 #include "utils/s2n_safety.h"
24
25 static int s2n_server_key_share_send(struct s2n_connection *conn, struct s2n_stuffer *out);
26 static int s2n_server_key_share_recv(struct s2n_connection *conn, struct s2n_stuffer *extension);
27
28 const s2n_extension_type s2n_server_key_share_extension = {
29 .iana_value = TLS_EXTENSION_KEY_SHARE,
30 .minimum_version = S2N_TLS13,
31 .is_response = true,
32 .send = s2n_server_key_share_send,
33 .recv = s2n_server_key_share_recv,
34 .should_send = s2n_extension_always_send,
35 .if_missing = s2n_extension_noop_if_missing,
36 };
37
38 static int s2n_server_key_share_send_hybrid_partial_ecc(struct s2n_connection *conn, struct s2n_stuffer *out)
39 {
40 POSIX_ENSURE_REF(conn);
41 POSIX_ENSURE_REF(out);
42
43 struct s2n_kem_group_params *server_kem_group_params = &conn->kex_params.server_kem_group_params;
44 struct s2n_kem_params *client_kem_params = &conn->kex_params.client_kem_group_params.kem_params;
45
46 struct s2n_ecc_evp_params *server_ecc_params = &server_kem_group_params->ecc_params;
47 POSIX_ENSURE_REF(server_ecc_params->negotiated_curve);
48 if (client_kem_params->len_prefixed) {
49 POSIX_GUARD(s2n_stuffer_write_uint16(out, server_ecc_params->negotiated_curve->share_size));
50 }
51 POSIX_GUARD(s2n_ecc_evp_generate_ephemeral_key(server_ecc_params));
52 POSIX_GUARD(s2n_ecc_evp_write_params_point(server_ecc_params, out));
53
54 return S2N_SUCCESS;
55 }
56
57 static int s2n_server_key_share_generate_pq(struct s2n_connection *conn, struct s2n_stuffer *out)
58 {
59 POSIX_ENSURE_REF(out);
60 POSIX_ENSURE_REF(conn);
61
62 POSIX_ENSURE(s2n_pq_is_enabled(), S2N_ERR_UNIMPLEMENTED);
63
64 struct s2n_kem_group_params *server_kem_group_params = &conn->kex_params.server_kem_group_params;
65 struct s2n_kem_params *client_kem_params = &conn->kex_params.client_kem_group_params.kem_params;
66 POSIX_ENSURE_REF(client_kem_params->public_key.data);
67
68 POSIX_ENSURE_REF(server_kem_group_params->kem_group);
69 POSIX_GUARD(s2n_stuffer_write_uint16(out, server_kem_group_params->kem_group->iana_id));
70
71 struct s2n_stuffer_reservation total_share_size = { 0 };
72 POSIX_GUARD(s2n_stuffer_reserve_uint16(out, &total_share_size));
73
74 /* s2n_kem_send_ciphertext() will generate the PQ shared secret and use
75 * the client's public key to encapsulate; the PQ shared secret will be
76 * stored in client_kem_params, and will be used during the shared
77 * secret derivation. */
78 if (server_kem_group_params->kem_group->curve == &s2n_ecc_curve_none) { /* Pure PQ */
79 POSIX_GUARD(s2n_kem_send_ciphertext(out, client_kem_params));
80 } else { /* Hybrid PQ */
81 if (server_kem_group_params->kem_group->send_kem_first) {
82 POSIX_GUARD(s2n_kem_send_ciphertext(out, client_kem_params));
83 POSIX_GUARD(s2n_server_key_share_send_hybrid_partial_ecc(conn, out));
84 } else {
85 POSIX_GUARD(s2n_server_key_share_send_hybrid_partial_ecc(conn, out));
86 POSIX_GUARD(s2n_kem_send_ciphertext(out, client_kem_params));
87 }
88 }
89
90 POSIX_GUARD(s2n_stuffer_write_vector_size(&total_share_size));
91 return S2N_SUCCESS;
92 }
93
94 /* Check that client has sent a corresponding key share for the server's KEM group */
95 int s2n_server_key_share_send_check_pq(struct s2n_connection *conn)
96 {
97 POSIX_ENSURE_REF(conn);
98
99 POSIX_ENSURE(s2n_pq_is_enabled(), S2N_ERR_UNIMPLEMENTED);
100
101 POSIX_ENSURE_REF(conn->kex_params.server_kem_group_params.kem_group);
102 POSIX_ENSURE_REF(conn->kex_params.server_kem_group_params.kem_params.kem);
103
104 const struct s2n_kem_preferences *kem_pref = NULL;
105 POSIX_GUARD(s2n_connection_get_kem_preferences(conn, &kem_pref));
106 POSIX_ENSURE_REF(kem_pref);
107
108 const struct s2n_kem_group *server_kem_group = conn->kex_params.server_kem_group_params.kem_group;
109 POSIX_ENSURE(s2n_kem_preferences_includes_tls13_kem_group(kem_pref, server_kem_group->iana_id),
110 S2N_ERR_KEM_UNSUPPORTED_PARAMS);
111
112 struct s2n_kem_group_params *client_params = &conn->kex_params.client_kem_group_params;
113 POSIX_ENSURE(client_params->kem_group == server_kem_group, S2N_ERR_BAD_KEY_SHARE);
114
115 POSIX_ENSURE(client_params->kem_params.kem == server_kem_group->kem, S2N_ERR_BAD_KEY_SHARE);
116 POSIX_ENSURE(client_params->kem_params.public_key.size == server_kem_group->kem->public_key_length, S2N_ERR_BAD_KEY_SHARE);
117 POSIX_ENSURE(client_params->kem_params.public_key.data != NULL, S2N_ERR_BAD_KEY_SHARE);
118
119 return S2N_SUCCESS;
120 }
121
122 /* Check that client has sent a corresponding key share for the server's EC curve */
123 int s2n_server_key_share_send_check_ecdhe(struct s2n_connection *conn)
124 {
125 POSIX_ENSURE_REF(conn);
126
127 const struct s2n_ecc_preferences *ecc_pref = NULL;
128 POSIX_GUARD(s2n_connection_get_ecc_preferences(conn, &ecc_pref));
129 POSIX_ENSURE_REF(ecc_pref);
130
131 const struct s2n_ecc_named_curve *server_curve = conn->kex_params.server_ecc_evp_params.negotiated_curve;
132 POSIX_ENSURE_REF(server_curve);
133
134 struct s2n_ecc_evp_params *client_params = &conn->kex_params.client_ecc_evp_params;
135 POSIX_ENSURE(client_params->negotiated_curve == server_curve, S2N_ERR_BAD_KEY_SHARE);
136 POSIX_ENSURE(client_params->evp_pkey != NULL, S2N_ERR_BAD_KEY_SHARE);
137
138 return S2N_SUCCESS;
139 }
140
141 static int s2n_server_key_share_send(struct s2n_connection *conn, struct s2n_stuffer *out)
142 {
143 POSIX_ENSURE_REF(conn);
144 POSIX_ENSURE_REF(out);
145
146 const struct s2n_ecc_named_curve *curve = conn->kex_params.server_ecc_evp_params.negotiated_curve;
147 const struct s2n_kem_group *kem_group = conn->kex_params.server_kem_group_params.kem_group;
148
149 /* Boolean XOR: exactly one of {server_curve, server_kem_group} should be non-null. */
150 POSIX_ENSURE((curve == NULL) != (kem_group == NULL), S2N_ERR_INVALID_SUPPORTED_GROUP_STATE);
151
152 /* Retry requests only require the selected named group, not an actual share.
153 * https://tools.ietf.org/html/rfc8446#section-4.2.8 */
154 if (s2n_is_hello_retry_message(conn)) {
155 uint16_t named_group_id = 0;
156 if (curve != NULL) {
157 named_group_id = curve->iana_id;
158 } else {
159 named_group_id = kem_group->iana_id;
160 }
161
162 POSIX_GUARD(s2n_stuffer_write_uint16(out, named_group_id));
163 return S2N_SUCCESS;
164 }
165
166 if (curve != NULL) {
167 POSIX_GUARD(s2n_server_key_share_send_check_ecdhe(conn));
168 POSIX_GUARD(s2n_ecdhe_parameters_send(&conn->kex_params.server_ecc_evp_params, out));
169 } else {
170 POSIX_GUARD(s2n_server_key_share_send_check_pq(conn));
171 POSIX_GUARD(s2n_server_key_share_generate_pq(conn, out));
172 }
173
174 return S2N_SUCCESS;
175 }
176
177 static int s2n_server_key_share_recv_hybrid_partial_ecc(struct s2n_connection *conn, struct s2n_stuffer *extension)
178 {
179 POSIX_ENSURE_REF(conn);
180 POSIX_ENSURE_REF(extension);
181
182 struct s2n_kem_params *client_kem_params = &conn->kex_params.client_kem_group_params.kem_params;
183 struct s2n_kem_group_params *server_kem_group_params = &conn->kex_params.server_kem_group_params;
184 const struct s2n_kem_group *server_kem_group = server_kem_group_params->kem_group;
185 POSIX_ENSURE_REF(server_kem_group);
186 uint16_t expected_ecc_share_size = server_kem_group->curve->share_size;
187
188 /* Parse ECC key share */
189 if (client_kem_params->len_prefixed) {
190 uint16_t actual_ecc_share_size = 0;
191 POSIX_GUARD(s2n_stuffer_read_uint16(extension, &actual_ecc_share_size));
192 POSIX_ENSURE(actual_ecc_share_size == expected_ecc_share_size, S2N_ERR_BAD_KEY_SHARE);
193 }
194
195 struct s2n_blob point_blob = { 0 };
196 POSIX_ENSURE(s2n_ecc_evp_read_params_point(extension, expected_ecc_share_size, &point_blob) == S2N_SUCCESS, S2N_ERR_BAD_KEY_SHARE);
197 POSIX_ENSURE(s2n_ecc_evp_parse_params_point(&point_blob, &server_kem_group_params->ecc_params) == S2N_SUCCESS, S2N_ERR_BAD_KEY_SHARE);
198 POSIX_ENSURE(server_kem_group_params->ecc_params.evp_pkey != NULL, S2N_ERR_BAD_KEY_SHARE);
199
200 return S2N_SUCCESS;
201 }
202
203 static int s2n_server_key_share_recv_pq(struct s2n_connection *conn, uint16_t named_group_iana,
204 struct s2n_stuffer *extension)
205 {
206 POSIX_ENSURE_REF(conn);
207 POSIX_ENSURE_REF(extension);
208
209 /* If PQ is disabled, the client should not have sent any PQ IDs
210 * in the supported_groups list of the initial ClientHello */
211 POSIX_ENSURE(s2n_pq_is_enabled(), S2N_ERR_ECDHE_UNSUPPORTED_CURVE);
212
213 const struct s2n_kem_preferences *kem_pref = NULL;
214 POSIX_GUARD(s2n_connection_get_kem_preferences(conn, &kem_pref));
215 POSIX_ENSURE_REF(kem_pref);
216
217 /* This check should have been done higher up, but including it here as well for extra defense.
218 * Uses S2N_ERR_ECDHE_UNSUPPORTED_CURVE for backward compatibility. */
219 POSIX_ENSURE(s2n_kem_preferences_includes_tls13_kem_group(kem_pref, named_group_iana), S2N_ERR_ECDHE_UNSUPPORTED_CURVE);
220
221 size_t kem_group_index = 0;
222 for (size_t i = 0; i < kem_pref->tls13_kem_group_count; i++) {
223 if (named_group_iana == kem_pref->tls13_kem_groups[i]->iana_id
224 && s2n_kem_group_is_available(kem_pref->tls13_kem_groups[i])) {
225 kem_group_index = i;
226 break;
227 }
228 }
229
230 struct s2n_kem_group_params *server_kem_group_params = &conn->kex_params.server_kem_group_params;
231 server_kem_group_params->kem_group = kem_pref->tls13_kem_groups[kem_group_index];
232 server_kem_group_params->kem_params.kem = kem_pref->tls13_kem_groups[kem_group_index]->kem;
233 server_kem_group_params->ecc_params.negotiated_curve = kem_pref->tls13_kem_groups[kem_group_index]->curve;
234
235 /* If this a HRR, the server will only have sent the named group ID. We assign the
236 * appropriate KEM group params above, then exit early so that the client can
237 * generate the correct key share. */
238 if (s2n_is_hello_retry_message(conn)) {
239 return S2N_SUCCESS;
240 }
241
242 /* Ensure that the server's key share corresponds with a key share previously sent by the client */
243 struct s2n_kem_group_params *client_kem_group_params = &conn->kex_params.client_kem_group_params;
244 POSIX_ENSURE(client_kem_group_params->kem_params.private_key.data, S2N_ERR_BAD_KEY_SHARE);
245 POSIX_ENSURE(client_kem_group_params->kem_group == server_kem_group_params->kem_group, S2N_ERR_BAD_KEY_SHARE);
246
247 uint16_t actual_hybrid_share_size = 0;
248 POSIX_GUARD(s2n_stuffer_read_uint16(extension, &actual_hybrid_share_size));
249 POSIX_ENSURE(s2n_stuffer_data_available(extension) == actual_hybrid_share_size, S2N_ERR_BAD_KEY_SHARE);
250
251 struct s2n_kem_params *client_kem_params = &conn->kex_params.client_kem_group_params.kem_params;
252
253 /* Don't need to set client_kem_params->len_prefixed since we are the client;
254 * server-side should auto-detect hybrid share size and match our behavior. */
255
256 if (server_kem_group_params->kem_group->curve == &s2n_ecc_curve_none) { /* Pure PQ */
257 POSIX_ENSURE(s2n_kem_recv_ciphertext(extension, client_kem_params) == S2N_SUCCESS, S2N_ERR_BAD_KEY_SHARE);
258 } else { /* Hybrid PQ */
259 if (!server_kem_group_params->kem_group->send_kem_first) {
260 POSIX_ENSURE(s2n_server_key_share_recv_hybrid_partial_ecc(conn, extension) == S2N_SUCCESS, S2N_ERR_BAD_KEY_SHARE);
261 POSIX_ENSURE(s2n_kem_recv_ciphertext(extension, client_kem_params) == S2N_SUCCESS, S2N_ERR_BAD_KEY_SHARE);
262 } else {
263 POSIX_ENSURE(s2n_kem_recv_ciphertext(extension, client_kem_params) == S2N_SUCCESS, S2N_ERR_BAD_KEY_SHARE);
264 POSIX_ENSURE(s2n_server_key_share_recv_hybrid_partial_ecc(conn, extension) == S2N_SUCCESS, S2N_ERR_BAD_KEY_SHARE);
265 }
266 }
267
268 return S2N_SUCCESS;
269 }
270
271 static int s2n_server_key_share_recv_ecc(struct s2n_connection *conn, uint16_t named_group_iana,
272 struct s2n_stuffer *extension)
273 {
274 POSIX_ENSURE_REF(conn);
275 POSIX_ENSURE_REF(extension);
276
277 const struct s2n_ecc_preferences *ecc_pref = NULL;
278 POSIX_GUARD(s2n_connection_get_ecc_preferences(conn, &ecc_pref));
279 POSIX_ENSURE_REF(ecc_pref);
280
281 /* This check should have been done higher up, but including it here as well for extra defense. */
282 POSIX_ENSURE(s2n_ecc_preferences_includes_curve(ecc_pref, named_group_iana),
283 S2N_ERR_ECDHE_UNSUPPORTED_CURVE);
284
285 size_t supported_curve_index = 0;
286
287 for (size_t i = 0; i < ecc_pref->count; i++) {
288 if (named_group_iana == ecc_pref->ecc_curves[i]->iana_id) {
289 supported_curve_index = i;
290 break;
291 }
292 }
293
294 struct s2n_ecc_evp_params *server_ecc_evp_params = &conn->kex_params.server_ecc_evp_params;
295 const struct s2n_ecc_named_curve *negotiated_curve = ecc_pref->ecc_curves[supported_curve_index];
296
297 /**
298 *= https://www.rfc-editor.org/rfc/rfc8446#4.2.8
299 *# If using (EC)DHE key establishment and a HelloRetryRequest containing a
300 *# "key_share" extension was received by the client, the client MUST
301 *# verify that the selected NamedGroup in the ServerHello is the same as
302 *# that in the HelloRetryRequest. If this check fails, the client MUST
303 *# abort the handshake with an "illegal_parameter" alert.
304 **/
305 if (s2n_is_hello_retry_handshake(conn) && !s2n_is_hello_retry_message(conn)) {
306 POSIX_ENSURE_REF(server_ecc_evp_params->negotiated_curve);
307 const struct s2n_ecc_named_curve *previous_negotiated_curve = server_ecc_evp_params->negotiated_curve;
308 POSIX_ENSURE(negotiated_curve == previous_negotiated_curve,
309 S2N_ERR_BAD_MESSAGE);
310 }
311
312 server_ecc_evp_params->negotiated_curve = negotiated_curve;
313
314 /* Now that ECC has been negotiated, null out this connection's preferred Hybrid KEMs. They will not be used any
315 * more during this TLS connection, but can still be printed by s2nc's client debugging output. */
316 conn->kex_params.client_kem_group_params.kem_group = NULL;
317 conn->kex_params.server_kem_group_params.ecc_params.negotiated_curve = NULL;
318 conn->kex_params.client_kem_group_params.kem_params.kem = NULL;
319
320 /* If this is a HelloRetryRequest, we won't have a key share. We just have the selected group.
321 * Set the server negotiated curve and exit early so a proper keyshare can be generated. */
322 if (s2n_is_hello_retry_message(conn)) {
323 return S2N_SUCCESS;
324 }
325
326 /* Verify key share sent by client */
327 struct s2n_ecc_evp_params *client_ecc_evp_params = &conn->kex_params.client_ecc_evp_params;
328 POSIX_ENSURE(client_ecc_evp_params->negotiated_curve == server_ecc_evp_params->negotiated_curve, S2N_ERR_BAD_KEY_SHARE);
329 POSIX_ENSURE(client_ecc_evp_params->evp_pkey, S2N_ERR_BAD_KEY_SHARE);
330
331 uint16_t share_size = 0;
332 S2N_ERROR_IF(s2n_stuffer_data_available(extension) < sizeof(share_size), S2N_ERR_BAD_KEY_SHARE);
333 POSIX_GUARD(s2n_stuffer_read_uint16(extension, &share_size));
334 S2N_ERROR_IF(s2n_stuffer_data_available(extension) < share_size, S2N_ERR_BAD_KEY_SHARE);
335
336 /* Proceed to parse share */
337 struct s2n_blob point_blob = { 0 };
338 S2N_ERROR_IF(s2n_ecc_evp_read_params_point(extension, share_size, &point_blob) < 0, S2N_ERR_BAD_KEY_SHARE);
339 S2N_ERROR_IF(s2n_ecc_evp_parse_params_point(&point_blob, server_ecc_evp_params) < 0, S2N_ERR_BAD_KEY_SHARE);
340 S2N_ERROR_IF(server_ecc_evp_params->evp_pkey == NULL, S2N_ERR_BAD_KEY_SHARE);
341
342 return S2N_SUCCESS;
343 }
344
345 /*
346 * From https://tools.ietf.org/html/rfc8446#section-4.2.8
347 *
348 * If using (EC)DHE key establishment, servers offer exactly one
349 * KeyShareEntry in the ServerHello. This value MUST be in the same
350 * group as the KeyShareEntry value offered by the client that the
351 * server has selected for the negotiated key exchange.
352 */
353 static int s2n_server_key_share_recv(struct s2n_connection *conn, struct s2n_stuffer *extension)
354 {
355 POSIX_ENSURE_REF(conn);
356 POSIX_ENSURE_REF(extension);
357
358 uint16_t negotiated_named_group_iana = 0;
359 S2N_ERROR_IF(s2n_stuffer_data_available(extension) < sizeof(negotiated_named_group_iana), S2N_ERR_BAD_KEY_SHARE);
360 POSIX_GUARD(s2n_stuffer_read_uint16(extension, &negotiated_named_group_iana));
361
362 const struct s2n_kem_preferences *kem_pref = NULL;
363 POSIX_GUARD(s2n_connection_get_kem_preferences(conn, &kem_pref));
364 POSIX_ENSURE_REF(kem_pref);
365
366 const struct s2n_ecc_preferences *ecc_pref = NULL;
367 POSIX_GUARD(s2n_connection_get_ecc_preferences(conn, &ecc_pref));
368 POSIX_ENSURE_REF(ecc_pref);
369
370 if (s2n_ecc_preferences_includes_curve(ecc_pref, negotiated_named_group_iana)) {
371 POSIX_GUARD(s2n_server_key_share_recv_ecc(conn, negotiated_named_group_iana, extension));
372 } else if (s2n_kem_preferences_includes_tls13_kem_group(kem_pref, negotiated_named_group_iana)) {
373 POSIX_GUARD(s2n_server_key_share_recv_pq(conn, negotiated_named_group_iana, extension));
374 } else {
375 POSIX_BAIL(S2N_ERR_ECDHE_UNSUPPORTED_CURVE);
376 }
377
378 return S2N_SUCCESS;
379 }
380
381 /* Selects highest priority mutually supported key share, or indicates need for HRR */
382 int s2n_extensions_server_key_share_select(struct s2n_connection *conn)
383 {
384 POSIX_ENSURE_REF(conn);
385
386 /* Our most preferred mutually supported KeyShares that are negotiable in 1-RTT */
387 const struct s2n_ecc_named_curve *client_curve = conn->kex_params.client_ecc_evp_params.negotiated_curve;
388 const struct s2n_kem_group *client_kem_group = conn->kex_params.client_kem_group_params.kem_group;
389
390 /* Our most preferred mutually supported KeyShares that negotiable in 1 or 2 round trips (which may or may not have been sent in the KeyShare by the client) */
391 const struct s2n_ecc_named_curve *server_curve = conn->kex_params.server_ecc_evp_params.negotiated_curve;
392 const struct s2n_kem_group *server_kem_group = conn->kex_params.server_kem_group_params.kem_group;
393
394 /* Boolean XOR check. When receiving the supported_groups extension, s2n server
395 * should (exclusively) set either server_curve or server_kem_group based on the
396 * set of mutually supported groups. If both server_curve and server_kem_group
397 * are NULL, it is because client and server do not share any mutually supported
398 * groups; key negotiation is not possible and the handshake should be aborted
399 * without sending HRR. (The case of both being non-NULL should never occur, and
400 * is an error.) */
401 POSIX_ENSURE((server_curve == NULL) != (server_kem_group == NULL), S2N_ERR_INVALID_SUPPORTED_GROUP_STATE);
402
403 const struct s2n_security_policy *policy = NULL;
404 POSIX_GUARD(s2n_connection_get_security_policy(conn, &policy));
405 POSIX_ENSURE_REF(policy);
406
407 const struct s2n_ecc_named_curve *strongly_preferred_curve = NULL;
408 const struct s2n_kem_group *strongly_preferred_kem_group = NULL;
409 bool matched_strongly_preferred_iana = false;
410 bool need_hrr_for_strongly_preferred_group = false;
411
412 /* Check if there are any strongly preferred SupportedGroups worth performing a 2-RTT upgrade for. */
413 for (size_t i = 0; policy->strongly_preferred_groups != NULL && i < policy->strongly_preferred_groups->count && !matched_strongly_preferred_iana; i++) {
414 uint16_t strongly_preferred_iana = policy->strongly_preferred_groups->iana_ids[i];
415
416 /* Strongly preferred groups are not allowed on policies that support PQ, so we don't check KEMs */
417
418 for (int j = 0; j < S2N_ECC_EVP_SUPPORTED_CURVES_COUNT && !matched_strongly_preferred_iana; j++) {
419 const struct s2n_ecc_named_curve *mutually_supported_curve = conn->kex_params.mutually_supported_curves[j];
420 if (mutually_supported_curve == NULL) {
421 break; /* Reached end of mutually supported ECC curves */
422 }
423 if (strongly_preferred_iana == mutually_supported_curve->iana_id) {
424 matched_strongly_preferred_iana = true;
425 strongly_preferred_curve = mutually_supported_curve;
426
427 /* Check if we can negotiate our strongly preferred ECC Curve in 1-RTT */
428 if (client_curve != NULL && (strongly_preferred_iana == client_curve->iana_id)) {
429 need_hrr_for_strongly_preferred_group = false;
430 } else {
431 need_hrr_for_strongly_preferred_group = true;
432 }
433 }
434 }
435 }
436
437 /* Option 1: Perform a 2-RTT handshake if there is a strongly-preferred SupportedGroup that requires a 2-RTT handshake. */
438 if (matched_strongly_preferred_iana && need_hrr_for_strongly_preferred_group) {
439 /* Ensure that we chose exactly 1 strongly preferred SupportedGroup */
440 POSIX_ENSURE((strongly_preferred_curve == NULL) != (strongly_preferred_kem_group == NULL), S2N_ERR_INVALID_SUPPORTED_GROUP_STATE);
441
442 conn->kex_params.server_kem_group_params.kem_group = strongly_preferred_kem_group;
443 conn->kex_params.server_ecc_evp_params.negotiated_curve = strongly_preferred_curve;
444 POSIX_GUARD(s2n_set_hello_retry_required(conn));
445 return S2N_SUCCESS;
446 }
447
448 /* Option 2: Select the best mutually supported PQ KEM Group that can be negotiated in 1-RTT */
449 if (client_kem_group != NULL) {
450 POSIX_ENSURE_REF(conn->kex_params.client_kem_group_params.kem_params.kem);
451
452 conn->kex_params.server_kem_group_params.kem_group = conn->kex_params.client_kem_group_params.kem_group;
453 conn->kex_params.server_kem_group_params.ecc_params.negotiated_curve = conn->kex_params.client_kem_group_params.ecc_params.negotiated_curve;
454 conn->kex_params.server_kem_group_params.kem_params.kem = conn->kex_params.client_kem_group_params.kem_params.kem;
455 conn->kex_params.server_ecc_evp_params.negotiated_curve = NULL;
456 return S2N_SUCCESS;
457 }
458
459 /* Option 3: Otherwise, if any PQ Hybrid Groups can be negotiated in 2-RTT's select that one. This ensures that
460 * clients who offer PQ (and presumably therefore have concerns about quantum computing impacting the long term
461 * confidentiality of their data), have their choice to offer PQ respected, even if they predict the server-side
462 * supports a different PQ KeyShare algorithms. This ensures clients with PQ support are never downgraded to non-PQ
463 * algorithms. */
464 if (server_kem_group != NULL) {
465 /* Null out any available ECC curves so that they won't be sent in the ClientHelloRetry */
466 conn->kex_params.server_ecc_evp_params.negotiated_curve = NULL;
467 POSIX_GUARD(s2n_set_hello_retry_required(conn));
468 return S2N_SUCCESS;
469 }
470
471 /* Option 4: Otherwise, if there is a mutually supported classical ECDHE-only group can be negotiated in 1-RTT, select that one */
472 if (client_curve) {
473 conn->kex_params.server_ecc_evp_params.negotiated_curve = conn->kex_params.client_ecc_evp_params.negotiated_curve;
474 conn->kex_params.server_kem_group_params.kem_group = NULL;
475 conn->kex_params.server_kem_group_params.ecc_params.negotiated_curve = NULL;
476 conn->kex_params.server_kem_group_params.kem_params.kem = NULL;
477 return S2N_SUCCESS;
478 }
479
480 /* Option 5: Server and client have at least 1 mutually supported group, but the client did not send key shares for
481 * any of them. Send a HelloRetryRequest indicating the server's preference. */
482 POSIX_GUARD(s2n_set_hello_retry_required(conn));
483 return S2N_SUCCESS;
484 }
485
486 /* Old-style extension functions -- remove after extensions refactor is complete */
487
488 /*
489 * Calculate the data length for Server Key Share extension
490 * based on negotiated_curve selected in server_ecc_evp_params.
491 *
492 * Retry requests have a different key share format,
493 * https://tools.ietf.org/html/rfc8446#section-4.2.8
494 *
495 * This functions does not error, but s2n_extensions_server_key_share_send() would
496 */
497 int s2n_extensions_server_key_share_send_size(struct s2n_connection *conn)
498 {
499 const struct s2n_ecc_named_curve *curve = conn->kex_params.server_ecc_evp_params.negotiated_curve;
500 int key_share_size = S2N_SIZE_OF_EXTENSION_TYPE
501 + S2N_SIZE_OF_EXTENSION_DATA_SIZE
502 + S2N_SIZE_OF_NAMED_GROUP;
503
504 /* If this is a KeyShareHelloRetryRequest we don't include the share size */
505 if (s2n_is_hello_retry_message(conn)) {
506 return key_share_size;
507 }
508
509 if (curve == NULL) {
510 return 0;
511 }
512
513 /* If this is a full KeyShareEntry, include the share size */
514 key_share_size += (S2N_SIZE_OF_KEY_SHARE_SIZE + curve->share_size);
515
516 return key_share_size;
517 }
518
519 /*
520 * Sends Key Share extension in Server Hello.
521 *
522 * Expects negotiated_curve to be set and generates a ephemeral key for key sharing
523 */
524 int s2n_extensions_server_key_share_send(struct s2n_connection *conn, struct s2n_stuffer *out)
525 {
526 return s2n_extension_send(&s2n_server_key_share_extension, conn, out);
527 }
528
529 /*
530 * Client receives a Server Hello key share.
531 *
532 * If the curve is supported, conn->kex_params.server_ecc_evp_params will be set.
533 */
534 int s2n_extensions_server_key_share_recv(struct s2n_connection *conn, struct s2n_stuffer *extension)
535 {
536 return s2n_extension_recv(&s2n_server_key_share_extension, conn, extension);
537 }