s2n_server_extensions.c 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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/s2n_server_extensions.h"
  16. #include "stuffer/s2n_stuffer.h"
  17. #include "tls/extensions/s2n_extension_list.h"
  18. #include "tls/extensions/s2n_server_supported_versions.h"
  19. #include "tls/s2n_connection.h"
  20. #include "utils/s2n_safety.h"
  21. /* An empty list will just contain the uint16_t list size */
  22. #define S2N_EMPTY_EXTENSION_LIST_SIZE sizeof(uint16_t)
  23. int s2n_server_extensions_send(struct s2n_connection *conn, struct s2n_stuffer *out)
  24. {
  25. uint32_t data_available_before_extensions = s2n_stuffer_data_available(out);
  26. if (s2n_is_hello_retry_message(conn)) {
  27. POSIX_GUARD(s2n_extension_list_send(S2N_EXTENSION_LIST_HELLO_RETRY_REQUEST, conn, out));
  28. } else if (conn->actual_protocol_version >= S2N_TLS13) {
  29. POSIX_GUARD(s2n_extension_list_send(S2N_EXTENSION_LIST_SERVER_HELLO_TLS13, conn, out));
  30. } else {
  31. POSIX_GUARD(s2n_extension_list_send(S2N_EXTENSION_LIST_SERVER_HELLO_DEFAULT, conn, out));
  32. }
  33. /* The ServerHello extension list size (uint16_t) is NOT written if the list is empty.
  34. * This is to support older clients written before extensions existed that might fail
  35. * on any unexpected bytes at the end of the ServerHello.
  36. *
  37. * This behavior is outlined in the TLS1.2 RFC: https://tools.ietf.org/html/rfc5246#appendix-A.4.1
  38. *
  39. * This behavior does not affect TLS1.3, which always requires at least the supported_version extension
  40. * so will never produce an empty list.
  41. */
  42. if (s2n_stuffer_data_available(out) - data_available_before_extensions == S2N_EMPTY_EXTENSION_LIST_SIZE) {
  43. POSIX_GUARD(s2n_stuffer_wipe_n(out, S2N_EMPTY_EXTENSION_LIST_SIZE));
  44. }
  45. return S2N_SUCCESS;
  46. }
  47. int s2n_server_extensions_recv(struct s2n_connection *conn, struct s2n_stuffer *in)
  48. {
  49. s2n_parsed_extensions_list parsed_extension_list = { 0 };
  50. POSIX_GUARD(s2n_extension_list_parse(in, &parsed_extension_list));
  51. /**
  52. * Process supported_versions first so that we know which extensions list to use.
  53. * - If the supported_versions extension exists, then it will set server_protocol_version.
  54. * - If the supported_versions extension does not exist, then the server_protocol_version will remain
  55. * unknown and we will use the default list of allowed extension types.
  56. **/
  57. POSIX_GUARD(s2n_extension_process(&s2n_server_supported_versions_extension, conn, &parsed_extension_list));
  58. if (s2n_is_hello_retry_message(conn)) {
  59. /**
  60. *= https://tools.ietf.org/rfc/rfc8446#4.1.4
  61. *# Otherwise, the client MUST process all extensions in the
  62. *# HelloRetryRequest
  63. */
  64. POSIX_GUARD(s2n_extension_list_process(S2N_EXTENSION_LIST_HELLO_RETRY_REQUEST, conn, &parsed_extension_list));
  65. } else if (conn->server_protocol_version >= S2N_TLS13) {
  66. POSIX_GUARD(s2n_extension_list_process(S2N_EXTENSION_LIST_SERVER_HELLO_TLS13, conn, &parsed_extension_list));
  67. } else {
  68. POSIX_GUARD(s2n_extension_list_process(S2N_EXTENSION_LIST_SERVER_HELLO_DEFAULT, conn, &parsed_extension_list));
  69. }
  70. return S2N_SUCCESS;
  71. }