s2n_server_psk.c 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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_server_psk.h"
  16. #include <stdint.h>
  17. #include <sys/param.h>
  18. #include "tls/s2n_tls.h"
  19. #include "utils/s2n_bitmap.h"
  20. #include "utils/s2n_safety.h"
  21. static bool s2n_server_psk_should_send(struct s2n_connection *conn);
  22. static int s2n_server_psk_send(struct s2n_connection *conn, struct s2n_stuffer *out);
  23. static int s2n_server_psk_recv(struct s2n_connection *conn, struct s2n_stuffer *extension);
  24. const s2n_extension_type s2n_server_psk_extension = {
  25. .iana_value = TLS_EXTENSION_PRE_SHARED_KEY,
  26. .minimum_version = S2N_TLS13,
  27. .is_response = true,
  28. .send = s2n_server_psk_send,
  29. .recv = s2n_server_psk_recv,
  30. .should_send = s2n_server_psk_should_send,
  31. .if_missing = s2n_extension_noop_if_missing,
  32. };
  33. static bool s2n_server_psk_should_send(struct s2n_connection *conn)
  34. {
  35. /* Only send a server pre_shared_key extension if a chosen PSK is set on the connection */
  36. return conn && conn->psk_params.chosen_psk;
  37. }
  38. static int s2n_server_psk_send(struct s2n_connection *conn, struct s2n_stuffer *out)
  39. {
  40. POSIX_ENSURE_REF(conn);
  41. /* Send the index of the chosen PSK that is stored on the connection. */
  42. POSIX_GUARD(s2n_stuffer_write_uint16(out, conn->psk_params.chosen_psk_wire_index));
  43. return S2N_SUCCESS;
  44. }
  45. static int s2n_server_psk_recv(struct s2n_connection *conn, struct s2n_stuffer *extension)
  46. {
  47. POSIX_ENSURE_REF(conn);
  48. /* Currently in s2n, only (EC)DHE key exchange mode is supported.
  49. * Any other mode selected by the server is invalid because it was not offered by the client.
  50. * A key_share extension MUST have been received in order to use a pre-shared key in (EC)DHE key exchange mode.
  51. */
  52. s2n_extension_type_id key_share_ext_id = s2n_unsupported_extension;
  53. POSIX_GUARD(s2n_extension_supported_iana_value_to_id(TLS_EXTENSION_KEY_SHARE, &key_share_ext_id));
  54. POSIX_ENSURE(S2N_CBIT_TEST(conn->extension_responses_received, key_share_ext_id), S2N_ERR_MISSING_EXTENSION);
  55. /* From RFC section: https://tools.ietf.org/html/rfc8446#section-4.2.8.1
  56. * Any future values that are allocated must ensure that the transmitted protocol messages
  57. * unambiguously identify which mode was selected by the server; at present, this is
  58. * indicated by the presence of the "key_share" in the ServerHello.
  59. */
  60. conn->psk_params.psk_ke_mode = S2N_PSK_DHE_KE;
  61. uint16_t chosen_psk_wire_index = 0;
  62. POSIX_GUARD(s2n_stuffer_read_uint16(extension, &chosen_psk_wire_index));
  63. /* From RFC section: https://tools.ietf.org/html/rfc8446#section-4.2.11
  64. * Clients MUST verify that the server's selected_identity is within the
  65. * range supplied by the client.
  66. */
  67. POSIX_ENSURE(chosen_psk_wire_index < conn->psk_params.psk_list.len, S2N_ERR_INVALID_ARGUMENT);
  68. conn->psk_params.chosen_psk_wire_index = chosen_psk_wire_index;
  69. POSIX_GUARD_RESULT(s2n_array_get(&conn->psk_params.psk_list, conn->psk_params.chosen_psk_wire_index,
  70. (void **) &conn->psk_params.chosen_psk));
  71. return S2N_SUCCESS;
  72. }