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
509
use std::collections::HashSet;
use bc_components::{
EncapsulationPublicKey, KeyDerivationMethod, PrivateKeys,
PrivateKeysProvider, PublicKeys, PublicKeysProvider, Reference,
ReferenceProvider, Salt, SigningPublicKey, URI, Verifier,
};
use bc_envelope::{PrivateKeyBase, prelude::*};
use known_values::{ENDPOINT, NICKNAME, PRIVATE_KEY};
use super::Permissions;
use crate::{Error, HasNickname, HasPermissions, Privilege, Result};
/// Private key data that can be either decrypted or encrypted.
#[derive(Debug, Clone)]
pub enum PrivateKeyData {
/// Decrypted private keys that can be used for signing/decryption.
Decrypted(PrivateKeys),
/// Encrypted private key envelope that cannot be used without decryption.
/// This preserves the encrypted assertion when a document is loaded
/// without the decryption password.
///
/// Note: Envelope uses internal reference counting (Rc/Arc) so cloning
/// is cheap - no need for additional wrapper.
Encrypted(Envelope),
}
impl PartialEq for PrivateKeyData {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Decrypted(a), Self::Decrypted(b)) => a == b,
(Self::Encrypted(a), Self::Encrypted(b)) => {
// Compare envelopes by their UR string representation
a.ur_string() == b.ur_string()
}
_ => false,
}
}
}
impl Eq for PrivateKeyData {}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Key {
public_keys: PublicKeys,
private_keys: Option<(PrivateKeyData, Salt)>,
nickname: String,
endpoints: HashSet<URI>,
permissions: Permissions,
}
impl Verifier for Key {
fn verify(
&self,
signature: &bc_components::Signature,
message: &dyn AsRef<[u8]>,
) -> bool {
self.public_keys.verify(signature, message)
}
}
impl PublicKeysProvider for Key {
fn public_keys(&self) -> PublicKeys { self.public_keys.clone() }
}
impl std::hash::Hash for Key {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.public_keys.hash(state);
}
}
impl Key {
pub fn new(public_keys: impl AsRef<PublicKeys>) -> Self {
Self {
public_keys: public_keys.as_ref().clone(),
private_keys: None,
nickname: String::new(),
endpoints: HashSet::new(),
permissions: Permissions::new(),
}
}
pub fn new_allow_all(public_keys: impl AsRef<PublicKeys>) -> Self {
Self {
public_keys: public_keys.as_ref().clone(),
private_keys: None,
nickname: String::new(),
endpoints: HashSet::new(),
permissions: Permissions::new_allow_all(),
}
}
pub fn new_with_private_keys(
private_keys: PrivateKeys,
public_keys: PublicKeys,
) -> Self {
let salt = Salt::new_with_len(32).unwrap();
Self {
public_keys,
private_keys: Some((PrivateKeyData::Decrypted(private_keys), salt)),
nickname: String::new(),
endpoints: HashSet::new(),
permissions: Permissions::new_allow_all(),
}
}
pub fn new_with_private_key_base(private_key_base: PrivateKeyBase) -> Self {
let private_keys = private_key_base.private_keys();
let public_keys = private_key_base.public_keys();
Self::new_with_private_keys(private_keys, public_keys)
}
pub fn public_keys(&self) -> &PublicKeys { &self.public_keys }
pub fn private_keys(&self) -> Option<&PrivateKeys> {
self.private_keys.as_ref().and_then(|(data, _)| match data {
PrivateKeyData::Decrypted(keys) => Some(keys),
PrivateKeyData::Encrypted(_) => None,
})
}
pub fn has_private_keys(&self) -> bool {
matches!(
self.private_keys.as_ref(),
Some((PrivateKeyData::Decrypted(_), _))
)
}
pub fn has_encrypted_private_keys(&self) -> bool {
matches!(
self.private_keys.as_ref(),
Some((PrivateKeyData::Encrypted(_), _))
)
}
pub fn private_key_salt(&self) -> Option<&Salt> {
self.private_keys.as_ref().map(|(_, salt)| salt)
}
/// Extract the private key data as an Envelope, optionally decrypting it.
///
/// # Returns
///
/// - `Ok(None)` if no private key is present
/// - `Ok(Some(Envelope))` containing:
/// - Decrypted `PrivateKeys` if unencrypted
/// - Decrypted `PrivateKeys` if encrypted and correct password provided
/// - Encrypted envelope if encrypted and no password provided
/// - `Err(...)` if encrypted and wrong password provided
///
/// # Examples
///
/// ```
/// use bc_components::PrivateKeyBase;
/// use bc_envelope::prelude::*;
/// use bc_xid::Key;
///
/// // Unencrypted key
/// let prvkey_base = PrivateKeyBase::new();
/// let key = Key::new_with_private_key_base(prvkey_base.clone());
/// let envelope = key.private_key_envelope(None).unwrap().unwrap();
/// // Returns envelope containing PrivateKeys
///
/// // Encrypted key without password
/// // Returns the encrypted envelope as-is
///
/// // Encrypted key with correct password
/// // Returns envelope containing decrypted PrivateKeys
/// ```
pub fn private_key_envelope(
&self,
password: Option<&str>,
) -> Result<Option<Envelope>> {
match &self.private_keys {
None => Ok(None),
Some((PrivateKeyData::Decrypted(private_keys), _)) => {
// Unencrypted key - return as envelope
Ok(Some(Envelope::new(private_keys.clone())))
}
Some((PrivateKeyData::Encrypted(encrypted_envelope), _)) => {
if let Some(pwd) = password {
// Try to decrypt with provided password
match encrypted_envelope.clone().unlock_subject(pwd) {
Ok(decrypted) => {
// Successfully decrypted
Ok(Some(decrypted))
}
Err(_) => {
// Wrong password
Err(Error::InvalidPassword)
}
}
} else {
// No password provided, return encrypted envelope as-is
Ok(Some(encrypted_envelope.clone()))
}
}
}
}
pub fn signing_public_key(&self) -> &SigningPublicKey {
self.public_keys.signing_public_key()
}
pub fn encapsulation_public_key(&self) -> &EncapsulationPublicKey {
self.public_keys.enapsulation_public_key()
}
pub fn endpoints(&self) -> &HashSet<URI> { &self.endpoints }
pub fn endpoints_mut(&mut self) -> &mut HashSet<URI> { &mut self.endpoints }
pub fn add_endpoint(&mut self, endpoint: URI) {
self.endpoints.insert(endpoint);
}
pub fn permissions(&self) -> &Permissions { &self.permissions }
pub fn permissions_mut(&mut self) -> &mut Permissions {
&mut self.permissions
}
pub fn add_permission(&mut self, privilege: Privilege) {
self.permissions.add_allow(privilege);
}
}
impl HasNickname for Key {
fn nickname(&self) -> &str { &self.nickname }
fn set_nickname(&mut self, nickname: impl Into<String>) {
self.nickname = nickname.into();
}
}
impl HasPermissions for Key {
fn permissions(&self) -> &Permissions { &self.permissions }
fn permissions_mut(&mut self) -> &mut Permissions { &mut self.permissions }
}
/// Options for handling private keys in envelopes.
#[derive(Clone, Debug, PartialEq, Eq, Default)]
pub enum XIDPrivateKeyOptions {
/// Omit the private key from the envelope (default).
#[default]
Omit,
/// Include the private key in plaintext (with salt for decorrelation).
Include,
/// Include the private key assertion but elide it (maintains digest tree).
Elide,
/// Include the private key encrypted with a password using the specified
/// key derivation method.
Encrypt {
method: KeyDerivationMethod,
password: Vec<u8>,
},
}
impl Key {
fn private_key_assertion_envelope(&self) -> Envelope {
let (private_key_data, salt) = self.private_keys.clone().unwrap();
match private_key_data {
PrivateKeyData::Decrypted(private_keys) => {
Envelope::new_assertion(PRIVATE_KEY, private_keys)
.add_salt_instance(salt)
}
PrivateKeyData::Encrypted(encrypted_envelope) => {
// Already encrypted, just wrap with privateKey predicate and
// salt
Envelope::new_assertion(PRIVATE_KEY, encrypted_envelope)
.add_salt_instance(salt)
}
}
}
fn extract_optional_private_key_with_password(
envelope: &Envelope,
password: Option<&[u8]>,
) -> Result<Option<(PrivateKeyData, Salt)>> {
if let Some(private_key_assertion) =
envelope.optional_assertion_with_predicate(PRIVATE_KEY)?
{
let private_key_object =
private_key_assertion.subject().try_object()?;
// Extract the salt (always present)
let salt = private_key_assertion
.extract_object_for_predicate::<Salt>(known_values::SALT)?;
// Check if the private key object is locked with a password
if private_key_object.is_locked_with_password() {
// Need a password to decrypt
if let Some(pwd) = password {
// Try to unlock with the password
match private_key_object.unlock_subject(pwd) {
Ok(decrypted) => {
// Successfully decrypted, extract the private key
let private_keys_cbor =
decrypted.subject().try_leaf()?;
let private_keys =
PrivateKeys::try_from(private_keys_cbor)?;
return Ok(Some((
PrivateKeyData::Decrypted(private_keys),
salt,
)));
}
Err(_) => {
// Wrong password or decryption failed
// Store the encrypted envelope for later
return Ok(Some((
PrivateKeyData::Encrypted(
private_key_object.clone(),
),
salt,
)));
}
}
} else {
// No password provided, store encrypted envelope
return Ok(Some((
PrivateKeyData::Encrypted(private_key_object.clone()),
salt,
)));
}
}
// Extract plaintext private key
let private_keys_cbor = private_key_object.try_leaf()?;
let private_keys = PrivateKeys::try_from(private_keys_cbor)?;
return Ok(Some((PrivateKeyData::Decrypted(private_keys), salt)));
}
Ok(None)
}
pub fn into_envelope_opt(
self,
private_key_options: XIDPrivateKeyOptions,
) -> Envelope {
let mut envelope = Envelope::new(self.public_keys().clone());
if let Some((private_key_data, _)) = &self.private_keys {
match private_key_data {
PrivateKeyData::Encrypted(_) => {
// Always preserve encrypted keys, regardless of options
let assertion_envelope =
self.private_key_assertion_envelope();
envelope = envelope
.add_assertion_envelope(assertion_envelope)
.unwrap();
}
PrivateKeyData::Decrypted(_) => {
// For decrypted keys, respect the private_key_options
match private_key_options {
XIDPrivateKeyOptions::Include => {
let assertion_envelope =
self.private_key_assertion_envelope();
envelope = envelope
.add_assertion_envelope(assertion_envelope)
.unwrap();
}
XIDPrivateKeyOptions::Elide => {
let assertion_envelope =
self.private_key_assertion_envelope().elide();
envelope = envelope
.add_assertion_envelope(assertion_envelope)
.unwrap();
}
XIDPrivateKeyOptions::Encrypt { method, password } => {
let (private_keys, salt) =
self.private_keys.clone().unwrap();
match private_keys {
PrivateKeyData::Decrypted(keys) => {
// Create an envelope with just the private
// keys
let private_keys_envelope =
Envelope::new(keys);
// Encrypt it using lock_subject
let encrypted = private_keys_envelope
.lock_subject(method, password)
.expect(
"Failed to encrypt private key",
);
// Create the privateKey assertion with the
// encrypted envelope
let assertion_envelope =
Envelope::new_assertion(
PRIVATE_KEY,
encrypted,
)
.add_salt_instance(salt);
envelope = envelope
.add_assertion_envelope(
assertion_envelope,
)
.unwrap();
}
PrivateKeyData::Encrypted(
encrypted_envelope,
) => {
// Already encrypted - we can't re-encrypt
// without
// decrypting first. Just preserve the
// existing
// encrypted envelope.
let assertion_envelope =
Envelope::new_assertion(
PRIVATE_KEY,
encrypted_envelope,
)
.add_salt_instance(salt);
envelope = envelope
.add_assertion_envelope(
assertion_envelope,
)
.unwrap();
}
}
}
XIDPrivateKeyOptions::Omit => {
// Omit decrypted private keys
}
}
}
}
}
envelope = envelope.add_nonempty_string_assertion(
known_values::NICKNAME,
self.nickname,
);
envelope = self
.endpoints
.into_iter()
.fold(envelope, |envelope, endpoint| {
envelope.add_assertion(ENDPOINT, endpoint)
});
self.permissions.add_to_envelope(envelope)
}
}
impl EnvelopeEncodable for Key {
fn into_envelope(self) -> Envelope {
self.into_envelope_opt(XIDPrivateKeyOptions::Omit)
}
}
impl TryFrom<&Envelope> for Key {
type Error = Error;
fn try_from(envelope: &Envelope) -> Result<Self> {
Self::try_from_envelope(envelope, None)
}
}
impl TryFrom<Envelope> for Key {
type Error = Error;
fn try_from(envelope: Envelope) -> Result<Self> { Key::try_from(&envelope) }
}
impl Key {
/// Try to extract a `Key` from an envelope, optionally providing a
/// password to decrypt an encrypted private key.
///
/// If the private key is encrypted and no password is provided, the `Key`
/// will be created without the private key (it will be `None`).
pub fn try_from_envelope(
envelope: &Envelope,
password: Option<&[u8]>,
) -> Result<Self> {
let public_keys = PublicKeys::try_from(envelope.subject().try_leaf()?)?;
let private_keys = Key::extract_optional_private_key_with_password(
envelope, password,
)?;
let nickname = envelope.extract_object_for_predicate_with_default(
NICKNAME,
String::new(),
)?;
let mut endpoints = HashSet::new();
for assertion in envelope.assertions_with_predicate(ENDPOINT) {
let endpoint =
URI::try_from(assertion.try_object()?.subject().try_leaf()?)?;
endpoints.insert(endpoint);
}
let permissions = Permissions::try_from_envelope(envelope)?;
Ok(Self {
public_keys,
private_keys,
nickname,
endpoints,
permissions,
})
}
}
impl ReferenceProvider for &Key {
fn reference(&self) -> Reference { self.public_keys.reference() }
}