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
use crate::keys::{EncodedVerKey, PrivateKey};
use std::future::Future;
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct JWE {
pub protected: String,
pub iv: String,
pub ciphertext: String,
pub tag: String,
}
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct Recipient {
pub encrypted_key: String,
pub header: Header,
}
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct Header {
pub kid: String,
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub iv: Option<String>,
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub sender: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct Protected {
pub enc: String,
pub typ: String,
pub alg: String,
pub recipients: Vec<Recipient>,
}
pub trait KeyLookup<'f> {
fn find<'a>(
self,
key: &'a Vec<EncodedVerKey>,
) -> std::pin::Pin<Box<dyn Future<Output = Option<(usize, PrivateKey)>> + Send + 'a>>
where
'f: 'a;
}
type KeyLookupCb<'a> =
Box<dyn Fn(&Vec<EncodedVerKey>) -> Option<(usize, PrivateKey)> + Send + Sync + 'a>;
pub struct KeyLookupFn<'a> {
cb: KeyLookupCb<'a>,
}
pub fn key_lookup_fn<'a, F>(cb: F) -> KeyLookupFn<'a>
where
F: Fn(&Vec<EncodedVerKey>) -> Option<(usize, PrivateKey)> + Send + Sync + 'a,
{
KeyLookupFn {
cb: Box::new(cb) as KeyLookupCb,
}
}
impl<'a, 'l, 'r> KeyLookup<'l> for &'r KeyLookupFn<'a>
where
'a: 'l,
'r: 'a,
{
fn find<'f>(
self,
keys: &'f Vec<EncodedVerKey>,
) -> std::pin::Pin<Box<dyn Future<Output = Option<(usize, PrivateKey)>> + Send + 'f>>
where
'l: 'f,
{
Box::pin(async move { (&self.cb)(keys) })
}
}