|
| 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 | +/** |
| 21 | + * @file ktls.h |
| 22 | + * |
| 23 | + * The following APIs enable applications to use kernel TLS (kTLS), meaning that |
| 24 | + * encrypting and decrypting TLS records is handled by the kernel rather than by |
| 25 | + * the s2n-tls library. |
| 26 | + * |
| 27 | + * The kTLS APIs are currently considered unstable. kTLS is a relatively new |
| 28 | + * feature with limited and volatile support from different kernels and hardware. |
| 29 | + * |
| 30 | + * Currently, s2n-tls supports ktls for only very limited scenarios: |
| 31 | + * - You must be using Linux. We have not tested with other kernels. |
| 32 | + * - Your kernel must support kTLS. For Linux, versions >4.13 should support kTLS. |
| 33 | + * - The TLS kernel module must be enabled. While some environments enable the |
| 34 | + * module by default, most will require you to run `sudo modprobe tls`. |
| 35 | + * - You must negotiate TLS1.2. TLS1.3 support is blocked on kernel support for |
| 36 | + * TLS KeyUpdate messages. |
| 37 | + * - You must negotiate AES128-GCM, which is the most preferred cipher suite |
| 38 | + * in the "default" security policy. Other ciphers are supported by the kernel, |
| 39 | + * but not implemented in s2n-tls yet. |
| 40 | + */ |
| 41 | + |
| 42 | +/** |
| 43 | + * Enables sending using kTLS on a given connection. |
| 44 | + * |
| 45 | + * See above for the limitations on when kTLS can be enabled. Additionally, |
| 46 | + * s2n_connection_ktls_enable_send must be called after the handshake completes. |
| 47 | + * It may be called after some application data is sent and received without kTLS, |
| 48 | + * but there must be no pending application data that requires flushing. If these |
| 49 | + * requirements are not met, enabling kTLS will fail with an error. |
| 50 | + * |
| 51 | + * After kTLS is enabled for sending, s2n_send, s2n_sendv, and s2n_sendv_with_offset |
| 52 | + * will use kTLS. kTLS should result in memory and CPU savings. s2n_sendfile will |
| 53 | + * also become available. |
| 54 | + * |
| 55 | + * For applications using kTLS to avoid copying or allocating memory, s2n_sendv |
| 56 | + * should be preferred over s2n_sendv_with_offset. For s2n_sendv_with_offset, |
| 57 | + * s2n-tls may need to copy the provided iovec array to apply the offset, and may |
| 58 | + * need to allocate memory to copy large (>16) iovec arrays. |
| 59 | + * |
| 60 | + * If kTLS is enabled for sending, s2n_connection_get_wire_bytes_out will always |
| 61 | + * return 0 instead of an accurate count. |
| 62 | + * |
| 63 | + * @warning Due to the uncertainty around kTLS support, the signature of this |
| 64 | + * method is likely to change before kTLS is marked as stable. |
| 65 | + * |
| 66 | + * @param conn A pointer to the connection. |
| 67 | + * @returns S2N_SUCCESS if kTLS is successfully enabled. If kTlS is not successfully |
| 68 | + * enabled, returns S2N_FAILURE but the connection may proceed without kTLS. |
| 69 | + */ |
| 70 | +S2N_API int s2n_connection_ktls_enable_send(struct s2n_connection *conn); |
| 71 | + |
| 72 | +/** |
| 73 | + * Enables receiving using kTLS on a given connection. |
| 74 | + * |
| 75 | + * See above for the limitations on when kTLS can be enabled. Additionally, |
| 76 | + * s2n_connection_ktls_enable_recv must be called after the handshake completes. |
| 77 | + * It may be called after some application data is sent and received without kTLS, |
| 78 | + * but there must be no buffered application data that requires draining. If these |
| 79 | + * requirements are not met, enabling kTLS will fail with an error. |
| 80 | + * |
| 81 | + * After kTLS is enabled for receiving, s2n_recv will use kTLS. This may result |
| 82 | + * in memory and CPU savings, but currently will still buffer and copy application data. |
| 83 | + * We will further optimize s2n_recv for kTLS in the future. |
| 84 | + * |
| 85 | + * If kTLS is enabled for receiving, s2n_connection_get_wire_bytes_in will always |
| 86 | + * return 0 instead of an accurate count. |
| 87 | + * |
| 88 | + * @warning Due to the uncertainty around kTLS support, the signature of this |
| 89 | + * method is likely to change before kTLS is marked as stable. |
| 90 | + * |
| 91 | + * @param conn A pointer to the connection. |
| 92 | + * @returns S2N_SUCCESS if kTLS is successfully enabled. If kTlS is not successfully |
| 93 | + * enabled, returns S2N_FAILURE but the connection may proceed without kTLS. |
| 94 | + */ |
| 95 | +S2N_API int s2n_connection_ktls_enable_recv(struct s2n_connection *conn); |
| 96 | + |
| 97 | +/** |
| 98 | + * Sends the contents of a file as application data. |
| 99 | + * |
| 100 | + * s2n_sendfile should be more efficient than s2n_send because the copy between |
| 101 | + * the file and the write socket happens inside the kernel. |
| 102 | + * |
| 103 | + * This method is only supported if kTLS is enabled for sending. |
| 104 | + * |
| 105 | + * @param conn A pointer to the connection. |
| 106 | + * @param fd The file descriptor to read from. It must be opened for reading and |
| 107 | + * support mmap-like operations (i.e., it cannot be a socket). |
| 108 | + * @param offset The offset in the file to begin reading at. |
| 109 | + * @param count The maximum number of bytes to read from the file. |
| 110 | + * @param bytes_written Will be set to the number of bytes written if successful. |
| 111 | + * @param blocked Will be set to the blocked status if an `S2N_ERR_T_BLOCKED` error is returned. |
| 112 | + * @returns S2N_SUCCESS if any bytes are successfully written, S2N_FAILURE otherwise. |
| 113 | + */ |
| 114 | +S2N_API int s2n_sendfile(struct s2n_connection *conn, int fd, off_t offset, size_t count, |
| 115 | + size_t *bytes_written, s2n_blocked_status *blocked); |
0 commit comments