tor_keymgr/
raw.rs

1//! Raw keystore entry identifiers used in plumbing CLI functionalities.
2
3use std::path::PathBuf;
4
5use amplify::Getters;
6use tor_basic_utils::PathExt;
7use tor_key_forge::KeystoreItemType;
8
9use crate::{ArtiPath, KeystoreId, UnrecognizedEntry};
10
11/// A raw keystore entry descriptor.
12#[cfg_attr(feature = "onion-service-cli-extra", visibility::make(pub))]
13#[derive(Debug, Clone, PartialEq, derive_more::From, Getters)]
14pub(crate) struct RawKeystoreEntry {
15    /// The underlying keystore-specific raw identifier of the entry.
16    #[getter(skip)]
17    raw_id: RawEntryId,
18    /// The keystore this entry was found in.
19    #[getter(skip)]
20    keystore_id: KeystoreId,
21}
22
23impl RawKeystoreEntry {
24    /// Return the underlying keystore-specific raw identifier of the entry.
25    #[cfg(feature = "onion-service-cli-extra")]
26    pub fn raw_id(&self) -> &RawEntryId {
27        &self.raw_id
28    }
29
30    /// Return the ID of the keystore this entry was found in.
31    #[cfg(feature = "onion-service-cli-extra")]
32    pub fn keystore_id(&self) -> &KeystoreId {
33        &self.keystore_id
34    }
35}
36
37impl From<&UnrecognizedEntry> for RawKeystoreEntry {
38    fn from(value: &UnrecognizedEntry) -> Self {
39        value.clone().into()
40    }
41}
42
43impl RawKeystoreEntry {
44    /// Returns a new instance of [`RawKeystoreEntry`]
45    /// that identifies an entry with the specified `raw_id`
46    /// raw identifier in the keystore with the specified
47    /// `keystore_id`.
48    pub(crate) fn new(raw_id: RawEntryId, keystore_id: KeystoreId) -> Self {
49        Self {
50            raw_id,
51            keystore_id,
52        }
53    }
54}
55
56/// The raw identifier of a key inside a [`Keystore`](crate::Keystore).
57///
58/// The exact type of the identifier depends on the backing storage of the keystore
59/// (for example, an on-disk keystore will identify its entries by [`Path`](RawEntryId::Path)).
60#[cfg_attr(feature = "onion-service-cli-extra", visibility::make(pub))]
61#[non_exhaustive]
62#[derive(Debug, Clone, PartialEq, derive_more::Display)]
63pub(crate) enum RawEntryId {
64    /// An entry identified by path inside an on-disk keystore.
65    // NOTE: this will only be used by on-disk keystores like
66    // [`ArtiNativeKeystore`](crate::ArtiNativeKeystore)
67    #[display("{}", _0.display_lossy())]
68    Path(PathBuf),
69
70    /// An entry of an in-memory ephemeral key storage
71    /// [`ArtiEphemeralKeystore`](crate::ArtiEphemeralKeystore)
72    #[display("{} {:?}", _0.0, _0.1)]
73    Ephemeral((ArtiPath, KeystoreItemType)),
74    // TODO: when/if we add support for non on-disk keystores,
75    // new variants will be added
76}