s2n_client_alpn.c 3.2 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_client_alpn.h"
  16. #include <stdint.h>
  17. #include <sys/param.h>
  18. #include "tls/extensions/s2n_extension_type.h"
  19. #include "tls/s2n_protocol_preferences.h"
  20. #include "tls/s2n_tls.h"
  21. #include "tls/s2n_tls_parameters.h"
  22. #include "utils/s2n_safety.h"
  23. bool s2n_client_alpn_should_send(struct s2n_connection *conn);
  24. static int s2n_client_alpn_send(struct s2n_connection *conn, struct s2n_stuffer *out);
  25. static int s2n_client_alpn_recv(struct s2n_connection *conn, struct s2n_stuffer *extension);
  26. const s2n_extension_type s2n_client_alpn_extension = {
  27. .iana_value = TLS_EXTENSION_ALPN,
  28. .is_response = false,
  29. .send = s2n_client_alpn_send,
  30. .recv = s2n_client_alpn_recv,
  31. .should_send = s2n_client_alpn_should_send,
  32. .if_missing = s2n_extension_noop_if_missing,
  33. };
  34. bool s2n_client_alpn_should_send(struct s2n_connection *conn)
  35. {
  36. struct s2n_blob *client_app_protocols;
  37. return s2n_connection_get_protocol_preferences(conn, &client_app_protocols) == S2N_SUCCESS
  38. && client_app_protocols->size != 0 && client_app_protocols->data != NULL;
  39. }
  40. static int s2n_client_alpn_send(struct s2n_connection *conn, struct s2n_stuffer *out)
  41. {
  42. struct s2n_blob *client_app_protocols;
  43. POSIX_GUARD(s2n_connection_get_protocol_preferences(conn, &client_app_protocols));
  44. POSIX_ENSURE_REF(client_app_protocols);
  45. POSIX_GUARD(s2n_stuffer_write_uint16(out, client_app_protocols->size));
  46. POSIX_GUARD(s2n_stuffer_write(out, client_app_protocols));
  47. return S2N_SUCCESS;
  48. }
  49. static int s2n_client_alpn_recv(struct s2n_connection *conn, struct s2n_stuffer *extension)
  50. {
  51. struct s2n_blob *supported_protocols = NULL;
  52. POSIX_GUARD(s2n_connection_get_protocol_preferences(conn, &supported_protocols));
  53. POSIX_ENSURE_REF(supported_protocols);
  54. if (supported_protocols->size == 0) {
  55. /* No protocols configured, nothing to do */
  56. return S2N_SUCCESS;
  57. }
  58. uint16_t wire_size = 0;
  59. POSIX_GUARD(s2n_stuffer_read_uint16(extension, &wire_size));
  60. if (wire_size > s2n_stuffer_data_available(extension) || wire_size < 3) {
  61. /* Malformed length, ignore the extension */
  62. return S2N_SUCCESS;
  63. }
  64. struct s2n_blob client_protocols = { 0 };
  65. POSIX_GUARD(s2n_blob_init(&client_protocols, s2n_stuffer_raw_read(extension, wire_size), wire_size));
  66. struct s2n_stuffer server_protocols = { 0 };
  67. POSIX_GUARD(s2n_stuffer_init(&server_protocols, supported_protocols));
  68. POSIX_GUARD(s2n_stuffer_skip_write(&server_protocols, supported_protocols->size));
  69. POSIX_GUARD_RESULT(s2n_select_server_preference_protocol(conn, &server_protocols, &client_protocols));
  70. return S2N_SUCCESS;
  71. }