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 1 commit
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
28 changes: 28 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,8 @@ pub struct Cipher {
pub folder_id: Option<Uuid>,
pub collection_ids: Vec<Uuid>,

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 +79,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 +135,15 @@ pub struct CipherListView {

impl KeyEncryptable<Cipher> for CipherView {
fn encrypt_with_key(self, key: &SymmetricCryptoKey) -> Result<Cipher> {
let key_owned = Cipher::get_cipher_key(key, &self.key)?;
let key = key_owned.as_ref().unwrap_or(key);
dani-garcia marked this conversation as resolved.
Show resolved Hide resolved

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 +169,15 @@ impl KeyEncryptable<Cipher> for CipherView {

impl KeyDecryptable<CipherView> for Cipher {
fn decrypt_with_key(&self, key: &SymmetricCryptoKey) -> Result<CipherView> {
let key_owned = Cipher::get_cipher_key(key, &self.key)?;
let key = key_owned.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 +202,19 @@ impl KeyDecryptable<CipherView> for Cipher {
}

impl Cipher {
fn get_cipher_key(
key: &SymmetricCryptoKey,
inner_key: &Option<EncString>,
) -> Result<Option<SymmetricCryptoKey>> {
inner_key
.as_ref()
.map(|k| {
let key: Vec<u8> = k.decrypt_with_key(key)?;
SymmetricCryptoKey::try_from(key.as_slice())
})
.transpose()
}
dani-garcia marked this conversation as resolved.
Show resolved Hide resolved

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

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

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