Skip to main content

ratify_protocol/v2/
limits.rs

1//! Protocol v2 wire limits (SPEC-v2 §2.6, Go `limits_v2.go`) and
2//! configuration and clock bounds (SPEC-v2 §2.5, Go `clock_bounds_v2.go`).
3//!
4//! Every time value is integer Unix seconds and every deadline is exclusive.
5
6use super::errors::{err, plain, V2ErrorClass, V2Result};
7
8pub const MAX_DELEGATION_CERT_BYTES_V2: usize = 32 << 10;
9pub const MAX_PROOF_BUNDLE_BYTES_V2: usize = 128 << 10;
10pub const MAX_KEY_ROTATION_STATEMENT_BYTES_V2: usize = 32 << 10;
11pub const MAX_REVOCATION_PUSH_BYTES_V2: usize = 256 << 10;
12pub const MAX_REVOCATION_PUSH_REFS_V2: usize = 512;
13pub const MAX_REVOCATION_LIST_BYTES_V2: usize = 4 << 20;
14pub const MAX_REVOCATION_LIST_REFS_V2: usize = 10000;
15pub const MAX_WITNESS_ENTRY_BYTES_V2: usize = 64 << 10;
16pub const MAX_WITNESS_ENTRY_DATA_BYTES_V2: usize = 16 << 10;
17pub const MAX_VERIFICATION_RECEIPT_BYTES_V2: usize = 64 << 10;
18pub const MAX_VERIFICATION_RECEIPT_BODY_BYTES_V2: usize = 64 << 10;
19pub const MAX_STATUS_EVIDENCE_BATCH_BYTES_V2: usize = 8 << 20;
20pub const MAX_STATUS_EVIDENCE_BATCH_OBJECTS_V2: usize = 64;
21pub const MAX_TRANSACTION_RECEIPT_BYTES_V2: usize = 5 << 20;
22pub const MAX_TRANSACTION_PARTIES_V2: usize = 32;
23pub const MAX_CHECKPOINT_ACKNOWLEDGEMENT_BYTES_V2: usize = 1024;
24
25/// 9999-12-31T23:59:59Z. Timestamps are 0..=this.
26pub const V2_MAX_TIMESTAMP_SECONDS: i64 = 253402300799;
27pub const V2_MAX_FUTURE_SKEW_SECONDS: i64 = 300;
28pub const V2_MAX_CHALLENGE_AGE_SECONDS: i64 = 300;
29pub const V2_MAX_STATUS_LIFETIME_SECONDS: i64 = 86400;
30pub const V2_MAX_AUDIENCE_MEMBERSHIP_AGE_SECONDS: i64 = 86400;
31pub const V2_MAX_RATE_WINDOW_SECONDS: i64 = 86400;
32pub const V2_MAX_DELEGATION_LIFETIME_SECONDS: i64 = 315360000;
33/// 2^53 - 1.
34pub const V2_MAX_SAFE_INTEGER: i64 = 9007199254740991;
35
36/// The pre-parse size gate shared by every v2 decoder.
37pub(crate) fn check_v2_wire_size(data: &[u8], object: &str, limit_name: &str, limit: usize) -> V2Result<()> {
38    if data.len() > limit {
39        return plain(format!(
40            "wire v2: {object} exceeds {limit_name} ({limit} bytes); rejected before parsing"
41        ));
42    }
43    Ok(())
44}
45
46/// Whether `t` is inside the v2 timestamp domain.
47pub fn valid_v2_timestamp(t: i64) -> bool {
48    (0..=V2_MAX_TIMESTAMP_SECONDS).contains(&t)
49}
50
51/// Add two second counts inside the v2 timestamp domain; `None` if either
52/// operand is outside it.
53pub(crate) fn checked_add_v2(a: i64, b: i64) -> Option<i64> {
54    if !(0..=V2_MAX_TIMESTAMP_SECONDS).contains(&a) || !(0..=V2_MAX_TIMESTAMP_SECONDS).contains(&b) {
55        return None;
56    }
57    a.checked_add(b)
58}
59
60/// Exclusive challenge deadlines (Go `v2ChallengeTimeCurrent`).
61pub(crate) fn v2_challenge_time_current(
62    now: i64,
63    issued_at: i64,
64    expires_at: i64,
65    max_age: i64,
66    future_skew: i64,
67) -> bool {
68    if !valid_v2_timestamp(now) || !valid_v2_timestamp(issued_at) || !valid_v2_timestamp(expires_at) {
69        return false;
70    }
71    if issued_at > now && issued_at - now > future_skew {
72        return false;
73    }
74    match checked_add_v2(issued_at, max_age) {
75        Some(age_deadline) => now < expires_at && now < age_deadline,
76        None => false,
77    }
78}
79
80/// Request-level clock bounds (Go `validateV2FreshnessConfiguration`).
81pub(crate) fn validate_v2_freshness_configuration(
82    now: i64,
83    max_age_seconds: i64,
84    future_skew_seconds: i64,
85) -> V2Result<()> {
86    if !valid_v2_timestamp(now) {
87        return err(
88            V2ErrorClass::ConfigurationInvalid,
89            format!("v2 verifier time is outside 0..{V2_MAX_TIMESTAMP_SECONDS}"),
90        );
91    }
92    if !(1..=V2_MAX_CHALLENGE_AGE_SECONDS).contains(&max_age_seconds) {
93        return err(
94            V2ErrorClass::ConfigurationInvalid,
95            format!("v2 challenge maximum age must be 1..{V2_MAX_CHALLENGE_AGE_SECONDS} seconds"),
96        );
97    }
98    if !(0..=V2_MAX_FUTURE_SKEW_SECONDS).contains(&future_skew_seconds) {
99        return err(
100            V2ErrorClass::ConfigurationInvalid,
101            format!("v2 future skew must be 0..{V2_MAX_FUTURE_SKEW_SECONDS} seconds"),
102        );
103    }
104    Ok(())
105}