Skip to main content

TrustStore

Struct TrustStore 

Source
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§

Source§

impl TrustStore

Source

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

Opens (creating when absent) the store under <state_dir>/peers.

§Errors

Error::Io when the directories cannot be created.

Source

pub fn with_limits(self, limits: WaitingLimits) -> Self

This store with other bounds on its waiting queue (the default: 16 per source, 1000 in all, 60 seconds).

Source

pub fn with_ipv6_prefix(self, bits: u8) -> Self

This store with IPv6 sources grouped by their first bits bits for the per-source limits (64 unless set). 128 counts each address alone, which lets the holder of a subnet fill the queue; values above 128 are 128. IPv4 sources, IPv4-mapped IPv6 ones included, always count per address.

Source

pub fn with_clock(self, clock: Clock) -> Self

This store reading clock for invitation expiry and key retirement (the system clock unless set).

Source

pub fn with_rotation(self, options: RotationOptions) -> Self

This store rotating keys by options (24 hours, automatic, unless set).

Source

pub fn rotation(&self) -> RotationOptions

The rotation options in force.

Source

pub fn clock(&self) -> &Clock

The clock in force.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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:.

Source

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.

Source

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.

Source

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 (its oldest is replaced); 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 with_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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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§

Source§

impl Clone for TrustStore

Source§

fn clone(&self) -> Self

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
Source§

impl Debug for TrustStore

Source§

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

Formats the value using the given formatter. Read more

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
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.