s2n_next_protocol.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 "error/s2n_errno.h"
  16. #include "stuffer/s2n_stuffer.h"
  17. #include "tls/s2n_tls.h"
  18. #include "utils/s2n_safety.h"
  19. S2N_RESULT s2n_calculate_padding(uint8_t protocol_len, uint8_t *padding_len)
  20. {
  21. RESULT_ENSURE_REF(padding_len);
  22. /*
  23. *= https://datatracker.ietf.org/doc/id/draft-agl-tls-nextprotoneg-03#section-3
  24. *# The length of "padding" SHOULD be 32 - ((len(selected_protocol) + 2) % 32).
  25. */
  26. *padding_len = 32 - (((uint16_t) protocol_len + 2) % 32);
  27. return S2N_RESULT_OK;
  28. }
  29. S2N_RESULT s2n_write_npn_protocol(struct s2n_connection *conn, struct s2n_stuffer *out)
  30. {
  31. RESULT_ENSURE_REF(conn);
  32. uint8_t protocol_len = strlen(conn->application_protocol);
  33. RESULT_GUARD_POSIX(s2n_stuffer_write_uint8(out, protocol_len));
  34. RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(out, (uint8_t *) conn->application_protocol, protocol_len));
  35. uint8_t padding_len = 0;
  36. RESULT_GUARD(s2n_calculate_padding(protocol_len, &padding_len));
  37. RESULT_GUARD_POSIX(s2n_stuffer_write_uint8(out, padding_len));
  38. uint8_t *data_ptr = s2n_stuffer_raw_write(out, padding_len);
  39. RESULT_ENSURE_REF(data_ptr);
  40. RESULT_CHECKED_MEMSET(data_ptr, 0, padding_len);
  41. return S2N_RESULT_OK;
  42. }
  43. S2N_RESULT s2n_read_npn_protocol(struct s2n_connection *conn, struct s2n_stuffer *in)
  44. {
  45. RESULT_ENSURE_REF(conn);
  46. uint8_t protocol_len = 0;
  47. RESULT_GUARD_POSIX(s2n_stuffer_read_uint8(in, &protocol_len));
  48. uint8_t *protocol = s2n_stuffer_raw_read(in, protocol_len);
  49. RESULT_ENSURE_REF(protocol);
  50. RESULT_CHECKED_MEMCPY(conn->application_protocol, protocol, protocol_len);
  51. conn->application_protocol[protocol_len] = '\0';
  52. uint8_t expected_padding_len = 0;
  53. RESULT_GUARD(s2n_calculate_padding(protocol_len, &expected_padding_len));
  54. uint8_t padding_len = 0;
  55. RESULT_GUARD_POSIX(s2n_stuffer_read_uint8(in, &padding_len));
  56. RESULT_ENSURE_EQ(padding_len, expected_padding_len);
  57. uint8_t *data_ptr = s2n_stuffer_raw_read(in, padding_len);
  58. RESULT_ENSURE_REF(data_ptr);
  59. uint8_t empty_array[UINT8_MAX] = { 0 };
  60. RESULT_ENSURE_EQ(s2n_constant_time_equals(data_ptr, empty_array, padding_len), 1);
  61. RESULT_ENSURE_EQ(s2n_stuffer_data_available(in), 0);
  62. return S2N_RESULT_OK;
  63. }
  64. int s2n_next_protocol_send(struct s2n_connection *conn)
  65. {
  66. POSIX_ENSURE_REF(conn);
  67. POSIX_ENSURE(conn->actual_protocol_version < S2N_TLS13, S2N_ERR_BAD_MESSAGE);
  68. struct s2n_stuffer *out = &conn->handshake.io;
  69. POSIX_GUARD_RESULT(s2n_write_npn_protocol(conn, out));
  70. POSIX_GUARD_RESULT(s2n_crypto_parameters_switch(conn));
  71. return S2N_SUCCESS;
  72. }
  73. int s2n_next_protocol_recv(struct s2n_connection *conn)
  74. {
  75. POSIX_ENSURE_REF(conn);
  76. POSIX_ENSURE(conn->actual_protocol_version < S2N_TLS13, S2N_ERR_BAD_MESSAGE);
  77. struct s2n_stuffer *in = &conn->handshake.io;
  78. POSIX_GUARD_RESULT(s2n_read_npn_protocol(conn, in));
  79. return S2N_SUCCESS;
  80. }