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