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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
use crate::{utils::wrap_err, TinkError};
use prost::Message;
use std::sync::Arc;
use tink_proto::{key_data::KeyMaterialType, Keyset, KeysetInfo};
pub struct Handle {
ks: Keyset,
}
impl Handle {
pub fn new(kt: &tink_proto::KeyTemplate) -> Result<Self, TinkError> {
let mut ksm = super::Manager::new();
ksm.rotate(kt)
.map_err(|e| wrap_err("keyset::Handle: cannot generate new keyset", e))?;
ksm.handle()
.map_err(|e| wrap_err("keyset::Handle: cannot get keyset handle", e))
}
pub fn new_with_no_secrets(ks: Keyset) -> Result<Self, TinkError> {
let h = Handle {
ks: validate_keyset(ks)?,
};
if h.has_secrets()? {
return Err("importing unencrypted secret key material is forbidden".into());
}
Ok(h)
}
pub fn read<T>(reader: &mut T, master_key: Box<dyn crate::Aead>) -> Result<Self, TinkError>
where
T: crate::keyset::Reader,
{
let encrypted_keyset = reader.read_encrypted()?;
let ks = decrypt(&encrypted_keyset, master_key)?;
Ok(Handle {
ks: validate_keyset(ks)?,
})
}
pub fn read_with_no_secrets<T>(reader: &mut T) -> Result<Self, TinkError>
where
T: crate::keyset::Reader,
{
let ks = reader.read()?;
Handle::new_with_no_secrets(ks)
}
pub fn public(&self) -> Result<Self, TinkError> {
let priv_keys = &self.ks.key;
let mut pub_keys = Vec::with_capacity(priv_keys.len());
for priv_key in priv_keys {
let priv_key_data = priv_key
.key_data
.as_ref()
.ok_or_else(|| TinkError::new("keyset::Handle: invalid keyset"))?;
let pub_key_data =
public_key_data(priv_key_data).map_err(|e| wrap_err("keyset::Handle", e))?;
pub_keys.push(tink_proto::keyset::Key {
key_data: Some(pub_key_data),
status: priv_key.status,
key_id: priv_key.key_id,
output_prefix_type: priv_key.output_prefix_type,
});
}
let ks = Keyset {
primary_key_id: self.ks.primary_key_id,
key: pub_keys,
};
Ok(Handle { ks })
}
pub fn write<T>(
&self,
writer: &mut T,
master_key: Box<dyn crate::Aead>,
) -> Result<(), TinkError>
where
T: super::Writer,
{
let encrypted = encrypt(&self.ks, master_key)?;
writer.write_encrypted(&encrypted)
}
pub fn write_with_no_secrets<T>(&self, w: &mut T) -> Result<(), TinkError>
where
T: super::Writer,
{
if self.has_secrets()? {
Err("exporting unencrypted secret key material is forbidden".into())
} else {
w.write(&self.ks)
}
}
pub fn primitives(&self) -> Result<crate::primitiveset::PrimitiveSet, TinkError> {
self.primitives_with_key_manager(None)
}
pub fn primitives_with_key_manager(
&self,
km: Option<Arc<dyn crate::registry::KeyManager>>,
) -> Result<crate::primitiveset::PrimitiveSet, TinkError> {
super::validate(&self.ks)
.map_err(|e| wrap_err("primitives_with_key_manager: invalid keyset", e))?;
let mut primitive_set = crate::primitiveset::PrimitiveSet::new();
for key in &self.ks.key {
if key.status != tink_proto::KeyStatusType::Enabled as i32 {
continue;
}
let key_data = key
.key_data
.as_ref()
.ok_or_else(|| TinkError::new("primitives_with_key_manager: no key_data"))?;
let primitive = match &km {
Some(km) if km.does_support(&key_data.type_url) => km.primitive(&key_data.value),
Some(_) | None => crate::registry::primitive_from_key_data(&key_data),
}
.map_err(|e| {
wrap_err(
"primitives_with_key_manager: cannot get primitive from key",
e,
)
})?;
let entry = primitive_set
.add(primitive, key)
.map_err(|e| wrap_err("primitives_with_key_manager: cannot add primitive", e))?;
if key.key_id == self.ks.primary_key_id {
primitive_set.primary = Some(entry.clone());
}
}
Ok(primitive_set)
}
fn has_secrets(&self) -> Result<bool, TinkError> {
let mut result = false;
for k in &self.ks.key {
match &k.key_data {
None => return Err("invalid keyset".into()),
Some(kd) => match KeyMaterialType::from_i32(kd.key_material_type) {
Some(KeyMaterialType::UnknownKeymaterial) => result = true,
Some(KeyMaterialType::Symmetric) => result = true,
Some(KeyMaterialType::AsymmetricPrivate) => result = true,
Some(KeyMaterialType::AsymmetricPublic) => {}
Some(KeyMaterialType::Remote) => {}
None => return Err("invalid key material type".into()),
},
}
}
Ok(result)
}
pub fn keyset_info(&self) -> KeysetInfo {
get_keyset_info(&self.ks)
}
pub(crate) fn into_inner(self) -> Keyset {
self.ks
}
#[cfg(feature = "insecure")]
pub(crate) fn clone_keyset(&self) -> Keyset {
self.ks.clone()
}
pub(crate) fn from_keyset(ks: Keyset) -> Result<Self, TinkError> {
Ok(Handle {
ks: validate_keyset(ks)?,
})
}
}
fn validate_keyset(ks: Keyset) -> Result<Keyset, TinkError> {
for k in &ks.key {
match &k.key_data {
None if k.status == tink_proto::KeyStatusType::Destroyed as i32 => {}
None => return Err("invalid keyset".into()),
Some(kd) => match KeyMaterialType::from_i32(kd.key_material_type) {
Some(_) => {}
None => return Err("invalid key material type".into()),
},
}
}
Ok(ks)
}
fn public_key_data(priv_key_data: &tink_proto::KeyData) -> Result<tink_proto::KeyData, TinkError> {
if priv_key_data.key_material_type
!= tink_proto::key_data::KeyMaterialType::AsymmetricPrivate as i32
{
return Err("keyset::Handle: keyset contains a non-private key".into());
}
let km = crate::registry::get_key_manager(&priv_key_data.type_url)?;
if !km.supports_private_keys() {
return Err(format!(
"keyset::Handle: {} does not belong to a KeyManager that handles private keys",
priv_key_data.type_url
)
.into());
}
km.public_key_data(&priv_key_data.value)
}
fn decrypt(
encrypted_keyset: &tink_proto::EncryptedKeyset,
master_key: Box<dyn crate::Aead>,
) -> Result<Keyset, TinkError> {
let decrypted = master_key
.decrypt(&encrypted_keyset.encrypted_keyset, &[])
.map_err(|e| wrap_err("keyset::Handle: decryption failed", e))?;
Keyset::decode(&decrypted[..]).map_err(|_| TinkError::new("keyset::Handle:: invalid keyset"))
}
fn encrypt(
keyset: &Keyset,
master_key: Box<dyn crate::Aead>,
) -> Result<tink_proto::EncryptedKeyset, TinkError> {
let mut serialized_keyset = vec![];
keyset
.encode(&mut serialized_keyset)
.map_err(|e| wrap_err("keyset::Handle: invalid keyset", e))?;
let encrypted = master_key
.encrypt(&serialized_keyset, &[])
.map_err(|e| wrap_err("keyset::Handle: encrypted failed", e))?;
Ok(tink_proto::EncryptedKeyset {
encrypted_keyset: encrypted,
keyset_info: Some(get_keyset_info(keyset)),
})
}
fn get_keyset_info(keyset: &Keyset) -> KeysetInfo {
let n_key = keyset.key.len();
let mut key_infos = Vec::with_capacity(n_key);
for key in &keyset.key {
key_infos.push(get_key_info(key));
}
KeysetInfo {
primary_key_id: keyset.primary_key_id,
key_info: key_infos,
}
}
fn get_key_info(key: &tink_proto::keyset::Key) -> tink_proto::keyset_info::KeyInfo {
tink_proto::keyset_info::KeyInfo {
type_url: match &key.key_data {
Some(kd) => kd.type_url.clone(),
None => "".to_string(),
},
status: key.status,
key_id: key.key_id,
output_prefix_type: key.output_prefix_type,
}
}
impl std::fmt::Debug for Handle {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", get_keyset_info(&self.ks))
}
}