Skip to content

Commit c53a2df

Browse files
committed
api: fix up key management APIs
After reading the documentation, this seems to fit a bit better with how it is meant to be used. See docstrings for details.
1 parent dc8f8a8 commit c53a2df

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

src/api.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -516,13 +516,6 @@ impl Key {
516516
}
517517
}
518518

519-
/// Requests a key with the given description by searching the thread, process, and session
520-
/// keyrings.
521-
pub fn request_key_auth_key(create: bool) -> Result<Self> {
522-
let res = unsafe { keyctl_get_keyring_ID(KEY_SPEC_REQKEY_AUTH_KEY, create as libc::c_int) };
523-
check_call(i64::from(res), Key::new_impl(res))
524-
}
525-
526519
/// Requests a key with the given description by searching the thread, process, and session
527520
/// keyrings.
528521
pub fn request<K, D>(description: D) -> Result<Self>
@@ -635,6 +628,14 @@ impl Key {
635628
}
636629

637630
/// Create an object to manage a key request.
631+
///
632+
/// Before a key may be managed on a thread, an authorization key must be attached to an
633+
/// available thread keyring.
634+
///
635+
/// Only one key may be managed on a thread at a time. Managing a second key will
636+
/// invalidate any previous `KeyManager` constructions.
637+
///
638+
/// See `KeyManager::request_key_auth_key`.
638639
pub fn manage(&mut self) -> Result<KeyManager> {
639640
check_call(
640641
unsafe { keyctl_assume_authority(self.id) },
@@ -721,7 +722,7 @@ impl Description {
721722
}
722723

723724
/// A manager for a key to respond to instantiate a key request by the kernel.
724-
#[derive(Debug, Clone, PartialEq, Eq)]
725+
#[derive(Debug, PartialEq, Eq)]
725726
pub struct KeyManager {
726727
key: Key,
727728
}
@@ -733,6 +734,21 @@ impl KeyManager {
733734
}
734735
}
735736

737+
/// Requests the authorization key created by `request_key`.
738+
///
739+
/// This key must be present in an available keyring before `Key::manage` may be called.
740+
pub fn request_key_auth_key(create: bool) -> Result<Key> {
741+
let res = unsafe { keyctl_get_keyring_ID(KEY_SPEC_REQKEY_AUTH_KEY, create as libc::c_int) };
742+
check_call(i64::from(res), Key::new_impl(res))
743+
}
744+
745+
/// Drop authority for the current thread.
746+
///
747+
/// This invalidates
748+
pub fn drop_authority() -> Result<()> {
749+
check_call(unsafe { keyctl_assume_authority(0) }, ())
750+
}
751+
736752
/// Instantiate the key with the given payload.
737753
pub fn instantiate<P>(self, keyring: &Keyring, payload: P) -> Result<()>
738754
where

0 commit comments

Comments
 (0)