Skip to main content

tor_keymgr/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![doc = include_str!("../README.md")]
3// @@ begin lint list maintained by maint/add_warning @@
4#![allow(renamed_and_removed_lints)] // @@REMOVE_WHEN(ci_arti_stable)
5#![allow(unknown_lints)] // @@REMOVE_WHEN(ci_arti_nightly)
6#![warn(missing_docs)]
7#![warn(noop_method_call)]
8#![warn(unreachable_pub)]
9#![warn(clippy::all)]
10#![deny(clippy::await_holding_lock)]
11#![deny(clippy::cargo_common_metadata)]
12#![deny(clippy::cast_lossless)]
13#![deny(clippy::checked_conversions)]
14#![allow(clippy::cognitive_complexity)] // See arti#2556
15#![deny(clippy::debug_assert_with_mut_call)]
16#![deny(clippy::exhaustive_enums)]
17#![deny(clippy::exhaustive_structs)]
18#![deny(clippy::expl_impl_clone_on_copy)]
19#![deny(clippy::fallible_impl_from)]
20#![deny(clippy::implicit_clone)]
21#![deny(clippy::large_stack_arrays)]
22#![warn(clippy::manual_ok_or)]
23#![deny(clippy::missing_docs_in_private_items)]
24#![warn(clippy::needless_borrow)]
25#![warn(clippy::needless_pass_by_value)]
26#![warn(clippy::option_option)]
27#![warn(clippy::or_fun_call)]
28#![deny(clippy::print_stderr)]
29#![deny(clippy::print_stdout)]
30#![warn(clippy::rc_buffer)]
31#![deny(clippy::ref_option_ref)]
32#![warn(clippy::semicolon_if_nothing_returned)]
33#![warn(clippy::trait_duplication_in_bounds)]
34#![deny(clippy::unchecked_time_subtraction)]
35#![deny(clippy::unnecessary_wraps)]
36#![warn(clippy::unseparated_literal_suffix)]
37#![deny(clippy::unwrap_used)]
38#![deny(clippy::mod_module_files)]
39#![allow(clippy::let_unit_value)] // This can reasonably be done for explicitness
40#![allow(clippy::uninlined_format_args)]
41#![allow(clippy::significant_drop_in_scrutinee)] // arti/-/merge_requests/588/#note_2812945
42#![allow(clippy::result_large_err)] // temporary workaround for arti#587
43#![allow(clippy::needless_raw_string_hashes)] // complained-about code is fine, often best
44#![allow(clippy::needless_lifetimes)] // See arti#1765
45#![allow(mismatched_lifetime_syntaxes)] // temporary workaround for arti#2060
46#![allow(clippy::collapsible_if)] // See arti#2342
47#![deny(clippy::unused_async)]
48#![deny(clippy::string_slice)] // See arti#2571
49#![allow(recursion_depth_exceeding_limit)] // arti#2715, rust/issues/159228
50//! <!-- @@ end lint list maintained by maint/add_warning @@ -->
51
52// TODO #1645 (either remove this, or decide to have it everywhere)
53#![cfg_attr(not(all(feature = "full", feature = "experimental")), allow(unused))]
54// TODO: add to the list maintained by maint/add_warning once it's available on all crates
55#![warn(unnameable_types)]
56
57// TODO: write more comprehensive documentation when the API is a bit more
58// stable
59
60mod arti_path;
61pub mod config;
62mod err;
63mod key_specifier;
64pub(crate) mod raw;
65#[cfg(any(test, feature = "testing"))]
66pub mod test_utils;
67
68#[cfg(feature = "keymgr")]
69mod keystore;
70#[cfg(feature = "keymgr")]
71mod mgr;
72
73#[cfg(not(feature = "keymgr"))]
74mod dummy;
75
76pub use arti_path::{ArtiPath, DENOTATOR_GROUP_SEP, DENOTATOR_SEP};
77pub use err::{
78    ArtiPathSyntaxError, Error, KeystoreCorruptionError, KeystoreError, UnknownKeyTypeError,
79    UnrecognizedEntry, UnrecognizedEntryError,
80};
81pub use key_specifier::{
82    ArtiPathError, ArtiPathRange, ArtiPathUnavailableError, CTorKeySpecifier, CTorPath,
83    CTorPathError, InvalidKeyPathComponentValue, KeyCertificateSpecifier, KeyPath, KeyPathError,
84    KeyPathInfo, KeyPathInfoBuilder, KeyPathInfoExtractor, KeyPathPattern, KeySpecifier,
85    KeySpecifierComponent, KeySpecifierComponentViaDisplayFromStr, KeySpecifierPattern,
86};
87#[cfg(any(feature = "onion-service-cli-extra", feature = "experimental-api"))]
88pub use raw::RawEntryId;
89
90#[cfg(feature = "experimental-api")]
91pub use key_specifier::CertSpecifierPattern;
92
93#[cfg(feature = "keymgr")]
94pub use {
95    keystore::arti::ArtiNativeKeystore,
96    keystore::ctor::{CTorClientKeystore, CTorServiceKeystore},
97    keystore::{Keystore, KeystoreEntryResult},
98    mgr::{KeyMgr, KeyMgrBuilder, KeyMgrBuilderError, KeystoreEntry},
99    ssh_key,
100};
101
102#[cfg(all(feature = "keymgr", feature = "ephemeral-keystore"))]
103pub use keystore::ephemeral::ArtiEphemeralKeystore;
104
105#[doc(hidden)]
106pub use key_specifier::derive as key_specifier_derive;
107
108pub use tor_key_forge::{
109    EncodableItem, ErasedKey, KeyType, Keygen, KeygenRng, SshKeyAlgorithm, SshKeyData,
110    ToEncodableKey,
111};
112
113derive_deftly::template_export_semver_check! { "0.12.1" }
114
115#[cfg(not(feature = "keymgr"))]
116pub use dummy::*;
117
118/// A boxed [`Keystore`].
119pub(crate) type BoxedKeystore = Box<dyn Keystore>;
120
121#[doc(hidden)]
122pub use {derive_deftly, inventory};
123
124use derive_more::{AsRef, Display, From};
125use serde::{Deserialize, Serialize};
126use std::str::FromStr;
127
128/// A Result type for this crate.
129pub type Result<T> = std::result::Result<T, Error>;
130
131/// An identifier for a particular [`Keystore`] instance.
132//
133// TODO (#1193): restrict the charset of this ID
134#[derive(
135    Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize, Display, AsRef,
136)]
137#[serde(transparent)]
138#[non_exhaustive]
139pub struct KeystoreId(String);
140
141impl FromStr for KeystoreId {
142    type Err = Error;
143
144    fn from_str(s: &str) -> Result<Self> {
145        Ok(Self(s.into()))
146    }
147}
148
149/// Specifies which keystores a [`KeyMgr`] operation should apply to.
150#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, Hash, From)]
151#[non_exhaustive]
152pub enum KeystoreSelector<'a> {
153    /// Try to use the keystore with the specified ID.
154    Id(&'a KeystoreId),
155    /// Use the primary key store.
156    #[default]
157    Primary,
158}