Skip to content

[PM-24263] Pin protected key envelope unlock #372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 23 commits into
base: km/beeep/safe-password-protected-key-envelope
Choose a base branch
from

Conversation

quexten
Copy link
Contributor

@quexten quexten commented Jul 31, 2025

🎟️ Tracking

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

📔 Objective

Adds enrollment functionality for Password(PIN)Protected user key envelope. Both crypto initialization via the init request, and a function exposing the raw key material are provided. The latter is required since unlock is not yet done via the init methods on WASM/clients.

⏰ 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

Copy link
Contributor

github-actions bot commented Jul 31, 2025

Logo
Checkmarx One – Scan Summary & Details02371255-09e1-4185-b886-5cdf8c1a6eb5

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

@quexten quexten changed the title Km/pin unlock [PM-24263] Pin protected key envelope unlock Jul 31, 2025
@quexten quexten marked this pull request as ready for review August 13, 2025 11:47
@quexten quexten requested review from a team as code owners August 13, 2025 11:47
@quexten quexten requested review from dani-garcia, mzieniukbw and coroiu and removed request for a team and dani-garcia August 13, 2025 11:47
Copy link
Contributor

@coroiu coroiu left a comment

Choose a reason for hiding this comment

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

Looks good to me, but I don't have a ton of experience with the key store. Let me know if there's anything you'd like me to take a deeper look at!

@quexten
Copy link
Contributor Author

quexten commented Aug 14, 2025

Looks like we had a deadlock on the keystore lock, caught by the CI / tests which should be fixed in the latest revision.

Copy link

codecov bot commented Aug 14, 2025

Codecov Report

❌ Patch coverage is 92.54658% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.56%. Comparing base (eb6bff0) to head (9c293f5).

Files with missing lines Patch % Lines
crates/bitwarden-uniffi/src/crypto.rs 0.00% 12 Missing ⚠️
Additional details and impacted files
@@                                Coverage Diff                                @@
##           km/beeep/safe-password-protected-key-envelope     #372      +/-   ##
=================================================================================
+ Coverage                                          74.36%   74.56%   +0.19%     
=================================================================================
  Files                                                255      255              
  Lines                                              22352    22513     +161     
=================================================================================
+ Hits                                               16622    16786     +164     
+ Misses                                              5730     5727       -3     

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

/// Decrypts a `PasswordProtectedKeyEnvelope`, returning the user key, if successful.
/// This is a stop-gap solution, until initialization of the SDK is used.
#[cfg(feature = "wasm")]
pub fn unseal_password_protected_key_envelope(
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Shall we add some unit test coverage ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added test for the enroll -> re-enroll with encrypted -> unseal flow.

Comment on lines +51 to +53

#[error("Wrong Pin")]
WrongPin,
Copy link
Member

Choose a reason for hiding this comment

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

question: What error do we return if you pass in the wrong password? It would seem reasonable that we return a similar error for both.

I suspect we derive the master key and then attempt to decrypt the user key which would fail and you'd get a decrypt error? Maybe it would be more elegant to handle that with a better error message in which case both wrong password and pin could use the same error?

Copy link
Contributor Author

@quexten quexten Aug 15, 2025

Choose a reason for hiding this comment

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

With master-key decryption you cannot know if:

  • Your email is different to the one used on creation
  • Your kdf settings are different to the one used on creation
  • Your password is different "wrong" to the one used on creation
  • (Your master-key-wrapped-user-key was tampered with)

and all of these would result in the same error. In most cases the true reason would be "wrong password" but there are reasonable cases such as the KDF settings being out of sync, that would lead to the same error path.

With the PasswordProtectedKeyEnvelope, it's only:

  • Your password is different "wrong"
  • (Your key envelope was tampered with)
    so for me it feels more appropriate to use the "WrongPin" error.

(I'm OK changing both to return "WrongPassword", but then we should document that this could also mean that KDF/salt(email) can be wrong for masterkey encrypted items).

Copy link
Member

Choose a reason for hiding this comment

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

Can we lift this into the previous PR or is there a good reason this is handled 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.

GitHub hid what this references, but I've lifted anything that I found to be non-pin-generic to the previous PR, which should make the diff a lot cleaner.

use crate::key_management::SymmetricKeyId;
let ctx = &mut self.key_store.context_mut();
let decrypted_user_key_id = pin_protected_user_key_envelope
.unseal(SymmetricKeyId::Local("tmp_unlock_pin"), &pin, ctx)
Copy link
Member

Choose a reason for hiding this comment

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

@dani-garcia did we have any convention for local IDs? Or is it just the wild west.

Copy link
Contributor Author

@quexten quexten Aug 15, 2025

Choose a reason for hiding this comment

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

I think the approach here is to push the other work dani proposed ( https://bitwarden.atlassian.net/browse/PM-18102), so that we don't have to deal with naming collisions..

@@ -69,6 +69,9 @@ pub enum CryptoError {

#[error("Encoding error, {0}")]
EncodingError(#[from] EncodingError),

#[error("Password protected key envelope error, {0}")]
PasswordProtectedKeyEnvelopeError(#[from] PasswordProtectedKeyEnvelopeError),
Copy link
Member

Choose a reason for hiding this comment

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

nit: We try and avoid having Error suffixes in the variant names.

Suggested change
PasswordProtectedKeyEnvelopeError(#[from] PasswordProtectedKeyEnvelopeError),
PasswordProtectedKeyEnvelope(#[from] PasswordProtectedKeyEnvelopeError),

Copy link
Member

Choose a reason for hiding this comment

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

Is this required to live in CryptoError or could we lift it up. I'm a bit concerned about the size of CryptoError since it's intended as a low level error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would it be more acceptable to be a variant of CryptoClientError? In that case we can lift it up to 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.

(Lifted for now)

Copy link
Member

Choose a reason for hiding this comment

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

Lift to previous PR?

Copy link
Member

Choose a reason for hiding this comment

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

question: What happens if we want to sent another generic of PasswordProtectedKeyEnvelope? We've reserved the name of the generic now.

Copy link
Contributor Author

@quexten quexten Aug 15, 2025

Choose a reason for hiding this comment

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

I think in this specific case it's OK. The reason is that the generic parameter is KeyIds, of which only one instance is ever supposed to exist as far as I'm aware - the one defined in core, describing all Bitwarden keys in the key hierarchy.

I would have preferred to not even make the key envelope generic in the first place, but had to do so in order to have the public interface take a KeyStoreContext and key ids instead of literal keys.

For cases where we have actually varying generic parameters, we should make the names of the non-generic wrappers such that they don't collide. I.e "CoseKeyBytes". "LegacyBitwardenKeyBytes", etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: I've lifted this into the previous PR too, because it's not specific to pin unlock.

user_key_encrypted_pin: EncString,
}

pub(super) fn enroll_pin(
Copy link
Contributor

Choose a reason for hiding this comment

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

❓ Does this need to be exposed to uniffi? I'm not seeing in the Swift bindings.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for catching this! It seems we still separate out clients which is confusing, but it should be fixed in the latest revision of the PR, and I have verified this gets emitted to the swift bindings locally.

Copy link

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.

5 participants