| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 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 |
uint8_t content_type, |
| 34 |
uint16_t encrypted_length, |
| 35 |
uint8_t *implicit_iv, |
| 36 |
struct s2n_hmac_state *mac, |
| 37 |
uint8_t *sequence_number, |
| 38 |
struct s2n_session_key *session_key) |
| 39 |
{ |
| 40 |
const int is_tls13_record = cipher_suite->record_alg->flags & S2N_TLS13_RECORD_AEAD_NONCE; |
| 41 |
|
| 42 |
s2n_stack_blob(aad, is_tls13_record ? S2N_TLS13_AAD_LEN : S2N_TLS_MAX_AAD_LEN, S2N_TLS_MAX_AAD_LEN); |
| 43 |
|
| 44 |
struct s2n_blob en = { 0 }; |
| 45 |
POSIX_GUARD(s2n_blob_init(&en, s2n_stuffer_raw_read(&conn->in, encrypted_length), encrypted_length)); |
| 46 |
POSIX_ENSURE_REF(en.data); |
| 47 |
|
| 48 |
POSIX_ENSURE_GTE(en.size, cipher_suite->record_alg->cipher->io.aead.record_iv_size); |
| 49 |
|
| 50 |
uint8_t aad_iv[S2N_TLS_MAX_IV_LEN] = { 0 }; |
| 51 |
struct s2n_blob iv = { 0 }; |
| 52 |
POSIX_GUARD(s2n_blob_init(&iv, aad_iv, sizeof(aad_iv))); |
| 53 |
struct s2n_stuffer iv_stuffer = { 0 }; |
| 54 |
POSIX_GUARD(s2n_stuffer_init(&iv_stuffer, &iv)); |
| 55 |
|
| 56 |
if (cipher_suite->record_alg->flags & S2N_TLS12_AES_GCM_AEAD_NONCE) { |
| 57 |
|
| 58 |
POSIX_GUARD(s2n_stuffer_write_bytes(&iv_stuffer, implicit_iv, cipher_suite->record_alg->cipher->io.aead.fixed_iv_size)); |
| 59 |
POSIX_GUARD(s2n_stuffer_write_bytes(&iv_stuffer, en.data, cipher_suite->record_alg->cipher->io.aead.record_iv_size)); |
| 60 |
} else if (cipher_suite->record_alg->flags & S2N_TLS12_CHACHA_POLY_AEAD_NONCE || is_tls13_record) { |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
uint8_t four_zeroes[4] = { 0 }; |
| 69 |
POSIX_GUARD(s2n_stuffer_write_bytes(&iv_stuffer, four_zeroes, 4)); |
| 70 |
POSIX_GUARD(s2n_stuffer_write_bytes(&iv_stuffer, sequence_number, S2N_TLS_SEQUENCE_NUM_LEN)); |
| 71 |
for (int i = 0; i < cipher_suite->record_alg->cipher->io.aead.fixed_iv_size; i++) { |
| 72 |
S2N_INVARIANT(i <= cipher_suite->record_alg->cipher->io.aead.fixed_iv_size); |
| 73 |
aad_iv[i] = aad_iv[i] ^ implicit_iv[i]; |
| 74 |
} |
| 75 |
} else { |
| 76 |
POSIX_BAIL(S2N_ERR_INVALID_NONCE_TYPE); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
iv.size = s2n_stuffer_data_available(&iv_stuffer); |
| 81 |
|
| 82 |
uint16_t payload_length = encrypted_length; |
| 83 |
|
| 84 |
POSIX_ENSURE_GTE(payload_length, cipher_suite->record_alg->cipher->io.aead.record_iv_size + cipher_suite->record_alg->cipher->io.aead.tag_size); |
| 85 |
payload_length -= cipher_suite->record_alg->cipher->io.aead.record_iv_size; |
| 86 |
payload_length -= cipher_suite->record_alg->cipher->io.aead.tag_size; |
| 87 |
|
| 88 |
if (is_tls13_record) { |
| 89 |
POSIX_GUARD_RESULT(s2n_tls13_aead_aad_init(payload_length, cipher_suite->record_alg->cipher->io.aead.tag_size, &aad)); |
| 90 |
} else { |
| 91 |
POSIX_GUARD_RESULT(s2n_aead_aad_init(conn, sequence_number, content_type, payload_length, &aad)); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
en.size -= cipher_suite->record_alg->cipher->io.aead.record_iv_size; |
| 97 |
en.data += cipher_suite->record_alg->cipher->io.aead.record_iv_size; |
| 98 |
|
| 99 |
|
| 100 |
POSIX_ENSURE_NE(en.size, 0); |
| 101 |
|
| 102 |
POSIX_GUARD(cipher_suite->record_alg->cipher->io.aead.decrypt(session_key, &iv, &aad, &en, &en)); |
| 103 |
struct s2n_blob seq = { 0 }; |
| 104 |
POSIX_GUARD(s2n_blob_init(&seq, sequence_number, S2N_TLS_SEQUENCE_NUM_LEN)); |
| 105 |
POSIX_GUARD(s2n_increment_sequence_number(&seq)); |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
POSIX_GUARD(s2n_stuffer_reread(&conn->in)); |
| 111 |
POSIX_GUARD(s2n_stuffer_reread(&conn->header_in)); |
| 112 |
|
| 113 |
|
| 114 |
if (conn->actual_protocol_version >= S2N_TLS12) { |
| 115 |
POSIX_GUARD(s2n_stuffer_skip_read(&conn->in, cipher_suite->record_alg->cipher->io.aead.record_iv_size)); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
POSIX_GUARD(s2n_stuffer_wipe_n(&conn->in, s2n_stuffer_data_available(&conn->in) - payload_length)); |
| 120 |
conn->in_status = PLAINTEXT; |
| 121 |
|
| 122 |
return 0; |
| 123 |
} |