1use crate::error::{Error, Result};
8use crate::message::{Jwe, JweProtected, JwsProtected};
9use async_trait::async_trait;
10use serde_json::Value;
11use std::fmt::Debug;
12use std::sync::Arc;
13
14#[async_trait]
20pub trait AgentKey: Send + Sync + Debug {
21 fn key_id(&self) -> &str;
23
24 fn public_key_jwk(&self) -> Result<Value>;
26
27 fn did(&self) -> &str;
29
30 fn key_type(&self) -> &str;
32}
33
34#[derive(Debug, Clone, PartialEq, Eq)]
36pub enum JwsAlgorithm {
37 EdDSA,
39 ES256,
41 ES256K,
43}
44
45impl JwsAlgorithm {
46 pub fn as_str(&self) -> &'static str {
48 match self {
49 JwsAlgorithm::EdDSA => "EdDSA",
50 JwsAlgorithm::ES256 => "ES256",
51 JwsAlgorithm::ES256K => "ES256K",
52 }
53 }
54}
55
56#[derive(Debug, Clone, PartialEq, Eq)]
58pub enum JweAlgorithm {
59 EcdhEsA256kw,
61 EcdhEs,
63}
64
65impl JweAlgorithm {
66 pub fn as_str(&self) -> &'static str {
68 match self {
69 JweAlgorithm::EcdhEsA256kw => "ECDH-ES+A256KW",
70 JweAlgorithm::EcdhEs => "ECDH-ES",
71 }
72 }
73}
74
75#[derive(Debug, Clone, PartialEq, Eq)]
77pub enum JweEncryption {
78 A256GCM,
80}
81
82impl JweEncryption {
83 pub fn as_str(&self) -> &'static str {
85 match self {
86 JweEncryption::A256GCM => "A256GCM",
87 }
88 }
89}
90
91#[async_trait]
95pub trait SigningKey: AgentKey {
96 async fn sign(&self, data: &[u8]) -> Result<Vec<u8>>;
98
99 fn recommended_jws_alg(&self) -> JwsAlgorithm;
101
102 async fn create_jws(
104 &self,
105 payload: &[u8],
106 protected_header: Option<JwsProtected>,
107 ) -> Result<crate::message::Jws>;
108}
109
110#[async_trait]
115pub trait VerificationKey: Send + Sync + Debug {
116 fn key_id(&self) -> &str;
118
119 fn public_key_jwk(&self) -> Result<Value>;
121
122 async fn verify_signature(
124 &self,
125 payload: &[u8],
126 signature: &[u8],
127 protected_header: &JwsProtected,
128 ) -> Result<bool>;
129}
130
131#[async_trait]
136pub trait EncryptionKey: AgentKey {
137 async fn encrypt(
139 &self,
140 plaintext: &[u8],
141 aad: Option<&[u8]>,
142 recipient_public_key: &dyn VerificationKey,
143 ) -> Result<(Vec<u8>, Vec<u8>, Vec<u8>)>; fn recommended_jwe_alg_enc(&self) -> (JweAlgorithm, JweEncryption);
147
148 async fn create_jwe(
150 &self,
151 plaintext: &[u8],
152 recipients: &[Arc<dyn VerificationKey>],
153 protected_header: Option<JweProtected>,
154 ) -> Result<Jwe>;
155}
156
157#[async_trait]
162pub trait DecryptionKey: AgentKey {
163 async fn decrypt(
165 &self,
166 ciphertext: &[u8],
167 encrypted_key: &[u8],
168 iv: &[u8],
169 tag: &[u8],
170 aad: Option<&[u8]>,
171 sender_key: Option<&dyn VerificationKey>,
172 ) -> Result<Vec<u8>>;
173
174 async fn unwrap_jwe(&self, jwe: &Jwe) -> Result<Vec<u8>>;
176}
177
178#[derive(Debug, thiserror::Error)]
180pub enum AgentKeyError {
181 #[error("Key operation failed: {0}")]
182 Operation(String),
183
184 #[error("Invalid key: {0}")]
185 InvalidKey(String),
186
187 #[error("Unsupported algorithm: {0}")]
188 UnsupportedAlgorithm(String),
189
190 #[error("Verification failed")]
191 VerificationFailed,
192
193 #[error("Decryption failed: {0}")]
194 DecryptionFailed(String),
195
196 #[error("Serialization error: {0}")]
197 Serialization(String),
198
199 #[error("Invalid format: {0}")]
200 InvalidFormat(String),
201}
202
203impl From<AgentKeyError> for Error {
204 fn from(err: AgentKeyError) -> Self {
205 Error::Cryptography(err.to_string())
206 }
207}