Skip to content

Commit c2434ae

Browse files
authored
Exposed temporary function to decrypt Key (#212)
## 🎟️ Tracking <!-- Paste the link to the Jira or GitHub issue or otherwise describe / point to where this change is coming from. --> ## 📔 Objective `decrypt_fido2_private_key` is exposed temporarily because the typescript clients still use the internal Fido2 authentication logic. As a result, we need the `keyValue` on the Fido2CredentialsView to remain in the decrypted state. This workaround will be removed once the migration to the SDK-based Fido2 Authentication is done. <!-- Describe what the purpose of this PR is, for example what bug you're fixing or new feature you're adding. --> ## ⏰ 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 f28b4ef commit c2434ae

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

crates/bitwarden-vault/src/cipher/cipher.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use bitwarden_core::{
66
use bitwarden_crypto::{
77
CryptoError, Decryptable, EncString, Encryptable, IdentifyKey, KeyStoreContext,
88
};
9+
use bitwarden_error::bitwarden_error;
910
use chrono::{DateTime, Utc};
1011
use schemars::JsonSchema;
1112
use serde::{Deserialize, Serialize};
@@ -30,6 +31,7 @@ use crate::{
3031
VaultParseError,
3132
};
3233

34+
#[bitwarden_error(flat)]
3335
#[derive(Debug, Error)]
3436
pub enum CipherError {
3537
#[error(transparent)]
@@ -584,6 +586,15 @@ impl CipherView {
584586
let res = creds.decrypt(ctx, ciphers_key)?;
585587
Ok(res)
586588
}
589+
590+
pub fn decrypt_fido2_private_key(
591+
&self,
592+
ctx: &mut KeyStoreContext<KeyIds>,
593+
) -> Result<String, CipherError> {
594+
let fido2_credential = self.get_fido2_credentials(ctx)?;
595+
596+
Ok(fido2_credential[0].key_value.clone())
597+
}
587598
}
588599

589600
impl Decryptable<KeyIds, SymmetricKeyId, CipherListView> for Cipher {
@@ -1326,4 +1337,27 @@ mod tests {
13261337
.unwrap();
13271338
assert_eq!(subtitle, original_subtitle);
13281339
}
1340+
1341+
#[test]
1342+
fn test_decrypt_fido2_private_key() {
1343+
let key_store =
1344+
create_test_crypto_with_user_key(SymmetricCryptoKey::generate(rand::thread_rng()));
1345+
let mut ctx = key_store.context();
1346+
1347+
let mut cipher_view = generate_cipher();
1348+
cipher_view
1349+
.generate_cipher_key(&mut ctx, cipher_view.key_identifier())
1350+
.unwrap();
1351+
1352+
let key_id = cipher_view.key_identifier();
1353+
let ciphers_key = Cipher::decrypt_cipher_key(&mut ctx, key_id, &cipher_view.key).unwrap();
1354+
1355+
let fido2_credential = generate_fido2(&mut ctx, ciphers_key);
1356+
1357+
cipher_view.login.as_mut().unwrap().fido2_credentials =
1358+
Some(vec![fido2_credential.clone()]);
1359+
1360+
let decrypted_key_value = cipher_view.decrypt_fido2_private_key(&mut ctx).unwrap();
1361+
assert_eq!(decrypted_key_value, "123");
1362+
}
13291363
}

crates/bitwarden-vault/src/mobile/cipher_client.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ impl ClientCiphers<'_> {
6161
cipher_view.move_to_organization(&mut key_store.context(), organization_id)?;
6262
Ok(cipher_view)
6363
}
64+
65+
pub fn decrypt_fido2_private_key(
66+
&self,
67+
cipher_view: CipherView,
68+
) -> Result<String, CipherError> {
69+
let key_store = self.client.internal.get_key_store();
70+
let decrypted_key = cipher_view.decrypt_fido2_private_key(&mut key_store.context())?;
71+
Ok(decrypted_key)
72+
}
6473
}
6574

6675
impl<'a> VaultClient<'a> {

crates/bitwarden-wasm-internal/src/vault/ciphers.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use std::rc::Rc;
22

33
use bitwarden_core::Client;
44
use bitwarden_vault::{
5-
Cipher, CipherListView, CipherView, DecryptError, EncryptError, Fido2CredentialView,
6-
VaultClientExt,
5+
Cipher, CipherError, CipherListView, CipherView, DecryptError, EncryptError,
6+
Fido2CredentialView, VaultClientExt,
77
};
88
use wasm_bindgen::prelude::wasm_bindgen;
99

@@ -71,4 +71,25 @@ impl ClientCiphers {
7171
.ciphers()
7272
.decrypt_fido2_credentials(cipher_view)
7373
}
74+
75+
/// Decrypt key
76+
///
77+
/// This method is a temporary solution to allow typescript client access to decrypted key
78+
/// values, particularly for FIDO2 credentials.
79+
///
80+
/// # Arguments
81+
/// - `cipher_view` - Decrypted cipher containing the key
82+
///
83+
/// # Returns
84+
/// - `Ok(String)` containing the decrypted key
85+
/// - `Err(CipherError)`
86+
pub fn decrypt_fido2_private_key(
87+
&self,
88+
cipher_view: CipherView,
89+
) -> Result<String, CipherError> {
90+
self.0
91+
.vault()
92+
.ciphers()
93+
.decrypt_fido2_private_key(cipher_view)
94+
}
7495
}

0 commit comments

Comments
 (0)