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_server_alpn.h" 17 : : 18 : : #include "stuffer/s2n_stuffer.h" 19 : : #include "tls/s2n_connection.h" 20 : : #include "tls/s2n_tls.h" 21 : : #include "utils/s2n_safety.h" 22 : : 23 : : bool s2n_server_alpn_should_send(struct s2n_connection *conn); 24 : : static int s2n_alpn_send(struct s2n_connection *conn, struct s2n_stuffer *out); 25 : : static int s2n_alpn_recv(struct s2n_connection *conn, struct s2n_stuffer *extension); 26 : : 27 : : const s2n_extension_type s2n_server_alpn_extension = { 28 : : .iana_value = TLS_EXTENSION_ALPN, 29 : : .is_response = true, 30 : : .send = s2n_alpn_send, 31 : : .recv = s2n_alpn_recv, 32 : : .should_send = s2n_server_alpn_should_send, 33 : : .if_missing = s2n_extension_noop_if_missing, 34 : : }; 35 : : 36 : : bool s2n_server_alpn_should_send(struct s2n_connection *conn) 37 : 58 : { 38 [ + - ][ + + ]: 58 : return conn && strlen(conn->application_protocol) > 0; 39 : 58 : } 40 : : 41 : : static int s2n_alpn_send(struct s2n_connection *conn, struct s2n_stuffer *out) 42 : 36 : { 43 [ - + ][ # # ]: 36 : POSIX_ENSURE_REF(conn); 44 : 36 : const uint8_t application_protocol_len = strlen(conn->application_protocol); 45 : : 46 : : /* Size of protocol name list */ 47 [ - + ]: 36 : POSIX_GUARD(s2n_stuffer_write_uint16(out, application_protocol_len + sizeof(uint8_t))); 48 : : 49 : : /* Single entry in protocol name list */ 50 [ - + ]: 36 : POSIX_GUARD(s2n_stuffer_write_uint8(out, application_protocol_len)); 51 [ - + ]: 36 : POSIX_GUARD(s2n_stuffer_write_bytes(out, (uint8_t *) conn->application_protocol, application_protocol_len)); 52 : : 53 : 36 : return S2N_SUCCESS; 54 : 36 : } 55 : : 56 : : static int s2n_alpn_recv(struct s2n_connection *conn, struct s2n_stuffer *extension) 57 : 11 : { 58 [ - + ][ # # ]: 11 : POSIX_ENSURE_REF(conn); 59 : : 60 : 11 : uint16_t size_of_all = 0; 61 [ - + ]: 11 : POSIX_GUARD(s2n_stuffer_read_uint16(extension, &size_of_all)); 62 [ + + ][ - + ]: 11 : if (size_of_all > s2n_stuffer_data_available(extension) || size_of_all < 3) { 63 : : /* ignore invalid extension size */ 64 : 1 : return S2N_SUCCESS; 65 : 1 : } 66 : : 67 : 10 : uint8_t protocol_len = 0; 68 [ - + ]: 10 : POSIX_GUARD(s2n_stuffer_read_uint8(extension, &protocol_len)); 69 : : 70 : 10 : uint8_t *protocol = s2n_stuffer_raw_read(extension, protocol_len); 71 [ # # ][ - + ]: 10 : POSIX_ENSURE_REF(protocol); 72 : : 73 : : /* copy the first protocol name */ 74 [ - + ][ # # ]: 10 : POSIX_CHECKED_MEMCPY(conn->application_protocol, protocol, protocol_len); [ + - ] 75 : 10 : conn->application_protocol[protocol_len] = '\0'; 76 : : 77 : 10 : return S2N_SUCCESS; 78 : 10 : }