Add optional callback for user-defined TLS 1.2 key derivation function#5107
Add optional callback for user-defined TLS 1.2 key derivation function#5107lars-du wants to merge 1 commit intorandombit:masterfrom
Conversation
reneme
left a comment
There was a problem hiding this comment.
Interesting use case! Thanks for the polished PR. In fact I always wondered if it would be useful to enable the application to swap out cryptographic primitives for things like this.
I think it would be valuable to generalize this to TLS 1.3 though. And that shouldn't be too hard either, given that TLS 1.3's key schedule is also just based on KDFs. See the constructor of Cipher_State in tls_cipher_state.cpp (roughly line 442). We should be able to replace those hard-coded constructions of HKDF_Extract and HKDF_Expand with calls into your new callback.
The only caveat I see immediately: in your example you're using the "label" to decide when to delegate to your HPE to obtain the PSK. In TLS 1.3 the invocation to HKDF-Extract doesn't have use-case specific labels. So that might be a show stopper.
72dcf0d to
53fad43
Compare
Yes, we also thought about extending this to TLS 1.3 and we did some analysis a while ago. The issue with TLS 1.3 is, that it derives a lot of keys and some of them already quite early before any random/connection specific data is involved. TLS 1.3 derives the If the From the The The So if an attacker gets the This might still be some gain in security compared to having the PSK in the userland, but not much. Since the This would then maybe provide sufficient security as the So all in all, we think it would be possible to achieve something similar with TLS 1.3 but it is definitely way more complex. |
53fad43 to
d77260d
Compare
|
Hi @reneme @randombit, do you need more input from our side for this PR? |
d77260d to
b0a2ccf
Compare
reneme
left a comment
There was a problem hiding this comment.
Thanks for your patience on this. This looks good to me, but I'm leaving a second review and the merge to @randombit.
I agree with your observations. Essentially, in Botan we would need to make some of the TLS 1.3 key schedule implementation (currently in tls_cipher_state.h) customizable and optionally expose it to the application. Currently, objects of this class derive and hold all the relevant secrets of a TLS 1.3 connection and implement their usages. For your use case of keeping the PSK in a protected environment, most of the key derivation would therefore need to be implemented inside the HPE (at least until the ServerHello is received and the ephemeral key exchange is done). Therefore, in Botan, we would need to at least make the state transition methods customizable by the application. See tls_cipher_state.cpp for some more documentation. Al least those methods would need to become customizable:
But I think, if we ever extend the TLS 1.3 implementation in that way, it would probably make sense to make the entire cipher state class customizable. That would give an application maximal flexibility when and even if key material leaves the HPE. In the extreme case no such key material would ever have to leave the HPE including for the bulk traffic encryption if this is feasible or desired. |
b0a2ccf to
19c70c5
Compare
… secret calculation
19c70c5 to
2d5ff07
Compare
Use Case
We use Botan for TLS-PSK on embedded devices equipped with a hardware-protected environment (HPE), such as a Hardware Security Module (HSM) or Trusted Execution Environment (TEE). The pre-shared key (PSK) is securely provisioned to the devices and stored within the HPE. For security reasons, the PSK must not leave the HPE at runtime. This presents a challenge: Botan runs in the normal user environment (Rich Execution Environment), while the PSK is only accessible inside the HPE, meaning Botan cannot access the PSK directly.
To address this issue, this PR introduces an optional callback that allows the TLS key derivation function (KDF) to be delegated to the application. In our scenario, this callback is used to offload the computation of the TLS master secret to the HPE during the TLS 1.2 handshake. This enables Botan to use the master secret derived from the PSK without ever requiring direct access to the PSK itself.
Usage example
Callback implementation in user code
Implementation of user-defined TLS 1.2 pseudo random function