s2n_client_hello_request.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 "api/s2n.h"
  16. #include "tls/s2n_alerts.h"
  17. #include "tls/s2n_connection.h"
  18. #include "utils/s2n_safety.h"
  19. S2N_RESULT s2n_client_hello_request_validate(struct s2n_connection *conn)
  20. {
  21. RESULT_ENSURE_REF(conn);
  22. if (IS_NEGOTIATED(conn)) {
  23. RESULT_ENSURE(conn->actual_protocol_version < S2N_TLS13, S2N_ERR_BAD_MESSAGE);
  24. }
  25. /*
  26. *= https://tools.ietf.org/rfc/rfc5246#section-7.4.1.1
  27. *# The HelloRequest message MAY be sent by the server at any time.
  28. */
  29. RESULT_ENSURE(conn->mode == S2N_CLIENT, S2N_ERR_BAD_MESSAGE);
  30. return S2N_RESULT_OK;
  31. }
  32. S2N_RESULT s2n_client_hello_request_recv(struct s2n_connection *conn)
  33. {
  34. RESULT_ENSURE_REF(conn);
  35. RESULT_ENSURE_REF(conn->config);
  36. RESULT_GUARD(s2n_client_hello_request_validate(conn));
  37. /* Maintain the old s2n-tls behavior by default.
  38. * Traditionally, s2n-tls has just ignored all hello requests.
  39. */
  40. if (!conn->config->renegotiate_request_cb) {
  41. return S2N_RESULT_OK;
  42. }
  43. /*
  44. *= https://tools.ietf.org/rfc/rfc5746#section-4.2
  45. *# This text applies if the connection's "secure_renegotiation" flag is
  46. *# set to FALSE.
  47. *#
  48. *# It is possible that un-upgraded servers will request that the client
  49. *# renegotiate. It is RECOMMENDED that clients refuse this
  50. *# renegotiation request. Clients that do so MUST respond to such
  51. *# requests with a "no_renegotiation" alert (RFC 5246 requires this
  52. *# alert to be at the "warning" level). It is possible that the
  53. *# apparently un-upgraded server is in fact an attacker who is then
  54. *# allowing the client to renegotiate with a different, legitimate,
  55. *# upgraded server.
  56. */
  57. if (!conn->secure_renegotiation) {
  58. RESULT_GUARD(s2n_queue_reader_no_renegotiation_alert(conn));
  59. return S2N_RESULT_OK;
  60. }
  61. s2n_renegotiate_response response = S2N_RENEGOTIATE_REJECT;
  62. int result = conn->config->renegotiate_request_cb(conn, conn->config->renegotiate_request_ctx, &response);
  63. RESULT_ENSURE(result == S2N_SUCCESS, S2N_ERR_CANCELLED);
  64. /*
  65. *= https://tools.ietf.org/rfc/rfc5246#section-7.4.1.1
  66. *# This message MAY be ignored by
  67. *# the client if it does not wish to renegotiate a session, or the
  68. *# client may, if it wishes, respond with a no_renegotiation alert.
  69. */
  70. if (response == S2N_RENEGOTIATE_REJECT) {
  71. RESULT_GUARD(s2n_queue_reader_no_renegotiation_alert(conn));
  72. return S2N_RESULT_OK;
  73. }
  74. return S2N_RESULT_OK;
  75. }