Debian Codegraph @

crt/s2n/tls/extensions/s2n_server_psk.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_psk.h"
17
18 #include <stdint.h>
19
20 #include "tls/s2n_tls.h"
21 #include "utils/s2n_bitmap.h"
22 #include "utils/s2n_safety.h"
23
24 static bool s2n_server_psk_should_send(struct s2n_connection *conn);
25 static int s2n_server_psk_send(struct s2n_connection *conn, struct s2n_stuffer *out);
26 static int s2n_server_psk_recv(struct s2n_connection *conn, struct s2n_stuffer *extension);
27
28 const s2n_extension_type s2n_server_psk_extension = {
29 .iana_value = TLS_EXTENSION_PRE_SHARED_KEY,
30 .minimum_version = S2N_TLS13,
31 .is_response = true,
32 .send = s2n_server_psk_send,
33 .recv = s2n_server_psk_recv,
34 .should_send = s2n_server_psk_should_send,
35 .if_missing = s2n_extension_noop_if_missing,
36 };
37
38 static bool s2n_server_psk_should_send(struct s2n_connection *conn)
39 {
40 /* Only send a server pre_shared_key extension if a chosen PSK is set on the connection */
41 return conn && conn->psk_params.chosen_psk;
42 }
43
44 static int s2n_server_psk_send(struct s2n_connection *conn, struct s2n_stuffer *out)
45 {
46 POSIX_ENSURE_REF(conn);
47
48 /* Send the index of the chosen PSK that is stored on the connection. */
49 POSIX_GUARD(s2n_stuffer_write_uint16(out, conn->psk_params.chosen_psk_wire_index));
50
51 return S2N_SUCCESS;
52 }
53
54 static int s2n_server_psk_recv(struct s2n_connection *conn, struct s2n_stuffer *extension)
55 {
56 POSIX_ENSURE_REF(conn);
57
58 /* Currently in s2n, only (EC)DHE key exchange mode is supported.
59 * Any other mode selected by the server is invalid because it was not offered by the client.
60 * A key_share extension MUST have been received in order to use a pre-shared key in (EC)DHE key exchange mode.
61 */
62 s2n_extension_type_id key_share_ext_id = s2n_unsupported_extension;
63 POSIX_GUARD(s2n_extension_supported_iana_value_to_id(TLS_EXTENSION_KEY_SHARE, &key_share_ext_id));
64 POSIX_ENSURE(S2N_CBIT_TEST(conn->extension_responses_received, key_share_ext_id), S2N_ERR_MISSING_EXTENSION);
65
66 /* From RFC section: https://tools.ietf.org/html/rfc8446#section-4.2.8.1
67 * Any future values that are allocated must ensure that the transmitted protocol messages
68 * unambiguously identify which mode was selected by the server; at present, this is
69 * indicated by the presence of the "key_share" in the ServerHello.
70 */
71 conn->psk_params.psk_ke_mode = S2N_PSK_DHE_KE;
72
73 uint16_t chosen_psk_wire_index = 0;
74 POSIX_GUARD(s2n_stuffer_read_uint16(extension, &chosen_psk_wire_index));
75
76 /* From RFC section: https://tools.ietf.org/html/rfc8446#section-4.2.11
77 * Clients MUST verify that the server's selected_identity is within the
78 * range supplied by the client.
79 */
80 POSIX_ENSURE(chosen_psk_wire_index < conn->psk_params.psk_list.len, S2N_ERR_INVALID_ARGUMENT);
81 conn->psk_params.chosen_psk_wire_index = chosen_psk_wire_index;
82
83 POSIX_GUARD_RESULT(s2n_array_get(&conn->psk_params.psk_list, conn->psk_params.chosen_psk_wire_index,
84 (void **) &conn->psk_params.chosen_psk));
85
86 return S2N_SUCCESS;
87 }