sequoia_openpgp/packet/pkesk/
v6.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//! PublicKey-Encrypted Session Key packets version 6.
//!
//! The session key is needed to decrypt the actual ciphertext.  See
//! [Version 6 Public Key Encrypted Session Key Packet Format] for
//! details.
//!
//! [Version 6 Public Key Encrypted Session Key Packet Format]: https://www.rfc-editor.org/rfc/rfc9580.html#name-version-6-public-key-encryp

#[cfg(test)]
use quickcheck::{Arbitrary, Gen};

use crate::packet::key;
use crate::packet::Key;
use crate::Fingerprint;
use crate::crypto::Decryptor;
use crate::crypto::mpi::Ciphertext;
use crate::Packet;
use crate::PublicKeyAlgorithm;
use crate::Result;
use crate::SymmetricAlgorithm;
use crate::crypto::SessionKey;
use crate::packet;

/// Holds an asymmetrically encrypted session key.
///
/// The session key is needed to decrypt the actual ciphertext.  See
/// [Version 6 Public Key Encrypted Session Key Packet Format] for
/// details.
///
/// [Version 6 Public Key Encrypted Session Key Packet Format]: https://www.rfc-editor.org/rfc/rfc9580.html#name-version-6-public-key-encryp
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct PKESK6 {
    /// CTB header fields.
    pub(crate) common: packet::Common,

    /// Fingerprint of the key this is encrypted to.
    ///
    /// If the value is `None`, the recipient has not been specified
    /// by the sender to decrease metadata leakage.
    recipient: Option<Fingerprint>,

    /// Public key algorithm used to encrypt the session key.
    pk_algo: PublicKeyAlgorithm,

    /// The encrypted session key.
    esk: Ciphertext,
}

assert_send_and_sync!(PKESK6);

impl PKESK6 {
    /// Creates a new PKESK6 packet.
    pub fn new(recipient: Option<Fingerprint>, pk_algo: PublicKeyAlgorithm,
               encrypted_session_key: Ciphertext)
               -> Result<PKESK6>
    {
        Ok(PKESK6 {
            common: Default::default(),
            recipient,
            pk_algo,
            esk: encrypted_session_key,
        })
    }

    /// Creates a new PKESK6 packet for the given recipient.
    ///
    /// The given symmetric algorithm must match the algorithm that is
    /// used to encrypt the payload.
    pub fn for_recipient<P, R>(session_key: &SessionKey,
                               recipient: &Key<P, R>)
                               -> Result<PKESK6>
    where
        P: key::KeyParts,
        R: key::KeyRole,
    {
        // ElGamal is phased out in RFC 9580.
        #[allow(deprecated)]
        if recipient.pk_algo() == PublicKeyAlgorithm::ElGamalEncrypt
            || recipient.pk_algo() == PublicKeyAlgorithm::ElGamalEncryptSign
        {
            return Err(crate::Error::InvalidOperation(
                "MUST NOT encrypt with version 6 ElGamal keys".into())
                       .into());
        }

        Ok(PKESK6 {
            common: Default::default(),
            recipient: Some(recipient.fingerprint()),
            pk_algo: recipient.pk_algo(),
            esk: packet::PKESK::encrypt_common(
                None, session_key,
                recipient.parts_as_unspecified().role_as_unspecified())?,
        })
    }

    /// Gets the recipient.
    pub fn recipient(&self) -> Option<&Fingerprint> {
        self.recipient.as_ref()
    }

    /// Sets the recipient.
    pub fn set_recipient(&mut self, recipient: Option<Fingerprint>)
                         -> Option<Fingerprint> {
        std::mem::replace(&mut self.recipient, recipient)
    }

    /// Gets the public key algorithm.
    pub fn pk_algo(&self) -> PublicKeyAlgorithm {
        self.pk_algo
    }

    /// Sets the public key algorithm.
    pub fn set_pk_algo(&mut self, algo: PublicKeyAlgorithm)
                       -> PublicKeyAlgorithm {
        std::mem::replace(&mut self.pk_algo, algo)
    }

    /// Gets the encrypted session key.
    pub fn esk(&self) -> &Ciphertext {
        &self.esk
    }

    /// Sets the encrypted session key.
    pub fn set_esk(&mut self, esk: Ciphertext) -> Ciphertext {
        std::mem::replace(&mut self.esk, esk)
    }

    /// Decrypts the encrypted session key.
    ///
    /// If the symmetric algorithm used to encrypt the message is
    /// known in advance, it should be given as argument.  This allows
    /// us to reduce the side-channel leakage of the decryption
    /// operation for RSA.
    ///
    /// Returns the session key and symmetric algorithm used to
    /// encrypt the following payload.
    ///
    /// Returns `None` on errors.  This prevents leaking information
    /// to an attacker, which could lead to compromise of secret key
    /// material with certain algorithms (RSA).  See [Avoiding Leaks
    /// from PKCS#1 Errors].
    ///
    /// [Avoiding Leaks from PKCS#1 Errors]: https://www.rfc-editor.org/rfc/rfc9580.html#name-avoiding-leaks-from-pkcs1-e
    pub fn decrypt(&self, decryptor: &mut dyn Decryptor,
                   sym_algo_hint: Option<SymmetricAlgorithm>)
                   -> Option<SessionKey>
    {
        self.decrypt_insecure(decryptor, sym_algo_hint).ok()
    }

    fn decrypt_insecure(&self, decryptor: &mut dyn Decryptor,
                        sym_algo_hint: Option<SymmetricAlgorithm>)
                        -> Result<SessionKey>
    {
        packet::PKESK::decrypt_common(&self.esk, decryptor, sym_algo_hint, false)
            .map(|(_sym_algo, key)| key)
    }
}

impl From<PKESK6> for packet::PKESK {
    fn from(p: PKESK6) -> Self {
        packet::PKESK::V6(p)
    }
}

impl From<PKESK6> for Packet {
    fn from(p: PKESK6) -> Self {
        Packet::PKESK(p.into())
    }
}

#[cfg(test)]
impl Arbitrary for PKESK6 {
    fn arbitrary(g: &mut Gen) -> Self {
        let (ciphertext, pk_algo) = loop {
            let ciphertext = Ciphertext::arbitrary(g);
            if let Some(pk_algo) = ciphertext.pk_algo() {
                break (ciphertext, pk_algo);
            }
        };

        PKESK6::new(bool::arbitrary(g).then(|| Fingerprint::arbitrary_v6(g)),
                    pk_algo, ciphertext).unwrap()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parse::Parse;
    use crate::serialize::MarshalInto;

    quickcheck! {
        fn roundtrip(p: PKESK6) -> bool {
            let q = PKESK6::from_bytes(&p.to_vec().unwrap()).unwrap();
            assert_eq!(p, q);
            true
        }
    }
}