s2n_server_cookie.c 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. *# Cookies serve two primary purposes:
  21. *#
  22. *# - Allowing the server to force the client to demonstrate
  23. *# reachability at their apparent network address (thus providing a
  24. *# measure of DoS protection). This is primarily useful for
  25. *# non-connection-oriented transports (see [RFC6347] for an example
  26. *# of this).
  27. *#
  28. *# - Allowing the server to offload state to the client, thus allowing
  29. *# it to send a HelloRetryRequest without storing any state. The
  30. *# server can do this by storing the hash of the ClientHello in the
  31. *# HelloRetryRequest cookie (protected with some suitable integrity
  32. *# protection algorithm).
  33. *#
  34. *# When sending a HelloRetryRequest, the server MAY provide a "cookie"
  35. *# extension to the client (this is an exception to the usual rule that
  36. *# the only extensions that may be sent are those that appear in the
  37. *# ClientHello).
  38. *
  39. * So our server does not send cookies in production,
  40. * because it doesn't support DTLS and isn't stateless.
  41. *
  42. * However, we will sometimes send cookies for testing.
  43. */
  44. static bool s2n_server_cookie_should_send(struct s2n_connection *conn)
  45. {
  46. return conn && conn->cookie.size > 0 && s2n_in_unit_test();
  47. }
  48. /*
  49. *= https://tools.ietf.org/rfc/rfc8446#section-4.2.2
  50. *# When sending the new ClientHello, the client MUST copy
  51. *# the contents of the extension received in the HelloRetryRequest into
  52. *# a "cookie" extension in the new ClientHello.
  53. *
  54. * Store the server's cookie for later use in the new ClientHello.
  55. */
  56. static int s2n_server_cookie_recv(struct s2n_connection *conn, struct s2n_stuffer *extension)
  57. {
  58. POSIX_ENSURE_REF(conn);
  59. /* This extension should only appear on the hello retry request extension list,
  60. * but verify the retry anyway.
  61. */
  62. POSIX_ENSURE(s2n_is_hello_retry_handshake(conn), S2N_ERR_UNSUPPORTED_EXTENSION);
  63. uint16_t size = 0;
  64. POSIX_GUARD(s2n_stuffer_read_uint16(extension, &size));
  65. POSIX_ENSURE(s2n_stuffer_data_available(extension) >= size, S2N_ERR_BAD_MESSAGE);
  66. POSIX_GUARD(s2n_realloc(&conn->cookie, size));
  67. POSIX_GUARD(s2n_stuffer_read(extension, &conn->cookie));
  68. return S2N_SUCCESS;
  69. }
  70. const s2n_extension_type s2n_server_cookie_extension = {
  71. .iana_value = TLS_EXTENSION_COOKIE,
  72. .minimum_version = S2N_TLS13,
  73. .is_response = false,
  74. .send = s2n_cookie_send,
  75. .recv = s2n_server_cookie_recv,
  76. .should_send = s2n_server_cookie_should_send,
  77. .if_missing = s2n_extension_noop_if_missing,
  78. };