use crate::database::universal_types::{UniversalTimestamp, UniversalUuid};
use async_trait::async_trait;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum KeyError {
#[error("Key not found: {0}")]
NotFound(UniversalUuid),
#[error("Key has been revoked: {0}")]
Revoked(UniversalUuid),
#[error("Key name already exists for this organization: {0}")]
DuplicateName(String),
#[error("Invalid key format: {0}")]
InvalidFormat(String),
#[error("Invalid PEM format: {0}")]
InvalidPem(String),
#[error("Encryption error: {0}")]
Encryption(String),
#[error("Decryption error: {0}")]
Decryption(String),
#[error("Trust relationship already exists")]
TrustAlreadyExists,
#[error("Trust relationship not found")]
TrustNotFound,
#[error("Database error: {0}")]
Database(String),
}
#[derive(Debug, Clone)]
pub struct SigningKeyInfo {
pub id: UniversalUuid,
pub org_id: UniversalUuid,
pub key_name: String,
pub fingerprint: String,
pub public_key: Vec<u8>,
pub created_at: UniversalTimestamp,
pub revoked_at: Option<UniversalTimestamp>,
}
impl SigningKeyInfo {
pub fn is_active(&self) -> bool {
self.revoked_at.is_none()
}
}
#[derive(Debug, Clone)]
pub struct TrustedKeyInfo {
pub id: UniversalUuid,
pub org_id: UniversalUuid,
pub fingerprint: String,
pub public_key: Vec<u8>,
pub key_name: Option<String>,
pub trusted_at: UniversalTimestamp,
pub revoked_at: Option<UniversalTimestamp>,
}
impl TrustedKeyInfo {
pub fn is_active(&self) -> bool {
self.revoked_at.is_none()
}
}
#[derive(Debug, Clone)]
pub struct PublicKeyExport {
pub fingerprint: String,
pub public_key_pem: String,
pub public_key_raw: Vec<u8>,
}
#[async_trait]
pub trait KeyManager: Send + Sync {
async fn create_signing_key(
&self,
org_id: UniversalUuid,
name: &str,
master_key: &[u8],
) -> Result<SigningKeyInfo, KeyError>;
async fn get_signing_key_info(&self, key_id: UniversalUuid)
-> Result<SigningKeyInfo, KeyError>;
async fn get_signing_key(
&self,
key_id: UniversalUuid,
master_key: &[u8],
) -> Result<(Vec<u8>, Vec<u8>), KeyError>;
async fn export_public_key(&self, key_id: UniversalUuid) -> Result<PublicKeyExport, KeyError>;
async fn trust_public_key(
&self,
org_id: UniversalUuid,
public_key: &[u8],
name: Option<&str>,
) -> Result<TrustedKeyInfo, KeyError>;
async fn trust_public_key_pem(
&self,
org_id: UniversalUuid,
pem: &str,
name: Option<&str>,
) -> Result<TrustedKeyInfo, KeyError>;
async fn revoke_signing_key(&self, key_id: UniversalUuid) -> Result<(), KeyError>;
async fn revoke_trusted_key(&self, key_id: UniversalUuid) -> Result<(), KeyError>;
async fn grant_trust(
&self,
parent_org: UniversalUuid,
child_org: UniversalUuid,
) -> Result<(), KeyError>;
async fn revoke_trust(
&self,
parent_org: UniversalUuid,
child_org: UniversalUuid,
) -> Result<(), KeyError>;
async fn list_signing_keys(
&self,
org_id: UniversalUuid,
) -> Result<Vec<SigningKeyInfo>, KeyError>;
async fn list_trusted_keys(
&self,
org_id: UniversalUuid,
) -> Result<Vec<TrustedKeyInfo>, KeyError>;
async fn find_trusted_key(
&self,
org_id: UniversalUuid,
fingerprint: &str,
) -> Result<Option<TrustedKeyInfo>, KeyError>;
}