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 "crypto/s2n_cipher.h" 17 : : #include "crypto/s2n_hmac.h" 18 : : #include "crypto/s2n_sequence.h" 19 : : #include "error/s2n_errno.h" 20 : : #include "stuffer/s2n_stuffer.h" 21 : : #include "tls/s2n_cipher_suites.h" 22 : : #include "tls/s2n_connection.h" 23 : : #include "tls/s2n_crypto.h" 24 : : #include "tls/s2n_record_read.h" 25 : : #include "utils/s2n_blob.h" 26 : : #include "utils/s2n_safety.h" 27 : : 28 : : int s2n_record_parse_stream( 29 : : const struct s2n_cipher_suite *cipher_suite, 30 : : struct s2n_connection *conn, 31 : : struct s2n_record_header *header, 32 : : uint8_t *implicit_iv, 33 : : struct s2n_hmac_state *mac, 34 : : uint8_t *sequence_number, 35 : : struct s2n_session_key *session_key) 36 : 76826 : { 37 : 76826 : struct s2n_blob en = { .size = header->length, .data = s2n_stuffer_raw_read(&conn->in, header->length) }; 38 [ - + ][ # # ]: 76826 : POSIX_ENSURE_REF(en.data); 39 : : 40 : 76826 : uint16_t payload_length = header->length; 41 : 76826 : uint8_t mac_digest_size = 0; 42 [ - + ]: 76826 : POSIX_GUARD(s2n_hmac_digest_size(mac->alg, &mac_digest_size)); 43 : : 44 [ - + ][ # # ]: 76826 : POSIX_ENSURE_GTE(payload_length, mac_digest_size); 45 : 76826 : payload_length -= mac_digest_size; 46 : : 47 : : /* Decrypt stuff! */ 48 [ - + ]: 76826 : POSIX_GUARD(cipher_suite->record_alg->cipher->io.stream.decrypt(session_key, &en, &en)); 49 : : 50 : : /* Update the MAC */ 51 [ - + ]: 76826 : POSIX_GUARD(s2n_hmac_reset(mac)); 52 [ - + ]: 76826 : POSIX_GUARD(s2n_hmac_update(mac, sequence_number, S2N_TLS_SEQUENCE_NUM_LEN)); 53 : : 54 [ + + ]: 76826 : if (conn->actual_protocol_version == S2N_SSLv3) { 55 [ - + ]: 362 : POSIX_GUARD(s2n_hmac_update(mac, &header->content_type, 1)); 56 [ - + ]: 362 : POSIX_GUARD(s2n_hmac_update_u16(mac, payload_length)); 57 : 76464 : } else { 58 [ - + ]: 76464 : POSIX_GUARD(s2n_hmac_update(mac, &header->content_type, sizeof(header->content_type))); 59 [ - + ]: 76464 : POSIX_GUARD(s2n_hmac_update_u16(mac, header->version)); 60 [ - + ]: 76464 : POSIX_GUARD(s2n_hmac_update_u16(mac, payload_length)); 61 : 76464 : } 62 : : 63 : 76826 : struct s2n_blob seq = { .data = sequence_number, .size = S2N_TLS_SEQUENCE_NUM_LEN }; 64 [ - + ]: 76826 : POSIX_GUARD(s2n_increment_sequence_number(&seq)); 65 : : 66 : : /* MAC check for streaming ciphers - no padding */ 67 [ - + ]: 76826 : POSIX_GUARD(s2n_hmac_update(mac, en.data, payload_length)); 68 : : 69 : 76826 : uint8_t check_digest[S2N_MAX_DIGEST_LEN] = { 0 }; 70 [ - + ][ # # ]: 76826 : POSIX_ENSURE_LTE(mac_digest_size, sizeof(check_digest)); 71 [ - + ]: 76826 : POSIX_GUARD(s2n_hmac_digest(mac, check_digest, mac_digest_size)); 72 : : 73 [ + + ]: 76826 : if (s2n_hmac_digest_verify(en.data + payload_length, check_digest, mac_digest_size) < 0) { 74 [ + - ]: 16178 : POSIX_BAIL(S2N_ERR_BAD_MESSAGE); 75 : 16178 : } 76 : : 77 : : /* O.k., we've successfully read and decrypted the record, now we need to align the stuffer 78 : : * for reading the plaintext data. 79 : : */ 80 [ - + ]: 60648 : POSIX_GUARD(s2n_stuffer_reread(&conn->in)); 81 : : 82 : : /* Truncate and wipe the MAC and any padding */ 83 : 60648 : uint32_t mac_len = 0; 84 [ - + ]: 60648 : POSIX_GUARD(s2n_sub_overflow(s2n_stuffer_data_available(&conn->in), payload_length, &mac_len)); 85 [ - + ]: 60648 : POSIX_GUARD(s2n_stuffer_wipe_n(&conn->in, mac_len)); 86 : 60648 : conn->in_status = PLAINTEXT; 87 : : 88 : 60648 : return 0; 89 : 60648 : }