Files
Legatus/crates/protocol/src/lib.rs
T

63 lines
2.2 KiB
Rust

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! The Legatus protocol v1 (docs/02_protocol.md): what Legatus and the Fabrica server say to
//! each other, and the checks both sides make. Pure: no files, no network, no clock; the caller
//! passes `now` (unix seconds) and the random bytes.
//!
//! - [`EnrollmentCode`]: the one-line code; [`EnrollmentBook`]: the server's single-use logic.
//! - [`msg`]: the messages; [`parse_job`] reads a job so that even a bad one can be reported.
//! - [`JobGuard`]: expired, future, too long-lived and replayed jobs are refused.
//! - [`Registry`]: the server's check that a certificate is the agent's current one.
//! - [`verify_release`], [`check_artifact`]: nothing unsigned runs.
//! - [`check_entry`]: what may be unpacked from an archive.
#![warn(missing_docs)]
mod archive;
mod code;
mod enroll;
mod guard;
pub mod msg;
mod registry;
mod release;
pub use archive::{ArchiveError, EntryKind, MAX_ARCHIVE_BYTES, MAX_ARCHIVE_ENTRIES, check_entry};
pub use code::{CODE_PREFIX, CodeError, EnrollmentCode};
pub use enroll::{
CODE_MAX_TTL_SECONDS, CODE_TTL_SECONDS, CodeRecord, EnrollError, EnrollmentBook, Grant,
IssueError, secret_digest,
};
pub use guard::{GuardError, JobGuard, MAX_JOB_SECONDS, SKEW_SECONDS};
pub use msg::{
Job, JobBody, JobHeader, MAX_BODY, MsgError, Report, Roles, Status, Step, decode, encode,
parse_job,
};
pub use registry::{Refusal, Registry};
pub use release::{
Approved, ArtifactEntry, Expected, MAX_ARTIFACT_BYTES, Manifest, ReleaseError, check_artifact,
valid_artifact_name, valid_version, verify_release,
};
/// Lowercase hex.
pub fn hex(b: &[u8]) -> String {
b.iter().map(|x| format!("{x:02x}")).collect()
}
/// Lowercase or uppercase hex to bytes.
pub(crate) fn unhex(s: &str) -> Option<Vec<u8>> {
if !s.len().is_multiple_of(2) {
return None;
}
(0..s.len())
.step_by(2)
.map(|i| u8::from_str_radix(s.get(i..i + 2)?, 16).ok())
.collect()
}
/// SHA-256 of `b`, lowercase hex.
pub fn sha256_hex(b: &[u8]) -> String {
hex(ring::digest::digest(&ring::digest::SHA256, b).as_ref())
}