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
//! Serialized data associated with a parent Document and key string.
//!
//! Entries are created by calling [`NewEntry::new`] with serializable data, the hash of the parent
//! document, and the key string. Once created, they can be signed and have their compression
//! settings chosen. Entries (new or otherwise) are verified and encoded using a
//! [`Schema`][crate::schema::Schema], which should match the schema used by the parent document.

use crate::error::{Error, Result};
use crate::{
    compress::CompressType,
    de::FogDeserializer,
    element::{serialize_elem, Element},
    ser::FogSerializer,
    MAX_ENTRY_SIZE,
};
use byteorder::{LittleEndian, ReadBytesExt};
use fog_crypto::{
    hash::{Hash, HashState},
    identity::{Identity, IdentityKey},
};
use serde::{Deserialize, Serialize};
use std::convert::TryFrom;

pub(crate) const ENTRY_PREFIX_LEN: usize = 3;

pub(crate) struct SplitEntry<'a> {
    pub compress_raw: u8,
    pub data: &'a [u8],
    pub signature_raw: &'a [u8],
}

impl<'a> SplitEntry<'a> {
    pub(crate) fn split(buf: &'a [u8]) -> Result<SplitEntry> {
        // Compression marker
        let (&compress_raw, mut buf) = buf.split_first().ok_or(Error::LengthTooShort {
            step: "get compress type",
            actual: 0,
            expected: 1,
        })?;
        // Data length
        let data_len = buf
            .read_u16::<LittleEndian>()
            .map_err(|_| Error::LengthTooShort {
                step: "get data length",
                actual: buf.len(),
                expected: 2,
            })? as usize;
        if data_len > buf.len() {
            return Err(Error::LengthTooShort {
                step: "get document data",
                actual: buf.len(),
                expected: data_len,
            });
        }
        // Data & signature
        let (data, signature_raw) = buf.split_at(data_len);
        Ok(Self {
            compress_raw,
            data,
            signature_raw,
        })
    }
}

/// A new Entry that has not yet been validated.
///
/// This struct acts like an Entry, but cannot be decoded until it has passed through a
/// [`Schema`][crate::schema::Schema].
pub struct NewEntry {
    buf: Vec<u8>,
    hash_state: HashState,
    key: String,
    parent_hash: Hash,
    entry_hash: Hash,
    has_signature: bool,
    set_compress: Option<Option<u8>>,
}

impl NewEntry {
    fn new_from<F>(key: &str, parent: &Hash, encoder: F) -> Result<Self>
    where
        F: FnOnce(Vec<u8>) -> Result<Vec<u8>>,
    {
        // Serialize the data
        let buf: Vec<u8> = vec![CompressType::NoCompress.into(), 0u8, 0u8];
        let mut buf = encoder(buf)?;

        // Check the total size and update the data length
        if buf.len() > MAX_ENTRY_SIZE {
            return Err(Error::LengthTooLong {
                max: MAX_ENTRY_SIZE,
                actual: buf.len(),
            });
        }
        let data_len = (buf.len() - ENTRY_PREFIX_LEN).to_le_bytes();
        buf[1] = data_len[0];
        buf[2] = data_len[1];

        // Create and update the Hasher
        let mut hash_state = HashState::new();
        let mut prefix = Vec::new();
        serialize_elem(&mut prefix, Element::Hash(parent.clone()));
        serialize_elem(&mut prefix, Element::Str(key));
        hash_state.update(&prefix);
        hash_state.update(&buf[ENTRY_PREFIX_LEN..]);
        let entry_hash = hash_state.hash();

        Ok(Self {
            buf,
            hash_state,
            key: key.to_owned(),
            parent_hash: parent.to_owned(),
            entry_hash,
            has_signature: false,
            set_compress: None,
        })
    }

    /// Create a new Entry from any serializable data, a key, and the Hash of the parent document.
    pub fn new<S: Serialize>(data: S, key: &str, parent: &Hash) -> Result<Self> {
        Self::new_from(key, parent, |buf| {
            // Serialize the data
            let mut ser = FogSerializer::from_vec(buf, false);
            data.serialize(&mut ser)?;
            Ok(ser.finish())
        })
    }

    /// Create a new Entry from a key, the Hash of the parent document, and any serializable data
    /// whose keys are all ordered. For structs, this means all fields are declared in
    /// lexicographic order. For maps, this means a `BTreeMap` type must be used, whose keys are
    /// ordered such that they serialize to lexicographically ordered strings. All sub-structs and
    /// sub-maps must be similarly ordered.
    pub fn new_ordered<S: Serialize>(data: S, key: &str, parent: &Hash) -> Result<Self> {
        Self::new_from(key, parent, |buf| {
            // Serialize the data
            let mut ser = FogSerializer::from_vec(buf, false);
            data.serialize(&mut ser)?;
            Ok(ser.finish())
        })
    }

    /// Override the default compression settings. `None` will disable compression. `Some(level)`
    /// will compress with the provided level as the setting for the algorithm.
    pub fn compression(mut self, setting: Option<u8>) -> Self {
        self.set_compress = Some(setting);
        self
    }

    /// Sign the document, or or replace the existing signature if one exists already. Fails if the
    /// signature would grow the document size beyond the maximum allowed.
    pub fn sign(mut self, key: &IdentityKey) -> Result<Self> {
        // Sign and check for size violation
        let signature = key.sign(&self.entry_hash);
        let new_len = if self.has_signature {
            self.buf.len() - self.split().signature_raw.len() + signature.size()
        } else {
            self.buf.len() + signature.size()
        };
        if new_len > MAX_ENTRY_SIZE {
            return Err(Error::LengthTooLong {
                max: MAX_ENTRY_SIZE,
                actual: self.buf.len(),
            });
        }

        if self.has_signature {
            let split = SplitEntry::split(&self.buf).unwrap();
            let new_len = split.data.len() + ENTRY_PREFIX_LEN;
            let mut hash_state = HashState::new();
            let mut prefix = Vec::new();
            serialize_elem(&mut prefix, Element::Hash(self.parent_hash.clone()));
            serialize_elem(&mut prefix, Element::Str(&self.key));
            hash_state.update(&prefix);
            hash_state.update(split.data);
            self.buf.resize(new_len, 0);
            self.hash_state = hash_state;
        }

        // Append the signature and update the hasher
        let pre_len = self.buf.len();
        signature.encode_vec(&mut self.buf);
        self.hash_state.update(&self.buf[pre_len..]);
        self.has_signature = true;
        Ok(self)
    }

    /// Get what the document's hash will be, given its current state
    pub fn hash(&self) -> Hash {
        self.hash_state.hash()
    }

    pub(crate) fn split(&self) -> SplitEntry {
        SplitEntry::split(&self.buf).unwrap()
    }

    pub(crate) fn data(&self) -> &[u8] {
        self.split().data
    }

    /// Get the hash of the Entry's parent [`Document`][crate::document::Document].
    pub fn parent(&self) -> &Hash {
        &self.parent_hash
    }

    /// Get the Entry's string key.
    pub fn key(&self) -> &str {
        &self.key
    }

    pub(crate) fn complete(self) -> (Hash, Vec<u8>, Option<Option<u8>>) {
        (self.hash_state.finalize(), self.buf, self.set_compress)
    }
}

/// Holds serialized data associated with a parent document and a key string.
///
/// An Entry holds a piece of serialized data, which may be deserialized by calling
/// [`deserialize`][Entry::deserialize].
pub struct Entry {
    buf: Vec<u8>,
    hash_state: HashState,
    key: String,
    parent_hash: Hash,
    entry_hash: Hash,
    signer: Option<Identity>,
    set_compress: Option<Option<u8>>,
}

impl Entry {
    pub(crate) fn new(buf: Vec<u8>, key: &str, parent: &Hash) -> Result<Self> {
        if buf.len() > MAX_ENTRY_SIZE {
            return Err(Error::LengthTooLong {
                max: MAX_ENTRY_SIZE,
                actual: buf.len(),
            });
        }

        let split = SplitEntry::split(&buf)?;

        let mut hash_state = HashState::new();
        let mut prefix = Vec::new();
        serialize_elem(&mut prefix, Element::Hash(parent.clone()));
        serialize_elem(&mut prefix, Element::Str(key));
        hash_state.update(&prefix);
        hash_state.update(split.data);
        let entry_hash = hash_state.hash();
        hash_state.update(split.signature_raw);

        let signer = if !split.signature_raw.is_empty() {
            let unverified =
                fog_crypto::identity::UnverifiedSignature::try_from(split.signature_raw)?;
            let verified = unverified.verify(&entry_hash)?;
            Some(verified.signer().clone())
        } else {
            None
        };

        Ok(Self {
            buf,
            hash_state,
            key: key.to_owned(),
            parent_hash: parent.to_owned(),
            entry_hash,
            signer,
            set_compress: None,
        })
    }

    pub(crate) fn split(&self) -> SplitEntry {
        SplitEntry::split(&self.buf).unwrap()
    }

    pub(crate) fn data(&self) -> &[u8] {
        self.split().data
    }

    /// Get the hash of the Entry's parent [`Document`][crate::document::Document].
    pub fn parent(&self) -> &Hash {
        &self.parent_hash
    }

    /// Get the Entry's string key.
    pub fn key(&self) -> &str {
        &self.key
    }

    /// Get the Identity of the signer of this document, if the document is signed.
    pub fn signer(&self) -> Option<&Identity> {
        self.signer.as_ref()
    }

    /// Get the hash of the complete entry. This can change if the entry is signed again with the
    /// [`sign`][Self::sign] function.
    pub fn hash(&self) -> Hash {
        self.hash_state.hash()
    }

    /// Deserialize the entry's contained data into a value.
    pub fn deserialize<'de, D: Deserialize<'de>>(&'de self) -> Result<D> {
        let buf = self.data();
        let mut de = FogDeserializer::new(buf);
        D::deserialize(&mut de)
    }

    /// Override the default compression settings. `None` will disable compression. `Some(level)`
    /// will compress with the provided level as the setting for the algorithm.
    pub fn compression(mut self, setting: Option<u8>) -> Self {
        self.set_compress = Some(setting);
        self
    }

    /// Sign the entry, or or replace the existing signature if one exists already. Fails if the
    /// signature would grow the entry size beyond the maximum allowed. In the event of a failure.
    /// the entry is unmodified.
    pub fn sign(mut self, key: &IdentityKey) -> Result<Self> {
        // Sign and check for size violation
        let signature = key.sign(&self.entry_hash);
        let new_len = if self.signer.is_some() {
            self.buf.len() - self.split().signature_raw.len() + signature.size()
        } else {
            self.buf.len() + signature.size()
        };
        if new_len > MAX_ENTRY_SIZE {
            return Err(Error::LengthTooLong {
                max: MAX_ENTRY_SIZE,
                actual: self.buf.len(),
            });
        }

        if self.signer.is_some() {
            let split = SplitEntry::split(&self.buf).unwrap();
            let new_len = split.data.len() + ENTRY_PREFIX_LEN;
            let mut hash_state = HashState::new();
            let mut prefix = Vec::new();
            serialize_elem(&mut prefix, Element::Hash(self.parent_hash.clone()));
            serialize_elem(&mut prefix, Element::Str(&self.key));
            hash_state.update(&prefix);
            hash_state.update(split.data);
            self.buf.resize(new_len, 0);
            self.hash_state = hash_state;
        }

        // Append the signature and update the hasher
        let pre_len = self.buf.len();
        signature.encode_vec(&mut self.buf);
        self.hash_state.update(&self.buf[pre_len..]);
        self.signer = Some(key.id().clone());
        Ok(self)
    }

    pub(crate) fn complete(self) -> (Hash, Vec<u8>, Option<Option<u8>>) {
        (self.hash_state.finalize(), self.buf, self.set_compress)
    }
}