Branch data Line data Source code
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_key_share.h"
17 : :
18 : : #include "crypto/s2n_openssl.h"
19 : : #include "crypto/s2n_pq.h"
20 : : #include "error/s2n_errno.h"
21 : : #include "stuffer/s2n_stuffer.h"
22 : : #include "tls/extensions/s2n_key_share.h"
23 : : #include "tls/s2n_kem_preferences.h"
24 : : #include "tls/s2n_security_policies.h"
25 : : #include "tls/s2n_tls13.h"
26 : : #include "utils/s2n_safety.h"
27 : :
28 : : /**
29 : : * Specified in https://tools.ietf.org/html/rfc8446#section-4.2.8
30 : : * "The "key_share" extension contains the endpoint's cryptographic parameters."
31 : : *
32 : : * Structure:
33 : : * Extension type (2 bytes)
34 : : * Extension data size (2 bytes)
35 : : * Client shares size (2 bytes)
36 : : * Client shares:
37 : : * Named group (2 bytes)
38 : : * Key share size (2 bytes)
39 : : * Key share (variable size)
40 : : *
41 : : * This extension only modifies the connection's client ecc_evp_params. It does
42 : : * not make any decisions about which set of params to use.
43 : : *
44 : : * The server will NOT alert when processing a client extension that violates the RFC.
45 : : * So the server will accept:
46 : : * - Multiple key shares for the same named group. The server will accept the first
47 : : * key share for the group and ignore any duplicates.
48 : : * - Key shares for named groups not in the client's supported_groups extension.
49 : : **/
50 : :
51 : : static int s2n_client_key_share_send(struct s2n_connection *conn, struct s2n_stuffer *out);
52 : : static int s2n_client_key_share_recv(struct s2n_connection *conn, struct s2n_stuffer *extension);
53 : :
54 : : const s2n_extension_type s2n_client_key_share_extension = {
55 : : .iana_value = TLS_EXTENSION_KEY_SHARE,
56 : : .minimum_version = S2N_TLS13,
57 : : .is_response = false,
58 : : .send = s2n_client_key_share_send,
59 : : .recv = s2n_client_key_share_recv,
60 : : .should_send = s2n_extension_always_send,
61 : : .if_missing = s2n_extension_noop_if_missing,
62 : : };
63 : :
64 : : static int s2n_generate_default_ecc_key_share(struct s2n_connection *conn, struct s2n_stuffer *out)
65 : 5807 : {
66 [ - + ][ # # ]: 5807 : POSIX_ENSURE_REF(conn);
67 : 5807 : const struct s2n_ecc_preferences *ecc_pref = NULL;
68 [ - + ]: 5807 : POSIX_GUARD(s2n_connection_get_ecc_preferences(conn, &ecc_pref));
69 [ - + ][ # # ]: 5807 : POSIX_ENSURE_REF(ecc_pref);
70 : :
71 : : /* Skip sending classical ECC curves for PQ only policies. */
72 [ - + ]: 5807 : if (ecc_pref->count == 0) {
73 : 0 : return S2N_SUCCESS;
74 : 0 : }
75 : :
76 : : /* We only ever send a single EC key share: either the share requested by the server
77 : : * during a retry, or the most preferred share according to local preferences.
78 : : */
79 : 5807 : struct s2n_ecc_evp_params *client_params = &conn->kex_params.client_ecc_evp_params;
80 [ + + ]: 5807 : if (s2n_is_hello_retry_handshake(conn)) {
81 : 647 : const struct s2n_ecc_named_curve *server_curve = conn->kex_params.server_ecc_evp_params.negotiated_curve;
82 : :
83 : : /* If the server did not request a specific ECC keyshare, don't send one */
84 [ - + ]: 647 : if (!server_curve) {
85 : 0 : return S2N_SUCCESS;
86 : 0 : }
87 : :
88 : : /* If the server requested a new ECC keyshare, free the old one */
89 [ + - ]: 647 : if (server_curve != client_params->negotiated_curve) {
90 [ - + ]: 647 : POSIX_GUARD(s2n_ecc_evp_params_free(client_params));
91 : 647 : }
92 : :
93 : : /**
94 : : *= https://www.rfc-editor.org/rfc/rfc8446#4.2.8
95 : : *# Otherwise, when sending the new ClientHello, the client MUST
96 : : *# replace the original "key_share" extension with one containing only a
97 : : *# new KeyShareEntry for the group indicated in the selected_group field
98 : : *# of the triggering HelloRetryRequest.
99 : : **/
100 : 647 : client_params->negotiated_curve = server_curve;
101 : 5160 : } else {
102 : : /* The standalone share always uses the most preferred curve.
103 : : * The optimization below never changes which curve is
104 : : * offered, only whether a key has to be generated for it.
105 : : */
106 : 5160 : client_params->negotiated_curve = ecc_pref->ecc_curves[0];
107 : :
108 : : /* The PQ hybrid share is generated first and contains a classical ECDHE key.
109 : : * If that key is already on the curve we want, reuse it instead of generating
110 : : * a second key on the same curve. RFC 9954 Section 3.2 permits reusing the
111 : : * key_exchange value of one algorithm across KeyShareEntries in a ClientHello:
112 : : * https://www.rfc-editor.org/rfc/rfc9954#section-3.2
113 : : */
114 : :
115 : : /* EVP_PKEY_up_ref was added in OpenSSL 1.1.0 and is absent from OpenSSL 1.0.2,
116 : : * which s2n still supports. s2n normalizes LibreSSL's OPENSSL_VERSION_NUMBER in
117 : : * s2n_openssl.h, so this check is also correct for LibreSSL, and BoringSSL and
118 : : * AWS-LC both provide EVP_PKEY_up_ref.
119 : : */
120 : 5160 : #if S2N_OPENSSL_VERSION_AT_LEAST(1, 1, 0)
121 : 5160 : struct s2n_ecc_evp_params *hybrid_ecc = &conn->kex_params.client_kem_group_params.ecc_params;
122 [ - + ][ # # ]: 5160 : POSIX_ENSURE_REF(hybrid_ecc);
123 : 5160 : const struct s2n_ecc_named_curve *hybrid_curve = hybrid_ecc->negotiated_curve;
124 : :
125 [ + - ][ - + ]: 5160 : if (client_params->evp_pkey == NULL && hybrid_curve && hybrid_ecc->evp_pkey != NULL
[ # # ]
126 [ # # ]: 5160 : && hybrid_curve == ecc_pref->ecc_curves[0]) {
127 : : /*
128 : : * Assigning evp_pkey is what skips the keygen: s2n_ecdhe_parameters_send
129 : : * below only generates a key when evp_pkey is NULL, so it now just
130 : : * serializes this key into the standalone KeyShareEntry.
131 : : */
132 [ # # ][ # # ]: 0 : POSIX_GUARD_OSSL(EVP_PKEY_up_ref(hybrid_ecc->evp_pkey), S2N_ERR_ECDHE_GEN_KEY);
133 : 0 : client_params->evp_pkey = hybrid_ecc->evp_pkey;
134 : 0 : }
135 : 5160 : #endif
136 : 5160 : }
137 [ - + ]: 5807 : POSIX_GUARD(s2n_ecdhe_parameters_send(client_params, out));
138 : :
139 : 5807 : return S2N_SUCCESS;
140 : 5807 : }
141 : :
142 : : static int s2n_generate_pq_key_share(struct s2n_stuffer *out, struct s2n_kem_group_params *kem_group_params)
143 : 0 : {
144 [ # # ][ # # ]: 0 : POSIX_ENSURE_REF(out);
145 [ # # ][ # # ]: 0 : POSIX_ENSURE_REF(kem_group_params);
146 : :
147 : : /* This function should never be called when PQ is disabled */
148 [ # # ][ # # ]: 0 : POSIX_ENSURE(s2n_pq_is_enabled(), S2N_ERR_UNIMPLEMENTED);
149 : :
150 : 0 : const struct s2n_kem_group *kem_group = kem_group_params->kem_group;
151 [ # # ][ # # ]: 0 : POSIX_ENSURE_REF(kem_group);
152 : :
153 [ # # ]: 0 : POSIX_GUARD(s2n_stuffer_write_uint16(out, kem_group->iana_id));
154 : :
155 : 0 : struct s2n_stuffer_reservation total_share_size = { 0 };
156 [ # # ]: 0 : POSIX_GUARD(s2n_stuffer_reserve_uint16(out, &total_share_size));
157 : :
158 : 0 : struct s2n_ecc_evp_params *ecc_params = &kem_group_params->ecc_params;
159 : 0 : ecc_params->negotiated_curve = kem_group->curve;
160 : :
161 : 0 : struct s2n_kem_params *kem_params = &kem_group_params->kem_params;
162 : 0 : kem_params->kem = kem_group->kem;
163 : :
164 [ # # ]: 0 : if (kem_group->curve == &s2n_ecc_curve_none) { /* Pure PQ */
165 [ # # ]: 0 : POSIX_GUARD(s2n_kem_send_public_key(out, kem_params));
166 : 0 : } else { /* Hybrid PQ */
167 [ # # ]: 0 : if (kem_group->send_kem_first) {
168 [ # # ]: 0 : POSIX_GUARD(s2n_kem_send_public_key(out, kem_params));
169 [ # # ]: 0 : POSIX_GUARD_RESULT(s2n_ecdhe_send_public_key(ecc_params, out));
170 : 0 : } else {
171 [ # # ]: 0 : POSIX_GUARD_RESULT(s2n_ecdhe_send_public_key(ecc_params, out));
172 [ # # ]: 0 : POSIX_GUARD(s2n_kem_send_public_key(out, kem_params));
173 : 0 : }
174 : 0 : }
175 : :
176 [ # # ]: 0 : POSIX_GUARD(s2n_stuffer_write_vector_size(&total_share_size));
177 : :
178 : 0 : return S2N_SUCCESS;
179 : 0 : }
180 : :
181 : : static int s2n_generate_default_pq_key_share(struct s2n_connection *conn, struct s2n_stuffer *out)
182 : 5807 : {
183 [ # # ][ - + ]: 5807 : POSIX_ENSURE_REF(conn);
184 [ # # ][ - + ]: 5807 : POSIX_ENSURE_REF(out);
185 : :
186 : : /* Client should skip sending PQ groups/key shares if PQ is disabled */
187 [ + - ]: 5807 : if (!s2n_pq_is_enabled()) {
188 : 5807 : return S2N_SUCCESS;
189 : 5807 : }
190 : :
191 : 0 : const struct s2n_kem_preferences *kem_pref = NULL;
192 [ # # ]: 0 : POSIX_GUARD(s2n_connection_get_kem_preferences(conn, &kem_pref));
193 [ # # ][ # # ]: 0 : POSIX_ENSURE_REF(kem_pref);
194 : :
195 : 0 : uint32_t available_groups = 0;
196 [ # # ]: 0 : POSIX_GUARD_RESULT(s2n_kem_preferences_groups_available(kem_pref, &available_groups));
197 [ # # ]: 0 : if (available_groups == 0) {
198 : 0 : return S2N_SUCCESS;
199 : 0 : }
200 : :
201 : : /* We only ever send a single PQ key share: either the share requested by the server
202 : : * during a retry, or the most preferred share according to local preferences.
203 : : */
204 : 0 : struct s2n_kem_group_params *client_params = &conn->kex_params.client_kem_group_params;
205 : :
206 [ # # ]: 0 : if (s2n_is_hello_retry_handshake(conn)) {
207 : 0 : const struct s2n_kem_group *server_group = conn->kex_params.server_kem_group_params.kem_group;
208 : :
209 : : /* If the server did not request a specific PQ keyshare, don't send one */
210 [ # # ]: 0 : if (!server_group) {
211 : 0 : return S2N_SUCCESS;
212 : 0 : }
213 : :
214 : : /* If the server requested a new PQ keyshare, free the old one */
215 [ # # ]: 0 : if (client_params->kem_group != server_group) {
216 [ # # ]: 0 : POSIX_GUARD(s2n_kem_group_free(client_params));
217 : 0 : }
218 : :
219 : : /**
220 : : *= https://www.rfc-editor.org/rfc/rfc8446#4.2.8
221 : : *# Otherwise, when sending the new ClientHello, the client MUST
222 : : *# replace the original "key_share" extension with one containing only a
223 : : *# new KeyShareEntry for the group indicated in the selected_group field
224 : : *# of the triggering HelloRetryRequest.
225 : : **/
226 : 0 : client_params->kem_group = server_group;
227 : 0 : } else {
228 : 0 : client_params->kem_group = s2n_kem_preferences_get_highest_priority_group(kem_pref);
229 [ # # ][ # # ]: 0 : POSIX_ENSURE_REF(client_params->kem_group);
230 : 0 : }
231 : :
232 [ # # ]: 0 : POSIX_GUARD(s2n_generate_pq_key_share(out, client_params));
233 : :
234 : 0 : return S2N_SUCCESS;
235 : 0 : }
236 : :
237 : : static int s2n_client_key_share_send(struct s2n_connection *conn, struct s2n_stuffer *out)
238 : 5809 : {
239 [ + + ]: 5809 : if (s2n_is_hello_retry_handshake(conn)) {
240 : 649 : const struct s2n_ecc_named_curve *server_curve = conn->kex_params.server_ecc_evp_params.negotiated_curve;
241 : 649 : const struct s2n_ecc_named_curve *client_curve = conn->kex_params.client_ecc_evp_params.negotiated_curve;
242 : 649 : const struct s2n_kem_group *server_group = conn->kex_params.server_kem_group_params.kem_group;
243 : 649 : const struct s2n_kem_group *client_group = conn->kex_params.client_kem_group_params.kem_group;
244 : :
245 : : /* Ensure a new key share will be sent after a hello retry request */
246 [ + - ][ + + ]: 649 : POSIX_ENSURE(server_curve != client_curve || server_group != client_group, S2N_ERR_BAD_KEY_SHARE);
[ - + ]
247 : 649 : }
248 : :
249 : 5807 : struct s2n_stuffer_reservation shares_size = { 0 };
250 [ - + ]: 5807 : POSIX_GUARD(s2n_stuffer_reserve_uint16(out, &shares_size));
251 [ - + ]: 5807 : POSIX_GUARD(s2n_generate_default_pq_key_share(conn, out));
252 [ - + ]: 5807 : POSIX_GUARD(s2n_generate_default_ecc_key_share(conn, out));
253 [ - + ]: 5807 : POSIX_GUARD(s2n_stuffer_write_vector_size(&shares_size));
254 : :
255 : : /* We must have written at least one share */
256 [ - + ][ # # ]: 5807 : POSIX_ENSURE(s2n_stuffer_data_available(out) > shares_size.length, S2N_ERR_BAD_KEY_SHARE);
257 : :
258 : 5807 : return S2N_SUCCESS;
259 : 5807 : }
260 : :
261 : : static int s2n_client_key_share_parse_ecc(struct s2n_stuffer *key_share, const struct s2n_ecc_named_curve *curve,
262 : : struct s2n_ecc_evp_params *ecc_params)
263 : 4776 : {
264 [ # # ][ - + ]: 4776 : POSIX_ENSURE_REF(key_share);
265 [ - + ][ # # ]: 4776 : POSIX_ENSURE_REF(curve);
266 [ # # ][ - + ]: 4776 : POSIX_ENSURE_REF(ecc_params);
267 : :
268 : 4776 : struct s2n_blob point_blob = { 0 };
269 [ - + ]: 4776 : POSIX_GUARD(s2n_ecc_evp_read_params_point(key_share, curve->share_size, &point_blob));
270 : :
271 : : /* Ignore curves with points we can't parse */
272 : 4776 : ecc_params->negotiated_curve = curve;
273 [ + + ]: 4776 : if (s2n_ecc_evp_parse_params_point(&point_blob, ecc_params) != S2N_SUCCESS) {
274 : 2 : ecc_params->negotiated_curve = NULL;
275 [ - + ]: 2 : POSIX_GUARD(s2n_ecc_evp_params_free(ecc_params));
276 : 2 : }
277 : :
278 : 4776 : return S2N_SUCCESS;
279 : 4776 : }
280 : :
281 : : static int s2n_client_key_share_recv_ecc(struct s2n_connection *conn, struct s2n_stuffer *key_share, uint16_t curve_iana_id)
282 : 5438 : {
283 [ # # ][ - + ]: 5438 : POSIX_ENSURE_REF(conn);
284 [ # # ][ - + ]: 5438 : POSIX_ENSURE_REF(key_share);
285 : :
286 : 5438 : const struct s2n_ecc_preferences *ecc_pref = NULL;
287 [ - + ]: 5438 : POSIX_GUARD(s2n_connection_get_ecc_preferences(conn, &ecc_pref));
288 [ # # ][ - + ]: 5438 : POSIX_ENSURE_REF(ecc_pref);
289 : :
290 : 5438 : struct s2n_ecc_evp_params *client_params = &conn->kex_params.client_ecc_evp_params;
291 : :
292 : 5438 : const struct s2n_ecc_named_curve *curve = NULL;
293 [ + + ]: 8057 : for (size_t i = 0; i < ecc_pref->count; i++) {
294 : 7402 : const struct s2n_ecc_named_curve *supported_curve = ecc_pref->ecc_curves[i];
295 [ - + ][ # # ]: 7402 : POSIX_ENSURE_REF(supported_curve);
296 : :
297 : : /* Stop if we reach the current highest priority share.
298 : : * Any share of lower priority is discarded.
299 : : */
300 [ + + ]: 7402 : if (client_params->negotiated_curve == supported_curve) {
301 : 6 : break;
302 : 6 : }
303 : :
304 : : /* Skip if not supported by the client.
305 : : * The client must not send shares it doesn't support, but the server
306 : : * is not required to error if they are encountered.
307 : : */
308 [ + + ]: 7396 : if (!conn->kex_params.mutually_supported_curves[i]) {
309 : 13 : continue;
310 : 13 : }
311 : :
312 : : /* Stop if we find a match */
313 [ + + ]: 7383 : if (curve_iana_id == supported_curve->iana_id) {
314 : 4777 : curve = supported_curve;
315 : 4777 : break;
316 : 4777 : }
317 : 7383 : }
318 : :
319 : : /* Ignore unsupported curves */
320 [ + + ]: 5438 : if (!curve) {
321 : 661 : return S2N_SUCCESS;
322 : 661 : }
323 : :
324 : : /* Ignore curves with unexpected share sizes */
325 [ + + ]: 4777 : if (key_share->blob.size != curve->share_size) {
326 : 1 : return S2N_SUCCESS;
327 : 1 : }
328 : :
329 : 4776 : DEFER_CLEANUP(struct s2n_ecc_evp_params new_client_params = { 0 }, s2n_ecc_evp_params_free);
330 : :
331 [ - + ]: 4776 : POSIX_GUARD(s2n_client_key_share_parse_ecc(key_share, curve, &new_client_params));
332 : : /* negotiated_curve will be NULL if the key share was not parsed successfully */
333 [ + + ]: 4776 : if (!new_client_params.negotiated_curve) {
334 : 2 : return S2N_SUCCESS;
335 : 2 : }
336 : :
337 [ - + ]: 4774 : POSIX_GUARD(s2n_ecc_evp_params_free(client_params));
338 : 4774 : *client_params = new_client_params;
339 : :
340 : 4774 : ZERO_TO_DISABLE_DEFER_CLEANUP(new_client_params);
341 : 4774 : return S2N_SUCCESS;
342 : 4774 : }
343 : :
344 : : static int s2n_client_key_share_recv_hybrid_partial_ecc(struct s2n_stuffer *key_share, struct s2n_kem_group_params *new_client_params)
345 : 0 : {
346 [ # # ][ # # ]: 0 : POSIX_ENSURE_REF(new_client_params);
347 : 0 : const struct s2n_kem_group *kem_group = new_client_params->kem_group;
348 [ # # ][ # # ]: 0 : POSIX_ENSURE_REF(kem_group);
349 [ # # ][ # # ]: 0 : POSIX_ENSURE_REF(kem_group->curve);
350 : :
351 [ # # ]: 0 : POSIX_GUARD(s2n_client_key_share_parse_ecc(key_share, kem_group->curve, &new_client_params->ecc_params));
352 : :
353 : : /* If we were unable to parse the EC portion of the share, negotiated_curve
354 : : * will be NULL, and we should ignore the entire key share. */
355 [ # # ][ # # ]: 0 : POSIX_ENSURE_REF(new_client_params->ecc_params.negotiated_curve);
356 : :
357 : 0 : return S2N_SUCCESS;
358 : 0 : }
359 : :
360 : : static int s2n_client_key_share_recv_pq(struct s2n_connection *conn, struct s2n_stuffer *key_share, uint16_t kem_group_iana_id)
361 : 5438 : {
362 [ - + ][ # # ]: 5438 : POSIX_ENSURE_REF(conn);
363 [ # # ][ - + ]: 5438 : POSIX_ENSURE_REF(key_share);
364 : :
365 : 5438 : const struct s2n_kem_preferences *kem_pref = NULL;
366 [ - + ]: 5438 : POSIX_GUARD(s2n_connection_get_kem_preferences(conn, &kem_pref));
367 [ - + ][ # # ]: 5438 : POSIX_ENSURE_REF(kem_pref);
368 : :
369 : : /* Ignore key share if PQ is not enabled */
370 [ + - ]: 5438 : if (!s2n_pq_is_enabled()) {
371 : 5438 : return S2N_SUCCESS;
372 : 5438 : }
373 : :
374 : 0 : struct s2n_kem_group_params *client_params = &conn->kex_params.client_kem_group_params;
375 : :
376 : 0 : const struct s2n_kem_group *kem_group = NULL;
377 [ # # ]: 0 : for (size_t i = 0; i < kem_pref->tls13_kem_group_count; i++) {
378 : 0 : const struct s2n_kem_group *supported_group = kem_pref->tls13_kem_groups[i];
379 [ # # ][ # # ]: 0 : POSIX_ENSURE_REF(supported_group);
380 : :
381 : : /* Skip if the group is not available */
382 [ # # ]: 0 : if (!s2n_kem_group_is_available(supported_group)) {
383 : 0 : continue;
384 : 0 : }
385 : :
386 : : /* Stop if we reach the current highest priority share.
387 : : * Any share of lower priority is discarded.
388 : : */
389 [ # # ]: 0 : if (client_params->kem_group == supported_group) {
390 : 0 : break;
391 : 0 : }
392 : :
393 : : /* Skip if not supported by the client.
394 : : * The client must not send shares it doesn't support, but the server
395 : : * is not required to error if they are encountered.
396 : : */
397 [ # # ]: 0 : if (!conn->kex_params.mutually_supported_kem_groups[i]) {
398 : 0 : continue;
399 : 0 : }
400 : :
401 : : /* Stop if we find a match */
402 [ # # ]: 0 : if (kem_group_iana_id == supported_group->iana_id) {
403 : 0 : kem_group = supported_group;
404 : 0 : break;
405 : 0 : }
406 : 0 : }
407 : :
408 : : /* Ignore unsupported KEM groups */
409 [ # # ]: 0 : if (!kem_group) {
410 : 0 : return S2N_SUCCESS;
411 : 0 : }
412 : :
413 : 0 : uint16_t actual_share_size = key_share->blob.size;
414 : 0 : uint16_t expected_share_size = kem_group->curve->share_size + kem_group->kem->public_key_length;
415 : :
416 : : /* Ignore KEM groups with unexpected overall total share sizes */
417 [ # # ]: 0 : if (actual_share_size != expected_share_size) {
418 : 0 : return S2N_SUCCESS;
419 : 0 : }
420 : :
421 : 0 : DEFER_CLEANUP(struct s2n_kem_group_params new_client_params = { 0 }, s2n_kem_group_free);
422 : 0 : new_client_params.kem_group = kem_group;
423 : :
424 : 0 : new_client_params.kem_params.kem = kem_group->kem;
425 : :
426 : : /* Note: the PQ share size is validated in s2n_kem_recv_public_key() */
427 : : /* Ignore PQ and ECC groups with public keys we can't parse */
428 [ # # ]: 0 : if (kem_group->curve == &s2n_ecc_curve_none) { /* Pure PQ */
429 [ # # ]: 0 : if (s2n_kem_recv_public_key(key_share, &new_client_params.kem_params) != S2N_SUCCESS) {
430 : 0 : return S2N_SUCCESS;
431 : 0 : }
432 : 0 : } else { /* Hybrid PQ */
433 [ # # ]: 0 : if (kem_group->send_kem_first) {
434 [ # # ]: 0 : if (s2n_kem_recv_public_key(key_share, &new_client_params.kem_params) != S2N_SUCCESS) {
435 : 0 : return S2N_SUCCESS;
436 : 0 : }
437 [ # # ]: 0 : if (s2n_client_key_share_recv_hybrid_partial_ecc(key_share, &new_client_params) != S2N_SUCCESS) {
438 : 0 : return S2N_SUCCESS;
439 : 0 : }
440 : 0 : } else {
441 [ # # ]: 0 : if (s2n_client_key_share_recv_hybrid_partial_ecc(key_share, &new_client_params) != S2N_SUCCESS) {
442 : 0 : return S2N_SUCCESS;
443 : 0 : }
444 [ # # ]: 0 : if (s2n_kem_recv_public_key(key_share, &new_client_params.kem_params) != S2N_SUCCESS) {
445 : 0 : return S2N_SUCCESS;
446 : 0 : }
447 : 0 : }
448 : 0 : }
449 : :
450 [ # # ]: 0 : POSIX_GUARD(s2n_kem_group_free(client_params));
451 : 0 : *client_params = new_client_params;
452 : :
453 : 0 : ZERO_TO_DISABLE_DEFER_CLEANUP(new_client_params);
454 : 0 : return S2N_SUCCESS;
455 : 0 : }
456 : :
457 : : /*
458 : : * We chose our most preferred group of the mutually supported groups while processing the
459 : : * supported_groups extension. However, our true most preferred group is always the
460 : : * group that we already have a key share for, since retries are expensive.
461 : : *
462 : : * This method modifies our group selection based on what keyshares are available.
463 : : * It then stores the client keyshare for the selected group, or initiates a retry
464 : : * if no valid keyshares are available.
465 : : */
466 : : static int s2n_client_key_share_recv(struct s2n_connection *conn, struct s2n_stuffer *extension)
467 : 5433 : {
468 [ # # ][ - + ]: 5433 : POSIX_ENSURE_REF(conn);
469 [ - + ][ # # ]: 5433 : POSIX_ENSURE_REF(extension);
470 : :
471 : 5433 : uint16_t key_shares_size = 0;
472 [ - + ]: 5433 : POSIX_GUARD(s2n_stuffer_read_uint16(extension, &key_shares_size));
473 [ + - ][ + + ]: 5433 : POSIX_ENSURE(s2n_stuffer_data_available(extension) == key_shares_size, S2N_ERR_BAD_MESSAGE);
474 : :
475 : 5432 : uint16_t named_group = 0, share_size = 0;
476 : 5432 : struct s2n_blob key_share_blob = { 0 };
477 : 5432 : struct s2n_stuffer key_share = { 0 };
478 : :
479 : 5432 : uint16_t keyshare_count = 0;
480 [ + + ]: 10870 : while (s2n_stuffer_data_available(extension) > 0) {
481 [ - + ]: 5439 : POSIX_GUARD(s2n_stuffer_read_uint16(extension, &named_group));
482 [ - + ]: 5439 : POSIX_GUARD(s2n_stuffer_read_uint16(extension, &share_size));
483 [ + - ][ + + ]: 5439 : POSIX_ENSURE(s2n_stuffer_data_available(extension) >= share_size, S2N_ERR_BAD_MESSAGE);
484 : :
485 [ - + ]: 5438 : POSIX_GUARD(s2n_blob_init(&key_share_blob,
486 : 5438 : s2n_stuffer_raw_read(extension, share_size), share_size));
487 [ - + ]: 5438 : POSIX_GUARD(s2n_stuffer_init(&key_share, &key_share_blob));
488 [ - + ]: 5438 : POSIX_GUARD(s2n_stuffer_skip_write(&key_share, share_size));
489 : 5438 : keyshare_count++;
490 : :
491 : : /* Try to parse the share as ECC, then as PQ; will ignore
492 : : * shares for unrecognized groups. */
493 [ - + ]: 5438 : POSIX_GUARD(s2n_client_key_share_recv_ecc(conn, &key_share, named_group));
494 [ - + ]: 5438 : POSIX_GUARD(s2n_client_key_share_recv_pq(conn, &key_share, named_group));
495 : 5438 : }
496 : :
497 : : /* During a retry, the client should only have sent one keyshare */
498 [ # # ][ + + ]: 5431 : POSIX_ENSURE(!s2n_is_hello_retry_handshake(conn) || keyshare_count == 1, S2N_ERR_BAD_MESSAGE);
[ + - ]
499 : :
500 : : /**
501 : : * If there were no matching key shares, then we received an empty key share extension
502 : : * or we didn't match a key share with a supported group. We should send a retry.
503 : : *
504 : : *= https://www.rfc-editor.org/rfc/rfc8446#4.1.1
505 : : *# If the server selects an (EC)DHE group and the client did not offer a
506 : : *# compatible "key_share" extension in the initial ClientHello, the
507 : : *# server MUST respond with a HelloRetryRequest (Section 4.1.4) message.
508 : : **/
509 : 5431 : struct s2n_ecc_evp_params *client_ecc_params = &conn->kex_params.client_ecc_evp_params;
510 : 5431 : struct s2n_kem_group_params *client_pq_params = &conn->kex_params.client_kem_group_params;
511 [ + - ][ + + ]: 5431 : if (!client_pq_params->kem_group && !client_ecc_params->negotiated_curve) {
512 [ - + ]: 656 : POSIX_GUARD(s2n_set_hello_retry_required(conn));
513 : 656 : }
514 : :
515 : 5431 : return S2N_SUCCESS;
516 : 5431 : }
517 : :
518 : : /* Old-style extension functions -- remove after extensions refactor is complete */
519 : :
520 : : int s2n_extensions_client_key_share_recv(struct s2n_connection *conn, struct s2n_stuffer *extension)
521 : 0 : {
522 : 0 : return s2n_extension_recv(&s2n_client_key_share_extension, conn, extension);
523 : 0 : }
|