Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PM-4270] Individual cipher key encryption #278

Merged
merged 2 commits into from
Nov 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions crates/bitwarden/src/vault/cipher/cipher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ pub struct Cipher {
pub folder_id: Option<Uuid>,
pub collection_ids: Vec<Uuid>,

/// More recent ciphers uses individual encryption keys to encrypt the other fields of the Cipher.
pub key: Option<EncString>,
dani-garcia marked this conversation as resolved.
Show resolved Hide resolved

pub name: EncString,
pub notes: Option<EncString>,

Expand Down Expand Up @@ -77,6 +80,8 @@ pub struct CipherView {
pub folder_id: Option<Uuid>,
pub collection_ids: Vec<Uuid>,

pub key: Option<EncString>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to expose the key in a view?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to encrypt it back with the same Key, yeah. Though I guess we can generate a new key each time we encrypt a CipherView into a Cipher, if we wanted.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, once the SDK has state we don't have to expose it though and can just look it up internally.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a TODO or open a ticket for the future?


pub name: String,
pub notes: Option<String>,

Expand Down Expand Up @@ -131,11 +136,15 @@ pub struct CipherListView {

impl KeyEncryptable<Cipher> for CipherView {
fn encrypt_with_key(self, key: &SymmetricCryptoKey) -> Result<Cipher> {
let ciphers_key = Cipher::get_cipher_key(key, &self.key)?;
let key = ciphers_key.as_ref().unwrap_or(key);

Ok(Cipher {
id: self.id,
organization_id: self.organization_id,
folder_id: self.folder_id,
collection_ids: self.collection_ids,
key: self.key,
name: self.name.encrypt_with_key(key)?,
notes: self.notes.encrypt_with_key(key)?,
r#type: self.r#type,
Expand All @@ -161,11 +170,15 @@ impl KeyEncryptable<Cipher> for CipherView {

impl KeyDecryptable<CipherView> for Cipher {
fn decrypt_with_key(&self, key: &SymmetricCryptoKey) -> Result<CipherView> {
let ciphers_key = Cipher::get_cipher_key(key, &self.key)?;
let key = ciphers_key.as_ref().unwrap_or(key);

Ok(CipherView {
id: self.id,
organization_id: self.organization_id,
folder_id: self.folder_id,
collection_ids: self.collection_ids.clone(),
key: self.key.clone(),
name: self.name.decrypt_with_key(key)?,
notes: self.notes.decrypt_with_key(key)?,
r#type: self.r#type,
Expand All @@ -190,6 +203,23 @@ impl KeyDecryptable<CipherView> for Cipher {
}

impl Cipher {
/// Get the decrypted individual encryption key for this cipher.
/// Note that some ciphers do not have individual encryption keys,
/// in which case this will return Ok(None) and the key associated
/// with this cipher's user or organization must be used instead
fn get_cipher_key(
key: &SymmetricCryptoKey,
ciphers_key: &Option<EncString>,
) -> Result<Option<SymmetricCryptoKey>> {
ciphers_key
.as_ref()
.map(|k| {
let key: Vec<u8> = k.decrypt_with_key(key)?;
SymmetricCryptoKey::try_from(key.as_slice())
})
.transpose()
}

fn get_decrypted_subtitle(&self, key: &SymmetricCryptoKey) -> Result<String> {
Ok(match self.r#type {
CipherType::Login => {
Expand Down Expand Up @@ -257,6 +287,9 @@ impl Cipher {

impl KeyDecryptable<CipherListView> for Cipher {
fn decrypt_with_key(&self, key: &SymmetricCryptoKey) -> Result<CipherListView> {
let ciphers_key = Cipher::get_cipher_key(key, &self.key)?;
let key = ciphers_key.as_ref().unwrap_or(key);

Ok(CipherListView {
id: self.id,
organization_id: self.organization_id,
Expand Down