1
//! Code for managing multiple [`Keystore`](crate::Keystore)s.
2
//!
3
//! See the [`KeyMgr`] docs for more details.
4

            
5
use crate::raw::{RawEntryId, RawKeystoreEntry};
6
use crate::{
7
    ArtiPath, BoxedKeystore, Error, KeyCertificateSpecifier, KeyPath, KeyPathError, KeyPathInfo,
8
    KeyPathInfoExtractor, KeyPathPattern, KeySpecifier, KeystoreCorruptionError,
9
    KeystoreEntryResult, KeystoreId, KeystoreSelector, Result,
10
};
11

            
12
use itertools::Itertools;
13
use std::iter;
14
use std::result::Result as StdResult;
15
use tor_error::{bad_api_usage, internal, into_bad_api_usage};
16
use tor_key_forge::{
17
    ItemType, Keygen, KeygenRng, KeystoreItemType, ToEncodableCert, ToEncodableKey,
18
};
19

            
20
/// A key manager that acts as a frontend to a primary [`Keystore`](crate::Keystore) and
21
/// any number of secondary [`Keystore`](crate::Keystore)s.
22
///
23
/// Note: [`KeyMgr`] is a low-level utility and does not implement caching (the key stores are
24
/// accessed for every read/write).
25
///
26
/// The `KeyMgr` accessors - currently just [`get()`](KeyMgr::get) -
27
/// search the configured key stores in order: first the primary key store,
28
/// and then the secondary stores, in order.
29
///
30
///
31
/// ## Concurrent key store access
32
///
33
/// The key stores will allow concurrent modification by different processes. In
34
/// order to implement this safely without locking, the key store operations (get,
35
/// insert, remove) will need to be atomic.
36
///
37
/// **Note**: [`KeyMgr::generate`] and [`KeyMgr::get_or_generate`] should **not** be used
38
/// concurrently with any other `KeyMgr` operation that mutates the same key
39
/// (i.e. a key with the same `ArtiPath`), because
40
/// their outcome depends on whether the selected key store
41
/// [`contains`][crate::Keystore::contains]
42
/// the specified key (and thus suffers from a TOCTOU race).
43
#[derive(derive_builder::Builder)]
44
#[builder(pattern = "owned", build_fn(private, name = "build_unvalidated"))]
45
pub struct KeyMgr {
46
    /// The primary key store.
47
    primary_store: BoxedKeystore,
48
    /// The secondary key stores.
49
    #[builder(default, setter(custom))]
50
    secondary_stores: Vec<BoxedKeystore>,
51
    /// The key info extractors.
52
    ///
53
    /// These are initialized internally by [`KeyMgrBuilder::build`], using the values collected
54
    /// using `inventory`.
55
    #[builder(default, setter(skip))]
56
    key_info_extractors: Vec<&'static dyn KeyPathInfoExtractor>,
57
}
58

            
59
/// A keystore entry descriptor.
60
///
61
/// This identifies a key entry from a specific keystore.
62
/// The key entry can be retrieved, using [`KeyMgr::get_entry`],
63
/// or removed, using [`KeyMgr::remove_entry`].
64
///
65
/// Returned from [`KeyMgr::list_matching`].
66
#[derive(Clone, Debug, PartialEq, amplify::Getters)]
67
pub struct KeystoreEntry<'a> {
68
    /// The [`KeyPath`] of the key.
69
    key_path: KeyPath,
70
    /// The [`KeystoreItemType`] of the key.
71
    key_type: KeystoreItemType,
72
    /// The [`KeystoreId`] of the keystore where the key was found.
73
    #[getter(as_copy)]
74
    keystore_id: &'a KeystoreId,
75
    /// The [`RawEntryId`] of the key, an identifier used in
76
    /// `arti raw` operations.
77
    #[getter(skip)]
78
    raw_id: RawEntryId,
79
}
80

            
81
impl<'a> KeystoreEntry<'a> {
82
    /// Create a new `KeystoreEntry`
83
6006
    pub(crate) fn new(
84
6006
        key_path: KeyPath,
85
6006
        key_type: KeystoreItemType,
86
6006
        keystore_id: &'a KeystoreId,
87
6006
        raw_id: RawEntryId,
88
6006
    ) -> Self {
89
6006
        Self {
90
6006
            key_path,
91
6006
            key_type,
92
6006
            keystore_id,
93
6006
            raw_id,
94
6006
        }
95
6006
    }
96

            
97
    /// Return an instance of [`RawKeystoreEntry`]
98
    #[cfg(feature = "onion-service-cli-extra")]
99
    #[cfg_attr(docsrs, doc(cfg(feature = "onion-service-cli-extra")))]
100
540
    pub fn raw_entry(&self) -> RawKeystoreEntry {
101
540
        RawKeystoreEntry::new(self.raw_id.clone(), self.keystore_id.clone())
102
540
    }
103
}
104

            
105
// NOTE: Some methods require a `KeystoreEntryResult<KeystoreEntry>` as an
106
// argument (e.g.: `KeyMgr::raw_keystore_entry`). For this reason  implementing
107
// `From<KeystoreEntry<'a>> for KeystoreEntryResult<KeystoreEntry<'a>>` makes
108
// `KeystoreEntry` more ergonomic.
109
impl<'a> From<KeystoreEntry<'a>> for KeystoreEntryResult<KeystoreEntry<'a>> {
110
    fn from(val: KeystoreEntry<'a>) -> Self {
111
        Ok(val)
112
    }
113
}
114

            
115
impl KeyMgrBuilder {
116
    /// Construct a [`KeyMgr`] from this builder.
117
2182
    pub fn build(self) -> StdResult<KeyMgr, KeyMgrBuilderError> {
118
        use itertools::Itertools as _;
119

            
120
2182
        let mut keymgr = self.build_unvalidated()?;
121

            
122
2907
        if !keymgr.all_stores().map(|s| s.id()).all_unique() {
123
            return Err(KeyMgrBuilderError::ValidationError(
124
                "the keystore IDs are not pairwise unique".into(),
125
            ));
126
2182
        }
127

            
128
2182
        keymgr.key_info_extractors = inventory::iter::<&'static dyn KeyPathInfoExtractor>
129
2182
            .into_iter()
130
2182
            .copied()
131
2182
            .collect();
132

            
133
2182
        Ok(keymgr)
134
2182
    }
135
}
136

            
137
// TODO: auto-generate using define_list_builder_accessors/define_list_builder_helper
138
// when that becomes possible.
139
//
140
// See https://gitlab.torproject.org/tpo/core/arti/-/merge_requests/1760#note_2969841
141
impl KeyMgrBuilder {
142
    /// Access the being-built list of secondary stores (resolving default)
143
    ///
144
    /// If the field has not yet been set or accessed, the default list will be
145
    /// constructed and a mutable reference to the now-defaulted list of builders
146
    /// will be returned.
147
648
    pub fn secondary_stores(&mut self) -> &mut Vec<BoxedKeystore> {
148
648
        self.secondary_stores.get_or_insert(Default::default())
149
648
    }
150

            
151
    /// Set the whole list (overriding the default)
152
    pub fn set_secondary_stores(mut self, list: Vec<BoxedKeystore>) -> Self {
153
        self.secondary_stores = Some(list);
154
        self
155
    }
156

            
157
    /// Inspect the being-built list (with default unresolved)
158
    ///
159
    /// If the list has not yet been set, or accessed, `&None` is returned.
160
    pub fn opt_secondary_stores(&self) -> &Option<Vec<BoxedKeystore>> {
161
        &self.secondary_stores
162
    }
163

            
164
    /// Mutably access the being-built list (with default unresolved)
165
    ///
166
    /// If the list has not yet been set, or accessed, `&mut None` is returned.
167
    pub fn opt_secondary_stores_mut(&mut self) -> &mut Option<Vec<BoxedKeystore>> {
168
        &mut self.secondary_stores
169
    }
170
}
171

            
172
inventory::collect!(&'static dyn crate::KeyPathInfoExtractor);
173

            
174
impl KeyMgr {
175
    /// Read a key from one of the key stores, and try to deserialize it as `K::Key`.
176
    ///
177
    /// The key returned is retrieved from the first key store that contains an entry for the given
178
    /// specifier.
179
    ///
180
    /// Returns `Ok(None)` if none of the key stores have the requested key.
181
1578
    pub fn get<K: ToEncodableKey>(&self, key_spec: &dyn KeySpecifier) -> Result<Option<K>> {
182
1578
        let result = self.get_from_store(key_spec, &K::Key::item_type(), self.all_stores())?;
183
1578
        if result.is_none() {
184
            // If the key_spec is the specifier for the public part of a keypair,
185
            // try getting the pair and extracting the public portion from it.
186
474
            if let Some(key_pair_spec) = key_spec.keypair_specifier() {
187
224
                return Ok(self.get::<K::KeyPair>(&*key_pair_spec)?.map(|k| k.into()));
188
250
            }
189
1104
        }
190
1354
        Ok(result)
191
1578
    }
192

            
193
    /// Retrieve the specified keystore entry, and try to deserialize it as `K::Key`.
194
    ///
195
    /// The key returned is retrieved from the key store specified in the [`KeystoreEntry`].
196
    ///
197
    /// Returns `Ok(None)` if the key store does not contain the requested entry.
198
    ///
199
    /// Returns an error if the specified `key_type` does not match `K::Key::item_type()`.
200
32
    pub fn get_entry<K: ToEncodableKey>(&self, entry: &KeystoreEntry) -> Result<Option<K>> {
201
32
        let selector = entry.keystore_id().into();
202
32
        let store = self.select_keystore(&selector)?;
203
32
        self.get_from_store(entry.key_path(), entry.key_type(), [store].into_iter())
204
32
    }
205

            
206
    /// Read the key identified by `key_spec`.
207
    ///
208
    /// The key returned is retrieved from the first key store that contains an entry for the given
209
    /// specifier.
210
    ///
211
    /// If the requested key does not exist in any of the key stores, this generates a new key of
212
    /// type `K` from the key created using using `K::Key`'s [`Keygen`] implementation, and inserts
213
    /// it into the specified keystore, returning the newly inserted value.
214
    ///
215
    /// This is a convenience wrapper around [`get()`](KeyMgr::get) and
216
    /// [`generate()`](KeyMgr::generate).
217
132
    pub fn get_or_generate<K>(
218
132
        &self,
219
132
        key_spec: &dyn KeySpecifier,
220
132
        selector: KeystoreSelector,
221
132
        rng: &mut dyn KeygenRng,
222
132
    ) -> Result<K>
223
132
    where
224
132
        K: ToEncodableKey,
225
132
        K::Key: Keygen,
226
    {
227
132
        match self.get(key_spec)? {
228
130
            Some(k) => Ok(k),
229
2
            None => self.generate(key_spec, selector, rng, false),
230
        }
231
132
    }
232

            
233
    /// Read a key from one of the key stores specified, and try to deserialize it as `K::Key`.
234
    ///
235
    /// Returns `Ok(None)` if none of the key stores have the requested key.
236
    ///
237
    /// Returns an error if the specified keystore does not exist.
238
    #[cfg(feature = "onion-service-cli-extra")]
239
38
    pub fn get_from<K: ToEncodableKey>(
240
38
        &self,
241
38
        key_spec: &dyn KeySpecifier,
242
38
        keystore_id: &KeystoreId,
243
38
    ) -> Result<Option<K>> {
244
38
        let store = std::iter::once(self.find_keystore(keystore_id)?);
245
38
        self.get_from_store(key_spec, &K::Key::item_type(), store)
246
38
    }
247

            
248
    /// Validates the integrity of a [`KeystoreEntry`].
249
    ///
250
    /// This retrieves the key corresponding to the provided [`KeystoreEntry`],
251
    /// and checks if its contents are valid (i.e. that the key can be parsed).
252
    /// The [`KeyPath`] of the entry is further validated using [`describe`](KeyMgr::describe).
253
    ///
254
    /// NOTE: Currently, ctor entries cannot be validated using [`describe`](KeyMgr::describe), so they
255
    /// are considered valid if the manager successfully retrieves the corresponding keys,
256
    /// but are otherwise not validated by this procedure.
257
    ///
258
    /// Returns `Ok(())` if the specified keystore entry is valid, and `Err` otherwise.
259
    ///
260
    /// NOTE: If the specified entry does not exist, this will only validate its [`KeyPath`].
261
    #[cfg(feature = "onion-service-cli-extra")]
262
    pub fn validate_entry_integrity(&self, entry: &KeystoreEntry) -> Result<()> {
263
        let selector = entry.keystore_id().into();
264
        let store = self.select_keystore(&selector)?;
265
        // Ignore the parsed key, only checking if it parses correctly
266
        let _ = store.get(entry.key_path(), entry.key_type())?;
267

            
268
        // TODO: Implement `describe()` support for CTor keystore entries
269
        if !matches!(entry.key_path(), KeyPath::CTor(_)) {
270
            // Ignore the result, just checking if the path is recognized
271
            let _ = self
272
                .describe(entry.key_path())
273
                // TODO: `Error::Corruption` might not be the best fit for this situation.
274
                .map_err(|e| Error::Corruption(e.into()))?;
275
        }
276

            
277
        Ok(())
278
    }
279

            
280
    /// Generate a new key of type `K`, and insert it into the key store specified by `selector`.
281
    ///
282
    /// If the key already exists in the specified key store, the `overwrite` flag is used to
283
    /// decide whether to overwrite it with a newly generated key.
284
    ///
285
    /// On success, this function returns the newly generated key.
286
    ///
287
    /// Returns [`Error::KeyAlreadyExists`] if the key already exists in the specified
288
    /// key store and `overwrite` is `false`.
289
    ///
290
    /// **IMPORTANT**: using this function concurrently with any other `KeyMgr` operation that
291
    /// mutates the key store state is **not** recommended, as it can yield surprising results! The
292
    /// outcome of [`KeyMgr::generate`] depends on whether the selected key store
293
    /// [`contains`][crate::Keystore::contains] the specified key, and thus suffers from a TOCTOU race.
294
    //
295
    // TODO (#1119): can we make this less racy without a lock? Perhaps we should say we'll always
296
    // overwrite any existing keys.
297
    //
298
    // TODO: consider replacing the overwrite boolean with a GenerateOptions type
299
    // (sort of like std::fs::OpenOptions)
300
212
    pub fn generate<K>(
301
212
        &self,
302
212
        key_spec: &dyn KeySpecifier,
303
212
        selector: KeystoreSelector,
304
212
        rng: &mut dyn KeygenRng,
305
212
        overwrite: bool,
306
212
    ) -> Result<K>
307
212
    where
308
212
        K: ToEncodableKey,
309
212
        K::Key: Keygen,
310
    {
311
212
        let store = self.select_keystore(&selector)?;
312

            
313
212
        if overwrite || !store.contains(key_spec, &K::Key::item_type())? {
314
210
            let key = K::Key::generate(rng)?;
315
210
            store.insert(&key, key_spec)?;
316

            
317
210
            Ok(K::from_encodable_key(key))
318
        } else {
319
2
            Err(crate::Error::KeyAlreadyExists)
320
        }
321
212
    }
322

            
323
    /// Insert `key` into the [`Keystore`](crate::Keystore) specified by `selector`.
324
    ///
325
    /// If the key already exists in the specified key store, the `overwrite` flag is used to
326
    /// decide whether to overwrite it with the provided key.
327
    ///
328
    /// If this key is not already in the keystore, `None` is returned.
329
    ///
330
    /// If this key already exists in the keystore, its value is updated
331
    /// and the old value is returned.
332
    ///
333
    /// Returns an error if the selected keystore is not the primary keystore or one of the
334
    /// configured secondary stores.
335
96
    pub fn insert<K: ToEncodableKey>(
336
96
        &self,
337
96
        key: K,
338
96
        key_spec: &dyn KeySpecifier,
339
96
        selector: KeystoreSelector,
340
96
        overwrite: bool,
341
96
    ) -> Result<Option<K>> {
342
96
        let key = key.to_encodable_key();
343
96
        let store = self.select_keystore(&selector)?;
344
96
        let key_type = K::Key::item_type();
345
96
        let old_key: Option<K> = self.get_from_store(key_spec, &key_type, [store].into_iter())?;
346

            
347
96
        if old_key.is_some() && !overwrite {
348
2
            Err(crate::Error::KeyAlreadyExists)
349
        } else {
350
94
            let () = store.insert(&key, key_spec)?;
351
94
            Ok(old_key)
352
        }
353
96
    }
354

            
355
    /// Remove the key identified by `key_spec` from the [`Keystore`](crate::Keystore)
356
    /// specified by `selector`.
357
    ///
358
    /// Returns an error if the selected keystore is not the primary keystore or one of the
359
    /// configured secondary stores.
360
    ///
361
    /// Returns the value of the removed key,
362
    /// or `Ok(None)` if the key does not exist in the requested keystore.
363
    ///
364
    /// Returns `Err` if an error occurred while trying to remove the key.
365
40
    pub fn remove<K: ToEncodableKey>(
366
40
        &self,
367
40
        key_spec: &dyn KeySpecifier,
368
40
        selector: KeystoreSelector,
369
40
    ) -> Result<Option<K>> {
370
40
        let store = self.select_keystore(&selector)?;
371
38
        let key_type = K::Key::item_type();
372
38
        let old_key: Option<K> = self.get_from_store(key_spec, &key_type, [store].into_iter())?;
373

            
374
38
        store.remove(key_spec, &key_type)?;
375

            
376
38
        Ok(old_key)
377
40
    }
378

            
379
    /// Remove the specified keystore entry.
380
    ///
381
    /// Like [`KeyMgr::remove`], except this function does not return the value of the removed key.
382
    ///
383
    /// A return value of `Ok(None)` indicates the key was not found in the specified key store,
384
    /// whereas `Ok(Some(())` means the key was successfully removed.
385
    //
386
    // TODO: We should be consistent and return the removed key.
387
    //
388
    // This probably will involve changing the return type of Keystore::remove
389
    // to Result<Option<ErasedKey>>.
390
1129
    pub fn remove_entry(&self, entry: &KeystoreEntry) -> Result<Option<()>> {
391
1129
        let selector = entry.keystore_id().into();
392
1129
        let store = self.select_keystore(&selector)?;
393

            
394
1129
        store.remove(entry.key_path(), entry.key_type())
395
1129
    }
396

            
397
    /// Remove the specified keystore entry.
398
    ///
399
    /// Similar to [`KeyMgr::remove_entry`], except this method accepts both recognized and
400
    /// unrecognized entries, identified by a raw id (in the form of a `&str`) and a
401
    /// [`KeystoreId`].
402
    ///
403
    /// Returns an error if the entry could not be removed, or if the entry doesn't exist.
404
    #[cfg(feature = "onion-service-cli-extra")]
405
4
    pub fn remove_unchecked(&self, raw_id: &str, keystore_id: &KeystoreId) -> Result<()> {
406
4
        let selector = KeystoreSelector::from(keystore_id);
407
4
        let store = self.select_keystore(&selector)?;
408
4
        let raw_id = store.raw_entry_id(raw_id)?;
409
4
        let store = self.select_keystore(&selector)?;
410
4
        store.remove_unchecked(&raw_id)
411
4
    }
412

            
413
    /// Return the keystore entry descriptors of the keys matching the specified [`KeyPathPattern`].
414
    ///
415
    /// NOTE: This searches for matching keys in _all_ keystores.
416
    ///
417
    /// NOTE: This function only returns the *recognized* entries that match the provided pattern.
418
    /// The unrecognized entries (i.e. those that do not have a valid [`KeyPath`]) will be filtered out,
419
    /// even if they match the specified pattern.
420
814
    pub fn list_matching(&self, pat: &KeyPathPattern) -> Result<Vec<KeystoreEntry>> {
421
814
        self.all_stores()
422
1108
            .map(|store| -> Result<Vec<_>> {
423
1088
                Ok(store
424
1088
                    .list()?
425
1088
                    .into_iter()
426
5948
                    .filter_map(|entry| entry.ok())
427
5408
                    .filter(|entry| entry.key_path().matches(pat))
428
1088
                    .collect::<Vec<_>>())
429
1088
            })
430
814
            .flatten_ok()
431
814
            .collect::<Result<Vec<_>>>()
432
814
    }
433

            
434
    /// List keys and certificates of the specified keystore.
435
    #[cfg(feature = "onion-service-cli-extra")]
436
184
    pub fn list_by_id(&self, id: &KeystoreId) -> Result<Vec<KeystoreEntryResult<KeystoreEntry>>> {
437
184
        self.find_keystore(id)?.list()
438
184
    }
439

            
440
    /// List keys and certificates of all the keystores.
441
    #[cfg(feature = "onion-service-cli-extra")]
442
137
    pub fn list(&self) -> Result<Vec<KeystoreEntryResult<KeystoreEntry>>> {
443
137
        self.all_stores()
444
235
            .map(|store| -> Result<Vec<_>> { store.list() })
445
137
            .flatten_ok()
446
137
            .collect::<Result<Vec<_>>>()
447
137
    }
448

            
449
    /// List keys and certificates of a specific keystore.
450
    #[cfg(feature = "onion-service-cli-extra")]
451
137
    pub fn list_keystores(&self) -> Vec<KeystoreId> {
452
137
        self.all_stores()
453
190
            .map(|store| store.id().to_owned())
454
137
            .collect()
455
137
    }
456

            
457
    /// Describe the specified key.
458
    ///
459
    /// Returns [`KeyPathError::Unrecognized`] if none of the registered
460
    /// [`KeyPathInfoExtractor`]s is able to parse the specified [`KeyPath`].
461
    ///
462
    /// This function uses the [`KeyPathInfoExtractor`]s registered using
463
    /// [`register_key_info_extractor`](crate::register_key_info_extractor),
464
    /// or by [`DefaultKeySpecifier`](crate::derive_deftly_template_KeySpecifier).
465
630
    pub fn describe(&self, path: &KeyPath) -> StdResult<KeyPathInfo, KeyPathError> {
466
3330
        for info_extractor in &self.key_info_extractors {
467
3240
            if let Ok(info) = info_extractor.describe(path) {
468
540
                return Ok(info);
469
2700
            }
470
        }
471

            
472
90
        Err(KeyPathError::Unrecognized(path.clone()))
473
630
    }
474

            
475
    /// Attempt to retrieve a key from one of the specified `stores`.
476
    ///
477
    /// Returns the `<K as ToEncodableKey>::Key` representation of the key.
478
    ///
479
    /// See [`KeyMgr::get`] for more details.
480
2008
    fn get_from_store_raw<'a, K: ItemType>(
481
2008
        &self,
482
2008
        key_spec: &dyn KeySpecifier,
483
2008
        key_type: &KeystoreItemType,
484
2008
        stores: impl Iterator<Item = &'a BoxedKeystore>,
485
2008
    ) -> Result<Option<K>> {
486
2008
        let static_key_type = K::item_type();
487
2008
        if key_type != &static_key_type {
488
            return Err(internal!(
489
                "key type {:?} does not match the key type {:?} of requested key K::Key",
490
                key_type,
491
                static_key_type
492
            )
493
            .into());
494
2008
        }
495

            
496
2816
        for store in stores {
497
2142
            let key = match store.get(key_spec, &K::item_type()) {
498
                Ok(None) => {
499
                    // The key doesn't exist in this store, so we check the next one...
500
808
                    continue;
501
                }
502
1334
                Ok(Some(k)) => k,
503
                Err(e) => {
504
                    // Note: we immediately return if one of the keystores is inaccessible.
505
                    return Err(e);
506
                }
507
            };
508

            
509
            // Found it! Now try to downcast it to the right type (this should _not_ fail)...
510
1334
            let key: K = key
511
1334
                .downcast::<K>()
512
1334
                .map(|k| *k)
513
1334
                .map_err(|_| internal!("failed to downcast key to requested type"))?;
514

            
515
1334
            return Ok(Some(key));
516
        }
517

            
518
674
        Ok(None)
519
2008
    }
520

            
521
    /// Attempt to retrieve a key from one of the specified `stores`.
522
    ///
523
    /// See [`KeyMgr::get`] for more details.
524
1790
    fn get_from_store<'a, K: ToEncodableKey>(
525
1790
        &self,
526
1790
        key_spec: &dyn KeySpecifier,
527
1790
        key_type: &KeystoreItemType,
528
1790
        stores: impl Iterator<Item = &'a BoxedKeystore>,
529
1790
    ) -> Result<Option<K>> {
530
1790
        let Some(key) = self.get_from_store_raw::<K::Key>(key_spec, key_type, stores)? else {
531
576
            return Ok(None);
532
        };
533

            
534
1214
        Ok(Some(K::from_encodable_key(key)))
535
1790
    }
536

            
537
    /// Read the specified key and certificate from one of the key stores,
538
    /// deserializing the subject key as `K::Key`, the cert as `C::Cert`,
539
    /// and the signing key as `C::SigningKey`.
540
    ///
541
    /// Returns `Ok(None)` if none of the key stores have the requested key.
542
    ///
543
    // Note: the behavior of this function is a bit inconsistent with
544
    // get_or_generate_key_and_cert: here, if the cert is absent but
545
    // its subject key is not, we return Ok(None).
546
    // In get_or_generate_key_and_cert, OTOH< we return an error in that case
547
    // (because we can't possibly generate the missing subject key
548
    // without overwriting the cert of the missing key).
549
    ///
550
    /// This function validates the certificate using [`ToEncodableCert::validate`],
551
    /// returning an error if it is invalid or missing.
552
    #[cfg(feature = "experimental-api")]
553
    pub fn get_key_and_cert<K, C>(
554
        &self,
555
        spec: &dyn KeyCertificateSpecifier,
556
    ) -> Result<Option<(K, C)>>
557
    where
558
        K: ToEncodableKey,
559
        C: ToEncodableCert<K>,
560
    {
561
        let subject_key_spec = spec.subject_key_specifier();
562
        // Get the subject key...
563
        let Some(key) =
564
            self.get_from_store::<K>(subject_key_spec, &K::Key::item_type(), self.all_stores())?
565
        else {
566
            return Ok(None);
567
        };
568

            
569
        let subject_key_arti_path = subject_key_spec
570
            .arti_path()
571
            .map_err(|_| bad_api_usage!("subject key does not have an ArtiPath?!"))?;
572
        let cert_spec =
573
            ArtiPath::from_path_and_denotators(subject_key_arti_path, &spec.cert_denotators())
574
                .map_err(into_bad_api_usage!("invalid certificate specifier"))?;
575

            
576
        let Some(cert) = self.get_from_store_raw::<C::ParsedCert>(
577
            &cert_spec,
578
            &<C::ParsedCert as ItemType>::item_type(),
579
            self.all_stores(),
580
        )?
581
        else {
582
            return Err(KeystoreCorruptionError::MissingCertificate.into());
583
        };
584

            
585
        // Finally, get the signing key and validate the cert
586
        let signed_with = self.get_cert_signing_key::<K, C>(spec)?;
587
        let cert = C::validate(cert, &key, &signed_with)?;
588

            
589
        Ok(Some((key, cert)))
590
    }
591

            
592
    /// Like [`KeyMgr::get_key_and_cert`], except this function also generates the subject key
593
    /// and its corresponding certificate if they don't already exist.
594
    ///
595
    /// If the key certificate is missing, it will be generated
596
    /// from the subject key and signing key using the provided `make_certificate` callback.
597
    ///
598
    /// Generates the missing key and/or certificate as follows:
599
    ///
600
    /// ```text
601
    /// | Subject Key exists | Signing Key exists | Cert exists | Action                                 |
602
    /// |--------------------|--------------------|-------------|----------------------------------------|
603
    /// | Y                  | Y                  | Y           | Validate cert, return key and cert     |
604
    /// |                    |                    |             | if valid, error otherwise              |
605
    /// |--------------------|--------------------|-------------|----------------------------------------|
606
    /// | N                  | Y                  | N           | Generate subject key and               |
607
    /// |                    |                    |             | a new cert signed with signing key     |
608
    /// |--------------------|--------------------|-------------|----------------------------------------|
609
    /// | Y                  | Y                  | N           | Generate cert signed with signing key  |
610
    /// |--------------------|--------------------|-------------|----------------------------------------|
611
    /// | Y                  | N                  | N           | Error - cannot validate cert           |
612
    /// |                    |                    |             | if signing key is not available        |
613
    /// |--------------------|--------------------|-------------|----------------------------------------|
614
    /// | Y/N                | N                  | N           | Error - cannot generate cert           |
615
    /// |                    |                    |             | if signing key is not available        |
616
    /// |--------------------|--------------------|-------------|----------------------------------------|
617
    /// | N                  | Y/N                | Y           | Error - subject key was removed?       |
618
    /// |                    |                    |             | (we found the cert,                    |
619
    /// |                    |                    |             | but the subject key is missing)        |
620
    /// ```
621
    ///
622
    //
623
    // Note; the table above isn't a markdown table because CommonMark-flavor markdown
624
    // doesn't support multiline text in tables. Even if we trim down the text,
625
    // the resulting markdown table would be pretty unreadable in raw form
626
    // (it would have several excessively long lines, over 120 chars in len).
627
    #[cfg(feature = "experimental-api")]
628
8
    pub fn get_or_generate_key_and_cert<K, C>(
629
8
        &self,
630
8
        spec: &dyn KeyCertificateSpecifier,
631
8
        make_certificate: impl FnOnce(&K, &<C as ToEncodableCert<K>>::SigningKey) -> C,
632
8
        selector: KeystoreSelector,
633
8
        rng: &mut dyn KeygenRng,
634
8
    ) -> Result<(K, C)>
635
8
    where
636
8
        K: ToEncodableKey,
637
8
        K::Key: Keygen,
638
8
        C: ToEncodableCert<K>,
639
    {
640
8
        let subject_key_spec = spec.subject_key_specifier();
641
8
        let subject_key_arti_path = subject_key_spec
642
8
            .arti_path()
643
8
            .map_err(|_| bad_api_usage!("subject key does not have an ArtiPath?!"))?;
644

            
645
8
        let cert_specifier =
646
8
            ArtiPath::from_path_and_denotators(subject_key_arti_path, &spec.cert_denotators())
647
8
                .map_err(into_bad_api_usage!("invalid certificate specifier"))?;
648

            
649
8
        let maybe_cert = self.get_from_store_raw::<C::ParsedCert>(
650
8
            &cert_specifier,
651
8
            &C::ParsedCert::item_type(),
652
8
            self.all_stores(),
653
        )?;
654

            
655
8
        let maybe_subject_key = self.get::<K>(subject_key_spec)?;
656

            
657
8
        match (&maybe_cert, &maybe_subject_key) {
658
            (Some(_), None) => {
659
                return Err(KeystoreCorruptionError::MissingSubjectKey.into());
660
            }
661
8
            _ => {
662
8
                // generate key and/or cert
663
8
            }
664
        }
665
8
        let subject_key = match maybe_subject_key {
666
4
            Some(key) => key,
667
4
            _ => self.generate(subject_key_spec, selector, rng, false)?,
668
        };
669

            
670
8
        let signed_with = self.get_cert_signing_key::<K, C>(spec)?;
671
4
        let cert = match maybe_cert {
672
            Some(cert) => C::validate(cert, &subject_key, &signed_with)?,
673
            None => {
674
4
                let cert = make_certificate(&subject_key, &signed_with);
675

            
676
4
                let () = self.insert_cert(cert.clone(), &cert_specifier, selector)?;
677

            
678
4
                cert
679
            }
680
        };
681

            
682
4
        Ok((subject_key, cert))
683
8
    }
684

            
685
    /// Return an iterator over all configured stores.
686
26254
    fn all_stores(&self) -> impl Iterator<Item = &BoxedKeystore> {
687
26254
        iter::once(&self.primary_store).chain(self.secondary_stores.iter())
688
26254
    }
689

            
690
    /// Return the [`Keystore`](crate::Keystore) matching the specified `selector`.
691
    ///
692
    /// Returns an error if the selected keystore is not the primary keystore or one of the
693
    /// configured secondary stores.
694
4042
    fn select_keystore(&self, selector: &KeystoreSelector) -> Result<&BoxedKeystore> {
695
4042
        match selector {
696
1500
            KeystoreSelector::Id(keystore_id) => self.find_keystore(keystore_id),
697
2542
            KeystoreSelector::Primary => Ok(&self.primary_store),
698
        }
699
4042
    }
700

            
701
    /// Return the [`Keystore`](crate::Keystore) with the specified `id`.
702
    ///
703
    /// Returns an error if the specified ID is not the ID of the primary keystore or
704
    /// the ID of one of the configured secondary stores.
705
1956
    fn find_keystore(&self, id: &KeystoreId) -> Result<&BoxedKeystore> {
706
1956
        self.all_stores()
707
2408
            .find(|keystore| keystore.id() == id)
708
1958
            .ok_or_else(|| crate::Error::KeystoreNotFound(id.clone()))
709
1956
    }
710

            
711
    /// Get the signing key of the certificate described by `spec`.
712
    ///
713
    /// Returns a [`KeystoreCorruptionError::MissingSigningKey`] error
714
    /// if the signing key doesn't exist in any of the keystores.
715
    #[cfg(feature = "experimental-api")]
716
8
    fn get_cert_signing_key<K, C>(
717
8
        &self,
718
8
        spec: &dyn KeyCertificateSpecifier,
719
8
    ) -> Result<C::SigningKey>
720
8
    where
721
8
        K: ToEncodableKey,
722
8
        C: ToEncodableCert<K>,
723
    {
724
8
        let Some(signing_key_spec) = spec.signing_key_specifier() else {
725
            return Err(bad_api_usage!(
726
                "signing key specifier is None, but external signing key was not provided?"
727
            )
728
            .into());
729
        };
730

            
731
8
        let Some(signing_key) = self.get_from_store::<C::SigningKey>(
732
8
            signing_key_spec,
733
8
            &<C::SigningKey as ToEncodableKey>::Key::item_type(),
734
8
            self.all_stores(),
735
        )?
736
        else {
737
4
            return Err(KeystoreCorruptionError::MissingSigningKey.into());
738
        };
739

            
740
4
        Ok(signing_key)
741
8
    }
742

            
743
    /// Insert `cert` into the [`Keystore`](crate::Keystore) specified by `selector`.
744
    ///
745
    /// If the key already exists in the specified key store, it will be overwritten.
746
    ///
747
    // NOTE: if we ever make this public we should rethink/improve its API.
748
    // TODO: maybe fold this into insert() somehow?
749
4
    fn insert_cert<K, C>(
750
4
        &self,
751
4
        cert: C,
752
4
        cert_spec: &dyn KeySpecifier,
753
4
        selector: KeystoreSelector,
754
4
    ) -> Result<()>
755
4
    where
756
4
        K: ToEncodableKey,
757
4
        K::Key: Keygen,
758
4
        C: ToEncodableCert<K>,
759
    {
760
4
        let cert = cert.to_encodable_cert();
761
4
        let store = self.select_keystore(&selector)?;
762

            
763
4
        let () = store.insert(&cert, cert_spec)?;
764
4
        Ok(())
765
4
    }
766
}
767

            
768
#[cfg(test)]
769
mod tests {
770
    // @@ begin test lint list maintained by maint/add_warning @@
771
    #![allow(clippy::bool_assert_comparison)]
772
    #![allow(clippy::clone_on_copy)]
773
    #![allow(clippy::dbg_macro)]
774
    #![allow(clippy::mixed_attributes_style)]
775
    #![allow(clippy::print_stderr)]
776
    #![allow(clippy::print_stdout)]
777
    #![allow(clippy::single_char_pattern)]
778
    #![allow(clippy::unwrap_used)]
779
    #![allow(clippy::unchecked_duration_subtraction)]
780
    #![allow(clippy::useless_vec)]
781
    #![allow(clippy::needless_pass_by_value)]
782
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
783
    use super::*;
784
    use crate::keystore::arti::err::{ArtiNativeKeystoreError, MalformedPathError};
785
    use crate::raw::{RawEntryId, RawKeystoreEntry};
786
    use crate::{
787
        ArtiPath, ArtiPathUnavailableError, Error, KeyPath, KeystoreEntryResult, KeystoreError,
788
        UnrecognizedEntryError,
789
    };
790
    use std::path::PathBuf;
791
    use std::result::Result as StdResult;
792
    use std::str::FromStr;
793
    use std::sync::{Arc, RwLock};
794
    use std::time::{Duration, SystemTime};
795
    use tor_basic_utils::test_rng::testing_rng;
796
    use tor_cert::CertifiedKey;
797
    use tor_cert::Ed25519Cert;
798
    use tor_error::{ErrorKind, HasKind};
799
    use tor_key_forge::{
800
        CertData, EncodableItem, ErasedKey, InvalidCertError, KeyType, KeystoreItem,
801
    };
802
    use tor_llcrypto::pk::ed25519::{self, Ed25519PublicKey as _};
803
    use tor_llcrypto::rng::FakeEntropicRng;
804

            
805
    /// Metadata structure for tracking key operations in tests.
806
    #[derive(Clone, Debug, PartialEq)]
807
    struct KeyMetadata {
808
        /// The identifier for the item (e.g., "coot", "moorhen").
809
        item_id: String,
810
        /// The keystore from which the item was retrieved.
811
        ///
812
        /// Set by `Keystore::get`.
813
        retrieved_from: Option<KeystoreId>,
814
        /// Whether the item was generated via `Keygen::generate`.
815
        is_generated: bool,
816
    }
817

            
818
    /// Metadata structure for tracking certificate operations in tests.
819
    #[derive(Clone, Debug, PartialEq)]
820
    struct CertMetadata {
821
        /// The identifier for the subject key (e.g., "coot").
822
        subject_key_id: String,
823
        /// The identifier for the signing key (e.g., "moorhen").
824
        signing_key_id: String,
825
        /// The keystore from which the certificate was retrieved.
826
        ///
827
        /// Set by `Keystore::get`.
828
        retrieved_from: Option<KeystoreId>,
829
        /// Whether the certificate was freshly generated (i.e. returned from the "or generate"
830
        /// branch of `get_or_generate()`) or retrieved from a keystore.
831
        is_generated: bool,
832
    }
833

            
834
    /// Metadata structure for tracking item operations in tests.
835
    #[derive(Clone, Debug, PartialEq, derive_more::From)]
836
    enum ItemMetadata {
837
        /// Metadata about a key.
838
        Key(KeyMetadata),
839
        /// Metadata about a certificate.
840
        Cert(CertMetadata),
841
    }
842

            
843
    impl ItemMetadata {
844
        /// Get the item ID.
845
        ///
846
        /// For keys, this returns the key's ID.
847
        /// For certificates, this returns a formatted string identifying the subject key.
848
        fn item_id(&self) -> &str {
849
            match self {
850
                ItemMetadata::Key(k) => &k.item_id,
851
                ItemMetadata::Cert(c) => &c.subject_key_id,
852
            }
853
        }
854

            
855
        /// Get retrieved_from.
856
        fn retrieved_from(&self) -> Option<&KeystoreId> {
857
            match self {
858
                ItemMetadata::Key(k) => k.retrieved_from.as_ref(),
859
                ItemMetadata::Cert(c) => c.retrieved_from.as_ref(),
860
            }
861
        }
862

            
863
        /// Get is_generated.
864
        fn is_generated(&self) -> bool {
865
            match self {
866
                ItemMetadata::Key(k) => k.is_generated,
867
                ItemMetadata::Cert(c) => c.is_generated,
868
            }
869
        }
870

            
871
        /// Set the retrieved_from field to the specified keystore ID.
872
        fn set_retrieved_from(&mut self, id: KeystoreId) {
873
            match self {
874
                ItemMetadata::Key(meta) => meta.retrieved_from = Some(id),
875
                ItemMetadata::Cert(meta) => meta.retrieved_from = Some(id),
876
            }
877
        }
878

            
879
        /// Returns a reference to key metadata if this is a Key variant.
880
        fn as_key(&self) -> Option<&KeyMetadata> {
881
            match self {
882
                ItemMetadata::Key(meta) => Some(meta),
883
                _ => None,
884
            }
885
        }
886

            
887
        /// Returns a reference to certificate metadata if this is a Cert variant.
888
        fn as_cert(&self) -> Option<&CertMetadata> {
889
            match self {
890
                ItemMetadata::Cert(meta) => Some(meta),
891
                _ => None,
892
            }
893
        }
894
    }
895

            
896
    /// The type of "key" stored in the test key stores.
897
    #[derive(Clone, Debug)]
898
    struct TestItem {
899
        /// The underlying key.
900
        item: KeystoreItem,
901
        /// Metadata about the key.
902
        meta: ItemMetadata,
903
    }
904

            
905
    /// A "certificate" used for testing purposes.
906
    #[derive(Clone, Debug)]
907
    struct AlwaysValidCert(TestItem);
908

            
909
    /// The corresponding fake public key type.
910
    #[derive(Clone, Debug)]
911
    struct TestPublicKey {
912
        /// The underlying key.
913
        key: KeystoreItem,
914
    }
915

            
916
    impl From<TestItem> for TestPublicKey {
917
        fn from(tk: TestItem) -> TestPublicKey {
918
            TestPublicKey { key: tk.item }
919
        }
920
    }
921

            
922
    impl TestItem {
923
        /// Create a new test key with the specified metadata.
924
        fn new(item_id: &str) -> Self {
925
            let mut rng = testing_rng();
926
            TestItem {
927
                item: ed25519::Keypair::generate(&mut rng)
928
                    .as_keystore_item()
929
                    .unwrap(),
930
                meta: ItemMetadata::Key(KeyMetadata {
931
                    item_id: item_id.to_string(),
932
                    retrieved_from: None,
933
                    is_generated: false,
934
                }),
935
            }
936
        }
937
    }
938

            
939
    impl Keygen for TestItem {
940
        fn generate(mut rng: &mut dyn KeygenRng) -> tor_key_forge::Result<Self>
941
        where
942
            Self: Sized,
943
        {
944
            Ok(TestItem {
945
                item: ed25519::Keypair::generate(&mut rng).as_keystore_item()?,
946
                meta: ItemMetadata::Key(KeyMetadata {
947
                    item_id: "generated_test_key".to_string(),
948
                    retrieved_from: None,
949
                    is_generated: true,
950
                }),
951
            })
952
        }
953
    }
954

            
955
    impl ItemType for TestItem {
956
        fn item_type() -> KeystoreItemType
957
        where
958
            Self: Sized,
959
        {
960
            // Dummy value
961
            KeyType::Ed25519Keypair.into()
962
        }
963
    }
964

            
965
    impl EncodableItem for TestItem {
966
        fn as_keystore_item(&self) -> tor_key_forge::Result<KeystoreItem> {
967
            Ok(self.item.clone())
968
        }
969
    }
970

            
971
    impl ToEncodableKey for TestItem {
972
        type Key = Self;
973
        type KeyPair = Self;
974

            
975
        fn to_encodable_key(self) -> Self::Key {
976
            self
977
        }
978

            
979
        fn from_encodable_key(key: Self::Key) -> Self {
980
            key
981
        }
982
    }
983

            
984
    impl ItemType for TestPublicKey {
985
        fn item_type() -> KeystoreItemType
986
        where
987
            Self: Sized,
988
        {
989
            KeyType::Ed25519PublicKey.into()
990
        }
991
    }
992

            
993
    impl EncodableItem for TestPublicKey {
994
        fn as_keystore_item(&self) -> tor_key_forge::Result<KeystoreItem> {
995
            Ok(self.key.clone())
996
        }
997
    }
998

            
999
    impl ToEncodableKey for TestPublicKey {
        type Key = Self;
        type KeyPair = TestItem;
        fn to_encodable_key(self) -> Self::Key {
            self
        }
        fn from_encodable_key(key: Self::Key) -> Self {
            key
        }
    }
    impl ToEncodableCert<TestItem> for AlwaysValidCert {
        type ParsedCert = TestItem;
        type EncodableCert = TestItem;
        type SigningKey = TestItem;
        fn validate(
            cert: Self::ParsedCert,
            _subject: &TestItem,
            _signed_with: &Self::SigningKey,
        ) -> StdResult<Self, InvalidCertError> {
            // AlwaysValidCert is always valid
            Ok(Self(cert))
        }
        /// Convert this cert to a type that implements [`EncodableKey`].
        fn to_encodable_cert(self) -> Self::EncodableCert {
            self.0
        }
    }
    #[derive(thiserror::Error, Debug, Clone, derive_more::Display)]
    enum MockKeystoreError {
        NotFound,
    }
    impl KeystoreError for MockKeystoreError {}
    impl HasKind for MockKeystoreError {
        fn kind(&self) -> ErrorKind {
            // Return a dummy ErrorKind for the purposes of this test
            tor_error::ErrorKind::Other
        }
    }
    fn build_raw_id_path<T: ToString>(key_path: &T, key_type: &KeystoreItemType) -> RawEntryId {
        let mut path = key_path.to_string();
        path.push('.');
        path.push_str(&key_type.arti_extension());
        RawEntryId::Path(PathBuf::from(&path))
    }
    macro_rules! impl_keystore {
        ($name:tt, $id:expr $(,$unrec:expr)?) => {
            struct $name {
                inner: RwLock<
                    Vec<StdResult<(ArtiPath, KeystoreItemType, TestItem), UnrecognizedEntryError>>,
                >,
                id: KeystoreId,
            }
            impl Default for $name {
                fn default() -> Self {
                    let id = KeystoreId::from_str($id).unwrap();
                    let inner: RwLock<
                        Vec<
                            StdResult<
                                (ArtiPath, KeystoreItemType, TestItem),
                                UnrecognizedEntryError,
                            >,
                        >,
                    > = Default::default();
                    // Populate the Keystore with the specified number
                    // of unrecognized entries.
                    $(
                        for i in 0..$unrec {
                            let invalid_key_path =
                                PathBuf::from(&format!("unrecognized_entry{}", i));
                            let raw_id = RawEntryId::Path(invalid_key_path.clone());
                            let entry = RawKeystoreEntry::new(raw_id, id.clone()).into();
                            let entry = UnrecognizedEntryError::new(
                                entry,
                                Arc::new(ArtiNativeKeystoreError::MalformedPath {
                                    path: invalid_key_path,
                                    err: MalformedPathError::NoExtension,
                                }),
                            );
                            inner.write().unwrap().push(Err(entry));
                        }
                    )?
                    Self {
                        inner,
                        id,
                    }
                }
            }
            #[allow(dead_code)] // this is only dead code for Keystore1
            impl $name {
                fn new_boxed() -> BoxedKeystore {
                    Box::<Self>::default()
                }
            }
            impl crate::Keystore for $name {
                fn contains(
                    &self,
                    key_spec: &dyn KeySpecifier,
                    item_type: &KeystoreItemType,
                ) -> Result<bool> {
                    let wanted_arti_path = key_spec.arti_path().unwrap();
                    Ok(self
                        .inner
                        .read()
                        .unwrap()
                        .iter()
                        .find(|res| match res {
                            Ok((spec, ty, _)) => spec == &wanted_arti_path && ty == item_type,
                            Err(_) => false,
                        })
                        .is_some())
                }
                fn id(&self) -> &KeystoreId {
                    &self.id
                }
                fn get(
                    &self,
                    key_spec: &dyn KeySpecifier,
                    item_type: &KeystoreItemType,
                ) -> Result<Option<ErasedKey>> {
                    let key_spec = key_spec.arti_path().unwrap();
                    Ok(self.inner.read().unwrap().iter().find_map(|res| {
                        match res {
                            Ok((arti_path, ty, k)) => {
                                if arti_path == &key_spec && ty == item_type {
                                    let mut k = k.clone();
                                    k.meta.set_retrieved_from(self.id().clone());
                                    return Some(Box::new(k) as Box<dyn ItemType>);
                                }
                            }
                            Err(_) => {}
                        }
                        None
                    }))
                }
                #[cfg(feature = "onion-service-cli-extra")]
                fn raw_entry_id(&self, raw_id: &str) -> Result<RawEntryId> {
                    Ok(RawEntryId::Path(
                        PathBuf::from(raw_id.to_string()),
                    ))
                }
                fn insert(
                    &self,
                    key: &dyn EncodableItem,
                    key_spec: &dyn KeySpecifier,
                ) -> Result<()> {
                    let key = key.downcast_ref::<TestItem>().unwrap();
                    let item = key.as_keystore_item()?;
                    let meta = key.meta.clone();
                    let item_type = item.item_type()?;
                    let key = TestItem { item, meta };
                    self.inner
                        .write()
                        .unwrap()
                        // TODO: `insert` is used instead of `push`, because some of the
                        // tests (mainly `insert_and_get` and `keygen`) fail otherwise.
                        // It could be a good idea to use `push` and adapt the tests,
                        // in order to reduce cognitive complexity.
                        .insert(0, (Ok((key_spec.arti_path().unwrap(), item_type, key))));
                    Ok(())
                }
                fn remove(
                    &self,
                    key_spec: &dyn KeySpecifier,
                    item_type: &KeystoreItemType,
                ) -> Result<Option<()>> {
                    let wanted_arti_path = key_spec.arti_path().unwrap();
                    let index = self.inner.read().unwrap().iter().position(|res| {
                        if let Ok((arti_path, ty, _)) = res {
                            arti_path == &wanted_arti_path && ty == item_type
                        } else {
                            false
                        }
                    });
                    let Some(index) = index else {
                        return Ok(None);
                    };
                    let _ = self.inner.write().unwrap().remove(index);
                    Ok(Some(()))
                }
                #[cfg(feature = "onion-service-cli-extra")]
                fn remove_unchecked(&self, entry_id: &RawEntryId) -> Result<()> {
                    let index = self.inner.read().unwrap().iter().position(|res| match res {
                        Ok((spec, ty, _)) => {
                            let id = build_raw_id_path(spec, ty);
                            entry_id == &id
                        }
                        Err(e) => {
                            e.entry().raw_id() == entry_id
                        }
                    });
                    let Some(index) = index else {
                        return Err(Error::Keystore(Arc::new(MockKeystoreError::NotFound)));
                    };
                    let _ = self.inner.write().unwrap().remove(index);
                    Ok(())
                }
                fn list(&self) -> Result<Vec<KeystoreEntryResult<KeystoreEntry>>> {
                    Ok(self
                        .inner
                        .read()
                        .unwrap()
                        .iter()
                        .map(|res| match res {
                            Ok((arti_path, ty, _)) => {
                                let raw_id = RawEntryId::Path(
                                    PathBuf::from(
                                        &arti_path.to_string(),
                                    )
                                );
                                Ok(KeystoreEntry::new(KeyPath::Arti(arti_path.clone()), ty.clone(), self.id(), raw_id))
                            }
                            Err(e) => Err(e.clone()),
                        })
                        .collect())
                }
            }
        };
    }
    macro_rules! impl_specifier {
        ($name:tt, $id:expr) => {
            struct $name;
            impl KeySpecifier for $name {
                fn arti_path(&self) -> StdResult<ArtiPath, ArtiPathUnavailableError> {
                    Ok(ArtiPath::new($id.into()).map_err(|e| tor_error::internal!("{e}"))?)
                }
                fn ctor_path(&self) -> Option<crate::CTorPath> {
                    None
                }
                fn keypair_specifier(&self) -> Option<Box<dyn KeySpecifier>> {
                    None
                }
            }
        };
    }
    impl_keystore!(Keystore1, "keystore1");
    impl_keystore!(Keystore2, "keystore2");
    impl_keystore!(Keystore3, "keystore3");
    impl_keystore!(KeystoreUnrec1, "keystore_unrec1", 1);
    impl_specifier!(TestKeySpecifier1, "spec1");
    impl_specifier!(TestKeySpecifier2, "spec2");
    impl_specifier!(TestKeySpecifier3, "spec3");
    impl_specifier!(TestKeySpecifier4, "spec4");
    impl_specifier!(TestPublicKeySpecifier1, "pub-spec1");
    /// Create a test `KeystoreEntry`.
    fn entry_descriptor(specifier: impl KeySpecifier, keystore_id: &KeystoreId) -> KeystoreEntry {
        let arti_path = specifier.arti_path().unwrap();
        let raw_id = RawEntryId::Path(PathBuf::from(arti_path.as_ref()));
        KeystoreEntry {
            key_path: arti_path.into(),
            key_type: TestItem::item_type(),
            keystore_id,
            raw_id,
        }
    }
    #[test]
    #[allow(clippy::cognitive_complexity)]
    fn insert_and_get() {
        let mut builder = KeyMgrBuilder::default().primary_store(Box::<Keystore1>::default());
        builder
            .secondary_stores()
            .extend([Keystore2::new_boxed(), Keystore3::new_boxed()]);
        let mgr = builder.build().unwrap();
        // Insert a key into Keystore2
        let old_key = mgr
            .insert(
                TestItem::new("coot"),
                &TestKeySpecifier1,
                KeystoreSelector::Id(&KeystoreId::from_str("keystore2").unwrap()),
                true,
            )
            .unwrap();
        assert!(old_key.is_none());
        let key = mgr.get::<TestItem>(&TestKeySpecifier1).unwrap().unwrap();
        assert_eq!(key.meta.item_id(), "coot");
        assert_eq!(
            key.meta.retrieved_from(),
            Some(&KeystoreId::from_str("keystore2").unwrap())
        );
        assert_eq!(key.meta.is_generated(), false);
        // Insert a different key using the _same_ key specifier.
        let old_key = mgr
            .insert(
                TestItem::new("gull"),
                &TestKeySpecifier1,
                KeystoreSelector::Id(&KeystoreId::from_str("keystore2").unwrap()),
                true,
            )
            .unwrap()
            .unwrap();
        assert_eq!(old_key.meta.item_id(), "coot");
        assert_eq!(
            old_key.meta.retrieved_from(),
            Some(&KeystoreId::from_str("keystore2").unwrap())
        );
        assert_eq!(old_key.meta.is_generated(), false);
        // Check that the original value was overwritten:
        let key = mgr.get::<TestItem>(&TestKeySpecifier1).unwrap().unwrap();
        assert_eq!(key.meta.item_id(), "gull");
        assert_eq!(
            key.meta.retrieved_from(),
            Some(&KeystoreId::from_str("keystore2").unwrap())
        );
        assert_eq!(key.meta.is_generated(), false);
        // Insert a different key using the _same_ key specifier (overwrite = false)
        let err = mgr
            .insert(
                TestItem::new("gull"),
                &TestKeySpecifier1,
                KeystoreSelector::Id(&KeystoreId::from_str("keystore2").unwrap()),
                false,
            )
            .unwrap_err();
        assert!(matches!(err, crate::Error::KeyAlreadyExists));
        // Insert a new key into Keystore2 (overwrite = false)
        let old_key = mgr
            .insert(
                TestItem::new("penguin"),
                &TestKeySpecifier2,
                KeystoreSelector::Id(&KeystoreId::from_str("keystore2").unwrap()),
                false,
            )
            .unwrap();
        assert!(old_key.is_none());
        // Insert a key into the primary keystore
        let old_key = mgr
            .insert(
                TestItem::new("moorhen"),
                &TestKeySpecifier3,
                KeystoreSelector::Primary,
                true,
            )
            .unwrap();
        assert!(old_key.is_none());
        let key = mgr.get::<TestItem>(&TestKeySpecifier3).unwrap().unwrap();
        assert_eq!(key.meta.item_id(), "moorhen");
        assert_eq!(
            key.meta.retrieved_from(),
            Some(&KeystoreId::from_str("keystore1").unwrap())
        );
        assert_eq!(key.meta.is_generated(), false);
        // The key doesn't exist in any of the stores yet.
        assert!(mgr.get::<TestItem>(&TestKeySpecifier4).unwrap().is_none());
        // Insert the same key into all 3 key stores, in reverse order of keystore priority
        // (otherwise KeyMgr::get will return the key from the primary store for each iteration and
        // we won't be able to see the key was actually inserted in each store).
        for store in ["keystore3", "keystore2", "keystore1"] {
            let old_key = mgr
                .insert(
                    TestItem::new("cormorant"),
                    &TestKeySpecifier4,
                    KeystoreSelector::Id(&KeystoreId::from_str(store).unwrap()),
                    true,
                )
                .unwrap();
            assert!(old_key.is_none());
            // Ensure the key now exists in `store`.
            let key = mgr.get::<TestItem>(&TestKeySpecifier4).unwrap().unwrap();
            assert_eq!(key.meta.item_id(), "cormorant");
            assert_eq!(
                key.meta.retrieved_from(),
                Some(&KeystoreId::from_str(store).unwrap())
            );
            assert_eq!(key.meta.is_generated(), false);
        }
        // The key exists in all key stores, but if no keystore_id is specified, we return the
        // value from the first key store it is found in (in this case, Keystore1)
        let key = mgr.get::<TestItem>(&TestKeySpecifier4).unwrap().unwrap();
        assert_eq!(key.meta.item_id(), "cormorant");
        assert_eq!(
            key.meta.retrieved_from(),
            Some(&KeystoreId::from_str("keystore1").unwrap())
        );
        assert_eq!(key.meta.is_generated(), false);
    }
    #[test]
    #[cfg(feature = "onion-service-cli-extra")]
    fn get_from() {
        let mut builder = KeyMgrBuilder::default().primary_store(Box::<Keystore1>::default());
        builder
            .secondary_stores()
            .extend([Keystore2::new_boxed(), Keystore3::new_boxed()]);
        let mgr = builder.build().unwrap();
        let keystore1_id = KeystoreId::from_str("keystore1").unwrap();
        let keystore2_id = KeystoreId::from_str("keystore2").unwrap();
        let key_id_1 = "mantis shrimp";
        let key_id_2 = "tardigrade";
        // Insert a key into Keystore1
        let _ = mgr
            .insert(
                TestItem::new(key_id_1),
                &TestKeySpecifier1,
                KeystoreSelector::Id(&keystore1_id),
                true,
            )
            .unwrap();
        // Insert a key into Keystore2
        let _ = mgr
            .insert(
                TestItem::new(key_id_2),
                &TestKeySpecifier1,
                KeystoreSelector::Id(&keystore2_id),
                true,
            )
            .unwrap();
        // Retrieve key
        let key = mgr
            .get_from::<TestItem>(&TestKeySpecifier1, &keystore2_id)
            .unwrap()
            .unwrap();
        assert_eq!(key.meta.item_id(), key_id_2);
        assert_eq!(key.meta.retrieved_from(), Some(&keystore2_id));
    }
    #[test]
    fn remove() {
        let mut builder = KeyMgrBuilder::default().primary_store(Box::<Keystore1>::default());
        builder
            .secondary_stores()
            .extend([Keystore2::new_boxed(), Keystore3::new_boxed()]);
        let mgr = builder.build().unwrap();
        assert!(
            !mgr.secondary_stores[0]
                .contains(&TestKeySpecifier1, &TestItem::item_type())
                .unwrap()
        );
        // Insert a key into Keystore2
        mgr.insert(
            TestItem::new("coot"),
            &TestKeySpecifier1,
            KeystoreSelector::Id(&KeystoreId::from_str("keystore2").unwrap()),
            true,
        )
        .unwrap();
        let key = mgr.get::<TestItem>(&TestKeySpecifier1).unwrap().unwrap();
        assert_eq!(key.meta.item_id(), "coot");
        assert_eq!(
            key.meta.retrieved_from(),
            Some(&KeystoreId::from_str("keystore2").unwrap())
        );
        assert_eq!(key.meta.is_generated(), false);
        // Try to remove the key from a non-existent key store
        assert!(
            mgr.remove::<TestItem>(
                &TestKeySpecifier1,
                KeystoreSelector::Id(&KeystoreId::from_str("not_an_id_we_know_of").unwrap())
            )
            .is_err()
        );
        // The key still exists in Keystore2
        assert!(
            mgr.secondary_stores[0]
                .contains(&TestKeySpecifier1, &TestItem::item_type())
                .unwrap()
        );
        // Try to remove the key from the primary key store
        assert!(
            mgr.remove::<TestItem>(&TestKeySpecifier1, KeystoreSelector::Primary)
                .unwrap()
                .is_none()
        );
        // The key still exists in Keystore2
        assert!(
            mgr.secondary_stores[0]
                .contains(&TestKeySpecifier1, &TestItem::item_type())
                .unwrap()
        );
        // Removing from Keystore2 should succeed.
        let removed_key = mgr
            .remove::<TestItem>(
                &TestKeySpecifier1,
                KeystoreSelector::Id(&KeystoreId::from_str("keystore2").unwrap()),
            )
            .unwrap()
            .unwrap();
        assert_eq!(removed_key.meta.item_id(), "coot");
        assert_eq!(
            removed_key.meta.retrieved_from(),
            Some(&KeystoreId::from_str("keystore2").unwrap())
        );
        assert_eq!(removed_key.meta.is_generated(), false);
        // The key doesn't exist in Keystore2 anymore
        assert!(
            !mgr.secondary_stores[0]
                .contains(&TestKeySpecifier1, &TestItem::item_type())
                .unwrap()
        );
    }
    #[test]
    fn keygen() {
        let mut rng = FakeEntropicRng(testing_rng());
        let mgr = KeyMgrBuilder::default()
            .primary_store(Box::<Keystore1>::default())
            .build()
            .unwrap();
        mgr.insert(
            TestItem::new("coot"),
            &TestKeySpecifier1,
            KeystoreSelector::Primary,
            true,
        )
        .unwrap();
        // There is no corresponding public key entry.
        assert!(
            mgr.get::<TestPublicKey>(&TestPublicKeySpecifier1)
                .unwrap()
                .is_none()
        );
        // Try to generate a new key (overwrite = false)
        let err = mgr
            .generate::<TestItem>(
                &TestKeySpecifier1,
                KeystoreSelector::Primary,
                &mut rng,
                false,
            )
            .unwrap_err();
        assert!(matches!(err, crate::Error::KeyAlreadyExists));
        // The previous entry was not overwritten because overwrite = false
        let key = mgr.get::<TestItem>(&TestKeySpecifier1).unwrap().unwrap();
        assert_eq!(key.meta.item_id(), "coot");
        assert_eq!(
            key.meta.retrieved_from(),
            Some(&KeystoreId::from_str("keystore1").unwrap())
        );
        assert_eq!(key.meta.is_generated(), false);
        // We don't store public keys in the keystore
        assert!(
            mgr.get::<TestPublicKey>(&TestPublicKeySpecifier1)
                .unwrap()
                .is_none()
        );
        // Try to generate a new key (overwrite = true)
        let generated_key = mgr
            .generate::<TestItem>(
                &TestKeySpecifier1,
                KeystoreSelector::Primary,
                &mut rng,
                true,
            )
            .unwrap();
        assert_eq!(generated_key.meta.item_id(), "generated_test_key");
        // Not set in a freshly generated key, because KeyMgr::generate()
        // returns it straight away, without going through Keystore::get()
        assert_eq!(generated_key.meta.retrieved_from(), None);
        assert_eq!(generated_key.meta.is_generated(), true);
        // Retrieve the inserted key
        let retrieved_key = mgr.get::<TestItem>(&TestKeySpecifier1).unwrap().unwrap();
        assert_eq!(retrieved_key.meta.item_id(), "generated_test_key");
        assert_eq!(
            retrieved_key.meta.retrieved_from(),
            Some(&KeystoreId::from_str("keystore1").unwrap())
        );
        assert_eq!(retrieved_key.meta.is_generated(), true);
        // We don't store public keys in the keystore
        assert!(
            mgr.get::<TestPublicKey>(&TestPublicKeySpecifier1)
                .unwrap()
                .is_none()
        );
    }
    #[test]
    fn get_or_generate() {
        let mut rng = FakeEntropicRng(testing_rng());
        let mut builder = KeyMgrBuilder::default().primary_store(Box::<Keystore1>::default());
        builder
            .secondary_stores()
            .extend([Keystore2::new_boxed(), Keystore3::new_boxed()]);
        let mgr = builder.build().unwrap();
        let keystore2 = KeystoreId::from_str("keystore2").unwrap();
        let entry_desc1 = entry_descriptor(TestKeySpecifier1, &keystore2);
        assert!(mgr.get_entry::<TestItem>(&entry_desc1).unwrap().is_none());
        mgr.insert(
            TestItem::new("coot"),
            &TestKeySpecifier1,
            KeystoreSelector::Id(&keystore2),
            true,
        )
        .unwrap();
        // The key already exists in keystore 2 so it won't be auto-generated.
        let key = mgr
            .get_or_generate::<TestItem>(&TestKeySpecifier1, KeystoreSelector::Primary, &mut rng)
            .unwrap();
        assert_eq!(key.meta.item_id(), "coot");
        assert_eq!(
            key.meta.retrieved_from(),
            Some(&KeystoreId::from_str("keystore2").unwrap())
        );
        assert_eq!(key.meta.is_generated(), false);
        assert_eq!(
            mgr.get_entry::<TestItem>(&entry_desc1)
                .unwrap()
                .map(|k| k.meta),
            Some(ItemMetadata::Key(KeyMetadata {
                item_id: "coot".to_string(),
                retrieved_from: Some(keystore2.clone()),
                is_generated: false,
            }))
        );
        // This key doesn't exist in any of the keystores, so it will be auto-generated and
        // inserted into keystore 3.
        let keystore3 = KeystoreId::from_str("keystore3").unwrap();
        let generated_key = mgr
            .get_or_generate::<TestItem>(
                &TestKeySpecifier2,
                KeystoreSelector::Id(&keystore3),
                &mut rng,
            )
            .unwrap();
        assert_eq!(generated_key.meta.item_id(), "generated_test_key");
        // Not set in a freshly generated key, because KeyMgr::get_or_generate()
        // returns it straight away, without going through Keystore::get()
        assert_eq!(generated_key.meta.retrieved_from(), None);
        assert_eq!(generated_key.meta.is_generated(), true);
        // Retrieve the inserted key
        let retrieved_key = mgr.get::<TestItem>(&TestKeySpecifier2).unwrap().unwrap();
        assert_eq!(retrieved_key.meta.item_id(), "generated_test_key");
        assert_eq!(
            retrieved_key.meta.retrieved_from(),
            Some(&KeystoreId::from_str("keystore3").unwrap())
        );
        assert_eq!(retrieved_key.meta.is_generated(), true);
        let entry_desc2 = entry_descriptor(TestKeySpecifier2, &keystore3);
        assert_eq!(
            mgr.get_entry::<TestItem>(&entry_desc2)
                .unwrap()
                .map(|k| k.meta),
            Some(ItemMetadata::Key(KeyMetadata {
                item_id: "generated_test_key".to_string(),
                retrieved_from: Some(keystore3.clone()),
                is_generated: true,
            }))
        );
        let arti_pat = KeyPathPattern::Arti("*".to_string());
        let matching = mgr.list_matching(&arti_pat).unwrap();
        assert_eq!(matching.len(), 2);
        assert!(matching.contains(&entry_desc1));
        assert!(matching.contains(&entry_desc2));
        assert_eq!(mgr.remove_entry(&entry_desc2).unwrap(), Some(()));
        assert!(mgr.get_entry::<TestItem>(&entry_desc2).unwrap().is_none());
        assert!(mgr.remove_entry(&entry_desc2).unwrap().is_none());
    }
    #[test]
    fn list_matching_ignores_unrecognized_keys() {
        let builder = KeyMgrBuilder::default().primary_store(Box::new(KeystoreUnrec1::default()));
        let mgr = builder.build().unwrap();
        let unrec_1 = KeystoreId::from_str("keystore_unrec1").unwrap();
        mgr.insert(
            TestItem::new("whale shark"),
            &TestKeySpecifier1,
            KeystoreSelector::Id(&unrec_1),
            true,
        )
        .unwrap();
        let arti_pat = KeyPathPattern::Arti("*".to_string());
        let valid_key_path = KeyPath::Arti(TestKeySpecifier1.arti_path().unwrap());
        let matching = mgr.list_matching(&arti_pat).unwrap();
        // assert the unrecognized key has been filtered out
        assert_eq!(matching.len(), 1);
        assert_eq!(matching.first().unwrap().key_path(), &valid_key_path);
    }
    #[cfg(feature = "onion-service-cli-extra")]
    #[test]
    /// Test all `arti keys` subcommands
    // TODO: split this in different tests
    fn keys_subcommands() {
        let mut builder =
            KeyMgrBuilder::default().primary_store(Box::new(KeystoreUnrec1::default()));
        builder
            .secondary_stores()
            .extend([Keystore2::new_boxed(), Keystore3::new_boxed()]);
        let mgr = builder.build().unwrap();
        let ks_unrec1id = KeystoreId::from_str("keystore_unrec1").unwrap();
        let keystore2id = KeystoreId::from_str("keystore2").unwrap();
        let keystore3id = KeystoreId::from_str("keystore3").unwrap();
        // Insert a key into KeystoreUnrec1
        let _ = mgr
            .insert(
                TestItem::new("pangolin"),
                &TestKeySpecifier1,
                KeystoreSelector::Id(&ks_unrec1id),
                true,
            )
            .unwrap();
        // Insert a key into Keystore2
        let _ = mgr
            .insert(
                TestItem::new("coot"),
                &TestKeySpecifier2,
                KeystoreSelector::Id(&keystore2id),
                true,
            )
            .unwrap();
        // Insert a key into Keystore3
        let _ = mgr
            .insert(
                TestItem::new("penguin"),
                &TestKeySpecifier3,
                KeystoreSelector::Id(&keystore3id),
                true,
            )
            .unwrap();
        let assert_key = |path, ty, expected_path: &ArtiPath, expected_type| {
            assert_eq!(ty, expected_type);
            assert_eq!(path, &KeyPath::Arti(expected_path.clone()));
        };
        let item_type = TestItem::new("axolotl").item.item_type().unwrap();
        let unrecognized_entry_id = RawEntryId::Path(PathBuf::from("unrecognized_entry0"));
        // Test `list`
        let entries = mgr.list().unwrap();
        let expected_items = [
            (ks_unrec1id, TestKeySpecifier1.arti_path().unwrap()),
            (keystore2id, TestKeySpecifier2.arti_path().unwrap()),
            (keystore3id, TestKeySpecifier3.arti_path().unwrap()),
        ];
        // Secondary keystores contain 1 valid key each
        let mut recognized_entries = 0;
        let mut unrecognized_entries = 0;
        for entry in entries.iter() {
            match entry {
                Ok(e) => {
                    if let Some((_, expected_arti_path)) = expected_items
                        .iter()
                        .find(|(keystore_id, _)| keystore_id == e.keystore_id())
                    {
                        assert_key(e.key_path(), e.key_type(), expected_arti_path, &item_type);
                        recognized_entries += 1;
                        continue;
                    }
                    panic!("Unexpected key encountered {:?}", e);
                }
                Err(u) => {
                    assert_eq!(u.entry().raw_id(), &unrecognized_entry_id);
                    unrecognized_entries += 1;
                }
            }
        }
        assert_eq!(recognized_entries, 3);
        assert_eq!(unrecognized_entries, 1);
        // Test `list_keystores`
        let keystores = mgr.list_keystores().iter().len();
        assert_eq!(keystores, 3);
        // Test `list_by_id`
        let primary_keystore_id = KeystoreId::from_str("keystore_unrec1").unwrap();
        let entries = mgr.list_by_id(&primary_keystore_id).unwrap();
        // Primary keystore contains a valid key and an unrecognized key
        let mut recognized_entries = 0;
        let mut unrecognized_entries = 0;
        // A list of entries, in a form that can be consumed by remove_unchecked
        let mut all_entries = vec![];
        for entry in entries.iter() {
            match entry {
                Ok(entry) => {
                    assert_key(
                        entry.key_path(),
                        entry.key_type(),
                        &TestKeySpecifier1.arti_path().unwrap(),
                        &item_type,
                    );
                    recognized_entries += 1;
                    all_entries.push(RawKeystoreEntry::new(
                        build_raw_id_path(entry.key_path(), entry.key_type()),
                        primary_keystore_id.clone(),
                    ));
                }
                Err(u) => {
                    assert_eq!(u.entry().raw_id(), &unrecognized_entry_id);
                    unrecognized_entries += 1;
                    all_entries.push(u.entry().into());
                }
            }
        }
        assert_eq!(recognized_entries, 1);
        assert_eq!(unrecognized_entries, 1);
        // Remove a recognized entry and an recognized one
        for entry in all_entries {
            mgr.remove_unchecked(&entry.raw_id().to_string(), entry.keystore_id())
                .unwrap();
        }
        // Check the keys have been removed
        let entries = mgr.list_by_id(&primary_keystore_id).unwrap();
        assert_eq!(entries.len(), 0);
    }
    /// Whether to generate a given item before running the `run_certificate_test`.
    #[cfg(feature = "experimental-api")]
    #[derive(Clone, Copy, Debug, PartialEq)]
    enum GenerateItem {
        Yes,
        No,
    }
    #[cfg(feature = "experimental-api")]
    macro_rules! run_certificate_test {
        (
            generate_subject_key = $generate_subject_key:expr,
            generate_signing_key = $generate_signing_key:expr,
            $($expected_err:tt)?
        ) => {{
            use GenerateItem::*;
            let mut rng = FakeEntropicRng(testing_rng());
            let mut builder = KeyMgrBuilder::default().primary_store(Box::<Keystore1>::default());
            builder
                .secondary_stores()
                .extend([Keystore2::new_boxed(), Keystore3::new_boxed()]);
            let mgr = builder.build().unwrap();
            let spec = crate::test_utils::TestCertSpecifier {
                subject_key_spec: TestKeySpecifier1,
                signing_key_spec: TestKeySpecifier2,
                denotator: vec!["foo".into()],
            };
            if $generate_subject_key == Yes {
                let _ = mgr
                    .generate::<TestItem>(
                        &TestKeySpecifier1,
                        KeystoreSelector::Primary,
                        &mut rng,
                        false,
                    )
                    .unwrap();
            }
            if $generate_signing_key == Yes {
                let _ = mgr
                    .generate::<TestItem>(
                        &TestKeySpecifier2,
                        KeystoreSelector::Id(&KeystoreId::from_str("keystore2").unwrap()),
                        &mut rng,
                        false,
                    )
                    .unwrap();
            }
            let make_certificate = move |subject_key: &TestItem, signed_with: &TestItem| {
                let subject_id = subject_key.meta.as_key().unwrap().item_id.clone();
                let signing_id = signed_with.meta.as_key().unwrap().item_id.clone();
                let meta = ItemMetadata::Cert(CertMetadata {
                    subject_key_id: subject_id,
                    signing_key_id: signing_id,
                    retrieved_from: None,
                    is_generated: true,
                });
                // Note: this is not really a cert for `subject_key` signed with the `signed_with`
                // key!. The two are `TestItem`s and not keys, so we can't really generate a real
                // cert from them. We can, however, pretend we did, for testing purposes.
                // Eventually we might want to rewrite these tests to use real items
                // (like the `ArtiNativeKeystore` tests)
                let mut rng = FakeEntropicRng(testing_rng());
                let keypair = ed25519::Keypair::generate(&mut rng);
                let encoded_cert = Ed25519Cert::constructor()
                    .cert_type(tor_cert::CertType::IDENTITY_V_SIGNING)
                    .expiration(SystemTime::now() + Duration::from_secs(180))
                    .signing_key(keypair.public_key().into())
                    .cert_key(CertifiedKey::Ed25519(keypair.public_key().into()))
                    .encode_and_sign(&keypair)
                    .unwrap();
                let test_cert = CertData::TorEd25519Cert(encoded_cert);
                AlwaysValidCert(TestItem {
                    item: KeystoreItem::Cert(test_cert),
                    meta,
                })
            };
            let res = mgr
                .get_or_generate_key_and_cert::<TestItem, AlwaysValidCert>(
                    &spec,
                    &make_certificate,
                    KeystoreSelector::Primary,
                    &mut rng,
                );
            #[allow(unused_assignments)]
            #[allow(unused_mut)]
            let mut has_error = false;
            $(
                has_error = true;
                let err = res.clone().unwrap_err();
                assert!(
                    matches!(
                        err,
                        crate::Error::Corruption(KeystoreCorruptionError::$expected_err)
                    ),
                    "unexpected error: {err:?}",
                );
            )?
            if !has_error {
                let (key, cert) = res.unwrap();
                let expected_subj_key_id = if $generate_subject_key == Yes {
                    "generated_test_key"
                } else {
                    "generated_test_key"
                };
                assert_eq!(key.meta.item_id(), expected_subj_key_id);
                assert_eq!(
                    cert.0.meta.as_cert().unwrap().subject_key_id,
                    expected_subj_key_id
                );
                assert_eq!(
                    cert.0.meta.as_cert().unwrap().signing_key_id,
                    "generated_test_key"
                );
                assert_eq!(cert.0.meta.is_generated(), true);
            }
        }}
    }
    #[test]
    #[cfg(feature = "experimental-api")]
    #[rustfmt::skip] // preserve the layout for readability
    #[allow(clippy::cognitive_complexity)] // clippy seems confused here...
    fn get_certificate() {
        run_certificate_test!(
            generate_subject_key = No,
            generate_signing_key = No,
            MissingSigningKey
        );
        run_certificate_test!(
            generate_subject_key = Yes,
            generate_signing_key = No,
            MissingSigningKey
        );
        run_certificate_test!(
            generate_subject_key = No,
            generate_signing_key = Yes,
        );
        run_certificate_test!(
            generate_subject_key = Yes,
            generate_signing_key = Yes,
        );
    }
}