Skip to main content

opentdf_kas/
types.rs

1//! Request and response types for KAS rewrap operations
2
3use serde::{Deserialize, Serialize};
4
5/// Algorithm type for rewrap operations
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub enum KeyAlgorithm {
8    /// EC P-256 for NanoTDF
9    EcP256,
10    /// RSA-2048 for Standard TDF
11    Rsa2048,
12}
13
14impl KeyAlgorithm {
15    /// Parse algorithm string from OpenTDF spec format
16    pub fn parse(s: &str) -> Option<Self> {
17        match s {
18            "" => Some(KeyAlgorithm::Rsa2048), // Empty = Standard TDF RSA
19            s if s.starts_with("ec") || s.contains("secp256") => Some(KeyAlgorithm::EcP256),
20            s if s.starts_with("rsa") => Some(KeyAlgorithm::Rsa2048),
21            _ => None,
22        }
23    }
24
25    /// Check if this is an EC algorithm
26    pub fn is_ec(&self) -> bool {
27        matches!(self, KeyAlgorithm::EcP256)
28    }
29
30    /// Check if this is an RSA algorithm
31    pub fn is_rsa(&self) -> bool {
32        matches!(self, KeyAlgorithm::Rsa2048)
33    }
34}
35
36/// Key access object for rewrap
37#[derive(Debug, Clone, Serialize, Deserialize)]
38#[serde(rename_all = "camelCase")]
39pub struct KeyAccessObject {
40    /// Base64-encoded header (NanoTDF header or RSA-wrapped DEK)
41    pub header: String,
42    /// Key access type
43    #[serde(rename = "type")]
44    pub type_field: String,
45    /// KAS URL
46    pub url: String,
47    /// Protocol (e.g., "nanotdf", "tdf")
48    pub protocol: String,
49}
50
51/// Information about a key to unwrap
52#[derive(Debug, Clone)]
53pub struct KeyInfo {
54    /// Algorithm type
55    pub algorithm: KeyAlgorithm,
56    /// Base64-encoded wrapped key material
57    pub wrapped_key: String,
58    /// Optional policy ID
59    pub policy_id: Option<String>,
60}
61
62/// Result of a rewrap operation
63#[derive(Debug, Clone, Serialize, Deserialize)]
64#[serde(rename_all = "camelCase")]
65pub struct RewrapResult {
66    /// Status: "permit" or "fail"
67    pub status: String,
68    /// Base64-encoded rewrapped key (nonce + ciphertext + tag)
69    pub kas_wrapped_key: Option<String>,
70    /// Error message if status is "fail"
71    pub error: Option<String>,
72}
73
74impl RewrapResult {
75    /// Create a successful result
76    pub fn success(wrapped_key: String) -> Self {
77        Self {
78            status: "permit".to_string(),
79            kas_wrapped_key: Some(wrapped_key),
80            error: None,
81        }
82    }
83
84    /// Create a failed result
85    pub fn failure(error: String) -> Self {
86        Self {
87            status: "fail".to_string(),
88            kas_wrapped_key: None,
89            error: Some(error),
90        }
91    }
92}
93
94#[cfg(test)]
95mod tests {
96    use super::*;
97
98    #[test]
99    fn test_algorithm_parsing() {
100        assert_eq!(
101            KeyAlgorithm::parse("ec:secp256r1"),
102            Some(KeyAlgorithm::EcP256)
103        );
104        assert_eq!(KeyAlgorithm::parse("ec"), Some(KeyAlgorithm::EcP256));
105        assert_eq!(KeyAlgorithm::parse("rsa:2048"), Some(KeyAlgorithm::Rsa2048));
106        assert_eq!(KeyAlgorithm::parse("rsa"), Some(KeyAlgorithm::Rsa2048));
107        assert_eq!(KeyAlgorithm::parse(""), Some(KeyAlgorithm::Rsa2048));
108        assert_eq!(KeyAlgorithm::parse("unknown"), None);
109    }
110
111    #[test]
112    fn test_rewrap_result() {
113        let success = RewrapResult::success("encoded_key".to_string());
114        assert_eq!(success.status, "permit");
115        assert!(success.kas_wrapped_key.is_some());
116        assert!(success.error.is_none());
117
118        let failure = RewrapResult::failure("error message".to_string());
119        assert_eq!(failure.status, "fail");
120        assert!(failure.kas_wrapped_key.is_none());
121        assert!(failure.error.is_some());
122    }
123}