ic-auth 0.57.0

Canonical authentication, proof verification and bounded receiver/issuer state with optional stable upgrade checkpoints
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
//! Root authorization of an issuer delegation under protected host inputs.
//!
//! This component verifies a bounded root batch and certificate correspondence.
//! It is not portable-credential verification, enrollment installation or session
//! admission. The full credential verifier must meter the whole proof before
//! calling this component; this function itself changes no state or budget.

use crate::{
    claims::validate_grants,
    delegation_encoding::{
        DelegationEncodingError, batch_header_hash, certificate_hash, delegation_leaf_hash,
        derivation_path_hash, issuer_binding_hash,
    },
};
use ic_auth_protocol_types::{
    DelegatedRoleGrant, DelegationCertificate, DelegationLeaf, MerkleStep, Principal,
    RootBatchHeader,
};
use ic_platform::ecdsa::{PrehashVerifier, Signature, VerifyingKey};
use sha2::{Digest, Sha256};
use std::{error::Error, fmt};

///
/// RootSignature
///
/// Untrusted root-signature fields in the current ECDSA/secp256k1 profile.
///

#[derive(Clone, Copy, Debug)]
pub struct RootSignature<'a> {
    /// Submitted key name; it must match both the header and protected policy.
    pub key_name: &'a str,
    /// Ordered submitted derivation components.
    pub derivation_path: &'a [Vec<u8>],
    /// Submitted SEC1 key; byte identity must match the independently enrolled key.
    pub public_key: &'a [u8],
    /// Compact 64-byte signature; high-S values are never normalized into validity.
    pub signature: &'a [u8],
}

///
/// RootDelegationProof
///
/// Borrowed root proof; outer transport decoding must already be bounded.
///

#[derive(Clone, Copy, Debug)]
pub struct RootDelegationProof<'a> {
    /// Certificate whose identity is committed by a portable credential.
    pub certificate: &'a DelegationCertificate,
    /// Root-signed batch header.
    pub header: &'a RootBatchHeader,
    /// Issuer authorization leaf committed by the batch tree.
    pub leaf: &'a DelegationLeaf,
    /// Ordered sibling path, capped independently of byte material.
    pub witness: &'a [MerkleStep],
    /// Submitted root signature material.
    pub signature: RootSignature<'a>,
}

impl<'a> From<&'a ic_auth_protocol_types::RootDelegation> for RootDelegationProof<'a> {
    fn from(value: &'a ic_auth_protocol_types::RootDelegation) -> Self {
        Self {
            certificate: &value.certificate,
            header: &value.header,
            leaf: &value.leaf,
            witness: &value.witness,
            signature: RootSignature {
                key_name: &value.signature.key_name,
                derivation_path: &value.signature.derivation_path,
                public_key: &value.signature.public_key,
                signature: &value.signature.signature,
            },
        }
    }
}

///
/// RootKeyPolicy
///
/// Current independently enrolled root authority; never populate from a proof.
///

#[derive(Clone, Copy, Debug)]
pub struct RootKeyPolicy<'a> {
    /// Approved root canister.
    pub root: Principal,
    /// Network-approved ECDSA/secp256k1 key name.
    pub key_name: &'a str,
    /// Enrolled ordered derivation-path commitment.
    pub derivation_path_hash: [u8; 32],
    /// Enrolled SEC1 key, with exact byte identity retained.
    pub public_key: &'a [u8],
    /// Exact current enrolled version.
    pub key_version: u64,
    /// Inclusive key-version revocation floor.
    pub min_key_version: u64,
    /// Inclusive proof-epoch revocation floor.
    pub min_proof_epoch: u64,
    /// Inclusive registry-epoch floor.
    pub min_registry_epoch: u64,
    /// Inclusive enrollment start.
    pub valid_from_ns: u64,
    /// Exclusive enrollment expiry.
    pub accept_until_ns: u64,
    /// Maximum batch lifetime, bounding stale root-grant authority.
    pub max_revocation_latency_ns: u64,
}

///
/// DelegationLimits
///
/// Protected limits for the current zero-future-grace delegation profile.
///

#[derive(Clone, Copy, Debug)]
pub struct DelegationLimits {
    /// Maximum certificate validity duration.
    pub max_certificate_ttl_ns: u64,
    /// Maximum lifetime the certificate may delegate to a credential.
    pub max_token_ttl_ns: u64,
    /// Variable byte/entry allowance, also capped at 131072 bytes.
    pub max_variable_bytes: usize,
    /// Maximum sibling steps, also capped at the transport profile's 64.
    pub max_witness_steps: usize,
}

///
/// ValidityTarget
///
/// A validity window whose boundary failed admission.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ValidityTarget {
    /// Issuer authorization certificate.
    Certificate,
    /// Protected root-key enrollment.
    RootKey,
    /// Root-signed batch.
    Batch,
    /// Issuer leaf.
    Leaf,
}

///
/// DelegationError
///
/// Root proof refusal, without any installed authority or session mutation.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DelegationError {
    /// Variable material, field bounds or witness work exceeds admission limits.
    InputTooLarge,
    /// Certificate root/issuer is anonymous or differs from protected selection.
    IdentityMismatch,
    /// Certificate issue time is after its validity start.
    IssuedAfterStart,
    /// Empty or reversed validity interval.
    InvalidWindow(ValidityTarget),
    /// Validity starts in the future; this profile allows no grace.
    NotYetValid(ValidityTarget),
    /// Exclusive deadline has been reached.
    Expired(ValidityTarget),
    /// Certificate or delegated-token lifetime exceeds protected limits.
    CertificateLifetime,
    /// Grant shape/order or bounded canonical encoding failed.
    Encoding(DelegationEncodingError),
    /// Submitted issuer/seed commitment is inconsistent.
    IssuerBindingMismatch,
    /// Unsupported batch schema or disagreement between certificate, leaf and header.
    ProofBindingMismatch,
    /// Signature metadata disagrees with the submitted header.
    SignatureBindingMismatch,
    /// Submitted metadata disagrees with protected root-key enrollment.
    RootKeyMismatch,
    /// Version or epoch is below a protected revocation floor.
    StaleAuthority,
    /// Batch lifetime exceeds protected revocation latency.
    BatchLifetime,
    /// Issuer leaf and sibling path do not reproduce the signed root.
    InvalidWitness,
    /// SEC1 key, compact signature or ECDSA prehash verification is invalid.
    InvalidSignature,
    /// Signature has high-S form; the verifier does not repair it.
    HighSignatureScalar,
}

impl fmt::Display for DelegationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "root delegation refused: {self:?}")
    }
}

impl Error for DelegationError {}

///
/// VerifiedDelegation
///
/// Root-verified issuer authorization at one protected snapshot.
/// The certificate's issue-time metadata is checked for consistency but not
/// independently signed by the root. Later use still needs current enrollment,
/// claim binding, issuer signature, work charging and receiver/session policy.
/// ```compile_fail
/// use ic_auth::root_delegation::VerifiedDelegation;
/// use ic_auth_protocol_types::DelegationCertificate;
/// fn forge(cert: &DelegationCertificate) -> VerifiedDelegation<'_> { cert.into() }
/// ```
/// ```compile_fail
/// use ic_auth::root_delegation::VerifiedDelegation;
/// fn change(proof: &mut VerifiedDelegation<'_>) { proof.certificate().issued_at_ns = 0; }
/// ```
///

#[derive(Clone, Debug)]
pub struct VerifiedDelegation<'a> {
    certificate: &'a DelegationCertificate,
    certificate_hash: [u8; 32],
    expires_at_ns: u64,
}

impl<'a> VerifiedDelegation<'a> {
    /// Borrow the checked certificate, not a continuing authorization grant.
    #[must_use]
    pub const fn certificate(&self) -> &'a DelegationCertificate {
        self.certificate
    }

    /// Exact certificate identity, including its root-unsigned issuance metadata.
    #[must_use]
    pub const fn certificate_hash(&self) -> [u8; 32] {
        self.certificate_hash
    }

    /// Exclusive certificate deadline capped by current root-key enrollment.
    #[must_use]
    pub const fn expires_at_ns(&self) -> u64 {
        self.expires_at_ns
    }
}

/// Verify root authorization for the protected issuer, without installing it.
///
/// Bounds precede canonical encoding and cryptography. Current enrollment,
/// versions, epochs and windows are rechecked on every invocation. Platform owns
/// key/scalar parsing and ECDSA; SHA-256 only assembles the fixed protocol's
/// header and ordered Merkle commitments. Full credential admission must first
/// charge its shared receiver-wide budget; this standalone component does not.
pub fn verify_root_delegation<'a>(
    proof: RootDelegationProof<'a>,
    expected_issuer: Principal,
    policy: &RootKeyPolicy<'_>,
    now_ns: u64,
    limits: DelegationLimits,
) -> Result<VerifiedDelegation<'a>, DelegationError> {
    let mut remaining = limits.max_variable_bytes.min(131_072);
    consume_proof_bytes(&proof, limits.max_witness_steps, &mut remaining)?;
    verify_authorization(proof, expected_issuer, policy, now_ns, limits)?;
    Ok(VerifiedDelegation {
        certificate: proof.certificate,
        certificate_hash: certificate_hash(proof.certificate).map_err(DelegationError::Encoding)?,
        expires_at_ns: proof
            .certificate
            .delegation
            .expires_at_ns
            .min(policy.accept_until_ns),
    })
}

// Callers admit aggregate material before entering this shared root verifier.
// The full credential path has already hashed the immutable certificate;
// authorization itself needs no second certificate commitment or copied result.
pub(crate) fn verify_authorization(
    proof: RootDelegationProof<'_>,
    expected_issuer: Principal,
    policy: &RootKeyPolicy<'_>,
    now_ns: u64,
    limits: DelegationLimits,
) -> Result<(), DelegationError> {
    use DelegationError as E;
    let cert = proof.certificate;
    let d = &cert.delegation;
    let h = proof.header;
    let leaf = proof.leaf;
    let signature = proof.signature;
    if d.root == Principal::anonymous()
        || d.issuer == Principal::anonymous()
        || d.root != policy.root
        || d.issuer != expected_issuer
    {
        return Err(E::IdentityMismatch);
    }
    if cert.issued_at_ns > d.not_before_ns {
        return Err(E::IssuedAfterStart);
    }
    if d.not_before_ns >= d.expires_at_ns {
        return Err(E::InvalidWindow(ValidityTarget::Certificate));
    }
    let ttl = d.expires_at_ns - d.not_before_ns;
    if ttl > limits.max_certificate_ttl_ns
        || d.max_token_ttl_ns == 0
        || d.max_token_ttl_ns > ttl.min(limits.max_token_ttl_ns)
    {
        return Err(E::CertificateLifetime);
    }
    window(
        ValidityTarget::Certificate,
        d.not_before_ns,
        d.expires_at_ns,
        now_ns,
    )?;
    validate_grants(&d.grants).map_err(|e| E::Encoding(DelegationEncodingError::Grants(e)))?;
    if issuer_binding_hash(d.issuer, d.seed_hash) != d.binding_hash {
        return Err(E::IssuerBindingMismatch);
    }
    if h.schema_version != 1 || h.root != policy.root || leaf.delegation.root != h.root {
        return Err(E::ProofBindingMismatch);
    }
    window(
        ValidityTarget::RootKey,
        policy.valid_from_ns,
        policy.accept_until_ns,
        now_ns,
    )?;
    window(
        ValidityTarget::Batch,
        h.not_before_ns,
        h.expires_at_ns,
        now_ns,
    )?;
    window(
        ValidityTarget::Leaf,
        leaf.delegation.not_before_ns,
        leaf.delegation.expires_at_ns,
        now_ns,
    )?;
    if leaf.delegation.not_before_ns < h.not_before_ns
        || leaf.delegation.expires_at_ns > h.expires_at_ns
        || leaf.proof_epoch != h.proof_epoch
        || leaf.registry_epoch != h.registry_epoch
        || leaf.registry_hash != h.registry_hash
        || leaf.delegation != *d
    {
        return Err(E::ProofBindingMismatch);
    }
    if signature.key_name != h.key_name {
        return Err(E::SignatureBindingMismatch);
    }
    if h.key_name != policy.key_name || signature.public_key != policy.public_key {
        return Err(E::RootKeyMismatch);
    }
    if derivation_path_hash(signature.derivation_path).map_err(E::Encoding)?
        != h.derivation_path_hash
    {
        return Err(E::SignatureBindingMismatch);
    }
    if h.derivation_path_hash != policy.derivation_path_hash || h.key_version != policy.key_version
    {
        return Err(E::RootKeyMismatch);
    }
    if h.key_version < policy.min_key_version
        || h.proof_epoch < policy.min_proof_epoch
        || h.registry_epoch < policy.min_registry_epoch
    {
        return Err(E::StaleAuthority);
    }
    if h.expires_at_ns - h.not_before_ns > policy.max_revocation_latency_ns {
        return Err(E::BatchLifetime);
    }
    let mut root = delegation_leaf_hash(leaf).map_err(E::Encoding)?;
    for step in proof.witness {
        let (left, right) = match step {
            MerkleStep::Left(hash) => (hash, &root),
            MerkleStep::Right(hash) => (&root, hash),
        };
        let mut digest = Sha256::new();
        digest.update([1]);
        digest.update(left);
        digest.update(right);
        root = digest.finalize().into();
    }
    if root != h.tree_root {
        return Err(E::InvalidWitness);
    }
    let key = VerifyingKey::from_sec1_bytes(policy.public_key).map_err(|_| E::InvalidSignature)?;
    let signature = Signature::from_slice(signature.signature).map_err(|_| E::InvalidSignature)?;
    if signature.normalize_s() != signature {
        return Err(E::HighSignatureScalar);
    }
    key.verify_prehash(&batch_header_hash(h).map_err(E::Encoding)?, &signature)
        .map_err(|_| E::InvalidSignature)?;
    Ok(())
}

fn window(target: ValidityTarget, start: u64, end: u64, now: u64) -> Result<(), DelegationError> {
    if start >= end {
        return Err(DelegationError::InvalidWindow(target));
    }
    if start > now {
        return Err(DelegationError::NotYetValid(target));
    }
    if now >= end {
        return Err(DelegationError::Expired(target));
    }
    Ok(())
}

fn take(remaining: &mut usize, count: usize) -> Result<(), DelegationError> {
    *remaining = remaining
        .checked_sub(count)
        .ok_or(DelegationError::InputTooLarge)?;
    Ok(())
}

fn grants_size(
    grants: &[DelegatedRoleGrant],
    remaining: &mut usize,
) -> Result<(), DelegationError> {
    take(remaining, grants.len())?;
    for grant in grants {
        take(remaining, grant.target.as_str().len())?;
        take(remaining, grant.scopes.len())?;
        for scope in &grant.scopes {
            take(remaining, scope.as_str().len())?;
        }
    }
    Ok(())
}

pub(crate) fn consume_proof_bytes(
    proof: &RootDelegationProof<'_>,
    max_witness_steps: usize,
    remaining: &mut usize,
) -> Result<(), DelegationError> {
    let s = proof.signature;
    if proof.witness.len() > max_witness_steps.min(64)
        || proof.header.key_name.len() > 128
        || s.key_name.len() > 128
        || s.public_key.len() > 65
        || s.signature.len() > 64
        || s.derivation_path.len() > 32
        || s.derivation_path.iter().any(|p| p.len() > 256)
    {
        return Err(DelegationError::InputTooLarge);
    }
    grants_size(&proof.certificate.delegation.grants, remaining)?;
    grants_size(&proof.leaf.delegation.grants, remaining)?;
    take(remaining, proof.witness.len() * 33)?;
    for count in [
        proof.header.key_name.len(),
        s.key_name.len(),
        s.public_key.len(),
        s.signature.len(),
        s.derivation_path.len(),
    ] {
        take(remaining, count)?;
    }
    for part in s.derivation_path {
        take(remaining, part.len())?;
    }
    Ok(())
}