khive_capability/durable/
mod.rs1mod codec;
8mod envelope;
9mod keys;
10mod permission;
11
12pub use envelope::{
13 AuthenticatedEnvelope, BootstrapSource, CreationOperation, EnvelopeDraft, EnvelopeIssuer,
14 EnvelopeVerifier, GrantBody, IssuanceAssurance, IssuanceContext, NativeCapability,
15 ParentBinding,
16};
17pub use keys::{KeyRef, ProvisionedKey, SuppliedKeys};
18pub use permission::*;
19
20pub const MAX_RECORD_BYTES: usize = 2 * 1024 * 1024;
21pub const MAX_REQUEST_BYTES: usize = 1024 * 1024;
22pub const MAX_COLLECTION_ITEMS: usize = 256;
23pub const ALIAS_START: u128 = 1_u128 << 126;
24pub const ALIAS_END: u128 = 1_u128 << 127;
25pub const CARRIER_VERSION: u16 = 1;
26
27#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
28pub enum EnvelopeError {
29 #[error("invalid input: {0}")]
30 InvalidInput(&'static str),
31 #[error("{field} has {measured} bytes/items; maximum is {bound}")]
32 OverLimit {
33 field: &'static str,
34 measured: usize,
35 bound: usize,
36 },
37 #[error("right not supported by the M1 profile: {0}")]
38 RightNotSupported(UnsupportedRight),
39 #[error("cryptographic or structural verification rejected: {0}")]
40 Reject(&'static str),
41 #[error("verification material unavailable: {0}")]
42 Unavailable(&'static str),
43 #[error("native local identifier capacity exhausted")]
44 IdSpaceExhausted,
45 #[error("native delegation failed: {0}")]
46 Native(String),
47}
48
49#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
50pub enum UnsupportedRight {
51 #[error("ordinary_namespace_right_not_supported_in_m1")]
52 OrdinaryNamespace,
53 #[error("claim_time_bundle_not_supported_in_m1")]
54 ClaimTimePlatformBundleInM1,
55 #[error("internal_producer_not_public_registration")]
56 InternalProducer,
57}
58
59pub type EnvelopeResult<T> = Result<T, EnvelopeError>;
60
61fn bound(field: &'static str, measured: usize, maximum: usize) -> EnvelopeResult<()> {
62 if measured > maximum {
63 return Err(EnvelopeError::OverLimit {
64 field,
65 measured,
66 bound: maximum,
67 });
68 }
69 Ok(())
70}
71fn nonempty(value: &str) -> EnvelopeResult<()> {
72 if value.is_empty() {
73 return Err(EnvelopeError::InvalidInput("empty exact text"));
74 }
75 bound("text", value.len(), MAX_RECORD_BYTES)
76}
77fn collection<T>(values: &[T], required: bool) -> EnvelopeResult<()> {
78 bound("collection", values.len(), MAX_COLLECTION_ITEMS)?;
79 if required && values.is_empty() {
80 return Err(EnvelopeError::InvalidInput("empty collection"));
81 }
82 Ok(())
83}
84fn sorted_unique<T: Ord + codec::Wire>(values: &[T], required: bool) -> EnvelopeResult<()> {
85 collection(values, required)?;
86 let mut previous = None;
87 for value in values {
88 let encoded = codec::encode(value)?;
89 if previous
90 .as_ref()
91 .is_some_and(|bytes: &Vec<u8>| bytes >= &encoded)
92 {
93 return Err(EnvelopeError::InvalidInput(
94 "collection must be sorted and unique",
95 ));
96 }
97 previous = Some(encoded);
98 }
99 Ok(())
100}
101
102#[cfg(test)]
103mod cold_tests;
104#[cfg(test)]
105mod tests;
106
107#[cfg(test)]
108mod fixtures;
109
110#[cfg(feature = "authority-store")]
111pub(crate) fn canonical_principal(value: &Principal) -> EnvelopeResult<Vec<u8>> {
112 codec::encode(value)
113}
114
115#[cfg(feature = "authority-store")]
116pub(crate) fn canonical_rights(value: &[RightSpec]) -> EnvelopeResult<Vec<u8>> {
117 codec::encode(&value.to_vec())
118}
119
120#[cfg(feature = "authority-store")]
121pub(crate) fn require_key_reference(keys: &SuppliedKeys, reference: &KeyRef) -> EnvelopeResult<()> {
122 let _ = keys.native(reference)?;
123 let _ = keys.extension(reference)?;
124 Ok(())
125}