Skip to main content

Crate coqui_peer

Crate coqui_peer 

Source
Expand description

§coqui-peer

Version Rust version License: MPL-2.0 Docs CI

Who a peer is, and whether we trust it, with no TLS and no cryptographic transport. This is the core every authentication method shares: the peer id, a trust store with pairing and key rotation, and a sans-IO handshake trait. A method lives in a crate of its own; coqui-peer-tls is the TLS one. See the documentation.

Status: alpha. 0.0.0-alpha.1 is a pre-release: the API may change in any release before 1.0.

§Features

  • PeerId: a peer’s id as <alg>:<hash> text (sha256:3f15...), parsed from the forms people paste (bare hex, colon-separated hex, grouped base32), compared in constant time.
  • TrustStore: accepted and waiting peers on disk. A peer is recorded as waiting only after it proved its key, bounded per source (IPv6 grouped by /64) and in total; an operator accepts or removes it by id. WaitingQueue is the same bounding for a host that stores waiting entries elsewhere.
  • Invitations and key rotation: one-time pairing secrets the store keeps only as a hash, redeemed atomically; a successor key announced under the current one, promoted once proven, the old key retiring after a window you choose.
  • Pairing (module pair): a joiner and a responder accept each other’s keys by an invitation, a short PIN checked by SPAKE2 (feature pin), or an RFC 8628-style device flow. Sans-IO; codes, pending joiners and decisions are data your app shows and acts on, and every limit is an option with a default.
  • A sans-IO Handshake: feed received bytes or messages in, get bytes or messages out, over a byte stream, WebSocket messages or datagrams. Blocking drivers, with deadlines; async ones with the tokio feature. One length-prefixed frame format for every message.
  • Files and PEM: the store’s atomic, no-replace writes and its PEM format, for method crates.
  • Plain data and no process-wide state; nothing reads a file or an environment variable unless you name a directory.

§Installation

cargo add coqui-peer@0.0.0-alpha.1
FeatureAddsNeeded forDefault
serdeserdereading and writing PeerId and Digest as text, in the format you chooseoff
tokiotokio (io-util, time)the asynch drivers and frame functionsoff
pinspake2, hkdf, hmac, rand_corePIN pairing (Pairing::pin), Random::osoff

Rust 1.89 or later. A default build depends on data-encoding, sha1, sha2, subtle and zeroize.

§Quick start

Open a store, record a peer that has proved its key, and let the operator accept it:

use coqui_peer::{KeyMaterial, PeerId, TrustStore};

let store = TrustStore::open(std::path::Path::new("/var/lib/app"))?;

// A method hands the store the peer's public key; the id is its SHA-256.
let key = KeyMaterial::new("example", "PUBLIC KEY", vec![0u8; 32])?;
store.record_waiting(&key, Some("192.0.2.7:4000".parse()?)); // only after the key proof
store.accept(&key.id())?;
assert!(store.is_accepted(&key.id()));

// An id from text, in any accepted spelling.
let pinned: PeerId =
    "sha256:630dcd2966c4336691125448bbb25b4ff412a49c732db2c8abc1b8581bd710dd".parse()?;
assert_eq!(pinned, key.id());

Running a method’s handshake over a stream, with a deadline:

ⓘ
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
let established = coqui_peer::drive_stream_until(method.initiate()?, &mut socket, deadline)?;
let peer: PeerId = established.peer;        // verified
let channel = established.session;          // what the method produced

§API overview

ModulePurpose
coqui_peerPeerId, Digest; TrustStore, WaitingQueue, invitations and rotation; KeyMaterial; Clock, Random, Weakening; the Handshake and Method traits, the drivers and the frame functions; Error.
coqui_peer::pairPairing by invitation, PIN or device flow: Pairing, PairingJoiner, PairingResponder, PairingDesk and their options.
coqui_peer::asynchThe drivers and frame functions over tokio streams and async message transports (tokio).
coqui_peer::fileAtomic writes: write_new never replaces, replace does; owner-only on request.
coqui_peer::pemThe PEM block format the store writes, with its header lines.

§The key-proof contract

A peer is recorded as waiting only after it proved, inside the handshake, that it holds the private key of the key it presented. A presented key proves nothing: public keys and certificates are copied freely, and recording them unproven would let a stranger queue somebody else’s identity. Every method crate meets this contract; it is documented on TrustStore.

§Development

cargo fmt --all --check
cargo clippy --all-targets --all-features -- -D warnings
cargo +1.89 clippy --all-targets --all-features -- -D warnings
cargo test --all-features
RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features
cargo +nightly-2026-09-21 public-api --all-features -sss --color never | diff -u tools/public-api.txt -

§Releasing

This project follows Semantic Versioning and keeps a CHANGELOG.md. Pushing a tag matching v* runs the release workflow: the tests and a strict documentation build gate a GitHub release, marked as a pre-release when the version has a - suffix. The crate is published to crates.io with cargo publish by a maintainer.

§License

MPL-2.0; see LICENSE.

Modules§

asynchtokio
The loops of drive_stream and drive_messages over tokio streams and async message transports.
file
Whole-file writes that a crash or a concurrent writer cannot leave half done: the contents go to a temporary file beside the target, are synced, and only then take the target’s name.
pair
Pairing: how a joiner and a responder come to accept each other’s keys, over any channel.
pem
PEM blocks as the trust store writes them: header lines, then one block with 64-character base64 lines and LF line endings.

Structs§

Admission
What WaitingQueue::admit decided for one peer.
Clock
The wall clock a TrustStore or a pairing desk reads for expiry and retirement times.
Established
A verified peer and what the method produced besides.
Invitation
A one-time pairing secret: whoever presents it may pair, until it expires or its uses run out.
InvitationEntry
An open invitation, as listed: everything but the secret.
InvitationId
The id of an invitation: the SHA-256 of its secret, and the name of its file. Knowing it does not let anyone redeem the invitation.
InvitationOptions
How TrustStore::invite makes an invitation.
KeyMaterial
The public key material a method gives the trust store for one peer, with the name of the method that understands it.
PeerId
Who a peer is: the hash of its public key or certificate, the whole of what one peer is told of another.
Random
A source of cryptographically secure random bytes: invitation secrets, user codes and pairing slots are drawn from it.
Retiring
An accepted key with a retirement time: it is refused from retires on.
RotationOptions
How an accepted peer’s key is replaced by its successor.
Successor
A pending successor: new was announced by the peer accepted as old, and is accepted once the peer proves it (or the caller promotes it).
TrustStore
Persistent trust: peers an operator accepted, peers that were turned away and wait to be accepted (pairing), open invitations, and announced successor keys.
WaitingEntry
A peer waiting for acceptance.
WaitingLimits
Bounds on the waiting queue.
WaitingQueue
Decides which unaccepted peers are recorded as waiting, without storing anything: the policy of TrustStore::record_waiting, for a host that keeps its waiting entries elsewhere.
Weakening
A setting looser than its strict value, as plain data for the caller to show or log.

Enums§

Digest
The algorithm a PeerId is made with: the <alg> of its <alg>:<hash> text.
DriveError
Why driving a handshake failed.
Error
Failures of ids, key material and the trust store. No message holds key material or a path.
Framing
How a method divides what it sends.
RotationEvent
A change of a peer’s key, for the caller to report as it chooses.

Constants§

FORMAT_VERSION
The version of the on-disk format TrustStore writes and reads.
MAX_KEY_BYTES
The most key material a store holds for one peer.
MAX_LABEL
The longest invitation label, in bytes.
MAX_MESSAGE
The longest message drive_stream frames or accepts, in bytes.

Traits§

Handshake
One authentication exchange, driven by the caller.
MessageTransport
A blocking transport of whole messages: WebSocket messages, datagrams, a shared-memory queue.
Method
An authentication method: a configured factory of handshakes.
ReadTimeout
A blocking transport whose reads can be bounded in time, as the _until loops need to end at their deadline.
WriteTimeout
A blocking transport whose writes can be bounded in time, as the _until loops need when the peer stops reading.

Functions§

complete_messages
drive_messages without the final finish, as complete_stream is to drive_stream.
complete_messages_until
complete_messages that ends at deadline, as drive_messages_until does.
complete_stream
drive_stream without the final finish: returns the handshake once it is_complete, with everything it queued sent.
complete_stream_until
complete_stream that ends at deadline, as drive_stream_until does.
drive_messages
Runs handshake to completion over a blocking message transport. Each message the handshake sends is one transport message, and each received one is handed over whole, for either Framing.
drive_messages_until
drive_messages that ends at deadline: every receive and send waits at most what is left until it, through ReadTimeout and WriteTimeout, and both timeouts are cleared before it returns.
drive_stream
Runs handshake to completion over a blocking byte stream.
drive_stream_until
drive_stream that ends at deadline: every read and write waits at most what is left until it, through ReadTimeout and WriteTimeout, and both timeouts are cleared before it returns.
read_frame
Reads one frame written by write_frame. The buffer grows as bytes arrive, so a declared length costs nothing until the peer sends it.
write_frame
Writes message as one frame, its length first as four big-endian bytes, and flushes.