s2n_handshake_transcript.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 "stuffer/s2n_stuffer.h"
  16. #include "tls/s2n_connection.h"
  17. #include "tls/s2n_tls.h"
  18. #include "tls/s2n_tls13_handshake.h"
  19. #include "utils/s2n_blob.h"
  20. /* Length of the synthetic message header */
  21. #define MESSAGE_HASH_HEADER_LENGTH 4
  22. S2N_RESULT s2n_handshake_transcript_update(struct s2n_connection *conn)
  23. {
  24. RESULT_ENSURE_REF(conn);
  25. struct s2n_stuffer message = conn->handshake.io;
  26. RESULT_GUARD_POSIX(s2n_stuffer_reread(&message));
  27. struct s2n_blob data = { 0 };
  28. uint32_t len = s2n_stuffer_data_available(&message);
  29. uint8_t *bytes = s2n_stuffer_raw_read(&message, len);
  30. RESULT_ENSURE_REF(bytes);
  31. RESULT_GUARD_POSIX(s2n_blob_init(&data, bytes, len));
  32. RESULT_GUARD_POSIX(s2n_conn_update_handshake_hashes(conn, &data));
  33. return S2N_RESULT_OK;
  34. }
  35. int s2n_conn_update_handshake_hashes(struct s2n_connection *conn, struct s2n_blob *data)
  36. {
  37. POSIX_ENSURE_REF(conn);
  38. POSIX_ENSURE_REF(data);
  39. struct s2n_handshake_hashes *hashes = conn->handshake.hashes;
  40. POSIX_ENSURE_REF(hashes);
  41. if (s2n_handshake_is_hash_required(&conn->handshake, S2N_HASH_MD5)) {
  42. /* The handshake MD5 hash state will fail the s2n_hash_is_available() check
  43. * since MD5 is not permitted in FIPS mode. This check will not be used as
  44. * the handshake MD5 hash state is specifically used by the TLS 1.0 and TLS 1.1
  45. * PRF, which is required to comply with the TLS 1.0 and 1.1 RFCs and is approved
  46. * as per NIST Special Publication 800-52 Revision 1.
  47. */
  48. POSIX_GUARD(s2n_hash_update(&hashes->md5, data->data, data->size));
  49. }
  50. if (s2n_handshake_is_hash_required(&conn->handshake, S2N_HASH_SHA1)) {
  51. POSIX_GUARD(s2n_hash_update(&hashes->sha1, data->data, data->size));
  52. }
  53. const uint8_t md5_sha1_required =
  54. (s2n_handshake_is_hash_required(&conn->handshake, S2N_HASH_MD5)
  55. && s2n_handshake_is_hash_required(&conn->handshake, S2N_HASH_SHA1));
  56. if (md5_sha1_required) {
  57. /* The MD5_SHA1 hash can still be used for TLS 1.0 and 1.1 in FIPS mode for
  58. * the handshake hashes. This will only be used for the signature check in the
  59. * CertificateVerify message and the PRF. NIST SP 800-52r1 approves use
  60. * of MD5_SHA1 for these use cases (see footnotes 15 and 20, and section
  61. * 3.3.2) */
  62. POSIX_GUARD(s2n_hash_update(&hashes->md5_sha1, data->data, data->size));
  63. }
  64. if (s2n_handshake_is_hash_required(&conn->handshake, S2N_HASH_SHA224)) {
  65. POSIX_GUARD(s2n_hash_update(&hashes->sha224, data->data, data->size));
  66. }
  67. if (s2n_handshake_is_hash_required(&conn->handshake, S2N_HASH_SHA256)) {
  68. POSIX_GUARD(s2n_hash_update(&hashes->sha256, data->data, data->size));
  69. }
  70. if (s2n_handshake_is_hash_required(&conn->handshake, S2N_HASH_SHA384)) {
  71. POSIX_GUARD(s2n_hash_update(&hashes->sha384, data->data, data->size));
  72. }
  73. if (s2n_handshake_is_hash_required(&conn->handshake, S2N_HASH_SHA512)) {
  74. POSIX_GUARD(s2n_hash_update(&hashes->sha512, data->data, data->size));
  75. }
  76. return S2N_SUCCESS;
  77. }
  78. /* When a HelloRetryRequest message is used, the hash transcript needs to be recreated.
  79. * This is done with a synthetic message header, and the hash of ClientHello1.
  80. *
  81. * https://tools.ietf.org/html/rfc8446#section-4.4.1
  82. */
  83. int s2n_server_hello_retry_recreate_transcript(struct s2n_connection *conn)
  84. {
  85. POSIX_ENSURE_REF(conn);
  86. struct s2n_handshake_hashes *hashes = conn->handshake.hashes;
  87. POSIX_ENSURE_REF(hashes);
  88. s2n_tls13_connection_keys(keys, conn);
  89. uint8_t hash_digest_length = keys.size;
  90. /* Create the MessageHash (our synthetic message) */
  91. uint8_t msghdr[MESSAGE_HASH_HEADER_LENGTH] = { 0 };
  92. msghdr[0] = TLS_MESSAGE_HASH;
  93. msghdr[MESSAGE_HASH_HEADER_LENGTH - 1] = hash_digest_length;
  94. /* Grab the current transcript hash to use as the ClientHello1 value. */
  95. struct s2n_hash_state *client_hello1_hash = &hashes->hash_workspace;
  96. uint8_t client_hello1_digest_out[S2N_MAX_DIGEST_LEN] = { 0 };
  97. POSIX_GUARD_RESULT(s2n_handshake_copy_hash_state(conn, keys.hash_algorithm, client_hello1_hash));
  98. POSIX_GUARD(s2n_hash_digest(client_hello1_hash, client_hello1_digest_out, hash_digest_length));
  99. /* Step 1: Reset the hash state */
  100. POSIX_GUARD_RESULT(s2n_handshake_reset_hash_state(conn, keys.hash_algorithm));
  101. /* Step 2: Update the transcript with the synthetic message */
  102. struct s2n_blob msg_blob = { 0 };
  103. POSIX_GUARD(s2n_blob_init(&msg_blob, msghdr, MESSAGE_HASH_HEADER_LENGTH));
  104. POSIX_GUARD(s2n_conn_update_handshake_hashes(conn, &msg_blob));
  105. /* Step 3: Update the transcript with the ClientHello1 hash */
  106. POSIX_GUARD(s2n_blob_init(&msg_blob, client_hello1_digest_out, hash_digest_length));
  107. POSIX_GUARD(s2n_conn_update_handshake_hashes(conn, &msg_blob));
  108. return S2N_SUCCESS;
  109. }