s2n_change_cipher_spec.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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_cipher_suites.h"
  19. #include "tls/s2n_connection.h"
  20. #include "tls/s2n_tls.h"
  21. #include "utils/s2n_safety.h"
  22. /* From RFC5246 7.1: https://tools.ietf.org/html/rfc5246#section-7.1 */
  23. #define CHANGE_CIPHER_SPEC_TYPE 1
  24. int s2n_basic_ccs_recv(struct s2n_connection *conn)
  25. {
  26. uint8_t type;
  27. POSIX_GUARD(s2n_stuffer_read_uint8(&conn->handshake.io, &type));
  28. S2N_ERROR_IF(type != CHANGE_CIPHER_SPEC_TYPE, S2N_ERR_BAD_MESSAGE);
  29. return 0;
  30. }
  31. int s2n_client_ccs_recv(struct s2n_connection *conn)
  32. {
  33. POSIX_ENSURE_REF(conn);
  34. POSIX_ENSURE_REF(conn->secure);
  35. POSIX_GUARD(s2n_basic_ccs_recv(conn));
  36. /* Zero the sequence number */
  37. struct s2n_blob seq = { 0 };
  38. POSIX_GUARD(s2n_blob_init(&seq, conn->secure->client_sequence_number, sizeof(conn->secure->client_sequence_number)));
  39. POSIX_GUARD(s2n_blob_zero(&seq));
  40. /* Update the client to use the cipher-suite */
  41. conn->client = conn->secure;
  42. /* Flush any partial alert messages that were pending.
  43. * If we don't do this, an attacker can inject a 1-byte alert message into the handshake
  44. * and cause later, valid alerts to be processed incorrectly. */
  45. POSIX_GUARD(s2n_stuffer_wipe(&conn->alert_in));
  46. return 0;
  47. }
  48. int s2n_server_ccs_recv(struct s2n_connection *conn)
  49. {
  50. POSIX_ENSURE_REF(conn);
  51. POSIX_ENSURE_REF(conn->secure);
  52. POSIX_GUARD(s2n_basic_ccs_recv(conn));
  53. /* Zero the sequence number */
  54. struct s2n_blob seq = { 0 };
  55. POSIX_GUARD(s2n_blob_init(&seq, conn->secure->server_sequence_number, sizeof(conn->secure->server_sequence_number)));
  56. POSIX_GUARD(s2n_blob_zero(&seq));
  57. /* Compute the finished message */
  58. POSIX_GUARD(s2n_prf_server_finished(conn));
  59. /* Update the secure state to active, and point the client at the active state */
  60. conn->server = conn->secure;
  61. /* Flush any partial alert messages that were pending.
  62. * If we don't do this, an attacker can inject a 1-byte alert message into the handshake
  63. * and cause later, valid alerts to be processed incorrectly. */
  64. POSIX_GUARD(s2n_stuffer_wipe(&conn->alert_in));
  65. return 0;
  66. }
  67. int s2n_ccs_send(struct s2n_connection *conn)
  68. {
  69. POSIX_GUARD(s2n_stuffer_write_uint8(&conn->handshake.io, CHANGE_CIPHER_SPEC_TYPE));
  70. return 0;
  71. }