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_alpn.h" 17 : : 18 : : #include <stdint.h> 19 : : 20 : : #include "tls/extensions/s2n_extension_type.h" 21 : : #include "tls/s2n_protocol_preferences.h" 22 : : #include "tls/s2n_tls.h" 23 : : #include "tls/s2n_tls_parameters.h" 24 : : #include "utils/s2n_safety.h" 25 : : 26 : : bool s2n_client_alpn_should_send(struct s2n_connection *conn); 27 : : static int s2n_client_alpn_send(struct s2n_connection *conn, struct s2n_stuffer *out); 28 : : static int s2n_client_alpn_recv(struct s2n_connection *conn, struct s2n_stuffer *extension); 29 : : 30 : : const s2n_extension_type s2n_client_alpn_extension = { 31 : : .iana_value = TLS_EXTENSION_ALPN, 32 : : .is_response = false, 33 : : .send = s2n_client_alpn_send, 34 : : .recv = s2n_client_alpn_recv, 35 : : .should_send = s2n_client_alpn_should_send, 36 : : .if_missing = s2n_extension_noop_if_missing, 37 : : }; 38 : : 39 : : bool s2n_client_alpn_should_send(struct s2n_connection *conn) 40 : 15464 : { 41 : 15464 : struct s2n_blob *client_app_protocols = NULL; 42 : : 43 [ + + ]: 15464 : return s2n_connection_get_protocol_preferences(conn, &client_app_protocols) == S2N_SUCCESS 44 [ + + ][ + - ]: 15464 : && client_app_protocols->size != 0 && client_app_protocols->data != NULL; 45 : 15464 : } 46 : : 47 : : static int s2n_client_alpn_send(struct s2n_connection *conn, struct s2n_stuffer *out) 48 : 24 : { 49 : 24 : struct s2n_blob *client_app_protocols = NULL; 50 [ - + ]: 24 : POSIX_GUARD(s2n_connection_get_protocol_preferences(conn, &client_app_protocols)); 51 [ # # ][ - + ]: 24 : POSIX_ENSURE_REF(client_app_protocols); 52 : : 53 [ - + ]: 24 : POSIX_GUARD(s2n_stuffer_write_uint16(out, client_app_protocols->size)); 54 [ - + ]: 24 : POSIX_GUARD(s2n_stuffer_write(out, client_app_protocols)); 55 : : 56 : 24 : return S2N_SUCCESS; 57 : 24 : } 58 : : 59 : : static int s2n_client_alpn_recv(struct s2n_connection *conn, struct s2n_stuffer *extension) 60 : 50 : { 61 : 50 : struct s2n_blob *supported_protocols = NULL; 62 [ - + ]: 50 : POSIX_GUARD(s2n_connection_get_protocol_preferences(conn, &supported_protocols)); 63 [ - + ][ # # ]: 50 : POSIX_ENSURE_REF(supported_protocols); 64 : : 65 [ + + ]: 50 : if (supported_protocols->size == 0) { 66 : : /* No protocols configured, nothing to do */ 67 : 16 : return S2N_SUCCESS; 68 : 16 : } 69 : : 70 : 34 : uint16_t wire_size = 0; 71 [ - + ]: 34 : POSIX_GUARD(s2n_stuffer_read_uint16(extension, &wire_size)); 72 [ - + ][ + + ]: 34 : if (wire_size > s2n_stuffer_data_available(extension) || wire_size < 3) { 73 : : /* Malformed length, ignore the extension */ 74 : 2 : return S2N_SUCCESS; 75 : 2 : } 76 : : 77 : 32 : struct s2n_blob client_protocols = { 0 }; 78 [ - + ]: 32 : POSIX_GUARD(s2n_blob_init(&client_protocols, s2n_stuffer_raw_read(extension, wire_size), wire_size)); 79 : : 80 : 32 : struct s2n_stuffer server_protocols = { 0 }; 81 [ - + ]: 32 : POSIX_GUARD(s2n_stuffer_init(&server_protocols, supported_protocols)); 82 [ - + ]: 32 : POSIX_GUARD(s2n_stuffer_skip_write(&server_protocols, supported_protocols->size)); 83 : : 84 [ - + ]: 32 : POSIX_GUARD_RESULT(s2n_select_server_preference_protocol(conn, &server_protocols, &client_protocols)); 85 : : 86 : 32 : return S2N_SUCCESS; 87 : 32 : }