| 1 | # Offloading Private Key Operations |
| 2 | By default, s2n-tls automatically uses the configured private key to synchronously perform the signature |
| 3 | and decryption operations required for a tls handshake. However, this default behavior may not |
| 4 | work for some situations. |
| 5 | |
| 6 | For example: |
| 7 | * An application may want to perform the CPU-expensive signature and decryption operations |
| 8 | asynchronously to avoid blocking the main event loop. |
| 9 | See [Asynchronous private key operations](#asynchronous-private-key-operations) |
| 10 | * An application may not have direct access to the private key, such as when using PKCS#11. |
| 11 | See [Offloading private key operations](#offloading-private-key-operations-1) |
| 12 | |
| 13 | To handle these use cases, s2n-tls provides a callback to allow users to control how these operations |
| 14 | are performed. The callback is set via `s2n_config_set_async_pkey_callback()` and is triggered |
| 15 | every time `s2n_negotiate()` performs an action involving the private key. The callback is passed |
| 16 | **op**, an opaque object representing the private key operation. To avoid memory leaks, **op** must |
| 17 | always eventually be freed by calling `s2n_async_pkey_op_free()`. |
| 18 | |
| 19 | The private key operation can be performed by calling `s2n_async_pkey_op_perform()` |
| 20 | (or `s2n_async_pkey_op_set_output()`: see [Offloading private key operations](#offloading-private-key-operations-1)). |
| 21 | The required private key can be retrieved using the `s2n_connection_get_selected_cert()` and `s2n_cert_chain_and_key_get_private_key()` calls. The operation can then be finalized with `s2n_async_pkey_op_apply()` to continue the handshake. |
| 22 | |
| 23 | ## Asynchronous Private Key Operations |
| 24 | |
| 25 | When s2n-tls is used in non-blocking mode, private key operations can be completed |
| 26 | asynchronously. This model can be useful to move execution of |
| 27 | CPU-heavy private key operations out of the main |
| 28 | event loop, preventing `s2n_negotiate()` from blocking the loop for a few |
| 29 | milliseconds each time the private key operation needs to be performed. |
| 30 | |
| 31 | To handle private key operations asynchronously, return from the callback without calling |
| 32 | `s2n_async_pkey_op_perform()` or `s2n_async_pkey_op_apply()`. Usually the user would do this |
| 33 | by spawning a separate thread to perform **op** and immediately returning **S2N_SUCCESS** |
| 34 | from the callback without waiting for that separate thread to complete. In response, |
| 35 | `s2n_negotiate()` will return **S2N_FAILURE** with an error of type **S2N_ERR_T_BLOCKED** |
| 36 | and **s2n_blocked_status** set to **S2N_BLOCKED_ON_APPLICATION_INPUT**. |
| 37 | All subsequent calls to `s2n_negotiate()` will produce the same result until `s2n_async_pkey_op_apply()` |
| 38 | is called to finalize the **op**. |
| 39 | |
| 40 | Note: It is not safe to call multiple functions on the same **conn** or |
| 41 | **op** objects from 2 different threads at the same time. Doing so will |
| 42 | produce undefined behavior. However it is safe to have a call to a |
| 43 | function involving only **conn** at the same time as a call to a |
| 44 | function involving only **op**, as those objects are not coupled with |
| 45 | each other. It is also safe to free **conn** or **op** at any moment with |
| 46 | respective function calls, with the exception that **conn** cannot |
| 47 | be freed inside the `s2n_async_pkey_fn()` callback. |
| 48 | |
| 49 | ## Synchronous Private Key Operations |
| 50 | |
| 51 | Despite the "async" in the function names, private key operations can also be completed synchronously using the callback. |
| 52 | To complete an operation synchronously, simply call `s2n_async_pkey_op_perform()` and `s2n_async_pkey_op_apply()` inside the callback. |
| 53 | If the callback succeeds, the handshake will continue uninterrupted. |
| 54 | If the callback fails, `s2n_negotiate()` will fail with an error of type **S2N_ERR_T_INTERNAL**. |
| 55 | |
| 56 | ## Offloading Private Key Operations |
| 57 | |
| 58 | The `s2n_async_pkey_op_perform()` call used to perform a private key operation requires |
| 59 | direct access to the private key. In some cases, like when using PKCS#11, users may not |
| 60 | have access to the private key. In these cases, we can substitute `s2n_async_pkey_op_set_output()` |
| 61 | for `s2n_async_pkey_op_perform()` to tell s2n-tls the result of the operation rather than |
| 62 | having s2n-tls perform the operation itself. |
| 63 | |
| 64 | s2n-tls provides a number of calls to gather the information necessary for |
| 65 | an outside module or library to perform the operation. The application can query the type of private |
| 66 | key operation by calling `s2n_async_pkey_op_get_op_type()`. In order to perform |
| 67 | an operation, the application must ask s2n-tls to copy the operation's input into an |
| 68 | application supplied buffer. The appropriate buffer size can be determined by calling |
| 69 | `s2n_async_pkey_op_get_input_size()`. Once a buffer of the proper size is |
| 70 | allocated, the application can request the input data by calling `s2n_async_pkey_op_get_input()`. |
| 71 | After the operation is completed, the finished output can be copied back to S2N by calling `s2n_async_pkey_op_set_output()`. |
| 72 | Once the output is set, the private key operation can be completed by calling `s2n_async_pkey_op_apply()` as usual. |
| 73 | |
| 74 | Offloading can be performed either synchronously or asynchronously. If the offloaded operation |
| 75 | fails synchronously, simply return **S2N_FAILURE** from the callback. If the offloaded operation |
| 76 | fails asynchronously, s2n-tls does not provide a way to communicate that result. Instead, |
| 77 | simply shutdown and cleanup the connection as you would for any other error. |