1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use std::borrow::Cow;
use std::fmt::Debug;
use crate::jwe::{JweContentEncryption, JweHeader};
use crate::JoseError;
/// Represent a algorithm of JWE alg header claim.
pub trait JweAlgorithm: Debug + Send + Sync {
/// Return the "alg" (algorithm) header parameter value of JWE.
fn name(&self) -> &str;
fn box_clone(&self) -> Box<dyn JweAlgorithm>;
}
impl PartialEq for Box<dyn JweAlgorithm> {
fn eq(&self, other: &Self) -> bool {
self == other
}
}
impl Eq for Box<dyn JweAlgorithm> {}
impl Clone for Box<dyn JweAlgorithm> {
fn clone(&self) -> Self {
self.box_clone()
}
}
pub trait JweEncrypter: Debug + Send + Sync {
/// Return the source algorithm instance.
fn algorithm(&self) -> &dyn JweAlgorithm;
/// Return the source key ID.
/// The default value is a value of kid parameter in JWK.
fn key_id(&self) -> Option<&str>;
/// Compute a content encryption key.
///
/// # Arguments
///
/// * `cencryption` - The content encryption method.
/// * `in_header` - the input header
/// * `out_header` - the output header
fn compute_content_encryption_key(
&self,
cencryption: &dyn JweContentEncryption,
in_header: &JweHeader,
out_header: &mut JweHeader,
) -> Result<Option<Cow<[u8]>>, JoseError>;
/// Return a encypted key.
///
/// # Arguments
///
/// * `key` - The content encryption key
/// * `in_header` - the input header
/// * `out_header` - the output header
fn encrypt(
&self,
key: &[u8],
in_header: &JweHeader,
out_header: &mut JweHeader,
) -> Result<Option<Vec<u8>>, JoseError>;
fn box_clone(&self) -> Box<dyn JweEncrypter>;
}
impl Clone for Box<dyn JweEncrypter> {
fn clone(&self) -> Self {
self.box_clone()
}
}
pub trait JweDecrypter: Debug + Send + Sync {
/// Return the source algorithm instance.
fn algorithm(&self) -> &dyn JweAlgorithm;
/// Return the source key ID.
/// The default value is a value of kid parameter in JWK.
fn key_id(&self) -> Option<&str>;
/// Return a decrypted key.
///
/// # Arguments
///
/// * `encrypted_key` - The encrypted key.
/// * `cencryption` - The content encryption method.
/// * `header` - The header
fn decrypt(
&self,
encrypted_key: Option<&[u8]>,
cencryption: &dyn JweContentEncryption,
header: &JweHeader,
) -> Result<Cow<[u8]>, JoseError>;
fn box_clone(&self) -> Box<dyn JweDecrypter>;
}
impl Clone for Box<dyn JweDecrypter> {
fn clone(&self) -> Self {
self.box_clone()
}
}