Skip to content

[PM-32439] Consolidate safe module cryptographic namespacing strategy - #770

Merged
quexten merged 28 commits into
mainfrom
km/consolidate-values
Feb 26, 2026
Merged

[PM-32439] Consolidate safe module cryptographic namespacing strategy#770
quexten merged 28 commits into
mainfrom
km/consolidate-values

Conversation

@quexten

@quexten quexten commented Feb 16, 2026

Copy link
Copy Markdown
Contributor

🎟️ Tracking

https://bitwarden.atlassian.net/browse/PM-32439

📔 Objective

To solve a class of cryptographic issues plaguing our e2ee layer, we need cryptographic namespaces. We already had an initial stab at this for signatures and for data envelopes. To make this approach scalable, we need a more systematic approach. Specifically, the problem to solve here is that encrypted/signed objects, encrypted/signed under the same key can be swapped by the server, causing the client to mis-interpret these, leading to security bugs. Namespaces solve this by creating a cryptographic partitioning (by adding a namespace number to the protected headers, in the signed data / authenticated additional data).

This PR introduces this systematic approach for these. Signatures have their own namespace pratitioning, since they are a lower-level primitive.

Safe objects have two layers: the object layer and the content layer.

On the object layer we partition the cryptographic primitives (Encrypt0, Sign1) into safe primitives (DataEnvelope, PasswordProtectedKeyEnvelope, etc).

On the content layer, we partition each of the above subsets further, by use-case. For example, a valid partitioning for PasswordProtectedKeyEnvelope would be {MasterPassword, Pin}.

This prevents a class of attacks where the server switches cryptographic objects, signed/encrypted under the same key. This eliminates any kind of cryptographic analysis that would be needed to prevent the above attacks / limits security analysis to each sub-namespace spanned by the combination of the object and content namespace layer.

🚨 Breaking Changes

@github-actions

github-actions Bot commented Feb 16, 2026

Copy link
Copy Markdown
Contributor

Logo
Checkmarx One – Scan Summary & Details1478a7e6-97af-40d3-ac77-b049b8488355

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

@github-actions

github-actions Bot commented Feb 16, 2026

Copy link
Copy Markdown
Contributor

🔍 SDK Breaking Change Detection Results

SDK Version: km/consolidate-values (52876b6)
Completed: 2026-02-25 14:32:17 UTC
Total Time: 250s

Client Status Details
typescript ✅ No breaking changes detected TypeScript compilation passed with new SDK version - View Details

Breaking change detection completed. View SDK workflow

@codecov

codecov Bot commented Feb 16, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.17526% with 42 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.68%. Comparing base (dabe980) to head (52876b6).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
crates/bitwarden-crypto/src/safe/data_envelope.rs 75.92% 13 Missing ⚠️
crates/bitwarden-crypto/src/safe/helpers.rs 90.97% 13 Missing ⚠️
...crypto/src/safe/password_protected_key_envelope.rs 91.09% 13 Missing ⚠️
crates/bitwarden-core/src/key_management/crypto.rs 83.33% 1 Missing ⚠️
...bitwarden-core/src/key_management/crypto_client.rs 80.00% 1 Missing ⚠️
crates/bitwarden-crypto/src/cose.rs 88.88% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #770      +/-   ##
==========================================
+ Coverage   81.61%   81.68%   +0.07%     
==========================================
  Files         340      340              
  Lines       39785    40073     +288     
==========================================
+ Hits        32469    32734     +265     
- Misses       7316     7339      +23     

☔ 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.

@quexten
quexten force-pushed the km/consolidate-values branch from 96c814a to 9f988f2 Compare February 16, 2026 13:17
@quexten quexten changed the title Consolidate namespace headers Consolidate safe namespacing strategy Feb 16, 2026
@quexten quexten changed the title Consolidate safe namespacing strategy Consolidate safe module cryptographic namespacing strategy Feb 16, 2026
@quexten quexten changed the title Consolidate safe module cryptographic namespacing strategy [PM-32439] Consolidate safe module cryptographic namespacing strategy Feb 18, 2026
@quexten
quexten force-pushed the km/consolidate-values branch from a267c1d to db20456 Compare February 18, 2026 13:03
Comment thread crates/bitwarden-crypto/src/cose.rs Outdated
@quexten
quexten marked this pull request as ready for review February 19, 2026 13:19
@quexten
quexten requested review from a team as code owners February 19, 2026 13:19
@quexten
quexten marked this pull request as draft February 19, 2026 13:20
@quexten
quexten marked this pull request as ready for review February 20, 2026 11:53
dani-garcia
dani-garcia previously approved these changes Feb 20, 2026

@dani-garcia dani-garcia left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, just two small improvements that we may be able to make

Comment thread crates/bitwarden-crypto/src/safe/password_protected_key_envelope.rs Outdated
Comment thread crates/bitwarden-crypto/src/safe/password_protected_key_envelope.rs Outdated
@quexten
quexten marked this pull request as draft February 20, 2026 16:47
dani-garcia
dani-garcia previously approved these changes Feb 24, 2026

@Thomas-Avery Thomas-Avery left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good, a few minor things to look at from me.

expected_content_namespace: T,
) -> Result<(), ExtractionError> {
let obj = extract_safe_object_namespace(header);
println!("Object namespace extraction result: {obj:?}");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should this be trace debug or just removed?

@quexten quexten Feb 25, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oops, yeah we do not want this. It's left over from debugging

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This reminded me that there's a lint to disable println!, so I've made a separate PR to enable it: #795

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice job on these helpers, it helped 😉 me wrap my head around this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Nice. It usually needs a few revisions for new API design to find a good abstraction, I think this is validation that this is the right direction.

Comment thread crates/bitwarden-crypto/src/safe/README.md Outdated
Comment thread crates/bitwarden-crypto/src/safe/README.md Outdated
Comment thread crates/bitwarden-crypto/src/safe/README.md
Comment thread crates/bitwarden-crypto/src/cose.rs Outdated
Comment on lines 278 to 279
// Note: This block ensures ctx is dropped. Otherwise it would cause a deadlock when
// initializing the user crypto

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should this comment be moved to be over the ctx again?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Moved to outside of the block actually, but yeah, it definitely was not in the right place.

Comment thread crates/bitwarden-crypto/src/safe/data_envelope.rs Outdated
Comment thread crates/bitwarden-crypto/src/safe/password_protected_key_envelope.rs Outdated
Comment thread crates/bitwarden-crypto/src/cose.rs Outdated
pub(crate) const SAFE_OBJECT_NAMESPACE: i64 = -80002;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SafeObjectNamespace {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

💭 I would think both SafeObjectNamespace and ContentNamespace would be pub(crate)? Maybe I'm missing something.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You are right. Changed!

Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com>
quexten and others added 10 commits February 25, 2026 13:23
…pe.rs

Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com>
Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com>
Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com>
Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com>
Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com>
Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com>
@quexten

quexten commented Feb 25, 2026

Copy link
Copy Markdown
Contributor Author

Had to resolve some conflicts after the improved logging PR merged. We added the namespaces to the logging of the dataenvelope and passwordprotectedkeyenvelope structs now, so you can easily see them while debugging.

@quexten
quexten enabled auto-merge (squash) February 25, 2026 12:46
@quexten
quexten requested a review from Thomas-Avery February 25, 2026 12:46
@sonarqubecloud

Copy link
Copy Markdown

@Thomas-Avery Thomas-Avery left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@quexten
quexten requested a review from dani-garcia February 25, 2026 17:51
@quexten
quexten merged commit 3164240 into main Feb 26, 2026
65 checks passed
@quexten
quexten deleted the km/consolidate-values branch February 26, 2026 13:16
bw-ghapp Bot pushed a commit to bitwarden/sdk-swift that referenced this pull request Feb 26, 2026
dani-garcia added a commit that referenced this pull request Mar 13, 2026
## 🎟️ Tracking

https://bitwarden.atlassian.net/browse/PM-32729

## 📔 Objective

This comment
#770 (comment)
reminded me that there's a lint we can enable against println! usage.
Our libraries should use `tracing` instead. We allow them to be used in
tests, same as unwrap, and the rest of the use cases (CLI, examples)
should just allow it as needed.

Note, I've set this new lint to warn (and moved unused_async to warn
too) as it's something that a dev might quickly try in some code while
developing, and setting it to deny is just annoying during development.
CI treats all warnings as errors anyway, so it does't have any negative
effects.

## 🚨 Breaking Changes

<!-- Does this PR introduce any breaking changes? If so, please describe
the impact and migration path for clients.

If you're unsure, the automated TypeScript compatibility check will run
when you open/update this PR and provide feedback.

For breaking changes:
1. Describe what changed in the client interface
2. Explain why the change was necessary
3. Provide migration steps for client developers
4. Link to any paired client PRs if needed

Otherwise, you can remove this section. -->
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.

3 participants