Debian Codegraph @

crt/s2n/api/unstable/ktls.h

raw symbols SCIP
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 /* kTLS is not supported on Windows */
19 #ifndef _WIN32
20
21 #include <s2n.h>
22
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26
27 /**
28 * @file ktls.h
29 *
30 * The following APIs enable applications to use kernel TLS (kTLS), meaning that
31 * encrypting and decrypting TLS records is handled by the kernel rather than by
32 * the s2n-tls library.
33 *
34 * The kTLS APIs are currently considered unstable. kTLS is a relatively new
35 * feature with limited and volatile support from different kernels and hardware.
36 *
37 * Currently, s2n-tls supports ktls for limited scenarios:
38 * - You must be using Linux. We have not tested with other kernels.
39 * - Your kernel must support kTLS. For Linux, versions >4.13 should support kTLS.
40 * - The TLS kernel module must be enabled. While some environments enable the
41 * module by default, most will require you to run `sudo modprobe tls`.
42 * - You must negotiate AES128-GCM or AES256-GCM. Other ciphers are supported by
43 * the kernel, but not implemented in s2n-tls yet.
44 * - You must not use the s2n_renegotiate_request_cb from unstable/negotiate.h.
45 * The TLS kernel module currently doesn't support renegotiation.
46 * - By default, you must negotiate TLS1.2. See s2n_config_ktls_enable_unsafe_tls13
47 * for the requirements to also support TLS1.3.
48 * - You must not use s2n_connection_set_recv_buffering
49 */
50
51 /**
52 * Enables sending using kTLS on a given connection.
53 *
54 * See above for the limitations on when kTLS can be enabled. Additionally,
55 * s2n_connection_ktls_enable_send must be called after the handshake completes
56 * but before the handshake is freed with s2n_connection_free_handshake.
57 * It may be called after some application data is sent and received without kTLS,
58 * but there must be no pending application data that requires flushing. If these
59 * requirements are not met, enabling kTLS will fail with an error.
60 *
61 * After kTLS is enabled for sending, s2n_send, s2n_sendv, and s2n_sendv_with_offset
62 * will use kTLS. kTLS should result in memory and CPU savings. s2n_sendfile will
63 * also become available.
64 *
65 * For applications using kTLS to avoid copying or allocating memory, s2n_sendv
66 * should be preferred over s2n_sendv_with_offset. For s2n_sendv_with_offset,
67 * s2n-tls may need to copy the provided iovec array to apply the offset, and may
68 * need to allocate memory to copy large (>16) iovec arrays.
69 *
70 * If kTLS is enabled for sending, s2n_connection_get_wire_bytes_out will always
71 * return 0 instead of an accurate count.
72 *
73 * @warning Due to the uncertainty around kTLS support, the signature of this
74 * method is likely to change before kTLS is marked as stable.
75 *
76 * @param conn A pointer to the connection.
77 * @returns S2N_SUCCESS if kTLS is successfully enabled. If kTlS is not successfully
78 * enabled, returns S2N_FAILURE but the connection may proceed without kTLS.
79 */
80 S2N_API int s2n_connection_ktls_enable_send(struct s2n_connection *conn);
81
82 /**
83 * Enables receiving using kTLS on a given connection.
84 *
85 * See above for the limitations on when kTLS can be enabled. Additionally,
86 * s2n_connection_ktls_enable_recv must be called after the handshake completes
87 * but before the handshake is freed with s2n_connection_free_handshake.
88 * It may be called after some application data is sent and received without kTLS,
89 * but there must be no buffered application data that requires draining. If these
90 * requirements are not met, enabling kTLS will fail with an error.
91 *
92 * After kTLS is enabled for receiving, s2n_recv will use kTLS. This may result
93 * in memory and CPU savings, but currently will still buffer and copy application data.
94 * We will further optimize s2n_recv for kTLS in the future.
95 *
96 * If kTLS is enabled for receiving, s2n_connection_get_wire_bytes_in will always
97 * return 0 instead of an accurate count.
98 *
99 * @warning Due to the uncertainty around kTLS support, the signature of this
100 * method is likely to change before kTLS is marked as stable.
101 *
102 * @param conn A pointer to the connection.
103 * @returns S2N_SUCCESS if kTLS is successfully enabled. If kTlS is not successfully
104 * enabled, returns S2N_FAILURE but the connection may proceed without kTLS.
105 */
106 S2N_API int s2n_connection_ktls_enable_recv(struct s2n_connection *conn);
107
108 /**
109 * Allows kTLS to be enabled if a connection negotiates TLS1.3.
110 *
111 * @warning Enabling TLS1.3 with this method is considered "unsafe" because only linux
112 * kernel versions >=6.14 support TLS 1.3 key updates, and s2n-tls cannot detect
113 * if your kernel has the required key update patch. Receiving or sending a key update
114 * message in TLS1.3 without the kernel patch will cause a connection failure.
115 *
116 * @note Calling this API will force a limit of 388GB per s2n_send/sendfile call.
117 * See https://github.com/aws/s2n-tls/issues/5524. The S2N_ERR_INVALID_ARGUMENT error
118 * will be thrown if you call these APIs with > 388GB.
119 *
120 * This method must be called before enabling kTLS on a connection using
121 * s2n_connection_ktls_enable_send or s2n_connection_ktls_enable_recv.
122 *
123 * @param config A pointer to the config.
124 * @returns S2N_SUCCESS if successfully enabled, S2N_FAILURE otherwise.
125 */
126 S2N_API int s2n_config_ktls_enable_unsafe_tls13(struct s2n_config *config);
127
128 /**
129 * Reports the number of times sending and receiving keys have been updated.
130 *
131 * This only applies to TLS1.3. Earlier versions do not support key updates.
132 *
133 * @warning s2n-tls only tracks up to UINT8_MAX (255) key updates. If this method
134 * reports 255 updates, then more than 255 updates may have occurred.
135 *
136 * @param conn A pointer to the connection.
137 * @param send_key_updates Number of times the sending key was updated.
138 * @param recv_key_updates Number of times the receiving key was updated.
139 * @returns S2N_SUCCESS if successful, S2N_FAILURE otherwise.
140 */
141 S2N_API int s2n_connection_get_key_update_counts(struct s2n_connection *conn,
142 uint8_t *send_key_updates, uint8_t *recv_key_updates);
143
144 /**
145 * Sends the contents of a file as application data.
146 *
147 * s2n_sendfile should be more efficient than s2n_send because the copy between
148 * the file and the write socket happens inside the kernel.
149 *
150 * This method is only supported if kTLS is enabled for sending.
151 *
152 * @note For a TLS1.3 connection, the `count` argument will be used to enforce
153 * safe sending limits regardless of the actual size of the file. Applications
154 * should not set `count` excessively high.
155 *
156 * @param conn A pointer to the connection.
157 * @param fd The file descriptor to read from. It must be opened for reading and
158 * support mmap-like operations (i.e., it cannot be a socket).
159 * @param offset The offset in the file to begin reading at.
160 * @param count The maximum number of bytes to read from the file.
161 * @param bytes_written Will be set to the number of bytes written if successful.
162 * @param blocked Will be set to the blocked status if an `S2N_ERR_T_BLOCKED` error is returned.
163 * @returns S2N_SUCCESS if any bytes are successfully written, S2N_FAILURE otherwise.
164 */
165 S2N_API int s2n_sendfile(struct s2n_connection *conn, int fd, off_t offset, size_t count,
166 size_t *bytes_written, s2n_blocked_status *blocked);
167
168 #ifdef __cplusplus
169 }
170 #endif
171
172 #endif