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
//! Lair api serialization types.

use crate::*;
use std::sync::Arc;

/// Helper traits for core types - you probably don't need these unless
/// you are implementing new lair core instance logic.
pub mod api_traits {
    use super::*;

    /// Defines a lair serialization object.
    pub trait AsLairCodec:
        'static
        + std::fmt::Debug
        + serde::Serialize
        + for<'de> serde::Deserialize<'de>
        + std::convert::TryFrom<LairApiEnum>
    {
        /// Convert this individual lair serialization object
        /// into a combined API enum instance variant.
        fn into_api_enum(self) -> LairApiEnum;
    }

    /// A "Request" type lair codec instance.
    pub trait AsLairRequest: AsLairCodec {
        /// The "Response" type associated with this request type.
        type Response: AsLairCodec;
    }

    /// A "Response" type lair codec instance.
    pub trait AsLairResponse: AsLairCodec {
        /// The "Request" type associated with this response type.
        type Request: AsLairCodec;
    }
}
use api_traits::*;

/// Instructions for how to argon2id pwhash a passphrase
/// for use in deep locking a seed.
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DeepLockPassphrase {
    /// argon2id ops_limit for decrypting runtime data
    pub ops_limit: u32,
    /// argon2id mem_limit for decrypting runtime data
    pub mem_limit: u32,
    /// if this new seed is to be deep_locked, the passphrase for that.
    pub passphrase: DeepLockPassphraseBytes,
}

impl DeepLockPassphrase {
    /// Constructor
    pub fn new(
        passphrase: DeepLockPassphraseBytes,
        limits: PwHashLimits,
    ) -> Self {
        Self {
            passphrase,
            ops_limit: limits.as_ops_limit(),
            mem_limit: limits.as_mem_limit(),
        }
    }
}

/// The secret bytes of the passphrase
pub type DeepLockPassphraseBytes = SecretDataSized<64, 81>;

fn new_msg_id() -> Arc<str> {
    nanoid::nanoid!().into()
}

fn is_false(b: impl std::borrow::Borrow<bool>) -> bool {
    !b.borrow()
}

mod error;
pub use error::*;

mod hello;
pub use hello::*;

mod unlock;
pub use unlock::*;

mod get_entry;
pub use get_entry::*;

mod list_entries;
pub use list_entries::*;

mod new_seed;
pub use new_seed::*;

mod export_seed_by_tag;
pub use export_seed_by_tag::*;

mod derive_seed;
pub use derive_seed::*;

mod import_seed;
pub use import_seed::*;

mod sign_by_pub_key;
pub use sign_by_pub_key::*;

mod crypto_box_xsalsa_by_pub_key;
pub use crypto_box_xsalsa_by_pub_key::*;

mod crypto_box_xsalsa_open_by_pub_key;
pub use crypto_box_xsalsa_open_by_pub_key::*;

mod crypto_box_xsalsa_by_sign_pub_key;
pub use crypto_box_xsalsa_by_sign_pub_key::*;

mod crypto_box_xsalsa_open_by_sign_pub_key;
pub use crypto_box_xsalsa_open_by_sign_pub_key::*;

mod new_wka_tls_cert;
pub use new_wka_tls_cert::*;

mod get_wka_tls_cert_priv_key;
pub use get_wka_tls_cert_priv_key::*;

mod secret_box_xsalsa_by_tag;
pub use secret_box_xsalsa_by_tag::*;

mod secret_box_xsalsa_open_by_tag;
pub use secret_box_xsalsa_open_by_tag::*;

/// Lair api enum.
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(tag = "type", rename_all = "camelCase")]
#[non_exhaustive]
pub enum LairApiEnum {
    /// An error response from the remote instance.
    ResError(LairApiResError),

    /// Initiate communication with the target lair instance.
    ReqHello(LairApiReqHello),

    /// The hello response from the target lair instance.
    /// This data allows us to verify we are speaking to our expected target.
    ResHello(LairApiResHello),

    /// Unlock the keystore -- this verifies the client to the keystore.
    ReqUnlock(LairApiReqUnlock),

    /// Sucess / Failure of the unlock request.
    ResUnlock(LairApiResUnlock),

    /// Get entry_info for an entry by tag from lair.
    ReqGetEntry(LairApiReqGetEntry),

    /// Response to a GetEntry request.
    ResGetEntry(LairApiResGetEntry),

    /// Request a list of entries from lair.
    ReqListEntries(LairApiReqListEntries),

    /// Respond to a list entries request.
    ResListEntries(LairApiResListEntries),

    /// Instruct lair to generate a new seed from cryptographically secure
    /// random data with given tag.
    ReqNewSeed(LairApiReqNewSeed),

    /// On new seed generation, lair will respond with info about
    /// that seed.
    ResNewSeed(LairApiResNewSeed),

    /// Export a seed (if it is marked exportable) for a specific
    /// target using the crypto box xsalsa20poly1305 algorithm.
    ReqExportSeedByTag(LairApiReqExportSeedByTag),

    /// Response for export seed by tag.
    ResExportSeedByTag(LairApiResExportSeedByTag),

    /// Import a seed encrypted as a xsalsa20poly1305 secretbox.
    ReqImportSeed(LairApiReqImportSeed),

    /// Response for import seed.
    ResImportSeed(LairApiResImportSeed),

    /// Derive a new seed from an existing one according to a specified derivation path.
    ReqDeriveSeed(LairApiReqDeriveSeed),

    /// Response for drive seed.
    ResDeriveSeed(LairApiResDeriveSeed),

    /// Request a signature.
    ReqSignByPubKey(LairApiReqSignByPubKey),

    /// A signature response.
    ResSignByPubKey(LairApiResSignByPubKey),

    /// Request "crypto_box" encryption.
    ReqCryptoBoxXSalsaByPubKey(LairApiReqCryptoBoxXSalsaByPubKey),

    /// A "crypto_box" encryption response.
    ResCryptoBoxXSalsaByPubKey(LairApiResCryptoBoxXSalsaByPubKey),

    /// Request "crypto_box_open" decryption.
    ReqCryptoBoxXSalsaOpenByPubKey(LairApiReqCryptoBoxXSalsaOpenByPubKey),

    /// A "crypto_box_open" decryption response.
    ResCryptoBoxXSalsaOpenByPubKey(LairApiResCryptoBoxXSalsaOpenByPubKey),

    /// Request "crypto_box" encryption.
    ReqCryptoBoxXSalsaBySignPubKey(LairApiReqCryptoBoxXSalsaBySignPubKey),

    /// A "crypto_box" encryption response.
    ResCryptoBoxXSalsaBySignPubKey(LairApiResCryptoBoxXSalsaBySignPubKey),

    /// Request "crypto_box_open" decryption.
    ReqCryptoBoxXSalsaOpenBySignPubKey(
        LairApiReqCryptoBoxXSalsaOpenBySignPubKey,
    ),

    /// A "crypto_box_open" decryption response.
    ResCryptoBoxXSalsaOpenBySignPubKey(
        LairApiResCryptoBoxXSalsaOpenBySignPubKey,
    ),

    /// Instruct lair to generate a new wka tls certificate
    /// from cryptographically secure random data with given tag.
    ReqNewWkaTlsCert(LairApiReqNewWkaTlsCert),

    /// On new cert generation, lair will respond with info about
    /// that cert.
    ResNewWkaTlsCert(LairApiResNewWkaTlsCert),

    /// Request the private key associated with a tagged wka tls cert.
    ReqGetWkaTlsCertPrivKey(LairApiReqGetWkaTlsCertPrivKey),

    /// Returns the private key associated with a tagged wka tls cert.
    ResGetWkaTlsCertPrivKey(LairApiResGetWkaTlsCertPrivKey),

    /// Request "secretbox" encryption.
    ReqSecretBoxXSalsaByTag(LairApiReqSecretBoxXSalsaByTag),

    /// A "secretbox" encryption response.
    ResSecretBoxXSalsaByTag(LairApiResSecretBoxXSalsaByTag),

    /// Request "secretbox" decryption.
    ReqSecretBoxXSalsaOpenByTag(LairApiReqSecretBoxXSalsaOpenByTag),

    /// A "secretbox" decryption response.
    ResSecretBoxXSalsaOpenByTag(LairApiResSecretBoxXSalsaOpenByTag),
}

impl LairApiEnum {
    /// Get the msg_id associated with this msg variant.
    pub fn msg_id(&self) -> Arc<str> {
        match self {
            Self::ResError(LairApiResError { msg_id, .. }) => msg_id.clone(),
            Self::ReqHello(LairApiReqHello { msg_id, .. }) => msg_id.clone(),
            Self::ResHello(LairApiResHello { msg_id, .. }) => msg_id.clone(),
            Self::ReqUnlock(LairApiReqUnlock { msg_id, .. }) => msg_id.clone(),
            Self::ResUnlock(LairApiResUnlock { msg_id, .. }) => msg_id.clone(),
            Self::ReqGetEntry(LairApiReqGetEntry { msg_id, .. }) => {
                msg_id.clone()
            }
            Self::ResGetEntry(LairApiResGetEntry { msg_id, .. }) => {
                msg_id.clone()
            }
            Self::ReqListEntries(LairApiReqListEntries { msg_id, .. }) => {
                msg_id.clone()
            }
            Self::ResListEntries(LairApiResListEntries { msg_id, .. }) => {
                msg_id.clone()
            }
            Self::ReqNewSeed(LairApiReqNewSeed { msg_id, .. }) => {
                msg_id.clone()
            }
            Self::ResNewSeed(LairApiResNewSeed { msg_id, .. }) => {
                msg_id.clone()
            }
            Self::ReqExportSeedByTag(LairApiReqExportSeedByTag {
                msg_id,
                ..
            }) => msg_id.clone(),
            Self::ResExportSeedByTag(LairApiResExportSeedByTag {
                msg_id,
                ..
            }) => msg_id.clone(),
            Self::ReqImportSeed(LairApiReqImportSeed { msg_id, .. }) => {
                msg_id.clone()
            }
            Self::ResImportSeed(LairApiResImportSeed { msg_id, .. }) => {
                msg_id.clone()
            }
            Self::ReqDeriveSeed(LairApiReqDeriveSeed { msg_id, .. }) => {
                msg_id.clone()
            }
            Self::ResDeriveSeed(LairApiResDeriveSeed { msg_id, .. }) => {
                msg_id.clone()
            }
            Self::ReqSignByPubKey(LairApiReqSignByPubKey {
                msg_id, ..
            }) => msg_id.clone(),
            Self::ResSignByPubKey(LairApiResSignByPubKey {
                msg_id, ..
            }) => msg_id.clone(),
            Self::ReqCryptoBoxXSalsaByPubKey(
                LairApiReqCryptoBoxXSalsaByPubKey { msg_id, .. },
            ) => msg_id.clone(),
            Self::ResCryptoBoxXSalsaByPubKey(
                LairApiResCryptoBoxXSalsaByPubKey { msg_id, .. },
            ) => msg_id.clone(),
            Self::ReqCryptoBoxXSalsaOpenByPubKey(
                LairApiReqCryptoBoxXSalsaOpenByPubKey { msg_id, .. },
            ) => msg_id.clone(),
            Self::ResCryptoBoxXSalsaOpenByPubKey(
                LairApiResCryptoBoxXSalsaOpenByPubKey { msg_id, .. },
            ) => msg_id.clone(),
            Self::ReqCryptoBoxXSalsaBySignPubKey(
                LairApiReqCryptoBoxXSalsaBySignPubKey { msg_id, .. },
            ) => msg_id.clone(),
            Self::ResCryptoBoxXSalsaBySignPubKey(
                LairApiResCryptoBoxXSalsaBySignPubKey { msg_id, .. },
            ) => msg_id.clone(),
            Self::ReqCryptoBoxXSalsaOpenBySignPubKey(
                LairApiReqCryptoBoxXSalsaOpenBySignPubKey { msg_id, .. },
            ) => msg_id.clone(),
            Self::ResCryptoBoxXSalsaOpenBySignPubKey(
                LairApiResCryptoBoxXSalsaOpenBySignPubKey { msg_id, .. },
            ) => msg_id.clone(),
            Self::ReqNewWkaTlsCert(LairApiReqNewWkaTlsCert {
                msg_id, ..
            }) => msg_id.clone(),
            Self::ResNewWkaTlsCert(LairApiResNewWkaTlsCert {
                msg_id, ..
            }) => msg_id.clone(),
            Self::ReqGetWkaTlsCertPrivKey(LairApiReqGetWkaTlsCertPrivKey {
                msg_id,
                ..
            }) => msg_id.clone(),
            Self::ResGetWkaTlsCertPrivKey(LairApiResGetWkaTlsCertPrivKey {
                msg_id,
                ..
            }) => msg_id.clone(),
            Self::ReqSecretBoxXSalsaByTag(LairApiReqSecretBoxXSalsaByTag {
                msg_id,
                ..
            }) => msg_id.clone(),
            Self::ResSecretBoxXSalsaByTag(LairApiResSecretBoxXSalsaByTag {
                msg_id,
                ..
            }) => msg_id.clone(),
            Self::ReqSecretBoxXSalsaOpenByTag(
                LairApiReqSecretBoxXSalsaOpenByTag { msg_id, .. },
            ) => msg_id.clone(),
            Self::ResSecretBoxXSalsaOpenByTag(
                LairApiResSecretBoxXSalsaOpenByTag { msg_id, .. },
            ) => msg_id.clone(),
        }
    }
}