Skip to content

Commit

Permalink
Remove encrypt from EncryptionSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
dani-garcia committed Oct 30, 2023
1 parent a9d52f0 commit 432a9ee
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 32 deletions.
9 changes: 1 addition & 8 deletions crates/bitwarden/src/client/encryption_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use {
};

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

Expand Down Expand Up @@ -109,11 +109,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(),
project_ids: input.project_ids.clone(),
});

Expand Down

0 comments on commit 432a9ee

Please sign in to comment.