Skip to main content

sequoia_openpgp/parse/
stream.rs

1//! Streaming decryption and verification.
2//!
3//! This module provides convenient filters for decryption and
4//! verification of OpenPGP messages (see [Section 10.3 of RFC 9580]).
5//! It is the preferred interface to process OpenPGP messages:
6//!
7//!   [Section 10.3 of RFC 9580]: https://www.rfc-editor.org/rfc/rfc9580.html#section-10.3
8//!
9//!   - Use the [`Verifier`] to verify a signed message,
10//!   - [`DetachedVerifier`] to verify a detached signature,
11//!   - or [`Decryptor`] to decrypt and verify an encrypted and
12//!     possibly signed message.
13//!
14//!
15//! Consuming OpenPGP messages is more difficult than producing them.
16//! When we produce the message, we control the packet structure being
17//! generated using our programs control flow.  However, when we
18//! consume a message, the control flow is determined by the message
19//! being processed.
20//!
21//! To use Sequoia's streaming [`Verifier`] and [`Decryptor`], you
22//! need to provide an object that implements [`VerificationHelper`],
23//! and for the [`Decryptor`] also [`DecryptionHelper`].
24//!
25//!
26//! The [`VerificationHelper`] trait give certificates for the
27//! signature verification to the [`Verifier`] or [`Decryptor`], let
28//! you inspect the message structure (see [Section 10.3 of RFC
29//! 9580]), and implements the signature verification policy.
30//!
31//! The [`DecryptionHelper`] trait is concerned with producing the
32//! session key to decrypt a message, most commonly by decrypting one
33//! of the messages' [`PKESK`] or [`SKESK`] packets.  It could also
34//! use a cached session key, or one that has been explicitly provided
35//! to the decryption operation.
36//!
37//!   [`PKESK`]: crate::packet::PKESK
38//!   [`SKESK`]: crate::packet::SKESK
39//!
40//! The [`Verifier`] and [`Decryptor`] are filters: they consume
41//! OpenPGP data from a reader, file, or bytes, and implement
42//! [`io::Read`] that can be used to read the verified and/or
43//! decrypted data.
44//!
45//!   [`io::Read`]: std::io::Read
46//!
47//! [`DetachedVerifier`] does not provide the [`io::Read`] interface,
48//! because in this case, the data to be verified is easily available
49//! without any transformation.  Not providing a filter-like interface
50//! allows for a very performant implementation of the verification.
51//!
52//! # Examples
53//!
54//! This example demonstrates how to use the streaming interface using
55//! the [`Verifier`].  For brevity, no certificates are fed to the
56//! verifier, and the message structure is not verified, i.e. this
57//! merely extracts the literal data.  See the [`Verifier` examples]
58//! and the [`Decryptor` examples] for how to verify the message and
59//! its structure.
60//!
61//!   [`Verifier` examples]: Verifier#examples
62//!   [`Decryptor` examples]: Decryptor#examples
63//!
64//! ```
65//! # fn main() -> sequoia_openpgp::Result<()> {
66//! use std::io::Read;
67//! use sequoia_openpgp as openpgp;
68//! use openpgp::{KeyHandle, Cert, Result};
69//! use openpgp::parse::{Parse, stream::*};
70//! use openpgp::policy::StandardPolicy;
71//!
72//! let p = &StandardPolicy::new();
73//!
74//! // This fetches keys and computes the validity of the verification.
75//! struct Helper {}
76//! impl VerificationHelper for Helper {
77//!     fn get_certs(&mut self, _ids: &[KeyHandle]) -> Result<Vec<Cert>> {
78//!         Ok(Vec::new()) // Feed the Certs to the verifier here...
79//!     }
80//!     fn check(&mut self, structure: MessageStructure) -> Result<()> {
81//!         Ok(()) // Implement your verification policy here.
82//!     }
83//! }
84//!
85//! let message =
86//!    b"-----BEGIN PGP MESSAGE-----
87//!
88//!      xA0DAAoWBpwMNI3YLBkByxJiAAAAAABIZWxsbyBXb3JsZCHCdQQAFgoAJwWCW37P
89//!      8RahBI6MM/pGJjN5dtl5eAacDDSN2CwZCZAGnAw0jdgsGQAAeZQA/2amPbBXT96Q
90//!      O7PFms9DRuehsVVrFkaDtjN2WSxI4RGvAQDq/pzNdCMpy/Yo7AZNqZv5qNMtDdhE
91//!      b2WH5lghfKe/AQ==
92//!      =DjuO
93//!      -----END PGP MESSAGE-----";
94//!
95//! let h = Helper {};
96//! let mut v = VerifierBuilder::from_bytes(&message[..])?
97//!     .with_policy(p, None, h)?;
98//!
99//! let mut content = Vec::new();
100//! v.read_to_end(&mut content)?;
101//! assert_eq!(content, b"Hello World!");
102//! # Ok(()) }
103//! ```
104use std::cmp;
105use std::io;
106use std::path::Path;
107use std::time;
108
109use buffered_reader::BufferedReader;
110use crate::{
111    Error,
112    Fingerprint,
113    types::{
114        AEADAlgorithm,
115        CompressionAlgorithm,
116        RevocationStatus,
117        SymmetricAlgorithm,
118    },
119    packet::{
120        key,
121        OnePassSig,
122        PKESK,
123        SEIP,
124        SKESK,
125    },
126    KeyHandle,
127    Packet,
128    Result,
129    packet,
130    packet::{Signature, Unknown},
131    cert::prelude::*,
132    crypto::{
133        SessionKey,
134        mem::Protected,
135    },
136    policy::Policy,
137};
138use crate::parse::{
139    Cookie,
140    HashingMode,
141    PacketParser,
142    PacketParserBuilder,
143    PacketParserResult,
144    Parse,
145};
146
147/// Whether to trace execution by default (on stderr).
148const TRACE : bool = false;
149
150/// Indentation level for tracing in this module.
151const TRACE_INDENT: isize = 5;
152
153/// How much data to buffer before giving it to the caller.
154///
155/// Signature verification and detection of ciphertext tampering
156/// requires processing the whole message first.  Therefore, OpenPGP
157/// implementations supporting streaming operations necessarily must
158/// output unverified data.  This has been a source of problems in the
159/// past.  To alleviate this, we buffer the message first (up to 25
160/// megabytes of net message data by default), and verify the
161/// signatures if the message fits into our buffer.  Nevertheless it
162/// is important to treat the data as unverified and untrustworthy
163/// until you have seen a positive verification.
164///
165/// The default can be changed using [`VerifierBuilder::buffer_size`]
166/// and [`DecryptorBuilder::buffer_size`].
167///
168///   [`VerifierBuilder::buffer_size`]: VerifierBuilder::buffer_size()
169///   [`DecryptorBuilder::buffer_size`]: DecryptorBuilder::buffer_size()
170pub const DEFAULT_BUFFER_SIZE: usize = 25 * 1024 * 1024;
171
172/// Result of a signature verification.
173///
174/// A signature verification is either successful yielding a
175/// [`GoodChecksum`], or there was some [`VerificationError`]
176/// explaining the verification failure.
177///
178pub type VerificationResult<'a> =
179    std::result::Result<GoodChecksum<'a>, VerificationError<'a>>;
180
181/// A good signature.
182///
183/// Represents the result of a successful signature verification.  It
184/// includes the signature and the signing key with all the necessary
185/// context (i.e. certificate, time, policy) to evaluate the
186/// trustworthiness of the signature using a trust model.
187///
188/// `GoodChecksum` is used in [`VerificationResult`].  See also
189/// [`VerificationError`].
190///
191///
192/// A signature is considered good if and only if all the following
193/// conditions are met:
194///
195///   - The signature has a Signature Creation Time subpacket.
196///
197///   - The signature is alive at the specified time (the time
198///     parameter passed to, e.g., [`VerifierBuilder::with_policy`]).
199///
200///       [`VerifierBuilder::with_policy`]: VerifierBuilder::with_policy()
201///
202///   - The certificate is alive and not revoked as of the signature's
203///     creation time.
204///
205///   - The signing key is alive, not revoked, and signing capable as
206///     of the signature's creation time.
207///
208///   - The signature was generated by the signing key.
209///
210/// **Note**: This doesn't mean that the key that generated the
211/// signature is in any way trustworthy in the sense that it
212/// belongs to the person or entity that the user thinks it
213/// belongs to.  This property can only be evaluated within a
214/// trust model, such as the [web of trust] (WoT).  This policy is
215/// normally implemented in the [`VerificationHelper::check`]
216/// method.
217///
218///   [web of trust]: https://en.wikipedia.org/wiki/Web_of_trust
219#[derive(Debug)]
220pub struct GoodChecksum<'a> {
221    /// The signature.
222    pub sig: &'a Signature,
223
224    /// The signing key that made the signature.
225    ///
226    /// The amalgamation of the signing key includes the necessary
227    /// context (i.e. certificate, time, policy) to evaluate the
228    /// trustworthiness of the signature using a trust model.
229    pub ka: ValidErasedKeyAmalgamation<'a, key::PublicParts>,
230}
231assert_send_and_sync!(GoodChecksum<'_>);
232
233/// A bad signature.
234///
235/// Represents the result of an unsuccessful signature verification.
236/// It contains all the context that could be gathered until the
237/// verification process failed.
238///
239/// `VerificationError` is used in [`VerificationResult`].  See also
240/// [`GoodChecksum`].
241///
242///
243/// You can either explicitly match on the variants, or convert to
244/// [`Error`] using [`From`].
245///
246///   [`Error`]: super::super::Error
247///   [`From`]: std::convert::From
248#[non_exhaustive]
249#[derive(Debug)]
250pub enum VerificationError<'a> {
251    /// Missing Key
252    MissingKey {
253        /// The signature.
254        sig: &'a Signature,
255    },
256    /// Unbound key.
257    ///
258    /// There is no valid binding signature at the time the signature
259    /// was created under the given policy.
260    UnboundKey {
261        /// The signature.
262        sig: &'a Signature,
263
264        /// The certificate that made the signature.
265        cert: &'a Cert,
266
267        /// The reason why the key is not bound.
268        error: anyhow::Error,
269    },
270    /// Bad key (have a key, but it is not alive, etc.)
271    BadKey {
272        /// The signature.
273        sig: &'a Signature,
274
275        /// The signing key that made the signature.
276        ka: ValidErasedKeyAmalgamation<'a, key::PublicParts>,
277
278        /// The reason why the key is bad.
279        error: anyhow::Error,
280    },
281    /// Bad signature (have a valid key, but the signature didn't check out)
282    BadSignature {
283        /// The signature.
284        sig: &'a Signature,
285
286        /// The signing key that made the signature.
287        ka: ValidErasedKeyAmalgamation<'a, key::PublicParts>,
288
289        /// The reason why the signature is bad.
290        error: anyhow::Error,
291    },
292
293    /// Malformed signature (no signature creation subpacket, etc.).
294    MalformedSignature {
295        /// The signature.
296        sig: &'a Signature,
297
298        /// The reason why the signature is malformed.
299        error: anyhow::Error,
300    },
301
302    /// A signature that failed to parse at all.
303    UnknownSignature {
304        /// The signature parsed into an [`crate::packet::Unknown`]
305        /// packet.
306        sig: &'a Unknown,
307    }
308}
309assert_send_and_sync!(VerificationError<'_>);
310
311impl<'a> std::fmt::Display for VerificationError<'a> {
312    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
313        use self::VerificationError::*;
314        match self {
315            MalformedSignature { error, .. } =>
316                write!(f, "Malformed signature: {}", error),
317            UnknownSignature { sig, .. } =>
318                write!(f, "Malformed signature: {}", sig.error()),
319            MissingKey { sig } =>
320                if let Some(issuer) = sig.get_issuers().get(0) {
321                    write!(f, "Missing key: {}", issuer)
322                } else {
323                    write!(f, "Missing key")
324                },
325            UnboundKey { cert, error, .. } =>
326                write!(f, "Subkey of {} not bound: {}", cert, error),
327            BadKey { ka, error, .. } =>
328                write!(f, "Subkey of {} is bad: {}", ka.cert(), error),
329            BadSignature { error, .. } =>
330                write!(f, "Bad signature: {}", error),
331        }
332    }
333}
334
335impl<'a> std::error::Error for VerificationError<'a> {
336    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
337        use self::VerificationError::*;
338        match self {
339            MissingKey { .. } => None,
340            UnboundKey { error, .. } =>
341                Some(error.as_ref()),
342            BadKey { error, .. } =>
343                Some(error.as_ref()),
344            BadSignature { error, .. } =>
345                Some(error.as_ref()),
346            MalformedSignature { error, .. } =>
347                Some(error.as_ref()),
348            UnknownSignature { .. } => None,
349        }
350    }
351}
352
353impl<'a> From<VerificationError<'a>> for Error {
354    fn from(e: VerificationError<'a>) -> Self {
355        use self::VerificationError::*;
356        match e {
357            MalformedSignature { .. } =>
358                Error::MalformedPacket(e.to_string()),
359            UnknownSignature { sig } =>
360                Error::MalformedPacket(sig.error().to_string()),
361            MissingKey { .. } =>
362                Error::InvalidKey(e.to_string()),
363            UnboundKey { .. } =>
364                Error::InvalidKey(e.to_string()),
365            BadKey { .. } =>
366                Error::InvalidKey(e.to_string()),
367            BadSignature { .. } =>
368                Error::BadSignature(e.to_string()),
369        }
370    }
371}
372
373/// Like VerificationError, but without referencing the signature.
374///
375/// This avoids borrowing the signature, so that we can continue to
376/// mutably borrow the signature trying other keys.  After all keys
377/// are tried, we attach the reference to the signature, yielding a
378/// `VerificationError`.
379enum VerificationErrorInternal<'a> {
380    // MalformedSignature is not used, so it is omitted here.
381
382    /// Missing Key
383    MissingKey {
384    },
385    /// Unbound key.
386    ///
387    /// There is no valid binding signature at the time the signature
388    /// was created under the given policy.
389    UnboundKey {
390        /// The certificate that made the signature.
391        cert: &'a Cert,
392
393        /// The reason why the key is not bound.
394        error: anyhow::Error,
395    },
396    /// Bad key (have a key, but it is not alive, etc.)
397    BadKey {
398        /// The signing key that made the signature.
399        ka: ValidErasedKeyAmalgamation<'a, key::PublicParts>,
400
401        /// The reason why the key is bad.
402        error: anyhow::Error,
403    },
404    /// Bad signature (have a valid key, but the signature didn't check out)
405    BadSignature {
406        /// The signing key that made the signature.
407        ka: ValidErasedKeyAmalgamation<'a, key::PublicParts>,
408
409        /// The reason why the signature is bad.
410        error: anyhow::Error,
411    },
412}
413
414impl<'a> VerificationErrorInternal<'a> {
415    fn attach_sig(self, sig: &'a Signature) -> VerificationError<'a> {
416        use self::VerificationErrorInternal::*;
417        match self {
418            MissingKey {} =>
419                VerificationError::MissingKey { sig },
420            UnboundKey { cert, error } =>
421                VerificationError::UnboundKey { sig, cert, error },
422            BadKey { ka, error } =>
423                VerificationError::BadKey { sig, ka, error },
424            BadSignature { ka, error } =>
425                VerificationError::BadSignature { sig, ka, error },
426        }
427    }
428}
429
430/// Communicates the message structure to the VerificationHelper.
431///
432/// A valid OpenPGP message contains one literal data packet with
433/// optional [encryption, signing, and compression layers] freely
434/// combined on top.  This structure is passed to
435/// [`VerificationHelper::check`] for verification.
436///
437///  [encryption, signing, and compression layers]: MessageLayer
438///
439/// The most common structure is an optionally encrypted, optionally
440/// compressed, and optionally signed message, i.e. if the message is
441/// encrypted, then the encryption is the outermost layer; if the
442/// message is signed, then the signature group is the innermost
443/// layer.  This is a sketch of such a message:
444///
445/// ```text
446/// [ encryption layer: [ compression layer: [ signature group: [ literal data ]]]]
447/// ```
448///
449/// However, OpenPGP allows encryption, signing, and compression
450/// operations to be freely combined (see [Section 10.3 of RFC 9580]).
451/// This is represented as a stack of [`MessageLayer`]s, where
452/// signatures of the same level (i.e. those over the same data:
453/// either directly over the literal data, or over other signatures
454/// and the literal data) are grouped into one layer.  See also
455/// [`Signature::level`].
456///
457///   [Section 10.3 of RFC 9580]: https://www.rfc-editor.org/rfc/rfc9580.html#section-10.3
458///   [`Signature::level`]: crate::packet::Signature#method.level
459///
460/// Consider the following structure.  This is a set of notarizing
461/// signatures *N* over a set of signatures *S* over the literal data:
462///
463/// ```text
464/// [ signature group: [ signature group: [ literal data ]]]
465/// ```
466///
467/// The notarizing signatures *N* are said to be of level 1,
468/// i.e. signatures over the signatures *S* and the literal data.  The
469/// signatures *S* are level 0 signatures, i.e. signatures over the
470/// literal data.
471///
472/// OpenPGP's flexibility allows adaption to new use cases, but also
473/// presents a challenge to implementations and downstream users.  The
474/// message structure must be both validated, and possibly
475/// communicated to the application's user.  Note that if
476/// compatibility is a concern, generated messages must be restricted
477/// to a narrow subset of possible structures, see this [test of
478/// unusual message structures].
479///
480///   [test of unusual message structures]: https://tests.sequoia-pgp.org/#Unusual_Message_Structure
481#[derive(Debug)]
482pub struct MessageStructure<'a> {
483    layers: Vec<MessageLayer<'a>>,
484    processed_csf_message: bool,
485}
486assert_send_and_sync!(MessageStructure<'_>);
487
488impl<'a> MessageStructure<'a> {
489    fn new(processed_csf_message: bool) -> Self {
490        MessageStructure {
491            layers: Vec::new(),
492            processed_csf_message,
493        }
494    }
495
496    fn new_compression_layer(&mut self, algo: CompressionAlgorithm) {
497        self.layers.push(MessageLayer::Compression {
498            algo,
499        })
500    }
501
502    fn new_encryption_layer(&mut self, sym_algo: SymmetricAlgorithm,
503                            aead_algo: Option<AEADAlgorithm>) {
504        self.layers.push(MessageLayer::Encryption {
505            sym_algo,
506            aead_algo,
507        })
508    }
509
510    fn new_signature_group(&mut self) {
511        self.layers.push(MessageLayer::SignatureGroup {
512            results: Vec::new(),
513        })
514    }
515
516    fn push_verification_result(&mut self, sig: VerificationResult<'a>) {
517        if let Some(MessageLayer::SignatureGroup { ref mut results }) =
518            self.layers.iter_mut().last()
519        {
520            results.push(sig);
521        } else {
522            panic!("cannot push to encryption or compression layer");
523        }
524    }
525
526    /// Returns an iterator over the message layers.
527    pub fn iter(&self) -> impl Iterator<Item=&MessageLayer<'a>> {
528        self.layers.iter()
529    }
530
531    /// Returns whether we processed a signed message using the
532    /// Cleartext Signature Framework.
533    ///
534    /// This function returns whether the parser parsed a cleartext
535    /// signature using the cleartext transformation.
536    pub fn processed_csf_message(&self) -> bool {
537        self.processed_csf_message
538    }
539}
540
541impl<'a> IntoIterator for MessageStructure<'a> {
542    type Item = MessageLayer<'a>;
543    type IntoIter = std::vec::IntoIter<MessageLayer<'a>>;
544
545    fn into_iter(self) -> Self::IntoIter {
546        self.layers.into_iter()
547    }
548}
549
550/// Represents a layer of the message structure.
551///
552/// A valid OpenPGP message contains one literal data packet with
553/// optional encryption, signing, and compression layers freely
554/// combined on top (see [Section 10.3 of RFC 9580]).  This enum
555/// represents the layers.  The [`MessageStructure`] is communicated
556/// to the [`VerificationHelper::check`].  Iterating over the
557/// [`MessageStructure`] yields the individual message layers.
558///
559///   [Section 10.3 of RFC 9580]: https://www.rfc-editor.org/rfc/rfc9580.html#section-10.3
560#[derive(Debug)]
561pub enum MessageLayer<'a> {
562    /// Represents a compression container.
563    ///
564    /// Compression is usually transparent in OpenPGP, though it may
565    /// sometimes be interesting for advanced users to indicate that
566    /// the message was compressed, and how (see [Section 5.6 of RFC
567    /// 9580]).
568    ///
569    ///   [Section 5.6 of RFC 9580]: https://www.rfc-editor.org/rfc/rfc9580.html#section-5.6
570    Compression {
571        /// Compression algorithm used.
572        algo: CompressionAlgorithm,
573    },
574    /// Represents an encryption container.
575    ///
576    /// Indicates the fact that the message was encrypted (see
577    /// [Section 5.13 of RFC 9580]).  If you expect encrypted
578    /// messages, make sure that there is at least one encryption
579    /// container present.
580    ///
581    ///   [Section 5.13 of RFC 9580]: https://www.rfc-editor.org/rfc/rfc9580.html#section-5.13
582    Encryption {
583        /// Symmetric algorithm used.
584        sym_algo: SymmetricAlgorithm,
585        /// AEAD algorithm used, if any.
586        aead_algo: Option<AEADAlgorithm>,
587    },
588    /// Represents a signature group.
589    ///
590    /// A signature group consists of all signatures with the same
591    /// level (see [Section 5.2 of RFC 9580]).  Each
592    /// [`VerificationResult`] represents the result of a single
593    /// signature verification.  In your [`VerificationHelper::check`]
594    /// method, iterate over the verification results, see if it meets
595    /// your policies' demands, and communicate it to the user, if
596    /// applicable.
597    ///
598    ///   [Section 5.2 of RFC 9580]: https://www.rfc-editor.org/rfc/rfc9580.html#section-5.2
599    SignatureGroup {
600        /// The results of the signature verifications.
601        results: Vec<VerificationResult<'a>>,
602    }
603}
604assert_send_and_sync!(MessageLayer<'_>);
605
606/// Internal version of the message structure.
607///
608/// In contrast to MessageStructure, this owns unverified
609/// signature packets.
610#[derive(Debug)]
611struct IMessageStructure {
612    layers: Vec<IMessageLayer>,
613
614    // We insert a SignatureGroup layer every time we see a OnePassSig
615    // packet with the last flag.
616    //
617    // However, we need to make sure that we insert a SignatureGroup
618    // layer even if the OnePassSig packet has the last flag set to
619    // false.  To do that, we keep track of the fact that we saw such
620    // a OPS packet.
621    sig_group_counter: usize,
622}
623
624impl IMessageStructure {
625    fn new() -> Self {
626        IMessageStructure {
627            layers: Vec::new(),
628            sig_group_counter: 0,
629        }
630    }
631
632    fn new_compression_layer(&mut self, algo: CompressionAlgorithm) {
633        tracer!(TRACE, "IMessageStructure::new_compression_layer", TRACE_INDENT);
634        t!("pushing a {:?} layer", algo);
635
636        self.insert_missing_signature_group();
637        self.layers.push(IMessageLayer::Compression {
638            algo,
639        });
640    }
641
642    fn new_encryption_layer(&mut self,
643                            depth: isize,
644                            expect_mdc: bool,
645                            sym_algo: SymmetricAlgorithm,
646                            aead_algo: Option<AEADAlgorithm>) {
647        tracer!(TRACE, "IMessageStructure::new_encryption_layer", TRACE_INDENT);
648        t!("pushing a {:?}/{:?} layer", sym_algo, aead_algo);
649
650        self.insert_missing_signature_group();
651        self.layers.push(IMessageLayer::Encryption {
652            depth,
653            expect_mdc,
654            sym_algo,
655            aead_algo,
656        });
657    }
658
659    /// Returns whether we expect an MDC packet in an
660    /// encryption container at this recursion depth.
661    ///
662    /// Handling MDC packets has to be done carefully, otherwise, we
663    /// may create a decryption oracle.
664    fn expect_mdc_at(&self, at: isize) -> bool {
665        for l in &self.layers {
666            match l {
667                IMessageLayer::Encryption {
668                    depth,
669                    expect_mdc,
670                    ..
671                } if *depth == at && *expect_mdc => return true,
672                _ => (),
673            }
674        }
675        false
676    }
677
678    /// Makes sure that we insert a signature group even if the
679    /// previous OPS packet had the last flag set to false.
680    fn insert_missing_signature_group(&mut self) {
681        tracer!(TRACE, "IMessageStructure::insert_missing_signature_group",
682                TRACE_INDENT);
683
684        if self.sig_group_counter > 0 {
685            t!("implicit insert of signature group for {} sigs",
686               self.sig_group_counter);
687
688            self.layers.push(IMessageLayer::SignatureGroup {
689                sigs: Vec::new(),
690                count: self.sig_group_counter,
691            });
692        }
693        self.sig_group_counter = 0;
694    }
695
696    fn push_ops(&mut self, ops: &OnePassSig) {
697        tracer!(TRACE, "IMessageStructure::push_ops", TRACE_INDENT);
698        t!("Pushing {:?}", ops);
699
700        self.sig_group_counter += 1;
701        if ops.last() {
702            self.layers.push(IMessageLayer::SignatureGroup {
703                sigs: Vec::new(),
704                count: self.sig_group_counter,
705            });
706            self.sig_group_counter = 0;
707        }
708    }
709
710    fn push_signature(&mut self, sig: MaybeSignature, csf_message: bool) {
711        tracer!(TRACE, "IMessageStructure::push_signature", TRACE_INDENT);
712        t!("Pushing {:?}", sig);
713        if csf_message {
714            t!("Cleartext Signature Framework transformation enabled");
715        }
716
717        for (i, layer) in self.layers.iter_mut().enumerate().rev() {
718            t!("{}: {:?}", i, layer);
719            match layer {
720                IMessageLayer::SignatureGroup {
721                    ref mut sigs, ref mut count,
722                } if *count > 0 => {
723                    t!("Layer {} is a signature group with {} outstanding sigs",
724                       i, *count);
725
726                    sigs.push(sig);
727                    if csf_message {
728                        // The CSF transformation does not know how
729                        // many signatures will follow, so we may end
730                        // up with too few synthesized OPS packets.
731                        // But, we only have one layer anyway, and no
732                        // notarizations, so we don't need to concern
733                        // ourselves with the counter.
734                    } else {
735                        *count -= 1;
736                    }
737                    return;
738                },
739                _ => (),
740            }
741        }
742
743        // As a last resort, push a new signature group for this
744        // signature.  This may not accurately describe the structure,
745        // but if we get to this point, we failed to grasp the message
746        // structure in some way, so there is nothing we can do really.
747        t!("signature unaccounted for");
748        self.layers.push(IMessageLayer::SignatureGroup {
749            sigs: vec![sig],
750            count: 0,
751        });
752    }
753
754    fn push_bare_signature(&mut self, sig: MaybeSignature) {
755        if let Some(IMessageLayer::SignatureGroup { .. }) = self.layers.iter().last() {
756            // The last layer is a SignatureGroup.  We will append the
757            // signature there without accounting for it.
758        } else {
759            // The last layer is not a SignatureGroup, or there is no
760            // layer at all.  Create one.
761            self.layers.push(IMessageLayer::SignatureGroup {
762                sigs: Vec::new(),
763                count: 0,
764            });
765        }
766
767        if let IMessageLayer::SignatureGroup { ref mut sigs, .. } =
768            self.layers.iter_mut().last().expect("just checked or created")
769        {
770            sigs.push(sig);
771        } else {
772            unreachable!("just checked or created")
773        }
774    }
775
776}
777
778/// Internal version of a layer of the message structure.
779///
780/// In contrast to MessageLayer, this owns unverified signature packets.
781#[derive(Debug)]
782enum IMessageLayer {
783    Compression {
784        algo: CompressionAlgorithm,
785    },
786    Encryption {
787        /// Recursion depth of this container.
788        depth: isize,
789        /// Do we expect an MDC packet?
790        ///
791        /// I.e. is this a SEIPv1 container?
792        expect_mdc: bool,
793        sym_algo: SymmetricAlgorithm,
794        aead_algo: Option<AEADAlgorithm>,
795    },
796    SignatureGroup {
797        sigs: Vec<MaybeSignature>,
798        count: usize,
799    }
800}
801
802/// Represents [`Signature`]s and those that failed to parse in the
803/// form of [`Unknown`] packets.
804type MaybeSignature = std::result::Result<Signature, Unknown>;
805
806/// Helper for signature verification.
807///
808/// This trait abstracts over signature and message structure
809/// verification.  It allows us to provide the [`Verifier`],
810/// [`DetachedVerifier`], and [`Decryptor`] without imposing a policy
811/// on how certificates for signature verification are looked up, or
812/// what message structure is considered acceptable.
813///
814///
815/// It also allows you to inspect each packet that is processed during
816/// verification or decryption, optionally providing a [`Map`] for
817/// each packet.
818///
819///   [`Map`]: super::map::Map
820pub trait VerificationHelper {
821    /// Inspects the message.
822    ///
823    /// Called once per packet.  Can be used to inspect and dump
824    /// packets in encrypted messages.
825    ///
826    /// The default implementation does nothing.
827    fn inspect(&mut self, pp: &PacketParser) -> Result<()> {
828        // Do nothing.
829        let _ = pp;
830        Ok(())
831    }
832
833    /// Retrieves the certificates containing the specified keys.
834    ///
835    /// When implementing this method, you should return as many
836    /// certificates corresponding to the `ids` as you can.
837    ///
838    /// If an identifier is ambiguous, because, for instance, there
839    /// are multiple certificates with the same Key ID, then you
840    /// should return all of them.
841    ///
842    /// You should only return an error if processing should be
843    /// aborted.  In general, you shouldn't return an error if you
844    /// don't have a certificate for a given identifier: if there are
845    /// multiple signatures, then, depending on your policy, verifying
846    /// a subset of them may be sufficient.
847    ///
848    /// This method will be called at most once per message.
849    ///
850    /// # Examples
851    ///
852    /// This example demonstrates how to look up the certificates for
853    /// the signature verification given the list of signature
854    /// issuers.
855    ///
856    /// ```
857    /// use sequoia_openpgp as openpgp;
858    /// use openpgp::{KeyHandle, Cert, Result};
859    /// use openpgp::parse::stream::*;
860    /// # fn lookup_cert_by_handle(_: &KeyHandle) -> Result<Cert> {
861    /// #     unimplemented!()
862    /// # }
863    ///
864    /// struct Helper { /* ... */ }
865    /// impl VerificationHelper for Helper {
866    ///     fn get_certs(&mut self, ids: &[KeyHandle]) -> Result<Vec<Cert>> {
867    ///         let mut certs = Vec::new();
868    ///         for id in ids {
869    ///             certs.push(lookup_cert_by_handle(id)?);
870    ///         }
871    ///         Ok(certs)
872    ///     }
873    ///     // ...
874    /// #    fn check(&mut self, structure: MessageStructure) -> Result<()> {
875    /// #        unimplemented!()
876    /// #    }
877    /// }
878    /// ```
879    fn get_certs(&mut self, ids: &[crate::KeyHandle]) -> Result<Vec<Cert>>;
880
881    /// Validates the message structure.
882    ///
883    /// This function must validate the message's structure according
884    /// to an application specific policy.  For example, it could
885    /// check that the required number of signatures or notarizations
886    /// were confirmed as good, and evaluate every signature's
887    /// validity under a trust model.
888    ///
889    /// A valid OpenPGP message contains one literal data packet with
890    /// optional encryption, signing, and compression layers on top.
891    /// Notably, the message structure contains the results of
892    /// signature verifications.  See [`MessageStructure`] for more
893    /// information.
894    ///
895    ///
896    /// When verifying a message, this callback will be called exactly
897    /// once per message *after* the last signature has been verified
898    /// and *before* all the data has been returned.  Any error
899    /// returned by this function will abort reading, and the error
900    /// will be propagated via the [`io::Read`] operation.
901    ///
902    ///   [`io::Read`]: std::io::Read
903    ///
904    /// After this method was called, [`Verifier::message_processed`]
905    /// and [`Decryptor::message_processed`] return `true`.
906    ///
907    ///   [`Verifier::message_processed`]: Verifier::message_processed()
908    ///   [`Decryptor::message_processed`]: Decryptor::message_processed()
909    ///
910    /// When verifying a detached signature using the
911    /// [`DetachedVerifier`], this method will be called with a
912    /// [`MessageStructure`] containing exactly one layer, a signature
913    /// group.
914    ///
915    ///
916    /// # Examples
917    ///
918    /// This example demonstrates how to verify that the message is an
919    /// encrypted, optionally compressed, and signed message that has
920    /// at least one valid signature.
921    ///
922    /// ```
923    /// use sequoia_openpgp as openpgp;
924    /// use openpgp::{KeyHandle, Cert, Result};
925    /// use openpgp::parse::stream::*;
926    ///
927    /// struct Helper { /* ... */ }
928    /// impl VerificationHelper for Helper {
929    /// #    fn get_certs(&mut self, ids: &[KeyHandle]) -> Result<Vec<Cert>> {
930    /// #        unimplemented!();
931    /// #    }
932    ///     fn check(&mut self, structure: MessageStructure) -> Result<()> {
933    ///         for (i, layer) in structure.into_iter().enumerate() {
934    ///             match layer {
935    ///                 MessageLayer::Encryption { .. } if i == 0 => (),
936    ///                 MessageLayer::Compression { .. } if i == 1 => (),
937    ///                 MessageLayer::SignatureGroup { ref results }
938    ///                     if i == 1 || i == 2 =>
939    ///                 {
940    ///                     if ! results.iter().any(|r| r.is_ok()) {
941    ///                         return Err(anyhow::anyhow!(
942    ///                                        "No valid signature"));
943    ///                     }
944    ///                 }
945    ///                 _ => return Err(anyhow::anyhow!(
946    ///                                     "Unexpected message structure")),
947    ///             }
948    ///         }
949    ///         Ok(())
950    ///     }
951    ///     // ...
952    /// }
953    /// ```
954    fn check(&mut self, structure: MessageStructure) -> Result<()>;
955}
956
957/// Wraps a VerificationHelper and adds a non-functional
958/// DecryptionHelper implementation.
959struct NoDecryptionHelper<V: VerificationHelper> {
960    v: V,
961}
962
963impl<V: VerificationHelper> VerificationHelper for NoDecryptionHelper<V> {
964    fn get_certs(&mut self, ids: &[crate::KeyHandle]) -> Result<Vec<Cert>>
965    {
966        self.v.get_certs(ids)
967    }
968    fn check(&mut self, structure: MessageStructure) -> Result<()>
969    {
970        self.v.check(structure)
971    }
972    fn inspect(&mut self, pp: &PacketParser) -> Result<()> {
973        self.v.inspect(pp)
974    }
975}
976
977impl<V: VerificationHelper> DecryptionHelper for NoDecryptionHelper<V> {
978    fn decrypt(&mut self, _: &[PKESK], _: &[SKESK],
979               _: Option<SymmetricAlgorithm>,
980               _: &mut dyn FnMut(Option<SymmetricAlgorithm>, &SessionKey) -> bool)
981               -> Result<Option<Cert>>
982    {
983        unreachable!("This is not used for verifications")
984    }
985}
986
987/// Verifies a signed OpenPGP message.
988///
989/// To create a `Verifier`, create a [`VerifierBuilder`] using
990/// [`Parse`], and customize it to your needs.
991///
992///   [`Parse`]: super::Parse
993///
994/// Signature verification requires processing the whole message
995/// first.  Therefore, OpenPGP implementations supporting streaming
996/// operations necessarily must output unverified data.  This has been
997/// a source of problems in the past.  To alleviate this, we buffer
998/// the message first (up to 25 megabytes of net message data by
999/// default, see [`DEFAULT_BUFFER_SIZE`]), and verify the signatures
1000/// if the message fits into our buffer.  Nevertheless it is important
1001/// to treat the data as unverified and untrustworthy until you have
1002/// seen a positive verification.  See [`Verifier::message_processed`]
1003/// for more information.
1004///
1005///   [`Verifier::message_processed`]: Verifier::message_processed()
1006///
1007/// See [`GoodChecksum`] for what it means for a signature to be
1008/// considered valid.
1009///
1010///
1011/// # Examples
1012///
1013/// ```
1014/// # fn main() -> sequoia_openpgp::Result<()> {
1015/// use std::io::Read;
1016/// use sequoia_openpgp as openpgp;
1017/// use openpgp::{KeyHandle, Cert, Result};
1018/// use openpgp::parse::{Parse, stream::*};
1019/// use openpgp::policy::StandardPolicy;
1020/// # fn lookup_cert_by_handle(_: &KeyHandle) -> Result<Cert> {
1021/// #     Cert::from_bytes(
1022/// #       &b"-----BEGIN PGP PUBLIC KEY BLOCK-----
1023/// #
1024/// #          xjMEWlNvABYJKwYBBAHaRw8BAQdA+EC2pvebpEbzPA9YplVgVXzkIG5eK+7wEAez
1025/// #          lcBgLJrNMVRlc3R5IE1jVGVzdGZhY2UgKG15IG5ldyBrZXkpIDx0ZXN0eUBleGFt
1026/// #          cGxlLm9yZz7CkAQTFggAOBYhBDnRAKtn1b2MBAECBfs3UfFYfa7xBQJaU28AAhsD
1027/// #          BQsJCAcCBhUICQoLAgQWAgMBAh4BAheAAAoJEPs3UfFYfa7xJHQBAO4/GABMWUcJ
1028/// #          5D/DZ9b+6YiFnysSjCT/gILJgxMgl7uoAPwJherI1pAAh49RnPHBR1IkWDtwzX65
1029/// #          CJG8sDyO2FhzDs44BFpTbwASCisGAQQBl1UBBQEBB0B+A0GRHuBgdDX50T1nePjb
1030/// #          mKQ5PeqXJbWEtVrUtVJaPwMBCAfCeAQYFggAIBYhBDnRAKtn1b2MBAECBfs3UfFY
1031/// #          fa7xBQJaU28AAhsMAAoJEPs3UfFYfa7xzjIBANX2/FgDX3WkmvwpEHg/sn40zACM
1032/// #          W2hrBY5x0sZ8H7JlAP47mCfCuRVBqyaePuzKbxLJeLe2BpDdc0n2izMVj8t9Cg==
1033/// #          =QetZ
1034/// #          -----END PGP PUBLIC KEY BLOCK-----"[..])
1035/// # }
1036///
1037/// let p = &StandardPolicy::new();
1038///
1039/// // This fetches keys and computes the validity of the verification.
1040/// struct Helper {}
1041/// impl VerificationHelper for Helper {
1042///     fn get_certs(&mut self, ids: &[KeyHandle]) -> Result<Vec<Cert>> {
1043///         let mut certs = Vec::new();
1044///         for id in ids {
1045///             certs.push(lookup_cert_by_handle(id)?);
1046///         }
1047///         Ok(certs)
1048///     }
1049///
1050///     fn check(&mut self, structure: MessageStructure) -> Result<()> {
1051///         for (i, layer) in structure.into_iter().enumerate() {
1052///             match layer {
1053///                 MessageLayer::Encryption { .. } if i == 0 => (),
1054///                 MessageLayer::Compression { .. } if i == 1 => (),
1055///                 MessageLayer::SignatureGroup { ref results } => {
1056///                     if ! results.iter().any(|r| r.is_ok()) {
1057///                         return Err(anyhow::anyhow!(
1058///                                        "No valid signature"));
1059///                     }
1060///                 }
1061///                 _ => return Err(anyhow::anyhow!(
1062///                                     "Unexpected message structure")),
1063///             }
1064///         }
1065///         Ok(())
1066///     }
1067/// }
1068///
1069/// let message =
1070///    b"-----BEGIN PGP MESSAGE-----
1071///
1072///      xA0DAAoW+zdR8Vh9rvEByxJiAAAAAABIZWxsbyBXb3JsZCHCdQQAFgoABgWCXrLl
1073///      AQAhCRD7N1HxWH2u8RYhBDnRAKtn1b2MBAECBfs3UfFYfa7xRUsBAJaxkU/RCstf
1074///      UD7TM30IorO1Mb9cDa/hPRxyzipulT55AQDN1m9LMqi9yJDjHNHwYYVwxDcg+pLY
1075///      YmAFv/UfO0vYBw==
1076///      =+l94
1077///      -----END PGP MESSAGE-----
1078///      ";
1079///
1080/// let h = Helper {};
1081/// let mut v = VerifierBuilder::from_bytes(&message[..])?
1082///     .with_policy(p, None, h)?;
1083///
1084/// let mut content = Vec::new();
1085/// v.read_to_end(&mut content)?;
1086/// assert_eq!(content, b"Hello World!");
1087/// # Ok(()) }
1088pub struct Verifier<'a, H: VerificationHelper> {
1089    decryptor: Decryptor<'a, NoDecryptionHelper<H>>,
1090}
1091assert_send_and_sync!(Verifier<'_, H> where H: VerificationHelper);
1092
1093/// A builder for `Verifier`.
1094///
1095/// This allows the customization of [`Verifier`], which can
1096/// be built using [`VerifierBuilder::with_policy`].
1097///
1098///   [`VerifierBuilder::with_policy`]: VerifierBuilder::with_policy()
1099pub struct VerifierBuilder<'a> {
1100    message: Box<dyn BufferedReader<Cookie> + 'a>,
1101    buffer_size: usize,
1102    mapping: bool,
1103}
1104assert_send_and_sync!(VerifierBuilder<'_>);
1105
1106impl<'a> Parse<'a, VerifierBuilder<'a>>
1107    for VerifierBuilder<'a>
1108{
1109    fn from_buffered_reader<R>(reader: R) -> Result<VerifierBuilder<'a>>
1110    where
1111        R: BufferedReader<Cookie> + 'a,
1112    {
1113        VerifierBuilder::new(reader)
1114    }
1115}
1116
1117impl<'a> crate::seal::Sealed for VerifierBuilder<'a> {}
1118
1119impl<'a> VerifierBuilder<'a> {
1120    fn new<B>(signatures: B) -> Result<Self>
1121        where B: buffered_reader::BufferedReader<Cookie> + 'a
1122    {
1123        Ok(VerifierBuilder {
1124            message: Box::new(signatures),
1125            buffer_size: DEFAULT_BUFFER_SIZE,
1126            mapping: false,
1127        })
1128    }
1129
1130    /// Changes the amount of buffered data.
1131    ///
1132    /// By default, we buffer up to 25 megabytes of net message data
1133    /// (see [`DEFAULT_BUFFER_SIZE`]).  This changes the default.
1134    ///
1135    ///
1136    /// # Examples
1137    ///
1138    /// ```
1139    /// # fn main() -> sequoia_openpgp::Result<()> {
1140    /// use sequoia_openpgp as openpgp;
1141    /// # use openpgp::{KeyHandle, Cert, Result};
1142    /// use openpgp::parse::{Parse, stream::*};
1143    /// use openpgp::policy::StandardPolicy;
1144    ///
1145    /// let p = &StandardPolicy::new();
1146    ///
1147    /// struct Helper {}
1148    /// impl VerificationHelper for Helper {
1149    ///     // ...
1150    /// #   fn get_certs(&mut self, ids: &[KeyHandle]) -> Result<Vec<Cert>> {
1151    /// #       Ok(Vec::new())
1152    /// #   }
1153    /// #
1154    /// #   fn check(&mut self, structure: MessageStructure) -> Result<()> {
1155    /// #       Ok(())
1156    /// #   }
1157    /// }
1158    ///
1159    /// let message =
1160    ///     // ...
1161    /// # &b"-----BEGIN PGP MESSAGE-----
1162    /// #
1163    /// #    xA0DAAoW+zdR8Vh9rvEByxJiAAAAAABIZWxsbyBXb3JsZCHCdQQAFgoABgWCXrLl
1164    /// #    AQAhCRD7N1HxWH2u8RYhBDnRAKtn1b2MBAECBfs3UfFYfa7xRUsBAJaxkU/RCstf
1165    /// #    UD7TM30IorO1Mb9cDa/hPRxyzipulT55AQDN1m9LMqi9yJDjHNHwYYVwxDcg+pLY
1166    /// #    YmAFv/UfO0vYBw==
1167    /// #    =+l94
1168    /// #    -----END PGP MESSAGE-----
1169    /// #    "[..];
1170    ///
1171    /// let h = Helper {};
1172    /// let mut v = VerifierBuilder::from_bytes(message)?
1173    ///     .buffer_size(1 << 12)
1174    ///     .with_policy(p, None, h)?;
1175    /// # let _ = v;
1176    /// # Ok(()) }
1177    /// ```
1178    pub fn buffer_size(mut self, size: usize) -> Self {
1179        self.buffer_size = size;
1180        self
1181    }
1182
1183    /// Enables mapping.
1184    ///
1185    /// If mapping is enabled, the packet parser will create a [`Map`]
1186    /// of the packets that can be inspected in
1187    /// [`VerificationHelper::inspect`].  Note that this buffers the
1188    /// packets contents, and is not recommended unless you know that
1189    /// the packets are small.
1190    ///
1191    ///   [`Map`]: super::map::Map
1192    ///
1193    /// # Examples
1194    ///
1195    /// ```
1196    /// # fn main() -> sequoia_openpgp::Result<()> {
1197    /// use sequoia_openpgp as openpgp;
1198    /// # use openpgp::{KeyHandle, Cert, Result};
1199    /// use openpgp::parse::{Parse, stream::*};
1200    /// use openpgp::policy::StandardPolicy;
1201    ///
1202    /// let p = &StandardPolicy::new();
1203    ///
1204    /// struct Helper {}
1205    /// impl VerificationHelper for Helper {
1206    ///     // ...
1207    /// #   fn get_certs(&mut self, ids: &[KeyHandle]) -> Result<Vec<Cert>> {
1208    /// #       Ok(Vec::new())
1209    /// #   }
1210    /// #
1211    /// #   fn check(&mut self, structure: MessageStructure) -> Result<()> {
1212    /// #       Ok(())
1213    /// #   }
1214    /// }
1215    ///
1216    /// let message =
1217    ///     // ...
1218    /// # &b"-----BEGIN PGP MESSAGE-----
1219    /// #
1220    /// #    xA0DAAoW+zdR8Vh9rvEByxJiAAAAAABIZWxsbyBXb3JsZCHCdQQAFgoABgWCXrLl
1221    /// #    AQAhCRD7N1HxWH2u8RYhBDnRAKtn1b2MBAECBfs3UfFYfa7xRUsBAJaxkU/RCstf
1222    /// #    UD7TM30IorO1Mb9cDa/hPRxyzipulT55AQDN1m9LMqi9yJDjHNHwYYVwxDcg+pLY
1223    /// #    YmAFv/UfO0vYBw==
1224    /// #    =+l94
1225    /// #    -----END PGP MESSAGE-----
1226    /// #    "[..];
1227    ///
1228    /// let h = Helper {};
1229    /// let mut v = VerifierBuilder::from_bytes(message)?
1230    ///     .mapping(true)
1231    ///     .with_policy(p, None, h)?;
1232    /// # let _ = v;
1233    /// # Ok(()) }
1234    /// ```
1235    pub fn mapping(mut self, enabled: bool) -> Self {
1236        self.mapping = enabled;
1237        self
1238    }
1239
1240    /// Creates the `Verifier`.
1241    ///
1242    /// Signature verifications are done under the given `policy` and
1243    /// relative to time `time`, or the current time, if `time` is
1244    /// `None`.  `helper` is the [`VerificationHelper`] to use.
1245    ///
1246    ///
1247    /// # Examples
1248    ///
1249    /// ```
1250    /// # fn main() -> sequoia_openpgp::Result<()> {
1251    /// use sequoia_openpgp as openpgp;
1252    /// # use openpgp::{KeyHandle, Cert, Result};
1253    /// use openpgp::parse::{Parse, stream::*};
1254    /// use openpgp::policy::StandardPolicy;
1255    ///
1256    /// let p = &StandardPolicy::new();
1257    ///
1258    /// struct Helper {}
1259    /// impl VerificationHelper for Helper {
1260    ///     // ...
1261    /// #   fn get_certs(&mut self, ids: &[KeyHandle]) -> Result<Vec<Cert>> {
1262    /// #       Ok(Vec::new())
1263    /// #   }
1264    /// #
1265    /// #   fn check(&mut self, structure: MessageStructure) -> Result<()> {
1266    /// #       Ok(())
1267    /// #   }
1268    /// }
1269    ///
1270    /// let message =
1271    ///     // ...
1272    /// # &b"-----BEGIN PGP MESSAGE-----
1273    /// #
1274    /// #    xA0DAAoW+zdR8Vh9rvEByxJiAAAAAABIZWxsbyBXb3JsZCHCdQQAFgoABgWCXrLl
1275    /// #    AQAhCRD7N1HxWH2u8RYhBDnRAKtn1b2MBAECBfs3UfFYfa7xRUsBAJaxkU/RCstf
1276    /// #    UD7TM30IorO1Mb9cDa/hPRxyzipulT55AQDN1m9LMqi9yJDjHNHwYYVwxDcg+pLY
1277    /// #    YmAFv/UfO0vYBw==
1278    /// #    =+l94
1279    /// #    -----END PGP MESSAGE-----
1280    /// #    "[..];
1281    ///
1282    /// let h = Helper {};
1283    /// let mut v = VerifierBuilder::from_bytes(message)?
1284    ///     // Customize the `Verifier` here.
1285    ///     .with_policy(p, None, h)?;
1286    /// # let _ = v;
1287    /// # Ok(()) }
1288    /// ```
1289    pub fn with_policy<T, H>(self, policy: &'a dyn Policy, time: T, helper: H)
1290                             -> Result<Verifier<'a, H>>
1291        where H: VerificationHelper,
1292              T: Into<Option<time::SystemTime>>,
1293    {
1294        // Do not eagerly map `t` to the current time.
1295        let t = time.into();
1296        Ok(Verifier {
1297            decryptor: Decryptor::from_cookie_reader(
1298                policy,
1299                self.message,
1300                NoDecryptionHelper { v: helper, },
1301                t, Mode::Verify, self.buffer_size, self.mapping, true)?,
1302        })
1303    }
1304}
1305
1306impl<'a, H: VerificationHelper> Verifier<'a, H> {
1307    /// Returns a reference to the helper.
1308    pub fn helper_ref(&self) -> &H {
1309        &self.decryptor.helper_ref().v
1310    }
1311
1312    /// Returns a mutable reference to the helper.
1313    pub fn helper_mut(&mut self) -> &mut H {
1314        &mut self.decryptor.helper_mut().v
1315    }
1316
1317    /// Recovers the helper.
1318    pub fn into_helper(self) -> H {
1319        self.decryptor.into_helper().v
1320    }
1321
1322    /// Returns true if the whole message has been processed and
1323    /// authenticated.
1324    ///
1325    /// If the function returns `true`, the whole message has been
1326    /// processed, the signatures are verified, and the message
1327    /// structure has been passed to [`VerificationHelper::check`].
1328    /// Data read from this `Verifier` using [`io::Read`] has been
1329    /// authenticated.
1330    ///
1331    ///   [`io::Read`]: std::io::Read
1332    ///
1333    /// If the function returns `false`, the message did not fit into
1334    /// the internal buffer, and therefore data read from this
1335    /// `Verifier` using [`io::Read`] has **not yet been
1336    /// authenticated**.  It is important to treat this data as
1337    /// attacker controlled and not use it until it has been
1338    /// authenticated.
1339    ///
1340    /// # Examples
1341    ///
1342    /// This example demonstrates how to verify a message in a
1343    /// streaming fashion, writing the data to a temporary file and
1344    /// only commit the result once the data is authenticated.
1345    ///
1346    /// ```
1347    /// # fn main() -> sequoia_openpgp::Result<()> {
1348    /// use std::io::{Read, Seek, SeekFrom};
1349    /// use sequoia_openpgp as openpgp;
1350    /// use openpgp::{KeyHandle, Cert, Result};
1351    /// use openpgp::parse::{Parse, stream::*};
1352    /// use openpgp::policy::StandardPolicy;
1353    /// #
1354    /// # // Mock of `tempfile::tempfile`.
1355    /// # mod tempfile {
1356    /// #     pub fn tempfile() -> sequoia_openpgp::Result<std::fs::File> {
1357    /// #         unimplemented!()
1358    /// #     }
1359    /// # }
1360    ///
1361    /// let p = &StandardPolicy::new();
1362    ///
1363    /// // This fetches keys and computes the validity of the verification.
1364    /// struct Helper {}
1365    /// impl VerificationHelper for Helper {
1366    ///     // ...
1367    /// #   fn get_certs(&mut self, ids: &[KeyHandle]) -> Result<Vec<Cert>> {
1368    /// #       Ok(Vec::new())
1369    /// #   }
1370    /// #   fn check(&mut self, _: MessageStructure) -> Result<()> {
1371    /// #       Ok(())
1372    /// #   }
1373    /// }
1374    ///
1375    /// let mut source =
1376    ///    // ...
1377    /// #  std::io::Cursor::new(&b"-----BEGIN PGP MESSAGE-----
1378    /// #
1379    /// #    xA0DAAoW+zdR8Vh9rvEByxJiAAAAAABIZWxsbyBXb3JsZCHCdQQAFgoABgWCXrLl
1380    /// #    AQAhCRD7N1HxWH2u8RYhBDnRAKtn1b2MBAECBfs3UfFYfa7xRUsBAJaxkU/RCstf
1381    /// #    UD7TM30IorO1Mb9cDa/hPRxyzipulT55AQDN1m9LMqi9yJDjHNHwYYVwxDcg+pLY
1382    /// #    YmAFv/UfO0vYBw==
1383    /// #    =+l94
1384    /// #    -----END PGP MESSAGE-----
1385    /// #    "[..]);
1386    ///
1387    /// fn consume(r: &mut dyn Read) -> Result<()> {
1388    ///    // ...
1389    /// #   let _ = r; Ok(())
1390    /// }
1391    ///
1392    /// let h = Helper {};
1393    /// let mut v = VerifierBuilder::from_reader(&mut source)?
1394    ///     .with_policy(p, None, h)?;
1395    ///
1396    /// if v.message_processed() {
1397    ///     // The data has been authenticated.
1398    ///     consume(&mut v)?;
1399    /// } else {
1400    ///     let mut tmp = tempfile::tempfile()?;
1401    ///     std::io::copy(&mut v, &mut tmp)?;
1402    ///
1403    ///     // If the copy succeeds, the message has been fully
1404    ///     // processed and the data has been authenticated.
1405    ///     assert!(v.message_processed());
1406    ///
1407    ///     // Rewind and consume.
1408    ///     tmp.seek(SeekFrom::Start(0))?;
1409    ///     consume(&mut tmp)?;
1410    /// }
1411    /// # Ok(()) }
1412    /// ```
1413    pub fn message_processed(&self) -> bool {
1414        self.decryptor.message_processed()
1415    }
1416
1417    /// Returns if we are processing a signed message using the
1418    /// Cleartext Signature Framework.
1419    ///
1420    /// This function returns `None` if we don't know yet (i.e.,
1421    /// before we've started parsing the packets).  Once the first
1422    /// packet has been parsed, this will return `Some`.
1423    pub fn processing_csf_message(&self) -> Option<bool> {
1424        self.decryptor.processing_csf_message.clone()
1425    }
1426}
1427
1428impl<'a, H: VerificationHelper> io::Read for Verifier<'a, H> {
1429    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
1430        self.decryptor.read(buf)
1431    }
1432}
1433
1434
1435/// Verifies a detached signature.
1436///
1437/// To create a `DetachedVerifier`, create a
1438/// [`DetachedVerifierBuilder`] using [`Parse`], and customize it to
1439/// your needs.
1440///
1441///   [`Parse`]: super::Parse
1442///
1443/// See [`GoodChecksum`] for what it means for a signature to be
1444/// considered valid.  When the signature(s) are processed,
1445/// [`VerificationHelper::check`] will be called with a
1446/// [`MessageStructure`] containing exactly one layer, a signature
1447/// group.
1448///
1449///
1450/// # Examples
1451///
1452/// ```
1453/// # fn main() -> sequoia_openpgp::Result<()> {
1454/// use std::io::{self, Read};
1455/// use sequoia_openpgp as openpgp;
1456/// use openpgp::{KeyHandle, Cert, Result};
1457/// use openpgp::parse::{Parse, stream::*};
1458/// use sequoia_openpgp::policy::StandardPolicy;
1459///
1460/// let p = &StandardPolicy::new();
1461///
1462/// // This fetches keys and computes the validity of the verification.
1463/// struct Helper {}
1464/// impl VerificationHelper for Helper {
1465///     fn get_certs(&mut self, _ids: &[KeyHandle]) -> Result<Vec<Cert>> {
1466///         Ok(Vec::new()) // Feed the Certs to the verifier here...
1467///     }
1468///     fn check(&mut self, structure: MessageStructure) -> Result<()> {
1469///         Ok(()) // Implement your verification policy here.
1470///     }
1471/// }
1472///
1473/// let signature =
1474///    b"-----BEGIN PGP SIGNATURE-----
1475///
1476///      wnUEABYKACcFglt+z/EWoQSOjDP6RiYzeXbZeXgGnAw0jdgsGQmQBpwMNI3YLBkA
1477///      AHmUAP9mpj2wV0/ekDuzxZrPQ0bnobFVaxZGg7YzdlksSOERrwEA6v6czXQjKcv2
1478///      KOwGTamb+ajTLQ3YRG9lh+ZYIXynvwE=
1479///      =IJ29
1480///      -----END PGP SIGNATURE-----";
1481///
1482/// let data = b"Hello World!";
1483/// let h = Helper {};
1484/// let mut v = DetachedVerifierBuilder::from_bytes(&signature[..])?
1485///     .with_policy(p, None, h)?;
1486/// v.verify_bytes(data)?;
1487/// # Ok(()) }
1488pub struct DetachedVerifier<'a, H: VerificationHelper> {
1489    decryptor: Decryptor<'a, NoDecryptionHelper<H>>,
1490}
1491assert_send_and_sync!(DetachedVerifier<'_, H> where H: VerificationHelper);
1492
1493/// A builder for `DetachedVerifier`.
1494///
1495/// This allows the customization of [`DetachedVerifier`], which can
1496/// be built using [`DetachedVerifierBuilder::with_policy`].
1497///
1498///   [`DetachedVerifierBuilder::with_policy`]: DetachedVerifierBuilder::with_policy()
1499pub struct DetachedVerifierBuilder<'a> {
1500    signatures: Box<dyn BufferedReader<Cookie> + 'a>,
1501    mapping: bool,
1502}
1503assert_send_and_sync!(DetachedVerifierBuilder<'_>);
1504
1505impl<'a> Parse<'a, DetachedVerifierBuilder<'a>>
1506    for DetachedVerifierBuilder<'a>
1507{
1508    fn from_buffered_reader<R>(reader: R) -> Result<DetachedVerifierBuilder<'a>>
1509    where
1510        R: BufferedReader<Cookie> + 'a,
1511    {
1512        DetachedVerifierBuilder::new(reader)
1513    }
1514}
1515
1516impl<'a> crate::seal::Sealed for DetachedVerifierBuilder<'a> {}
1517
1518impl<'a> DetachedVerifierBuilder<'a> {
1519    fn new<B>(signatures: B) -> Result<Self>
1520        where B: buffered_reader::BufferedReader<Cookie> + 'a
1521    {
1522        Ok(DetachedVerifierBuilder {
1523            signatures: Box::new(signatures),
1524            mapping: false,
1525        })
1526    }
1527
1528    /// Enables mapping.
1529    ///
1530    /// If mapping is enabled, the packet parser will create a [`Map`]
1531    /// of the packets that can be inspected in
1532    /// [`VerificationHelper::inspect`].  Note that this buffers the
1533    /// packets contents, and is not recommended unless you know that
1534    /// the packets are small.
1535    ///
1536    ///   [`Map`]: super::map::Map
1537    ///
1538    /// # Examples
1539    ///
1540    /// ```
1541    /// # fn main() -> sequoia_openpgp::Result<()> {
1542    /// use sequoia_openpgp as openpgp;
1543    /// # use openpgp::{KeyHandle, Cert, Result};
1544    /// use openpgp::parse::{Parse, stream::*};
1545    /// use openpgp::policy::StandardPolicy;
1546    ///
1547    /// let p = &StandardPolicy::new();
1548    ///
1549    /// struct Helper {}
1550    /// impl VerificationHelper for Helper {
1551    ///     // ...
1552    /// #   fn get_certs(&mut self, ids: &[KeyHandle]) -> Result<Vec<Cert>> {
1553    /// #       Ok(Vec::new())
1554    /// #   }
1555    /// #
1556    /// #   fn check(&mut self, structure: MessageStructure) -> Result<()> {
1557    /// #       Ok(())
1558    /// #   }
1559    /// }
1560    ///
1561    /// let signature =
1562    ///     // ...
1563    /// #  b"-----BEGIN PGP SIGNATURE-----
1564    /// #
1565    /// #    wnUEABYKACcFglt+z/EWoQSOjDP6RiYzeXbZeXgGnAw0jdgsGQmQBpwMNI3YLBkA
1566    /// #    AHmUAP9mpj2wV0/ekDuzxZrPQ0bnobFVaxZGg7YzdlksSOERrwEA6v6czXQjKcv2
1567    /// #    KOwGTamb+ajTLQ3YRG9lh+ZYIXynvwE=
1568    /// #    =IJ29
1569    /// #    -----END PGP SIGNATURE-----";
1570    ///
1571    /// let h = Helper {};
1572    /// let mut v = DetachedVerifierBuilder::from_bytes(&signature[..])?
1573    ///     .mapping(true)
1574    ///     .with_policy(p, None, h)?;
1575    /// # let _ = v;
1576    /// # Ok(()) }
1577    /// ```
1578    pub fn mapping(mut self, enabled: bool) -> Self {
1579        self.mapping = enabled;
1580        self
1581    }
1582
1583    /// Creates the `DetachedVerifier`.
1584    ///
1585    /// Signature verifications are done under the given `policy` and
1586    /// relative to time `time`, or the current time, if `time` is
1587    /// `None`.  `helper` is the [`VerificationHelper`] to use.
1588    /// [`VerificationHelper::check`] will be called with a
1589    /// [`MessageStructure`] containing exactly one layer, a signature
1590    /// group.
1591    ///
1592    ///
1593    /// # Examples
1594    ///
1595    /// ```
1596    /// # fn main() -> sequoia_openpgp::Result<()> {
1597    /// use sequoia_openpgp as openpgp;
1598    /// # use openpgp::{KeyHandle, Cert, Result};
1599    /// use openpgp::parse::{Parse, stream::*};
1600    /// use openpgp::policy::StandardPolicy;
1601    ///
1602    /// let p = &StandardPolicy::new();
1603    ///
1604    /// struct Helper {}
1605    /// impl VerificationHelper for Helper {
1606    ///     // ...
1607    /// #   fn get_certs(&mut self, ids: &[KeyHandle]) -> Result<Vec<Cert>> {
1608    /// #       Ok(Vec::new())
1609    /// #   }
1610    /// #
1611    /// #   fn check(&mut self, structure: MessageStructure) -> Result<()> {
1612    /// #       Ok(())
1613    /// #   }
1614    /// }
1615    ///
1616    /// let signature =
1617    ///     // ...
1618    /// #  b"-----BEGIN PGP SIGNATURE-----
1619    /// #
1620    /// #    wnUEABYKACcFglt+z/EWoQSOjDP6RiYzeXbZeXgGnAw0jdgsGQmQBpwMNI3YLBkA
1621    /// #    AHmUAP9mpj2wV0/ekDuzxZrPQ0bnobFVaxZGg7YzdlksSOERrwEA6v6czXQjKcv2
1622    /// #    KOwGTamb+ajTLQ3YRG9lh+ZYIXynvwE=
1623    /// #    =IJ29
1624    /// #    -----END PGP SIGNATURE-----";
1625    ///
1626    /// let h = Helper {};
1627    /// let mut v = DetachedVerifierBuilder::from_bytes(&signature[..])?
1628    ///     // Customize the `DetachedVerifier` here.
1629    ///     .with_policy(p, None, h)?;
1630    /// # let _ = v;
1631    /// # Ok(()) }
1632    /// ```
1633    pub fn with_policy<T, H>(self, policy: &'a dyn Policy, time: T, helper: H)
1634                             -> Result<DetachedVerifier<'a, H>>
1635        where H: VerificationHelper,
1636              T: Into<Option<time::SystemTime>>,
1637    {
1638        // Do not eagerly map `t` to the current time.
1639        let t = time.into();
1640        Ok(DetachedVerifier {
1641            decryptor: Decryptor::from_cookie_reader(
1642                policy,
1643                self.signatures,
1644                NoDecryptionHelper { v: helper, },
1645                t, Mode::VerifyDetached, 0, self.mapping, false)?,
1646        })
1647    }
1648}
1649
1650impl<'a, H: VerificationHelper> DetachedVerifier<'a, H> {
1651    /// Verifies the given data.
1652    pub fn verify_buffered_reader<R>(&mut self, reader: R)
1653                                     -> Result<()>
1654    where
1655        R: BufferedReader<Cookie>,
1656    {
1657        self.decryptor.verify_detached(reader.into_boxed())
1658    }
1659
1660    /// Verifies the given data.
1661    pub fn verify_reader<R: io::Read + Send + Sync>(&mut self, reader: R) -> Result<()> {
1662        self.verify_buffered_reader(buffered_reader::Generic::with_cookie(
1663            reader, None, Default::default()))
1664    }
1665
1666    /// Verifies the given data.
1667    pub fn verify_file<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
1668        self.verify_buffered_reader(buffered_reader::File::with_cookie(
1669            path, Default::default())?)
1670    }
1671
1672    /// Verifies the given data.
1673    pub fn verify_bytes<B: AsRef<[u8]>>(&mut self, buf: B) -> Result<()> {
1674        self.verify_buffered_reader(buffered_reader::Memory::with_cookie(
1675            buf.as_ref(), Default::default()))
1676    }
1677
1678    /// Returns a reference to the helper.
1679    pub fn helper_ref(&self) -> &H {
1680        &self.decryptor.helper_ref().v
1681    }
1682
1683    /// Returns a mutable reference to the helper.
1684    pub fn helper_mut(&mut self) -> &mut H {
1685        &mut self.decryptor.helper_mut().v
1686    }
1687
1688    /// Recovers the helper.
1689    pub fn into_helper(self) -> H {
1690        self.decryptor.into_helper().v
1691    }
1692}
1693
1694
1695/// Modes of operation for the Decryptor.
1696#[derive(Debug, PartialEq, Eq)]
1697enum Mode {
1698    Decrypt,
1699    Verify,
1700    VerifyDetached,
1701}
1702
1703/// Decrypts and verifies an encrypted and optionally signed OpenPGP
1704/// message.
1705///
1706/// To create a `Decryptor`, create a [`DecryptorBuilder`] using
1707/// [`Parse`], and customize it to your needs.
1708///
1709///   [`Parse`]: super::Parse
1710///
1711/// Signature verification and detection of ciphertext tampering
1712/// requires processing the whole message first.  Therefore, OpenPGP
1713/// implementations supporting streaming operations necessarily must
1714/// output unverified data.  This has been a source of problems in the
1715/// past.  To alleviate this, we buffer the message first (up to 25
1716/// megabytes of net message data by default, see
1717/// [`DEFAULT_BUFFER_SIZE`]), and verify the signatures if the message
1718/// fits into our buffer.  Nevertheless it is important to treat the
1719/// data as unverified and untrustworthy until you have seen a
1720/// positive verification.  See [`Decryptor::message_processed`] for
1721/// more information.
1722///
1723///   [`Decryptor::message_processed`]: Decryptor::message_processed()
1724///
1725/// See [`GoodChecksum`] for what it means for a signature to be
1726/// considered valid.
1727///
1728///
1729/// # Examples
1730///
1731/// ```
1732/// # fn main() -> sequoia_openpgp::Result<()> {
1733/// use std::io::Read;
1734/// use sequoia_openpgp as openpgp;
1735/// use openpgp::crypto::SessionKey;
1736/// use openpgp::types::SymmetricAlgorithm;
1737/// use openpgp::{KeyID, Cert, Result, packet::{Key, PKESK, SKESK}};
1738/// use openpgp::parse::{Parse, stream::*};
1739/// use sequoia_openpgp::policy::StandardPolicy;
1740///
1741/// let p = &StandardPolicy::new();
1742///
1743/// // This fetches keys and computes the validity of the verification.
1744/// struct Helper {}
1745/// impl VerificationHelper for Helper {
1746///     fn get_certs(&mut self, _ids: &[openpgp::KeyHandle]) -> Result<Vec<Cert>> {
1747///         Ok(Vec::new()) // Feed the Certs to the verifier here...
1748///     }
1749///     fn check(&mut self, structure: MessageStructure) -> Result<()> {
1750///         Ok(()) // Implement your verification policy here.
1751///     }
1752/// }
1753/// impl DecryptionHelper for Helper {
1754///     fn decrypt(&mut self, _: &[PKESK], skesks: &[SKESK],
1755///                _sym_algo: Option<SymmetricAlgorithm>,
1756///                decrypt: &mut dyn FnMut(Option<SymmetricAlgorithm>, &SessionKey) -> bool)
1757///                -> Result<Option<Cert>>
1758///     {
1759///         skesks[0].decrypt(&"streng geheim".into())
1760///             .map(|(algo, session_key)| decrypt(algo, &session_key));
1761///         Ok(None)
1762///     }
1763/// }
1764///
1765/// let message =
1766///    b"-----BEGIN PGP MESSAGE-----
1767///
1768///      wy4ECQMIY5Zs8RerVcXp85UgoUKjKkevNPX3WfcS5eb7rkT9I6kw6N2eEc5PJUDh
1769///      0j0B9mnPKeIwhp2kBHpLX/en6RfNqYauX9eSeia7aqsd/AOLbO9WMCLZS5d2LTxN
1770///      rwwb8Aggyukj13Mi0FF5
1771///      =OB/8
1772///      -----END PGP MESSAGE-----";
1773///
1774/// let h = Helper {};
1775/// let mut v = DecryptorBuilder::from_bytes(&message[..])?
1776///     .with_policy(p, None, h)?;
1777///
1778/// let mut content = Vec::new();
1779/// v.read_to_end(&mut content)?;
1780/// assert_eq!(content, b"Hello World!");
1781/// # Ok(()) }
1782pub struct Decryptor<'a, H: VerificationHelper + DecryptionHelper> {
1783    helper: H,
1784
1785    /// The issuers collected from OPS and Signature packets.
1786    issuers: Vec<KeyHandle>,
1787
1788    /// The certificates used for signature verification.
1789    certs: Vec<Cert>,
1790
1791    oppr: Option<PacketParserResult<'a>>,
1792    identity: Option<Fingerprint>,
1793    structure: IMessageStructure,
1794
1795    /// We want to hold back some data until the signatures checked
1796    /// out.  We buffer this here, cursor is the offset of unread
1797    /// bytes in the buffer.
1798    buffer_size: usize,
1799    reserve: Option<Protected>,
1800    cursor: usize,
1801
1802    /// The mode of operation.
1803    mode: Mode,
1804
1805    /// Whether we are actually processing a cleartext signature
1806    /// framework message.  If so, we need to tweak our behavior a
1807    /// bit.
1808    processing_csf_message: Option<bool>,
1809
1810    /// Signature verification relative to this time.
1811    ///
1812    /// This is needed for checking the signature's liveness.
1813    ///
1814    /// We want the same semantics as `Subpacket::signature_alive`.
1815    /// Specifically, when using the current time, we want to tolerate
1816    /// some clock skew, but when using some specific time, we don't.
1817    /// (See `Subpacket::signature_alive` for an explanation.)
1818    ///
1819    /// These semantics can be realized by making `time` an
1820    /// `Option<time::SystemTime>` and passing that as is to
1821    /// `Subpacket::signature_alive`.  But that approach has two new
1822    /// problems.  First, if we are told to use the current time, then
1823    /// we want to use the time at which the Verifier was
1824    /// instantiated, not the time at which we call
1825    /// `Subpacket::signature_alive`.  Second, if we call
1826    /// `Subpacket::signature_alive` multiple times, they should all
1827    /// use the same time.  To work around these issues, when a
1828    /// Verifier is instantiated, we evaluate `time` and we record how
1829    /// much we want to tolerate clock skew in the same way as
1830    /// `Subpacket::signature_alive`.
1831    time: time::SystemTime,
1832    clock_skew_tolerance: time::Duration,
1833
1834    policy: &'a dyn Policy,
1835}
1836assert_send_and_sync!(Decryptor<'_, H>
1837      where H: VerificationHelper + DecryptionHelper);
1838
1839/// A builder for `Decryptor`.
1840///
1841/// This allows the customization of [`Decryptor`], which can
1842/// be built using [`DecryptorBuilder::with_policy`].
1843///
1844///   [`DecryptorBuilder::with_policy`]: DecryptorBuilder::with_policy()
1845pub struct DecryptorBuilder<'a> {
1846    message: Box<dyn BufferedReader<Cookie> + 'a>,
1847    buffer_size: usize,
1848    mapping: bool,
1849}
1850assert_send_and_sync!(DecryptorBuilder<'_>);
1851
1852impl<'a> Parse<'a, DecryptorBuilder<'a>>
1853    for DecryptorBuilder<'a>
1854{
1855    fn from_buffered_reader<R>(reader: R) -> Result<DecryptorBuilder<'a>>
1856    where
1857        R: BufferedReader<Cookie> + 'a,
1858    {
1859        DecryptorBuilder::new(reader)
1860    }
1861}
1862
1863impl<'a> crate::seal::Sealed for DecryptorBuilder<'a> {}
1864
1865impl<'a> DecryptorBuilder<'a> {
1866    fn new<B>(signatures: B) -> Result<Self>
1867        where B: buffered_reader::BufferedReader<Cookie> + 'a
1868    {
1869        Ok(DecryptorBuilder {
1870            message: Box::new(signatures),
1871            buffer_size: DEFAULT_BUFFER_SIZE,
1872            mapping: false,
1873        })
1874    }
1875
1876    /// Changes the amount of buffered data.
1877    ///
1878    /// By default, we buffer up to 25 megabytes of net message data
1879    /// (see [`DEFAULT_BUFFER_SIZE`]).  This changes the default.
1880    ///
1881    ///
1882    /// # Examples
1883    ///
1884    /// ```
1885    /// # fn main() -> sequoia_openpgp::Result<()> {
1886    /// use sequoia_openpgp as openpgp;
1887    /// # use openpgp::{*, crypto::*, packet::prelude::*, types::*};
1888    /// use openpgp::parse::{Parse, stream::*};
1889    /// use openpgp::policy::StandardPolicy;
1890    ///
1891    /// let p = &StandardPolicy::new();
1892    ///
1893    /// struct Helper {}
1894    /// impl VerificationHelper for Helper {
1895    ///     // ...
1896    /// #   fn get_certs(&mut self, ids: &[KeyHandle]) -> Result<Vec<Cert>> {
1897    /// #       Ok(Vec::new())
1898    /// #   }
1899    /// #
1900    /// #   fn check(&mut self, structure: MessageStructure) -> Result<()> {
1901    /// #       Ok(())
1902    /// #   }
1903    /// }
1904    /// impl DecryptionHelper for Helper {
1905    ///     // ...
1906    /// #   fn decrypt(&mut self, _: &[PKESK], skesks: &[SKESK],
1907    /// #              _sym_algo: Option<SymmetricAlgorithm>,
1908    /// #              decrypt: &mut dyn FnMut(Option<SymmetricAlgorithm>, &SessionKey) -> bool)
1909    /// #              -> Result<Option<Cert>>
1910    /// #   {
1911    /// #       Ok(None)
1912    /// #   }
1913    /// }
1914    ///
1915    /// let message =
1916    ///     // ...
1917    /// # &b"-----BEGIN PGP MESSAGE-----
1918    /// #
1919    /// #    xA0DAAoW+zdR8Vh9rvEByxJiAAAAAABIZWxsbyBXb3JsZCHCdQQAFgoABgWCXrLl
1920    /// #    AQAhCRD7N1HxWH2u8RYhBDnRAKtn1b2MBAECBfs3UfFYfa7xRUsBAJaxkU/RCstf
1921    /// #    UD7TM30IorO1Mb9cDa/hPRxyzipulT55AQDN1m9LMqi9yJDjHNHwYYVwxDcg+pLY
1922    /// #    YmAFv/UfO0vYBw==
1923    /// #    =+l94
1924    /// #    -----END PGP MESSAGE-----
1925    /// #    "[..];
1926    ///
1927    /// let h = Helper {};
1928    /// let mut v = DecryptorBuilder::from_bytes(message)?
1929    ///     .buffer_size(1 << 12)
1930    ///     .with_policy(p, None, h)?;
1931    /// # let _ = v;
1932    /// # Ok(()) }
1933    /// ```
1934    pub fn buffer_size(mut self, size: usize) -> Self {
1935        self.buffer_size = size;
1936        self
1937    }
1938
1939    /// Enables mapping.
1940    ///
1941    /// If mapping is enabled, the packet parser will create a [`Map`]
1942    /// of the packets that can be inspected in
1943    /// [`VerificationHelper::inspect`].  Note that this buffers the
1944    /// packets contents, and is not recommended unless you know that
1945    /// the packets are small.
1946    ///
1947    ///   [`Map`]: super::map::Map
1948    ///
1949    /// # Examples
1950    ///
1951    /// ```
1952    /// # fn main() -> sequoia_openpgp::Result<()> {
1953    /// use sequoia_openpgp as openpgp;
1954    /// # use openpgp::{*, crypto::*, packet::prelude::*, types::*};
1955    /// use openpgp::parse::{Parse, stream::*};
1956    /// use openpgp::policy::StandardPolicy;
1957    ///
1958    /// let p = &StandardPolicy::new();
1959    ///
1960    /// struct Helper {}
1961    /// impl VerificationHelper for Helper {
1962    ///     // ...
1963    /// #   fn get_certs(&mut self, ids: &[KeyHandle]) -> Result<Vec<Cert>> {
1964    /// #       Ok(Vec::new())
1965    /// #   }
1966    /// #
1967    /// #   fn check(&mut self, structure: MessageStructure) -> Result<()> {
1968    /// #       Ok(())
1969    /// #   }
1970    /// }
1971    /// impl DecryptionHelper for Helper {
1972    ///     // ...
1973    /// #   fn decrypt(&mut self, _: &[PKESK], skesks: &[SKESK],
1974    /// #              _sym_algo: Option<SymmetricAlgorithm>,
1975    /// #              decrypt: &mut dyn FnMut(Option<SymmetricAlgorithm>, &SessionKey) -> bool)
1976    /// #              -> Result<Option<Cert>>
1977    /// #   {
1978    /// #       Ok(None)
1979    /// #   }
1980    /// }
1981    ///
1982    /// let message =
1983    ///     // ...
1984    /// # &b"-----BEGIN PGP MESSAGE-----
1985    /// #
1986    /// #    xA0DAAoW+zdR8Vh9rvEByxJiAAAAAABIZWxsbyBXb3JsZCHCdQQAFgoABgWCXrLl
1987    /// #    AQAhCRD7N1HxWH2u8RYhBDnRAKtn1b2MBAECBfs3UfFYfa7xRUsBAJaxkU/RCstf
1988    /// #    UD7TM30IorO1Mb9cDa/hPRxyzipulT55AQDN1m9LMqi9yJDjHNHwYYVwxDcg+pLY
1989    /// #    YmAFv/UfO0vYBw==
1990    /// #    =+l94
1991    /// #    -----END PGP MESSAGE-----
1992    /// #    "[..];
1993    ///
1994    /// let h = Helper {};
1995    /// let mut v = DecryptorBuilder::from_bytes(message)?
1996    ///     .mapping(true)
1997    ///     .with_policy(p, None, h)?;
1998    /// # let _ = v;
1999    /// # Ok(()) }
2000    /// ```
2001    pub fn mapping(mut self, enabled: bool) -> Self {
2002        self.mapping = enabled;
2003        self
2004    }
2005
2006    /// Creates the `Decryptor`.
2007    ///
2008    /// Signature verifications are done under the given `policy` and
2009    /// relative to time `time`, or the current time, if `time` is
2010    /// `None`.  `helper` is the [`VerificationHelper`] and
2011    /// [`DecryptionHelper`] to use.
2012    ///
2013    ///
2014    /// # Examples
2015    ///
2016    /// ```
2017    /// # fn main() -> sequoia_openpgp::Result<()> {
2018    /// use sequoia_openpgp as openpgp;
2019    /// # use openpgp::{*, crypto::*, packet::prelude::*, types::*};
2020    /// use openpgp::parse::{Parse, stream::*};
2021    /// use openpgp::policy::StandardPolicy;
2022    ///
2023    /// let p = &StandardPolicy::new();
2024    ///
2025    /// struct Helper {}
2026    /// impl VerificationHelper for Helper {
2027    ///     // ...
2028    /// #   fn get_certs(&mut self, ids: &[KeyHandle]) -> Result<Vec<Cert>> {
2029    /// #       Ok(Vec::new())
2030    /// #   }
2031    /// #
2032    /// #   fn check(&mut self, structure: MessageStructure) -> Result<()> {
2033    /// #       Ok(())
2034    /// #   }
2035    /// }
2036    /// impl DecryptionHelper for Helper {
2037    ///     // ...
2038    /// #   fn decrypt(&mut self, _: &[PKESK], skesks: &[SKESK],
2039    /// #              _sym_algo: Option<SymmetricAlgorithm>,
2040    /// #              decrypt: &mut dyn FnMut(Option<SymmetricAlgorithm>, &SessionKey) -> bool)
2041    /// #              -> Result<Option<Cert>>
2042    /// #   {
2043    /// #       Ok(None)
2044    /// #   }
2045    /// }
2046    ///
2047    /// let message =
2048    ///     // ...
2049    /// # &b"-----BEGIN PGP MESSAGE-----
2050    /// #
2051    /// #    xA0DAAoW+zdR8Vh9rvEByxJiAAAAAABIZWxsbyBXb3JsZCHCdQQAFgoABgWCXrLl
2052    /// #    AQAhCRD7N1HxWH2u8RYhBDnRAKtn1b2MBAECBfs3UfFYfa7xRUsBAJaxkU/RCstf
2053    /// #    UD7TM30IorO1Mb9cDa/hPRxyzipulT55AQDN1m9LMqi9yJDjHNHwYYVwxDcg+pLY
2054    /// #    YmAFv/UfO0vYBw==
2055    /// #    =+l94
2056    /// #    -----END PGP MESSAGE-----
2057    /// #    "[..];
2058    ///
2059    /// let h = Helper {};
2060    /// let mut v = DecryptorBuilder::from_bytes(message)?
2061    ///     // Customize the `Decryptor` here.
2062    ///     .with_policy(p, None, h)?;
2063    /// # let _ = v;
2064    /// # Ok(()) }
2065    /// ```
2066    pub fn with_policy<T, H>(self, policy: &'a dyn Policy, time: T, helper: H)
2067                             -> Result<Decryptor<'a, H>>
2068        where H: VerificationHelper + DecryptionHelper,
2069              T: Into<Option<time::SystemTime>>,
2070    {
2071        // Do not eagerly map `t` to the current time.
2072        let t = time.into();
2073        Decryptor::from_cookie_reader(
2074            policy,
2075            self.message,
2076            helper,
2077            t, Mode::Decrypt, self.buffer_size, self.mapping, false)
2078    }
2079}
2080
2081/// Helper for decrypting messages.
2082///
2083/// This trait abstracts over session key decryption.  It allows us to
2084/// provide the [`Decryptor`] without imposing any policy on how the
2085/// session key is decrypted.
2086///
2087pub trait DecryptionHelper {
2088    /// Decrypts the message.
2089    ///
2090    /// This function is called with every [`PKESK`] and [`SKESK`]
2091    /// packet found in the message.  The implementation must decrypt
2092    /// the symmetric algorithm and session key from one of the
2093    /// [`PKESK`] packets, the [`SKESK`] packets, or retrieve it from
2094    /// a cache, and then call `decrypt` with the symmetric algorithm
2095    /// and session key.  `decrypt` returns `true` if the decryption
2096    /// was successful.
2097    ///
2098    ///   [`PKESK`]: crate::packet::PKESK
2099    ///   [`SKESK`]: crate::packet::SKESK
2100    ///
2101    /// If a symmetric algorithm is given, it should be passed on to
2102    /// [`PKESK::decrypt`].
2103    ///
2104    ///   [`PKESK::decrypt`]: crate::packet::PKESK#method.decrypt
2105    ///
2106    /// If the message is decrypted using a [`PKESK`] packet, then the
2107    /// fingerprint of the certificate containing the encryption
2108    /// subkey should be returned.  This is used in conjunction with
2109    /// the intended recipient subpacket (see [Intended Recipient
2110    /// Fingerprint]) to prevent [*Surreptitious Forwarding*].
2111    ///
2112    ///   [Intended Recipient Fingerprint]: https://www.rfc-editor.org/rfc/rfc9580.html#name-intended-recipient-fingerpr
2113    ///   [*Surreptitious Forwarding*]: http://world.std.com/~dtd/sign_encrypt/sign_encrypt7.html
2114    ///
2115    /// This method will be called once per encryption layer.
2116    ///
2117    /// # Examples
2118    ///
2119    /// This example demonstrates how to decrypt a message using local
2120    /// keys (i.e. excluding remote keys like smart cards) while
2121    /// maximizing convenience for the user.
2122    ///
2123    /// ```
2124    /// use sequoia_openpgp as openpgp;
2125    /// use openpgp::{Cert, Fingerprint, KeyHandle, KeyID, Result};
2126    /// use openpgp::crypto::SessionKey;
2127    /// use openpgp::types::SymmetricAlgorithm;
2128    /// use openpgp::packet::{PKESK, SKESK};
2129    /// # use openpgp::packet::{Key, key::*};
2130    /// use openpgp::parse::stream::*;
2131    /// # fn lookup_cache(_: &[PKESK], _: &[SKESK])
2132    /// #                 -> Option<(Option<Cert>, Option<SymmetricAlgorithm>, SessionKey)> {
2133    /// #     unimplemented!()
2134    /// # }
2135    /// # fn lookup_key(_: Option<KeyHandle>)
2136    /// #               -> Option<(Cert, Key<SecretParts, UnspecifiedRole>)> {
2137    /// #     unimplemented!()
2138    /// # }
2139    /// # fn all_keys() -> impl Iterator<Item = (Cert, Key<SecretParts, UnspecifiedRole>)> {
2140    /// #     Vec::new().into_iter()
2141    /// # }
2142    ///
2143    /// struct Helper { /* ... */ }
2144    /// impl DecryptionHelper for Helper {
2145    ///     fn decrypt(&mut self, pkesks: &[PKESK], skesks: &[SKESK],
2146    ///                sym_algo: Option<SymmetricAlgorithm>,
2147    ///                decrypt: &mut dyn FnMut(Option<SymmetricAlgorithm>, &SessionKey) -> bool)
2148    ///                -> Result<Option<Cert>>
2149    ///     {
2150    ///         // Try to decrypt, from the most convenient method to the
2151    ///         // least convenient one.
2152    ///
2153    ///         // First, see if it is in the cache.
2154    ///         if let Some((cert, algo, sk)) = lookup_cache(pkesks, skesks) {
2155    ///             if decrypt(algo, &sk) {
2156    ///                 return Ok(cert);
2157    ///             }
2158    ///         }
2159    ///
2160    ///         // Second, we try those keys that we can use without
2161    ///         // prompting for a password.
2162    ///         for pkesk in pkesks {
2163    ///             if let Some((cert, key)) = lookup_key(pkesk.recipient()) {
2164    ///                 if ! key.secret().is_encrypted() {
2165    ///                     let mut keypair = key.clone().into_keypair()?;
2166    ///                     if pkesk.decrypt(&mut keypair, sym_algo)
2167    ///                         .map(|(algo, sk)| decrypt(algo, &sk))
2168    ///                         .unwrap_or(false)
2169    ///                     {
2170    ///                         return Ok(Some(cert));
2171    ///                     }
2172    ///                 }
2173    ///             }
2174    ///         }
2175    ///
2176    ///         // Third, we try to decrypt PKESK packets with
2177    ///         // wildcard recipients using those keys that we can
2178    ///         // use without prompting for a password.
2179    ///         for pkesk in pkesks.iter().filter(
2180    ///             |p| p.recipient().is_none())
2181    ///         {
2182    ///             for (cert, key) in all_keys() {
2183    ///                 if ! key.secret().is_encrypted() {
2184    ///                     let mut keypair = key.clone().into_keypair()?;
2185    ///                     if pkesk.decrypt(&mut keypair, sym_algo)
2186    ///                         .map(|(algo, sk)| decrypt(algo, &sk))
2187    ///                         .unwrap_or(false)
2188    ///                     {
2189    ///                         return Ok(Some(cert));
2190    ///                     }
2191    ///                 }
2192    ///             }
2193    ///         }
2194    ///
2195    ///         // Fourth, we try to decrypt all PKESK packets that we
2196    ///         // need encrypted keys for.
2197    ///         // [...]
2198    ///
2199    ///         // Fifth, we try to decrypt all PKESK packets with
2200    ///         // wildcard recipients using encrypted keys.
2201    ///         // [...]
2202    ///
2203    ///         // At this point, we have exhausted our options at
2204    ///         // decrypting the PKESK packets.
2205    ///         if skesks.is_empty() {
2206    ///             return
2207    ///                 Err(anyhow::anyhow!("No key to decrypt message"));
2208    ///         }
2209    ///
2210    ///         // Finally, try to decrypt using the SKESKs.
2211    ///         loop {
2212    ///             let password = // Prompt for a password.
2213    /// #               "".into();
2214    ///
2215    ///             for skesk in skesks {
2216    ///                 if skesk.decrypt(&password)
2217    ///                     .map(|(algo, sk)| decrypt(algo, &sk))
2218    ///                     .unwrap_or(false)
2219    ///                 {
2220    ///                     return Ok(None);
2221    ///                 }
2222    ///             }
2223    ///
2224    ///             eprintln!("Bad password.");
2225    ///         }
2226    ///     }
2227    /// }
2228    /// ```
2229    fn decrypt(&mut self, pkesks: &[PKESK], skesks: &[SKESK],
2230               sym_algo: Option<SymmetricAlgorithm>,
2231               decrypt: &mut dyn FnMut(Option<SymmetricAlgorithm>, &SessionKey) -> bool)
2232               -> Result<Option<Cert>>;
2233}
2234
2235impl<'a, H: VerificationHelper + DecryptionHelper> Decryptor<'a, H> {
2236    /// Returns a reference to the helper.
2237    pub fn helper_ref(&self) -> &H {
2238        &self.helper
2239    }
2240
2241    /// Returns a mutable reference to the helper.
2242    pub fn helper_mut(&mut self) -> &mut H {
2243        &mut self.helper
2244    }
2245
2246    /// Recovers the helper.
2247    pub fn into_helper(self) -> H {
2248        self.helper
2249    }
2250
2251    /// Returns true if the whole message has been processed and
2252    /// authenticated.
2253    ///
2254    /// If the function returns `true`, the whole message has been
2255    /// processed, the signatures are verified, and the message
2256    /// structure has been passed to [`VerificationHelper::check`].
2257    /// Data read from this `Verifier` using [`io::Read`] has been
2258    /// authenticated.
2259    ///
2260    ///   [`io::Read`]: std::io::Read
2261    ///
2262    /// If the function returns `false`, the message did not fit into
2263    /// the internal buffer, and therefore data read from this
2264    /// `Verifier` using [`io::Read`] has **not yet been
2265    /// authenticated**.  It is important to treat this data as
2266    /// attacker controlled and not use it until it has been
2267    /// authenticated.
2268    ///
2269    /// # Examples
2270    ///
2271    /// This example demonstrates how to verify a message in a
2272    /// streaming fashion, writing the data to a temporary file and
2273    /// only commit the result once the data is authenticated.
2274    ///
2275    /// ```
2276    /// # fn main() -> sequoia_openpgp::Result<()> {
2277    /// use std::io::{Read, Seek, SeekFrom};
2278    /// use sequoia_openpgp as openpgp;
2279    /// use openpgp::{KeyHandle, Cert, Result};
2280    /// use openpgp::parse::{Parse, stream::*};
2281    /// use openpgp::policy::StandardPolicy;
2282    /// #
2283    /// # // Mock of `tempfile::tempfile`.
2284    /// # mod tempfile {
2285    /// #     pub fn tempfile() -> sequoia_openpgp::Result<std::fs::File> {
2286    /// #         unimplemented!()
2287    /// #     }
2288    /// # }
2289    ///
2290    /// let p = &StandardPolicy::new();
2291    ///
2292    /// // This fetches keys and computes the validity of the verification.
2293    /// struct Helper {}
2294    /// impl VerificationHelper for Helper {
2295    ///     // ...
2296    /// #   fn get_certs(&mut self, ids: &[KeyHandle]) -> Result<Vec<Cert>> {
2297    /// #       Ok(Vec::new())
2298    /// #   }
2299    /// #   fn check(&mut self, _: MessageStructure) -> Result<()> {
2300    /// #       Ok(())
2301    /// #   }
2302    /// }
2303    ///
2304    /// let mut source =
2305    ///    // ...
2306    /// #  std::io::Cursor::new(&b"-----BEGIN PGP MESSAGE-----
2307    /// #
2308    /// #    xA0DAAoW+zdR8Vh9rvEByxJiAAAAAABIZWxsbyBXb3JsZCHCdQQAFgoABgWCXrLl
2309    /// #    AQAhCRD7N1HxWH2u8RYhBDnRAKtn1b2MBAECBfs3UfFYfa7xRUsBAJaxkU/RCstf
2310    /// #    UD7TM30IorO1Mb9cDa/hPRxyzipulT55AQDN1m9LMqi9yJDjHNHwYYVwxDcg+pLY
2311    /// #    YmAFv/UfO0vYBw==
2312    /// #    =+l94
2313    /// #    -----END PGP MESSAGE-----
2314    /// #    "[..]);
2315    ///
2316    /// fn consume(r: &mut dyn Read) -> Result<()> {
2317    ///    // ...
2318    /// #   let _ = r; Ok(())
2319    /// }
2320    ///
2321    /// let h = Helper {};
2322    /// let mut v = VerifierBuilder::from_reader(&mut source)?
2323    ///     .with_policy(p, None, h)?;
2324    ///
2325    /// if v.message_processed() {
2326    ///     // The data has been authenticated.
2327    ///     consume(&mut v)?;
2328    /// } else {
2329    ///     let mut tmp = tempfile::tempfile()?;
2330    ///     std::io::copy(&mut v, &mut tmp)?;
2331    ///
2332    ///     // If the copy succeeds, the message has been fully
2333    ///     // processed and the data has been authenticated.
2334    ///     assert!(v.message_processed());
2335    ///
2336    ///     // Rewind and consume.
2337    ///     tmp.seek(SeekFrom::Start(0))?;
2338    ///     consume(&mut tmp)?;
2339    /// }
2340    /// # Ok(()) }
2341    /// ```
2342    pub fn message_processed(&self) -> bool {
2343        // oppr is only None after we've processed the packet sequence.
2344        self.oppr.is_none()
2345    }
2346
2347    /// Creates the `Decryptor`, and buffers the data up to `buffer_size`.
2348    fn from_cookie_reader<T>(
2349        policy: &'a dyn Policy,
2350        bio: Box<dyn BufferedReader<Cookie> + 'a>,
2351        helper: H, time: T,
2352        mode: Mode,
2353        buffer_size: usize,
2354        mapping: bool,
2355        csf_transformation: bool,
2356    )
2357        -> Result<Decryptor<'a, H>>
2358        where T: Into<Option<time::SystemTime>>
2359    {
2360        tracer!(TRACE, "Decryptor::from_cookie_reader", TRACE_INDENT);
2361
2362        let time = time.into();
2363        let tolerance = time
2364            .map(|_| time::Duration::new(0, 0))
2365            .unwrap_or(
2366                crate::packet::signature::subpacket::CLOCK_SKEW_TOLERANCE);
2367        let time = time.unwrap_or_else(crate::now);
2368
2369        let mut ppr = PacketParserBuilder::from_cookie_reader(bio)?
2370            .map(mapping)
2371            .process_csf_message(csf_transformation)
2372            .build()?;
2373
2374        let mut v = Decryptor {
2375            helper,
2376            issuers: Vec::new(),
2377            certs: Vec::new(),
2378            oppr: None,
2379            identity: None,
2380            structure: IMessageStructure::new(),
2381            // Do not accept unreasonably small buffer sizes.
2382            buffer_size: buffer_size.max(1024),
2383            reserve: None,
2384            cursor: 0,
2385            mode,
2386            time,
2387            clock_skew_tolerance: tolerance,
2388            policy,
2389            processing_csf_message: None, // We don't know yet.
2390        };
2391
2392        let mut pkesks: Vec<packet::PKESK> = Vec::new();
2393        let mut skesks: Vec<packet::SKESK> = Vec::new();
2394
2395        while let PacketParserResult::Some(mut pp) = ppr {
2396            match &pp.packet {
2397                Packet::PKESK(p) =>
2398                    t!("Found a {:?}v{} at depth {}",
2399                       pp.packet.tag(), p.version(),
2400                       pp.recursion_depth()),
2401                Packet::SKESK(p) =>
2402                    t!("Found a {:?}v{} at depth {}",
2403                       pp.packet.tag(), p.version(),
2404                       pp.recursion_depth()),
2405                Packet::SEIP(p) =>
2406                    t!("Found a {:?}v{} at depth {}",
2407                       pp.packet.tag(), p.version(),
2408                       pp.recursion_depth()),
2409                _ =>
2410                    t!("Found a {:?} at depth {}", pp.packet.tag(),
2411                       pp.recursion_depth()),
2412            }
2413
2414            // Check whether we are actually processing a cleartext
2415            // signature framework message.
2416            if v.processing_csf_message.is_none() {
2417                v.processing_csf_message = Some(pp.processing_csf_message());
2418            }
2419
2420            v.policy.packet(&pp.packet)?;
2421            v.helper.inspect(&pp)?;
2422
2423            // When verifying detached signatures, we parse only the
2424            // signatures here, which on their own are not a valid
2425            // message.
2426            if v.mode == Mode::VerifyDetached {
2427                if pp.packet.tag() != packet::Tag::Signature
2428                    && pp.packet.tag() != packet::Tag::Marker
2429                {
2430                    return Err(Error::MalformedMessage(
2431                        format!("Expected signature, got {}", pp.packet.tag()))
2432                               .into());
2433                }
2434            } else if let Err(err) = pp.possible_message() {
2435                if v.processing_csf_message.expect("set by now") {
2436                    // Our CSF transformation yields just one OPS
2437                    // packet per encountered 'Hash' algorithm header,
2438                    // and it cannot know how many signatures are in
2439                    // fact following.  Therefore, the message will
2440                    // not be well-formed according to the grammar.
2441                    // But, since we created the message structure
2442                    // during the transformation, we know it is good,
2443                    // even if it is a little out of spec.
2444                } else {
2445                    t!("Malformed message: {}", err);
2446                    return Err(err.context("Malformed OpenPGP message"));
2447                }
2448            }
2449
2450            let sym_algo_hint = match &pp.packet {
2451                Packet::SEIP(SEIP::V2(seip)) => Some(seip.symmetric_algo()),
2452                _ => None,
2453            };
2454
2455            match pp.packet {
2456                Packet::CompressedData(ref p) =>
2457                    v.structure.new_compression_layer(p.algo()),
2458                Packet::SEIP(ref seip) if v.mode == Mode::Decrypt => {
2459                    t!("Found the encryption container");
2460
2461                    // Bail early (and provide a useful error message)
2462                    // if we can't decrypt the SEIP packet.
2463                    if let SEIP::V2(seipv2) = seip {
2464                        if ! seipv2.symmetric_algo().is_supported() {
2465                            return Err(Error::UnsupportedSymmetricAlgorithm(
2466                                seipv2.symmetric_algo()).into());
2467                        }
2468                        if ! seipv2.aead().is_supported() {
2469                            return Err(Error::UnsupportedAEADAlgorithm(
2470                                seipv2.aead()).into());
2471                        }
2472                    }
2473
2474                    // Get the symmetric algorithm from the decryption
2475                    // proxy function.  This is necessary because we
2476                    // cannot get the algorithm from the SEIP packet.
2477                    let mut sym_algo = None;
2478                    {
2479                        let mut decryption_proxy = |algo, secret: &SessionKey| {
2480                            // Take the algo from the SEIPDv2 packet over
2481                            // the dummy one from the SKESK6 packet.
2482                            let algo = sym_algo_hint.or(algo);
2483                            let result = pp.decrypt(algo, secret);
2484                            t!("pp.decrypt({:?}, {:?}) => {:?}",
2485                               algo, secret, result);
2486                            if let Ok(_) = result {
2487                                sym_algo = Some(algo);
2488                                true
2489                            } else {
2490                                false
2491                            }
2492                        };
2493
2494                        v.identity =
2495                            v.helper.decrypt(&pkesks[..], &skesks[..],
2496                                             sym_algo_hint,
2497                                             &mut decryption_proxy)?
2498                            .map(|cert| cert.fingerprint());
2499                    }
2500                    if ! pp.processed() {
2501                        return Err(
2502                            Error::MissingSessionKey(
2503                                "No session key decrypted".into()).into());
2504                    }
2505
2506                    let sym_algo = if let Some(Some(a)) = sym_algo {
2507                        a
2508                    } else {
2509                        return Err(Error::InvalidOperation(
2510                            "No symmetric algorithm known".into()).into());
2511                    };
2512
2513                    v.policy.symmetric_algorithm(sym_algo)?;
2514                    if let Packet::SEIP(SEIP::V2(p)) = &pp.packet {
2515                        v.policy.aead_algorithm(p.aead())?;
2516                    }
2517
2518                    v.structure.new_encryption_layer(
2519                        pp.recursion_depth(),
2520                        pp.packet.tag() == packet::Tag::SEIP
2521                            && pp.packet.version() == Some(1),
2522                        sym_algo,
2523                        if let Packet::SEIP(SEIP::V2(p)) = &pp.packet {
2524                            Some(p.aead())
2525                        } else {
2526                            None
2527                        });
2528                },
2529                Packet::OnePassSig(ref ops) => {
2530                    v.structure.push_ops(ops);
2531                    v.push_issuer(ops.issuer().clone());
2532                },
2533                Packet::Literal(_) => {
2534                    v.structure.insert_missing_signature_group();
2535                    v.oppr = Some(PacketParserResult::Some(pp));
2536                    v.finish_maybe()?;
2537
2538                    return Ok(v);
2539                },
2540                #[allow(deprecated)]
2541                Packet::MDC(ref mdc) => if ! mdc.valid() {
2542                    return Err(Error::ManipulatedMessage.into());
2543                },
2544                _ => (),
2545            }
2546
2547            let (p, ppr_tmp) = pp.recurse()?;
2548            match p {
2549                Packet::PKESK(pkesk) => pkesks.push(pkesk),
2550                Packet::SKESK(skesk) => skesks.push(skesk),
2551                Packet::Signature(sig) => {
2552                    // The following structure is allowed:
2553                    //
2554                    //   SIG LITERAL
2555                    //
2556                    // In this case, we get the issuer from the
2557                    // signature itself.
2558                    sig.get_issuers().into_iter()
2559                        .for_each(|i| v.push_issuer(i));
2560                    v.structure.push_bare_signature(Ok(sig));
2561                },
2562
2563                Packet::Unknown(u) if u.tag() == packet::Tag::Signature => {
2564                    v.structure.push_bare_signature(Err(u));
2565                },
2566
2567                _ => (),
2568            }
2569            ppr = ppr_tmp;
2570        }
2571
2572        if v.mode == Mode::VerifyDetached && !v.structure.layers.is_empty() {
2573            return Ok(v);
2574        }
2575
2576        // We can only get here if we didn't encounter a literal data
2577        // packet.
2578        Err(Error::MalformedMessage(
2579            "Malformed OpenPGP message".into()).into())
2580    }
2581
2582    /// Verifies the given data in detached verification mode.
2583    fn verify_detached<'d>(&mut self,
2584                           data: Box<dyn BufferedReader<Cookie> + 'd>)
2585                           -> Result<()>
2586    {
2587        assert_eq!(self.mode, Mode::VerifyDetached);
2588
2589        let sigs = if let IMessageLayer::SignatureGroup {
2590            sigs, .. } = &mut self.structure.layers[0] {
2591            sigs
2592        } else {
2593            unreachable!("There is exactly one signature group layer")
2594        };
2595
2596        // Compute the necessary hashes.
2597        let algos: Vec<_> = sigs.iter().filter_map(|s| {
2598            let s = s.as_ref().ok()?;
2599            let h = s.hash_algo();
2600            Some(HashingMode::for_signature(h, s))
2601        }).collect();
2602        let hashes =
2603            crate::parse::hashed_reader::hash_buffered_reader(data, &algos)?;
2604
2605        // Attach the digests.
2606        for sig in sigs.iter_mut().filter_map(|s| s.as_ref().ok()) {
2607            let need_hash =
2608                HashingMode::for_signature(sig.hash_algo(), sig);
2609            // Note: |hashes| < 10, most likely 1.
2610            for mode in hashes.iter()
2611                .filter(|m| m.map(|c| c.algo()) == need_hash)
2612            {
2613                // Clone the hash context, update it with the
2614                // signature.
2615                use crate::crypto::hash::Hash;
2616                let mut hash = mode.as_ref().clone();
2617                sig.hash(&mut hash)?;
2618
2619                // Attach digest to the signature.
2620                let mut digest = vec![0; hash.digest_size()];
2621                let _ = hash.digest(&mut digest);
2622                sig.set_computed_digest(Some(digest));
2623            }
2624        }
2625
2626        self.verify_signatures()
2627    }
2628
2629    /// Stashes the given Signature (if it is one) for later
2630    /// verification.
2631    fn push_sig(&mut self, p: Packet) -> Result<()> {
2632        match p {
2633            Packet::Signature(sig) => {
2634                sig.get_issuers().into_iter().for_each(|i| self.push_issuer(i));
2635                self.structure.push_signature(
2636                    Ok(sig), self.processing_csf_message.expect("set by now"));
2637            },
2638            Packet::Unknown(sig) if sig.tag() == packet::Tag::Signature => {
2639                self.structure.push_signature(
2640                    Err(sig), self.processing_csf_message.expect("set by now"));
2641            },
2642            _ => (),
2643        }
2644        Ok(())
2645    }
2646
2647    /// Records the issuer for the later certificate lookup.
2648    fn push_issuer<I: Into<KeyHandle>>(&mut self, issuer: I) {
2649        let issuer = issuer.into();
2650        match issuer {
2651            KeyHandle::KeyID(id) if id.is_wildcard() => {
2652                // Ignore, they are not useful for lookups.
2653            },
2654
2655            KeyHandle::KeyID(_) => {
2656                for known in self.issuers.iter() {
2657                    if known.aliases(&issuer) {
2658                        return;
2659                    }
2660                }
2661
2662                // Unknown, record.
2663                self.issuers.push(issuer);
2664            },
2665
2666            KeyHandle::Fingerprint(_) => {
2667                for known in self.issuers.iter_mut() {
2668                    if known.aliases(&issuer) {
2669                        // Replace.  We may upgrade a KeyID to a
2670                        // Fingerprint.
2671                        *known = issuer;
2672                        return;
2673                    }
2674                }
2675
2676                // Unknown, record.
2677                self.issuers.push(issuer);
2678            },
2679        }
2680    }
2681
2682    // If the amount of remaining data does not exceed the reserve,
2683    // finish processing the OpenPGP packet sequence.
2684    //
2685    // Note: once this call succeeds, you may not call it again.
2686    fn finish_maybe(&mut self) -> Result<()> {
2687        tracer!(TRACE, "Decryptor::finish_maybe", TRACE_INDENT);
2688        if let Some(PacketParserResult::Some(mut pp)) = self.oppr.take() {
2689            // Check if we hit EOF.
2690            let data_len = pp.data(self.buffer_size + 1)?.len();
2691            if data_len - self.cursor <= self.buffer_size {
2692                // Stash the reserve.
2693                t!("Hit eof with {} bytes of the current buffer consumed.",
2694                   self.cursor);
2695                pp.consume(self.cursor);
2696                self.cursor = 0;
2697                self.reserve = Some(Protected::from(pp.steal_eof()?));
2698
2699                // Process the rest of the packets.
2700                let mut ppr = PacketParserResult::Some(pp);
2701                let mut first = true;
2702                while let PacketParserResult::Some(pp) = ppr {
2703                    t!("Found a {:?} at depth {}", pp.packet.tag(),
2704                       pp.recursion_depth());
2705
2706                    // The literal data packet was already inspected.
2707                    if first {
2708                        assert_eq!(pp.packet.tag(), packet::Tag::Literal);
2709                        first = false;
2710                    } else {
2711                        self.helper.inspect(&pp)?;
2712                    }
2713
2714                    let possible_message = pp.possible_message();
2715
2716                    // If we are ascending, and the packet was the
2717                    // last packet in a SEIP container, we need to be
2718                    // extra careful with reporting errors to avoid
2719                    // creating a decryption oracle.
2720
2721                    let last_recursion_depth = pp.recursion_depth();
2722                    let (p, ppr_tmp) = match pp.recurse() {
2723                        Ok(v) => v,
2724                        Err(e) => {
2725                            // Assuming we just tried to ascend,
2726                            // should there have been a MDC packet?
2727                            // If so, this may be an attack.
2728                            if self.structure.expect_mdc_at(
2729                                last_recursion_depth - 1)
2730                            {
2731                                return Err(Error::ManipulatedMessage.into());
2732                            } else {
2733                                return Err(e);
2734                            }
2735                        },
2736                    };
2737                    ppr = ppr_tmp;
2738                    let recursion_depth = ppr.as_ref()
2739                        .map(|pp| pp.recursion_depth()).unwrap_or(0);
2740
2741                    // Did we just ascend?
2742                    if recursion_depth + 1 == last_recursion_depth
2743                        && self.structure.expect_mdc_at(recursion_depth)
2744                    {
2745                        match &p {
2746                            #[allow(deprecated)]
2747                            Packet::MDC(mdc) if mdc.valid() =>
2748                                (), // Good.
2749                            _ =>    // Bad.
2750                                return Err(Error::ManipulatedMessage.into()),
2751                        }
2752
2753                        if possible_message.is_err() {
2754                            return Err(Error::ManipulatedMessage.into());
2755                        }
2756                    }
2757
2758                    if let Err(_err) = possible_message {
2759                        if self.processing_csf_message.expect("set by now") {
2760                            // CSF transformation creates slightly out
2761                            // of spec message structure.  See above
2762                            // for longer explanation.
2763                        } else {
2764                            return Err(Error::ManipulatedMessage.into());
2765                        }
2766                    }
2767
2768                    self.push_sig(p)?;
2769                }
2770
2771                // If we finished parsing, validate the message structure.
2772                if let PacketParserResult::EOF(eof) = ppr {
2773                    // If we parse a signed message synthesized from a
2774                    // cleartext signature framework message, we don't
2775                    // quite get the structure right, so relax the
2776                    // requirement in this case.
2777                    if ! self.processing_csf_message.expect("set by now") {
2778                        eof.is_message()?;
2779                    }
2780                }
2781
2782                self.verify_signatures()
2783            } else {
2784                t!("Didn't hit EOF.");
2785                self.oppr = Some(PacketParserResult::Some(pp));
2786                Ok(())
2787            }
2788        } else {
2789            panic!("No ppr.");
2790        }
2791    }
2792
2793    /// Verifies the signatures.
2794    fn verify_signatures(&mut self) -> Result<()> {
2795        tracer!(TRACE, "Decryptor::verify_signatures", TRACE_INDENT);
2796        t!("called");
2797
2798        self.certs = self.helper.get_certs(&self.issuers)?;
2799        t!("VerificationHelper::get_certs produced {} certs", self.certs.len());
2800
2801        let mut results = MessageStructure::new(
2802            self.processing_csf_message.unwrap_or(false));
2803        for layer in self.structure.layers.iter_mut() {
2804            match layer {
2805                IMessageLayer::Compression { algo } =>
2806                    results.new_compression_layer(*algo),
2807                IMessageLayer::Encryption { sym_algo, aead_algo, .. } =>
2808                    results.new_encryption_layer(*sym_algo, *aead_algo),
2809                IMessageLayer::SignatureGroup { sigs, .. } => {
2810                    results.new_signature_group();
2811                    'sigs: for sig in sigs.iter_mut() {
2812                        let sig = match sig {
2813                            Ok(s) => s,
2814                            Err(u) => {
2815                                // Unparsablee signature.
2816                                t!("Unparsablee signature: {}", u.error());
2817                                results.push_verification_result(
2818                                    Err(VerificationError::UnknownSignature {
2819                                        sig: u,
2820                                    }));
2821                                continue;
2822                            }
2823                        };
2824
2825                        let sigid = *sig.digest_prefix();
2826
2827                        let sig_time = if let Some(t) = sig.signature_creation_time() {
2828                            t
2829                        } else {
2830                            // Invalid signature.
2831                            results.push_verification_result(
2832                                Err(VerificationError::MalformedSignature {
2833                                    sig,
2834                                    error: Error::MalformedPacket(
2835                                        "missing a Signature Creation Time \
2836                                         subpacket"
2837                                            .into()).into(),
2838                                }));
2839                            t!("{:02X}{:02X}: Missing a signature creation time subpacket",
2840                               sigid[0], sigid[1]);
2841                            continue;
2842                        };
2843
2844                        let mut err = VerificationErrorInternal::MissingKey {};
2845
2846                        let issuers = sig.get_issuers();
2847                        // Note: If there are no issuers, the only way
2848                        // to verify the signature is to try every key
2849                        // that could possibly have created the
2850                        // signature.  While this may be feasible if
2851                        // the set of potential signing keys is small,
2852                        // the use case of hiding the signer's
2853                        // identity seems better solved using
2854                        // encryption.  Furthermore, no other OpenPGP
2855                        // implementation seems to support this kind
2856                        // of wildcard signatures.
2857                        let no_issuers = issuers.is_empty();
2858
2859                        for ka in self.certs.iter().flat_map(
2860                            |c| c.keys().key_handles(issuers.clone()))
2861                        {
2862                            if no_issuers {
2863                                // Slightly awkward control flow
2864                                // change.  Below this loop, we still
2865                                // have to add this signature to the
2866                                // results with the default error,
2867                                // `VerificationError::MissingKey`.
2868                                break;
2869                            }
2870
2871                            let cert = ka.cert();
2872                            let fingerprint = ka.key().fingerprint();
2873                            let ka = match ka.with_policy(self.policy, sig_time) {
2874                                Err(policy_err) => {
2875                                    t!("{:02X}{:02X}: key {} rejected by policy: {}",
2876                                       sigid[0], sigid[1], fingerprint, policy_err);
2877                                    err = VerificationErrorInternal::UnboundKey {
2878                                        cert,
2879                                        error: policy_err,
2880                                    };
2881                                    continue;
2882                                }
2883                                Ok(ka) => {
2884                                    t!("{:02X}{:02X}: key {} accepted by policy",
2885                                       sigid[0], sigid[1], fingerprint);
2886                                    ka
2887                                }
2888                            };
2889
2890                            err = if let Err(error) = ka.valid_cert().alive() {
2891                                t!("{:02X}{:02X}: cert {} not alive: {}",
2892                                   sigid[0], sigid[1], ka.cert().fingerprint(), error);
2893                                VerificationErrorInternal::BadKey {
2894                                    ka,
2895                                    error,
2896                                }
2897                            } else if let Err(error) = ka.alive() {
2898                                t!("{:02X}{:02X}: key {} not alive: {}",
2899                                   sigid[0], sigid[1], ka.key().fingerprint(), error);
2900                                VerificationErrorInternal::BadKey {
2901                                    ka,
2902                                    error,
2903                                }
2904                            } else if let
2905                                RevocationStatus::Revoked(rev) = ka.valid_cert().revocation_status()
2906                            {
2907                                t!("{:02X}{:02X}: cert {} revoked: {:?}",
2908                                   sigid[0], sigid[1], ka.cert().fingerprint(), rev);
2909                                VerificationErrorInternal::BadKey {
2910                                    ka,
2911                                    error: Error::InvalidKey(
2912                                        "certificate is revoked".into())
2913                                        .into(),
2914                                }
2915                            } else if let
2916                                RevocationStatus::Revoked(rev) = ka.revocation_status()
2917                            {
2918                                t!("{:02X}{:02X}: key {} revoked: {:?}",
2919                                   sigid[0], sigid[1], ka.key().fingerprint(), rev);
2920                                VerificationErrorInternal::BadKey {
2921                                    ka,
2922                                    error: Error::InvalidKey(
2923                                        "signing key is revoked".into())
2924                                        .into(),
2925                                }
2926                            } else if ! ka.for_signing() {
2927                                t!("{:02X}{:02X}: key {} not signing capable",
2928                                   sigid[0], sigid[1], ka.key().fingerprint());
2929                                VerificationErrorInternal::BadKey {
2930                                    ka,
2931                                    error: Error::InvalidKey(
2932                                        "key is not signing capable".into())
2933                                        .into(),
2934                                }
2935                            } else if let Err(error) = sig.signature_alive(
2936                                self.time, self.clock_skew_tolerance)
2937                            {
2938                                t!("{:02X}{:02X}: Signature not alive: {}",
2939                                   sigid[0], sigid[1], error);
2940                                VerificationErrorInternal::BadSignature {
2941                                    ka,
2942                                    error,
2943                                }
2944                            } else if self.identity.as_ref().map(|identity| {
2945                                let (have_one, contains_identity) =
2946                                    sig.intended_recipients()
2947                                        .fold((false, false),
2948                                              |(_, contains_one), ir| {
2949                                                  (
2950                                                      true,
2951                                                      contains_one || identity == ir
2952                                                  )
2953                                              });
2954                                have_one && ! contains_identity
2955                            }).unwrap_or(false) {
2956                                // The signature contains intended
2957                                // recipients, but we are not one.
2958                                // Treat the signature as bad.
2959                                t!("{:02X}{:02X}: not an intended recipient",
2960                                   sigid[0], sigid[1]);
2961                                VerificationErrorInternal::BadSignature {
2962                                    ka,
2963                                    error: Error::BadSignature(
2964                                        "Not an intended recipient".into())
2965                                        .into(),
2966                                }
2967                            } else {
2968                                match sig.verify_document(ka.key()) {
2969                                    Ok(()) => {
2970                                        if let Err(error)
2971                                            = self.policy.signature(
2972                                                sig, Default::default())
2973                                        {
2974                                            t!("{:02X}{:02X}: signature rejected by policy: {}",
2975                                               sigid[0], sigid[1], error);
2976                                            VerificationErrorInternal::BadSignature {
2977                                                ka,
2978                                                error,
2979                                            }
2980                                        } else {
2981                                            t!("{:02X}{:02X}: good checksum using {}",
2982                                               sigid[0], sigid[1], ka.key().fingerprint());
2983                                            results.push_verification_result(
2984                                                Ok(GoodChecksum {
2985                                                    sig,
2986                                                    ka,
2987                                                }));
2988                                            // Continue to the next sig.
2989                                            continue 'sigs;
2990                                        }
2991                                    }
2992                                    Err(error) => {
2993                                        t!("{:02X}{:02X} using {}: error: {}",
2994                                           sigid[0], sigid[1], ka.key().fingerprint(), error);
2995                                        VerificationErrorInternal::BadSignature {
2996                                            ka,
2997                                            error,
2998                                        }
2999                                    }
3000                                }
3001                            }
3002                        }
3003
3004                        let err = err.attach_sig(sig);
3005                        t!("{:02X}{:02X}: returning: {:?}", sigid[0], sigid[1], err);
3006                        results.push_verification_result(Err(err));
3007                    }
3008                }
3009            }
3010        }
3011
3012        let r = self.helper.check(results);
3013        t!("-> {:?}", r);
3014        r
3015    }
3016
3017    /// Like `io::Read::read()`, but returns our `Result`.
3018    fn read_helper(&mut self, buf: &mut [u8]) -> Result<usize> {
3019        tracer!(TRACE, "Decryptor::read_helper", TRACE_INDENT);
3020        t!("read(buf of {} bytes)", buf.len());
3021
3022        if buf.is_empty() {
3023            return Ok(0);
3024        }
3025
3026        if let Some(ref mut reserve) = self.reserve {
3027            // The message has been verified.  We can now drain the
3028            // reserve.
3029            t!("Message verified, draining reserve.");
3030            assert!(self.oppr.is_none());
3031            assert!(self.cursor <= reserve.len());
3032            let n = cmp::min(buf.len(), reserve.len() - self.cursor);
3033            buf[..n]
3034                .copy_from_slice(&reserve[self.cursor..n + self.cursor]);
3035            self.cursor += n;
3036            return Ok(n);
3037        }
3038
3039        // Read the data from the Literal data packet.
3040        if let Some(PacketParserResult::Some(mut pp)) = self.oppr.take() {
3041            // Be careful to not read from the reserve.
3042            if self.cursor >= self.buffer_size {
3043                // Consume the active part of the buffer.
3044                t!("Consuming first part of the buffer.");
3045                pp.consume(self.buffer_size);
3046                self.cursor -= self.buffer_size;
3047            }
3048
3049            // We request two times what our buffer size is, the first
3050            // part is the one we give out, the second part is the one
3051            // we hold back.
3052            let data_len = pp.data(2 * self.buffer_size)?.len();
3053            t!("Read {} bytes.", data_len);
3054            if data_len - self.cursor <= self.buffer_size {
3055                self.oppr = Some(PacketParserResult::Some(pp));
3056                self.finish_maybe()?;
3057                self.read_helper(buf)
3058            } else {
3059                let data = pp.data(2 * self.buffer_size - self.cursor)?;
3060                assert_eq!(data.len(), data_len);
3061
3062                let n =
3063                    buf.len().min(data_len - self.buffer_size - self.cursor);
3064                buf[..n].copy_from_slice(&data[self.cursor..self.cursor + n]);
3065                self.cursor += n;
3066                self.oppr = Some(PacketParserResult::Some(pp));
3067                t!("Copied {} bytes from buffer, cursor is {}.", n, self.cursor);
3068                Ok(n)
3069            }
3070        } else {
3071            panic!("No ppr.");
3072        }
3073    }
3074}
3075
3076impl<'a, H: VerificationHelper + DecryptionHelper> io::Read for Decryptor<'a, H>
3077{
3078    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
3079        match self.read_helper(buf) {
3080            Ok(n) => Ok(n),
3081            Err(e) => match e.downcast::<io::Error>() {
3082                // An io::Error.  Pass as-is.
3083                Ok(e) => Err(e),
3084                // A failure.  Wrap it.
3085                Err(e) => Err(io::Error::new(io::ErrorKind::Other, e)),
3086            },
3087        }
3088    }
3089}
3090
3091#[cfg(test)]
3092pub(crate) mod test {
3093    use std::io::Read;
3094    use super::*;
3095    use std::convert::TryFrom;
3096    use crate::parse::Parse;
3097    use crate::policy::{NullPolicy as NP, StandardPolicy as P};
3098    use crate::serialize::Serialize;
3099    use crate::{
3100        crypto::Password,
3101    };
3102
3103    /// Verification helper for the tests.
3104    #[derive(Clone)]
3105    pub struct VHelper {
3106        good: usize,
3107        unknown: usize,
3108        bad: usize,
3109        error: usize,
3110        expect_csf_message: bool,
3111        certs: Vec<Cert>,
3112        keys: Vec<Cert>,
3113        passwords: Vec<Password>,
3114        for_decryption: bool,
3115        error_out: bool,
3116        pub packets: Vec<Packet>,
3117    }
3118
3119    impl std::fmt::Debug for VHelper {
3120        fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
3121            f.debug_struct("VHelper")
3122                .field("good", &self.good)
3123                .field("unknown", &self.unknown)
3124                .field("bad", &self.bad)
3125                .field("error", &self.error)
3126                .field("error_out", &self.error_out)
3127                .field("expect_csf_message", &self.expect_csf_message)
3128                .finish()
3129        }
3130    }
3131
3132    impl VHelper {
3133        /// Creates a new verification helper.
3134        pub fn new(good: usize, unknown: usize, bad: usize, error: usize,
3135                   expect_csf_message: bool, certs: Vec<Cert>)
3136                   -> Self {
3137            VHelper {
3138                good,
3139                unknown,
3140                bad,
3141                error,
3142                expect_csf_message,
3143                certs,
3144                keys: Default::default(),
3145                passwords: Default::default(),
3146                for_decryption: false,
3147                error_out: true,
3148                packets: Default::default(),
3149            }
3150        }
3151
3152        /// Creates a new decryption helper.
3153        pub fn for_decryption(good: usize, unknown: usize, bad: usize,
3154                              error: usize,
3155                              certs: Vec<Cert>,
3156                              keys: Vec<Cert>,
3157                              passwords: Vec<Password>)
3158                              -> Self {
3159            VHelper {
3160                good,
3161                unknown,
3162                bad,
3163                error,
3164                expect_csf_message: false,
3165                certs,
3166                keys,
3167                passwords,
3168                for_decryption: true,
3169                error_out: true,
3170                packets: Default::default(),
3171            }
3172        }
3173
3174        /// Compares the stats.
3175        pub fn assert_stats_eq(&self, other: &Self) {
3176            assert_eq!(self.good, other.good);
3177            assert_eq!(self.unknown, other.unknown);
3178            assert_eq!(self.bad, other.bad);
3179            assert_eq!(self.error, other.error);
3180        }
3181    }
3182
3183    impl VerificationHelper for VHelper {
3184        fn inspect(&mut self, pp: &PacketParser<'_>) -> Result<()> {
3185            self.packets.push(pp.packet.clone());
3186            Ok(())
3187        }
3188
3189        fn get_certs(&mut self, _ids: &[crate::KeyHandle]) -> Result<Vec<Cert>> {
3190            Ok(self.certs.clone())
3191        }
3192
3193        fn check(&mut self, structure: MessageStructure) -> Result<()> {
3194            use self::VerificationError::*;
3195            for layer in structure.iter() {
3196                match layer {
3197                    MessageLayer::SignatureGroup { ref results } =>
3198                        for result in results {
3199                            match result {
3200                                Ok(_) => self.good += 1,
3201                                Err(MissingKey { .. }) => self.unknown += 1,
3202                                Err(UnboundKey { .. }) => self.unknown += 1,
3203                                Err(MalformedSignature { .. }) => self.bad += 1,
3204                                Err(UnknownSignature { .. }) => self.bad += 1,
3205                                Err(BadKey { .. }) => self.bad += 1,
3206                                Err(BadSignature { error, .. }) => {
3207                                    eprintln!("error: {}", error);
3208                                    self.bad += 1;
3209                                },
3210                            }
3211                        }
3212                    MessageLayer::Compression { .. } => (),
3213                    MessageLayer::Encryption { .. } => (),
3214                }
3215            }
3216
3217            if self.expect_csf_message != structure.processed_csf_message() {
3218                if self.expect_csf_message {
3219                    return Err(anyhow::anyhow!(
3220                        "Expected CSF, but didn't get it"));
3221                } else {
3222                    return Err(anyhow::anyhow!(
3223                        "Didn't expect CSF, but got it"));
3224                }
3225            }
3226
3227            if ! self.error_out || (self.good > 0 && self.bad == 0)
3228                || (self.for_decryption && self.certs.is_empty())
3229            {
3230                Ok(())
3231            } else {
3232                Err(anyhow::anyhow!("Verification failed: {:?}", self))
3233            }
3234        }
3235    }
3236
3237    impl DecryptionHelper for VHelper {
3238        fn decrypt(&mut self, pkesks: &[PKESK], skesks: &[SKESK],
3239                   sym_algo: Option<SymmetricAlgorithm>,
3240                   decrypt: &mut dyn FnMut(Option<SymmetricAlgorithm>, &SessionKey) -> bool)
3241                   -> Result<Option<Cert>>
3242        {
3243            tracer!(TRACE, "VHelper::decrypt", TRACE_INDENT);
3244
3245            let p = P::new();
3246            if ! self.for_decryption {
3247                unreachable!("Shouldn't be called for verifications");
3248            }
3249
3250            t!("Trying SKESKS: {:?}", skesks);
3251            for (i, skesk) in skesks.iter().enumerate() {
3252                for p in &self.passwords {
3253                    let r = skesk.decrypt(p);
3254                    t!("decrypting SKESK {}: {:?}", i, r);
3255                    if let Ok((algo, sk)) = r {
3256                        if decrypt(algo, &sk) {
3257                            t!("successfully decrypted encryption container");
3258                            return Ok(None);
3259                        }
3260                    }
3261                }
3262            }
3263
3264            t!("Trying PKESKS: {:?}", pkesks);
3265            for pkesk in pkesks.iter().filter(|p| p.recipient().is_some()) {
3266                for key in &self.keys {
3267                    for subkey in key.with_policy(&p, None)?.keys().secret()
3268                        .key_handles(pkesk.recipient())
3269                    {
3270                        t!("Trying to decrypt {:?} with {:?}", pkesk, subkey);
3271                        if let Some((algo, sk)) =
3272                            subkey.key().clone().into_keypair().ok()
3273                            .and_then(|mut k| pkesk.decrypt(&mut k, sym_algo))
3274                        {
3275                            if decrypt(algo, &sk) {
3276                                t!("successfully decrypted encryption container");
3277                                return Ok(None);
3278                            }
3279                        }
3280                    }
3281                }
3282            }
3283
3284            t!("decryption of session key failed");
3285            Err(Error::MissingSessionKey("Decryption failed".into()).into())
3286        }
3287    }
3288
3289    #[test]
3290    fn verifier() -> Result<()> {
3291        let p = P::new();
3292
3293        let certs = [
3294            "keys/neal.pgp",
3295            "keys/testy-new.pgp",
3296            "keys/emmelie-dorothea-dina-samantha-awina-ed25519.pgp",
3297            "crypto-refresh/v6-minimal-cert.key",
3298        ].iter()
3299         .map(|f| Cert::from_bytes(crate::tests::file(f)).unwrap())
3300         .collect::<Vec<_>>();
3301        let tests = &[
3302            // Signed messages.
3303            (crate::tests::message("signed-1.pgp").to_vec(),
3304             crate::tests::manifesto().to_vec(),
3305             true,
3306             Some(crate::frozen_time()),
3307             VHelper::new(1, 0, 0, 0, false, certs.clone())),
3308            // The same, but with a marker packet.
3309            ({
3310                let pp = crate::PacketPile::from_bytes(
3311                    crate::tests::message("signed-1.pgp"))?;
3312                let mut buf = Vec::new();
3313                Packet::Marker(Default::default()).serialize(&mut buf)?;
3314                pp.serialize(&mut buf)?;
3315                buf
3316            },
3317             crate::tests::manifesto().to_vec(),
3318             true,
3319             Some(crate::frozen_time()),
3320             VHelper::new(1, 0, 0, 0, false, certs.clone())),
3321            (crate::tests::message("signed-1-sha256-testy.pgp").to_vec(),
3322             crate::tests::manifesto().to_vec(),
3323             true,
3324             Some(crate::frozen_time()),
3325             VHelper::new(0, 1, 0, 0, false, certs.clone())),
3326            (crate::tests::message("signed-1-notarized-by-ed25519.pgp")
3327             .to_vec(),
3328             crate::tests::manifesto().to_vec(),
3329             true,
3330             Some(crate::frozen_time()),
3331             VHelper::new(2, 0, 0, 0, false, certs.clone())),
3332            // Signed messages using the Cleartext Signature Framework.
3333            (crate::tests::message("a-cypherpunks-manifesto.txt.cleartext.sig")
3334             .to_vec(),
3335             {
3336                 // The test vector, created by GnuPG, does not preserve
3337                 // the final newline.
3338                 //
3339                 // The transformation process trims trailing whitespace,
3340                 // and the manifesto has a trailing whitespace right at
3341                 // the end.
3342                 let mut manifesto = crate::tests::manifesto().to_vec();
3343                 assert_eq!(manifesto.pop(), Some(b'\n'));
3344                 assert_eq!(manifesto.pop(), Some(b' '));
3345                 manifesto
3346             },
3347             false,
3348             None,
3349             VHelper::new(1, 0, 0, 0, true, certs.clone())),
3350            (crate::tests::message("a-problematic-poem.txt.cleartext.sig")
3351             .to_vec(),
3352             {
3353                 // The test vector, created by GnuPG, does not preserve
3354                 // the final newline.
3355                 let mut reference =
3356                     crate::tests::message("a-problematic-poem.txt").to_vec();
3357                 assert_eq!(reference.pop(), Some(b'\n'));
3358                 reference
3359             },
3360             false,
3361             None,
3362             VHelper::new(1, 0, 0, 0, true, certs.clone())),
3363            (crate::tests::file("crypto-refresh/cleartext-signed-message.txt")
3364             .to_vec(),
3365             crate::tests::file("crypto-refresh/cleartext-signed-message.txt.plain")
3366             .to_vec(),
3367             false,
3368             None,
3369             VHelper::new(1, 0, 0, 0, true, certs.clone())),
3370            // A key as example of an invalid message.
3371            (crate::tests::key("neal.pgp").to_vec(),
3372             crate::tests::manifesto().to_vec(),
3373             true,
3374             Some(crate::frozen_time()),
3375             VHelper::new(0, 0, 0, 1, false, certs.clone())),
3376            // A signed message where the signature type is text and a
3377            // crlf straddles two chunks.
3378            (crate::tests::message("crlf-straddles-chunks.txt.sig").to_vec(),
3379             crate::tests::message("crlf-straddles-chunks.txt").to_vec(),
3380             false,
3381             None,
3382             VHelper::new(1, 0, 0, 0, false, certs.clone())),
3383            // Like crlf-straddles-chunks, but the signature includes a
3384            // notation with a '\n'.  Make sure it is not converted to
3385            // a '\r\n'.
3386            (crate::tests::message("text-signature-notation-has-lf.txt.sig").to_vec(),
3387             crate::tests::message("text-signature-notation-has-lf.txt").to_vec(),
3388             false,
3389             None,
3390             VHelper::new(1, 0, 0, 0, false, certs.clone())),
3391        ];
3392
3393        for (i, (signed, reference, test_decryptor, time, r))
3394            in tests.iter().enumerate()
3395        {
3396            eprintln!("{}...", i);
3397
3398            // Test Verifier.
3399            let h = VHelper::new(0, 0, 0, 0, r.expect_csf_message, certs.clone());
3400            let mut v =
3401                match VerifierBuilder::from_bytes(&signed)?
3402                    .with_policy(&p, *time, h) {
3403                    Ok(v) => v,
3404                    Err(e) => if r.error > 0 || r.unknown > 0 {
3405                        // Expected error.  No point in trying to read
3406                        // something.
3407                        continue;
3408                    } else {
3409                        panic!("{}: {}", i, e);
3410                    },
3411                };
3412            assert!(v.message_processed());
3413            r.assert_stats_eq(v.helper_ref());
3414
3415            if v.helper_ref().error > 0 {
3416                // Expected error.  No point in trying to read
3417                // something.
3418                continue;
3419            }
3420
3421            let mut content = Vec::new();
3422            v.read_to_end(&mut content).unwrap();
3423            assert_eq!(reference.len(), content.len());
3424            assert_eq!(&reference[..], &content[..]);
3425
3426            if ! test_decryptor {
3427                continue;
3428            }
3429
3430            // Test Decryptor.
3431            let h = VHelper::new(0, 0, 0, 0, r.expect_csf_message, certs.clone());
3432            let mut v = match DecryptorBuilder::from_bytes(&signed)?
3433                .with_policy(&p, *time, h) {
3434                    Ok(v) => v,
3435                    Err(e) => if r.error > 0 || r.unknown > 0 {
3436                        // Expected error.  No point in trying to read
3437                        // something.
3438                        continue;
3439                    } else {
3440                        panic!("{}: {}", i, e);
3441                    },
3442                };
3443            assert!(v.message_processed());
3444            r.assert_stats_eq(v.helper_ref());
3445
3446            if v.helper_ref().error > 0 {
3447                // Expected error.  No point in trying to read
3448                // something.
3449                continue;
3450            }
3451
3452            let mut content = Vec::new();
3453            v.read_to_end(&mut content).unwrap();
3454            assert_eq!(reference.len(), content.len());
3455            assert_eq!(&reference[..], &content[..]);
3456        }
3457        Ok(())
3458    }
3459
3460    #[test]
3461    fn decryptor() -> Result<()> {
3462        let p = P::new();
3463        for (key_file, message, plaintext) in &[
3464            ("messages/encrypted/rsa.sec.pgp",
3465             "messages/encrypted/rsa.msg.pgp",
3466             "Hello World!\n"),
3467            ("messages/encrypted/elg.sec.pgp",
3468             "messages/encrypted/elg.msg.pgp",
3469             "Hello World!\n"),
3470            ("messages/encrypted/cv25519.sec.pgp",
3471             "messages/encrypted/cv25519.msg.pgp",
3472             "Hello World!\n"),
3473            ("messages/encrypted/cv25519.unclamped.sec.pgp",
3474             "messages/encrypted/cv25519.unclamped.msg.pgp",
3475             "дружба"),
3476            ("messages/encrypted/nistp256.sec.pgp",
3477             "messages/encrypted/nistp256.msg.pgp",
3478             "Hello World!\n"),
3479            ("messages/encrypted/nistp384.sec.pgp",
3480             "messages/encrypted/nistp384.msg.pgp",
3481             "Hello World!\n"),
3482            ("messages/encrypted/nistp521.sec.pgp",
3483             "messages/encrypted/nistp521.msg.pgp",
3484             "Hello World!\n"),
3485            ("messages/encrypted/brainpoolP256r1.sec.pgp",
3486             "messages/encrypted/brainpoolP256r1.msg.pgp",
3487             "Hello World!\n"),
3488            ("messages/encrypted/brainpoolP384r1.sec.pgp",
3489             "messages/encrypted/brainpoolP384r1.msg.pgp",
3490             "Hello World!\n"),
3491            ("messages/encrypted/brainpoolP512r1.sec.pgp",
3492             "messages/encrypted/brainpoolP512r1.msg.pgp",
3493             "Hello World!\n"),
3494            ("messages/encrypted/secp256k1.sec.pgp",
3495             "messages/encrypted/secp256k1.msg.pgp",
3496             "Hello World!\n"),
3497            ("messages/encrypted/x448.sec.pgp",
3498             "messages/encrypted/x448.msg.pgp",
3499             "Hello World!\n"),
3500
3501            ("pqc/ietf/v6-eddsa-sample-sk.pgp",
3502             "pqc/ietf/v6-eddsa-sample-message.pgp",
3503             "Testing\n"),
3504            ("pqc/ietf/v4-eddsa-sample-sk.pgp",
3505             "pqc/ietf/v4-eddsa-sample-message-v1.pgp",
3506             "Testing\n"),
3507            ("pqc/ietf/v4-eddsa-sample-sk.pgp",
3508             "pqc/ietf/v4-eddsa-sample-message-v2.pgp",
3509             "Testing\n"),
3510            ("pqc/ietf/v6-mldsa-65-sample-sk.pgp",
3511             "pqc/ietf/v6-mldsa-65-sample-message.pgp",
3512             "Testing\n"),
3513            ("pqc/ietf/v6-mldsa-87-sample-sk.pgp",
3514             "pqc/ietf/v6-mldsa-87-sample-message.pgp",
3515             "Testing\n"),
3516            ("pqc/ietf/v6-slhdsa-128s-sample-sk.pgp",
3517             "pqc/ietf/v6-slhdsa-128s-sample-message.pgp",
3518             "Testing\n"),
3519
3520            // rpgp artifacts
3521            ("pqc/rpgp/rsop_draft-ietf-openpgp-pqc-08-v4-ed25519-mlkem768x25519_bob_sk.pgp",
3522             "pqc/rpgp/rsop_draft-ietf-openpgp-pqc-08-v4-ed25519-mlkem768x25519_message.pgp",
3523             "Hello World\n"),
3524            ("pqc/rpgp/rsop_draft-ietf-openpgp-pqc-08-v6-ed25519-mlkem768x25519_bob_sk.pgp",
3525             "pqc/rpgp/rsop_draft-ietf-openpgp-pqc-08-v6-ed25519-mlkem768x25519_message.pgp",
3526             "Hello World\n"),
3527            ("pqc/rpgp/rsop_draft-ietf-openpgp-pqc-08-v6-mldsa65ed25519-mlkem768x25519_bob_sk.pgp",
3528             "pqc/rpgp/rsop_draft-ietf-openpgp-pqc-08-v6-mldsa65ed25519-mlkem768x25519_message.pgp",
3529             "Hello World\n"),
3530            ("pqc/rpgp/rsop_draft-ietf-openpgp-pqc-08-v6-mldsa87ed448-mlkem1024x448_bob_sk.pgp",
3531             "pqc/rpgp/rsop_draft-ietf-openpgp-pqc-08-v6-mldsa87ed448-mlkem1024x448_message.pgp",
3532             "Hello World\n"),
3533            ("pqc/rpgp/rsop_draft-ietf-openpgp-pqc-08-v6-slhdsashake128f-mlkem768x25519_bob_sk.pgp",
3534             "pqc/rpgp/rsop_draft-ietf-openpgp-pqc-08-v6-slhdsashake128f-mlkem768x25519_message.pgp",
3535             "Hello World\n"),
3536            ("pqc/rpgp/rsop_draft-ietf-openpgp-pqc-08-v6-slhdsashake128s-mlkem768x25519_bob_sk.pgp",
3537             "pqc/rpgp/rsop_draft-ietf-openpgp-pqc-08-v6-slhdsashake128s-mlkem768x25519_message.pgp",
3538             "Hello World\n"),
3539            ("pqc/rpgp/rsop_draft-ietf-openpgp-pqc-08-v6-slhdsashake256s-mlkem1024x448_bob_sk.pgp",
3540             "pqc/rpgp/rsop_draft-ietf-openpgp-pqc-08-v6-slhdsashake256s-mlkem1024x448_message.pgp",
3541             "Hello World\n"),
3542
3543            // gopenpgp artifacts
3544            ("pqc/gopenpgp/gosop_draft-ietf-openpgp-pqc-09_bob_sk.pgp",
3545             "pqc/gopenpgp/gosop_draft-ietf-openpgp-pqc-09_message.pgp",
3546             "Hello World\n"),
3547            ("pqc/gopenpgp/gosop_draft-ietf-openpgp-pqc-09-high-security_bob_sk.pgp",
3548             "pqc/gopenpgp/gosop_draft-ietf-openpgp-pqc-09-high-security_message.pgp",
3549             "Hello World\n"),
3550
3551        ] {
3552            eprintln!("Test vector {:?}...", key_file);
3553            let key = Cert::from_bytes(crate::tests::file(key_file))?;
3554            if ! key.primary_key().key().pk_algo().is_supported() {
3555                eprintln!("Skipping {} because we don't support {}",
3556                          key, key.primary_key().key().pk_algo());
3557                continue;
3558            }
3559
3560            if let Some(k) =
3561                key.with_policy(&p, None)?.keys().subkeys().supported().last()
3562            {
3563                use crate::crypto::mpi::PublicKey;
3564                match k.key().mpis() {
3565                    PublicKey::ECDH { curve, .. } if ! curve.is_supported() => {
3566                        eprintln!("Skipping {} because we don't support \
3567                                   the curve {}", key_file, curve);
3568                        continue;
3569                    },
3570                    _ => (),
3571                }
3572            } else {
3573                eprintln!("Skipping {} because we don't support the algorithm",
3574                          key_file);
3575                continue;
3576            }
3577
3578            let h = VHelper::for_decryption(0, 0, 0, 0, Vec::new(),
3579                                            vec![key], Vec::new());
3580            let mut d = DecryptorBuilder::from_bytes(crate::tests::file(message))?
3581                .with_policy(&p, None, h)?;
3582            assert!(d.message_processed());
3583
3584            if d.helper_ref().error > 0 {
3585                // Expected error.  No point in trying to read
3586                // something.
3587                continue;
3588            }
3589
3590            let mut content = Vec::new();
3591            d.read_to_end(&mut content).unwrap();
3592            let content = String::from_utf8(content).unwrap();
3593            eprintln!("decrypted {:?} using {}", content, key_file);
3594            assert_eq!(&content[..], &plaintext[..]);
3595        }
3596
3597        Ok(())
3598    }
3599
3600    /// Tests legacy two-pass signature scheme, corner cases.
3601    ///
3602    /// XXX: This test needs to be adapted once
3603    /// https://gitlab.com/sequoia-pgp/sequoia/-/issues/128 is
3604    /// implemented.
3605    #[test]
3606    fn verifier_legacy() -> Result<()> {
3607        let packets = crate::PacketPile::from_bytes(
3608            crate::tests::message("signed-1.pgp")
3609        )?
3610            .into_children()
3611            .collect::<Vec<_>>();
3612
3613        fn check(msg: &str, buf: &[u8], expect_good: usize) -> Result<()> {
3614            eprintln!("{}...", msg);
3615            let p = P::new();
3616
3617            let certs = [
3618                "neal.pgp",
3619            ]
3620                .iter()
3621                .map(|f| Cert::from_bytes(crate::tests::key(f)).unwrap())
3622                .collect::<Vec<_>>();
3623
3624            let mut h = VHelper::new(0, 0, 0, 0, false, certs.clone());
3625            h.error_out = false;
3626            let mut v = VerifierBuilder::from_bytes(buf)?
3627                .with_policy(&p, crate::frozen_time(), h)?;
3628            assert!(v.message_processed());
3629            assert_eq!(v.processing_csf_message(), Some(false));
3630            assert_eq!(v.helper_ref().good, expect_good);
3631
3632            let mut content = Vec::new();
3633            v.read_to_end(&mut content).unwrap();
3634            let reference = crate::tests::manifesto();
3635            assert_eq!(reference.len(), content.len());
3636            assert_eq!(reference, &content[..]);
3637            Ok(())
3638        }
3639
3640        // Bare legacy signed message: SIG Literal
3641        let mut o = Vec::new();
3642        packets[2].serialize(&mut o)?;
3643        packets[1].serialize(&mut o)?;
3644        check("bare", &o, 0 /* XXX: should be 1 once #128 is implemented.  */)?;
3645
3646        // Legacy signed message, two signatures: SIG SIG Literal
3647        let mut o = Vec::new();
3648        packets[2].serialize(&mut o)?;
3649        packets[2].serialize(&mut o)?;
3650        packets[1].serialize(&mut o)?;
3651        check("double", &o, 0 /* XXX: should be 2 once #128 is implemented.  */)?;
3652
3653        // Weird legacy signed message: OPS SIG Literal SIG
3654        let mut o = Vec::new();
3655        packets[0].serialize(&mut o)?;
3656        packets[2].serialize(&mut o)?;
3657        packets[1].serialize(&mut o)?;
3658        packets[2].serialize(&mut o)?;
3659        check("weird", &o, 0 /* XXX: should be 2 once #128 is implemented.  */)?;
3660
3661        // Fubar legacy signed message: SIG OPS Literal SIG
3662        let mut o = Vec::new();
3663        packets[2].serialize(&mut o)?;
3664        packets[0].serialize(&mut o)?;
3665        packets[1].serialize(&mut o)?;
3666        packets[2].serialize(&mut o)?;
3667        check("fubar", &o, 1 /* XXX: should be 2 once #128 is implemented.  */)?;
3668
3669        Ok(())
3670    }
3671
3672    /// Tests the order of signatures given to
3673    /// VerificationHelper::check().
3674    #[test]
3675    fn verifier_levels() -> Result<()> {
3676        let p = P::new();
3677
3678        struct VHelper(());
3679        impl VerificationHelper for VHelper {
3680            fn get_certs(&mut self, _ids: &[crate::KeyHandle])
3681                               -> Result<Vec<Cert>> {
3682                Ok(Vec::new())
3683            }
3684
3685            fn check(&mut self, structure: MessageStructure) -> Result<()> {
3686                assert_eq!(structure.iter().count(), 2);
3687                for (i, layer) in structure.into_iter().enumerate() {
3688                    match layer {
3689                        MessageLayer::SignatureGroup { results } => {
3690                            assert_eq!(results.len(), 1);
3691                            if let Err(VerificationError::MissingKey {
3692                                sig, ..
3693                            }) = &results[0] {
3694                                assert_eq!(
3695                                    &sig.issuer_fingerprints().next().unwrap()
3696                                        .to_hex(),
3697                                    match i {
3698                                        0 => "8E8C33FA4626337976D97978069C0C348DD82C19",
3699                                        1 => "C03FA6411B03AE12576461187223B56678E02528",
3700                                        _ => unreachable!(),
3701                                    }
3702                                );
3703                            } else {
3704                                unreachable!()
3705                            }
3706                        },
3707                        _ => unreachable!(),
3708                    }
3709                }
3710                Ok(())
3711            }
3712        }
3713        impl DecryptionHelper for VHelper {
3714            fn decrypt(&mut self, _: &[PKESK], _: &[SKESK],
3715                       _: Option<SymmetricAlgorithm>,
3716                       _: &mut dyn FnMut(Option<SymmetricAlgorithm>, &SessionKey) -> bool)
3717                       -> Result<Option<Cert>>
3718            {
3719                unreachable!();
3720            }
3721        }
3722
3723        // Test verifier.
3724        let v = VerifierBuilder::from_bytes(
3725            crate::tests::message("signed-1-notarized-by-ed25519.pgp"))?
3726            .with_policy(&p, crate::frozen_time(), VHelper(()))?;
3727        assert!(v.message_processed());
3728        assert_eq!(v.processing_csf_message(), Some(false));
3729
3730        // Test decryptor.
3731        let v = DecryptorBuilder::from_bytes(
3732            crate::tests::message("signed-1-notarized-by-ed25519.pgp"))?
3733            .with_policy(&p, crate::frozen_time(), VHelper(()))?;
3734        assert!(v.message_processed());
3735        Ok(())
3736    }
3737
3738    #[test]
3739    fn detached_verifier() -> Result<()> {
3740        fn zeros() -> &'static [u8] {
3741            use std::sync::OnceLock;
3742            static ZEROS: OnceLock<Vec<u8>> = OnceLock::new();
3743            ZEROS.get_or_init(|| vec![0; 100 * 1024 * 1024])
3744        }
3745
3746        let p = P::new();
3747
3748        struct Test<'a> {
3749            sig: Vec<u8>,
3750            content: &'a [u8],
3751            reference: time::SystemTime,
3752        }
3753        let tests = [
3754            Test {
3755                sig: crate::tests::message(
3756                    "a-cypherpunks-manifesto.txt.ed25519.sig").to_vec(),
3757                content: crate::tests::manifesto(),
3758                reference: crate::frozen_time(),
3759            },
3760            // The same, but with a marker packet.
3761            Test {
3762                sig: {
3763                    let sig = crate::PacketPile::from_bytes(
3764                        crate::tests::message(
3765                            "a-cypherpunks-manifesto.txt.ed25519.sig"))?;
3766                    let mut buf = Vec::new();
3767                    Packet::Marker(Default::default()).serialize(&mut buf)?;
3768                    sig.serialize(&mut buf)?;
3769                    buf
3770                },
3771                content: crate::tests::manifesto(),
3772                reference: crate::frozen_time(),
3773            },
3774            Test {
3775                sig: crate::tests::message(
3776                    "emmelie-dorothea-dina-samantha-awina-detached-signature-of-100MB-of-zeros.sig")
3777                    .to_vec(),
3778                content: zeros(),
3779                reference:
3780                crate::types::Timestamp::try_from(1572602018).unwrap().into(),
3781            },
3782        ];
3783
3784        let certs = [
3785            "emmelie-dorothea-dina-samantha-awina-ed25519.pgp"
3786        ].iter()
3787            .map(|f| Cert::from_bytes(crate::tests::key(f)).unwrap())
3788            .collect::<Vec<_>>();
3789
3790        for test in tests.iter() {
3791            let sig = &test.sig;
3792            let content = test.content;
3793            let reference = test.reference;
3794
3795            let h = VHelper::new(0, 0, 0, 0, false, certs.clone());
3796            let mut v = DetachedVerifierBuilder::from_bytes(sig).unwrap()
3797                .with_policy(&p, reference, h).unwrap();
3798            v.verify_bytes(content).unwrap();
3799
3800            let h = v.into_helper();
3801            assert_eq!(h.good, 1);
3802            assert_eq!(h.bad, 0);
3803        }
3804        Ok(())
3805    }
3806
3807    #[test]
3808    fn issue_682() -> Result<()> {
3809        let p = P::new();
3810        let sig = crate::tests::message("signature-with-broken-mpis.sig");
3811
3812        let h = VHelper::new(0, 0, 0, 0, false, vec![]);
3813        let mut v = DetachedVerifierBuilder::from_bytes(sig)?
3814            .with_policy(&p, None, h)?;
3815
3816        assert!(v.verify_bytes(b"").is_err());
3817
3818        let h = v.into_helper();
3819        assert_eq!(h.bad, 1);
3820
3821        Ok(())
3822    }
3823
3824    #[test]
3825    fn verify_long_message() -> Result<()> {
3826        use std::io::Write;
3827        use crate::serialize::stream::{LiteralWriter, Signer, Message};
3828
3829        let p = &P::new();
3830
3831        let (cert, _) = CertBuilder::new()
3832            .set_cipher_suite(CipherSuite::Cv25519)
3833            .add_signing_subkey()
3834            .generate().unwrap();
3835
3836        // sign 3MiB message
3837        let mut buf = vec![];
3838        {
3839            let key = cert.keys().with_policy(p, None).for_signing().next().unwrap().key();
3840            let keypair =
3841                key.clone().parts_into_secret().unwrap()
3842                .into_keypair().unwrap();
3843
3844            let m = Message::new(&mut buf);
3845            let signer = Signer::new(m, keypair)?.build().unwrap();
3846            let mut ls = LiteralWriter::new(signer).build().unwrap();
3847
3848            ls.write_all(&mut vec![42u8; 3 * 1024 * 1024]).unwrap();
3849            ls.finalize().unwrap();
3850        }
3851
3852        // Test Verifier.
3853        let h = VHelper::new(0, 0, 0, 0, false, vec![cert.clone()]);
3854        let mut v = VerifierBuilder::from_bytes(&buf)?
3855            .buffer_size(2 * 2usize.pow(20))
3856            .with_policy(p, None, h)?;
3857
3858        assert!(!v.message_processed());
3859        assert!(v.helper_ref().good == 0);
3860        assert!(v.helper_ref().bad == 0);
3861        assert!(v.helper_ref().unknown == 0);
3862        assert!(v.helper_ref().error == 0);
3863
3864        let mut message = Vec::new();
3865
3866        v.read_to_end(&mut message).unwrap();
3867
3868        assert!(v.message_processed());
3869        assert_eq!(v.processing_csf_message(), Some(false));
3870        assert_eq!(3 * 1024 * 1024, message.len());
3871        assert!(message.iter().all(|&b| b == 42));
3872        assert!(v.helper_ref().good == 1);
3873        assert!(v.helper_ref().bad == 0);
3874        assert!(v.helper_ref().unknown == 0);
3875        assert!(v.helper_ref().error == 0);
3876
3877        // Try the same, but this time we let .check() fail.
3878        let h = VHelper::new(0, 0, /* makes check() fail: */ 1, 0,
3879                             false, vec![cert.clone()]);
3880        let mut v = VerifierBuilder::from_bytes(&buf)?
3881            .buffer_size(2 * 2usize.pow(20))
3882            .with_policy(p, None, h)?;
3883
3884        assert!(!v.message_processed());
3885        assert_eq!(v.processing_csf_message(), Some(false));
3886        assert!(v.helper_ref().good == 0);
3887        assert!(v.helper_ref().bad == 1);
3888        assert!(v.helper_ref().unknown == 0);
3889        assert!(v.helper_ref().error == 0);
3890
3891        let mut message = Vec::new();
3892        let r = v.read_to_end(&mut message);
3893        assert!(r.is_err());
3894
3895        // Check that we only got a truncated message.
3896        assert!(v.message_processed());
3897        assert_eq!(v.processing_csf_message(), Some(false));
3898        assert!(!message.is_empty());
3899        assert!(message.len() <= 1 * 1024 * 1024);
3900        assert!(message.iter().all(|&b| b == 42));
3901        assert!(v.helper_ref().good == 1);
3902        assert!(v.helper_ref().bad == 1);
3903        assert!(v.helper_ref().unknown == 0);
3904        assert!(v.helper_ref().error == 0);
3905
3906        // Test Decryptor.
3907        let h = VHelper::new(0, 0, 0, 0, false, vec![cert.clone()]);
3908        let mut v = DecryptorBuilder::from_bytes(&buf)?
3909            .buffer_size(2 * 2usize.pow(20))
3910            .with_policy(p, None, h)?;
3911
3912        assert!(!v.message_processed());
3913        assert!(v.helper_ref().good == 0);
3914        assert!(v.helper_ref().bad == 0);
3915        assert!(v.helper_ref().unknown == 0);
3916        assert!(v.helper_ref().error == 0);
3917
3918        let mut message = Vec::new();
3919
3920        v.read_to_end(&mut message).unwrap();
3921
3922        assert!(v.message_processed());
3923        assert_eq!(3 * 1024 * 1024, message.len());
3924        assert!(message.iter().all(|&b| b == 42));
3925        assert!(v.helper_ref().good == 1);
3926        assert!(v.helper_ref().bad == 0);
3927        assert!(v.helper_ref().unknown == 0);
3928        assert!(v.helper_ref().error == 0);
3929
3930        // Try the same, but this time we let .check() fail.
3931        let h = VHelper::new(0, 0, /* makes check() fail: */ 1, 0,
3932                             false, vec![cert.clone()]);
3933        let mut v = DecryptorBuilder::from_bytes(&buf)?
3934            .buffer_size(2 * 2usize.pow(20))
3935            .with_policy(p, None, h)?;
3936
3937        assert!(!v.message_processed());
3938        assert!(v.helper_ref().good == 0);
3939        assert!(v.helper_ref().bad == 1);
3940        assert!(v.helper_ref().unknown == 0);
3941        assert!(v.helper_ref().error == 0);
3942
3943        let mut message = Vec::new();
3944        let r = v.read_to_end(&mut message);
3945        assert!(r.is_err());
3946
3947        // Check that we only got a truncated message.
3948        assert!(v.message_processed());
3949        assert!(!message.is_empty());
3950        assert!(message.len() <= 1 * 1024 * 1024);
3951        assert!(message.iter().all(|&b| b == 42));
3952        assert!(v.helper_ref().good == 1);
3953        assert!(v.helper_ref().bad == 1);
3954        assert!(v.helper_ref().unknown == 0);
3955        assert!(v.helper_ref().error == 0);
3956        Ok(())
3957    }
3958
3959    /// Checks that tampering with the MDC yields a uniform error
3960    /// response.
3961    #[test]
3962    fn issue_693() -> Result<()> {
3963        struct H();
3964        impl VerificationHelper for H {
3965            fn get_certs(&mut self, _ids: &[crate::KeyHandle])
3966                         -> Result<Vec<Cert>> {
3967                Ok(Vec::new())
3968            }
3969
3970            fn check(&mut self, _: MessageStructure)
3971                     -> Result<()> {
3972                Ok(())
3973            }
3974        }
3975        impl DecryptionHelper for H {
3976            fn decrypt(&mut self, _: &[PKESK], s: &[SKESK],
3977                       _: Option<SymmetricAlgorithm>,
3978                       decrypt: &mut dyn FnMut(Option<SymmetricAlgorithm>, &SessionKey) -> bool)
3979                       -> Result<Option<Cert>>
3980            {
3981                let (algo, sk) = s[0].decrypt(&"123".into()).unwrap();
3982                let r = decrypt(algo, &sk);
3983                assert!(r);
3984                Ok(None)
3985            }
3986        }
3987
3988        fn check(m: &str) -> Result<()> {
3989            let doit = || -> Result<()> {
3990                let p = &P::new();
3991                let mut decryptor = DecryptorBuilder::from_bytes(m.as_bytes())?
3992                    .with_policy(p, None, H())?;
3993                let mut b = Vec::new();
3994                decryptor.read_to_end(&mut b)?;
3995                Ok(())
3996            };
3997
3998            let e = doit().unwrap_err();
3999            match e.downcast::<io::Error>() {
4000                Ok(e) =>
4001                    assert_eq!(e.into_inner().unwrap().downcast().unwrap(),
4002                               Box::new(Error::ManipulatedMessage)),
4003                Err(e) =>
4004                    assert_eq!(e.downcast::<Error>().unwrap(),
4005                               Error::ManipulatedMessage),
4006            };
4007            Ok(())
4008        }
4009
4010        // Bad hash.
4011        check("-----BEGIN PGP MESSAGE-----
4012
4013wx4EBwMI7dKRUiOYGCUAWmzhiYGS8Pn/16QkyTous6vSOgFMcilte26C7kej
4014rKhvjj6uYNT+mt+L2Yg/FHFvpgVF3KfP0fb+9jZwgt4qpDkTMY7AWPTK6wXX
4015Jo8=
4016=LS8u
4017-----END PGP MESSAGE-----
4018")?;
4019
4020        // Bad header.
4021        check("-----BEGIN PGP MESSAGE-----
4022
4023wx4EBwMI7sPTdlgQwd8AogIcbF/hLVrYbvVbgj4EC6/SOgGNaCyffrR4Fuwl
4024Ft2w56/hB/gTaGEhCgDGXg8NiFGIURqF3eIwxxdKWghUutYmsGwqOZmdJ49a
40259gE=
4026=DzKF
4027-----END PGP MESSAGE-----
4028")?;
4029
4030        // Bad header matching other packet type.
4031        check("-----BEGIN PGP MESSAGE-----
4032
4033wx4EBwMIhpEGBh3v0oMAYgGcj+4CG1mcWQwmyGIDRdvSOgFSHlL2GZ1ZKeXS
403429kScqGg2U8N6ZF9vmj/9Sn7CFtO5PGXn2owQVsopeUSTofV3BNUBpxaBDCO
4035EK8=
4036=TgeJ
4037-----END PGP MESSAGE-----
4038")?;
4039
4040        Ok(())
4041    }
4042
4043    /// Tests samples of messages signed with the cleartext signature
4044    /// framework.
4045    #[test]
4046    fn csf_verification() -> Result<()> {
4047        struct H(Vec<Cert>, bool);
4048        impl VerificationHelper for H {
4049            fn get_certs(&mut self, _ids: &[crate::KeyHandle])
4050                         -> Result<Vec<Cert>> {
4051                Ok(std::mem::take(&mut self.0))
4052            }
4053
4054            fn check(&mut self, m: MessageStructure)
4055                     -> Result<()> {
4056                for (i, layer) in m.into_iter().enumerate() {
4057                    assert_eq!(i, 0);
4058                    if let MessageLayer::SignatureGroup { results } = layer {
4059                        assert!(! results.is_empty());
4060                        for result in results {
4061                            result.unwrap();
4062                        }
4063                        self.1 = true;
4064                    } else {
4065                        panic!();
4066                    }
4067                }
4068
4069                Ok(())
4070            }
4071        }
4072
4073        for (m, c) in [
4074            ("InRelease", "InRelease.signers.pgp"),
4075            ("InRelease.msft", "InRelease.msft.signers.pgp"),
4076            ("InRelease.v3", "InRelease.v3.signers.pgp"),
4077        ] {
4078            let certs = crate::cert::CertParser::from_bytes(
4079                crate::tests::key(c))?.collect::<Result<Vec<_>>>()?;
4080
4081            // The Microsoft cert uses SHA-1.
4082            let p = unsafe { &NP::new() };
4083            eprintln!("Parsing {}...", m);
4084            let mut verifier = VerifierBuilder::from_bytes(
4085                crate::tests::message(m))?
4086                .with_policy(p, None, H(certs, false))?;
4087            let mut b = Vec::new();
4088            verifier.read_to_end(&mut b)?;
4089            assert_eq!(verifier.processing_csf_message(), Some(true));
4090            let h = verifier.into_helper();
4091            assert!(h.1);
4092        }
4093
4094        Ok(())
4095    }
4096
4097    /// Tests whether messages using the cleartext signature framework
4098    /// with multiple signatures and signers are correctly handled.
4099    #[test]
4100    fn csf_multiple_signers() -> Result<()> {
4101        struct H(bool);
4102        impl VerificationHelper for H {
4103            fn get_certs(&mut self, _ids: &[crate::KeyHandle])
4104                         -> Result<Vec<Cert>> {
4105                crate::cert::CertParser::from_bytes(
4106                    crate::tests::key("InRelease.signers.pgp"))?
4107                    .collect()
4108            }
4109
4110            fn check(&mut self, m: MessageStructure)
4111                     -> Result<()> {
4112                for (i, layer) in m.into_iter().enumerate() {
4113                    assert_eq!(i, 0);
4114                    if let MessageLayer::SignatureGroup { results } = layer {
4115                        assert_eq!(results.len(), 3);
4116                        for result in results {
4117                            assert!(result.is_ok());
4118                        }
4119                        self.0 = true;
4120                    } else {
4121                        panic!();
4122                    }
4123                }
4124
4125                Ok(())
4126            }
4127        }
4128
4129        let p = &P::new();
4130        let mut verifier = VerifierBuilder::from_bytes(
4131            crate::tests::message("InRelease"))?
4132            .with_policy(p, None, H(false))?;
4133        let mut b = Vec::new();
4134        verifier.read_to_end(&mut b)?;
4135        assert_eq!(verifier.processing_csf_message(), Some(true));
4136        let h = verifier.into_helper();
4137        assert!(h.0);
4138        Ok(())
4139    }
4140
4141    /// This sample from our test suite generated using GnuPG.
4142    #[test]
4143    fn v4skesk_v1seip_aes128() -> Result<()> {
4144        test_password_encrypted_message(
4145            SymmetricAlgorithm::AES128,
4146            "messages/encrypted-aes128-password-123456789.pgp",
4147            "123456789",
4148            crate::tests::manifesto())
4149    }
4150
4151    /// This sample from our test suite generated using GnuPG.
4152    #[test]
4153    fn v4skesk_v1seip_aes192() -> Result<()> {
4154        test_password_encrypted_message(
4155            SymmetricAlgorithm::AES192,
4156            "messages/encrypted-aes192-password-123456.pgp",
4157            "123456",
4158            crate::tests::manifesto())
4159    }
4160
4161    /// This sample from our test suite generated using GnuPG.
4162    #[test]
4163    fn v4skesk_v1seip_aes256() -> Result<()> {
4164        test_password_encrypted_message(
4165            SymmetricAlgorithm::AES256,
4166            "messages/encrypted-aes256-password-123.pgp",
4167            "123",
4168            crate::tests::manifesto())
4169    }
4170
4171    fn test_password_encrypted_message(cipher: SymmetricAlgorithm,
4172                                       name: &str,
4173                                       password: &str,
4174                                       plaintext: &[u8])
4175                                       -> Result<()> {
4176        if ! cipher.is_supported() {
4177            eprintln!("Skipping test vector {:?}...", name);
4178            return Ok(());
4179        }
4180
4181        eprintln!("Test vector {:?}...", name);
4182
4183        let p = &P::new();
4184        let password: Password = String::from(password).into();
4185
4186        let h = VHelper::for_decryption(0, 0, 0, 0, vec![], vec![],
4187                                        vec![password]);
4188        let mut d = DecryptorBuilder::from_bytes(crate::tests::file(name))?
4189            .with_policy(p, None, h)?;
4190        assert!(d.message_processed());
4191
4192        let mut content = Vec::new();
4193        d.read_to_end(&mut content).unwrap();
4194        assert_eq!(&content, plaintext);
4195
4196        Ok(())
4197    }
4198
4199    /// Checks for a crash with signatures that are unaccounted for.
4200    #[test]
4201    fn unaccounted_signatures() -> Result<()> {
4202        let p = P::new();
4203        let m = b"-----BEGIN PGP MESSAGE-----
4204
4205wgoEAAAAAAB6CkAAxADLBq8AAKurq8IKBCC/CAAAAAD0sA==
4206=KRn6
4207-----END PGP MESSAGE-----
4208";
4209
4210        let mut h = VHelper::new(0, 0, 0, 0, false, vec![
4211            Cert::from_bytes(crate::tests::key("testy.pgp"))?,
4212        ]);
4213        h.error_out = false;
4214        VerifierBuilder::from_bytes(m)?
4215            .with_policy(&p, None, h)
4216            .unwrap();
4217        Ok(())
4218    }
4219
4220    /// Checks for a crash related to HashedReader's HashingMode.
4221    #[test]
4222    fn csf_hashing_mode_assertion_failure() -> Result<()> {
4223        let p = P::new();
4224        let m = b"-----BEGIN PGP SIGNED MESSAGE-----
4225---BEGIN PGP SIGNATURE
42260iHUEARYIAB0QCyUHMcArrZbte9msAndEO9clJG5wpCAEA2/";
4227
4228        let mut h = VHelper::new(0, 0, 0, 0, true, vec![
4229            Cert::from_bytes(crate::tests::key("testy.pgp"))?,
4230        ]);
4231        h.error_out = false;
4232        let _ = VerifierBuilder::from_bytes(m)?
4233            .with_policy(&p, None, h);
4234        Ok(())
4235    }
4236
4237    /// Checks for a crash related to HashedReader's assumptions about
4238    /// the number of signature groups.
4239    #[test]
4240    fn csf_sig_group_count_assertion_failure() -> Result<()> {
4241        let p = P::new();
4242        let m = b"-----BEGIN PGP SIGNED MESSAGE-----
4243-----BEGIN PGP SIGNATURE-----
4244xHUDBRY0WIQ+50WENDPP";
4245
4246        let mut h = VHelper::new(0, 0, 0, 0, true, vec![
4247            Cert::from_bytes(crate::tests::key("testy.pgp"))?,
4248        ]);
4249        h.error_out = false;
4250        let _ = VerifierBuilder::from_bytes(m)?
4251            .with_policy(&p, None, h);
4252        Ok(())
4253    }
4254
4255    /// Tests that the message structure is checked at the end of
4256    /// parsing the packet stream.
4257    #[test]
4258    fn message_grammar_check() -> Result<()> {
4259        let p = P::new();
4260        let certs = vec![Cert::from_bytes(crate::tests::key("neal.pgp"))?];
4261        let helper = VHelper::new(1, 0, 0, 0, false, certs.clone());
4262
4263        let pp = crate::PacketPile::from_bytes(
4264            crate::tests::message("signed-1-notarized-by-ed25519.pgp"))?;
4265        let mut buf = Vec::new();
4266        assert_eq!(pp.children().count(), 5);
4267        // Drop the last signature packet!  Now the OPS and Signature
4268        // packets no longer bracket.
4269        pp.children().take(4).for_each(|p| p.serialize(&mut buf).unwrap());
4270
4271        // Test verifier.
4272        let do_it = || -> Result<()> {
4273            let v = VerifierBuilder::from_bytes(&buf)?
4274                .with_policy(&p, crate::frozen_time(), helper.clone())?;
4275            assert!(v.message_processed());
4276            Ok(())
4277        };
4278        assert!(do_it().is_err());
4279
4280        // Test decryptor.
4281        let do_it = || -> Result<()> {
4282            let v = DecryptorBuilder::from_bytes(&buf)?
4283                .with_policy(&p, crate::frozen_time(), helper)?;
4284            assert!(v.message_processed());
4285            Ok(())
4286        };
4287        assert!(do_it().is_err());
4288
4289        Ok(())
4290    }
4291
4292    /// Tests that an inline-signed message using two different hash
4293    /// algorithms verifies correctly.
4294    #[test]
4295    fn inline_signed_two_hashes() -> Result<()> {
4296        use crate::{
4297            types::{DataFormat, HashAlgorithm, SignatureType},
4298            packet::Literal,
4299            parse::SignatureBuilder,
4300        };
4301        let p = P::new();
4302        let cert = Cert::from_bytes(crate::tests::key("testy-private.pgp"))?;
4303        let helper = VHelper::new(0, 0, 0, 0, false, vec![cert.clone()]);
4304        let mut signer = cert.primary_key().key().clone().parts_into_secret()?
4305            .into_keypair()?;
4306        let msg = b"Hello, world!";
4307        let sig0 = SignatureBuilder::new(SignatureType::Binary)
4308            .set_signature_creation_time(crate::frozen_time())?
4309            .set_hash_algo(HashAlgorithm::SHA256)
4310            .sign_message(&mut signer, msg)?;
4311        let sig1 = SignatureBuilder::new(SignatureType::Binary)
4312            .set_signature_creation_time(crate::frozen_time())?
4313            .set_hash_algo(HashAlgorithm::SHA512)
4314            .sign_message(&mut signer, msg)?;
4315        let packets: Vec<Packet> = vec![
4316            OnePassSig::try_from(&sig0)?.into(),
4317            {
4318                let mut ops = OnePassSig::try_from(&sig1)?;
4319                ops.set_last(true);
4320                ops.into()
4321            },
4322            {
4323                let mut lit = Literal::new(DataFormat::Binary);
4324                lit.set_body((*msg).into());
4325                lit.into()
4326            },
4327            sig1.into(),
4328            sig0.into(),
4329        ];
4330        let mut buf = Vec::new();
4331        packets.iter().for_each(|p| p.serialize(&mut buf).unwrap());
4332        let v = VerifierBuilder::from_bytes(&buf)?
4333            .with_policy(&p, crate::frozen_time(), helper)?;
4334        assert_eq!(v.processing_csf_message(), Some(false));
4335        assert!(v.message_processed());
4336        assert_eq!(v.helper_ref().good, 2);
4337
4338        Ok(())
4339    }
4340
4341    /// This sample packet is from RFC9580.
4342    #[test]
4343    fn v6skesk_v2seip_aes128_ocb() -> Result<()> {
4344        sample_skesk6_packet(
4345            SymmetricAlgorithm::AES128,
4346            AEADAlgorithm::OCB,
4347            "password",
4348            "crypto-refresh/v6skesk-aes128-ocb.pgp",
4349            b"Hello, world!")
4350    }
4351
4352    /// This sample packet is from RFC9580.
4353    #[test]
4354    fn v6skesk_v2seip_aes128_eax() -> Result<()> {
4355        sample_skesk6_packet(
4356            SymmetricAlgorithm::AES128,
4357            AEADAlgorithm::EAX,
4358            "password",
4359            "crypto-refresh/v6skesk-aes128-eax.pgp",
4360            b"Hello, world!")
4361    }
4362
4363    /// This sample packet is from RFC9580.
4364    #[test]
4365    fn v6skesk_v2seip_aes128_gcm() -> Result<()> {
4366        sample_skesk6_packet(
4367            SymmetricAlgorithm::AES128,
4368            AEADAlgorithm::GCM,
4369            "password",
4370            "crypto-refresh/v6skesk-aes128-gcm.pgp",
4371            b"Hello, world!")
4372    }
4373
4374    fn sample_skesk6_packet(cipher: SymmetricAlgorithm,
4375                            aead: AEADAlgorithm,
4376                            password: &str,
4377                            name: &str,
4378                            plaintext: &[u8])
4379                            -> Result<()> {
4380        use crate::crypto::backend::{Backend, interface::Aead};
4381        if ! Backend::supports_algo_with_symmetric(aead, cipher)
4382        {
4383            eprintln!("Skipping test vector {:?}...", name);
4384            return Ok(());
4385        }
4386
4387        eprintln!("Test vector {:?}...", name);
4388
4389        let p = &P::new();
4390        let password: Password = String::from(password).into();
4391
4392        let h = VHelper::for_decryption(0, 0, 0, 0, vec![], vec![],
4393                                        vec![password]);
4394        let mut d = DecryptorBuilder::from_bytes(crate::tests::file(name))?
4395            .with_policy(p, None, h)?;
4396        assert!(d.message_processed());
4397
4398        let mut content = Vec::new();
4399        d.read_to_end(&mut content).unwrap();
4400        assert_eq!(&content, plaintext);
4401
4402        Ok(())
4403    }
4404
4405    // Check that the `signature` over `data` can be verified using
4406    // `cert`.
4407    fn check_detached_sig(cert: &str, sig: &str, data: &[u8])
4408        -> Result<()>
4409    {
4410        eprintln!("Test vector {}/{}...", cert, sig);
4411
4412        let cert = Cert::from_bytes(crate::tests::file(cert))?;
4413        eprintln!("Cert: {:?}", cert.fingerprint());
4414
4415        if let Some(unknown) = cert.keys().find_map(|ka| {
4416            if ka.key().pk_algo().is_supported() {
4417                None
4418            } else {
4419                Some(ka.key().pk_algo())
4420            }
4421        })
4422        {
4423            eprintln!("{}: {} is not supported, skipping.",
4424                      cert, unknown);
4425            return Ok(());
4426        }
4427
4428        let h = VHelper::new(0, 0, 0, 0, false, vec![cert]);
4429        let p = &P::new();
4430        let mut v = DetachedVerifierBuilder::from_bytes(
4431            crate::tests::file(sig))?
4432            .with_policy(p, None, h)?;
4433
4434        let result = v.verify_bytes(data);
4435        eprintln!("Result: {:?}", result);
4436
4437        result.expect("valid signature");
4438
4439        let h = v.into_helper();
4440        assert_eq!(h.good, 1);
4441
4442        Ok(())
4443    }
4444
4445    #[test]
4446    fn test_detached_sigs() -> Result<()> {
4447        let detached_sigs = vec![
4448            // ietf draft test vectors
4449            (
4450            "pqc/ietf/v6-mldsa-65-sample-pk.pgp",
4451            "pqc/ietf/v6-mldsa-65-sample-signature.pgp",
4452            "Testing\n"
4453            ),
4454            (
4455            "pqc/ietf/v6-mldsa-87-sample-pk.pgp",
4456            "pqc/ietf/v6-mldsa-87-sample-signature.pgp",
4457            "Testing\n"
4458            ),
4459            (
4460            "pqc/ietf/v6-slhdsa-128s-sample-pk.pgp",
4461            "pqc/ietf/v6-slhdsa-128s-sample-signature.pgp",
4462            "Testing\n"
4463            ),
4464            (
4465            "pqc/ietf/v6-slhdsa-128f-sample-pk.pgp",
4466            "pqc/ietf/v6-slhdsa-128f-sample-signature.pgp",
4467            "Testing\n"
4468            ),
4469            (
4470            "pqc/ietf/v6-slhdsa-256s-sample-pk.pgp",
4471            "pqc/ietf/v6-slhdsa-256s-sample-signature.pgp",
4472            "Testing\n"
4473            ),
4474            // rpgp artifacts
4475            (
4476            "pqc/rpgp/rsop_draft-ietf-openpgp-pqc-08-v4-ed25519-mlkem768x25519_alice_pk.pgp",
4477            "pqc/rpgp/rsop_draft-ietf-openpgp-pqc-08-v4-ed25519-mlkem768x25519_detached_sig.pgp",
4478            "Hello World\n"
4479            ),
4480            (
4481            "pqc/rpgp/rsop_draft-ietf-openpgp-pqc-08-v6-ed25519-mlkem768x25519_alice_pk.pgp",
4482            "pqc/rpgp/rsop_draft-ietf-openpgp-pqc-08-v6-ed25519-mlkem768x25519_detached_sig.pgp",
4483            "Hello World\n"
4484            ),
4485            (
4486            "pqc/rpgp/rsop_draft-ietf-openpgp-pqc-08-v6-mldsa65ed25519-mlkem768x25519_alice_pk.pgp",
4487            "pqc/rpgp/rsop_draft-ietf-openpgp-pqc-08-v6-mldsa65ed25519-mlkem768x25519_detached_sig.pgp",
4488            "Hello World\n"
4489            ),
4490            (
4491            "pqc/rpgp/rsop_draft-ietf-openpgp-pqc-08-v6-mldsa87ed448-mlkem1024x448_alice_pk.pgp",
4492            "pqc/rpgp/rsop_draft-ietf-openpgp-pqc-08-v6-mldsa87ed448-mlkem1024x448_detached_sig.pgp",
4493            "Hello World\n"
4494            ),
4495            (
4496            "pqc/rpgp/rsop_draft-ietf-openpgp-pqc-08-v6-slhdsashake128f-mlkem768x25519_alice_pk.pgp",
4497            "pqc/rpgp/rsop_draft-ietf-openpgp-pqc-08-v6-slhdsashake128f-mlkem768x25519_detached_sig.pgp",
4498            "Hello World\n"
4499            ),
4500            (
4501            "pqc/rpgp/rsop_draft-ietf-openpgp-pqc-08-v6-slhdsashake128s-mlkem768x25519_alice_pk.pgp",
4502            "pqc/rpgp/rsop_draft-ietf-openpgp-pqc-08-v6-slhdsashake128s-mlkem768x25519_detached_sig.pgp",
4503            "Hello World\n"
4504            ),
4505            (
4506            "pqc/rpgp/rsop_draft-ietf-openpgp-pqc-08-v6-slhdsashake256s-mlkem1024x448_alice_pk.pgp",
4507            "pqc/rpgp/rsop_draft-ietf-openpgp-pqc-08-v6-slhdsashake256s-mlkem1024x448_detached_sig.pgp",
4508            "Hello World\n"
4509            ),
4510            // gopenpgp artifacts
4511            (
4512            "pqc/gopenpgp/gosop_draft-ietf-openpgp-pqc-09_alice_pk.pgp",
4513            "pqc/gopenpgp/gosop_draft-ietf-openpgp-pqc-09_detached_sig.pgp",
4514            "Hello World\n"
4515            ),
4516            (
4517            "pqc/gopenpgp/gosop_draft-ietf-openpgp-pqc-09-high-security_alice_pk.pgp",
4518            "pqc/gopenpgp/gosop_draft-ietf-openpgp-pqc-09-high-security_detached_sig.pgp",
4519            "Hello World\n"
4520            ),
4521        ];
4522
4523        for (cert_file, detached_sig, data) in detached_sigs {
4524            check_detached_sig(cert_file, detached_sig, data.as_bytes())
4525                .expect("valid signature");
4526        }
4527        Ok(())
4528    }
4529
4530    #[test]
4531    fn underflow_in_decryptor_read_helper() -> Result<()> {
4532        use std::io::Write;
4533
4534        use crate::policy::StandardPolicy;
4535        use crate::serialize::stream::LiteralWriter;
4536        use crate::serialize::stream::Message;
4537        use crate::serialize::stream::Signer;
4538
4539        /// Helper for verification
4540        struct VHelper {
4541            certs: Vec<Cert>,
4542        }
4543
4544        impl VHelper {
4545            fn new(certs: Vec<Cert>) -> Self {
4546                Self { certs }
4547            }
4548        }
4549
4550        impl VerificationHelper for VHelper {
4551            fn get_certs(&mut self, _ids: &[KeyHandle]) -> Result<Vec<Cert>> {
4552                Ok(std::mem::take(&mut self.certs))
4553            }
4554            fn check(&mut self, _structure: MessageStructure) -> Result<()> {
4555                Ok(())
4556            }
4557        }
4558
4559        let p = StandardPolicy::new();
4560
4561        // Create a test certificate with signing capability
4562        let (cert, _) = CertBuilder::new()
4563            .add_signing_subkey()
4564            .generate()?;
4565
4566        // Create a valid signed OpenPGP message
4567        let mut signed_msg = Vec::new();
4568        {
4569            let key = cert.keys().with_policy(&p, None).for_signing().next().unwrap().key();
4570            let keypair = key.clone().parts_into_secret()?.into_keypair()?;
4571
4572            let m = Message::new(&mut signed_msg);
4573            let signer = Signer::new(m, keypair)?.build()?;
4574            let mut ls = LiteralWriter::new(signer).build()?;
4575            ls.write_all(b"Hello, this is a test message for the PoC!")?;
4576            ls.finalize()?;
4577        }
4578
4579        // Make sure small (pathological) buffer sizes don't cause a
4580        // panic (e.g., due to underflow).
4581        let h = VHelper::new(vec![cert.clone()]);
4582
4583        let mut verifier = VerifierBuilder::from_bytes(&signed_msg)?
4584            .buffer_size(1)
4585            .with_policy(&p, None, h)?;
4586
4587        let mut output = Vec::new();
4588
4589        verifier.read_to_end(&mut output)?;
4590
4591        Ok(())
4592    }
4593}