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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
use crate::secrets_store_capnp::{self, secret_entry, secret_version_ref};
use capnp::text_list;
use chrono::{TimeZone, Utc};
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::fmt;
use zeroize::Zeroize;
mod command;
mod config;
mod event;
mod zeroize_datetime;
#[cfg(test)]
mod tests;
pub use command::*;
pub use config::*;
pub use event::*;
pub use zeroize_datetime::*;
pub const PROPERTY_USERNAME: &str = "username";
pub const PROPERTY_PASSWORD: &str = "password";
pub const PROPERTY_TOTP_URL: &str = "totpUrl";
pub const PROPERTY_NOTES: &str = "notes";
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Zeroize)]
#[zeroize(drop)]
pub struct Status {
pub locked: bool,
pub unlocked_by: Option<Identity>,
pub autolock_at: Option<ZeroizeDateTime>,
pub version: String,
pub autolock_timeout: u64,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Zeroize)]
#[zeroize(drop)]
pub struct Identity {
pub id: String,
pub name: String,
pub email: String,
pub hidden: bool,
}
impl std::fmt::Display for Identity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} <{}>", self.name, self.email)
}
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum SecretType {
Login,
Note,
Licence,
Wlan,
Password,
#[serde(other)]
Other,
}
impl Zeroize for SecretType {
fn zeroize(&mut self) {
*self = SecretType::Other
}
}
impl SecretType {
pub fn password_properties(&self) -> &[&str] {
match self {
SecretType::Login => &[PROPERTY_PASSWORD],
SecretType::Note => &[],
SecretType::Licence => &[],
SecretType::Wlan => &[PROPERTY_PASSWORD],
SecretType::Password => &[PROPERTY_PASSWORD],
SecretType::Other => &[],
}
}
pub fn from_reader(api: secrets_store_capnp::SecretType) -> Self {
match api {
secrets_store_capnp::SecretType::Login => SecretType::Login,
secrets_store_capnp::SecretType::Licence => SecretType::Licence,
secrets_store_capnp::SecretType::Wlan => SecretType::Wlan,
secrets_store_capnp::SecretType::Note => SecretType::Note,
secrets_store_capnp::SecretType::Password => SecretType::Password,
secrets_store_capnp::SecretType::Other => SecretType::Other,
}
}
pub fn to_builder(self) -> secrets_store_capnp::SecretType {
match self {
SecretType::Login => secrets_store_capnp::SecretType::Login,
SecretType::Licence => secrets_store_capnp::SecretType::Licence,
SecretType::Note => secrets_store_capnp::SecretType::Note,
SecretType::Wlan => secrets_store_capnp::SecretType::Wlan,
SecretType::Password => secrets_store_capnp::SecretType::Password,
SecretType::Other => secrets_store_capnp::SecretType::Other,
}
}
}
impl fmt::Display for SecretType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
SecretType::Login => write!(f, "Login"),
SecretType::Note => write!(f, "Note"),
SecretType::Licence => write!(f, "Licence"),
SecretType::Wlan => write!(f, "WLAN"),
SecretType::Password => write!(f, "Password"),
SecretType::Other => write!(f, "Other"),
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize, Default, PartialEq, Eq, Zeroize)]
#[zeroize(drop)]
pub struct SecretListFilter {
pub url: Option<String>,
pub tag: Option<String>,
#[serde(rename = "type")]
pub secret_type: Option<SecretType>,
pub name: Option<String>,
#[serde(default)]
pub deleted: bool,
}
#[derive(Clone, Debug, Serialize, Deserialize, Eq, Zeroize)]
#[zeroize(drop)]
pub struct SecretEntry {
pub id: String,
pub name: String,
#[serde(rename = "type")]
pub secret_type: SecretType,
pub tags: Vec<String>,
pub urls: Vec<String>,
pub timestamp: ZeroizeDateTime,
pub deleted: bool,
}
impl SecretEntry {
pub fn from_reader(reader: secret_entry::Reader) -> capnp::Result<Self> {
Ok(SecretEntry {
id: reader.get_id()?.to_string(),
timestamp: Utc.timestamp_millis(reader.get_timestamp()).into(),
name: reader.get_name()?.to_string(),
secret_type: SecretType::from_reader(reader.get_type()?),
tags: reader
.get_tags()?
.into_iter()
.map(|t| t.map(|t| t.to_string()))
.collect::<capnp::Result<Vec<String>>>()?,
urls: reader
.get_urls()?
.into_iter()
.map(|u| u.map(|u| u.to_string()))
.collect::<capnp::Result<Vec<String>>>()?,
deleted: reader.get_deleted(),
})
}
pub fn to_builder(&self, mut builder: secret_entry::Builder) {
builder.set_id(&self.id);
builder.set_timestamp(self.timestamp.timestamp_millis());
builder.set_name(&self.name);
builder.set_type(self.secret_type.to_builder());
let mut tags = builder.reborrow().init_tags(self.tags.len() as u32);
for (idx, tag) in self.tags.iter().enumerate() {
tags.set(idx as u32, tag)
}
let mut urls = builder.reborrow().init_urls(self.urls.len() as u32);
for (idx, url) in self.urls.iter().enumerate() {
urls.set(idx as u32, url)
}
builder.set_deleted(self.deleted);
}
}
impl Ord for SecretEntry {
fn cmp(&self, other: &Self) -> Ordering {
match self.name.cmp(&other.name) {
Ordering::Equal => self.id.cmp(&other.id),
ord => ord,
}
}
}
impl PartialOrd for SecretEntry {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl PartialEq for SecretEntry {
fn eq(&self, other: &Self) -> bool {
self.id.eq(&other.id)
}
}
#[derive(Clone, Debug, Serialize, Deserialize, Eq, Zeroize)]
#[zeroize(drop)]
pub struct SecretEntryMatch {
pub entry: SecretEntry,
pub name_score: isize,
pub name_highlights: Vec<usize>,
pub url_highlights: Vec<usize>,
pub tags_highlights: Vec<usize>,
}
impl Ord for SecretEntryMatch {
fn cmp(&self, other: &Self) -> Ordering {
match other.name_score.cmp(&self.name_score) {
Ordering::Equal => self.entry.cmp(&other.entry),
ord => ord,
}
}
}
impl PartialOrd for SecretEntryMatch {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl PartialEq for SecretEntryMatch {
fn eq(&self, other: &Self) -> bool {
self.entry.eq(&other.entry)
}
}
#[derive(Clone, Debug, Serialize, Deserialize, Default, PartialEq, Eq, Zeroize)]
#[zeroize(drop)]
pub struct SecretList {
pub all_tags: Vec<String>,
pub entries: Vec<SecretEntryMatch>,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
#[serde(transparent)]
pub struct SecretProperties(BTreeMap<String, String>);
impl SecretProperties {
pub fn new(properties: BTreeMap<String, String>) -> Self {
SecretProperties(properties)
}
pub fn get(&self, name: &str) -> Option<&String> {
self.0.get(name)
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn iter(&self) -> impl Iterator<Item = (&str, &str)> {
self.0.iter().map(|(k, v)| (k.as_str(), v.as_str()))
}
}
impl Drop for SecretProperties {
fn drop(&mut self) {
self.zeroize()
}
}
impl Zeroize for SecretProperties {
fn zeroize(&mut self) {
self.0.values_mut().for_each(Zeroize::zeroize);
}
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Zeroize)]
#[zeroize(drop)]
pub struct SecretAttachment {
name: String,
mime_type: String,
content: Vec<u8>,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Zeroize)]
#[zeroize(drop)]
pub struct SecretVersion {
pub secret_id: String,
#[serde(rename = "type")]
pub secret_type: SecretType,
pub timestamp: ZeroizeDateTime,
pub name: String,
#[serde(default)]
pub tags: Vec<String>,
#[serde(default)]
pub urls: Vec<String>,
pub properties: SecretProperties,
#[serde(default)]
pub attachments: Vec<SecretAttachment>,
#[serde(default)]
pub deleted: bool,
#[serde(default)]
pub recipients: Vec<String>,
}
impl SecretVersion {
pub fn to_entry_builder(&self, mut builder: secret_entry::Builder) -> capnp::Result<()> {
builder.set_id(&self.secret_id);
builder.set_timestamp(self.timestamp.timestamp_millis());
builder.set_name(&self.name);
builder.set_type(self.secret_type.to_builder());
set_text_list(builder.reborrow().init_tags(self.tags.len() as u32), &self.tags)?;
set_text_list(builder.reborrow().init_urls(self.urls.len() as u32), &self.urls)?;
builder.set_deleted(self.deleted);
Ok(())
}
}
#[derive(Clone, Debug, Serialize, Deserialize, Zeroize)]
#[zeroize(drop)]
pub struct PasswordEstimate {
pub password: String,
pub inputs: Vec<String>,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Zeroize)]
#[zeroize(drop)]
pub struct PasswordStrength {
pub entropy: f64,
pub crack_time: f64,
pub crack_time_display: String,
pub score: u8,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Zeroize)]
#[zeroize(drop)]
pub struct SecretVersionRef {
pub block_id: String,
pub timestamp: ZeroizeDateTime,
}
impl SecretVersionRef {
pub fn from_reader(reader: secret_version_ref::Reader) -> capnp::Result<Self> {
Ok(SecretVersionRef {
block_id: reader.get_block_id()?.to_string(),
timestamp: Utc.timestamp_millis(reader.get_timestamp()).into(),
})
}
pub fn to_builder(&self, mut builder: secret_version_ref::Builder) {
builder.set_block_id(&self.block_id);
builder.set_timestamp(self.timestamp.timestamp_millis());
}
}
impl std::fmt::Display for SecretVersionRef {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.timestamp.format("%Y-%m-%d %H:%M:%S"))
}
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct Secret {
pub id: String,
#[serde(rename = "type")]
pub secret_type: SecretType,
pub current: SecretVersion,
pub current_block_id: String,
pub versions: Vec<SecretVersionRef>,
pub password_strengths: HashMap<String, PasswordStrength>,
}
impl Zeroize for Secret {
fn zeroize(&mut self) {
self.id.zeroize();
self.secret_type.zeroize();
self.current.zeroize();
self.current_block_id.zeroize();
self.versions.zeroize();
self.password_strengths.values_mut().for_each(Zeroize::zeroize);
}
}
impl Drop for Secret {
fn drop(&mut self) {
self.zeroize();
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Zeroize)]
#[zeroize(drop)]
pub struct PasswordGeneratorCharsParam {
pub num_chars: u8,
pub include_uppers: bool,
pub include_numbers: bool,
pub include_symbols: bool,
pub require_upper: bool,
pub require_number: bool,
pub require_symbol: bool,
pub exlcude_similar: bool,
pub exclude_ambiguous: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Zeroize)]
#[zeroize(drop)]
pub struct PasswordGeneratorWordsParam {
pub num_words: u8,
pub delim: char,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Zeroize)]
#[serde(rename_all = "lowercase")]
#[zeroize(drop)]
pub enum PasswordGeneratorParam {
Chars(PasswordGeneratorCharsParam),
Words(PasswordGeneratorWordsParam),
}
pub fn set_text_list<I, S>(mut text_list: text_list::Builder, texts: I) -> capnp::Result<()>
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
for (idx, text) in texts.into_iter().enumerate() {
text_list.set(idx as u32, capnp::text::new_reader(text.as_ref().as_bytes())?);
}
Ok(())
}