Skip to main content

ic_auth_wire/
projection.rs

1//! Consuming projection from bounded Wire records to passive Auth values.
2//!
3//! No caller, clock, enrollment, verifier selection or endpoint is supplied here.
4//! Opaque buffers and strings are moved. Only label grammar is checked; signed
5//! order, duplicate commitments, counts, schema and cryptographic validity remain
6//! the verifier's responsibility. Nothing is sorted or repaired into validity.
7
8use crate::model as wire;
9use ic_auth_protocol_types as domain;
10use std::{error::Error, fmt};
11
12///
13/// ProjectionError
14///
15/// A transport label cannot be represented by the passive domain grammar.
16///
17
18#[derive(Clone, Copy, Debug, Eq, PartialEq)]
19pub enum ProjectionError {
20    /// Invalid role spelling, without normalization.
21    Role(domain::RoleError),
22    /// Invalid scope spelling, without normalization.
23    Scope(domain::ScopeLabelError),
24}
25
26impl fmt::Display for ProjectionError {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        match self {
29            Self::Role(e) => e.fmt(f),
30            Self::Scope(e) => e.fmt(f),
31        }
32    }
33}
34
35impl Error for ProjectionError {}
36
37impl TryFrom<wire::PortableCredential> for domain::Credential {
38    type Error = ProjectionError;
39    fn try_from(value: wire::PortableCredential) -> Result<Self, Self::Error> {
40        Ok(Self {
41            claims: value.claims.into_vec(),
42            proof: match value.proof {
43                wire::PortableProof::Ed25519(signature) => {
44                    domain::CredentialProof::Ed25519(signature.into_vec())
45                }
46                wire::PortableProof::Certified(proof) => {
47                    domain::CredentialProof::Certified(Box::new(certified(proof)?))
48                }
49            },
50        })
51    }
52}
53
54fn audience(value: wire::DelegationAudience) -> domain::AudienceId {
55    let wire::DelegationAudience::Fleet(value) = value;
56    domain::AudienceId {
57        canonical_network_id: domain::CanonicalId::from_bytes(value.canonical_network_id),
58        fleet_id: domain::CanonicalId::from_bytes(value.fleet_id),
59    }
60}
61
62fn grants(
63    value: ic_wire::List<wire::DelegatedRoleGrant, 16>,
64) -> Result<Vec<domain::DelegatedRoleGrant>, ProjectionError> {
65    value
66        .into_vec()
67        .into_iter()
68        .map(|grant| {
69            Ok(domain::DelegatedRoleGrant {
70                target: domain::AuthRole::try_from(grant.target.into_string())
71                    .map_err(ProjectionError::Role)?,
72                scopes: grant
73                    .scopes
74                    .into_vec()
75                    .into_iter()
76                    .map(|scope| {
77                        domain::ScopeLabel::try_from(scope.into_string())
78                            .map_err(ProjectionError::Scope)
79                    })
80                    .collect::<Result<_, _>>()?,
81            })
82        })
83        .collect()
84}
85
86fn certified(
87    value: wire::CertifiedProof,
88) -> Result<domain::CertifiedProofMaterial, ProjectionError> {
89    let cert = value.delegation.cert;
90    let wire::IssuerProofAlgorithm::IcCanisterSignatureV1 = cert.issuer_proof_alg;
91    let wire::IssuerProofBinding::IcCanisterSignatureV1(seed_hash) = cert.issuer_proof_binding;
92    let certificate = domain::DelegationCertificate {
93        issued_at_ns: cert.issued_at_ns,
94        delegation: domain::IssuerDelegation {
95            root: cert.root_pid,
96            issuer: cert.issuer_pid,
97            binding_hash: cert.issuer_proof_binding_hash,
98            seed_hash,
99            max_token_ttl_ns: cert.max_token_ttl_ns,
100            audience: audience(cert.aud),
101            grants: grants(cert.grants)?,
102            not_before_ns: cert.not_before_ns,
103            expires_at_ns: cert.expires_at_ns,
104        },
105    };
106    let wire::RootProof::IcChainKeyBatchSignatureV1(root) = value.delegation.root_proof;
107    let h = root.header;
108    let wire::ChainKeyAlgorithm::EcdsaSecp256k1 = h.algorithm;
109    let header = domain::RootBatchHeader {
110        schema_version: h.schema_version,
111        root: h.root_canister_id,
112        batch_id: h.batch_id,
113        proof_epoch: h.proof_epoch,
114        registry_epoch: h.registry_epoch,
115        registry_hash: h.registry_hash,
116        tree_root: h.tree_root,
117        not_before_ns: h.not_before_ns,
118        expires_at_ns: h.expires_at_ns,
119        key_name: h.key_id.name.into_string(),
120        derivation_path_hash: h.derivation_path_hash,
121        key_version: h.key_version,
122    };
123    let leaf = root.delegation_cert;
124    let wire::IssuerProofAlgorithm::IcCanisterSignatureV1 = leaf.issuer_proof_algorithm;
125    let wire::IssuerProofBinding::IcCanisterSignatureV1(seed_hash) = leaf.issuer_proof_binding;
126    let leaf = domain::DelegationLeaf {
127        proof_epoch: leaf.proof_epoch,
128        registry_epoch: leaf.registry_epoch,
129        registry_hash: leaf.registry_hash,
130        delegation: domain::IssuerDelegation {
131            root: leaf.root_canister_id,
132            issuer: leaf.issuer_canister_id,
133            binding_hash: leaf.issuer_proof_binding_hash,
134            seed_hash,
135            max_token_ttl_ns: leaf.max_token_ttl_ns,
136            audience: audience(leaf.audience),
137            grants: grants(leaf.grants)?,
138            not_before_ns: leaf.not_before_ns,
139            expires_at_ns: leaf.expires_at_ns,
140        },
141    };
142    let signature = root.signature;
143    let wire::ChainKeyAlgorithm::EcdsaSecp256k1 = signature.algorithm;
144    Ok(domain::CertifiedProofMaterial {
145        delegation: domain::RootDelegation {
146            certificate,
147            header,
148            leaf,
149            witness: root
150                .issuer_witness
151                .steps
152                .into_vec()
153                .into_iter()
154                .map(|step| match step {
155                    wire::ChainKeyBatchWitnessStepV1::LeftSibling(hash) => {
156                        domain::MerkleStep::Left(hash)
157                    }
158                    wire::ChainKeyBatchWitnessStepV1::RightSibling(hash) => {
159                        domain::MerkleStep::Right(hash)
160                    }
161                })
162                .collect(),
163            signature: domain::RootSignatureMaterial {
164                key_name: signature.key_id.name.into_string(),
165                derivation_path: signature
166                    .derivation_path
167                    .into_vec()
168                    .into_iter()
169                    .map(ic_wire::Bytes::into_vec)
170                    .collect(),
171                public_key: signature.public_key.into_vec(),
172                signature: signature.signature.into_vec(),
173            },
174        },
175        issuer_signature_cbor: value.issuer_signature.signature_cbor.into_vec(),
176        issuer_public_key_der: value.issuer_signature.public_key_der.into_vec(),
177    })
178}