use crate::KeyError;
use core::ffi::CStr;
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct KeySerialId(pub i32);
pub enum KeyType {
KeyRing,
User,
Logon,
BigKey,
}
#[allow(dead_code)]
pub enum KeyRingIdentifier {
Thread = -1,
Process = -2,
Session = -3,
User = -4,
UserSession = -5,
Group = -6,
ReqKeyAuthKey = -7,
}
#[allow(dead_code)]
pub enum DefaultKeyring {
NoChange = -1,
Default = 0,
Thread = 1,
Process = 2,
Session = 3,
User = 4,
UserSession = 5,
Group = 6,
}
#[allow(dead_code)]
#[repr(u32)]
pub enum KeyCtlOperation {
GetKeyRingId = libc::KEYCTL_GET_KEYRING_ID,
JoinSessionKeyRing = libc::KEYCTL_JOIN_SESSION_KEYRING,
Update = libc::KEYCTL_UPDATE,
Revoke = libc::KEYCTL_REVOKE,
Chown = libc::KEYCTL_CHOWN,
SetPerm = libc::KEYCTL_SETPERM,
Describe = libc::KEYCTL_DESCRIBE,
Clear = libc::KEYCTL_CLEAR,
Link = libc::KEYCTL_LINK,
Unlink = libc::KEYCTL_UNLINK,
Search = libc::KEYCTL_SEARCH,
Read = libc::KEYCTL_READ,
Instantiate = libc::KEYCTL_INSTANTIATE,
Negate = libc::KEYCTL_NEGATE,
SetRequestKeyKeyring = libc::KEYCTL_SET_REQKEY_KEYRING,
SetTimeout = libc::KEYCTL_SET_TIMEOUT,
AssumeAuthority = libc::KEYCTL_ASSUME_AUTHORITY,
GetSecurityLabel = libc::KEYCTL_GET_SECURITY,
SessionToParent = libc::KEYCTL_SESSION_TO_PARENT,
Reject = libc::KEYCTL_REJECT,
InstantiageIov = libc::KEYCTL_INSTANTIATE_IOV,
Invalidate = libc::KEYCTL_INVALIDATE,
GetPersistent = libc::KEYCTL_GET_PERSISTENT,
DiffieHellmanCompute = libc::KEYCTL_DH_COMPUTE,
PubkeyQuery = libc::KEYCTL_PKEY_QUERY,
PubkeyEncrypt = libc::KEYCTL_PKEY_ENCRYPT,
PubkeyDecrypt = libc::KEYCTL_PKEY_DECRYPT,
PubkeySign = libc::KEYCTL_PKEY_SIGN,
PubkeyVerify = libc::KEYCTL_PKEY_VERIFY,
RestrictKeyring = libc::KEYCTL_RESTRICT_KEYRING,
Move = libc::KEYCTL_MOVE,
Capabilities = libc::KEYCTL_CAPABILITIES,
WatchKey = 32,
}
impl KeySerialId {
pub fn new(raw: i32) -> Self {
Self(raw)
}
pub fn as_raw_id(&self) -> i32 {
self.0
}
}
impl From<KeyType> for &'static CStr {
fn from(t: KeyType) -> &'static CStr {
unsafe {
match t {
KeyType::KeyRing => CStr::from_bytes_with_nul_unchecked(b"keyring\0"),
KeyType::User => CStr::from_bytes_with_nul_unchecked(b"user\0"),
KeyType::Logon => CStr::from_bytes_with_nul_unchecked(b"logon\0"),
KeyType::BigKey => CStr::from_bytes_with_nul_unchecked(b"big_key\0"),
}
}
}
}
impl From<KeySerialId> for i32 {
fn from(id: KeySerialId) -> i32 {
id.0
}
}
impl TryFrom<u64> for KeySerialId {
type Error = KeyError;
fn try_from(n: u64) -> Result<Self, Self::Error> {
Ok(Self(n.try_into().or(Err(KeyError::InvalidIdentifier))?))
}
}