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//! <!-- @@ end lint list maintained by maint/add_warning @@ -->
50
51// TODO #1645 (either remove this, or decide to have it everywhere)
52#![cfg_attr(not(all(feature = "full", feature = "experimental")), allow(unused))]
53// TODO: add to the list maintained by maint/add_warning once it's available on all crates
54#![warn(unnameable_types)]
55
56// TODO: write more comprehensive documentation when the API is a bit more
57// stable
58
59mod arti_path;
60pub mod config;
61mod err;
62mod key_specifier;
63pub(crate) mod raw;
64#[cfg(any(test, feature = "testing"))]
65pub mod test_utils;
66
67#[cfg(feature = "keymgr")]
68mod keystore;
69#[cfg(feature = "keymgr")]
70mod mgr;
71
72#[cfg(not(feature = "keymgr"))]
73mod dummy;
74
75pub use arti_path::{ArtiPath, DENOTATOR_GROUP_SEP, DENOTATOR_SEP};
76pub use err::{
77    ArtiPathSyntaxError, Error, KeystoreCorruptionError, KeystoreError, UnknownKeyTypeError,
78    UnrecognizedEntry, UnrecognizedEntryError,
79};
80pub use key_specifier::{
81    ArtiPathError, ArtiPathRange, ArtiPathUnavailableError, CTorKeySpecifier, CTorPath,
82    CTorPathError, InvalidKeyPathComponentValue, KeyCertificateSpecifier, KeyPath, KeyPathError,
83    KeyPathInfo, KeyPathInfoBuilder, KeyPathInfoExtractor, KeyPathPattern, KeySpecifier,
84    KeySpecifierComponent, KeySpecifierComponentViaDisplayFromStr, KeySpecifierPattern,
85};
86#[cfg(any(feature = "onion-service-cli-extra", feature = "experimental-api"))]
87pub use raw::RawEntryId;
88
89#[cfg(feature = "experimental-api")]
90pub use key_specifier::CertSpecifierPattern;
91
92#[cfg(feature = "keymgr")]
93pub use {
94    keystore::arti::ArtiNativeKeystore,
95    keystore::ctor::{CTorClientKeystore, CTorServiceKeystore},
96    keystore::{Keystore, KeystoreEntryResult},
97    mgr::{KeyMgr, KeyMgrBuilder, KeyMgrBuilderError, KeystoreEntry},
98    ssh_key,
99};
100
101#[cfg(all(feature = "keymgr", feature = "ephemeral-keystore"))]
102pub use keystore::ephemeral::ArtiEphemeralKeystore;
103
104#[doc(hidden)]
105pub use key_specifier::derive as key_specifier_derive;
106
107pub use tor_key_forge::{
108    EncodableItem, ErasedKey, KeyType, Keygen, KeygenRng, SshKeyAlgorithm, SshKeyData,
109    ToEncodableKey,
110};
111
112derive_deftly::template_export_semver_check! { "0.12.1" }
113
114#[cfg(not(feature = "keymgr"))]
115pub use dummy::*;
116
117/// A boxed [`Keystore`].
118pub(crate) type BoxedKeystore = Box<dyn Keystore>;
119
120#[doc(hidden)]
121pub use {derive_deftly, inventory};
122
123use derive_more::{AsRef, Display, From};
124use serde::{Deserialize, Serialize};
125use std::str::FromStr;
126
127/// A Result type for this crate.
128pub type Result<T> = std::result::Result<T, Error>;
129
130/// An identifier for a particular [`Keystore`] instance.
131//
132// TODO (#1193): restrict the charset of this ID
133#[derive(
134    Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize, Display, AsRef,
135)]
136#[serde(transparent)]
137#[non_exhaustive]
138pub struct KeystoreId(String);
139
140impl FromStr for KeystoreId {
141    type Err = Error;
142
143    fn from_str(s: &str) -> Result<Self> {
144        Ok(Self(s.into()))
145    }
146}
147
148/// Specifies which keystores a [`KeyMgr`] operation should apply to.
149#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, Hash, From)]
150#[non_exhaustive]
151pub enum KeystoreSelector<'a> {
152    /// Try to use the keystore with the specified ID.
153    Id(&'a KeystoreId),
154    /// Use the primary key store.
155    #[default]
156    Primary,
157}