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
use std::fmt;

use serde::{Deserialize, Serialize};

use crate::{
    attestation::{Attestation, Body, Header},
    hash::HashAlgorithm,
    merkle::{MerkleProof, MerkleTree},
    serialize::CanonicalSerialize,
    signing::{Signature, VerifyingKey},
    CryptoProvider,
};

/// Proof of an attestation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AttestationProof {
    signature: Signature,
    header: Header,
    body: BodyProof,
}

impl AttestationProof {
    pub(crate) fn new(
        provider: &CryptoProvider,
        attestation: &Attestation,
    ) -> Result<Self, AttestationError> {
        let hasher = provider
            .hash
            .get(&attestation.header.root.alg)
            .map_err(|e| AttestationError::new(ErrorKind::Provider, e))?;

        let body = BodyProof::new(hasher, attestation.body.clone())?;

        Ok(Self {
            signature: attestation.signature.clone(),
            header: attestation.header.clone(),
            body,
        })
    }

    /// Returns the verifying key.
    pub fn verifying_key(&self) -> &VerifyingKey {
        self.body.verifying_key()
    }

    /// Verifies the attestation proof.
    ///
    /// # Arguments
    ///
    /// * `provider` - Cryptography provider.
    /// * `verifying_key` - Verifying key for the Notary signature.
    pub fn verify(self, provider: &CryptoProvider) -> Result<Attestation, AttestationError> {
        let signature_verifier = provider
            .signature
            .get(&self.signature.alg)
            .map_err(|e| AttestationError::new(ErrorKind::Provider, e))?;

        // Verify body corresponding to the header.
        let body = self.body.verify_with_provider(provider, &self.header)?;

        // Verify signature of the header.
        signature_verifier
            .verify(
                &body.verifying_key.data,
                &CanonicalSerialize::serialize(&self.header),
                &self.signature.data,
            )
            .map_err(|e| AttestationError::new(ErrorKind::Signature, e))?;

        Ok(Attestation {
            signature: self.signature,
            header: self.header,
            body,
        })
    }
}

/// Proof of an attestation body.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct BodyProof {
    body: Body,
    proof: MerkleProof,
}

impl BodyProof {
    /// Returns a new body proof.
    // TODO: Support including a subset of fields instead of the entire body.
    pub(crate) fn new(
        hasher: &dyn HashAlgorithm,
        body: Body,
    ) -> Result<BodyProof, AttestationError> {
        let (indices, leaves): (Vec<_>, Vec<_>) = body
            .hash_fields(hasher)
            .into_iter()
            .map(|(id, hash)| (id.0 as usize, hash))
            .unzip();

        let mut tree = MerkleTree::new(hasher.id());
        tree.insert(hasher, leaves);

        let proof = tree.proof(&indices);

        Ok(BodyProof { body, proof })
    }

    pub(crate) fn verifying_key(&self) -> &VerifyingKey {
        &self.body.verifying_key.data
    }

    /// Verifies the proof against the attestation header.
    pub(crate) fn verify_with_provider(
        self,
        provider: &CryptoProvider,
        header: &Header,
    ) -> Result<Body, AttestationError> {
        let hasher = provider
            .hash
            .get(&header.root.alg)
            .map_err(|e| AttestationError::new(ErrorKind::Provider, e))?;

        let fields = self
            .body
            .hash_fields(hasher)
            .into_iter()
            .map(|(id, hash)| (id.0 as usize, hash));

        self.proof
            .verify(hasher, &header.root, fields)
            .map_err(|e| AttestationError::new(ErrorKind::Body, e))?;

        Ok(self.body)
    }
}

/// Error for [`AttestationProof`].
#[derive(Debug, thiserror::Error)]
pub struct AttestationError {
    kind: ErrorKind,
    source: Option<Box<dyn std::error::Error + Send + Sync>>,
}

impl AttestationError {
    fn new<E>(kind: ErrorKind, source: E) -> Self
    where
        E: Into<Box<dyn std::error::Error + Send + Sync>>,
    {
        Self {
            kind,
            source: Some(source.into()),
        }
    }
}

impl fmt::Display for AttestationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("attestation proof error: ")?;

        match self.kind {
            ErrorKind::Provider => f.write_str("provider error")?,
            ErrorKind::Signature => f.write_str("signature error")?,
            ErrorKind::Body => f.write_str("body proof error")?,
        }

        if let Some(source) = &self.source {
            write!(f, " caused by: {}", source)?;
        }

        Ok(())
    }
}

#[derive(Debug)]
enum ErrorKind {
    Provider,
    Signature,
    Body,
}