s2n_aead.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #include "error/s2n_errno.h"
  16. #include "tls/s2n_connection.h"
  17. #include "tls/s2n_record.h"
  18. #include "utils/s2n_mem.h"
  19. #include "utils/s2n_safety.h"
  20. /* Derive the AAD for an AEAD mode cipher suite from the connection state, per
  21. * RFC 5246 section 6.2.3.3 */
  22. S2N_RESULT s2n_aead_aad_init(const struct s2n_connection *conn, uint8_t *sequence_number, uint8_t content_type, uint16_t record_length, struct s2n_blob *ad)
  23. {
  24. RESULT_ENSURE_REF(ad);
  25. RESULT_ENSURE_GTE(ad->size, S2N_TLS_MAX_AAD_LEN);
  26. uint8_t *data = ad->data;
  27. RESULT_GUARD_PTR(data);
  28. /* ad = seq_num || record_type || version || length */
  29. size_t idx = 0;
  30. for (; idx < S2N_TLS_SEQUENCE_NUM_LEN; idx++) {
  31. data[idx] = sequence_number[idx];
  32. }
  33. data[idx++] = content_type;
  34. data[idx++] = conn->actual_protocol_version / 10;
  35. data[idx++] = conn->actual_protocol_version % 10;
  36. data[idx++] = record_length >> 8;
  37. data[idx++] = record_length & UINT8_MAX;
  38. /* Double check no overflow */
  39. RESULT_ENSURE_LTE(idx, ad->size);
  40. return S2N_RESULT_OK;
  41. }
  42. /* Prepares an AAD (additional authentication data) for a TLS 1.3 AEAD record */
  43. S2N_RESULT s2n_tls13_aead_aad_init(uint16_t record_length, uint8_t tag_length, struct s2n_blob *additional_data)
  44. {
  45. RESULT_ENSURE_GT(tag_length, 0);
  46. RESULT_ENSURE_REF(additional_data);
  47. RESULT_ENSURE_GTE(additional_data->size, S2N_TLS13_AAD_LEN);
  48. uint8_t *data = additional_data->data;
  49. RESULT_GUARD_PTR(data);
  50. size_t idx = 0;
  51. /**
  52. *= https://tools.ietf.org/rfc/rfc8446#section-5.2
  53. *# opaque_type: The outer opaque_type field of a TLSCiphertext record
  54. *# is always set to the value 23 (application_data) for outward
  55. *# compatibility with middleboxes accustomed to parsing previous
  56. *# versions of TLS. The actual content type of the record is found
  57. *# in TLSInnerPlaintext.type after decryption.
  58. **/
  59. data[idx++] = TLS_APPLICATION_DATA;
  60. /**
  61. *= https://tools.ietf.org/rfc/rfc8446#section-5.2
  62. *# legacy_record_version: The legacy_record_version field is always
  63. *# 0x0303. TLS 1.3 TLSCiphertexts are not generated until after
  64. *# TLS 1.3 has been negotiated, so there are no historical
  65. *# compatibility concerns where other values might be received. Note
  66. *# that the handshake protocol, including the ClientHello and
  67. *# ServerHello messages, authenticates the protocol version, so this
  68. *# value is redundant.
  69. */
  70. data[idx++] = 0x03;
  71. data[idx++] = 0x03;
  72. /**
  73. *= https://tools.ietf.org/rfc/rfc8446#section-5.2
  74. *# length: The length (in bytes) of the following
  75. *# TLSCiphertext.encrypted_record, which is the sum of the lengths of
  76. *# the content and the padding, plus one for the inner content type,
  77. *# plus any expansion added by the AEAD algorithm. The length
  78. *# MUST NOT exceed 2^14 + 256 bytes. An endpoint that receives a
  79. *# record that exceeds this length MUST terminate the connection with
  80. *# a "record_overflow" alert.
  81. */
  82. uint16_t length = record_length + tag_length;
  83. RESULT_ENSURE(length <= (1 << 14) + 256, S2N_ERR_RECORD_LIMIT);
  84. data[idx++] = length >> 8;
  85. data[idx++] = length & UINT8_MAX;
  86. /* Double check no overflow */
  87. RESULT_ENSURE_LTE(idx, additional_data->size);
  88. return S2N_RESULT_OK;
  89. }