Skip to main content

TrustStore

Struct TrustStore 

pub struct TrustStore { /* private fields */ }
Expand description

Persistent trust: peers an operator accepted, peers that were turned away and wait to be accepted (pairing), open invitations, and announced successor keys.

§Pairing

  1. A server turns away a peer it does not trust, after the peer proved it holds the key it presented, and records its key with record_waiting.
  2. The operator lists waiting and either accepts an entry by id or removes it.
  3. The next handshake finds the peer is_accepted.

Every method must meet this contract: nothing is recorded as waiting until the peer has shown, inside the handshake, that it holds the private key of the public key it presents (a signature over the handshake transcript, a key exchange that only the key’s holder can complete, …). Presenting a key proves nothing: anyone can copy a public certificate, and a store that records unproven keys lets a stranger fill the queue with somebody else’s identity, or put a real peer’s id in front of the operator under a source address that is not theirs. A handshake that fails to prove the key records nothing.

An invitation lets a joiner pair without waiting: it presents the secret over a channel that proves its key, and the inviter redeems it and accepts the key.

§Rotation

A peer accepted as key A announces its successor B over a session A authenticated (announce_successor). The first time the peer proves B in a handshake, the method promote_successors it, unless RotationOptions::require_approval leaves that to the caller; A is then accepted until RotationOptions::retire_after has passed, and refused from then on. Removing A removes its pending successor. Each step is queued as a RotationEvent.

§Key material and methods

The store keeps the bytes a method hands it as a KeyMaterial and never interprets them: a PEM certificate for tls, another method’s own key for it. Each file records the method name, so a store can hold the keys of several methods side by side, and a method reads only the entries that name it. The id of an entry is the SHA-256 of its bytes; the store checks that when it reads a file.

§On-disk format (version 1)

Under <state_dir>/peers/:

accepted/<64 lowercase hex>.pem     one PEM block; the name is the peer's id
waiting/<64 lowercase hex>.pem      the same, for a peer that proved its key and was refused
successors/<64 lowercase hex>.pem   an announced successor key, named by its id
invitations/<64 lowercase hex>.inv  an open invitation, named by the SHA-256 of its secret

Before the PEM block a file may hold header lines (PEM allows explanatory text there): Method: <name>; in a waiting file Source: <ip>:<port> (IPv6 as [addr]:port), the address the handshake came from; in an accepted file Successor: <64 hex> (the announced or promoting successor) and Retires: <unix seconds>; in a successor file Predecessor: <64 hex>. An invitation file is header lines only: Expires: <unix seconds>, Uses: <count> and, when not empty, Label: <text>; it never holds the secret. A key file without a Method: line, or named otherwise, is not read. Accepting moves a file from waiting/ to accepted/, rewritten without the Source: line.

Implementations§

§

impl TrustStore

pub fn builder() -> TrustStoreBuilder

A builder with nothing set.

§

impl TrustStore

pub fn open(state_dir: &Path) -> Result<TrustStore, Error>

Opens (creating when absent) the store under <state_dir>/peers with every setting at its default: TrustStore::builder().state_dir(state_dir).build().

§Errors

[Error::Io] when the directories cannot be created.

pub fn waiting_queue(self, settings: WaitingSettings) -> TrustStore

This store with a new, empty waiting queue under settings, the replacement for the queue it was built with. Rotation and the clock stay.

pub fn settings(&self) -> TrustStoreSettings

The settings it was built with.

pub fn non_defaults(&self) -> Vec<NonDefault>

Every setting that differs from its default, named by key path.

pub fn rotation(&self) -> RotationOptions

The rotation options in force.

pub fn clock(&self) -> &Clock

The clock in force.

pub fn waiting(&self) -> Result<Vec<WaitingEntry>, Error>

Peers awaiting operator acceptance, sorted by id, each with the address it connected from. Every entry proved it holds its key.

§Errors

[Error::Io] when the directory cannot be read.

pub fn accepted(&self) -> Result<Vec<PeerId>, Error>

Accepted ids, sorted; a key past its retirement time is not listed.

§Errors

[Error::Io] when the directory cannot be read.

pub fn accepted_key(&self, id: &PeerId) -> Option<KeyMaterial>

The key material of an accepted peer, when it is accepted, not retired, and its file is sound.

pub fn waiting_key(&self, id: &PeerId) -> Option<KeyMaterial>

The key material of a waiting peer, when it is waiting and its file is sound.

pub fn accept(&self, id: &PeerId) -> Result<(), Error>

Accepts a waiting peer by its id.

§Errors

[Error::NotWaiting], [Error::AlreadyAccepted], or [Error::Material] when the stored key does not hash to the name.

pub fn accept_key(&self, key: &KeyMaterial) -> Result<PeerId, Error>

Accepts a key directly, without it having waited.

§Errors

[Error::Material] when the key is not valid, [Error::AlreadyAccepted] when it is accepted and current, [Error::Io] on write failure. A key past its retirement time is accepted again, its file rewritten without Successor: and Retires:.

pub fn remove(&self, id: &PeerId) -> Result<bool, Error>

Removes an accepted, waiting or pending-successor peer, and the pending successor an accepted one announced; true when something was removed.

§Errors

[Error::Io] when a file cannot be removed.

pub fn is_accepted(&self, id: &PeerId) -> bool

Whether id is accepted, not past its retirement time, and its stored key really hashes to it.

pub fn record_waiting(&self, key: &KeyMaterial, source: Option<SocketAddr>)

Records a turned-away peer’s key as waiting. Failures are not reported to the peer.

Call this only after the peer proved it holds the key (see the pairing contract).

Bounded: invalid keys are ignored; one source holds at most per_source entries, a newcomer being refused ([WhenFull]); it records a new peer at most once per record_interval; the same peer from the same source is recorded at most once per rerecord; the directory holds at most total. A source is an IPv4 address (IPv4-mapped IPv6 included) or an IPv6 prefix of ipv6_prefix bits, /64 by default. The full source address is stored with the entry. None is the source of callers that do not know it, which share one bucket.

pub fn invite( &self, random: &Random, options: InvitationOptions, label: &str, ) -> Result<Invitation, Error>

Makes an invitation with a secret from random and records it (without the secret).

§Errors

[Error::InvalidOptions] when label is not one line of at most [MAX_LABEL] bytes without surrounding spaces, uses is 0 or valid_for is zero; [Error::Random] when random fails; [Error::Io] when the file cannot be written.

pub fn redeem(&self, secret: &[u8; 32]) -> Result<Invitation, Error>

Redeems an invitation by its secret, using one of its uses: the invitation with the uses left after this one. The last use removes it.

Atomic: of concurrent redemptions of a single-use invitation exactly one succeeds. An expired invitation is removed.

§Errors

[Error::NotInvited] when no open invitation has this secret, [Error::Expired] when it expired, [Error::Io] when its file cannot be read or moved.

pub fn invitations(&self) -> Result<Vec<InvitationEntry>, Error>

The open invitations, sorted by id, expired ones included until redeemed or revoked.

§Errors

[Error::Io] when the directory cannot be read.

pub fn revoke_invitation(&self, id: &InvitationId) -> Result<bool, Error>

Revokes an invitation; true when it was open. A redemption in flight when it is revoked fails, and its remaining uses are not restored: a <64 hex>.revoked file records the revocation before the invitation file goes.

§Errors

[Error::Io] when the revocation cannot be recorded or the file removed.

pub fn announce_successor( &self, old: &PeerId, new: &KeyMaterial, ) -> Result<(), Error>

Records new as the pending successor of the accepted key old, replacing a successor old announced before. Call it only with a key the holder of old sent over a session old authenticated.

§Errors

[Error::NotAccepted] when old is not accepted or is already retiring; [Error::AlreadyAccepted] when new is accepted; [Error::SuccessorTaken] when new is the pending successor of another key that is still accepted; [Error::Material] when new is not valid or is old; [Error::Io] on write failure.

pub fn successor_key(&self, new: &PeerId) -> Option<KeyMaterial>

The key of new when it is the pending successor of a key that is still accepted, not retiring, and names it.

pub fn prove_successor(&self, new: &PeerId) -> Result<(), Error>

Records that the holder of the pending successor new proved it in a handshake (a Proven: line with the time). A method calls it after the handshake signature verifies, whether or not it then promotes. The first proof’s time is kept.

§Errors

[Error::NoSuccessor] when new is not the pending successor of an accepted key; [Error::Io] on write failure.

pub fn promote_successor(&self, new: &PeerId) -> Result<PeerId, Error>

Accepts the pending successor new and gives its predecessor a retirement time (RotationOptions::retire_after from now). Returns the predecessor’s id. A method calls it once the peer proved new, unless RotationOptions::require_approval is set; the caller calls it to approve.

§Errors

[Error::NoSuccessor] when new is not the pending successor of an accepted key; [Error::NotProven] when nobody proved it and RotationOptions::allow_unproven is not set; [Error::Io] on write failure.

pub fn successors(&self) -> Result<Vec<Successor>, Error>

The pending successors, sorted by the new key’s id.

§Errors

[Error::Io] when the directory cannot be read.

pub fn retiring(&self) -> Result<Vec<Retiring>, Error>

Accepted keys with a retirement time, sorted by id, those already retired included.

§Errors

[Error::Io] when the directory cannot be read.

pub fn take_rotation_events(&self) -> Vec<RotationEvent>

The rotation events since the last call, oldest first. Only events of this process are kept, at most 1024; the on-disk state is in successors and retiring.

Trait Implementations§

§

impl Clone for TrustStore

§

fn clone(&self) -> TrustStore

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl Debug for TrustStore

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl PairingStore for TrustStore

§

fn accept_key(&self, key: &KeyMaterial) -> Result<PeerId, Error>

Accepts key and returns its id. Read more
§

fn redeem(&self, secret: &[u8; 32]) -> Result<Invitation, Error>

Uses up one redemption of the open invitation whose secret this is, atomically: of concurrent redemptions of a single-use invitation exactly one succeeds. Read more
§

fn accepted_key(&self, id: &PeerId) -> Option<KeyMaterial>

The key of an accepted, current peer, or None. The responder asks after accept_key and refuses to report a pairing the store does not hold.
Source§

impl TrustStoreExt for TrustStore

Source§

fn accept_certificate(&self, der: &[u8]) -> Result<PeerId, PeerError>

Accepts a certificate directly, without it having waited. Read more
Source§

fn record_waiting_certificate(&self, der: &[u8], source: Option<SocketAddr>)

Records a turned-away peer’s certificate as waiting. Call it only after the peer proved it holds the certificate’s key, as the verifiers of this crate do.
Source§

fn accept_public_key(&self, spki: &[u8]) -> Result<PeerId, PeerError>

Accepts a raw public key (a DER SubjectPublicKeyInfo) directly, without it having waited. Read more
Source§

fn record_waiting_public_key(&self, spki: &[u8], source: Option<SocketAddr>)

Records a turned-away peer’s raw public key as waiting. Call it only after the peer proved it holds the key, as the verifiers of this crate do.
§

impl TryFrom<TrustStoreParts> for TrustStore

§

fn try_from(parts: TrustStoreParts) -> Result<TrustStore, Error>

Opens the store (creating the directories when absent).

§Errors

[Error::Io] when the directories cannot be created.

§

type Error = Error

The type returned in the event of a conversion error.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.