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.h" 25 : : #include "tls/s2n_record_read.h" 26 : : #include "utils/s2n_annotations.h" 27 : : #include "utils/s2n_blob.h" 28 : : #include "utils/s2n_safety.h" 29 : : 30 : : int s2n_record_parse_aead( 31 : : const struct s2n_cipher_suite *cipher_suite, 32 : : struct s2n_connection *conn, 33 : : struct s2n_record_header *header, 34 : : uint8_t *implicit_iv, 35 : : struct s2n_hmac_state *mac, 36 : : uint8_t *sequence_number, 37 : : struct s2n_session_key *session_key) 38 : 3387037 : { 39 : 3387037 : const int is_tls13_record = cipher_suite->record_alg->flags & S2N_TLS13_RECORD_AEAD_NONCE; 40 : : /* TLS 1.3 record protection uses a different 5 byte associated data than TLS 1.2's */ 41 [ - + ][ # # ]: 3387037 : s2n_stack_blob(aad, is_tls13_record ? S2N_TLS13_AAD_LEN : S2N_TLS_MAX_AAD_LEN, S2N_TLS_MAX_AAD_LEN); [ - + ][ + + ] 42 : : 43 : 3387037 : struct s2n_blob en = { 0 }; 44 [ - + ]: 3387037 : POSIX_GUARD(s2n_blob_init(&en, s2n_stuffer_raw_read(&conn->in, header->length), header->length)); 45 [ # # ][ - + ]: 3387037 : POSIX_ENSURE_REF(en.data); 46 : : /* In AEAD mode, the explicit IV is in the record */ 47 [ - + ][ # # ]: 3387037 : POSIX_ENSURE_GTE(en.size, cipher_suite->record_alg->cipher->io.aead.record_iv_size); 48 : : 49 : 3387037 : uint8_t aad_iv[S2N_TLS_MAX_IV_LEN] = { 0 }; 50 : 3387037 : struct s2n_blob iv = { 0 }; 51 [ - + ]: 3387037 : POSIX_GUARD(s2n_blob_init(&iv, aad_iv, sizeof(aad_iv))); 52 : 3387037 : struct s2n_stuffer iv_stuffer = { 0 }; 53 [ - + ]: 3387037 : POSIX_GUARD(s2n_stuffer_init(&iv_stuffer, &iv)); 54 : : 55 [ + + ]: 3387037 : if (cipher_suite->record_alg->flags & S2N_TLS12_AES_GCM_AEAD_NONCE) { 56 : : /* Partially explicit nonce. See RFC 5288 Section 3 */ 57 [ - + ]: 2259136 : POSIX_GUARD(s2n_stuffer_write_bytes(&iv_stuffer, implicit_iv, cipher_suite->record_alg->cipher->io.aead.fixed_iv_size)); 58 [ - + ]: 2259136 : POSIX_GUARD(s2n_stuffer_write_bytes(&iv_stuffer, en.data, cipher_suite->record_alg->cipher->io.aead.record_iv_size)); 59 [ + + ][ + - ]: 2259136 : } else if (cipher_suite->record_alg->flags & S2N_TLS12_CHACHA_POLY_AEAD_NONCE || is_tls13_record) { 60 : : /* Fully implicit nonce. 61 : : * This is introduced with ChaChaPoly with RFC 7905 Section 2 62 : : * and also used for TLS 1.3 record protection (RFC 8446 Section 5.2). 63 : : * 64 : : * In these cipher modes, the sequence number (64 bits) is left padded by 4 bytes 65 : : * to align and xor-ed with the 96-bit IV. 66 : : **/ 67 : 1127901 : uint8_t four_zeroes[4] = { 0 }; 68 [ - + ]: 1127901 : POSIX_GUARD(s2n_stuffer_write_bytes(&iv_stuffer, four_zeroes, 4)); 69 [ - + ]: 1127901 : POSIX_GUARD(s2n_stuffer_write_bytes(&iv_stuffer, sequence_number, S2N_TLS_SEQUENCE_NUM_LEN)); 70 [ + + ]: 14662713 : for (int i = 0; i < cipher_suite->record_alg->cipher->io.aead.fixed_iv_size; i++) { 71 : 13534812 : S2N_INVARIANT(i <= cipher_suite->record_alg->cipher->io.aead.fixed_iv_size); 72 : 13534812 : aad_iv[i] = aad_iv[i] ^ implicit_iv[i]; 73 : 13534812 : } 74 : 1127901 : } else { 75 [ # # ]: 0 : POSIX_BAIL(S2N_ERR_INVALID_NONCE_TYPE); 76 : 0 : } 77 : : 78 : : /* Set the IV size to the amount of data written */ 79 : 3387037 : iv.size = s2n_stuffer_data_available(&iv_stuffer); 80 : : 81 : 3387037 : uint16_t payload_length = header->length; 82 : : /* remove the AEAD overhead from the record size */ 83 [ + + ][ + - ]: 3387037 : POSIX_ENSURE_GTE(payload_length, cipher_suite->record_alg->cipher->io.aead.record_iv_size + cipher_suite->record_alg->cipher->io.aead.tag_size); 84 : 3387035 : payload_length -= cipher_suite->record_alg->cipher->io.aead.record_iv_size; 85 : 3387035 : payload_length -= cipher_suite->record_alg->cipher->io.aead.tag_size; 86 : : 87 [ + + ]: 3387035 : if (is_tls13_record) { 88 [ - + ]: 70056 : POSIX_GUARD_RESULT(s2n_tls13_aead_aad_init(header, &aad)); 89 : 3316979 : } else { 90 [ - + ]: 3316979 : POSIX_GUARD_RESULT(s2n_aead_aad_init(conn, sequence_number, header->content_type, payload_length, &aad)); 91 : 3316979 : } 92 : : 93 : : /* Decrypt stuff! */ 94 : : /* Skip explicit IV for decryption */ 95 : 3387035 : en.size -= cipher_suite->record_alg->cipher->io.aead.record_iv_size; 96 : 3387035 : en.data += cipher_suite->record_alg->cipher->io.aead.record_iv_size; 97 : : 98 : : /* Check that we have some data to decrypt */ 99 [ # # ][ - + ]: 3387035 : POSIX_ENSURE_NE(en.size, 0); 100 : : 101 [ + + ]: 3387035 : POSIX_GUARD(cipher_suite->record_alg->cipher->io.aead.decrypt(session_key, &iv, &aad, &en, &en)); 102 : 194492 : struct s2n_blob seq = { 0 }; 103 [ - + ]: 194492 : POSIX_GUARD(s2n_blob_init(&seq, sequence_number, S2N_TLS_SEQUENCE_NUM_LEN)); 104 [ - + ]: 194492 : POSIX_GUARD(s2n_increment_sequence_number(&seq)); 105 : : 106 : : /* O.k., we've successfully read and decrypted the record, now we need to align the stuffer 107 : : * for reading the plaintext data. 108 : : */ 109 [ - + ]: 194492 : POSIX_GUARD(s2n_stuffer_reread(&conn->in)); 110 : : 111 : : /* Skip the IV, if any */ 112 [ + - ]: 194492 : if (conn->actual_protocol_version >= S2N_TLS12) { 113 [ - + ]: 194492 : POSIX_GUARD(s2n_stuffer_skip_read(&conn->in, cipher_suite->record_alg->cipher->io.aead.record_iv_size)); 114 : 194492 : } 115 : : 116 : : /* Truncate and wipe the MAC and any padding */ 117 : 194492 : uint32_t mac_len = 0; 118 [ - + ]: 194492 : POSIX_GUARD(s2n_sub_overflow(s2n_stuffer_data_available(&conn->in), payload_length, &mac_len)); 119 [ - + ]: 194492 : POSIX_GUARD(s2n_stuffer_wipe_n(&conn->in, mac_len)); 120 : 194492 : conn->in_status = PLAINTEXT; 121 : : 122 : 194492 : return 0; 123 : 194492 : }