pic_continuity/lib.rs
1/*
2 * Copyright Nitro Agility S.r.l.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! PIC Profile 0.2 — continuity artifacts, Prover, and Verifier.
18//!
19//! Implements the artifact family and procedures of the
20//! *PIC Prover and Verifier Specification* (draft 0.2):
21//!
22//! - [`artifacts`] — PIC PCA COSE, PIC Continuity COSE, PIC Continuity
23//! Transition COSE, and the PIC Token JWT envelope;
24//! - [`authority`] — Logical Context of Authority, canonical Indexed
25//! Authority Map, removal bitmaps, execution-contract additions, and the
26//! non-expansion order;
27//! - [`proposal`] — the Initial Continuity Proposal JSON object and its
28//! `continuity_proposal` wire encoding (compact JSON, unpadded Base64url);
29//! - [`prover`] — builds a workload-signed advancement candidate from the
30//! current trusted checkpoint;
31//! - [`verifier`] — ordinary verification of settled artifacts and the
32//! settlement-authority validation procedure (the role PIC-X realizes);
33//! - [`por`] — the pluggable Proof of Relationship validation boundary;
34//! - [`trust`] — the traits a host supplies (trusted checkpoints, key
35//! material, revocation, policy);
36//! - [`cose`] — a generic, crypto-agnostic COSE_Sign1 envelope. Self
37//! contained by design: it is the candidate for extraction into its own
38//! crate when a second consumer needs it.
39//!
40//! The Verifier is pure: it performs no I/O, and everything environmental
41//! (trusted checkpoint state, issuer trust, revocation, policy) enters
42//! through the [`trust`] and [`por`] traits.
43//!
44//! # Roles
45//!
46//! | Role | Entry point |
47//! |------|-------------|
48//! | Workload / Prover | [`prover::build_candidate`] |
49//! | Ordinary verifier | [`verifier::verify_settled`] |
50//! | Settlement authority (realm) | [`verifier::SettlementAuthority::settle`], [`verifier::issue_settled`] |
51//!
52//! # Example: issue and verify a settled state
53//!
54//! The initialization path — a realm signs checkpoint 0 (for example after
55//! an OAuth-to-PIC token exchange) and any holder of the realm public key
56//! verifies the resulting PIC Token JWT offline:
57//!
58#![cfg_attr(feature = "ed25519", doc = "```")]
59#![cfg_attr(not(feature = "ed25519"), doc = "```ignore")]
60//! use pic_continuity::artifacts::PicPcaPayload;
61//! use pic_continuity::authority::{
62//! AuthorityValue, IndexedAuthorityMap, Invariant, LogicalAuthority,
63//! };
64//! use pic_continuity::trust::{Ed25519Signer, Ed25519Verifier};
65//! use pic_continuity::verifier::{issue_settled, verify_settled, SettlementContext};
66//! use std::collections::BTreeMap;
67//!
68//! // The realm signing key (settlement authority).
69//! let realm_key = ed25519_dalek::SigningKey::generate(&mut rand::rngs::OsRng);
70//! let realm = Ed25519Signer::new(realm_key, "https://pic-x.example.com/realms/acme#key-1");
71//!
72//! // A Logical Context of Authority, canonicalized deterministically.
73//! let mut contract = BTreeMap::new();
74//! contract.insert("corporation".into(), AuthorityValue::One("ACME".into()));
75//! let logical = LogicalAuthority::new(
76//! None,
77//! vec![Invariant::new(
78//! "documents:read:document-42", "read", "documents", "document-42",
79//! )],
80//! contract,
81//! );
82//! let authority = IndexedAuthorityMap::from_logical(&logical)?;
83//!
84//! // Checkpoint 0 with its verifier-issued challenge.
85//! let checkpoint = PicPcaPayload::new(0, authority, vec![0x7b; 32]);
86//! let issued = issue_settled(checkpoint, &realm, &SettlementContext {
87//! iss: "https://pic-x.example.com/realms/acme".into(),
88//! ..Default::default()
89//! })?;
90//!
91//! // Any workload with the realm public key verifies the settled token.
92//! let verifier = Ed25519Verifier::new(realm.verifying_key());
93//! let settled = verify_settled(&issued.token, &verifier)?;
94//! assert_eq!(settled.checkpoint.position, 0);
95//! # Ok::<(), pic_continuity::error::ContinuityError>(())
96//! ```
97//!
98//! Advancement is the [`prover::build_candidate`] →
99//! [`verifier::SettlementAuthority::settle`] round trip; the crate's
100//! `walkthrough` integration test exercises the full lineage
101//! (checkpoint 0 → 1 → 2) including attenuation and rejection paths.
102//!
103//! # Feature flags
104//!
105//! | Feature | Effect |
106//! |---------|--------|
107//! | `ed25519` *(default)* | [`trust::Ed25519Signer`] / [`trust::Ed25519Verifier`] and COSE `EdDSA` support via `ed25519-dalek` |
108//! | `p256` | ECDSA P-256 (`ES256`) COSE helpers |
109//! | `p384` | ECDSA P-384 (`ES384`) COSE helpers |
110//! | `full` | All of the above |
111//!
112//! The core protocol logic is crypto-agnostic: with no features enabled the
113//! crate still builds, and signing/verification enter through closures or
114//! the [`trust::ArtifactSigner`] / [`trust::ArtifactVerifier`] traits.
115//!
116//! # What this crate does not do
117//!
118//! - **SD-JWT Proof of Relationship validation** — deployment-specific;
119//! supply it through [`por::PorValidator`].
120//! - **Revocation state** — supply it through [`trust::RevocationCheck`].
121//! - **Transport, storage, OAuth endpoints** — this crate is pure protocol
122//! logic; PIC-X is the reference deployment that hosts it.
123
124#![warn(missing_docs)]
125
126pub mod artifacts;
127pub mod authority;
128pub mod cose;
129pub mod error;
130pub mod por;
131pub mod proposal;
132pub mod prover;
133pub mod trust;
134pub mod verifier;
135
136/// The active PIC profile identifier implemented by this crate.
137pub const PROFILE_0_2: &str = "https://pic-protocol.org/profiles/0.2";
138
139/// Profile 0.2 artifact format identifier for the PIC Token JWT.
140pub const FORMAT_PIC_TOKEN_JWT: &str = "pic+jwt";
141/// Profile 0.2 artifact format identifier for the PIC PCA COSE.
142pub const FORMAT_PIC_PCA_COSE: &str = "pic-pca+cose";
143/// Profile 0.2 artifact format identifier for the PIC Continuity COSE.
144pub const FORMAT_PIC_CONTINUITY_COSE: &str = "pic-continuity+cose";
145/// Profile 0.2 artifact format identifier for the PIC Continuity Transition COSE.
146pub const FORMAT_PIC_TRANSITION_COSE: &str = "pic-continuity-transition+cose";
147
148/// Stable semantic URI for the PIC token type (RFC 8693 token exchange binding).
149pub const TOKEN_TYPE_PIC: &str = "https://pic-protocol.org/definitions/token-types/pic";
150/// Stable semantic URI for the Initial Continuity Proposal type.
151pub const PROPOSAL_TYPE_CONTINUITY_INITIAL: &str =
152 "https://pic-protocol.org/definitions/proposal-types/continuity-initial";
153
154/// The Proof of Relationship type required by current Profile 0.2.
155pub const POR_TYPE_SD_JWT: &str = "sd-jwt";
156/// The predecessor reference type required by current Profile 0.2.
157pub const PREDECESSOR_TYPE_PCA: &str = "pca";
158
159/// This crate's version, as published.
160pub fn continuity_version() -> &'static str {
161 env!("CARGO_PKG_VERSION")
162}