Skip to content

Conversation

@quexten
Copy link
Contributor

@quexten quexten commented Jun 27, 2025

🎟️ Tracking

https://bitwarden.atlassian.net/browse/PM-26459
https://bitwarden.atlassian.net/wiki/spaces/EN/pages/2052653095/Draft+Tech+Breakdown+-+DataEnvelope

📔 Objective

Important

The confluence document has a more detailed breakdown. This is a shortened version.

Consumers want to protect complex structs such as:

  • Vault exports
  • Ciphers (vault items)
  • Organization reports

Currently, they serialize these themselves (organization reports are serialized as json), and encrypt fields individually. This is slow to handle during encrypt/decrypt, and complex to maintain. The server now has to know the same representation as clients. At the same time, this can be abused by the server omitting certain fields, or swapping encrypted fields encrypted under the same key.

EncStrings are not the correct abstraction layer. A new abstraction / API that is safe and easier to use (an impossible to misuse) is needed.

Security Definition

Attacker model: The relevant attacker model here is a fully compromised server. That’s what E2E encryption / zero knowledge protect against.

Security Goals:

SG1: The structure should not be modifiable (malleable)

  • Reason: This can be abused by the attacker by tampering with the encrypted item in (to the developer) unexpected ways

SG2: The attacker should not be able to infer anything about the contents of the structure aside from length

  • Reason: We have a “Zero knowledge” product and customers expect their data to not be readable by anyone but them.

SG3: The structure should only be encryptable in the specific section of code that the developer intends

  • I.e it should not be possible to swap an encrypted vault item into an encrypted “user settings” “slot”, since the behavior here is undefined.
  • Reason: Same as SG1

This PR implements a "DataEnvelope". The DataEnvelope solves the problem of encrypting entire structs. The caller just provides a struct that is Serializable/Deserializable, and gets back an encrypted blob. The caller does not decide serialization.

Further, we force the creation of a "content encryption key" by the interface. This ensures that teams do not create a coupling to keys. When implementing key-rotation we do not want to re-upload data, but only re-upload re-encrypted keys, and the content-encryption-key enforces this key being present.

An example is provided to show usage.

⏰ 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

  • 👍 (:+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

@github-actions
Copy link
Contributor

github-actions bot commented Jun 27, 2025

Logo
Checkmarx One – Scan Summary & Details9eaf16b4-bb4c-4cb2-802c-16fcff9e3ae2

Great job! No new security vulnerabilities introduced in this pull request

@quexten quexten changed the title [RFC] Implement Data Envelope [RFC] Implement data envelope Jun 27, 2025
@codecov
Copy link

codecov bot commented Jun 27, 2025

Codecov Report

❌ Patch coverage is 81.12392% with 131 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.34%. Comparing base (d553d37) to head (8c8568b).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
crates/bitwarden-crypto/src/safe/data_envelope.rs 70.52% 102 Missing ⚠️
crates/bitwarden-crypto/src/content_format.rs 0.00% 6 Missing ⚠️
crates/bitwarden-crypto/src/store/context.rs 92.20% 6 Missing ⚠️
crates/bitwarden-crypto/src/uniffi_support.rs 0.00% 5 Missing ⚠️
crates/bitwarden-crypto/src/cose.rs 91.30% 4 Missing ⚠️
...twarden-crypto/src/safe/data_envelope_namespace.rs 80.00% 3 Missing ⚠️
...rates/bitwarden-crypto/src/enc_string/symmetric.rs 95.74% 2 Missing ⚠️
...bitwarden-vault/src/cipher/cipher_client/create.rs 0.00% 1 Missing ⚠️
...s/bitwarden-vault/src/cipher/cipher_client/edit.rs 0.00% 1 Missing ⚠️
...es/bitwarden-vault/src/cipher/cipher_client/mod.rs 75.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #336      +/-   ##
==========================================
- Coverage   78.37%   78.34%   -0.03%     
==========================================
  Files         291      293       +2     
  Lines       29391    29892     +501     
==========================================
+ Hits        23034    23419     +385     
- Misses       6357     6473     +116     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Base automatically changed from km/cose-content-format to main June 30, 2025 10:53
# Conflicts:
#	crates/bitwarden-crypto/src/store/context.rs
#	crates/bitwarden-vault/src/cipher/attachment.rs
#	crates/bitwarden-wasm-internal/src/pure_crypto.rs
@sonarqubecloud
Copy link

sonarqubecloud bot commented Jul 4, 2025

1 similar comment
@sonarqubecloud
Copy link

sonarqubecloud bot commented Aug 8, 2025

dani-garcia and others added 3 commits August 19, 2025 16:51
# Conflicts:
#	crates/bitwarden-core/src/key_management/crypto.rs
#	crates/bitwarden-crypto/src/store/context.rs
#	crates/bitwarden-vault/src/cipher/cipher.rs
#	crates/bitwarden-wasm-internal/src/pure_crypto.rs
# Conflicts:
#	crates/bitwarden-crypto/src/safe/password_protected_key_envelope.rs
@quexten quexten force-pushed the km/beeep/safe-data-envelope branch from f5187ff to 16369db Compare October 2, 2025 13:41
Copy link
Member

@Hinton Hinton left a comment

Choose a reason for hiding this comment

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

Overall looks great, some general thoughts.

Copy link
Member

Choose a reason for hiding this comment

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

I don't know if these macros provide much value, at best they save 2 lines of code but they also interup the control flow. I know we have precedence of this with require! ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Imo, in this case it's not so much about saving 2 lines, versus readability. The ensure is used in a cluster of checks. Thus, matches or an equality check seem (slightly) harder to parse than something that states "ensure_{matches/equal}":

ensure_matches!(msg.protected.header.alg, Some(coset::Algorithm::PrivateUse(XCHACHA20_POLY1305)) => DataEnvelopeError::DecryptionError);
ensure_equal!(msg.protected.header.key_id, cek.key_id => DataEnvelopeError::WrongKey);
ensure_equal!(envelope_namespace, *namespace => DataEnvelopeError::InvalidNamespace);
ensure_equal!(content_format, CONTENT_TYPE_PADDED_CBOR => DataEnvelopeError::UnsupportedContentFormat);

vs

if !matches(msg.protected.header.alg, Some(coset::Algorithm::PrivateUse(XCHACHA20_POLY1305))) {
  return Err(DataEnvelopeError::DecryptionError);
}
if !msg.protected.header.key_id == cek.key_id {
  return Err(DataEnvelopeError::WrongKey);
}
if !envelope_namespace == *namespace {
  return Err(DataEnvelopeError::InvalidNamespace);
}
if !content_format == CONTENT_TYPE_PADDED_CBOR {
  return Err(DataEnvelopeError::UnsupportedContentFormat);
}

Copy link
Member

Choose a reason for hiding this comment

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

I'm just saying that in the first case to understand what's actually going in I need to jump into the macros. The syntax is somewhat confusing with the last argument being an expression.

Unwrapped everything I need to know to follow the execution flow is there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense. These were intended to make readability nicer, if they don't I'll undo them.

Looking for additional feedback @mzieniukbw and @Thomas-Avery. Do you have thoughts on readability here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, I'll make this an item for the next KM dev sync. I'll make a follow-up PR to undo the macros assuming the outcome is that they don't provide readability benefit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Update: undoing this in a follow-up PR.

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DataEnvelopeNamespace {
/// The namespace for vault items
VaultItem = 1,
Copy link
Member

Choose a reason for hiding this comment

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

question: This is generally called Cipher in other places. Could this cause confusion?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The vault team is free to rename this entry; However I'll argue that despite precedent of using the term for a long time, Cipher is extremely confusing, especially in the context of an end-to-end encrypted app. Thus I opted for simpler naming here.

I've added a comment clarifying that this is the same as "ciphers"

Copy link
Member

Choose a reason for hiding this comment

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

CC @bitwarden/team-vault-dev

Copy link
Contributor

@Thomas-Avery Thomas-Avery left a comment

Choose a reason for hiding this comment

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

Looking good, have a few minor things and some questions/confusion around versioning.

quexten and others added 4 commits October 29, 2025 11:30
Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com>
Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com>
@claude

This comment was marked as resolved.

@quexten
Copy link
Contributor Author

quexten commented Oct 29, 2025

Claude, re: Critical issues:

  1. The CEK keyid collision is just incoherent nonsense. Even if the key id collides, the key is different so nothing can be swapped between envelopes and SG1, SG3 are not broken.

  2. This is not correct, we don't pass in any ephemeral AAD yes, but cose creates AAD from the protected headers.

  3. No, it does not. The mac is validated before the padding. Only the correct padded message decrypts.

  4. This is not a security issue but a usability issue, we just don't want developers to mis-use the key for unintended operations.

As the review is not useful, I'm muting it.

@quexten quexten merged commit 07e1998 into main Oct 30, 2025
51 of 52 checks passed
@quexten quexten deleted the km/beeep/safe-data-envelope branch October 30, 2025 10:36
bw-ghapp bot pushed a commit to bitwarden/sdk-swift that referenced this pull request Oct 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants