Skip to content

Commit e91bfd2

Browse files
authored
Undo use of macro (#544)
## 🎟️ Tracking <!-- Paste the link to the Jira or GitHub issue or otherwise describe / point to where this change is coming from. --> ## 📔 Objective By consensus the macro did not improve readability, and so we are undoing it. ## ⏰ Reminders before review - Contributor guidelines followed - All formatters and local linters executed and passed - Written new unit and / or integration tests where applicable - Protected functional changes with optionality (feature flags) - Used internationalization (i18n) for all UI strings - CI builds passed - Communicated to DevOps any deployment requirements - Updated any necessary documentation (Confluence, contributing docs) or informed the documentation team ## 🦮 Reviewer guidelines <!-- Suggested interactions but feel free to use (or not) as you desire! --> - 👍 (`:+1:`) or similar for great changes - 📝 (`:memo:`) or ℹ️ (`:information_source:`) for notes or general info - ❓ (`:question:`) for questions - 🤔 (`:thinking:`) or 💭 (`:thought_balloon:`) for more open inquiry that's not quite a confirmed issue and could potentially benefit from discussion - 🎨 (`:art:`) for suggestions / improvements - ❌ (`:x:`) or ⚠️ (`:warning:`) for more significant problems or concerns needing attention - 🌱 (`:seedling:`) or ♻️ (`:recycle:`) for future improvements or indications of technical debt - ⛏ (`:pick:`) for minor or nitpick changes
1 parent 045ced5 commit e91bfd2

File tree

5 files changed

+30
-79
lines changed

5 files changed

+30
-79
lines changed

crates/bitwarden-crypto/src/enc_string/symmetric.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use serde::Deserialize;
77
use super::{check_length, from_b64, from_b64_vec, split_enc_string};
88
use crate::{
99
Aes256CbcHmacKey, ContentFormat, CoseEncrypt0Bytes, KeyDecryptable, KeyEncryptable,
10-
KeyEncryptableWithContentType, SymmetricCryptoKey, Utf8Bytes, XChaCha20Poly1305Key, ensure,
10+
KeyEncryptableWithContentType, SymmetricCryptoKey, Utf8Bytes, XChaCha20Poly1305Key,
1111
error::{CryptoError, EncStringParseError, Result, UnsupportedOperationError},
1212
};
1313

@@ -293,12 +293,12 @@ impl KeyEncryptableWithContentType<SymmetricCryptoKey, EncString> for &[u8] {
293293
match key {
294294
SymmetricCryptoKey::Aes256CbcHmacKey(key) => EncString::encrypt_aes256_hmac(self, key),
295295
SymmetricCryptoKey::XChaCha20Poly1305Key(inner_key) => {
296-
ensure!(
297-
inner_key
298-
.supported_operations
299-
.contains(&KeyOperation::Encrypt) =>
300-
CryptoError::KeyOperationNotSupported(KeyOperation::Encrypt)
301-
);
296+
if !inner_key
297+
.supported_operations
298+
.contains(&KeyOperation::Encrypt)
299+
{
300+
return Err(CryptoError::KeyOperationNotSupported(KeyOperation::Encrypt));
301+
}
302302
EncString::encrypt_xchacha20_poly1305(self, inner_key, content_format)
303303
}
304304
SymmetricCryptoKey::Aes256CbcKey(_) => Err(CryptoError::OperationNotSupported(

crates/bitwarden-crypto/src/ensure.rs

Lines changed: 0 additions & 61 deletions
This file was deleted.

crates/bitwarden-crypto/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ pub use store::{
3535
KeyStore, KeyStoreContext, RotatedUserKeys, dangerous_get_v2_rotated_account_keys,
3636
};
3737
mod cose;
38-
mod ensure;
3938
pub(crate) use cose::CONTENT_TYPE_PADDED_CBOR;
4039
pub use cose::CoseSerializable;
4140
pub mod safe;

crates/bitwarden-crypto/src/safe/data_envelope.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use crate::{
1313
CONTENT_TYPE_PADDED_CBOR, CoseEncrypt0Bytes, CryptoError, EncString, EncodingError, KeyIds,
1414
SerializedMessage, SymmetricCryptoKey, XChaCha20Poly1305Key,
1515
cose::{DATA_ENVELOPE_NAMESPACE, XCHACHA20_POLY1305},
16-
ensure_equal, ensure_matches,
1716
safe::DataEnvelopeNamespace,
1817
utils::pad_bytes,
1918
xchacha20,
@@ -109,7 +108,10 @@ impl DataEnvelope {
109108
// Serialize the message
110109
let serialized_message =
111110
SerializedMessage::encode(&data).map_err(|_| DataEnvelopeError::EncodingError)?;
112-
ensure_equal!(serialized_message.content_type(), coset::iana::CoapContentFormat::Cbor => DataEnvelopeError::UnsupportedContentFormat);
111+
if serialized_message.content_type() != coset::iana::CoapContentFormat::Cbor {
112+
return Err(DataEnvelopeError::UnsupportedContentFormat);
113+
}
114+
113115
let serialized_and_padded_message = pad_cbor(serialized_message.as_bytes())
114116
.map_err(|_| DataEnvelopeError::EncodingError)?;
115117

@@ -204,10 +206,21 @@ impl DataEnvelope {
204206
content_format(&msg.protected).map_err(|_| DataEnvelopeError::DecodingError)?;
205207

206208
// Validate the message
207-
ensure_matches!(msg.protected.header.alg, Some(coset::Algorithm::PrivateUse(XCHACHA20_POLY1305)) => DataEnvelopeError::DecryptionError);
208-
ensure_equal!(msg.protected.header.key_id, cek.key_id => DataEnvelopeError::WrongKey);
209-
ensure_equal!(envelope_namespace, *namespace => DataEnvelopeError::InvalidNamespace);
210-
ensure_equal!(content_format, CONTENT_TYPE_PADDED_CBOR => DataEnvelopeError::UnsupportedContentFormat);
209+
if !matches!(
210+
msg.protected.header.alg,
211+
Some(coset::Algorithm::PrivateUse(XCHACHA20_POLY1305)),
212+
) {
213+
return Err(DataEnvelopeError::DecryptionError);
214+
}
215+
if msg.protected.header.key_id != cek.key_id {
216+
return Err(DataEnvelopeError::WrongKey);
217+
}
218+
if envelope_namespace != *namespace {
219+
return Err(DataEnvelopeError::InvalidNamespace);
220+
}
221+
if content_format != CONTENT_TYPE_PADDED_CBOR {
222+
return Err(DataEnvelopeError::UnsupportedContentFormat);
223+
}
211224

212225
// Decrypt the message
213226
let decrypted_message = msg

crates/bitwarden-crypto/src/store/context.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
AsymmetricCryptoKey, BitwardenLegacyKeyBytes, ContentFormat, CoseEncrypt0Bytes, CryptoError,
1313
EncString, KeyId, KeyIds, LocalId, PublicKeyEncryptionAlgorithm, Result, RotatedUserKeys,
1414
Signature, SignatureAlgorithm, SignedObject, SignedPublicKey, SignedPublicKeyMessage,
15-
SigningKey, SymmetricCryptoKey, UnsignedSharedKey, derive_shareable_key, ensure,
15+
SigningKey, SymmetricCryptoKey, UnsignedSharedKey, derive_shareable_key,
1616
error::UnsupportedOperationError, signing, store::backend::StoreBackend,
1717
};
1818

@@ -542,9 +542,9 @@ impl<Ids: KeyIds> KeyStoreContext<'_, Ids> {
542542
)),
543543
SymmetricCryptoKey::Aes256CbcHmacKey(key) => EncString::encrypt_aes256_hmac(data, key),
544544
SymmetricCryptoKey::XChaCha20Poly1305Key(key) => {
545-
ensure!(
546-
key.supported_operations.contains(&KeyOperation::Encrypt) => CryptoError::KeyOperationNotSupported(KeyOperation::Encrypt)
547-
);
545+
if !key.supported_operations.contains(&KeyOperation::Encrypt) {
546+
return Err(CryptoError::KeyOperationNotSupported(KeyOperation::Encrypt));
547+
}
548548
EncString::encrypt_xchacha20_poly1305(data, key, content_format)
549549
}
550550
}

0 commit comments

Comments
 (0)