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