s2n_client_cookie.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 "tls/extensions/s2n_cookie.h"
  16. #include "tls/s2n_tls.h"
  17. #include "utils/s2n_random.h"
  18. /*
  19. *= https://tools.ietf.org/rfc/rfc8446#section-4.2.2
  20. *# When sending the new ClientHello, the client MUST copy
  21. *# the contents of the extension received in the HelloRetryRequest into
  22. *# a "cookie" extension in the new ClientHello.
  23. *
  24. * If the server sent a cookie, send it back.
  25. */
  26. static bool s2n_client_cookie_should_send(struct s2n_connection *conn)
  27. {
  28. return conn && conn->cookie.size > 0;
  29. }
  30. /*
  31. *= https://tools.ietf.org/rfc/rfc8446#section-4.2.2
  32. *# struct {
  33. *# opaque cookie<1..2^16-1>;
  34. *# } Cookie;
  35. */
  36. int s2n_cookie_send(struct s2n_connection *conn, struct s2n_stuffer *out)
  37. {
  38. POSIX_ENSURE_REF(conn);
  39. POSIX_GUARD(s2n_stuffer_write_uint16(out, conn->cookie.size));
  40. POSIX_GUARD(s2n_stuffer_write(out, &conn->cookie));
  41. return S2N_SUCCESS;
  42. }
  43. /*
  44. * Our server does not send cookies in production, so
  45. * should never receive a cookie back from the client.
  46. *
  47. * However, we may enable cookies for testing.
  48. * In that case, verify the proper client behavior by
  49. * checking that the cookie sent matches the cookie received.
  50. * This is also why the "if_missing" behavior is an error.
  51. */
  52. static int s2n_client_cookie_recv(struct s2n_connection *conn, struct s2n_stuffer *extension)
  53. {
  54. POSIX_ENSURE_REF(conn);
  55. POSIX_ENSURE(s2n_in_unit_test(), S2N_ERR_UNSUPPORTED_EXTENSION);
  56. uint16_t size = 0;
  57. POSIX_GUARD(s2n_stuffer_read_uint16(extension, &size));
  58. POSIX_ENSURE(size == conn->cookie.size, S2N_ERR_BAD_MESSAGE);
  59. POSIX_ENSURE(size >= s2n_stuffer_data_available(extension), S2N_ERR_BAD_MESSAGE);
  60. uint8_t *cookie = s2n_stuffer_raw_read(extension, size);
  61. POSIX_ENSURE_REF(cookie);
  62. POSIX_ENSURE(s2n_constant_time_equals(cookie, conn->cookie.data, size), S2N_ERR_BAD_MESSAGE);
  63. return S2N_SUCCESS;
  64. }
  65. const s2n_extension_type s2n_client_cookie_extension = {
  66. .iana_value = TLS_EXTENSION_COOKIE,
  67. .minimum_version = S2N_TLS13,
  68. .is_response = true,
  69. .send = s2n_cookie_send,
  70. .recv = s2n_client_cookie_recv,
  71. .should_send = s2n_client_cookie_should_send,
  72. .if_missing = s2n_extension_error_if_missing,
  73. };