s2n_client_finished.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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_tls.h"
  20. #include "tls/s2n_tls13_handshake.h"
  21. #include "utils/s2n_safety.h"
  22. S2N_RESULT s2n_finished_recv(struct s2n_connection *conn, uint8_t *our_version);
  23. S2N_RESULT s2n_finished_send(struct s2n_connection *conn, uint8_t *our_version);
  24. int s2n_client_finished_recv(struct s2n_connection *conn)
  25. {
  26. POSIX_ENSURE_REF(conn);
  27. POSIX_GUARD(s2n_prf_client_finished(conn));
  28. uint8_t *verify_data = conn->handshake.client_finished;
  29. POSIX_GUARD_RESULT(s2n_finished_recv(conn, verify_data));
  30. POSIX_ENSURE(!conn->handshake.rsa_failed, S2N_ERR_BAD_MESSAGE);
  31. return S2N_SUCCESS;
  32. }
  33. int s2n_client_finished_send(struct s2n_connection *conn)
  34. {
  35. POSIX_ENSURE_REF(conn);
  36. POSIX_GUARD(s2n_prf_client_finished(conn));
  37. uint8_t *verify_data = conn->handshake.client_finished;
  38. POSIX_GUARD_RESULT(s2n_finished_send(conn, verify_data));
  39. POSIX_GUARD_RESULT(s2n_crypto_parameters_switch(conn));
  40. return S2N_SUCCESS;
  41. }
  42. int s2n_tls13_client_finished_recv(struct s2n_connection *conn)
  43. {
  44. POSIX_ENSURE_EQ(conn->actual_protocol_version, S2N_TLS13);
  45. uint8_t length = s2n_stuffer_data_available(&conn->handshake.io);
  46. S2N_ERROR_IF(length == 0, S2N_ERR_BAD_MESSAGE);
  47. /* read finished mac from handshake */
  48. struct s2n_blob wire_finished_mac = { 0 };
  49. s2n_blob_init(&wire_finished_mac, s2n_stuffer_raw_read(&conn->handshake.io, length), length);
  50. /* get tls13 keys */
  51. s2n_tls13_connection_keys(keys, conn);
  52. /* get transcript hash */
  53. POSIX_ENSURE_REF(conn->handshake.hashes);
  54. struct s2n_hash_state *hash_state = &conn->handshake.hashes->hash_workspace;
  55. POSIX_GUARD_RESULT(s2n_handshake_copy_hash_state(conn, keys.hash_algorithm, hash_state));
  56. struct s2n_blob finished_key = { 0 };
  57. POSIX_GUARD(s2n_blob_init(&finished_key, conn->handshake.client_finished, keys.size));
  58. s2n_tls13_key_blob(client_finished_mac, keys.size);
  59. POSIX_GUARD(s2n_tls13_calculate_finished_mac(&keys, &finished_key, hash_state, &client_finished_mac));
  60. POSIX_GUARD(s2n_tls13_mac_verify(&keys, &client_finished_mac, &wire_finished_mac));
  61. return 0;
  62. }
  63. int s2n_tls13_client_finished_send(struct s2n_connection *conn)
  64. {
  65. POSIX_ENSURE_EQ(conn->actual_protocol_version, S2N_TLS13);
  66. /* get tls13 keys */
  67. s2n_tls13_connection_keys(keys, conn);
  68. /* get transcript hash */
  69. POSIX_ENSURE_REF(conn->handshake.hashes);
  70. struct s2n_hash_state *hash_state = &conn->handshake.hashes->hash_workspace;
  71. POSIX_GUARD_RESULT(s2n_handshake_copy_hash_state(conn, keys.hash_algorithm, hash_state));
  72. /* look up finished secret key */
  73. struct s2n_blob finished_key = { 0 };
  74. POSIX_GUARD(s2n_blob_init(&finished_key, conn->handshake.client_finished, keys.size));
  75. /* generate the hashed message authenticated code */
  76. s2n_stack_blob(client_finished_mac, keys.size, S2N_TLS13_SECRET_MAX_LEN);
  77. POSIX_GUARD(s2n_tls13_calculate_finished_mac(&keys, &finished_key, hash_state, &client_finished_mac));
  78. /* write to handshake io */
  79. POSIX_GUARD(s2n_stuffer_write(&conn->handshake.io, &client_finished_mac));
  80. return 0;
  81. }