| 1 | # Connection Serialization |
| 2 | |
| 3 | Connection Serialization allows TLS connection state to be serialized into a byte string. This allows the connection to be transported to a different host or stored to disk. |
| 4 | |
| 5 | <div class="warning"> |
| 6 | This feature is dangerous. The serialized connection contains cryptographic secrets, sequence numbers, the cipher suite, and the protocol version in plaintext. An attacker with access to this data can decrypt any past and future communications from the connection. Additionally, s2n-tls does not include a MAC or signature over the serialized data, so a modified blob will be deserialized and used as-is. |
| 7 | |
| 8 | Users MUST both encrypt and MAC the serialized connection to provide secrecy and integrity. The simplest way to do this is to transport the serialized connection using a protocol like TLS. If the blob is stored in a database, shared cache, or sent over a network, it must be protected before storage or transmission. |
| 9 | </div> |
| 10 | |
| 11 | ## Use Case |
| 12 | Connection Serialization is a feature used to support advanced, exceptional use cases. Most users will not find it useful. |
| 13 | |
| 14 | One use case for connection serialization is to enable a more complete TLS offload. Traditional private-key offloading allows the certificate private key to be stored separately from the application server, but connection-serialization TLS offloading allows all TLS materials and TLS configuration to be stored separately from the application server. |
| 15 | |
| 16 | ### Private Key Offloading |
| 17 | For reference, this is a common private key offloading architecture: |
| 18 | ``` |
| 19 | client application server key store |
| 20 | ┌─────────────────────────┐ |
| 21 | ┌─────┐ │ s2n-tls config ┼───────►┌─────────────┐ |
| 22 | │ ┼───────►│ │◄───────┼ private key │ |
| 23 | └─────┘ │ public key (x509 chain) │ ▲ └─────────────┘ |
| 24 | └─────────────────────────┘ │ |
| 25 | │ |
| 26 | server sends material to sign |
| 27 | key store returns signature |
| 28 | ``` |
| 29 | |
| 30 | The client will send TLS handshake bytes (e.g. the Client Hello message) to the application server. The server sends all private key operations to the key store, but the public x509 certificate chain and TLS configuration are still owned by the application server. |
| 31 | |
| 32 | ### Connection-Serialization TLS Offloading |
| 33 | ``` |
| 34 | key store |
| 35 | client application server ┌──────────────────────────┐ |
| 36 | │ s2n-tls config │ |
| 37 | ┌─────┐ ┌─────┐ ────────►│ │ |
| 38 | │ ┼───────►│ │◄─────────┼ private key │ |
| 39 | └─────┘ └─────┘ ▲ │ │ |
| 40 | │ │ public key (x509 chain) │ |
| 41 | │ └──────────────────────────┘ |
| 42 | │ |
| 43 | server sends handshake bytes |
| 44 | key store returns serialized connection |
| 45 | ``` |
| 46 | In connection-serialization TLS Offloading, the application server will forward all of the handshake bytes to the key store. For the duration of the handshake, the application server is behaving as an L4 proxy, and doesn't parse any of the TLS protocol. Once the TLS handshake is complete, the key store will call `s2n_connection_serialize()` to serialize the connection to bytes, and send it back to the application server. The application server will then deserialize the connection using `s2n_connection_deserialize()`. At this point, the application server can read and write application data to the client. |
| 47 | |
| 48 | In this architecture the application server does not store any TLS certificate material, and it does not own the TLS configuration. |
| 49 | |
| 50 | ## Usage |
| 51 | |
| 52 | To enable connection serialization, applications must first set a serialization version on the config using `s2n_config_set_serialization_version()`. Setting a version prevents forwards-incompatible changes from causing deserialization failures. See [Serialization Version Deployment](ch14-connection-serialization.md#serialization-version-deployment) for more information. |
| 53 | |
| 54 | To serialize a connection, applications must first obtain a large enough buffer to store the serialized connection. The size of the serialized connection can be obtained with `s2n_connection_serialization_length()`. The serialized connection can then be written into the buffer with `s2n_connection_serialize()`. |
| 55 | |
| 56 | To deserialize the connection, call `s2n_connection_deserialize()`. Note that a serialized connection stores a minimal amount of state. So while the deserialized connection can be used to read and write application data, most handshake information will not be preserved, and connection-level getters may not function normally. |
| 57 | |
| 58 | ## Serialization Version Deployment |
| 59 | Connection Serialization is backwards compatible, but not forwards compatible. This means that a new version of s2n-tls can deserialize old formats, but an old version of s2n-tls can not deserialize new formats. |
| 60 | |
| 61 | The serialization version controls what format is written, and the library version controls what formats can be read. To avoid deserialization failures every host in a fleet should support reading a new format before any hosts begin writing that format. |