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