Skip to content

Conversation

@toidiu
Copy link
Contributor

@toidiu toidiu commented Mar 11, 2025

Add a wrapper for the SSL_CTX_set_tlsext_ticket_key_cb, which allows consumers to configure the EVP_CIPHER_CTX and HMAC_CTX used for encrypting/decrypting session tickets.

See https://docs.openssl.org/1.0.2/man3/SSL_CTX_set_tlsext_ticket_key_cb/ for more details.

@toidiu toidiu marked this pull request as ready for review March 11, 2025 04:29
@toidiu toidiu force-pushed the toidiu/ticket_key_callback branch from 5a54a4d to 28f7678 Compare March 11, 2025 19:22
@toidiu toidiu requested a review from rushilmehra March 11, 2025 21:22
@toidiu toidiu force-pushed the toidiu/ticket_key_callback branch from 28f7678 to 9a6e166 Compare April 1, 2025 19:20
Copy link
Collaborator

@kornelski kornelski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

key_name is uninitialized, so it can't be &mut [u8; 16]

@toidiu toidiu force-pushed the toidiu/ticket_key_callback branch from d5ecffd to 91c95bb Compare September 29, 2025 21:15
@kornelski
Copy link
Collaborator

I've changed the code to use MaybeUninit instead of raw pointers where possible.

Comment on lines +274 to +276
assert!(!ptr.is_null());
unsafe { &mut *ptr.cast::<MaybeUninit<T>>() }
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirming my understanding: panic on assert seems appropriate because otherwise we have UB.

Copy link
Collaborator

@kornelski kornelski Oct 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

References must never be null, so they can't be made from a null pointer.

This assert isn't strictly necessary as long as the function is always given non-null pointers.

I thought it's better to be extra careful about this, I don't feel strongly about this choice. It could have been a Result (assuming null is a real possibility), or downgraded to debug_assert if we trust that we'll never get a null pointer in these functions.

@kornelski kornelski force-pushed the toidiu/ticket_key_callback branch from 6d711ac to e395c52 Compare September 30, 2025 18:23
@toidiu toidiu force-pushed the toidiu/ticket_key_callback branch from fbf9dff to 8c3bca2 Compare September 30, 2025 21:49
@toidiu toidiu requested a review from kornelski September 30, 2025 21:55
///
/// # Safety
///
/// The caller must ensure HMAC_CTX has been initalized.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this safety requirement. It's not clear what "initialized" here refers to, since this is an init function doing initialization.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was conflating documentation between this and session resumption. Removed and removed the unsafe.

///
/// # Safety
///
/// The caller must ensure EVP_CIPHER_CTX has been initalized.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again not clear what "initialized" is

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was conflating documentation between this and session resumption. Removed and removed the unsafe.

///
/// The caller must ensure EVP_CIPHER_CTX has been initalized.
///
/// The caller is responsible for ensuring the length of `key` and `iv` are appropriate for the
Copy link
Collaborator

@kornelski kornelski Oct 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only length of the key needs to be checked, right? The iv is an array of max len.

Is there a cheap way to check key length against the Cipher to make it a safe function?

In raw_ticket_key the key is assumed to be 16 bytes long. Why is one place fixed, and another is flexible?

Copy link
Contributor Author

@toidiu toidiu Oct 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 2d5be20

Only length of the key needs to be checked, right? The iv is an array of max len.
Is there a cheap way to check key length against the Cipher to make it a safe function?

Yea good point. We can do a key.len() != cipher.key_len() and return early. But not sure if ErrorStack::get() is correct error to return here.

In raw_ticket_key the key is assumed to be 16 bytes long. Why is one place fixed, and another is flexible?

raw_ticket_key is dealing with key_name, which is guaranteed to be 16 bytes (docs).

Here, the application is calling EVP_EncryptInit_ex with the correct key and iv, the sizes of which depend on the chosen cipher.

I also expanded the tests to use the iv and key_name values from the callback.

@kornelski kornelski force-pushed the toidiu/ticket_key_callback branch from b4c73ab to 93270a6 Compare October 1, 2025 02:53
@kornelski
Copy link
Collaborator

It's nicer with safe wrapper types. I've changed to use ForeignTypeRef to avoid ManuallyDrop.

Generally looks good.

@toidiu
Copy link
Contributor Author

toidiu commented Oct 1, 2025

It's nicer with safe wrapper types.

Yea, much nicer not to use the ffi functions, I was sold when I got rid of the ret == 1 check.

I've changed to use ForeignTypeRef to avoid ManuallyDrop.

Sweet. I actually had to debug a panic in tests before adding the ManuallyDrop so this seems like the correct choice.

@toidiu toidiu force-pushed the toidiu/ticket_key_callback branch from 2d5be20 to 20f91cb Compare October 1, 2025 06:17
@toidiu toidiu requested a review from kornelski October 1, 2025 06:19
@kornelski
Copy link
Collaborator

ErrorStack::get() is for cases when FFI reports an error. I'll fix it in a separate PR.

@kornelski kornelski merged commit 353ea62 into cloudflare:master Oct 1, 2025
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants