Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: derive from Clone on PrivateKeyDer enum #29

Conversation

ologbonowiwi
Copy link

No description provided.

@ctz
Copy link
Member

ctz commented Feb 7, 2024

I think we specifically want to avoid making private key material types Clone, so it is possible to minimise the number of places they exist in memory. What problem are you trying to solve?

@cpu
Copy link
Member

cpu commented Feb 7, 2024

Hi @ologbonowiwi

Can you speak to your motivation for this change?

In the past we've explicitly avoided adding Clone to private key types as it can lead to leaking private key bytes.

Edit: oops, sorry for the double post :-) Looks like Ctz and I were racing.

@cpu
Copy link
Member

cpu commented Feb 7, 2024

Some previous discussion in #8, #26, rustls/pemfile#19

@ologbonowiwi
Copy link
Author

I have a project where I depend on these keys, and I'd like to have the ability to Clone a struct that is an array of PrivateKeyDer.

Due to an architectural decision on the project I'm working on (tailcallhq/tailcall), we have restrictions on where to do IO operations, and we wish to evaluate these keys early and use them later on.

If I can't clone, what I'll end up having to do is holding the bytes/string (which is clonable, can be leaked, and will not have the explicitly of knowing it is a private key), and later on (right before using it), call rustls_pemfile::read_all with the bytes that hold the private key in plain text

@ologbonowiwi
Copy link
Author

And also, thanks for the quick response! 😄

@ctz
Copy link
Member

ctz commented Feb 7, 2024

These objects are immutable, so having multiple independent copies is not really useful. Have you considered wrapping them in Rc/Arc? This gives you (~free/cheap) clone without any underlying copies happening.

@ologbonowiwi
Copy link
Author

It makes sense @ctz! I'm quite new to the Rust ecosystem, so I didn't knew about this "workaround". Thanks for the hand!

@cpu
Copy link
Member

cpu commented Feb 7, 2024

I think this is a place where we could help users with more documentation on the rationale for not including Clone: #30

@ologbonowiwi
Copy link
Author

ologbonowiwi commented Feb 7, 2024

Hey guys, update on my use case: early evaluate the Item worked to move it around the application, but it did not work to load the file, as TlsAcceptor::builder().with_single_cert needs ownership of the key, which can't be given since it can't be cloned and is inside an Arc.

What I'm planning to do to avoid such issue is hold the plain file (content) on the application and late evaluate it to an Item right before the creation of TlsAcceptor. Anyway, thanks for your kindness and velocity on giving me feedback around this issue.

@djc
Copy link
Member

djc commented Feb 7, 2024

@ologbonowiwi
Copy link
Author

ologbonowiwi commented Feb 7, 2024

It worked @djc, thanks 🙌🏽 was able to achieve a clone-like behavior by using Arc + clone_key.

I apologize for my ignorance, but what's the difference between deriving explicitly from Clone and implementing clone_key, and why clone_key is safe to use?

@djc
Copy link
Member

djc commented Feb 7, 2024

The idea was that clone_key() forces an explicit usage, whereas it would be trivial to oversee deriving Clone of some type that includes a PrivateKeyDer, leading to the key potentially leaking into places it shouldn't go.

It's admittedly a nuanced topic -- this is the line we've ended up at for now.

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.

4 participants