connexa 0.5.1

High level abstraction of rust-libp2p
Documentation
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
pub mod cipher;
pub mod store;

use crate::keystore::store::memory::MemoryKeystore;
use cipher::xchacha20poly1305::XChaCha20Poly1305Cipher;
use futures::TryStreamExt;
use futures::stream::FuturesUnordered;
use libp2p::PeerId;
use libp2p::identity::{Keypair, PublicKey};
use rand::RngCore;
use std::fmt::Display;
use std::future::Future;
use std::sync::Arc;
use thiserror::Error;
use web_time::Duration;
use web_time::SystemTime;
use zeroize::Zeroizing;

type Result<T> = std::result::Result<T, Error>;

#[derive(Debug, Error)]
pub enum Error {
    #[error("no key found for label '{0}'")]
    NotFound(String),
    #[error("key '{0}' has expired")]
    Expired(String),
    #[error("failed to encrypt key material")]
    EncryptFailed,
    #[error("failed to decrypt key material (wrong master key or tampered entry)")]
    DecryptFailed,
    #[error(transparent)]
    Key(#[from] libp2p::identity::DecodingError),
    #[error("keystore backend error: {0}")]
    Backend(std::io::Error),
    #[error("cannot generate a key of type {0}")]
    UnsupportedKeyType(KeyType),
    #[error("key type mismatch: stored key is {has:?} but wanted {wanted:?}")]
    KeyTypeMismatch { has: KeyType, wanted: KeyType },
    #[error("invalid key label: {0:?}")]
    InvalidLabel(String),
    #[error("keychain is disabled")]
    Disabled,
}

/// The cryptographic family of a stored key, mirroring [`libp2p::identity::KeyType`].
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum KeyType {
    Ed25519,
    Rsa,
    Secp256k1,
    Ecdsa,
}

impl Display for KeyType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            KeyType::Ed25519 => write!(f, "Ed25519"),
            KeyType::Rsa => write!(f, "RSA"),
            KeyType::Secp256k1 => write!(f, "Secp256k1"),
            KeyType::Ecdsa => write!(f, "Ecdsa"),
        }
    }
}

impl From<libp2p::identity::KeyType> for KeyType {
    fn from(value: libp2p::identity::KeyType) -> Self {
        use libp2p::identity::KeyType as P2pKeyType;
        match value {
            P2pKeyType::Ed25519 => KeyType::Ed25519,
            P2pKeyType::RSA => KeyType::Rsa,
            P2pKeyType::Secp256k1 => KeyType::Secp256k1,
            P2pKeyType::Ecdsa => KeyType::Ecdsa,
        }
    }
}

/// What [`Keychain::rotate`] installs as the new key: a supplied keypair, or a freshly generated
/// one of the given [`KeyType`].
#[allow(clippy::large_enum_variant)]
pub enum RotateKey {
    Keypair(Keypair),
    Generate(KeyType),
}

impl From<Keypair> for RotateKey {
    fn from(keypair: Keypair) -> Self {
        RotateKey::Keypair(keypair)
    }
}

impl From<&Keypair> for RotateKey {
    fn from(keypair: &Keypair) -> Self {
        RotateKey::Keypair(keypair.clone())
    }
}

impl From<KeyType> for RotateKey {
    fn from(key_type: KeyType) -> Self {
        RotateKey::Generate(key_type)
    }
}

/// When a stored key should expire.
#[derive(Debug, Clone, Copy)]
pub enum Expiry {
    /// The key never expires.
    Never,
    /// The key expires this long after it is stored.
    After(Duration),
    /// The key expires at a specific point in time.
    At(SystemTime),
}

impl Expiry {
    fn resolve(self, created_at: SystemTime) -> Option<SystemTime> {
        match self {
            Expiry::Never => None,
            Expiry::After(ttl) => created_at.checked_add(ttl),
            Expiry::At(at) => Some(at),
        }
    }
}

/// Metadata describing a stored key.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone)]
pub struct KeyMetadata {
    pub label: String,
    pub key_type: KeyType,
    /// How many times this label has been written; starts at 1, bumped by [`Keychain::rotate`].
    pub version: u32,
    pub created_at: SystemTime,
    pub expires_at: Option<SystemTime>,
    #[cfg_attr(feature = "serde", serde(with = "serde_bytes"))]
    public_key: Vec<u8>,
}

impl KeyMetadata {
    /// Whether the key is past its expiration time.
    pub fn is_expired(&self) -> bool {
        self.expires_at.is_some_and(|exp| SystemTime::now() >= exp)
    }

    /// The [`PublicKey`] of the stored keypair.
    pub fn public_key(&self) -> Result<PublicKey> {
        let pubkey = PublicKey::try_decode_protobuf(&self.public_key).map_err(Error::from)?;
        let pub_key_type: KeyType = KeyType::from(pubkey.key_type());
        if pub_key_type != self.key_type {
            return Err(Error::KeyTypeMismatch {
                has: pub_key_type,
                wanted: self.key_type,
            });
        }
        Ok(pubkey)
    }

    /// The [`PeerId`] of the stored keypair.
    pub fn peer_id(&self) -> Result<PeerId> {
        Ok(self.public_key()?.to_peer_id())
    }
}

/// An encrypted key entry as seen by a [`Keystore`] backend. Backends only ever handle ciphertext.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone)]
pub struct EncryptedEntry {
    pub metadata: KeyMetadata,
    /// Opaque ciphertext blob produced by the [`Cipher`] backend (it owns any nonce/framing).
    #[cfg_attr(feature = "serde", serde(with = "serde_bytes"))]
    ciphertext: Vec<u8>,
}

impl EncryptedEntry {
    pub fn ciphertext(&self) -> &[u8] {
        &self.ciphertext
    }
}

/// Generate a random 32-byte master key for a [`Keychain`].
pub fn generate_key() -> [u8; 32] {
    let mut key = [0u8; 32];
    rand::thread_rng().fill_bytes(&mut key);
    key
}

fn generate_keypair(key_type: KeyType) -> Result<Keypair> {
    match key_type {
        #[cfg(feature = "ed25519")]
        KeyType::Ed25519 => Ok(Keypair::generate_ed25519()),
        #[cfg(feature = "secp256k1")]
        KeyType::Secp256k1 => Ok(Keypair::generate_secp256k1()),
        #[cfg(feature = "ecdsa")]
        KeyType::Ecdsa => Ok(Keypair::generate_ecdsa()),
        other => Err(Error::UnsupportedKeyType(other)),
    }
}

pub(crate) fn validate_label(label: &str) -> Result<()> {
    if label.is_empty() || label == "." || label == ".." || label.contains(['/', '\\', '\0']) {
        return Err(Error::InvalidLabel(label.to_owned()));
    }
    Ok(())
}

/// Backend responsible for persisting encrypted key entries.
pub trait Keystore: Send + Sync + 'static {
    /// Store (or replace) an entry, keyed by its `label`.
    fn put(&self, entry: EncryptedEntry) -> impl Future<Output = Result<()>> + Send;
    /// Store (or replace) many entries.
    fn put_many(&self, entries: Vec<EncryptedEntry>) -> impl Future<Output = Result<()>> + Send {
        async move {
            FuturesUnordered::from_iter(entries.into_iter().map(|entry| self.put(entry)))
                .try_collect::<()>()
                .await
        }
    }
    /// Fetch the entry for `label`, if present.
    fn get(&self, label: &str) -> impl Future<Output = Result<Option<EncryptedEntry>>> + Send;
    /// List metadata for all stored entries.
    fn list(&self) -> impl Future<Output = Result<Vec<KeyMetadata>>> + Send;
    /// Remove the entry for `label`, returning whether one existed.
    fn remove(&self, label: &str) -> impl Future<Output = Result<bool>> + Send;
}

/// Pluggable authenticated-encryption backend for a [`Keychain`].
pub trait Cipher: Send + Sync + 'static {
    fn encrypt(&self, aad: Option<&[u8]>, plaintext: &[u8]) -> Result<Vec<u8>>;
    fn decrypt(&self, aad: Option<&[u8]>, ciphertext: &[u8]) -> Result<Vec<u8>>;
}

/// An encrypted keychain
pub struct Keychain<S = MemoryKeystore> {
    cipher: Arc<dyn Cipher>,
    backend: Arc<S>,
    disabled: bool,
}

impl<S> Clone for Keychain<S> {
    fn clone(&self) -> Self {
        Self {
            cipher: self.cipher.clone(),
            backend: self.backend.clone(),
            disabled: self.disabled,
        }
    }
}

impl<S: Keystore + Default> Keychain<S> {
    /// Create a keychain.
    pub fn new(key: [u8; 32]) -> Self {
        Self::with_cipher(XChaCha20Poly1305Cipher::new(key), S::default())
    }

    /// Disabled keychain
    pub(crate) fn disabled() -> Self {
        Self {
            cipher: Arc::new(XChaCha20Poly1305Cipher::new([0u8; 32])),
            backend: Arc::new(S::default()),
            disabled: true,
        }
    }

    /// Create a new keychain with a custom cipher.
    pub fn new_with_custom_cipher(cipher: impl Cipher) -> Self {
        Self::with_cipher(cipher, S::default())
    }
}

impl<S: Keystore> Keychain<S> {
    /// Create a keychain with a custom storage backend.
    pub fn new_with_store(key: [u8; 32], backend: S) -> Self {
        Self::with_cipher(XChaCha20Poly1305Cipher::new(key), backend)
    }

    /// Create a keychain over `backend` using a custom [`Cipher`] backend (e.g. AES-GCM).
    pub fn with_cipher(cipher: impl Cipher, backend: S) -> Self {
        Self {
            cipher: Arc::new(cipher),
            backend: Arc::new(backend),
            disabled: false,
        }
    }

    pub fn is_disabled(&self) -> bool {
        self.disabled
    }

    fn disable_check(&self) -> Result<()> {
        if self.disabled {
            return Err(Error::Disabled);
        }
        Ok(())
    }

    /// Encrypt `keypair` and store it under `label` with no expiry, replacing any existing entry.
    pub async fn insert(&self, label: &str, keypair: &Keypair) -> Result<()> {
        self.store(label, keypair, Expiry::Never, 1).await
    }

    /// Like [`Keychain::insert`], but applying an expiration policy.
    pub async fn insert_with_expiry(
        &self,
        label: &str,
        keypair: &Keypair,
        expiry: Expiry,
    ) -> Result<()> {
        self.store(label, keypair, expiry, 1).await
    }

    /// Generate a fresh Ed25519 keypair, store it under `label`
    #[cfg(feature = "ed25519")]
    pub async fn generate_ed25519(&self, label: &str) -> Result<Keypair> {
        self.generate(label, KeyType::Ed25519).await
    }

    /// Like [`Keychain::generate_ed25519`], for Secp256k1.
    #[cfg(feature = "secp256k1")]
    pub async fn generate_secp256k1(&self, label: &str) -> Result<Keypair> {
        self.generate(label, KeyType::Secp256k1).await
    }

    /// Like [`Keychain::generate_ed25519`], for ECDSA.
    #[cfg(feature = "ecdsa")]
    pub async fn generate_ecdsa(&self, label: &str) -> Result<Keypair> {
        self.generate(label, KeyType::Ecdsa).await
    }

    /// Generate a fresh keypair of `key_type`, store it under `label`.
    pub async fn generate(&self, label: &str, key_type: KeyType) -> Result<Keypair> {
        let keypair = generate_keypair(key_type)?;
        self.insert(label, &keypair).await?;
        Ok(keypair)
    }

    /// Replace the key stored under `label`, bumping its [`KeyMetadata::version`]. The replacement
    /// is either a supplied keypair or one generated from a [`KeyType`].
    pub async fn rotate(
        &self,
        label: &str,
        key: impl Into<RotateKey>,
        expiry: Expiry,
    ) -> Result<()> {
        validate_label(label)?;
        let current = self
            .backend
            .get(label)
            .await?
            .ok_or_else(|| Error::NotFound(label.to_owned()))?;
        let version = current.metadata.version.saturating_add(1);
        let keypair = match key.into() {
            RotateKey::Keypair(keypair) => keypair,
            RotateKey::Generate(key_type) => generate_keypair(key_type)?,
        };
        self.store(label, &keypair, expiry, version).await
    }

    async fn store(
        &self,
        label: &str,
        keypair: &Keypair,
        expiry: Expiry,
        version: u32,
    ) -> Result<()> {
        self.disable_check()?;
        validate_label(label)?;
        let plaintext = Zeroizing::new(keypair.to_protobuf_encoding()?);
        let ciphertext = self
            .cipher
            .encrypt(Some(label.as_bytes()), plaintext.as_slice())?;
        let created_at = SystemTime::now();
        let entry = EncryptedEntry {
            metadata: KeyMetadata {
                label: label.to_owned(),
                key_type: keypair.key_type().into(),
                version,
                created_at,
                expires_at: expiry.resolve(created_at),
                public_key: keypair.public().encode_protobuf(),
            },
            ciphertext,
        };
        self.backend.put(entry).await
    }

    /// Fetch and decrypt the keypair stored under `label`. Returns with [`Error::Expired`] if the
    /// key is past its expiration (the entry is left in place. See [`Keychain::purge_expired`]).
    pub async fn get(&self, label: &str) -> Result<Keypair> {
        self.disable_check()?;
        validate_label(label)?;
        let entry = self
            .backend
            .get(label)
            .await?
            .ok_or_else(|| Error::NotFound(label.to_owned()))?;
        if entry.metadata.is_expired() {
            return Err(Error::Expired(label.to_owned()));
        }
        let plaintext = Zeroizing::new(
            self.cipher
                .decrypt(Some(label.as_bytes()), &entry.ciphertext)?,
        );
        Keypair::from_protobuf_encoding(plaintext.as_slice()).map_err(Error::from)
    }

    /// The public key stored under `label`.
    pub async fn public_key(&self, label: &str) -> Result<PublicKey> {
        self.disable_check()?;
        validate_label(label)?;
        let entry = self
            .backend
            .get(label)
            .await?
            .ok_or_else(|| Error::NotFound(label.to_owned()))?;
        if entry.metadata.is_expired() {
            return Err(Error::Expired(label.to_owned()));
        }
        entry.metadata.public_key()
    }

    /// The [`PeerId`] stored under `label`.
    pub async fn peer_id(&self, label: &str) -> Result<PeerId> {
        Ok(self.public_key(label).await?.to_peer_id())
    }

    /// Load the keypair stored under `label`, or generate and store a new identity if none
    /// exists yet. An existing-but-expired key surfaces as [`Error::Expired`] rather than being
    /// regenerated.
    pub async fn get_or_create(&self, label: &str) -> Result<Keypair> {
        match self.get(label).await {
            Ok(keypair) => Ok(keypair),
            Err(Error::NotFound(_)) => {
                let keypair = Keypair::generate_ed25519();
                self.insert(label, &keypair).await?;
                Ok(keypair)
            }
            Err(err) => Err(err),
        }
    }

    /// List metadata for all stored keys.
    pub async fn list(&self) -> Result<Vec<KeyMetadata>> {
        self.backend.list().await
    }

    /// Fetch the metadata for `label` without decrypting the key.
    pub async fn metadata(&self, label: &str) -> Result<Option<KeyMetadata>> {
        validate_label(label)?;
        Ok(self.backend.get(label).await?.map(|entry| entry.metadata))
    }

    /// Remove the key stored under `label`, returning whether one existed.
    pub async fn remove(&self, label: &str) -> Result<bool> {
        validate_label(label)?;
        self.backend.remove(label).await
    }

    /// Remove every expired key, returning how many were removed.
    pub async fn purge_expired(&self) -> Result<usize> {
        let expired = self
            .backend
            .list()
            .await?
            .into_iter()
            .filter(KeyMetadata::is_expired)
            .map(|metadata| metadata.label);
        let mut removed = 0;
        for label in expired {
            if self.backend.remove(&label).await? {
                removed += 1;
            }
        }
        Ok(removed)
    }

    /// Re-encrypt every entry under a new cipher, returning a keychain that shares this backend but
    /// uses the new cipher (the old cipher can no longer read the store).
    pub async fn migrate_cipher(&self, new_cipher: impl Cipher) -> Result<Keychain<S>> {
        self.disable_check()?;
        let new_cipher: Arc<dyn Cipher> = Arc::new(new_cipher);

        let prev_entries = self.backend.list().await?;

        let entries = FuturesUnordered::from_iter(prev_entries.into_iter().map(|metadata| {
            let new_cipher = new_cipher.clone();
            async move {
                let label = &metadata.label;
                let entry = self
                    .backend
                    .get(label)
                    .await?
                    .ok_or_else(|| Error::NotFound(label.clone()))?;
                let plaintext = Zeroizing::new(
                    self.cipher
                        .decrypt(Some(label.as_bytes()), &entry.ciphertext)?,
                );
                let ciphertext =
                    new_cipher.encrypt(Some(label.as_bytes()), plaintext.as_slice())?;
                Ok::<_, Error>(EncryptedEntry {
                    metadata,
                    ciphertext,
                })
            }
        }))
        .try_collect::<Vec<_>>()
        .await?;

        self.backend.put_many(entries).await?;

        Ok(Keychain {
            cipher: new_cipher,
            backend: self.backend.clone(),
            disabled: self.disabled,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[cfg(feature = "ed25519")]
    #[tokio::test]
    async fn round_trip_list_remove() {
        let keychain = Keychain::<MemoryKeystore>::new(generate_key());
        let keypair = Keypair::generate_ed25519();

        keychain.insert("identity", &keypair).await.unwrap();

        let recovered = keychain.get("identity").await.unwrap();
        assert_eq!(
            recovered.public().to_peer_id(),
            keypair.public().to_peer_id()
        );

        let meta = keychain.metadata("identity").await.unwrap().unwrap();
        assert_eq!(meta.label, "identity");
        assert_eq!(meta.key_type, KeyType::Ed25519);
        assert_eq!(meta.version, 1);
        assert!(meta.expires_at.is_none());

        assert!(keychain.remove("identity").await.unwrap());
        assert!(matches!(
            keychain.get("identity").await,
            Err(Error::NotFound(_))
        ));
    }

    #[cfg(feature = "ed25519")]
    #[tokio::test]
    async fn expired_key_is_rejected_and_purged() {
        let keychain = Keychain::<MemoryKeystore>::new(generate_key());
        let keypair = Keypair::generate_ed25519();

        keychain
            .insert_with_expiry("old", &keypair, Expiry::At(web_time::UNIX_EPOCH))
            .await
            .unwrap();
        keychain
            .insert_with_expiry("live", &keypair, Expiry::After(Duration::from_secs(3600)))
            .await
            .unwrap();

        assert!(matches!(keychain.get("old").await, Err(Error::Expired(_))));
        assert!(keychain.get("live").await.is_ok());

        assert_eq!(keychain.purge_expired().await.unwrap(), 1);
        assert!(matches!(keychain.get("old").await, Err(Error::NotFound(_))));
        assert!(keychain.get("live").await.is_ok());
    }

    #[cfg(feature = "ed25519")]
    #[tokio::test]
    async fn rotate_bumps_version_and_replaces_key() {
        let keychain = Keychain::<MemoryKeystore>::new(generate_key());
        let first = Keypair::generate_ed25519();
        let second = Keypair::generate_ed25519();

        keychain.insert("id", &first).await.unwrap();
        assert_eq!(keychain.metadata("id").await.unwrap().unwrap().version, 1);

        keychain.rotate("id", &second, Expiry::Never).await.unwrap();
        let meta = keychain.metadata("id").await.unwrap().unwrap();
        assert_eq!(meta.version, 2);
        assert_eq!(
            keychain.get("id").await.unwrap().public().to_peer_id(),
            second.public().to_peer_id()
        );

        assert!(matches!(
            keychain.rotate("missing", &first, Expiry::Never).await,
            Err(Error::NotFound(_))
        ));
    }

    #[cfg(feature = "ed25519")]
    #[tokio::test]
    async fn wrong_master_key_fails_to_decrypt() {
        let keypair = Keypair::generate_ed25519();
        let first = Keychain::<MemoryKeystore>::new(generate_key());
        first.insert("k", &keypair).await.unwrap();

        let other = Keychain {
            cipher: Arc::new(XChaCha20Poly1305Cipher::new(generate_key())),
            backend: first.backend.clone(),
            disabled: first.disabled,
        };
        assert!(matches!(other.get("k").await, Err(Error::DecryptFailed)));
    }

    #[cfg(feature = "ed25519")]
    #[tokio::test]
    async fn relabeled_ciphertext_fails_to_decrypt() {
        let keychain = Keychain::<MemoryKeystore>::new(generate_key());
        keychain
            .insert("a", &Keypair::generate_ed25519())
            .await
            .unwrap();

        let mut entry = keychain.backend.get("a").await.unwrap().unwrap();
        entry.metadata.label = "b".into();
        keychain.backend.put(entry).await.unwrap();

        assert!(matches!(keychain.get("b").await, Err(Error::DecryptFailed)));
    }

    #[cfg(feature = "ed25519")]
    #[tokio::test]
    async fn rejects_invalid_labels_on_any_backend() {
        let keychain = Keychain::<MemoryKeystore>::new(generate_key());
        let keypair = Keypair::generate_ed25519();

        for bad in ["", ".", "..", "a/b", "a\\b", "a\0b"] {
            assert!(
                matches!(
                    keychain.insert(bad, &keypair).await,
                    Err(Error::InvalidLabel(_))
                ),
                "insert({bad:?}) should be rejected"
            );
            assert!(
                matches!(keychain.get(bad).await, Err(Error::InvalidLabel(_))),
                "get({bad:?}) should be rejected"
            );
            assert!(
                matches!(keychain.remove(bad).await, Err(Error::InvalidLabel(_))),
                "remove({bad:?}) should be rejected"
            );
        }
    }

    #[cfg(feature = "ed25519")]
    #[tokio::test]
    async fn get_or_create_persists_identity() {
        let keychain = Keychain::<MemoryKeystore>::new(generate_key());
        let first = keychain.get_or_create("id").await.unwrap();
        let second = keychain.get_or_create("id").await.unwrap();
        assert_eq!(
            first.public().to_peer_id(),
            second.public().to_peer_id(),
            "get_or_create must not regenerate on the second call"
        );
        assert_eq!(keychain.metadata("id").await.unwrap().unwrap().version, 1);
    }

    #[cfg(feature = "ed25519")]
    #[tokio::test]
    async fn custom_cipher_round_trip() {
        // Note: XOR is not real encryption; it only exercises the custom-Crypto plug point.
        struct XorCipher(u8);
        impl Cipher for XorCipher {
            fn encrypt(&self, _aad: Option<&[u8]>, plaintext: &[u8]) -> Result<Vec<u8>> {
                Ok(plaintext.iter().map(|b| b ^ self.0).collect())
            }
            fn decrypt(&self, _aad: Option<&[u8]>, ciphertext: &[u8]) -> Result<Vec<u8>> {
                Ok(ciphertext.iter().map(|b| b ^ self.0).collect())
            }
        }

        let keychain = Keychain::<MemoryKeystore>::new_with_custom_cipher(XorCipher(0x5a));
        let keypair = Keypair::generate_ed25519();
        keychain.insert("id", &keypair).await.unwrap();
        let recovered = keychain.get("id").await.unwrap();
        assert_eq!(
            recovered.public().to_peer_id(),
            keypair.public().to_peer_id()
        );
    }

    #[cfg(feature = "ed25519")]
    #[tokio::test]
    async fn generate_stores_and_returns() {
        let keychain = Keychain::<MemoryKeystore>::new(generate_key());
        let kp = keychain.generate_ed25519("id").await.unwrap();
        assert_eq!(keychain.metadata("id").await.unwrap().unwrap().version, 1);
        assert_eq!(
            keychain.get("id").await.unwrap().public().to_peer_id(),
            kp.public().to_peer_id()
        );
    }

    #[cfg(feature = "secp256k1")]
    #[tokio::test]
    async fn generate_secp256k1_round_trip() {
        let keychain = Keychain::<MemoryKeystore>::new(generate_key());
        let kp = keychain.generate_secp256k1("id").await.unwrap();
        let meta = keychain.metadata("id").await.unwrap().unwrap();
        assert_eq!(meta.key_type, KeyType::Secp256k1);
        assert_eq!(meta.peer_id().unwrap(), kp.public().to_peer_id());
        assert_eq!(
            keychain.get("id").await.unwrap().public().to_peer_id(),
            kp.public().to_peer_id()
        );
    }

    #[cfg(feature = "ecdsa")]
    #[tokio::test]
    async fn generate_ecdsa_round_trip() {
        let keychain = Keychain::<MemoryKeystore>::new(generate_key());
        let kp = keychain.generate_ecdsa("id").await.unwrap();
        let meta = keychain.metadata("id").await.unwrap().unwrap();
        assert_eq!(meta.key_type, KeyType::Ecdsa);
        assert_eq!(meta.peer_id().unwrap(), kp.public().to_peer_id());
        assert_eq!(
            keychain.get("id").await.unwrap().public().to_peer_id(),
            kp.public().to_peer_id()
        );
    }

    #[cfg(feature = "ed25519")]
    #[tokio::test]
    async fn rotate_generate_makes_fresh_key() {
        let keychain = Keychain::<MemoryKeystore>::new(generate_key());
        let original = keychain.generate_ed25519("id").await.unwrap();

        keychain
            .rotate("id", KeyType::Ed25519, Expiry::Never)
            .await
            .unwrap();

        assert_eq!(keychain.metadata("id").await.unwrap().unwrap().version, 2);
        assert_ne!(
            keychain.get("id").await.unwrap().public().to_peer_id(),
            original.public().to_peer_id()
        );
    }

    #[tokio::test]
    async fn generate_rsa_is_unsupported() {
        let keychain = Keychain::<MemoryKeystore>::new(generate_key());
        assert!(matches!(
            keychain.generate("x", KeyType::Rsa).await,
            Err(Error::UnsupportedKeyType(KeyType::Rsa))
        ));
    }

    #[cfg(all(feature = "aes-gcm", feature = "ed25519"))]
    #[tokio::test]
    async fn aesgcm_round_trip() {
        let keychain = Keychain::<MemoryKeystore>::new_with_custom_cipher(
            cipher::aes_gcm::AesGcmCipher::new(generate_key()),
        );
        let keypair = Keypair::generate_ed25519();
        keychain.insert("id", &keypair).await.unwrap();
        let recovered = keychain.get("id").await.unwrap();
        assert_eq!(
            recovered.public().to_peer_id(),
            keypair.public().to_peer_id()
        );
    }
}