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 : : uint8_t content_type, 32 : : uint16_t encrypted_length, 33 : : uint8_t *implicit_iv, 34 : : struct s2n_hmac_state *mac, 35 : : uint8_t *sequence_number, 36 : : struct s2n_session_key *session_key) 37 : 74617 : { 38 : : /* Add the header to the HMAC */ 39 : 74617 : uint8_t *header = s2n_stuffer_raw_read(&conn->header_in, S2N_TLS_RECORD_HEADER_LENGTH); 40 [ - + ][ # # ]: 74617 : POSIX_ENSURE_REF(header); 41 : : 42 : 74617 : struct s2n_blob en = { .size = encrypted_length, .data = s2n_stuffer_raw_read(&conn->in, encrypted_length) }; 43 [ - + ][ # # ]: 74617 : POSIX_ENSURE_REF(en.data); 44 : : 45 : 74617 : uint16_t payload_length = encrypted_length; 46 : 74617 : uint8_t mac_digest_size = 0; 47 [ - + ]: 74617 : POSIX_GUARD(s2n_hmac_digest_size(mac->alg, &mac_digest_size)); 48 : : 49 [ + - ][ + + ]: 74617 : POSIX_ENSURE_GTE(payload_length, mac_digest_size); 50 : 74597 : payload_length -= mac_digest_size; 51 : : 52 : : /* Decrypt stuff! */ 53 [ - + ]: 74597 : POSIX_GUARD(cipher_suite->record_alg->cipher->io.stream.decrypt(session_key, &en, &en)); 54 : : 55 : : /* Update the MAC */ 56 : 74597 : header[3] = (payload_length >> 8); 57 : 74597 : header[4] = payload_length & 0xff; 58 [ - + ]: 74597 : POSIX_GUARD(s2n_hmac_reset(mac)); 59 [ - + ]: 74597 : POSIX_GUARD(s2n_hmac_update(mac, sequence_number, S2N_TLS_SEQUENCE_NUM_LEN)); 60 : : 61 [ + + ]: 74597 : if (conn->actual_protocol_version == S2N_SSLv3) { 62 [ - + ]: 357 : POSIX_GUARD(s2n_hmac_update(mac, header, 1)); 63 [ - + ]: 357 : POSIX_GUARD(s2n_hmac_update(mac, header + 3, 2)); 64 : 74240 : } else { 65 [ - + ]: 74240 : POSIX_GUARD(s2n_hmac_update(mac, header, S2N_TLS_RECORD_HEADER_LENGTH)); 66 : 74240 : } 67 : : 68 : 74597 : struct s2n_blob seq = { .data = sequence_number, .size = S2N_TLS_SEQUENCE_NUM_LEN }; 69 [ - + ]: 74597 : POSIX_GUARD(s2n_increment_sequence_number(&seq)); 70 : : 71 : : /* MAC check for streaming ciphers - no padding */ 72 [ - + ]: 74597 : POSIX_GUARD(s2n_hmac_update(mac, en.data, payload_length)); 73 : : 74 : 74597 : uint8_t check_digest[S2N_MAX_DIGEST_LEN]; 75 [ - + ][ # # ]: 74597 : POSIX_ENSURE_LTE(mac_digest_size, sizeof(check_digest)); 76 [ - + ]: 74597 : POSIX_GUARD(s2n_hmac_digest(mac, check_digest, mac_digest_size)); 77 : : 78 [ + + ]: 74597 : if (s2n_hmac_digest_verify(en.data + payload_length, check_digest, mac_digest_size) < 0) { 79 [ + - ]: 16158 : POSIX_BAIL(S2N_ERR_BAD_MESSAGE); 80 : 16158 : } 81 : : 82 : : /* O.k., we've successfully read and decrypted the record, now we need to align the stuffer 83 : : * for reading the plaintext data. 84 : : */ 85 [ - + ]: 58439 : POSIX_GUARD(s2n_stuffer_reread(&conn->in)); 86 [ - + ]: 58439 : POSIX_GUARD(s2n_stuffer_reread(&conn->header_in)); 87 : : 88 : : /* Truncate and wipe the MAC and any padding */ 89 [ - + ]: 58439 : POSIX_GUARD(s2n_stuffer_wipe_n(&conn->in, s2n_stuffer_data_available(&conn->in) - payload_length)); 90 : 58439 : conn->in_status = PLAINTEXT; 91 : : 92 : 58439 : return 0; 93 : 58439 : }