use crate::utils::CStr;
use crate::KeyError;
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[repr(transparent)]
pub struct KeySerialId(pub i32);
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum KeyType {
KeyRing,
User,
Logon,
BigKey,
}
#[allow(dead_code)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
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 = 0,
JoinSessionKeyRing = 1,
Update = 2,
Revoke = 3,
Chown = 4,
SetPerm = 5,
Describe = 6,
Clear = 7,
Link = 8,
Unlink = 9,
Search = 10,
Read = 11,
Instantiate = 12,
Negate = 13,
SetRequestKeyKeyring = 14,
SetTimeout = 15,
AssumeAuthority = 16,
GetSecurityLabel = 17,
SessionToParent = 18,
Reject = 19,
InstantiageIov = 20,
Invalidate = 21,
GetPersistent = 22,
DiffieHellmanCompute = 23,
PubkeyQuery = 24,
PubkeyEncrypt = 25,
PubkeyDecrypt = 26,
PubkeySign = 27,
PubkeyVerify = 28,
RestrictKeyring = 29,
Move = 30,
Capabilities = 31,
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 {
match t {
KeyType::KeyRing => c"keyring",
KeyType::User => c"user",
KeyType::Logon => c"logon",
KeyType::BigKey => c"big_key",
}
}
}
impl TryFrom<&str> for KeyType {
type Error = KeyError;
fn try_from(s: &str) -> Result<Self, Self::Error> {
let val = match s {
"keyring" => KeyType::KeyRing,
"user" => KeyType::User,
"logon" => KeyType::Logon,
"big_key" => KeyType::BigKey,
_ => return Err(KeyError::InvalidIdentifier),
};
Ok(val)
}
}
impl From<KeySerialId> for i32 {
fn from(id: KeySerialId) -> i32 {
id.0
}
}
impl From<i32> for KeySerialId {
fn from(n: i32) -> Self {
Self(n)
}
}
impl TryFrom<i64> for KeySerialId {
type Error = KeyError;
fn try_from(n: i64) -> Result<Self, Self::Error> {
Ok(Self(n.try_into().or(Err(KeyError::InvalidIdentifier))?))
}
}