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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use crate::*;
use ghost_actor::dependencies::futures::future::FutureExt;
use holo_hash::{HOLO_HASH_CORE_LEN, HOLO_HASH_PREFIX_LEN};
use holochain_zome_types::signature::{Sign, Signature};
use holochain_zome_types::x_salsa20_poly1305::{
X25519XSalsa20Poly1305Decrypt, X25519XSalsa20Poly1305Encrypt,
};
use lair_keystore_api::actor::{
Cert, CertDigest, CertPrivKey, LairClientApiSender, LairEntryType, TlsCertOptions,
};
pub type KeystoreSender = ghost_actor::GhostSender<lair_keystore_api::actor::LairClientApi>;
pub type KeystoreApiResult<T> = Result<T, KeystoreError>;
pub type KeystoreApiFuture<T> =
ghost_actor::dependencies::must_future::MustBoxFuture<'static, KeystoreApiResult<T>>;
pub trait KeystoreSenderExt {
fn generate_sign_keypair_from_pure_entropy(&self) -> KeystoreApiFuture<holo_hash::AgentPubKey>;
fn sign(&self, input: Sign) -> KeystoreApiFuture<Signature>;
fn get_first_tls_cert(&self) -> KeystoreApiFuture<(CertDigest, Cert, CertPrivKey)>;
fn get_or_create_first_tls_cert(&self) -> KeystoreApiFuture<(CertDigest, Cert, CertPrivKey)>;
fn create_x25519_keypair(
&self,
) -> KeystoreApiFuture<holochain_zome_types::x_salsa20_poly1305::x25519::X25519PubKey>;
fn x_25519_x_salsa20_poly1305_encrypt(
&self,
input: X25519XSalsa20Poly1305Encrypt,
) -> KeystoreApiFuture<
holochain_zome_types::x_salsa20_poly1305::encrypted_data::XSalsa20Poly1305EncryptedData,
>;
fn x_25519_x_salsa20_poly1305_decrypt(
&self,
input: X25519XSalsa20Poly1305Decrypt,
) -> KeystoreApiFuture<
Option<holochain_zome_types::x_salsa20_poly1305::data::XSalsa20Poly1305Data>,
>;
}
impl KeystoreSenderExt for KeystoreSender {
fn generate_sign_keypair_from_pure_entropy(&self) -> KeystoreApiFuture<holo_hash::AgentPubKey> {
let fut = self.sign_ed25519_new_from_entropy();
async move {
let (_, pk) = fut.await?;
Ok(holo_hash::AgentPubKey::from_raw_32(pk.to_vec()))
}
.boxed()
.into()
}
fn sign(&self, input: Sign) -> KeystoreApiFuture<Signature> {
let fut = self.sign_ed25519_sign_by_pub_key(
input.key.as_ref()[HOLO_HASH_PREFIX_LEN..HOLO_HASH_PREFIX_LEN + HOLO_HASH_CORE_LEN]
.to_vec()
.into(),
<Vec<u8>>::from(UnsafeBytes::from(input.data.to_vec())).into(),
);
async move {
tracing::trace!("signing request start");
let res = fut.await?;
tracing::trace!("signing request end");
Ok(Signature::try_from(res.to_vec().as_ref())?)
}
.boxed()
.into()
}
fn get_first_tls_cert(&self) -> KeystoreApiFuture<(CertDigest, Cert, CertPrivKey)> {
let this = self.clone();
async move {
let last_index = this.lair_get_last_entry_index().await?;
for i in 1..=*last_index {
if let Ok(LairEntryType::TlsCert) = this.lair_get_entry_type(i.into()).await {
let (_, digest) = this.tls_cert_get(i.into()).await?;
let cert = this.tls_cert_get_cert_by_index(i.into()).await?;
let cert_priv = this.tls_cert_get_priv_key_by_index(i.into()).await?;
return Ok((digest, cert, cert_priv));
}
}
Err("no tls cert registered".into())
}
.boxed()
.into()
}
fn get_or_create_first_tls_cert(&self) -> KeystoreApiFuture<(CertDigest, Cert, CertPrivKey)> {
let this = self.clone();
async move {
if let Ok(r) = this.get_first_tls_cert().await {
return Ok(r);
}
let mut tls_opt = TlsCertOptions::default();
tls_opt.alg = lair_keystore_api::actor::TlsCertAlg::PkcsEcdsaP256Sha256;
let _ = this.tls_cert_new_self_signed_from_entropy(tls_opt).await?;
this.get_first_tls_cert().await
}
.boxed()
.into()
}
fn create_x25519_keypair(
&self,
) -> KeystoreApiFuture<holochain_zome_types::x_salsa20_poly1305::x25519::X25519PubKey> {
let this = self.clone();
async move {
let fut = this.x25519_new_from_entropy();
let (_, pubkey) = fut.await?;
Ok(AsRef::<[u8]>::as_ref(&pubkey).try_into()?)
}
.boxed()
.into()
}
fn x_25519_x_salsa20_poly1305_encrypt(
&self,
input: X25519XSalsa20Poly1305Encrypt,
) -> KeystoreApiFuture<
holochain_zome_types::x_salsa20_poly1305::encrypted_data::XSalsa20Poly1305EncryptedData,
> {
let this = self.clone();
async move {
let fut = this.crypto_box_by_pub_key(
input.as_sender_ref().as_ref()
.try_into()?,
input.as_recipient_ref().as_ref()
.try_into()?,
std::sync::Arc::new(lair_keystore_api::internal::crypto_box::CryptoBoxData {
data: std::sync::Arc::new(input.as_data_ref().as_ref().to_owned())
})
);
let res = fut.await?;
Ok(holochain_zome_types::x_salsa20_poly1305::encrypted_data::XSalsa20Poly1305EncryptedData::new(
AsRef::<[u8]>::as_ref(&res.nonce).try_into()?,
res.encrypted_data.to_vec(),
))
}
.boxed()
.into()
}
fn x_25519_x_salsa20_poly1305_decrypt(
&self,
input: X25519XSalsa20Poly1305Decrypt,
) -> KeystoreApiFuture<
Option<holochain_zome_types::x_salsa20_poly1305::data::XSalsa20Poly1305Data>,
> {
let this = self.clone();
async move {
let fut = this.crypto_box_open_by_pub_key(
input.as_recipient_ref().as_ref().try_into()?,
input.as_sender_ref().as_ref().try_into()?,
std::sync::Arc::new(
lair_keystore_api::internal::crypto_box::CryptoBoxEncryptedData {
nonce: AsRef::<[u8]>::as_ref(&input.as_encrypted_data_ref().as_nonce_ref())
.try_into()?,
encrypted_data: std::sync::Arc::new(
input
.as_encrypted_data_ref()
.as_encrypted_data_ref()
.to_vec(),
),
},
),
);
let res = fut.await?;
Ok(res.map(|data| {
holochain_zome_types::x_salsa20_poly1305::data::XSalsa20Poly1305Data::from(
data.data.to_vec(),
)
}))
}
.boxed()
.into()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_keystore::*;
#[tokio::test(flavor = "multi_thread")]
async fn test_tls_cert_get_or_create() {
let keystore = spawn_test_keystore().await.unwrap();
let (dig1, cert1, priv1) = keystore.get_or_create_first_tls_cert().await.unwrap();
let (dig2, cert2, priv2) = keystore.get_or_create_first_tls_cert().await.unwrap();
assert_eq!(dig1, dig2);
assert_eq!(cert1, cert2);
assert_eq!(priv1, priv2);
}
}