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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
use std::convert::TryFrom;
use std::io;
use std::ops::{Deref, DerefMut};
use std::path::Path;

use crate::{
    Result,
    Packet,
    PacketPile,
};
use crate::parse::{
    PacketParserBuilder,
    PacketParserResult,
    PacketParser,
    Parse,
    Cookie
};
use buffered_reader::BufferedReader;

/// Parses an OpenPGP stream with the convenience of
/// [`PacketPile::from_file`] and the flexibility of a
/// [`PacketParser`].
///
///   [`PacketPile::from_file`]: ../struct.PacketPile.html#impl-Parse<%27a%2C%20PacketPile>
///
/// Like [`PacketPile::from_file`] (and unlike [`PacketParser`]), a
/// `PacketPileParser` parses an OpenPGP message and returns a
/// [`PacketPile`].  But, unlike [`PacketPile::from_file`] (and like
/// [`PacketParser`]), it allows the caller to inspect each packet as
/// it is being parsed.
///
///   [`PacketPile`]: crate::PacketPile
///
/// Thus, using a `PacketPileParser`, it is possible to decide on a
/// per-packet basis whether to stream, buffer or drop the packet's
/// body, whether to recurse into a container, or whether to abort
/// processing, for example.  And, `PacketPileParser` conveniently packs
/// the packets into a [`PacketPile`].
///
/// If old packets don't need to be retained, then [`PacketParser`]
/// should be preferred.  If no per-packet processing needs to be
/// done, then [`PacketPile::from_file`] will be slightly faster.
///
/// # Examples
///
/// These examples demonstrate how to process packet bodies by parsing
/// the simplest possible OpenPGP message containing just a single
/// literal data packet with the body "Hello world.".  There are three
/// options.  First, the body can be dropped.  Second, it can be
/// buffered.  Lastly, the body can be streamed.  In general,
/// streaming should be preferred, because it avoids buffering in
/// Sequoia.
///
/// This example demonstrates simply ignoring the packet body:
///
/// ```rust
/// # fn main() -> sequoia_openpgp::Result<()> {
/// use sequoia_openpgp as openpgp;
/// use openpgp::Packet;
/// use openpgp::parse::{Parse, PacketPileParser};
///
/// // By default, the `PacketPileParser` will drop packet bodies.
/// let mut ppp =
///     PacketPileParser::from_bytes(
///         b"\xcb\x12b\x00\x00\x00\x00\x00Hello world.")?;
/// while ppp.is_some() {
///     // Start parsing the next packet, recursing.
///     ppp.recurse()?;
/// }
///
/// let pile = ppp.finish();
/// // Process the packet.
/// if let Some(Packet::Literal(literal)) = pile.path_ref(&[0]) {
///     // The body was dropped.
///     assert_eq!(literal.body(), b"");
/// } else {
///     unreachable!("We know it is a literal packet.");
/// }
/// # Ok(()) }
/// ```
///
/// This example demonstrates how the body can be buffered by
/// configuring the `PacketPileParser` to buffer all packet bodies:
///
/// ```rust
/// # fn main() -> sequoia_openpgp::Result<()> {
/// use std::convert::TryFrom;
///
/// use sequoia_openpgp as openpgp;
/// use openpgp::Packet;
/// use openpgp::parse::{Parse, PacketPileParser, PacketParserBuilder};
///
/// // By default, the `PacketPileParser` will drop packet bodies.
/// // Use a `PacketParserBuilder` to change that.
/// let mut ppb =
///     PacketParserBuilder::from_bytes(
///         b"\xcb\x12b\x00\x00\x00\x00\x00Hello world.")?
///     .buffer_unread_content();
/// let mut ppp = PacketPileParser::try_from(ppb)?;
/// while ppp.is_some() {
///     // Start parsing the next packet, recursing.
///     ppp.recurse()?;
/// }
///
/// let pile = ppp.finish();
/// // Process the packet.
/// if let Some(Packet::Literal(literal)) = pile.path_ref(&[0]) {
///     // The body was buffered.
///     assert_eq!(literal.body(), b"Hello world.");
/// } else {
///     unreachable!("We know it is a literal packet.");
/// }
/// # Ok(()) }
/// ```
///
/// This example demonstrates how the body can be buffered by
/// buffering an individual packet:
///
/// ```rust
/// # fn main() -> sequoia_openpgp::Result<()> {
/// use sequoia_openpgp as openpgp;
/// use openpgp::Packet;
/// use openpgp::parse::{Parse, PacketPileParser};
///
/// // By default, the `PacketPileParser` will drop packet bodies.
/// let mut ppp =
///     PacketPileParser::from_bytes(
///         b"\xcb\x12b\x00\x00\x00\x00\x00Hello world.")?;
/// while let Ok(pp) = ppp.as_mut() {
///     if let Packet::Literal(_) = pp.packet {
///         // Buffer this packet's body.
///         pp.buffer_unread_content()?;
///     }
///
///     // Start parsing the next packet, recursing.
///     ppp.recurse()?;
/// }
///
/// let pile = ppp.finish();
/// // Process the packet.
/// if let Some(Packet::Literal(literal)) = pile.path_ref(&[0]) {
///     // The body was buffered.
///     assert_eq!(literal.body(), b"Hello world.");
/// } else {
///     unreachable!("We know it is a literal packet.");
/// }
/// # Ok(()) }
/// ```
///
/// This example demonstrates how to stream the packet body:
///
/// ```rust
/// # fn main() -> sequoia_openpgp::Result<()> {
/// use std::io::Read;
///
/// use sequoia_openpgp as openpgp;
/// use openpgp::Packet;
/// use openpgp::parse::{Parse, PacketPileParser};
///
/// // By default, the `PacketPileParser` will drop packet bodies.
/// let mut ppp =
///     PacketPileParser::from_bytes(
///         b"\xcb\x12b\x00\x00\x00\x00\x00Hello world.")?;
/// while let Ok(pp) = ppp.as_mut() {
///     if let Packet::Literal(_) = pp.packet {
///         // Stream the body.
///         let mut buf = Vec::new();
///         pp.read_to_end(&mut buf)?;
///         assert_eq!(buf, b"Hello world.");
///     }
///
///     // Start parsing the next packet, recursing.
///     ppp.recurse()?;
/// }
///
/// let pile = ppp.finish();
/// // Process the packet.
/// if let Some(Packet::Literal(literal)) = pile.path_ref(&[0]) {
///     // The body was streamed, not buffered.
///     assert_eq!(literal.body(), b"");
/// } else {
///     unreachable!("We know it is a literal packet.");
/// }
/// # Ok(()) }
/// ```
#[derive(Debug)]
pub struct PacketPileParser<'a> {
    /// The current packet.
    ppr: PacketParserResult<'a>,

    /// The packet pile that has been assembled so far.
    pile: PacketPile,
}
assert_send_and_sync!(PacketPileParser<'_>);

impl<'a> Deref for PacketPileParser<'a> {
    type Target = PacketParserResult<'a>;

    fn deref(&self) -> &Self::Target {
        &self.ppr
    }
}

impl<'a> DerefMut for PacketPileParser<'a> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.ppr
    }
}

impl<'a> TryFrom<PacketParserBuilder<'a>> for PacketPileParser<'a> {
    type Error = anyhow::Error;

    /// Finishes configuring the `PacketParser` and returns a
    /// `PacketPileParser`.
    fn try_from(ppb: PacketParserBuilder<'a>) -> Result<PacketPileParser<'a>> {
        Self::from_packet_parser(ppb.build()?)
    }
}

impl<'a> Parse<'a, PacketPileParser<'a>> for PacketPileParser<'a> {
    /// Creates a `PacketPileParser` to parse the OpenPGP message stored
    /// in the `io::Read` object.
    fn from_reader<R: io::Read + 'a + Send + Sync>(reader: R)
             -> Result<PacketPileParser<'a>> {
        let bio = Box::new(buffered_reader::Generic::with_cookie(
            reader, None, Cookie::default()));
        PacketPileParser::from_buffered_reader(bio)
    }

    /// Creates a `PacketPileParser` to parse the OpenPGP message stored
    /// in the file named by `path`.
    fn from_file<P: AsRef<Path>>(path: P)
            -> Result<PacketPileParser<'a>> {
        PacketPileParser::from_buffered_reader(
            Box::new(buffered_reader::File::with_cookie(path, Cookie::default())?))
    }

    /// Creates a `PacketPileParser` to parse the OpenPGP message stored
    /// in the provided buffer.
    fn from_bytes<D: AsRef<[u8]> + ?Sized + Send + Sync>(data: &'a D)
            -> Result<PacketPileParser<'a>> {
        let bio = Box::new(buffered_reader::Memory::with_cookie(
            data.as_ref(), Cookie::default()));
        PacketPileParser::from_buffered_reader(bio)
    }
}

#[allow(clippy::should_implement_trait)]
impl<'a> PacketPileParser<'a> {
    /// Creates a `PacketPileParser` from a *fresh* `PacketParser`.
    fn from_packet_parser(ppr: PacketParserResult<'a>)
        -> Result<PacketPileParser<'a>>
    {
        Ok(PacketPileParser {
            pile: Default::default(),
            ppr,
        })
    }

    /// Creates a `PacketPileParser` to parse the OpenPGP message stored
    /// in the `BufferedReader` object.
    pub(crate) fn from_buffered_reader(bio: Box<dyn BufferedReader<Cookie> + 'a>)
            -> Result<PacketPileParser<'a>> {
        Self::from_packet_parser(PacketParser::from_buffered_reader(bio)?)
    }

    /// Inserts the next packet into the `PacketPile`.
    fn insert_packet(&mut self, packet: Packet, position: isize) {
        // Find the right container.
        let mut container = self.pile.top_level_mut();

        assert!(position >= 0);

        for i in 0..position {
            // The most recent child.
            let tmp = container;
            let packets_len = tmp.children_ref().expect("is a container").len();
            let p = &mut tmp.children_mut()
                .expect("is a container")
                [packets_len - 1];
            if p.children().expect("is a container").next().is_none() {
                assert!(i == position - 1,
                        "Internal inconsistency while building message.");
            }

            container = p.container_mut().unwrap();
        }

        container.children_mut().unwrap().push(packet);
    }

    /// Finishes parsing the current packet and starts parsing the
    /// next one, recursing if possible.
    ///
    /// This method is similar to the [`next()`] method (see that
    /// method for more details), but if the current packet is a
    /// container (and we haven't reached the maximum recursion depth,
    /// and the user hasn't started reading the packet's contents), we
    /// recurse into the container, and return a `PacketParser` for
    /// its first child.  Otherwise, we return the next packet in the
    /// packet stream.  If this function recurses, then the new
    /// packet's recursion depth will be `last_recursion_depth() + 1`;
    /// because we always visit interior nodes, we can't recurse more
    /// than one level at a time.
    ///
    ///   [`next()`]: PacketPileParser::next()
    ///
    /// # Examples
    ///
    /// ```rust
    /// # fn main() -> sequoia_openpgp::Result<()> {
    /// use sequoia_openpgp as openpgp;
    /// use openpgp::parse::{Parse, PacketPileParser};
    ///
    /// // Parse a message.
    /// let message_data: &[u8] = // ...
    /// #    include_bytes!("../../tests/data/messages/compressed-data-algo-0.pgp");
    /// let mut ppp = PacketPileParser::from_bytes(message_data)?;
    /// while let Ok(pp) = ppp.as_ref() {
    ///     // Do something interesting with `pp` here.
    ///
    ///     // Start parsing the next packet, recursing.
    ///     ppp.recurse()?;
    /// }
    ///
    /// let pile = ppp.finish();
    /// # Ok(()) }
    /// ```
    pub fn recurse(&mut self) -> Result<()> {
        match self.ppr.take() {
            PacketParserResult::Some(pp) => {
                let recursion_depth = pp.recursion_depth();
                let (packet, ppr) = pp.recurse()?;
                self.insert_packet(
                    packet,
                    recursion_depth as isize);
                self.ppr = ppr;
            }
            eof @ PacketParserResult::EOF(_) => {
                self.ppr = eof;
            }
        }

        Ok(())
    }

    /// Finishes parsing the current packet and starts parsing the
    /// next one.
    ///
    /// This function finishes parsing the current packet.  By
    /// default, any unread content is dropped.  (See
    /// [`PacketParsererBuilder`] for how to configure this.)  It then
    /// creates a new packet parser for the next packet.  If the
    /// current packet is a container, this function does *not*
    /// recurse into the container, but skips any packets it contains.
    /// To recurse into the container, use the [`recurse()`] method.
    ///
    ///   [`PacketParsererBuilder`]: PacketParserBuilder
    ///   [`recurse()`]: PacketPileParser::recurse()
    ///
    /// # Examples
    ///
    /// ```rust
    /// # fn main() -> sequoia_openpgp::Result<()> {
    /// use sequoia_openpgp as openpgp;
    /// use openpgp::parse::{Parse, PacketPileParser};
    ///
    /// // Parse a message.
    /// let message_data: &[u8] = // ...
    /// #    include_bytes!("../../tests/data/messages/compressed-data-algo-0.pgp");
    /// let mut ppp = PacketPileParser::from_bytes(message_data)?;
    /// while let Ok(pp) = ppp.as_ref() {
    ///     // Do something interesting with `pp` here.
    ///
    ///     // Start parsing the next packet.
    ///     ppp.next()?;
    /// }
    ///
    /// let pile = ppp.finish();
    /// # Ok(()) }
    /// ```
    pub fn next(&mut self) -> Result<()> {
        match self.ppr.take() {
            PacketParserResult::Some(pp) => {
                let recursion_depth = pp.recursion_depth();
                let (packet, ppr) = pp.next()?;
                self.insert_packet(
                    packet,
                    recursion_depth as isize);
                self.ppr = ppr;
            },
            eof @ PacketParserResult::EOF(_) => {
                self.ppr = eof
            },
        }

        Ok(())
    }

    /// Returns the current packet's recursion depth.
    ///
    /// A top-level packet has a recursion depth of 0.  Packets in a
    /// top-level container have a recursion depth of 1.  Etc.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # fn main() -> sequoia_openpgp::Result<()> {
    /// use sequoia_openpgp as openpgp;
    /// use openpgp::Packet;
    /// use openpgp::parse::{Parse, PacketPileParser};
    ///
    /// // Parse a simple compressed message.
    /// let message_data: &[u8] = // ...
    /// #    include_bytes!("../../tests/data/messages/compressed-data-algo-0.pgp");
    /// let mut ppp = PacketPileParser::from_bytes(message_data)?;
    /// while let Ok(pp) = ppp.as_ref() {
    ///     match pp.packet {
    ///         Packet::CompressedData(_) =>
    ///             assert_eq!(ppp.recursion_depth(), Some(0)),
    ///         Packet::Literal(_) =>
    ///             assert_eq!(ppp.recursion_depth(), Some(1)),
    ///         _ => unreachable!(),
    ///     }
    ///
    ///     // Alternatively, the recursion depth can be queried
    ///     // from the packet parser.
    ///     assert_eq!(ppp.recursion_depth(), Some(pp.recursion_depth()));
    ///
    ///     // Start parsing the next packet.
    ///     ppp.next()?;
    /// }
    ///
    /// let pile = ppp.finish();
    /// # Ok(()) }
    /// ```
    pub fn recursion_depth(&self) -> Option<isize> {
        if let PacketParserResult::Some(ref pp) = self.ppr {
            Some(pp.recursion_depth())
        } else {
            None
        }
    }

    /// Returns whether the message has been completely parsed.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # fn main() -> sequoia_openpgp::Result<()> {
    /// use sequoia_openpgp as openpgp;
    /// use openpgp::Packet;
    /// use openpgp::parse::{Parse, PacketPileParser};
    ///
    /// // Parse a message.
    /// let message_data: &[u8] = // ...
    /// #    include_bytes!("../../tests/data/messages/compressed-data-algo-0.pgp");
    /// let mut ppp = PacketPileParser::from_bytes(message_data)?;
    /// while ppp.is_some() {
    ///     // Start parsing the next packet.
    ///     ppp.next()?;
    /// }
    ///
    /// assert!(ppp.is_done());
    /// let pile = ppp.finish();
    /// # Ok(()) }
    /// ```
    pub fn is_done(&self) -> bool {
        self.ppr.is_eof()
    }

    /// Finishes parsing the message and returns the assembled
    /// `PacketPile`.
    ///
    /// This function can be called at any time, not only when the
    /// message has been completely parsed.  If the packet sequence has not
    /// been completely parsed, this function aborts processing, and
    /// the returned `PacketPile` just contains those packets that were
    /// completely processed; the packet that is currently being
    /// processed is not included in the `PacketPile`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # fn main() -> sequoia_openpgp::Result<()> {
    /// use sequoia_openpgp as openpgp;
    /// use openpgp::Packet;
    /// use openpgp::parse::{Parse, PacketPileParser};
    ///
    /// // Parse a message.
    /// let message_data: &[u8] = // ...
    /// #    include_bytes!("../../tests/data/messages/compressed-data-algo-0.pgp");
    /// let mut ppp = PacketPileParser::from_bytes(message_data)?;
    /// ppp.next()?;
    ///
    /// let pp = ppp.finish();
    /// assert_eq!(pp.children().count(), 1);
    /// # Ok(()) }
    /// ```
    pub fn finish(self) -> PacketPile {
        self.pile
    }
}

#[test]
fn test_recurse() -> Result<()> {
    let mut count = 0;
    let mut ppp =
        PacketPileParser::from_bytes(crate::tests::key("public-key.gpg"))?;
    while ppp.is_some() {
        count += 1;
        ppp.recurse().unwrap();
    }
    assert_eq!(count, 61);
    let pp = ppp.finish();
    assert_eq!(pp.children().count(), 61);
    Ok(())
}

#[test]
fn test_next() -> Result<()> {
    let mut count = 0;
    let mut ppp =
        PacketPileParser::from_bytes(crate::tests::key("public-key.gpg"))?;
    while ppp.is_some() {
        count += 1;
        ppp.next().unwrap();
    }
    assert_eq!(count, 61);
    let pp = ppp.finish();
    assert_eq!(pp.children().count(), 61);
    Ok(())
}

/// Check that we can use the read interface to stream the contents of
/// a packet.
#[cfg(feature = "compression-deflate")]
#[test]
fn message_parser_reader_interface() {
    use std::io::Read;

    let expected = crate::tests::manifesto();

    // A message containing a compressed packet that contains a
    // literal packet.
    let mut ppp = PacketPileParser::from_bytes(
        crate::tests::message("compressed-data-algo-1.gpg")).unwrap();
    let mut count = 0;
    while let Ok(pp) = ppp.as_mut() {
        if let Packet::Literal(_) = pp.packet {
            assert_eq!(count, 1); // The *second* packet.

            // Check that we can read the packet's contents.  We do this one
            // byte at a time to exercise the cursor implementation.
            for i in 0..expected.len() {
                let mut buf = [0u8; 1];
                let r = pp.read(&mut buf).unwrap();
                assert_eq!(r, 1);
                assert_eq!(buf[0], expected[i]);
            }
            // And, now an EOF.
            let mut buf = [0u8; 1];
            let r = pp.read(&mut buf).unwrap();
            assert_eq!(r, 0);
        }
        ppp.recurse().unwrap();
        count += 1;
    }
    assert_eq!(count, 2);
}