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_pq_kem.h" 17 : : 18 : : #include <stdint.h> 19 : : #include <sys/param.h> 20 : : 21 : : #include "crypto/s2n_pq.h" 22 : : #include "tls/s2n_kem.h" 23 : : #include "tls/s2n_security_policies.h" 24 : : #include "tls/s2n_tls.h" 25 : : #include "tls/s2n_tls_parameters.h" 26 : : #include "utils/s2n_safety.h" 27 : : 28 : : static bool s2n_client_pq_kem_should_send(struct s2n_connection *conn); 29 : : static int s2n_client_pq_kem_send(struct s2n_connection *conn, struct s2n_stuffer *out); 30 : : static int s2n_client_pq_kem_recv(struct s2n_connection *conn, struct s2n_stuffer *extension); 31 : : 32 : : const s2n_extension_type s2n_client_pq_kem_extension = { 33 : : .iana_value = TLS_EXTENSION_PQ_KEM_PARAMETERS, 34 : : .is_response = false, 35 : : .send = s2n_client_pq_kem_send, 36 : : .recv = s2n_client_pq_kem_recv, 37 : : .should_send = s2n_client_pq_kem_should_send, 38 : : .if_missing = s2n_extension_noop_if_missing, 39 : : }; 40 : : 41 : : static bool s2n_client_pq_kem_should_send(struct s2n_connection *conn) 42 : 7352 : { 43 : 7352 : const struct s2n_security_policy *security_policy = NULL; 44 [ + - ]: 7352 : return s2n_connection_get_security_policy(conn, &security_policy) == S2N_SUCCESS 45 [ - + ]: 7352 : && s2n_pq_kem_is_extension_required(security_policy) 46 [ # # ]: 7352 : && s2n_pq_is_enabled(); 47 : 7352 : } 48 : : 49 : : static int s2n_client_pq_kem_send(struct s2n_connection *conn, struct s2n_stuffer *out) 50 : 0 : { 51 : 0 : const struct s2n_kem_preferences *kem_preferences = NULL; 52 [ # # ]: 0 : POSIX_GUARD(s2n_connection_get_kem_preferences(conn, &kem_preferences)); 53 [ # # ][ # # ]: 0 : POSIX_ENSURE_REF(kem_preferences); 54 : : 55 [ # # ]: 0 : POSIX_GUARD(s2n_stuffer_write_uint16(out, kem_preferences->kem_count * sizeof(kem_extension_size))); 56 [ # # ]: 0 : for (int i = 0; i < kem_preferences->kem_count; i++) { 57 [ # # ]: 0 : POSIX_GUARD(s2n_stuffer_write_uint16(out, kem_preferences->kems[i]->kem_extension_id)); 58 : 0 : } 59 : : 60 : 0 : return S2N_SUCCESS; 61 : 0 : } 62 : : 63 : : static int s2n_client_pq_kem_recv(struct s2n_connection *conn, struct s2n_stuffer *extension) 64 : 1 : { 65 : 1 : uint16_t size_of_all = 0; 66 : 1 : struct s2n_blob *proposed_kems = &conn->kex_params.client_pq_kem_extension; 67 : : 68 : : /* Ignore extension if PQ is disabled */ 69 [ + - ]: 1 : if (!s2n_pq_is_enabled()) { 70 : 1 : return S2N_SUCCESS; 71 : 1 : } 72 : : 73 [ # # ]: 0 : POSIX_GUARD(s2n_stuffer_read_uint16(extension, &size_of_all)); 74 [ # # ][ # # ]: 0 : if (size_of_all > s2n_stuffer_data_available(extension) || size_of_all % sizeof(kem_extension_size)) { 75 : : /* Malformed length, ignore the extension */ 76 : 0 : return S2N_SUCCESS; 77 : 0 : } 78 : : 79 : 0 : proposed_kems->size = size_of_all; 80 : 0 : proposed_kems->data = s2n_stuffer_raw_read(extension, proposed_kems->size); 81 [ # # ][ # # ]: 0 : POSIX_ENSURE_REF(proposed_kems->data); 82 : : 83 : 0 : return S2N_SUCCESS; 84 : 0 : }