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

Remove encrypt from EncryptionSettings #314

Merged
merged 3 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
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
18 changes: 6 additions & 12 deletions crates/bitwarden/src/client/encryption_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ use rsa::RsaPrivateKey;
use uuid::Uuid;
#[cfg(feature = "internal")]
use {
crate::{client::UserLoginMethod, crypto::KeyDecryptable},
crate::{
client::UserLoginMethod,
crypto::{EncString, KeyDecryptable},
error::{CryptoError, Result},
},
rsa::{pkcs8::DecodePrivateKey, Oaep},
};

use crate::{
crypto::{encrypt_aes256_hmac, EncString, SymmetricCryptoKey},
error::{CryptoError, Result},
};
use crate::crypto::SymmetricCryptoKey;

pub struct EncryptionSettings {
user_key: SymmetricCryptoKey,
Expand Down Expand Up @@ -109,11 +110,4 @@ impl EncryptionSettings {
None => Some(&self.user_key),
}
}

pub(crate) fn encrypt(&self, data: &[u8], org_id: &Option<Uuid>) -> Result<EncString> {
let key = self.get_key(org_id).ok_or(CryptoError::NoKeyForOrg)?;

let dec = encrypt_aes256_hmac(data, key.mac_key.ok_or(CryptoError::InvalidMac)?, key.key)?;
Ok(dec)
}
}
15 changes: 10 additions & 5 deletions crates/bitwarden/src/secrets_manager/projects/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ use serde::{Deserialize, Serialize};
use uuid::Uuid;

use super::ProjectResponse;
use crate::{client::Client, error::Result};
use crate::{
client::Client,
crypto::KeyEncryptable,
error::{Error, Result},
};

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
Expand All @@ -19,12 +23,13 @@ pub(crate) async fn create_project(
client: &mut Client,
input: &ProjectCreateRequest,
) -> Result<ProjectResponse> {
let enc = client.get_encryption_settings()?;

let org_id = Some(input.organization_id);
let key = client
.get_encryption_settings()?
.get_key(&Some(input.organization_id))
.ok_or(Error::VaultLocked)?;

let project = Some(ProjectCreateRequestModel {
name: enc.encrypt(input.name.as_bytes(), &org_id)?.to_string(),
name: input.name.clone().encrypt_with_key(key)?.to_string(),
});

let config = client.get_api_configurations().await;
Expand Down
15 changes: 10 additions & 5 deletions crates/bitwarden/src/secrets_manager/projects/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ use serde::{Deserialize, Serialize};
use uuid::Uuid;

use super::ProjectResponse;
use crate::{client::Client, error::Result};
use crate::{
client::Client,
crypto::KeyEncryptable,
error::{Error, Result},
};

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
Expand All @@ -21,12 +25,13 @@ pub(crate) async fn update_project(
client: &mut Client,
input: &ProjectPutRequest,
) -> Result<ProjectResponse> {
let enc = client.get_encryption_settings()?;

let org_id = Some(input.organization_id);
let key = client
.get_encryption_settings()?
.get_key(&Some(input.organization_id))
.ok_or(Error::VaultLocked)?;

let project = Some(ProjectUpdateRequestModel {
name: enc.encrypt(input.name.as_bytes(), &org_id)?.to_string(),
name: input.name.clone().encrypt_with_key(key)?.to_string(),
});

let config = client.get_api_configurations().await;
Expand Down
19 changes: 12 additions & 7 deletions crates/bitwarden/src/secrets_manager/secrets/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ use serde::{Deserialize, Serialize};
use uuid::Uuid;

use super::SecretResponse;
use crate::{error::Result, Client};
use crate::{
crypto::KeyEncryptable,
error::{Error, Result},
Client,
};

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
Expand All @@ -24,14 +28,15 @@ pub(crate) async fn create_secret(
client: &mut Client,
input: &SecretCreateRequest,
) -> Result<SecretResponse> {
let enc = client.get_encryption_settings()?;

let org_id = Some(input.organization_id);
let key = client
.get_encryption_settings()?
.get_key(&Some(input.organization_id))
.ok_or(Error::VaultLocked)?;

let secret = Some(SecretCreateRequestModel {
key: enc.encrypt(input.key.as_bytes(), &org_id)?.to_string(),
value: enc.encrypt(input.value.as_bytes(), &org_id)?.to_string(),
note: enc.encrypt(input.note.as_bytes(), &org_id)?.to_string(),
key: input.key.clone().encrypt_with_key(key)?.to_string(),
value: input.value.clone().encrypt_with_key(key)?.to_string(),
note: input.note.clone().encrypt_with_key(key)?.to_string(),
project_ids: input.project_ids.clone(),
});

Expand Down
19 changes: 12 additions & 7 deletions crates/bitwarden/src/secrets_manager/secrets/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ use serde::{Deserialize, Serialize};
use uuid::Uuid;

use super::SecretResponse;
use crate::{client::Client, error::Result};
use crate::{
client::Client,
crypto::KeyEncryptable,
error::{Error, Result},
};

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
Expand All @@ -24,14 +28,15 @@ pub(crate) async fn update_secret(
client: &mut Client,
input: &SecretPutRequest,
) -> Result<SecretResponse> {
let enc = client.get_encryption_settings()?;

let org_id = Some(input.organization_id);
let key = client
.get_encryption_settings()?
.get_key(&Some(input.organization_id))
.ok_or(Error::VaultLocked)?;

let secret = Some(SecretUpdateRequestModel {
key: enc.encrypt(input.key.as_bytes(), &org_id)?.to_string(),
value: enc.encrypt(input.value.as_bytes(), &org_id)?.to_string(),
note: enc.encrypt(input.note.as_bytes(), &org_id)?.to_string(),
key: input.key.clone().encrypt_with_key(key)?.to_string(),
value: input.value.clone().encrypt_with_key(key)?.to_string(),
note: input.note.clone().encrypt_with_key(key)?.to_string(),
Comment on lines +37 to +39
Copy link
Member

Choose a reason for hiding this comment

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

This makes me doubt that having encryptable interface consuming the string was correct 😩. @MGibson1

Would to_owned() be more appropriate than clone?

Copy link
Member Author

Choose a reason for hiding this comment

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

There is to_owned() and to_string(), either would work too, yeah.

Ultimately if we want the Encryptable interface to consume the decrypted items, we need to change the APIs to take the requests by value and not by reference, which is a breaking change, otherwise at some point we're going to need to copy the values to have ownership of them.

project_ids: input.project_ids.clone(),
});

Expand Down