push/internal/
crypto.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

//! Module providing all the cryptography needed by the push component
//!
//! Mainly exports a trait [`Cryptography`] and a concrete type that implements that trait
//! [`Crypto`]
//!
//! The push component encrypts its push notifications. When a subscription is created,
//! [`Cryptography::generate_key`] is called to generate a public/private key pair.
//!
//! The public key is then given to the subscriber (for example, Firefox Accounts) and the private key
//! is persisted in the client. Subscribers are required to encrypt their payloads using the public key and
//! when delivered to the client, the client would load the private key from storage and decrypt the payload.
//!

use std::borrow::Cow;
use std::collections::HashMap;
use std::fmt::Display;
use std::str::FromStr;

use crate::{error, PushError};
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine};
use rc_crypto::ece::{self, EcKeyComponents, LocalKeyPair};
use rc_crypto::ece_crypto::RcCryptoLocalKeyPair;
use rc_crypto::rand;
use serde::{Deserialize, Serialize};

pub const SER_AUTH_LENGTH: usize = 16;
pub type Decrypted = Vec<u8>;

#[derive(Serialize, Deserialize, Clone)]
pub(crate) enum VersionnedKey<'a> {
    V1(Cow<'a, KeyV1>),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
enum CryptoEncoding {
    Aesgcm,
    Aes128gcm,
}

impl FromStr for CryptoEncoding {
    type Err = PushError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(match s.to_lowercase().as_str() {
            "aesgcm" => Self::Aesgcm,
            "aes128gcm" => Self::Aes128gcm,
            _ => {
                return Err(PushError::CryptoError(format!(
                    "Invalid crypto encoding {}",
                    s
                )))
            }
        })
    }
}

impl Display for CryptoEncoding {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::Aesgcm => "aesgcm",
                Self::Aes128gcm => "aes128gcm",
            }
        )
    }
}

#[derive(Clone, PartialEq, Serialize, Deserialize)]
pub struct KeyV1 {
    pub(crate) p256key: EcKeyComponents,
    pub(crate) auth: Vec<u8>,
}
pub type Key = KeyV1;

impl std::fmt::Debug for KeyV1 {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("KeyV1").finish()
    }
}

impl Key {
    // We define this method so the type-checker prevents us from
    // trying to serialize `Key` directly since `bincode::serialize`
    // would compile because both types derive `Serialize`.
    pub(crate) fn serialize(&self) -> error::Result<Vec<u8>> {
        Ok(bincode::serialize(&VersionnedKey::V1(Cow::Borrowed(self)))?)
    }

    pub(crate) fn deserialize(bytes: &[u8]) -> error::Result<Self> {
        let versionned = bincode::deserialize(bytes)?;
        match versionned {
            VersionnedKey::V1(prv_key) => Ok(prv_key.into_owned()),
        }
    }

    pub fn key_pair(&self) -> &EcKeyComponents {
        &self.p256key
    }

    pub fn auth_secret(&self) -> &[u8] {
        &self.auth
    }

    pub fn private_key(&self) -> &[u8] {
        self.p256key.private_key()
    }

    pub fn public_key(&self) -> &[u8] {
        self.p256key.public_key()
    }
}

#[cfg_attr(test, mockall::automock)]
pub trait Cryptography: Default {
    /// generate a new local EC p256 key
    fn generate_key() -> error::Result<Key>;

    /// General decrypt function. Calls to decrypt_aesgcm or decrypt_aes128gcm as needed.
    #[allow(clippy::needless_lifetimes)]
    // Clippy complains here although the lifetime is needed, seems like a bug with automock
    fn decrypt<'a>(key: &Key, push_payload: PushPayload<'a>) -> error::Result<Decrypted>;

    /// Decrypt the obsolete "aesgcm" format (which is still used by a number of providers)
    fn decrypt_aesgcm(
        key: &Key,
        content: &[u8],
        salt: Option<Vec<u8>>,
        crypto_key: Option<Vec<u8>>,
    ) -> error::Result<Decrypted>;

    /// Decrypt the RFC 8188 format.
    fn decrypt_aes128gcm(key: &Key, content: &[u8]) -> error::Result<Decrypted>;
}

#[derive(Default)]
pub struct Crypto;

pub fn get_random_bytes(size: usize) -> error::Result<Vec<u8>> {
    let mut bytes = vec![0u8; size];
    rand::fill(&mut bytes).map_err(|e| {
        error::PushError::CryptoError(format!("Could not generate random bytes: {:?}", e))
    })?;
    Ok(bytes)
}

/// Extract the sub-value from the header.
/// Sub values have the form of `label=value`. Due to a bug in some push providers, treat ',' and ';' as
/// equivalent.
fn extract_value(val: &str, target: &str) -> Option<Vec<u8>> {
    if !val.contains(&format!("{}=", target)) {
        log::debug!("No sub-value found for {}", target);
        return None;
    }
    let items = val.split([',', ';']);
    for item in items {
        let mut kv = item.split('=');
        if kv.next() == Some(target) {
            if let Some(val) = kv.next() {
                return match URL_SAFE_NO_PAD.decode(val) {
                    Ok(v) => Some(v),
                    Err(e) => {
                        error_support::report_error!(
                            "push-base64-decode",
                            "base64 failed for target:{}; {:?}",
                            target,
                            e
                        );
                        None
                    }
                };
            }
        }
    }
    None
}

impl Cryptography for Crypto {
    fn generate_key() -> error::Result<Key> {
        rc_crypto::ensure_initialized();

        let key = RcCryptoLocalKeyPair::generate_random()?;
        let components = key.raw_components()?;
        let auth = get_random_bytes(SER_AUTH_LENGTH)?;
        Ok(Key {
            p256key: components,
            auth,
        })
    }

    fn decrypt(key: &Key, push_payload: PushPayload<'_>) -> error::Result<Decrypted> {
        rc_crypto::ensure_initialized();
        // convert the private key into something useful.
        let d_salt = extract_value(push_payload.salt, "salt");
        let d_dh = extract_value(push_payload.dh, "dh");
        let d_body = URL_SAFE_NO_PAD.decode(push_payload.body)?;

        match CryptoEncoding::from_str(push_payload.encoding)? {
            CryptoEncoding::Aesgcm => Self::decrypt_aesgcm(key, &d_body, d_salt, d_dh),
            CryptoEncoding::Aes128gcm => Self::decrypt_aes128gcm(key, &d_body),
        }
    }

    fn decrypt_aesgcm(
        key: &Key,
        content: &[u8],
        salt: Option<Vec<u8>>,
        crypto_key: Option<Vec<u8>>,
    ) -> error::Result<Decrypted> {
        let dh = crypto_key
            .ok_or_else(|| error::PushError::CryptoError("Missing public key".to_string()))?;
        let salt = salt.ok_or_else(|| error::PushError::CryptoError("Missing salt".to_string()))?;
        let block = ece::legacy::AesGcmEncryptedBlock::new(&dh, &salt, 4096, content.to_vec())?;
        Ok(ece::legacy::decrypt_aesgcm(
            key.key_pair(),
            key.auth_secret(),
            &block,
        )?)
    }

    fn decrypt_aes128gcm(key: &Key, content: &[u8]) -> error::Result<Vec<u8>> {
        Ok(ece::decrypt(key.key_pair(), key.auth_secret(), content)?)
    }
}

#[derive(Debug, Deserialize)]
pub struct PushPayload<'a> {
    pub(crate) channel_id: &'a str,
    pub(crate) body: &'a str,
    pub(crate) encoding: &'a str,
    pub(crate) salt: &'a str,
    pub(crate) dh: &'a str,
}

impl<'a> TryFrom<&'a HashMap<String, String>> for PushPayload<'a> {
    type Error = PushError;

    fn try_from(value: &'a HashMap<String, String>) -> Result<Self, Self::Error> {
        let channel_id = value
            .get("chid")
            .ok_or_else(|| PushError::CryptoError("Invalid Push payload".to_string()))?;
        let body = value
            .get("body")
            .ok_or_else(|| PushError::CryptoError("Invalid Push payload".to_string()))?;
        let encoding = value.get("con").map(|s| s.as_str()).unwrap_or("aes128gcm");
        let salt = value.get("enc").map(|s| s.as_str()).unwrap_or("");
        let dh = value.get("cryptokey").map(|s| s.as_str()).unwrap_or("");
        Ok(Self {
            channel_id,
            body,
            encoding,
            salt,
            dh,
        })
    }
}

#[cfg(test)]
mod crypto_tests {
    use super::*;

    // generate unit test key
    fn test_key(priv_key: &str, pub_key: &str, auth: &str) -> Key {
        let components = EcKeyComponents::new(
            URL_SAFE_NO_PAD.decode(priv_key).unwrap(),
            URL_SAFE_NO_PAD.decode(pub_key).unwrap(),
        );
        let auth = URL_SAFE_NO_PAD.decode(auth).unwrap();
        Key {
            p256key: components,
            auth,
        }
    }

    const PLAINTEXT:&str = "Amidst the mists and coldest frosts I thrust my fists against the\nposts and still demand to see the ghosts.\n\n";

    fn decrypter(ciphertext: &str, encoding: &str, salt: &str, dh: &str) -> error::Result<Vec<u8>> {
        let priv_key_d = "qJkxxWGVVxy7BKvraNY3hg8Gs-Y8qi0lRaXWJ3R3aJ8";
        // The auth token
        let auth_raw = "LsuUOBKVQRY6-l7_Ajo-Ag";
        // This would be the public key sent to the subscription service.
        let pub_key_raw = "BBcJdfs1GtMyymFTtty6lIGWRFXrEtJP40Df0gOvRDR4D8CKVgqE6vlYR7tCYksIRdKD1MxDPhQVmKLnzuife50";

        let key = test_key(priv_key_d, pub_key_raw, auth_raw);
        Crypto::decrypt(
            &key,
            PushPayload {
                channel_id: "channel_id",
                body: ciphertext,
                encoding,
                salt,
                dh,
            },
        )
    }

    #[test]
    fn test_decrypt_aesgcm() {
        // The following comes from the delivered message body
        let ciphertext = "BNKu5uTFhjyS-06eECU9-6O61int3Rr7ARbm-xPhFuyDO5sfxVs-HywGaVonvzkarvfvXE9IRT_YNA81Og2uSqDasdMuw\
                          qm1zd0O3f7049IkQep3RJ2pEZTy5DqvI7kwMLDLzea9nroq3EMH5hYhvQtQgtKXeWieEL_3yVDQVg";
        // and now from the header values
        let dh = "keyid=foo;dh=BMOebOMWSRisAhWpRK9ZPszJC8BL9MiWvLZBoBU6pG6Kh6vUFSW4BHFMh0b83xCg3_7IgfQZXwmVuyu27vwiv5c,otherval=abcde";
        let salt = "salt=tSf2qu43C9BD0zkvRW5eUg";

        // and this is what it should be.

        let decrypted = decrypter(ciphertext, "aesgcm", salt, dh).unwrap();

        assert_eq!(String::from_utf8(decrypted).unwrap(), PLAINTEXT.to_string());
    }

    #[test]
    fn test_fail_decrypt_aesgcm() {
        let ciphertext = "BNKu5uTFhjyS-06eECU9-6O61int3Rr7ARbm-xPhFuyDO5sfxVs-HywGaVonvzkarvfvXE9IRT_\
                          YNA81Og2uSqDasdMuwqm1zd0O3f7049IkQep3RJ2pEZTy5DqvI7kwMLDLzea9nroq3EMH5hYhvQtQgtKXeWieEL_3yVDQVg";
        let dh = "dh=BMOebOMWSRisAhWpRK9ZPszJC8BL9MiWvLZBoBU6pG6Kh6vUFSW4BHFMh0b83xCg3_7IgfQZXwmVuyu27vwiv5c";
        let salt = "salt=SomeInvalidSaltValue";

        decrypter(ciphertext, "aesgcm", salt, dh).expect_err("Failed to abort, bad salt");
    }

    #[test]
    fn test_decrypt_aes128gcm() {
        let ciphertext = "Ek7iQgliMqS9kjFoiVOqRgAAEABBBFirfBtF6XTeHVPABFDveb1iu7uO1XVA_MYJeAo-\
             4ih8WYUsXSTIYmkKMv5_UB3tZuQI7BQ2EVpYYQfvOCrWZVMRL8fJCuB5wVXcoRoTaFJw\
             TlJ5hnw6IMSiaMqGVlc8drX7Hzy-ugzzAKRhGPV2x-gdsp58DZh9Ww5vHpHyT1xwVkXz\
             x3KTyeBZu4gl_zR0Q00li17g0xGsE6Dg3xlkKEmaalgyUyObl6_a8RA6Ko1Rc6RhAy2jdyY1LQbBUnA";

        let decrypted = decrypter(ciphertext, "aes128gcm", "", "").unwrap();
        assert_eq!(String::from_utf8(decrypted).unwrap(), PLAINTEXT.to_string());
    }
}