fxa_client/internal/commands/
keys.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
/* 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/. */

// All commands share the same structs for their crypto-keys.

use serde::{de::DeserializeOwned, Deserialize, Serialize};

use super::super::device::Device;
use super::super::scopes;
use crate::{Error, Result, ScopedKey};
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine};
use rc_crypto::ece::{self, EcKeyComponents};
use sync15::{EncryptedPayload, KeyBundle};

#[derive(Serialize, Deserialize, Clone)]
pub(crate) enum VersionedPrivateCommandKeys {
    V1(PrivateCommandKeysV1),
}

#[derive(Serialize, Deserialize, Clone)]
pub(crate) struct PrivateCommandKeysV1 {
    p256key: EcKeyComponents,
    auth_secret: Vec<u8>,
}
pub(crate) type PrivateCommandKeys = PrivateCommandKeysV1;

impl PrivateCommandKeys {
    // We define this method so if someone attempts to serialize `PrivateCommandKeys` directly
    // they actually get a serialization of `VersionedPrivateCommandKeys`, which is what we want,
    // because the latter "tags" the version.
    // We should work out how to clean this up to avoid these hacks.
    pub(crate) fn serialize(&self) -> Result<String> {
        Ok(serde_json::to_string(&VersionedPrivateCommandKeys::V1(
            self.clone(),
        ))?)
    }

    pub(crate) fn deserialize(s: &str) -> Result<Self> {
        let versionned: VersionedPrivateCommandKeys = serde_json::from_str(s)?;
        match versionned {
            VersionedPrivateCommandKeys::V1(prv_key) => Ok(prv_key),
        }
    }
}

impl PrivateCommandKeys {
    pub fn from_random() -> Result<Self> {
        rc_crypto::ensure_initialized();
        let (key_pair, auth_secret) = ece::generate_keypair_and_auth_secret()?;
        Ok(Self {
            p256key: key_pair.raw_components()?,
            auth_secret: auth_secret.to_vec(),
        })
    }

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

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

#[derive(Serialize, Deserialize)]
struct CommandKeysPayload {
    /// Hex encoded kid.
    kid: String,
    /// Base 64 encoded IV.
    #[serde(rename = "IV")]
    iv: String,
    /// Hex encoded hmac.
    hmac: String,
    /// Base 64 encoded ciphertext.
    ciphertext: String,
}

impl CommandKeysPayload {
    fn decrypt(self, scoped_key: &ScopedKey) -> Result<PublicCommandKeys> {
        let (ksync, kxcs) = extract_oldsync_key_components(scoped_key)?;
        if hex::decode(self.kid)? != kxcs {
            return Err(Error::MismatchedKeys);
        }
        let key = KeyBundle::from_ksync_bytes(&ksync)?;
        let encrypted_payload = EncryptedPayload {
            iv: self.iv,
            hmac: self.hmac,
            ciphertext: self.ciphertext,
        };
        Ok(encrypted_payload.decrypt_into(&key)?)
    }
}

#[derive(Serialize, Deserialize)]
pub struct PublicCommandKeys {
    /// URL Safe Base 64 encoded push public key.
    #[serde(rename = "publicKey")]
    public_key: String,
    /// URL Safe Base 64 encoded auth secret.
    #[serde(rename = "authSecret")]
    auth_secret: String,
}

impl PublicCommandKeys {
    fn encrypt(&self, scoped_key: &ScopedKey) -> Result<CommandKeysPayload> {
        let (ksync, kxcs) = extract_oldsync_key_components(scoped_key)?;
        let key = KeyBundle::from_ksync_bytes(&ksync)?;
        let encrypted_payload = EncryptedPayload::from_cleartext_payload(&key, &self)?;
        Ok(CommandKeysPayload {
            kid: hex::encode(kxcs),
            iv: encrypted_payload.iv,
            hmac: encrypted_payload.hmac,
            ciphertext: encrypted_payload.ciphertext,
        })
    }
    pub fn as_command_data(&self, scoped_key: &ScopedKey) -> Result<String> {
        let encrypted_public_keys = self.encrypt(scoped_key)?;
        Ok(serde_json::to_string(&encrypted_public_keys)?)
    }
    pub(crate) fn public_key(&self) -> &str {
        &self.public_key
    }
    pub(crate) fn auth_secret(&self) -> &str {
        &self.auth_secret
    }
}

impl From<PrivateCommandKeys> for PublicCommandKeys {
    fn from(internal: PrivateCommandKeys) -> Self {
        Self {
            public_key: URL_SAFE_NO_PAD.encode(internal.p256key.public_key()),
            auth_secret: URL_SAFE_NO_PAD.encode(&internal.auth_secret),
        }
    }
}

fn extract_oldsync_key_components(oldsync_key: &ScopedKey) -> Result<(Vec<u8>, Vec<u8>)> {
    if oldsync_key.scope != scopes::OLD_SYNC {
        return Err(Error::IllegalState(
            "Only oldsync scoped keys are supported at the moment.",
        ));
    }
    let kxcs: &str = oldsync_key.kid.splitn(2, '-').collect::<Vec<_>>()[1];
    let kxcs = URL_SAFE_NO_PAD.decode(kxcs)?;
    let ksync = oldsync_key.key_bytes()?;
    Ok((ksync, kxcs))
}

#[derive(Debug, Serialize, Deserialize)]
struct EncryptedCommandPayload {
    /// URL Safe Base 64 encrypted send-tab payload.
    encrypted: String,
}

impl EncryptedCommandPayload {
    pub(crate) fn decrypt<T: DeserializeOwned>(self, keys: &PrivateCommandKeys) -> Result<T> {
        rc_crypto::ensure_initialized();
        let encrypted = URL_SAFE_NO_PAD.decode(self.encrypted)?;
        let decrypted = ece::decrypt(keys.p256key(), keys.auth_secret(), &encrypted)?;
        Ok(serde_json::from_slice(&decrypted)?)
    }
}

fn encrypt_payload<T: Serialize>(
    payload: &T,
    keys: PublicCommandKeys,
) -> Result<EncryptedCommandPayload> {
    rc_crypto::ensure_initialized();
    let bytes = serde_json::to_vec(payload)?;
    let public_key = URL_SAFE_NO_PAD.decode(keys.public_key())?;
    let auth_secret = URL_SAFE_NO_PAD.decode(keys.auth_secret())?;
    let encrypted = ece::encrypt(&public_key, &auth_secret, &bytes)?;
    let encrypted = URL_SAFE_NO_PAD.encode(encrypted);
    Ok(EncryptedCommandPayload { encrypted })
}

/// encrypt a command suitable for sending via a push message to another device.
pub(crate) fn encrypt_command<T: Serialize>(
    scoped_key: &ScopedKey,
    target: &Device,
    command: &'static str,
    payload: &T,
) -> Result<serde_json::Value> {
    let public_keys = get_public_keys(scoped_key, target, command)?;
    let encrypted_payload = encrypt_payload(payload, public_keys)?;
    Ok(serde_json::to_value(encrypted_payload)?)
}

/// Get the public keys for a command for a device. These are encrypted in a device record.
pub(crate) fn get_public_keys(
    scoped_key: &ScopedKey,
    target: &Device,
    command: &'static str,
) -> Result<PublicCommandKeys> {
    let command = target
        .available_commands
        .get(command)
        .ok_or(Error::UnsupportedCommand(command))?;
    let bundle: CommandKeysPayload = serde_json::from_str(command)?;
    bundle.decrypt(scoped_key)
}

/// decrypt a command sent from another device.
pub(crate) fn decrypt_command<T: DeserializeOwned>(
    v: serde_json::Value,
    keys: &PrivateCommandKeys,
) -> Result<T> {
    let encrypted_payload: EncryptedCommandPayload = serde_json::from_value(v)?;
    encrypted_payload.decrypt(keys)
}