s2n_server_finished.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 <stdint.h>
  16. #include "error/s2n_errno.h"
  17. #include "stuffer/s2n_stuffer.h"
  18. #include "tls/s2n_connection.h"
  19. #include "tls/s2n_resume.h"
  20. #include "tls/s2n_tls.h"
  21. #include "tls/s2n_tls13_handshake.h"
  22. #include "utils/s2n_safety.h"
  23. S2N_RESULT s2n_finished_recv(struct s2n_connection *conn, uint8_t *local_verify_data)
  24. {
  25. RESULT_ENSURE_REF(conn);
  26. uint8_t length = conn->handshake.finished_len;
  27. /* Recalculate length to ensure that we're validating the right number of bytes */
  28. if (conn->actual_protocol_version == S2N_SSLv3) {
  29. RESULT_ENSURE_EQ(length, S2N_SSL_FINISHED_LEN);
  30. } else {
  31. RESULT_ENSURE_EQ(length, S2N_TLS_FINISHED_LEN);
  32. }
  33. uint8_t *peer_verify_data = s2n_stuffer_raw_read(&conn->handshake.io, length);
  34. RESULT_ENSURE_REF(peer_verify_data);
  35. RESULT_ENSURE(s2n_constant_time_equals(local_verify_data, peer_verify_data, length), S2N_ERR_BAD_MESSAGE);
  36. return S2N_RESULT_OK;
  37. }
  38. S2N_RESULT s2n_finished_send(struct s2n_connection *conn, uint8_t *verify_data)
  39. {
  40. RESULT_ENSURE_REF(conn);
  41. uint8_t length = conn->handshake.finished_len;
  42. RESULT_ENSURE_GT(length, 0);
  43. RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(&conn->handshake.io, verify_data, length));
  44. return S2N_RESULT_OK;
  45. }
  46. int s2n_server_finished_recv(struct s2n_connection *conn)
  47. {
  48. POSIX_ENSURE_REF(conn);
  49. uint8_t *verify_data = conn->handshake.server_finished;
  50. POSIX_GUARD_RESULT(s2n_finished_recv(conn, verify_data));
  51. return S2N_SUCCESS;
  52. }
  53. int s2n_server_finished_send(struct s2n_connection *conn)
  54. {
  55. POSIX_ENSURE_REF(conn);
  56. uint8_t *verify_data = conn->handshake.server_finished;
  57. POSIX_GUARD(s2n_prf_server_finished(conn));
  58. POSIX_GUARD_RESULT(s2n_finished_send(conn, verify_data));
  59. POSIX_GUARD_RESULT(s2n_crypto_parameters_switch(conn));
  60. if (s2n_connection_is_session_resumed(conn)) {
  61. POSIX_GUARD(s2n_prf_key_expansion(conn));
  62. }
  63. return S2N_SUCCESS;
  64. }
  65. int s2n_tls13_server_finished_recv(struct s2n_connection *conn)
  66. {
  67. POSIX_ENSURE_EQ(conn->actual_protocol_version, S2N_TLS13);
  68. uint8_t length = s2n_stuffer_data_available(&conn->handshake.io);
  69. S2N_ERROR_IF(length == 0, S2N_ERR_BAD_MESSAGE);
  70. /* read finished mac from handshake */
  71. struct s2n_blob wire_finished_mac = { 0 };
  72. s2n_blob_init(&wire_finished_mac, s2n_stuffer_raw_read(&conn->handshake.io, length), length);
  73. /* get tls13 keys */
  74. s2n_tls13_connection_keys(keys, conn);
  75. /* get transcript hash */
  76. POSIX_ENSURE_REF(conn->handshake.hashes);
  77. struct s2n_hash_state *hash_state = &conn->handshake.hashes->hash_workspace;
  78. POSIX_GUARD_RESULT(s2n_handshake_copy_hash_state(conn, keys.hash_algorithm, hash_state));
  79. /* look up finished secret key */
  80. struct s2n_blob finished_key = { 0 };
  81. POSIX_GUARD(s2n_blob_init(&finished_key, conn->handshake.server_finished, keys.size));
  82. /* generate the hashed message authenticated code */
  83. s2n_tls13_key_blob(server_finished_mac, keys.size);
  84. POSIX_GUARD(s2n_tls13_calculate_finished_mac(&keys, &finished_key, hash_state, &server_finished_mac));
  85. /* compare mac with received message */
  86. POSIX_GUARD(s2n_tls13_mac_verify(&keys, &server_finished_mac, &wire_finished_mac));
  87. return 0;
  88. }
  89. int s2n_tls13_server_finished_send(struct s2n_connection *conn)
  90. {
  91. POSIX_ENSURE_EQ(conn->actual_protocol_version, S2N_TLS13);
  92. /* get tls13 keys */
  93. s2n_tls13_connection_keys(keys, conn);
  94. /* get transcript hash */
  95. POSIX_ENSURE_REF(conn->handshake.hashes);
  96. struct s2n_hash_state *hash_state = &conn->handshake.hashes->hash_workspace;
  97. POSIX_GUARD_RESULT(s2n_handshake_copy_hash_state(conn, keys.hash_algorithm, hash_state));
  98. /* look up finished secret key */
  99. struct s2n_blob finished_key = { 0 };
  100. POSIX_GUARD(s2n_blob_init(&finished_key, conn->handshake.server_finished, keys.size));
  101. /* generate the hashed message authenticated code */
  102. s2n_tls13_key_blob(server_finished_mac, keys.size);
  103. POSIX_GUARD(s2n_tls13_calculate_finished_mac(&keys, &finished_key, hash_state, &server_finished_mac));
  104. /* write to handshake io */
  105. POSIX_GUARD(s2n_stuffer_write(&conn->handshake.io, &server_finished_mac));
  106. return 0;
  107. }