renegotiate.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #pragma once
  16. #include <s2n.h>
  17. /**
  18. * @file renegotiate.h
  19. *
  20. * "Renegotiation" is a TLS feature offered in TLS1.2 and earlier.
  21. * During renegotiation, a new handshake is performed on an already established
  22. * connection. The new handshake is encrypted using the keys from the original handshake.
  23. * The new handshake may not match the first handshake; for example, the server may choose
  24. * a different cipher suite or require client authentication for the new handshake.
  25. *
  26. * s2n-tls clients support secure (compliant with RFC5746) renegotiation for compatibility reasons,
  27. * but s2n-tls does NOT recommend its use. While s2n-tls addresses all currently known security concerns,
  28. * renegotiation has appeared in many CVEs and was completely removed from TLS1.3.
  29. */
  30. /**
  31. * Used to indicate that an attempt to renegotiate encountered
  32. * application data which the application should process before
  33. * continuing the handshake.
  34. */
  35. extern const s2n_blocked_status S2N_BLOCKED_ON_APPLICATION_DATA;
  36. /**
  37. * Indicates how a renegotiation request should be handled.
  38. */
  39. typedef enum {
  40. /* The client will take no action */
  41. S2N_RENEGOTIATE_IGNORE = 0,
  42. /* The client will send a warning alert to the server */
  43. S2N_RENEGOTIATE_REJECT,
  44. /* The client will begin renegotiation in the future */
  45. S2N_RENEGOTIATE_ACCEPT,
  46. } s2n_renegotiate_response;
  47. /**
  48. * Callback function to handle requests for renegotiation.
  49. *
  50. * s2n-tls calls this method when a client receives a request from the server
  51. * to renegotiate the connection. If the server makes multiple requests,
  52. * s2n-tls will call this method multiple times.
  53. *
  54. * Applications should use the `response` value to indicate how the request
  55. * should be handled. If `response` is set to `S2N_RENEGOTIATE_IGNORE`
  56. * or `S2N_RENEGOTIATE_REJECT`, no further application involvement is required.
  57. *
  58. * If `response` is set to `S2N_RENEGOTIATE_ACCEPT`, then the application should
  59. * handle renegotiation. The application should stop calling s2n_send and s2n_recv,
  60. * wipe the connection with s2n_renegotiate_wipe, and then call s2n_renegotiate
  61. * until the handshake is complete.
  62. *
  63. * @param conn A pointer to the connection object.
  64. * @param context Context for the callback function.
  65. * @param response How the request should be handled.
  66. * @returns S2N_SUCCESS on success, S2N_FAILURE on error.
  67. */
  68. typedef int (*s2n_renegotiate_request_cb)(struct s2n_connection *conn, void *context, s2n_renegotiate_response *response);
  69. /**
  70. * Sets a method to be called when the client receives a request to renegotiate.
  71. *
  72. * @param config A pointer to the config object.
  73. * @param callback The function to be called when a renegotiation request is received.
  74. * @param context Context to be passed to the callback function.
  75. * @returns S2N_SUCCESS on success, S2N_FAILURE on error.
  76. */
  77. S2N_API int s2n_config_set_renegotiate_request_cb(struct s2n_config *config, s2n_renegotiate_request_cb callback, void *context);
  78. /**
  79. * Reset the connection so that it can be renegotiated.
  80. *
  81. * Similar to `s2n_connection_wipe`, this method resets a connection so that it can be used again.
  82. * However, unlike `s2n_connection_wipe`, it retains enough state from the previous connection
  83. * that the connection can continue to send and receive data encrypted with the old keys.
  84. *
  85. * The application MUST handle any incomplete IO before calling this method. The last call to `s2n_send` must
  86. * have succeeded, and `s2n_peek` must return zero. If there is any data in the send or receive buffers,
  87. * this method will fail.
  88. *
  89. * The application MUST repeat any connection-specific setup after calling this method. This method
  90. * cannot distinguish between internal connection state and configuration state set by the application,
  91. * so it wipes all state not directly related to handling encrypted records. For example,
  92. * if the application originally called `s2n_connection_set_blinding` on the connection,
  93. * then the application will need to call `s2n_connection_set_blinding` again after `s2n_renegotiate_wipe`.
  94. *
  95. * The connection-specific setup methods the application does not need to call again are:
  96. * - Methods to set the file descriptors
  97. * (`s2n_connection_set_fd`, `s2n_connection_set_read_fd`, `s2n_connection_set_write_fd`)
  98. * - Methods to set the send callback
  99. * (`s2n_connection_set_send_cb`, `s2n_connection_set_send_ctx`)
  100. * - Methods to set the recv callback
  101. * (`s2n_connection_set_recv_cb`, `s2n_connection_set_recv_ctx`)
  102. *
  103. * @note This method MUST be called before s2n_renegotiate.
  104. * @note Calling this method on a server connection will fail. s2n-tls servers do not support renegotiation.
  105. *
  106. * @param conn A pointer to the connection object.
  107. * @returns S2N_SUCCESS on success, S2N_FAILURE on error.
  108. */
  109. S2N_API int s2n_renegotiate_wipe(struct s2n_connection *conn);
  110. /**
  111. * Perform a new handshake on an already established connection.
  112. *
  113. * This method should be called like `s2n_negotiate`, with the same handling of return values,
  114. * error types, and blocked statuses.
  115. *
  116. * However, unlike the initial handshake performed by `s2n_negotiate`, the renegotiation
  117. * handshake can encounter valid application data. In that case, this method will fail
  118. * with an error of type S2N_ERR_T_BLOCKED, set the `blocked` field to `S2N_BLOCKED_ON_APPLICATION_DATA`,
  119. * copy the data to `app_data_buf`, and set `app_data_size` to the size of the data.
  120. * The application should handle the data in `app_data_buf` before calling s2n_renegotiate again.
  121. *
  122. * @note s2n_renegotiate_wipe MUST be called before this method.
  123. * @note Calling this method on a server connection will fail. s2n-tls servers do not support renegotiation.
  124. *
  125. * @param conn A pointer to the connection object.
  126. * @param app_data_buf A pointer to a buffer that s2n will copy application data read into.
  127. * @param app_data_buf_size The size of `app_data_buf`.
  128. * @param app_data_size The number of application data bytes read.
  129. * @param blocked A pointer which will be set to the blocked status.
  130. * @returns S2N_SUCCESS if the handshake completed. S2N_FAILURE if the handshake encountered an error or is blocked.
  131. */
  132. S2N_API int s2n_renegotiate(struct s2n_connection *conn, uint8_t *app_data_buf, ssize_t app_data_buf_size,
  133. ssize_t *app_data_size, s2n_blocked_status *blocked);