| 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 | |
| 16 | #pragma once |
| 17 | |
| 18 | #include <s2n.h> |
| 19 | |
| 20 | #ifdef __cplusplus |
| 21 | extern "C" { |
| 22 | #endif |
| 23 | |
| 24 | /** |
| 25 | * @file renegotiate.h |
| 26 | * |
| 27 | * "Renegotiation" is a TLS feature offered in TLS1.2 and earlier. |
| 28 | * During renegotiation, a new handshake is performed on an already established |
| 29 | * connection. The new handshake is encrypted using the keys from the original handshake. |
| 30 | * The new handshake may not match the first handshake; for example, the server may choose |
| 31 | * a different cipher suite or require client authentication for the new handshake. |
| 32 | * |
| 33 | * s2n-tls clients support secure (compliant with RFC5746) renegotiation for compatibility reasons, |
| 34 | * but s2n-tls does NOT recommend its use. While s2n-tls addresses all currently known security concerns, |
| 35 | * renegotiation has appeared in many CVEs and was completely removed from TLS1.3. |
| 36 | * |
| 37 | * A basic renegotiation integration with s2n-tls would look like: |
| 38 | * 1. The application calls s2n_recv as part of normal IO. |
| 39 | * 2. s2n_recv receives a request for renegotiation (a HelloRequest message) |
| 40 | * instead of application data. |
| 41 | * 3. s2n_recv calls the configured s2n_renegotiate_request_cb. |
| 42 | * 4. The application's implementation of the s2n_renegotiate_request_cb should: |
| 43 | * 1. Set the `response` parameter to S2N_RENEGOTIATE_ACCEPT |
| 44 | * 2. Set some application state to indicate that renegotiation is required. |
| 45 | * s2n_connection_set_ctx can be used to associate application state with |
| 46 | * a specific connection. |
| 47 | * 3. Return success. |
| 48 | * 5. s2n_recv returns as part of normal IO. |
| 49 | * 6. The application should check the application state set in 4.2 to determine |
| 50 | * whether or not renegotiation is required. |
| 51 | * 7. The application should complete any in-progress IO. Failing to do this will |
| 52 | * cause s2n_renegotiate_wipe to fail. |
| 53 | * 1. For sending, the application must retry any blocked calls to s2n_send |
| 54 | * until they return success. |
| 55 | * 2. For receiving, the application must call s2n_recv to handle any buffered |
| 56 | * decrypted application data. s2n_peek indicates how much data is buffered. |
| 57 | * 8. The application should call s2n_renegotiate_wipe. |
| 58 | * 9. The application should reconfigure the connection, if necessary. |
| 59 | * 10. The application should call s2n_renegotiate until it indicates success, |
| 60 | * while handling any application data encountered. |
| 61 | */ |
| 62 | |
| 63 | /** |
| 64 | * Used to indicate that an attempt to renegotiate encountered |
| 65 | * application data which the application should process before |
| 66 | * continuing the handshake. |
| 67 | */ |
| 68 | extern const s2n_blocked_status S2N_BLOCKED_ON_APPLICATION_DATA; |
| 69 | |
| 70 | /** |
| 71 | * Indicates how a renegotiation request should be handled. |
| 72 | */ |
| 73 | typedef enum { |
| 74 | /* The client will take no action */ |
| 75 | S2N_RENEGOTIATE_IGNORE = 0, |
| 76 | /* The client will send a warning alert to the server */ |
| 77 | S2N_RENEGOTIATE_REJECT, |
| 78 | /* The client will begin renegotiation in the future */ |
| 79 | S2N_RENEGOTIATE_ACCEPT, |
| 80 | } s2n_renegotiate_response; |
| 81 | |
| 82 | /** |
| 83 | * Callback function to handle requests for renegotiation. |
| 84 | * |
| 85 | * s2n-tls calls this method when a client receives a request from the server |
| 86 | * to renegotiate the connection. If the server makes multiple requests, |
| 87 | * s2n-tls will call this method multiple times. |
| 88 | * |
| 89 | * Applications should use the `response` value to indicate how the request |
| 90 | * should be handled. If `response` is set to `S2N_RENEGOTIATE_IGNORE` |
| 91 | * or `S2N_RENEGOTIATE_REJECT`, no further application involvement is required. |
| 92 | * |
| 93 | * If `response` is set to `S2N_RENEGOTIATE_ACCEPT`, then the application should |
| 94 | * handle renegotiation. The application should stop calling s2n_send and s2n_recv, |
| 95 | * wipe the connection with s2n_renegotiate_wipe, and then call s2n_renegotiate |
| 96 | * until the handshake is complete. |
| 97 | * |
| 98 | * @param conn A pointer to the connection object. |
| 99 | * @param context Context for the callback function. |
| 100 | * @param response How the request should be handled. |
| 101 | * @returns S2N_SUCCESS on success, S2N_FAILURE on error. |
| 102 | */ |
| 103 | typedef int (*s2n_renegotiate_request_cb)(struct s2n_connection *conn, void *context, s2n_renegotiate_response *response); |
| 104 | |
| 105 | /** |
| 106 | * Sets a method to be called when the client receives a request to renegotiate. |
| 107 | * |
| 108 | * @param config A pointer to the config object. |
| 109 | * @param callback The function to be called when a renegotiation request is received. |
| 110 | * @param context Context to be passed to the callback function. |
| 111 | * @returns S2N_SUCCESS on success, S2N_FAILURE on error. |
| 112 | */ |
| 113 | S2N_API int s2n_config_set_renegotiate_request_cb(struct s2n_config *config, s2n_renegotiate_request_cb callback, void *context); |
| 114 | |
| 115 | /** |
| 116 | * Reset the connection so that it can be renegotiated. |
| 117 | * |
| 118 | * Similar to `s2n_connection_wipe`, this method resets a connection so that it can be used again. |
| 119 | * However, unlike `s2n_connection_wipe`, it retains enough state from the previous connection |
| 120 | * that the connection can continue to send and receive data encrypted with the old keys. |
| 121 | * |
| 122 | * The application MUST handle any incomplete IO before calling this method. The last call to `s2n_send` must |
| 123 | * have succeeded, and `s2n_peek` must return zero. If there is any data in the send or receive buffers, |
| 124 | * this method will fail. That means that this method cannot be called from inside s2n_renegotiate_request_cb. |
| 125 | * |
| 126 | * The application MUST repeat any connection-specific setup after calling this method. This method |
| 127 | * cannot distinguish between internal connection state and configuration state set by the application, |
| 128 | * so it wipes all state not directly related to handling encrypted records. For example, |
| 129 | * if the application originally called `s2n_connection_set_blinding` on the connection, |
| 130 | * then the application will need to call `s2n_connection_set_blinding` again after `s2n_renegotiate_wipe`. |
| 131 | * |
| 132 | * The connection-specific setup methods the application does not need to call again are: |
| 133 | * - Methods to set the file descriptors |
| 134 | * (`s2n_connection_set_fd`, `s2n_connection_set_read_fd`, `s2n_connection_set_write_fd`) |
| 135 | * - Methods to set the send callback |
| 136 | * (`s2n_connection_set_send_cb`, `s2n_connection_set_send_ctx`) |
| 137 | * - Methods to set the recv callback |
| 138 | * (`s2n_connection_set_recv_cb`, `s2n_connection_set_recv_ctx`) |
| 139 | * |
| 140 | * @note This method MUST be called before s2n_renegotiate. |
| 141 | * @note Calling this method on a server connection will fail. s2n-tls servers do not support renegotiation. |
| 142 | * @note This method will fail if the connection has indicated that it will be serialized with |
| 143 | * `s2n_config_set_serialization_version()`. |
| 144 | * |
| 145 | * @param conn A pointer to the connection object. |
| 146 | * @returns S2N_SUCCESS on success, S2N_FAILURE on error. |
| 147 | */ |
| 148 | S2N_API int s2n_renegotiate_wipe(struct s2n_connection *conn); |
| 149 | |
| 150 | /** |
| 151 | * Perform a new handshake on an already established connection. |
| 152 | * |
| 153 | * This method should be called like `s2n_negotiate`, with the same handling of return values, |
| 154 | * error types, and blocked statuses. |
| 155 | * |
| 156 | * However, unlike the initial handshake performed by `s2n_negotiate`, the renegotiation |
| 157 | * handshake can encounter valid application data. In that case, this method will fail |
| 158 | * with an error of type S2N_ERR_T_BLOCKED, set the `blocked` field to `S2N_BLOCKED_ON_APPLICATION_DATA`, |
| 159 | * copy the data to `app_data_buf`, and set `app_data_size` to the size of the data. |
| 160 | * The application should handle the data in `app_data_buf` before calling s2n_renegotiate again. |
| 161 | * |
| 162 | * This method cannot be called from inside s2n_renegotiate_request_cb. The receive |
| 163 | * call that triggered s2n_renegotiate_request_cb must complete before either |
| 164 | * s2n_renegotiate_wipe or s2n_renegotiate can be called. |
| 165 | * |
| 166 | * @note s2n_renegotiate_wipe MUST be called before this method. |
| 167 | * @note Calling this method on a server connection will fail. s2n-tls servers do not support renegotiation. |
| 168 | * |
| 169 | * @param conn A pointer to the connection object. |
| 170 | * @param app_data_buf A pointer to a buffer that s2n will copy application data read into. |
| 171 | * @param app_data_buf_size The size of `app_data_buf`. |
| 172 | * @param app_data_size The number of application data bytes read. |
| 173 | * @param blocked A pointer which will be set to the blocked status. |
| 174 | * @returns S2N_SUCCESS if the handshake completed. S2N_FAILURE if the handshake encountered an error or is blocked. |
| 175 | */ |
| 176 | S2N_API int s2n_renegotiate(struct s2n_connection *conn, uint8_t *app_data_buf, ssize_t app_data_buf_size, |
| 177 | ssize_t *app_data_size, s2n_blocked_status *blocked); |
| 178 | |
| 179 | #ifdef __cplusplus |
| 180 | } |
| 181 | #endif |