| 1 | # Sending and Receiving |
| 2 | ## Basic IO setup |
| 3 | |
| 4 | By default, s2n-tls sends and receives data using a provided file descriptor |
| 5 | (usually a socket) and the read/write system calls. The file descriptor can be set with |
| 6 | `s2n_connection_set_fd()`, or separate read and write file descriptors can be |
| 7 | set with `s2n_connection_set_read_fd()` and `s2n_connection_set_write_fd()`. |
| 8 | The provided file descriptor should be active and connected. |
| 9 | |
| 10 | In general the application is free to configure the file descriptor as preferred, |
| 11 | including socket options. s2n-tls itself sets a few socket options: |
| 12 | * If available, TCP_QUICKACK is used during the TLS handshake |
| 13 | * If available and enabled via `s2n_connection_use_corked_io()`, TCP_CORK is |
| 14 | used during the TLS handshake. TCP_NOPUSH or TCP_NODELAY may be used if TCP_CORK |
| 15 | is not available. |
| 16 | |
| 17 | **Important Note:** |
| 18 | If the read end of the pipe is closed unexpectedly, writing to the pipe will raise |
| 19 | a SIGPIPE signal. **s2n-tls does NOT handle SIGPIPE.** A SIGPIPE signal will cause |
| 20 | the process to terminate unless it is handled or ignored by the application. |
| 21 | See the [signal man page](https://linux.die.net/man/2/signal) for instructions on |
| 22 | how to handle C signals, or simply ignore the SIGPIPE signal by calling |
| 23 | `signal(SIGPIPE, SIG_IGN)` before calling any s2n-tls IO methods. |
| 24 | |
| 25 | ## Blocking or Non-Blocking? |
| 26 | |
| 27 | s2n-tls supports both blocking and non-blocking I/O. |
| 28 | * In blocking mode, each s2n-tls I/O function will not return until it has completed |
| 29 | the requested IO operation. |
| 30 | * In non-blocking mode, s2n-tls I/O functions will immediately return, even if the socket couldn't |
| 31 | send or receive all the requested data. In this case, the I/O function will return `S2N_FAILURE`, |
| 32 | and `s2n_error_get_type()` will return `S2N_ERR_T_BLOCKED`. The I/O operation will have to be |
| 33 | called again in order to send or receive the remaining requested data. |
| 34 | |
| 35 | Some s2n-tls I/O functions take a `blocked` argument. If an I/O function returns an |
| 36 | `S2N_ERR_T_BLOCKED` error, the `blocked` argument will be set to a `s2n_blocked_status` value, |
| 37 | indicating what s2n-tls is currently blocked on. Note that unless an I/O function returns |
| 38 | `S2N_FAILURE` with an `S2N_ERR_T_BLOCKED` error, the `blocked` argument is meaningless, and should |
| 39 | not be used in any application logic. |
| 40 | |
| 41 | Servers in particular usually prefer non-blocking mode. In blocking mode, a single connection |
| 42 | blocks the thread while waiting for more IO. In non-blocking mode, multiple connections |
| 43 | can make progress by returning control while waiting for more IO using methods like |
| 44 | [`poll`](https://linux.die.net/man/2/poll) or [`select`](https://linux.die.net/man/2/select). |
| 45 | |
| 46 | To use s2n-tls in non-blocking mode, set the underlying file descriptors as non-blocking. |
| 47 | For example: |
| 48 | ```c |
| 49 | int flags = fcntl(fd, F_GETFL, 0); |
| 50 | if (flags < 0) return S2N_FAILURE; |
| 51 | if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) return S2N_FAILURE; |
| 52 | ``` |
| 53 | |
| 54 | Note: If an application requires non-blocking IO, it likely also requires self-service blinding. See [Blinding](./ch03-error-handling.md#Blinding). |
| 55 | |
| 56 | ## Errors and Alerts |
| 57 | |
| 58 | If the peer sends an alert, the next call to a read IO method will report **S2N_FAILURE** and |
| 59 | `s2n_error_get_type()` will return **S2N_ERR_T_ALERT**. The specific alert received |
| 60 | is available by calling `s2n_connection_get_alert()`. |
| 61 | |
| 62 | In TLS1.3, all alerts are fatal. s2n-tls also treats all alerts as fatal in earlier |
| 63 | versions of TLS by default. `s2n_config_set_alert_behavior()` can be called to |
| 64 | force s2n-tls to treat pre-TLS1.3 warning alerts as not fatal, but that behavior |
| 65 | is not recommended unless required for compatibility. In the past, attacks against |
| 66 | TLS have involved manipulating the alert level to disguise fatal alerts as warnings. |
| 67 | |
| 68 | If s2n-tls encounters a fatal error, the next call to a write IO method will send |
| 69 | a close_notify alert to the peer. Except for a few exceptions, s2n-tls does not |
| 70 | send specific alerts in order to avoid leaking information that could be used for |
| 71 | a sidechannel attack. To ensure that the alert is sent, `s2n_shutdown()` should |
| 72 | be called after an error. |
| 73 | |
| 74 | ## Performing the TLS Handshake |
| 75 | |
| 76 | Before application data can be sent or received, an application must perform a handshake |
| 77 | to establish a TLS connection with the peer. |
| 78 | |
| 79 | To perform the handshake, call `s2n_negotiate()` until it either returns **S2N_SUCCESS** |
| 80 | or returns **S2N_FAILURE** without a **S2N_ERR_T_BLOCKED** error. |
| 81 | |
| 82 | For an example of how to perform a basic handshake, see [examples/s2n_negotiate.c](https://github.com/aws/s2n-tls/blob/main/docs/examples/s2n_negotiate.c) |
| 83 | |
| 84 | ## Application Data |
| 85 | |
| 86 | After the TLS handshake, an application can send and receive encrypted data. |
| 87 | |
| 88 | Although most s2n-tls APIs are not thread-safe, `s2n_send()` and `s2n_recv()` |
| 89 | may be called simultaneously from two different threads. This means that an |
| 90 | application may have one thread calling `s2n_send()` and one thread calling `s2n_recv()`, |
| 91 | but NOT multiple threads calling `s2n_recv()` or multiple threads calling `s2n_send()`. |
| 92 | |
| 93 | Even if an application only intends to send data or only intends to receive data, |
| 94 | it should implement both send and receive in order to handle alerts and post-handshake |
| 95 | TLS control messages like session tickets. |
| 96 | |
| 97 | ### Sending Application Data |
| 98 | |
| 99 | `s2n_send()` and its variants encrypt and send application data to the peer. |
| 100 | The sending methods return the number of bytes written and may indicate a partial |
| 101 | write. Partial writes are possible not just for non-blocking I/O, but also for |
| 102 | connections aborted while active. |
| 103 | |
| 104 | A single call to `s2n_send()` may involve multiple system calls to write the |
| 105 | provided application data. s2n-tls breaks the application data into fixed-sized |
| 106 | records before encryption, then calls write for each record. |
| 107 | [See the record size documentation for how record size may impact performance](./ch08-record-sizes.md). |
| 108 | |
| 109 | In non-blocking mode, `s2n_send()` will send data from the provided buffer and return the number of |
| 110 | bytes sent, as long as the socket was able to send at least 1 byte. If no bytes could be sent on the |
| 111 | socket, `s2n_send()` will return `S2N_FAILURE`, and `s2n_error_get_type()` will return |
| 112 | `S2N_ERR_T_BLOCKED`. To ensure that all the provided data gets sent, applications should continue |
| 113 | calling `s2n_send()` until the return values across all calls have added up to the length of the |
| 114 | data, or until `s2n_send()` returns an `S2N_ERR_T_BLOCKED` error. After an `S2N_ERR_T_BLOCKED` |
| 115 | error is returned, applications should call `s2n_send()` again only after the socket is |
| 116 | able to send more data. This can be determined by using methods like |
| 117 | [`poll`](https://linux.die.net/man/2/poll) or [`select`](https://linux.die.net/man/2/select). |
| 118 | |
| 119 | Unlike OpenSSL, repeated calls to `s2n_send()` should not duplicate the original |
| 120 | parameters, but should update the inputs per the indication of size written. s2n-tls |
| 121 | will attempt to sanity check that the inputs are properly updated between calls. |
| 122 | Because of those sanity checks, a zero-length send call cannot be used as a flushing mechanism. |
| 123 | |
| 124 | `s2n_sendv_with_offset()` behaves like `s2n_send()`, but supports vectorized buffers. |
| 125 | The offset input should be updated between calls to reflect the data already written. |
| 126 | |
| 127 | `s2n_sendv()` also supports vectorized buffers, but assumes an offset of 0. |
| 128 | Because of this assumption, a caller would have to make sure that the input vectors |
| 129 | are updated to account for a partial write. Therefore `s2n_sendv_with_offset()` |
| 130 | is preferred. |
| 131 | |
| 132 | For examples of how to send `data` of length `data_size` with `s2n_send()` |
| 133 | or `s2n_sendv_with_offset()`, see [examples/s2n_send.c](https://github.com/aws/s2n-tls/blob/main/docs/examples/s2n_send.c) |
| 134 | |
| 135 | ### Receiving Application Data |
| 136 | |
| 137 | `s2n_recv()` reads and decrypts application data from the peer, copying it into |
| 138 | the application-provided output buffer. It returns the number of bytes read, and |
| 139 | may indicate a partial read even if blocking IO is used. |
| 140 | It returns "0" to indicate that the peer has shutdown the connection. |
| 141 | |
| 142 | By default, `s2n_recv()` will return after reading a single TLS record. `s2n_recv()` can be called |
| 143 | repeatedly to read multiple records. To allow `s2n_recv()` to read multiple records with a single |
| 144 | call, use `s2n_config_set_recv_multi_record()`. |
| 145 | |
| 146 | In non-blocking mode, `s2n_recv()` will read data into the provided buffer and return the number of |
| 147 | bytes read, as long as at least 1 byte was read from the socket. If no bytes could be read from the |
| 148 | socket, `s2n_recv()` will return `S2N_FAILURE`, and `s2n_error_get_type()` will return |
| 149 | `S2N_ERR_T_BLOCKED`. To ensure that all data on the socket is properly received, applications |
| 150 | should continue calling `s2n_recv()` until it returns an `S2N_ERR_T_BLOCKED` error. After an |
| 151 | `S2N_ERR_T_BLOCKED` error is returned, applications should call `s2n_recv()` again only after the |
| 152 | socket has received more data. This can be determined by using methods like |
| 153 | [`poll`](https://linux.die.net/man/2/poll) or [`select`](https://linux.die.net/man/2/select). |
| 154 | |
| 155 | Unlike OpenSSL, repeated calls to `s2n_recv()` should not duplicate the original parameters, |
| 156 | but should update the inputs per the indication of size read. |
| 157 | |
| 158 | For an example of how to read all the data sent by the peer into one buffer, |
| 159 | see `s2n_example_recv()` in [examples/s2n_recv.c](https://github.com/aws/s2n-tls/blob/main/docs/examples/s2n_recv.c) |
| 160 | |
| 161 | For an example of how to echo any data sent by the peer, |
| 162 | see `s2n_example_recv_echo()` in [examples/s2n_recv.c](https://github.com/aws/s2n-tls/blob/main/docs/examples/s2n_recv.c) |
| 163 | |
| 164 | `s2n_peek()` can be used to check if more application data may be returned |
| 165 | from `s2n_recv()` without performing another read from the file descriptor. |
| 166 | This is useful when using `select()` on the underlying s2n-tls file descriptor, because |
| 167 | a call to `s2n_recv()` may read more data into s2n-tls's internal buffer than |
| 168 | was requested or can fit into the application-provided output buffer. This extra |
| 169 | application data will be returned by the next call to `s2n_recv()`, but `select()` |
| 170 | will be unable to tell the application that there is more data available and that |
| 171 | `s2n_recv()` should be called again. An application can solve this problem by |
| 172 | calling `s2n_peek()` to determine if `s2n_recv()` needs to be called again. |
| 173 | |
| 174 | ## Closing the Connection |
| 175 | |
| 176 | `s2n_shutdown()` attempts a graceful closure at the TLS layer. It does not close the |
| 177 | underlying transport. The call may block on either reading or writing. |
| 178 | |
| 179 | `s2n_shutdown()` should be called after an error in order to ensure that s2n-tls |
| 180 | sends an alert to notify the peer of the failure. |
| 181 | |
| 182 | `s2n_shutdown()` will discard any application data received from the peer. This |
| 183 | can lead to data truncation, so `s2n_shutdown_send()` may be preferred for TLS1.3 |
| 184 | connections where the peer continues sending after the application initiates |
| 185 | shutdown. See [Closing the connection for writes](#closing-the-connection-for-writes) |
| 186 | below. |
| 187 | |
| 188 | Because `s2n_shutdown()` attempts a graceful shutdown, it will not return success |
| 189 | unless a close_notify alert is successfully both sent and received. As a result, |
| 190 | `s2n_shutdown()` may fail when interacting with a non-conformant TLS implementation |
| 191 | or if called on a connection in a bad state. |
| 192 | |
| 193 | `s2n_shutdown()` may also read and decrypt multiple application data records while waiting |
| 194 | for the close_notify alert. This could result in calls to `s2n_shutdown()` taking a long |
| 195 | time to complete. If this is a problem, `s2n_shutdown_send()` may be preferable. |
| 196 | See [Closing the connection for writes](#closing-the-connection-for-writes) below. |
| 197 | |
| 198 | Once `s2n_shutdown()` is complete: |
| 199 | * The s2n_connection handle cannot be used for reading or writing. |
| 200 | * The underlying transport can be closed, most likely via `shutdown()` or `close()`. |
| 201 | * The s2n_connection handle can be freed via `s2n_connection_free()` or reused |
| 202 | via `s2n_connection_wipe()` |
| 203 | |
| 204 | ### Closing the connection for writes |
| 205 | |
| 206 | TLS1.3 supports closing the write side of a TLS connection while leaving the read |
| 207 | side unaffected. This indicates "end-of-data" to the peer without preventing |
| 208 | future reads. This feature is usually referred to as "half-close". |
| 209 | |
| 210 | s2n-tls offers the `s2n_shutdown_send()` method to close the write side of |
| 211 | a connection. Unlike `s2n_shutdown()`, it does not wait for the peer to respond |
| 212 | with a close_notify alert and does not discard any incoming application data. An |
| 213 | application can continue to call `s2n_recv()` after a call to `s2n_shutdown_send()`. |
| 214 | |
| 215 | `s2n_shutdown_send()` may still be called for earlier TLS versions, but most |
| 216 | TLS implementations will react by immediately discarding any pending writes and |
| 217 | closing the connection. |
| 218 | |
| 219 | If `s2n_shutdown_send()` is used, the application should still call `s2n_shutdown()` |
| 220 | or wait for `s2n_recv()` to return 0 to indicate end-of-data before cleaning up |
| 221 | the connection or closing the read side of the underlying transport. |
| 222 | |
| 223 | ## Custom IO Callbacks |
| 224 | |
| 225 | By default, s2n-tls sends and receives data using a provided file descriptor |
| 226 | (usually a socket) and the read/write system calls. To change this default behavior, |
| 227 | an application can implement custom send and receive methods using `s2n_connection_set_recv_cb()` |
| 228 | and `s2n_connection_set_send_cb()`. |
| 229 | The application can pass inputs (such as a file descriptor or destination buffer) |
| 230 | to the custom IO methods by using `s2n_connection_set_recv_ctx()` and `s2n_connection_set_send_ctx()`. |
| 231 | s2n-tls will call the custom IO methods with the custom context instead of calling |
| 232 | the default implementation. |
| 233 | |
| 234 | The custom IO methods may send or receive less than the requested length. They |
| 235 | should return the number of bytes sent/received, or set errno and return an error code < 0. |
| 236 | s2n-tls will interpret errno set to EWOULDBLOCK or EAGAIN as indicating a retriable |
| 237 | blocking error, and set **s2n_errno** and the s2n_blocked_status appropriately. |
| 238 | s2n-tls will interpret a return value of 0 as a closed connection. |