s2n_server_renegotiation_info.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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_renegotiation_info.h"
  16. #include "error/s2n_errno.h"
  17. #include "stuffer/s2n_stuffer.h"
  18. #include "tls/s2n_connection.h"
  19. #include "tls/s2n_tls.h"
  20. #include "tls/s2n_tls_parameters.h"
  21. #include "utils/s2n_safety.h"
  22. /**
  23. * s2n-tls servers do NOT support renegotiation.
  24. *
  25. * We implement this extension to handle clients that require secure renegotiation support:
  26. *= https://tools.ietf.org/rfc/rfc5746#4.3
  27. *# In order to enable clients to probe, even servers that do not support
  28. *# renegotiation MUST implement the minimal version of the extension
  29. *# described in this document for initial handshakes, thus signaling
  30. *# that they have been upgraded.
  31. */
  32. static bool s2n_renegotiation_info_should_send(struct s2n_connection *conn);
  33. static int s2n_renegotiation_info_send(struct s2n_connection *conn, struct s2n_stuffer *out);
  34. static int s2n_renegotiation_info_recv(struct s2n_connection *conn, struct s2n_stuffer *extension);
  35. static int s2n_renegotiation_info_if_missing(struct s2n_connection *conn);
  36. const s2n_extension_type s2n_server_renegotiation_info_extension = {
  37. .iana_value = TLS_EXTENSION_RENEGOTIATION_INFO,
  38. .send = s2n_renegotiation_info_send,
  39. .recv = s2n_renegotiation_info_recv,
  40. .should_send = s2n_renegotiation_info_should_send,
  41. .if_missing = s2n_renegotiation_info_if_missing,
  42. /**
  43. *= https://tools.ietf.org/rfc/rfc5746#3.6
  44. *# Note that sending a "renegotiation_info" extension in response to a
  45. *# ClientHello containing only the SCSV is an explicit exception to the
  46. *# prohibition in RFC 5246, Section 7.4.1.4, on the server sending
  47. *# unsolicited extensions and is only allowed because the client is
  48. *# signaling its willingness to receive the extension via the
  49. *# TLS_EMPTY_RENEGOTIATION_INFO_SCSV SCSV.
  50. *
  51. * This extension is technically a response extension, but doesn't
  52. * follow any of the usual response extension rules.
  53. * s2n-tls will therefore not treat it as a response extension.
  54. */
  55. .is_response = false,
  56. };
  57. /**
  58. *= https://tools.ietf.org/rfc/rfc5746#3.6
  59. *# o If the secure_renegotiation flag is set to TRUE, the server MUST
  60. *# include an empty "renegotiation_info" extension in the ServerHello
  61. *# message.
  62. */
  63. static bool s2n_renegotiation_info_should_send(struct s2n_connection *conn)
  64. {
  65. return conn && conn->secure_renegotiation && s2n_connection_get_protocol_version(conn) < S2N_TLS13;
  66. }
  67. /**
  68. *= https://tools.ietf.org/rfc/rfc5746#3.6
  69. *# o If the secure_renegotiation flag is set to TRUE, the server MUST
  70. *# include an empty "renegotiation_info" extension in the ServerHello
  71. *# message.
  72. */
  73. static int s2n_renegotiation_info_send_initial(struct s2n_connection *conn, struct s2n_stuffer *out)
  74. {
  75. POSIX_GUARD(s2n_stuffer_write_uint8(out, 0));
  76. return S2N_SUCCESS;
  77. }
  78. static int s2n_renegotiation_info_send_renegotiation(struct s2n_connection *conn, struct s2n_stuffer *out)
  79. {
  80. POSIX_ENSURE_REF(conn);
  81. /* s2n-tls servers do not support renegotiation.
  82. * We add the renegotiation version of this logic only for testing.
  83. */
  84. POSIX_ENSURE(s2n_in_unit_test(), S2N_ERR_NOT_IN_UNIT_TEST);
  85. /**
  86. *= https://tools.ietf.org/rfc/rfc5746#3.7
  87. *# This text applies if the connection's "secure_renegotiation" flag is
  88. *# set to TRUE (if it is set to FALSE, see Section 4.4).
  89. */
  90. POSIX_ENSURE(conn->secure_renegotiation, S2N_ERR_NO_RENEGOTIATION);
  91. /**
  92. *= https://tools.ietf.org/rfc/rfc5746#3.7
  93. *# o The server MUST include a "renegotiation_info" extension
  94. *# containing the saved client_verify_data and server_verify_data in
  95. *# the ServerHello.
  96. */
  97. const uint8_t verify_data_len = conn->handshake.finished_len;
  98. POSIX_ENSURE_GT(verify_data_len, 0);
  99. POSIX_GUARD(s2n_stuffer_write_uint8(out, verify_data_len * 2));
  100. POSIX_GUARD(s2n_stuffer_write_bytes(out, conn->handshake.client_finished, verify_data_len));
  101. POSIX_GUARD(s2n_stuffer_write_bytes(out, conn->handshake.server_finished, verify_data_len));
  102. return S2N_SUCCESS;
  103. }
  104. static int s2n_renegotiation_info_send(struct s2n_connection *conn, struct s2n_stuffer *out)
  105. {
  106. if (s2n_handshake_is_renegotiation(conn)) {
  107. POSIX_GUARD(s2n_renegotiation_info_send_renegotiation(conn, out));
  108. } else {
  109. POSIX_GUARD(s2n_renegotiation_info_send_initial(conn, out));
  110. }
  111. return S2N_SUCCESS;
  112. }
  113. /**
  114. *= https://tools.ietf.org/rfc/rfc5746#3.4
  115. *# o When a ServerHello is received, the client MUST check if it
  116. *# includes the "renegotiation_info" extension:
  117. */
  118. static int s2n_renegotiation_info_recv_initial(struct s2n_connection *conn, struct s2n_stuffer *extension)
  119. {
  120. POSIX_ENSURE_REF(conn);
  121. /**
  122. *= https://tools.ietf.org/rfc/rfc5746#3.4
  123. *# * The client MUST then verify that the length of the
  124. *# "renegotiated_connection" field is zero, and if it is not, MUST
  125. *# abort the handshake (by sending a fatal handshake_failure alert).
  126. */
  127. uint8_t renegotiated_connection_len = 0;
  128. POSIX_GUARD(s2n_stuffer_read_uint8(extension, &renegotiated_connection_len));
  129. POSIX_ENSURE(s2n_stuffer_data_available(extension) == 0, S2N_ERR_NON_EMPTY_RENEGOTIATION_INFO);
  130. POSIX_ENSURE(renegotiated_connection_len == 0, S2N_ERR_NON_EMPTY_RENEGOTIATION_INFO);
  131. /**
  132. *= https://tools.ietf.org/rfc/rfc5746#3.4
  133. *# * If the extension is present, set the secure_renegotiation flag to TRUE.
  134. */
  135. conn->secure_renegotiation = 1;
  136. return S2N_SUCCESS;
  137. }
  138. static int s2n_renegotiation_info_recv_renegotiation(struct s2n_connection *conn, struct s2n_stuffer *extension)
  139. {
  140. POSIX_ENSURE_REF(conn);
  141. uint8_t verify_data_len = conn->handshake.finished_len;
  142. POSIX_ENSURE_GT(verify_data_len, 0);
  143. /**
  144. *= https://tools.ietf.org/rfc/rfc5746#3.5
  145. *# This text applies if the connection's "secure_renegotiation" flag is
  146. *# set to TRUE (if it is set to FALSE, see Section 4.2).
  147. */
  148. POSIX_ENSURE(conn->secure_renegotiation, S2N_ERR_NO_RENEGOTIATION);
  149. /**
  150. *= https://tools.ietf.org/rfc/rfc5746#3.5
  151. *# o The client MUST then verify that the first half of the
  152. *# "renegotiated_connection" field is equal to the saved
  153. *# client_verify_data value, and the second half is equal to the
  154. *# saved server_verify_data value. If they are not, the client MUST
  155. *# abort the handshake.
  156. */
  157. uint8_t renegotiated_connection_len = 0;
  158. POSIX_GUARD(s2n_stuffer_read_uint8(extension, &renegotiated_connection_len));
  159. POSIX_ENSURE(verify_data_len * 2 == renegotiated_connection_len, S2N_ERR_BAD_MESSAGE);
  160. uint8_t *first_half = s2n_stuffer_raw_read(extension, verify_data_len);
  161. POSIX_ENSURE_REF(first_half);
  162. POSIX_ENSURE(s2n_constant_time_equals(first_half, conn->handshake.client_finished, verify_data_len),
  163. S2N_ERR_BAD_MESSAGE);
  164. uint8_t *second_half = s2n_stuffer_raw_read(extension, verify_data_len);
  165. POSIX_ENSURE_REF(second_half);
  166. POSIX_ENSURE(s2n_constant_time_equals(second_half, conn->handshake.server_finished, verify_data_len),
  167. S2N_ERR_BAD_MESSAGE);
  168. return S2N_SUCCESS;
  169. }
  170. /**
  171. * Note that this extension must also work for SSLv3:
  172. *= https://tools.ietf.org/rfc/rfc5746#4.5
  173. *# Clients that support SSLv3 and offer secure renegotiation (either via SCSV or
  174. *# "renegotiation_info") MUST accept the "renegotiation_info" extension
  175. *# from the server, even if the server version is {0x03, 0x00}, and
  176. *# behave as described in this specification.
  177. */
  178. static int s2n_renegotiation_info_recv(struct s2n_connection *conn, struct s2n_stuffer *extension)
  179. {
  180. if (s2n_handshake_is_renegotiation(conn)) {
  181. POSIX_GUARD(s2n_renegotiation_info_recv_renegotiation(conn, extension));
  182. } else {
  183. POSIX_GUARD(s2n_renegotiation_info_recv_initial(conn, extension));
  184. }
  185. return S2N_SUCCESS;
  186. }
  187. static int s2n_renegotiation_info_if_missing(struct s2n_connection *conn)
  188. {
  189. POSIX_ENSURE_REF(conn);
  190. if (s2n_handshake_is_renegotiation(conn)) {
  191. /**
  192. *= https://tools.ietf.org/rfc/rfc5746#3.5
  193. *# o When a ServerHello is received, the client MUST verify that the
  194. *# "renegotiation_info" extension is present; if it is not, the
  195. *# client MUST abort the handshake.
  196. */
  197. POSIX_BAIL(S2N_ERR_NO_RENEGOTIATION);
  198. } else {
  199. /**
  200. *= https://tools.ietf.org/rfc/rfc5746#3.4
  201. *# * If the extension is not present, the server does not support
  202. *# secure renegotiation; set secure_renegotiation flag to FALSE.
  203. *# In this case, some clients may want to terminate the handshake
  204. *# instead of continuing; see Section 4.1 for discussion.
  205. */
  206. conn->secure_renegotiation = false;
  207. /**
  208. *= https://tools.ietf.org/rfc/rfc5746#4.1
  209. *= type=exception
  210. *= reason=Avoid interoperability problems
  211. *# If clients wish to ensure that such attacks are impossible, they need
  212. *# to terminate the connection immediately upon failure to receive the
  213. *# extension without completing the handshake. Such clients MUST
  214. *# generate a fatal "handshake_failure" alert prior to terminating the
  215. *# connection. However, it is expected that many TLS servers that do
  216. *# not support renegotiation (and thus are not vulnerable) will not
  217. *# support this extension either, so in general, clients that implement
  218. *# this behavior will encounter interoperability problems.
  219. *
  220. * TODO: https://github.com/aws/s2n-tls/issues/3528
  221. */
  222. return S2N_SUCCESS;
  223. }
  224. }
  225. /* Old-style extension functions -- remove after extensions refactor is complete */
  226. int s2n_recv_server_renegotiation_info_ext(struct s2n_connection *conn, struct s2n_stuffer *extension)
  227. {
  228. return s2n_extension_recv(&s2n_server_renegotiation_info_extension, conn, extension);
  229. }