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
//! Pages are a high level representation of pages stored in the database
//! These can be converted into and from a base object for encoding and decoding.

use std::convert::TryFrom;
use std::ops::Add;
use std::time::{Duration, SystemTime};

use crate::base::Base;
use crate::base::{Body, PrivateOptions};
use crate::crypto;
use crate::options::Options;
use crate::types::*;

mod info;
pub use info::PageInfo;

//pub type Page = Base;
//pub type PageBuilder = BaseBuilder;

/// High level description of a database page
/// Check out `PageBuilder` for a helper for constructing `Page` objects
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[derive(Debug, Clone, Builder)]
pub struct Page {
    // Header
    pub id: Id,

    #[builder(default = "0")]
    pub application_id: u16,

    #[builder(default = "Flags::default()")]
    pub flags: Flags,

    #[builder(default = "0")]
    pub version: u16,

    // Page kind / identifier
    pub kind: Kind,

    // Information associated with different object kinds
    pub info: PageInfo,

    // Page Body
    #[builder(default = "Body::None")]
    pub body: Body,

    // Common options
    #[builder(default = "SystemTime::now().into()")]
    pub issued: DateTime,
    #[builder(default = "None")]
    pub expiry: Option<DateTime>,

    #[builder(default = "vec![]")]
    pub public_options: Vec<Options>,
    #[builder(default = "PrivateOptions::None")]
    pub private_options: PrivateOptions,

    // Previous page signature
    #[builder(default = "None")]
    pub previous_sig: Option<Signature>,

    // Signature (if signed or decoded)
    #[builder(default = "None")]
    pub signature: Option<Signature>,

    /// Verified flag
    #[builder(default = "false")]
    pub verified: bool,

    // Raw (encoded) data
    #[builder(default = "None")]
    pub raw: Option<Vec<u8>>,

    #[builder(default = "()")]
    _extend: (),
}

impl PartialEq for Page {
    fn eq(&self, o: &Self) -> bool {
        self.id == o.id
            && self.application_id == o.application_id
            && self.flags == o.flags
            && self.version == o.version
            && self.kind == o.kind
            && self.info == o.info
            && self.body == o.body
            && self.issued == o.issued
            && self.expiry == o.expiry
            && self.previous_sig == o.previous_sig
            && self.public_options == o.public_options
            && self.private_options == o.private_options
            && self.signature == o.signature
    }
}

impl Page {
    /// Create a new page
    pub fn new(
        id: Id,
        application_id: u16,
        kind: Kind,
        flags: Flags,
        version: u16,
        info: PageInfo,
        body: Body,
        issued: SystemTime,
        expiry: Option<SystemTime>,
    ) -> Self {
        Page {
            id,
            application_id,
            kind,
            flags,
            version,
            info,
            body,
            issued: issued.into(),
            expiry: expiry.map(|v| v.into()),

            public_options: vec![],
            private_options: PrivateOptions::None,

            previous_sig: None,

            signature: None,
            verified: false,
            raw: None,

            _extend: (),
        }
    }

    pub fn id(&self) -> &Id {
        &self.id
    }

    pub fn application_id(&self) -> u16 {
        self.application_id
    }

    pub fn kind(&self) -> Kind {
        self.kind
    }

    pub fn flags(&self) -> Flags {
        self.flags
    }

    pub fn version(&self) -> u16 {
        self.version
    }

    pub fn info(&self) -> &PageInfo {
        &self.info
    }

    pub fn body(&self) -> &Body {
        &self.body
    }

    pub fn issued(&self) -> SystemTime {
        self.issued.into()
    }

    pub fn expiry(&self) -> Option<SystemTime> {
        self.expiry.map(|t| t.into())
    }

    pub fn public_options(&self) -> &[Options] {
        &self.public_options
    }

    pub fn private_options(&self) -> &PrivateOptions {
        &self.private_options
    }

    pub fn signature(&self) -> Option<Signature> {
        self.signature.clone()
    }

    pub fn set_signature(&mut self, sig: Signature) {
        self.signature = Some(sig);
    }

    pub fn raw(&self) -> &Option<Vec<u8>> {
        &self.raw
    }

    pub fn clean(&mut self) {
        self.raw = None;
    }
}

impl Page {
    pub fn decode_pages<V>(buff: &[u8], key_source: V) -> Result<Vec<Page>, Error>
    where
        V: Fn(&Id) -> Option<PublicKey>,
    {
        let mut pages = vec![];
        let mut i = 0;

        // Last key used to cache the previous primary key to decode secondary pages published by a service in a single message.
        let mut last_key: Option<(Id, PublicKey)> = None;

        while i < buff.len() {
            // TODO: validate signatures against existing services!
            let (b, n) = Base::parse(
                &buff[i..],
                |id| {
                    // Try key_source first
                    if let Some(key) = (key_source)(id) {
                        return Some(key);
                    };

                    // Check for last entry second
                    if let Some(prev) = &last_key {
                        if &prev.0 == id {
                            return Some(prev.1.clone());
                        }
                    }

                    // Fail if no public key is found
                    None
                },
                // No encryption key source available here
                |_id| None,
            )?;

            i += n;

            let page = match Page::try_from(b) {
                Ok(p) => p,
                Err(e) => {
                    error!("Error loading page from message: {:?}", e);
                    continue;
                }
            };

            // Cache key for next run
            if let Some(key) = page.info().pub_key() {
                last_key = Some((page.id().clone(), key));
            }

            // Push page to parsed list
            pages.push(page);
        }

        Ok(pages)
    }

    pub fn encode_pages(pages: &[Page], buff: &mut [u8]) -> Result<usize, Error> {
        let mut i = 0;

        for p in pages {
            // Check page has associated signature
            match (&p.signature, &p.raw) {
                (None, None) => {
                    error!("cannot encode page without associated signature or private key");
                    continue;
                }
                _ => (),
            };

            // Convert and encode
            let mut b = Base::from(p);
            let n = b.encode(None, None, &mut buff[i..])?;

            i += n;
        }

        Ok(i)
    }
}

impl PageBuilder {
    pub fn append_public_option(&mut self, o: Options) -> &mut Self {
        match &mut self.public_options {
            Some(opts) => opts.push(o),
            None => self.public_options = Some(vec![o]),
        }
        self
    }

    pub fn append_private_option(&mut self, o: Options) -> &mut Self {
        if let Some(opts) = &mut self.private_options {
            opts.append(o)
        }
        self
    }

    pub fn valid_for(&mut self, d: Duration) -> &mut Self {
        self.expiry = Some(Some(SystemTime::now().add(d).into()));
        self
    }
}

impl From<&Page> for Base {
    fn from(page: &Page) -> Base {
        let flags = page.flags.clone();
        let sig = page.signature().clone();

        // Insert default options
        let mut default_options = vec![Options::issued(page.issued)];

        if let Some(expiry) = page.expiry {
            default_options.push(Options::expiry(expiry));
        }

        if let Some(prev_sig) = &page.previous_sig {
            default_options.push(Options::prev_sig(prev_sig));
        }

        // Add public fields for different object types
        match &page.info {
            PageInfo::Primary(primary) => {
                default_options.push(Options::public_key(primary.pub_key.clone()));
            }
            PageInfo::Secondary(secondary) => {
                default_options.push(Options::peer_id(secondary.peer_id.clone()));
            }
            PageInfo::Data(_data) => {}
        }

        // Add additional public options
        // TODO: ideally these should be specified by type rather than an arbitrary list
        let mut public_options = page.public_options.clone();
        public_options.append(&mut default_options);

        // Generate base object
        let mut b = Base::new(
            page.id.clone(),
            page.application_id,
            page.kind,
            flags,
            page.version,
            page.body.clone(),
            public_options,
            page.private_options.clone(),
        );

        if let Some(sig) = sig {
            b.set_signature(sig);
        }

        if let Some(raw) = &page.raw {
            b.set_raw(raw.clone());
        }

        b.verified = page.verified;

        b
    }
}

impl TryFrom<Base> for Page {
    type Error = Error;

    fn try_from(base: Base) -> Result<Self, Error> {
        let header = base.header();
        let signature = base.signature();

        let flags = header.flags();
        let kind = header.kind();

        if !kind.is_page() && !kind.is_data() {
            return Err(Error::InvalidPageKind);
        }

        let (mut issued, mut expiry, mut previous_sig, mut peer_id) = (None, None, None, None);
        let public_options = base
            .public_options()
            .iter()
            .filter_map(|o| match &o {
                Options::Issued(v) => {
                    issued = Some(v.when);
                    None
                }
                Options::Expiry(v) => {
                    expiry = Some(v.when);
                    None
                }
                Options::PrevSig(v) => {
                    previous_sig = Some(v.sig.clone());
                    None
                }
                Options::PeerId(v) => {
                    peer_id = Some(v.peer_id.clone());
                    None
                }
                _ => Some(o),
            })
            .map(|o| o.clone())
            .collect();

        let peer_id = base.peer_id.clone();

        // TODO: parse out private options too?
        let _private_options = base.private_options();

        let info = if kind.is_page() && !flags.contains(Flags::SECONDARY) {
            // Handle primary page parsing

            // Fetch public key from options
            let public_key: PublicKey = match &base.public_key {
                Some(pk) => Ok(pk.clone()),
                None => Err(Error::NoPublicKey),
            }?;

            // Check public key and ID match
            let hash: Id = crypto::hash(&public_key).unwrap().into();
            if &hash != base.id() {
                return Err(Error::KeyIdMismatch);
            }

            PageInfo::primary(public_key)
        } else if kind.is_page() && flags.contains(Flags::SECONDARY) {
            // Handle secondary page parsing
            let peer_id = match peer_id {
                Some(id) => Ok(id),
                None => Err(Error::NoPeerId),
            }?;

            PageInfo::secondary(peer_id)
        } else if kind.is_data() {
            PageInfo::Data(())
        } else {
            error!(
                "Attempted to convert non-page base object ({:?}) to page",
                kind
            );
            return Err(Error::UnexpectedPageType);
        };

        Ok(Page {
            id: base.id().clone(),
            application_id: header.application_id(),
            kind: header.kind(),
            flags: header.flags(),
            version: header.index(),
            info,
            body: base.body.clone(),
            issued: issued.expect("missing issued option"),
            expiry,

            previous_sig,

            public_options,
            private_options: base.private_options.clone(),
            signature: signature.clone(),
            verified: base.verified,

            raw: base.raw().clone(),
            _extend: (),
        })
    }
}