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

Make key slot release implicit #59

Merged
merged 1 commit into from
Oct 30, 2019

Conversation

ionut-arm
Copy link
Member

Key slots are now ✨automagically✨ released when the Key object is dropped.

Resolves #57

@ionut-arm ionut-arm added the enhancement New feature or request label Oct 29, 2019
@ionut-arm ionut-arm requested a review from hug-dev October 29, 2019 16:02
@ionut-arm ionut-arm self-assigned this Oct 29, 2019
Copy link
Member

@hug-dev hug-dev left a comment

Choose a reason for hiding this comment

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

Having a struct for key handles operations looks nicer ✨!


/// Wrapper around raw `psa_key_handle_t` which allows for easier manipulation of
/// handles and the attributes associated with them.
pub struct Key<'a>(psa_key_handle_t, &'a Mutex<()>);
Copy link
Member

Choose a reason for hiding this comment

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

Maybe it should be KeyHandle?

Copy link
Member Author

Choose a reason for hiding this comment

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

That's how it was named initially, but I renamed it because I thought an operation called get_key_attributes sounds a bit weird for a key handle.

Copy link
Member

Choose a reason for hiding this comment

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

Fair enough!

/// Release the key stored under this handle.
pub fn release_key(&mut self) {
unsafe {
let _guard = self.1.lock().expect("Grabbing key handle mutex failed");
Copy link
Member

Choose a reason for hiding this comment

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

I am wondering if there are any implications/safety concerns of putting a safe statement inside an unsafe block? Maybe that could be outside of it and still work?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll have a look through the Nomicon again, see what they think

Copy link
Member

Choose a reason for hiding this comment

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

I was thinking that if one of those safe function that we put in an unsafe block suddendly become unsafe, then we will still execute them without any warning 😕
But I do not know if that can happen


impl Drop for Key<'_> {
fn drop(&mut self) {
if self.0 != 0 {
Copy link
Member

Choose a reason for hiding this comment

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

Will the psa_close_key fail if the handle is 0? Maybe that check could be put in the release_key function if so.

Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure it does - we don't check the return status for close_key anyway! But we could put a check in release_key too.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've updated - now the check is done in release_key which is called every time a Key is dropped

Copy link
Member Author

@ionut-arm ionut-arm left a comment

Choose a reason for hiding this comment

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

Shall update tomorrow

This commit adds functionality to ensure that when key handles are
dropped in the Mbed Provider, the slot is released automatically. Thus,
there is no need to explicitly close handles anymore.

Signed-off-by: Ionut Mihalcea <ionut.mihalcea@arm.com>
Copy link
Member

@hug-dev hug-dev left a comment

Choose a reason for hiding this comment

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

Thanks for the change. We discussed that a new issue will be raised independently to look at our unsafe blocks.

@ionut-arm ionut-arm merged commit c4c369f into parallaxsecond:master Oct 30, 2019
@ionut-arm ionut-arm deleted the key_handle branch October 30, 2019 11:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Drop key handles implicitly
2 participants