s2n_server_alpn.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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_alpn.h"
  16. #include "stuffer/s2n_stuffer.h"
  17. #include "tls/s2n_connection.h"
  18. #include "tls/s2n_tls.h"
  19. #include "utils/s2n_safety.h"
  20. bool s2n_server_alpn_should_send(struct s2n_connection *conn);
  21. static int s2n_alpn_send(struct s2n_connection *conn, struct s2n_stuffer *out);
  22. static int s2n_alpn_recv(struct s2n_connection *conn, struct s2n_stuffer *extension);
  23. const s2n_extension_type s2n_server_alpn_extension = {
  24. .iana_value = TLS_EXTENSION_ALPN,
  25. .is_response = true,
  26. .send = s2n_alpn_send,
  27. .recv = s2n_alpn_recv,
  28. .should_send = s2n_server_alpn_should_send,
  29. .if_missing = s2n_extension_noop_if_missing,
  30. };
  31. bool s2n_server_alpn_should_send(struct s2n_connection *conn)
  32. {
  33. return conn && strlen(conn->application_protocol) > 0;
  34. }
  35. static int s2n_alpn_send(struct s2n_connection *conn, struct s2n_stuffer *out)
  36. {
  37. POSIX_ENSURE_REF(conn);
  38. const uint8_t application_protocol_len = strlen(conn->application_protocol);
  39. /* Size of protocol name list */
  40. POSIX_GUARD(s2n_stuffer_write_uint16(out, application_protocol_len + sizeof(uint8_t)));
  41. /* Single entry in protocol name list */
  42. POSIX_GUARD(s2n_stuffer_write_uint8(out, application_protocol_len));
  43. POSIX_GUARD(s2n_stuffer_write_bytes(out, (uint8_t *) conn->application_protocol, application_protocol_len));
  44. return S2N_SUCCESS;
  45. }
  46. static int s2n_alpn_recv(struct s2n_connection *conn, struct s2n_stuffer *extension)
  47. {
  48. POSIX_ENSURE_REF(conn);
  49. uint16_t size_of_all;
  50. POSIX_GUARD(s2n_stuffer_read_uint16(extension, &size_of_all));
  51. if (size_of_all > s2n_stuffer_data_available(extension) || size_of_all < 3) {
  52. /* ignore invalid extension size */
  53. return S2N_SUCCESS;
  54. }
  55. uint8_t protocol_len;
  56. POSIX_GUARD(s2n_stuffer_read_uint8(extension, &protocol_len));
  57. POSIX_ENSURE_LT(protocol_len, s2n_array_len(conn->application_protocol));
  58. uint8_t *protocol = s2n_stuffer_raw_read(extension, protocol_len);
  59. POSIX_ENSURE_REF(protocol);
  60. /* copy the first protocol name */
  61. POSIX_CHECKED_MEMCPY(conn->application_protocol, protocol, protocol_len);
  62. conn->application_protocol[protocol_len] = '\0';
  63. return S2N_SUCCESS;
  64. }