Skip to content

feat(crypto): add legacy compat symmetric key envelope - #1515

Merged
quexten merged 5 commits into
mainfrom
km/feat/legacy-compat-symmetric-key
Nov 5, 2026
Merged

quexten merged 5 commits into
mainfrom
km/feat/legacy-compat-symmetric-key

Conversation

@quexten

@quexten quexten commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

🎟️ Tracking

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

📔 Objective

We want to migrate existing wrapped keys - currently represented as EncString - to SymmetricKeyEnvelope. SymmetricKeyEnvelope is a completely new format, and provides extra metadata: contained key id, encrypted by key id. It also provides the additional security property: Namespace, which prevents attacks based on the isomorphism between certain keys, and thus binds keys to a specific key slot. (For example, a 64 byte name field could previously be mapped to a 64 byte key. 64 byte keys could also be mapped onto one another by the server).

To migrate, we implement the LegacyCompatSymmetricKey. This essentially allows placing symmetrickeyenvelopes in place of encstrings that wrap symmetric keys. First, a client has to add decrypt support. If a symmetrickeyenvelope gets parsed, the namespace is validated, if an encstring comes back, the namespace is ignored. In the second phase, cilents start writing this value. At that point data will be migrated. This MAY be lazy, or MAY be eager.

For new key types, LegacyCompatSymmetricKey should NEVER be used, only old values:

  • CipherKey
  • AttachmentKey
  • DataEnvelopeCEK
  • Invite Related Wrapped Keys
    should be replaced.

We will make follow-up tickets to migrate each of these.

There is an example provided for how a key is migrated.

Security Note

This opportunistically enhances the security. If an item is migrated, the server can do a rollback if there is a previously recorded state, or if there is another encstring with an isomorphism (or, encstring encrypted by the same key and parses to 32 or 64 bytes or to a valid cose key).

This MAY be prevented by other means in the future, either by fully dropping support for EncStrings (a breaking change), or by restricting the type in certain slots using other cryptographic means (the account security state).

🚨 Breaking Changes

@github-actions

github-actions Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

🔍 SDK Breaking Change Detection

SDK Version: km/feat/legacy-compat-symmetric-key (203ea73)

⚠️ If breaking changes are detected, a corresponding pull request addressing them must be ready for merge in the affected client repository.

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

Breaking change detection uses the build of the SDK from this branch, including any incompatibities pre-existing on or merged into this branch. Check the workflow logs to confirm.
Results update as workflows complete.

@codecov

codecov Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 81.71642% with 49 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.61%. Comparing base (f2f0534) to head (ed4b76b).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...src/compat/legacy_compat_symmetric_key_envelope.rs 81.29% 49 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1515      +/-   ##
==========================================
- Coverage   87.62%   87.61%   -0.02%     
==========================================
  Files         683      684       +1     
  Lines      105230   105498     +268     
==========================================
+ Hits        92208    92431     +223     
- Misses      13022    13067      +45     

☔ View full report in Codecov by Harness.
📢 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.

/// [EncString::Cose_Encrypt0_B64] carries one; [None] for all other variants, or if the COSE
/// message is malformed.
pub fn encrypted_by_key_id(&self) -> Option<KeyId> {
let EncString::Cose_Encrypt0_B64 { data } = self else {

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.

Thought: It's a bit sucky that we have to re-parse the cose struct every time. But I also don't see a way around it, as long as we pass the encstring via FFI, because the FFI parsing MUST not fail.

@quexten
quexten marked this pull request as ready for review September 24, 2026 08:35
@quexten
quexten requested a review from a team as a code owner September 24, 2026 08:35
@quexten
quexten requested a review from eligrubb September 24, 2026 08:35
@github-actions

github-actions Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

🤖 Bitwarden Claude Code Review

Overall Assessment: APPROVE

This review covered the new compat::LegacyCompatSymmetricKeyEnvelope type (seal, seal_legacy, unseal, lax parsing, serde, and its wire-string and TypeScript bindings), the new SymmetricKeyEnvelope::encrypted_by_key_id accessor, and the multi-release migration example. The duplicate EncString::encrypted_by_key_id is gone after ed4b76b, and the compat accessor now delegates to EncString::key_id(). Both earlier findings, on lax EncString parsing and on the module doc's description of the write format, are resolved. No new findings.

@quexten
quexten requested review from luke-bitwarden and removed request for eligrubb and luke-bitwarden October 6, 2026 07:49
@quexten
quexten marked this pull request as draft October 6, 2026 07:58
@quexten

quexten commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

Moving to draft, waiting for blocking PR to merge.

@quexten
quexten force-pushed the km/feat/legacy-compat-symmetric-key branch from cc14288 to b3896b7 Compare October 29, 2026 07:11
@quexten quexten added the ai-review Request a Claude code review label Oct 29, 2026
Replace hand-written wasm_bindgen ABI impls with the shared macro, so
malformed strings surface as WireError::Parse and the type can also be
returned to JS. The macro formats via ToString, so the String
conversion becomes a ToString impl, matching EncString.
seal_legacy still writes an EncString for releases N to N+2, so the
types do not only write the new format.
It duplicated the existing EncString::key_id() line for line, leaving
two public accessors for the same value.
@quexten
quexten requested a review from eligrubb October 29, 2026 07:57
@quexten
quexten marked this pull request as ready for review October 29, 2026 07:57

@eligrubb eligrubb 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.

Glad to see this being added!

@quexten
quexten merged commit 144b502 into main Nov 5, 2026
73 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai-review Request a Claude code review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants