Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

12.8 KB
Raw Blame
use std::{
    collections::BTreeMap,
    env,
    fs::{self, File, OpenOptions},
    io::ErrorKind,
    path::{Path, PathBuf},
};

use anyhow::{Context, Result, bail, ensure};
use ente_core::{
    b64,
    crypto::{Key, blob},
};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use zeroize::{ZeroizeOnDrop, Zeroizing};

use crate::{account::PendingCreation, args::App, home, parse_json};

const SCHEMA_VERSION: u8 = 1;
const VAULT_FILE: &str = "vault.json";

pub struct Vault {
    pub state: State,
    home: PathBuf,
    key: Key,
    _lock: File,
}

pub(crate) struct VaultAccess {
    home: PathBuf,
    key: Key,
}

#[derive(Default, Serialize, Deserialize)]
pub struct State {
    pub accounts: Vec<Account>,
    pub selected: Option<Uuid>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub creations: Vec<PendingCreation>,
}

#[derive(Serialize, Deserialize)]
struct VersionedState<T> {
    schema_version: u8,
    state: T,
}

#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct EncryptedVault {
    data: String,
}

#[derive(Serialize, Deserialize)]
pub struct Account {
    pub storage_id: Uuid,
    pub db_key: DbKey,
    pub name: String,
    pub email: String,
    pub origin: String,
    pub user_id: i64,
    pub identity: AccountKeys,
    pub sessions: BTreeMap<App, StoredSession>,
}

#[derive(Serialize, Deserialize, ZeroizeOnDrop)]
pub struct DbKey(pub [u8; Key::BYTES]);

#[derive(Serialize, Deserialize, ZeroizeOnDrop)]
pub struct AccountKeys {
    pub master_key: Vec<u8>,
    pub recovery_key: Vec<u8>,
    pub secret_key: Vec<u8>,
}

#[derive(Serialize, Deserialize, ZeroizeOnDrop)]
pub struct StoredSession {
    pub token: Vec<u8>,
}

impl Vault {
    pub fn open() -> Result<Self> {
        let override_key = environment_key()?;
        let home = home::application_home()?;
        home::create(&home).context("cannot create private CLI home")?;
        let home = fs::canonicalize(home)?;
        let lock = lock(&home)?;
        let encrypted = read_vault(&home)?;
        let key = load_key(override_key, encrypted.is_some(), || {
            keyring::Entry::new("io.ente.cli", &home.to_string_lossy())
        })?;
        let state = match encrypted {
            Some(bytes) => decrypt(&bytes, &key, &home)?,
            None => State::default(),
        };
        Ok(Self {
            state,
            home,
            key,
            _lock: lock,
        })
    }

    pub fn into_state(self) -> State {
        self.state
    }

    pub fn release(self) -> VaultAccess {
        VaultAccess {
            home: self.home,
            key: self.key,
        }
    }

    pub fn save(&self) -> Result<()> {
        let plaintext = Zeroizing::new(serde_json::to_vec(&VersionedState {
            schema_version: SCHEMA_VERSION,
            state: &self.state,
        })?);
        let encrypted = blob::encrypt_combined(&plaintext, &self.key)
            .map_err(|_| anyhow::anyhow!("cannot encrypt the CLI vault"))?;
        let mut file = tempfile::NamedTempFile::new_in(&self.home)?;
        serde_json::to_writer(
            &mut file,
            &EncryptedVault {
                data: b64::encode(&encrypted),
            },
        )?;
        file.as_file().sync_all()?;
        file.persist(self.home.join(VAULT_FILE))?;
        #[cfg(unix)]
        if let Err(error) = File::open(&self.home).and_then(|dir| dir.sync_all()) {
            eprintln!("Warning: vault saved, but could not sync its directory: {error}");
        }
        Ok(())
    }
}

impl VaultAccess {
    pub fn open(self) -> Result<Vault> {
        let Self { home, key } = self;
        let lock = lock(&home)?;
        let state = match read_vault(&home)? {
            Some(bytes) => decrypt(&bytes, &key, &home)?,
            None => State::default(),
        };
        Ok(Vault {
            state,
            home,
            key,
            _lock: lock,
        })
    }
}

impl State {
    pub fn load() -> Result<Self> {
        let override_key = environment_key()?;
        let home = home::application_home()?;
        // Atomic replacement makes the file a complete snapshot without a read lock.
        let Some(bytes) = read_vault(&home)? else {
            return Ok(Self::default());
        };
        let home = fs::canonicalize(home)?;
        let key = load_key(override_key, true, || {
            keyring::Entry::new("io.ente.cli", &home.to_string_lossy())
        })?;
        decrypt(&bytes, &key, &home)
    }

    pub fn named(&self, name: &str) -> Result<usize> {
        self.accounts
            .iter()
            .position(|a| a.name == name)
            .with_context(|| format!("no account named {name:?}; use account list"))
    }

    pub fn resolve(&self, name: Option<&str>) -> Result<usize> {
        if let Some(name) = name {
            return self.named(name);
        }
        if let Some(index) = self
            .accounts
            .iter()
            .position(|a| Some(a.storage_id) == self.selected)
        {
            return Ok(index);
        }
        match self.accounts.len() {
            0 => bail!("no accounts; use account login --app <APP>"),
            1 => Ok(0),
            _ => bail!(
                "no account selected; use account switch <NAME> or name an account explicitly"
            ),
        }
    }

    pub fn check_name(&self, name: &str) -> Result<()> {
        ensure!(!name.trim().is_empty(), "account name cannot be empty");
        ensure!(
            !self.accounts.iter().any(|a| a.name == name),
            "account name {name:?} is already in use; choose another name"
        );
        Ok(())
    }
}

impl Account {
    pub fn app(&self, requested: Option<App>) -> Result<App> {
        let mut apps = self.sessions.keys();
        match (requested, apps.next(), apps.next()) {
            (Some(app), _, _) | (None, Some(&app), None) => Ok(app),
            _ => {
                ensure!(
                    self.sessions.contains_key(&App::Photos),
                    "multiple apps are saved without Photos; select one with --app"
                );
                Ok(App::Photos)
            }
        }
    }

    pub fn token(&self, app: App) -> Result<&[u8]> {
        self.sessions
            .get(&app)
            .map(|s| s.token.as_slice())
            .with_context(|| {
                format!(
                    "account {:?} is not logged into {}; use account login --account {:?} --app {}",
                    self.name,
                    app.display_name(),
                    self.name,
                    app.name()
                )
            })
    }
}

fn read_vault(home: &Path) -> Result<Option<Vec<u8>>> {
    match fs::read(home.join(VAULT_FILE)) {
        Ok(bytes) => Ok(Some(bytes)),
        Err(error) if error.kind() == ErrorKind::NotFound => Ok(None),
        Err(error) => Err(error).context("cannot read the CLI vault"),
    }
}

fn lock(home: &Path) -> Result<File> {
    let mut options = OpenOptions::new();
    options.read(true).write(true).create(true);
    #[cfg(unix)]
    {
        use std::os::unix::fs::OpenOptionsExt;
        options.mode(0o600);
    }
    let lock = options.open(home.join("vault.lock"))?;
    lock.lock().context("cannot lock the CLI vault")?;
    Ok(lock)
}

fn environment_key() -> Result<Option<Key>> {
    match env::var("ENTE_CLI_VAULT_KEY") {
        Ok(value) => decode_key(&Zeroizing::new(value))
            .context("ENTE_CLI_VAULT_KEY must be standard padded base64 of exactly 32 bytes")
            .map(Some),
        Err(env::VarError::NotPresent) => Ok(None),
        Err(env::VarError::NotUnicode(_)) => {
            bail!("ENTE_CLI_VAULT_KEY must be standard padded base64 of exactly 32 bytes")
        }
    }
}

fn decode_key(value: &str) -> Result<Key> {
    let bytes = Zeroizing::new(b64::decode(value)?);
    Ok(Key::try_from_slice(&bytes)?)
}

fn load_key(
    override_key: Option<Key>,
    exists: bool,
    entry: impl FnOnce() -> keyring::Result<keyring::Entry>,
) -> Result<Key> {
    if let Some(key) = override_key {
        return Ok(key);
    }
    let entry = entry().map_err(|error| keyring_error(error, exists))?;
    match entry.get_secret() {
        Ok(bytes) => {
            let bytes = Zeroizing::new(bytes);
            if bytes.len() == Key::BYTES {
                Key::try_from_slice(&bytes).map_err(Into::into)
            } else {
                std::str::from_utf8(&bytes)
                    .map_err(anyhow::Error::from)
                    .and_then(decode_key)
            }
            .context(
                "system vault key is invalid; reset the saved key and CLI home, then log in again",
            )
        }
        Err(keyring::Error::NoEntry) if !exists => {
            let key = Key::generate();
            entry
                .set_secret(Zeroizing::new(b64::encode(key.as_bytes())).as_bytes())
                .map_err(|error| keyring_error(error, false))?;
            Ok(key)
        }
        Err(error) => Err(keyring_error(error, exists)),
    }
}

fn keyring_error(error: keyring::Error, exists: bool) -> anyhow::Error {
    let reason = match error {
        keyring::Error::NoEntry if exists => {
            return anyhow::anyhow!(
                "no saved vault key for this CLI home; if you moved the directory, restore its previous location or supply the original ENTE_CLI_VAULT_KEY"
            );
        }
        keyring::Error::NoEntry => "the vault key is missing".to_owned(),
        keyring::Error::NoStorageAccess(error) => {
            format!("access to system secret storage was denied: {error}")
        }
        keyring::Error::PlatformFailure(error) => format!("system secret storage failed: {error}"),
        keyring::Error::Ambiguous(_) => "multiple system vault keys match".to_owned(),
        _ => "system secret storage returned an invalid vault-key entry".to_owned(),
    };
    if exists {
        anyhow::anyhow!(
            "{reason}; restore system storage access or supply the original ENTE_CLI_VAULT_KEY"
        )
    } else {
        anyhow::anyhow!(
            "{reason}; use vault key generate, retain the key securely, and supply it through ENTE_CLI_VAULT_KEY"
        )
    }
}

fn decrypt(bytes: &[u8], key: &Key, home: &Path) -> Result<State> {
    let unlock_error = || anyhow::anyhow!("cannot unlock CLI vault: wrong key or damaged file");
    let encrypted: EncryptedVault =
        parse_json(bytes).context("unsupported or invalid CLI vault format")?;
    let ciphertext = b64::decode(&encrypted.data)
        .map_err(|_| anyhow::anyhow!("unsupported or invalid CLI vault format"))?;
    let plaintext =
        Zeroizing::new(blob::decrypt_combined(&ciphertext, key).map_err(|_| unlock_error())?);
    let version: VersionedState<serde::de::IgnoredAny> =
        parse_json(&plaintext).context("invalid CLI vault contents")?;
    ensure!(
        version.schema_version == SCHEMA_VERSION,
        "cannot read CLI vault format {}; this CLI supports {SCHEMA_VERSION}.\nTo start fresh, move {} aside, then log in again.",
        version.schema_version,
        home.join(VAULT_FILE).display(),
    );
    let stored: VersionedState<State> =
        parse_json(&plaintext).context("invalid CLI vault contents")?;
    Ok(stored.state)
}

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

    #[test]
    fn key_selection_never_replaces_an_existing_vault_key() {
        for exists in [false, true] {
            let key = Key::generate();
            let expected = Key::try_from_slice(key.as_bytes()).unwrap();
            assert_eq!(
                load_key(Some(key), exists, || panic!(
                    "environment override touched keyring"
                ))
                .unwrap(),
                expected
            );

            for stored in [
                expected.as_bytes().to_vec(),
                b64::encode(expected.as_bytes()).into_bytes(),
            ] {
                let entry = keyring::Entry::new_with_credential(Box::new(
                    keyring::mock::MockCredential::default(),
                ));
                entry.set_secret(&stored).unwrap();
                assert_eq!(load_key(None, exists, || Ok(entry)).unwrap(), expected);
            }
        }
        let entry = || {
            Ok(keyring::Entry::new_with_credential(Box::new(
                keyring::mock::MockCredential::default(),
            )))
        };
        assert!(load_key(None, false, entry).is_ok());
        let error = load_key(None, true, entry).unwrap_err().to_string();
        assert!(error.contains("original ENTE_CLI_VAULT_KEY"));
        assert!(!error.contains("generate"));

        for exists in [false, true] {
            let error = load_key(None, exists, || {
                Err(keyring::Error::NoStorageAccess(Box::new(
                    std::io::Error::from(ErrorKind::PermissionDenied),
                )))
            })
            .unwrap_err()
            .to_string();
            assert!(error.contains("denied"));
            assert_eq!(error.contains("generate"), !exists);
        }
    }
}