use std::{collections::HashMap, fmt};
use serde::{Deserialize, Serialize};
use zeroize::Zeroizing;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum KeyPurpose {
EncryptDecrypt,
SignVerify,
Mac,
}
impl fmt::Display for KeyPurpose {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::EncryptDecrypt => write!(f, "encrypt_decrypt"),
Self::SignVerify => write!(f, "sign_verify"),
Self::Mac => write!(f, "mac"),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum KeyState {
Enabled,
Disabled,
PendingDeletion,
Destroyed,
}
impl fmt::Display for KeyState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Enabled => write!(f, "enabled"),
Self::Disabled => write!(f, "disabled"),
Self::PendingDeletion => write!(f, "pending_deletion"),
Self::Destroyed => write!(f, "destroyed"),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KeyReference {
pub provider: String,
pub key_id: String,
pub key_alias: Option<String>,
pub purpose: KeyPurpose,
pub created_at: i64,
}
impl KeyReference {
#[must_use]
pub const fn new(
provider: String,
key_id: String,
purpose: KeyPurpose,
created_at: i64,
) -> Self {
Self {
provider,
key_id,
key_alias: None,
purpose,
created_at,
}
}
#[must_use]
pub fn with_alias(mut self, alias: String) -> Self {
self.key_alias = Some(alias);
self
}
#[must_use]
pub fn qualified_id(&self) -> String {
format!("{}:{}", self.provider, self.key_id)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EncryptedData {
pub ciphertext: String,
pub key_reference: KeyReference,
pub algorithm: String,
pub encrypted_at: i64,
pub context: HashMap<String, String>,
}
impl EncryptedData {
#[must_use]
pub const fn new(
ciphertext: String,
key_reference: KeyReference,
algorithm: String,
encrypted_at: i64,
context: HashMap<String, String>,
) -> Self {
Self {
ciphertext,
key_reference,
algorithm,
encrypted_at,
context,
}
}
}
#[derive(Debug, Clone)]
pub struct DataKeyPair {
pub plaintext_key: Zeroizing<Vec<u8>>,
pub encrypted_key: EncryptedData,
pub key_reference: KeyReference,
}
impl DataKeyPair {
#[must_use]
pub fn new(
plaintext_key: Vec<u8>,
encrypted_key: EncryptedData,
key_reference: KeyReference,
) -> Self {
Self {
plaintext_key: Zeroizing::new(plaintext_key),
encrypted_key,
key_reference,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RotationPolicy {
pub enabled: bool,
pub rotation_period_days: u32,
pub last_rotation: Option<i64>,
pub next_rotation: Option<i64>,
}
impl RotationPolicy {
#[must_use]
pub const fn new(enabled: bool, rotation_period_days: u32) -> Self {
Self {
enabled,
rotation_period_days,
last_rotation: None,
next_rotation: None,
}
}
}