Implement independent signed time contract with native Linux evidence
Rust development slice / linux (push) Failing after 6s
Rust development slice / linux (push) Failing after 6s
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
[package]
|
||||
name = "rmm-time-attestation"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
anyhow.workspace = true
|
||||
ed25519-dalek = "=2.2.0"
|
||||
hex.workspace = true
|
||||
rand.workspace = true
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
sha2.workspace = true
|
||||
uuid.workspace = true
|
||||
@@ -0,0 +1,555 @@
|
||||
//! Independent online time comparison, never a sync lease or execution permit.
|
||||
//! Nanoseconds are signed UTC Unix ns, intervals are unsigned ns. Callers must
|
||||
//! hold their own native clock observations and protected context/pins.
|
||||
use anyhow::{Result, ensure};
|
||||
use ed25519_dalek::{Signature, Signer, SigningKey, VerifyingKey};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::collections::BTreeMap;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub const ENDPOINT: &str = "https://discovery.ease-technologies.de/api/agent/v1/time-attestation";
|
||||
pub const MAX_RTT_NS: u64 = 1_000_000_000;
|
||||
pub const MAX_RECEIVED_AGE_NS: u64 = 100_000_000;
|
||||
pub const MAX_CAPTURE_ERROR_NS: u64 = 50_000_000;
|
||||
pub const MAX_SERVER_ERROR_NS: u64 = 500_000_000;
|
||||
pub const MAX_SYNC_AGE_NS: u64 = 3_600_000_000_000;
|
||||
const DOMAIN: &[u8] = b"RMM-TIME-ATTESTATION/v1\0";
|
||||
const MAX_BYTES: usize = 8192;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum Role {
|
||||
Agent,
|
||||
SystemHelper,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Context {
|
||||
pub tenant_id: Uuid,
|
||||
pub asset_id: Uuid,
|
||||
pub agent_id: Uuid,
|
||||
pub enrollment_generation: u64,
|
||||
pub assignment_generation: u64,
|
||||
pub connection_generation: u64,
|
||||
pub windows_boot_id: Uuid,
|
||||
pub verifier_incarnation: Uuid,
|
||||
pub role: Role,
|
||||
pub helper_image_sha256: Option<String>,
|
||||
pub binding_schema: u32,
|
||||
}
|
||||
impl Context {
|
||||
pub fn validate(&self) -> Result<()> {
|
||||
ensure!(
|
||||
[
|
||||
self.tenant_id,
|
||||
self.asset_id,
|
||||
self.agent_id,
|
||||
self.windows_boot_id,
|
||||
self.verifier_incarnation
|
||||
]
|
||||
.iter()
|
||||
.all(|v| !v.is_nil()),
|
||||
"TIME_CONTEXT_ID"
|
||||
);
|
||||
ensure!(
|
||||
self.enrollment_generation > 0
|
||||
&& self.assignment_generation > 0
|
||||
&& self.connection_generation > 0
|
||||
&& self.binding_schema == 1,
|
||||
"TIME_CONTEXT_GENERATION"
|
||||
);
|
||||
match self.role {
|
||||
Role::Agent => ensure!(
|
||||
self.helper_image_sha256.is_none(),
|
||||
"TIME_AGENT_HELPER_IMAGE"
|
||||
),
|
||||
Role::SystemHelper => {
|
||||
exact_hex(self.helper_image_sha256.as_deref().unwrap_or(""), 32)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Request {
|
||||
pub version: u32,
|
||||
pub endpoint: String,
|
||||
pub nonce: String,
|
||||
pub context: Context,
|
||||
}
|
||||
impl Request {
|
||||
pub fn validate(&self) -> Result<()> {
|
||||
ensure!(
|
||||
self.version == 1 && self.endpoint == ENDPOINT,
|
||||
"TIME_REQUEST_PROFILE"
|
||||
);
|
||||
exact_hex(&self.nonce, 32)?;
|
||||
self.context.validate()
|
||||
}
|
||||
pub fn sha256(&self) -> Result<String> {
|
||||
self.validate()?;
|
||||
Ok(hex::encode(Sha256::digest(serde_json::to_vec(self)?)))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Payload {
|
||||
pub version: u32,
|
||||
pub request_sha256: String,
|
||||
pub server_instance: Uuid,
|
||||
pub server_boot_id: Uuid,
|
||||
pub key_id: String,
|
||||
pub key_generation: u64,
|
||||
pub captured_utc_ns: i64,
|
||||
pub server_error_ns: u64,
|
||||
pub source_sync_age_ns: u64,
|
||||
}
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct SignedAttestation {
|
||||
/// Exact canonical JSON bytes (UTF-8); verification never normalizes input.
|
||||
pub payload: String,
|
||||
pub signature_hex: String,
|
||||
}
|
||||
fn exact_hex(value: &str, bytes: usize) -> Result<()> {
|
||||
ensure!(
|
||||
value.len() == bytes * 2
|
||||
&& value
|
||||
.bytes()
|
||||
.all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)),
|
||||
"TIME_HEX_FORMAT"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
fn validate_payload(p: &Payload) -> Result<()> {
|
||||
ensure!(
|
||||
p.version == 1 && !p.server_instance.is_nil() && !p.server_boot_id.is_nil(),
|
||||
"TIME_SERVER_CONTEXT"
|
||||
);
|
||||
ensure!(
|
||||
!p.key_id.is_empty()
|
||||
&& p.key_id.len() <= 64
|
||||
&& p.key_id
|
||||
.bytes()
|
||||
.all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_')
|
||||
&& p.key_generation > 0,
|
||||
"TIME_KEY_CONTEXT"
|
||||
);
|
||||
exact_hex(&p.request_sha256, 32)?;
|
||||
ensure!(
|
||||
p.captured_utc_ns > 0
|
||||
&& p.server_error_ns <= MAX_SERVER_ERROR_NS
|
||||
&& p.source_sync_age_ns <= MAX_SYNC_AGE_NS,
|
||||
"TIME_SERVER_QUALITY"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
/// Only server code which has independently qualified the native source may
|
||||
/// construct/sign this payload. This codec is not a provider qualification.
|
||||
pub fn sign(payload: &Payload, key: &SigningKey) -> Result<SignedAttestation> {
|
||||
validate_payload(payload)?;
|
||||
let raw = serde_json::to_string(payload)?;
|
||||
let mut message = DOMAIN.to_vec();
|
||||
message.extend_from_slice(raw.as_bytes());
|
||||
Ok(SignedAttestation {
|
||||
payload: raw,
|
||||
signature_hex: hex::encode(key.sign(&message).to_bytes()),
|
||||
})
|
||||
}
|
||||
/// Loaded through the verifier's protected configuration, never an HTTP reply.
|
||||
pub struct Pins {
|
||||
generation: u64,
|
||||
server_instance: Uuid,
|
||||
keys: BTreeMap<String, VerifyingKey>,
|
||||
}
|
||||
impl Pins {
|
||||
pub fn new(
|
||||
generation: u64,
|
||||
server_instance: Uuid,
|
||||
keys: BTreeMap<String, [u8; 32]>,
|
||||
) -> Result<Self> {
|
||||
ensure!(
|
||||
generation > 0 && !server_instance.is_nil() && !keys.is_empty() && keys.len() <= 8,
|
||||
"TIME_PINS"
|
||||
);
|
||||
let keys = keys
|
||||
.into_iter()
|
||||
.map(|(id, raw)| Ok((id, VerifyingKey::from_bytes(&raw)?)))
|
||||
.collect::<Result<_>>()?;
|
||||
Ok(Self {
|
||||
generation,
|
||||
server_instance,
|
||||
keys,
|
||||
})
|
||||
}
|
||||
}
|
||||
/// A native, locally captured observation; ns counters share one process's OS
|
||||
/// clock domains. capture_error_ns must come from platform qualification.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct Sample {
|
||||
pub wall_utc_ns: i64,
|
||||
pub interrupt_ns: u64,
|
||||
pub unbiased_ns: u64,
|
||||
pub capture_error_ns: u64,
|
||||
}
|
||||
impl Sample {
|
||||
fn validate(self) -> Result<()> {
|
||||
ensure!(
|
||||
self.wall_utc_ns > 0 && self.capture_error_ns <= MAX_CAPTURE_ERROR_NS,
|
||||
"TIME_LOCAL_SAMPLE"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
/// Opaque, noncloneable request authority: one attempt consumes it even when
|
||||
/// signature/quality checks fail. A transported Request alone is no authority.
|
||||
pub struct Challenge {
|
||||
request: Request,
|
||||
begin: Sample,
|
||||
epoch: u64,
|
||||
verifier_id: Uuid,
|
||||
}
|
||||
impl Challenge {
|
||||
pub fn request(&self) -> &Request {
|
||||
&self.request
|
||||
}
|
||||
}
|
||||
/// Lives with the verifier process; callers must not reconstruct it to clear a
|
||||
/// discontinuity. Native adapters additionally preserve the process-wide fence.
|
||||
pub struct Verifier {
|
||||
context: Context,
|
||||
pins: Pins,
|
||||
previous: Option<Sample>,
|
||||
epoch: u64,
|
||||
pending_nonce: Option<String>,
|
||||
verifier_id: Uuid,
|
||||
}
|
||||
impl Verifier {
|
||||
pub fn new(context: Context, pins: Pins) -> Result<Self> {
|
||||
context.validate()?;
|
||||
Ok(Self {
|
||||
context,
|
||||
pins,
|
||||
previous: None,
|
||||
epoch: 1,
|
||||
pending_nonce: None,
|
||||
verifier_id: Uuid::new_v4(),
|
||||
})
|
||||
}
|
||||
pub fn invalidate(&mut self) {
|
||||
self.pending_nonce = None;
|
||||
self.epoch = if self.epoch == 0 {
|
||||
0
|
||||
} else {
|
||||
self.epoch.checked_add(1).unwrap_or(0)
|
||||
};
|
||||
}
|
||||
pub fn observe(&mut self, sample: Sample) -> Result<()> {
|
||||
if let Err(error) = sample.validate() {
|
||||
self.invalidate();
|
||||
return Err(error);
|
||||
}
|
||||
let continuous = self.previous.is_none_or(|p| {
|
||||
match (
|
||||
sample.wall_utc_ns.checked_sub(p.wall_utc_ns),
|
||||
sample.interrupt_ns.checked_sub(p.interrupt_ns),
|
||||
sample.unbiased_ns.checked_sub(p.unbiased_ns),
|
||||
) {
|
||||
(Some(w), Some(i), Some(u)) if w >= 0 => {
|
||||
(w as u64).abs_diff(i) <= MAX_RTT_NS && i.abs_diff(u) <= MAX_RTT_NS
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
});
|
||||
self.previous = Some(sample);
|
||||
if !continuous {
|
||||
self.invalidate();
|
||||
}
|
||||
ensure!(continuous, "TIME_DISCONTINUITY");
|
||||
Ok(())
|
||||
}
|
||||
pub fn begin(&mut self, sample: Sample) -> Result<Challenge> {
|
||||
self.observe(sample)?;
|
||||
ensure!(
|
||||
self.epoch > 0 && self.pending_nonce.is_none(),
|
||||
"TIME_CHALLENGE_OUTSTANDING_OR_EXHAUSTED"
|
||||
);
|
||||
let nonce: [u8; 32] = rand::random();
|
||||
let request = Request {
|
||||
version: 1,
|
||||
endpoint: ENDPOINT.into(),
|
||||
nonce: hex::encode(nonce),
|
||||
context: self.context.clone(),
|
||||
};
|
||||
request.validate()?;
|
||||
self.pending_nonce = Some(request.nonce.clone());
|
||||
Ok(Challenge {
|
||||
request,
|
||||
begin: sample,
|
||||
epoch: self.epoch,
|
||||
verifier_id: self.verifier_id,
|
||||
})
|
||||
}
|
||||
pub fn finish(
|
||||
&mut self,
|
||||
challenge: Challenge,
|
||||
signed: &SignedAttestation,
|
||||
received: Sample,
|
||||
capture_checked: impl FnOnce() -> Result<Sample>,
|
||||
) -> Result<Interval> {
|
||||
let owned = challenge.verifier_id == self.verifier_id
|
||||
&& self.pending_nonce.as_deref() == Some(challenge.request.nonce.as_str())
|
||||
&& challenge.epoch == self.epoch
|
||||
&& challenge.request.context == self.context;
|
||||
self.pending_nonce = None;
|
||||
ensure!(owned, "TIME_CHALLENGE_NOT_OWNED");
|
||||
self.observe(received)?;
|
||||
ensure!(signed.payload.len() <= MAX_BYTES, "TIME_PAYLOAD_LIMIT");
|
||||
let p: Payload = serde_json::from_str(&signed.payload)?;
|
||||
ensure!(
|
||||
serde_json::to_string(&p)? == signed.payload,
|
||||
"TIME_NONCANONICAL_PAYLOAD"
|
||||
);
|
||||
validate_payload(&p)?;
|
||||
ensure!(
|
||||
p.request_sha256 == challenge.request.sha256()?
|
||||
&& p.server_instance == self.pins.server_instance
|
||||
&& p.key_generation == self.pins.generation,
|
||||
"TIME_BINDING"
|
||||
);
|
||||
let key = self
|
||||
.pins
|
||||
.keys
|
||||
.get(&p.key_id)
|
||||
.ok_or_else(|| anyhow::anyhow!("TIME_KEY_NOT_PINNED"))?;
|
||||
exact_hex(&signed.signature_hex, 64)?;
|
||||
let signature = Signature::from_slice(&hex::decode(&signed.signature_hex)?)?;
|
||||
let mut message = DOMAIN.to_vec();
|
||||
message.extend_from_slice(signed.payload.as_bytes());
|
||||
key.verify_strict(&message, &signature)?;
|
||||
let checked = capture_checked()?;
|
||||
self.observe(checked)?;
|
||||
let elapsed = checked
|
||||
.interrupt_ns
|
||||
.checked_sub(challenge.begin.interrupt_ns)
|
||||
.ok_or_else(|| anyhow::anyhow!("TIME_COUNTER"))?;
|
||||
let age = checked
|
||||
.interrupt_ns
|
||||
.checked_sub(received.interrupt_ns)
|
||||
.ok_or_else(|| anyhow::anyhow!("TIME_COUNTER"))?;
|
||||
ensure!(
|
||||
elapsed <= MAX_RTT_NS && age <= MAX_RECEIVED_AGE_NS,
|
||||
"TIME_STALE_RESPONSE"
|
||||
);
|
||||
// Include every local capture budget conservatively, never RTT/2.
|
||||
let delta = challenge
|
||||
.begin
|
||||
.capture_error_ns
|
||||
.checked_add(received.capture_error_ns)
|
||||
.and_then(|d| d.checked_add(checked.capture_error_ns))
|
||||
.ok_or_else(|| anyhow::anyhow!("TIME_ERROR_OVERFLOW"))?;
|
||||
ensure!(delta <= MAX_CAPTURE_ERROR_NS, "TIME_CAPTURE_BUDGET");
|
||||
ensure!(
|
||||
p.source_sync_age_ns
|
||||
.checked_add(elapsed)
|
||||
.and_then(|v| v.checked_add(delta))
|
||||
.is_some_and(|v| v <= MAX_SYNC_AGE_NS),
|
||||
"TIME_SOURCE_AGE_AT_CHECK"
|
||||
);
|
||||
let lower = i128::from(p.captured_utc_ns) - i128::from(p.server_error_ns);
|
||||
let upper = i128::from(p.captured_utc_ns)
|
||||
+ i128::from(p.server_error_ns)
|
||||
+ i128::from(elapsed)
|
||||
+ i128::from(delta);
|
||||
let wall = i128::from(checked.wall_utc_ns);
|
||||
ensure!(
|
||||
(wall - lower).abs().max((wall - upper).abs()) <= 2_000_000_000,
|
||||
"TIME_LOCAL_WALL_OUTSIDE_BOUND"
|
||||
);
|
||||
Ok(Interval {
|
||||
lower_utc_ns: lower.try_into()?,
|
||||
upper_utc_ns: upper.try_into()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
/// Fresh result only. Adapters must recapture/reverify for later use; this type
|
||||
/// carries no lease, serialization or execution authorization.
|
||||
#[derive(Debug)]
|
||||
pub struct Interval {
|
||||
lower_utc_ns: i64,
|
||||
upper_utc_ns: i64,
|
||||
}
|
||||
impl Interval {
|
||||
pub fn lower_utc_ns(&self) -> i64 {
|
||||
self.lower_utc_ns
|
||||
}
|
||||
pub fn upper_utc_ns(&self) -> i64 {
|
||||
self.upper_utc_ns
|
||||
}
|
||||
pub fn allows_start(&self, not_before_ns: i64, start_before_ns: i64) -> bool {
|
||||
self.lower_utc_ns >= not_before_ns && self.upper_utc_ns < start_before_ns
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
const T: i64 = 1_800_000_000_000_000_000;
|
||||
fn sample(elapsed: u64) -> Sample {
|
||||
Sample {
|
||||
wall_utc_ns: T + elapsed as i64,
|
||||
interrupt_ns: 10_000_000_000 + elapsed,
|
||||
unbiased_ns: 9_000_000_000 + elapsed,
|
||||
capture_error_ns: 1_000_000,
|
||||
}
|
||||
}
|
||||
fn context(role: Role) -> Context {
|
||||
Context {
|
||||
tenant_id: Uuid::new_v4(),
|
||||
asset_id: Uuid::new_v4(),
|
||||
agent_id: Uuid::new_v4(),
|
||||
enrollment_generation: 1,
|
||||
assignment_generation: 1,
|
||||
connection_generation: 2,
|
||||
windows_boot_id: Uuid::new_v4(),
|
||||
verifier_incarnation: Uuid::new_v4(),
|
||||
role,
|
||||
helper_image_sha256: (role == Role::SystemHelper).then(|| "ab".repeat(32)),
|
||||
binding_schema: 1,
|
||||
}
|
||||
}
|
||||
fn fixture(context: Context) -> (Verifier, SigningKey, Uuid) {
|
||||
let key = SigningKey::from_bytes(&rand::random());
|
||||
let server = Uuid::new_v4();
|
||||
let pins = Pins::new(
|
||||
4,
|
||||
server,
|
||||
BTreeMap::from([("time1".into(), key.verifying_key().to_bytes())]),
|
||||
)
|
||||
.unwrap();
|
||||
(Verifier::new(context, pins).unwrap(), key, server)
|
||||
}
|
||||
fn reply(c: &Challenge, key: &SigningKey, server: Uuid) -> SignedAttestation {
|
||||
sign(
|
||||
&Payload {
|
||||
version: 1,
|
||||
request_sha256: c.request().sha256().unwrap(),
|
||||
server_instance: server,
|
||||
server_boot_id: Uuid::new_v4(),
|
||||
key_id: "time1".into(),
|
||||
key_generation: 4,
|
||||
captured_utc_ns: T + 20_000_000,
|
||||
server_error_ns: 10_000_000,
|
||||
source_sync_age_ns: 100_000_000_000,
|
||||
},
|
||||
key,
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
#[test]
|
||||
fn signed_roundtrip_uses_entire_rtt_and_strict_deadline_interval() {
|
||||
let (mut v, key, server) = fixture(context(Role::SystemHelper));
|
||||
let c = v.begin(sample(0)).unwrap();
|
||||
let signed = reply(&c, &key, server);
|
||||
let result = v
|
||||
.finish(c, &signed, sample(70_000_000), || Ok(sample(80_000_000)))
|
||||
.unwrap();
|
||||
assert_eq!(result.lower_utc_ns(), T + 10_000_000);
|
||||
assert_eq!(result.upper_utc_ns(), T + 113_000_000);
|
||||
assert!(result.allows_start(T, T + 114_000_000));
|
||||
assert!(!result.allows_start(T, T + 113_000_000));
|
||||
assert!(!result.allows_start(T + 11_000_000, T + 114_000_000));
|
||||
}
|
||||
#[test]
|
||||
fn tampering_wrong_role_nonce_key_and_delays_never_open() {
|
||||
for mutation in 0..7 {
|
||||
let (mut v, key, server) = fixture(context(Role::Agent));
|
||||
let c = v.begin(sample(0)).unwrap();
|
||||
let mut signed = reply(&c, &key, server);
|
||||
let mut received = sample(70_000_000);
|
||||
let mut checked = sample(80_000_000);
|
||||
match mutation {
|
||||
0 => {
|
||||
let mut bytes = hex::decode(&signed.signature_hex).unwrap();
|
||||
bytes[0] ^= 1;
|
||||
signed.signature_hex = hex::encode(bytes);
|
||||
}
|
||||
1 => {
|
||||
let mut p: Payload = serde_json::from_str(&signed.payload).unwrap();
|
||||
p.request_sha256 = "cd".repeat(32);
|
||||
signed = sign(&p, &key).unwrap();
|
||||
}
|
||||
2 => {
|
||||
let other = SigningKey::from_bytes(&rand::random());
|
||||
signed = reply(&c, &other, server);
|
||||
}
|
||||
3 => checked = sample(MAX_RTT_NS + 1),
|
||||
4 => {
|
||||
received = sample(10_000_000);
|
||||
checked = sample(110_000_001);
|
||||
}
|
||||
5 => {
|
||||
let mut p: Payload = serde_json::from_str(&signed.payload).unwrap();
|
||||
p.key_generation = 3;
|
||||
signed = sign(&p, &key).unwrap();
|
||||
}
|
||||
_ => signed.payload.push(' '),
|
||||
}
|
||||
assert!(
|
||||
v.finish(c, &signed, received, || Ok(checked)).is_err(),
|
||||
"mutation {mutation}"
|
||||
);
|
||||
assert!(v.pending_nonce.is_none());
|
||||
}
|
||||
let ctx = context(Role::Agent);
|
||||
let (mut a, key, server) = fixture(ctx.clone());
|
||||
let (mut b, _, _) = fixture(ctx);
|
||||
let ca = a.begin(sample(0)).unwrap();
|
||||
let signed = reply(&ca, &key, server);
|
||||
let _cb = b.begin(sample(0)).unwrap();
|
||||
assert!(
|
||||
b.finish(ca, &signed, sample(20_000_000), || Ok(sample(30_000_000)))
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn foreign_reply_cannot_reenable_an_older_own_challenge() {
|
||||
let (mut a, key_a, server_a) = fixture(context(Role::Agent));
|
||||
let (mut b, key_b, server_b) = fixture(context(Role::Agent));
|
||||
let ca = a.begin(sample(0)).unwrap();
|
||||
let reply_a = reply(&ca, &key_a, server_a);
|
||||
let old_b = b.begin(sample(0)).unwrap();
|
||||
let old_reply_b = reply(&old_b, &key_b, server_b);
|
||||
assert!(
|
||||
b.finish(ca, &reply_a, sample(10_000_000), || Ok(sample(20_000_000)))
|
||||
.is_err()
|
||||
);
|
||||
let _current_b = b.begin(sample(30_000_000)).unwrap();
|
||||
assert!(
|
||||
b.finish(old_b, &old_reply_b, sample(40_000_000), || Ok(sample(
|
||||
50_000_000
|
||||
)))
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn discontinuity_invalidates_outstanding_and_requires_new_challenge() {
|
||||
let (mut v, key, server) = fixture(context(Role::SystemHelper));
|
||||
let c = v.begin(sample(0)).unwrap();
|
||||
let signed = reply(&c, &key, server);
|
||||
let mut resumed = sample(2_000_000_000);
|
||||
resumed.unbiased_ns = 9_000_000_000;
|
||||
assert!(v.observe(resumed).is_err());
|
||||
assert!(v.finish(c, &signed, resumed, || Ok(resumed)).is_err());
|
||||
// A new post-discontinuity challenge is permitted, no old nonce reuse.
|
||||
let c = v.begin(resumed).unwrap();
|
||||
let mut p: Payload = serde_json::from_str(&reply(&c, &key, server).payload).unwrap();
|
||||
p.captured_utc_ns = resumed.wall_utc_ns;
|
||||
let signed = sign(&p, &key).unwrap();
|
||||
assert!(v.finish(c, &signed, resumed, || Ok(resumed)).is_ok());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user