Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions crates/bitwarden-core/src/auth/auth_request.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bitwarden_crypto::{
AsymmetricCryptoKey, AsymmetricPublicCryptoKey, CryptoError, PublicKeyEncryptionAlgorithm,
SpkiPublicKeyBytes, UnsignedSharedKey, fingerprint, generate_random_alphanumeric,
CryptoError, PrivateKey, PublicKey, PublicKeyEncryptionAlgorithm, SpkiPublicKeyBytes,
UnsignedSharedKey, fingerprint, generate_random_alphanumeric,
};
#[cfg(feature = "internal")]
use bitwarden_crypto::{EncString, SymmetricCryptoKey};
Expand Down Expand Up @@ -32,7 +32,7 @@ pub struct AuthRequestResponse {
/// to another device. Where the user confirms the validity by confirming the fingerprint. The user
/// key is then encrypted using the public key and returned to the initiating device.
pub(crate) fn new_auth_request(email: &str) -> Result<AuthRequestResponse, CryptoError> {
let key = AsymmetricCryptoKey::make(PublicKeyEncryptionAlgorithm::RsaOaepSha1);
let key = PrivateKey::make(PublicKeyEncryptionAlgorithm::RsaOaepSha1);

let spki = key.to_public_key().to_der()?;

Expand All @@ -52,7 +52,7 @@ pub(crate) fn auth_request_decrypt_user_key(
private_key: B64,
user_key: UnsignedSharedKey,
) -> Result<SymmetricCryptoKey, EncryptionSettingsError> {
let key = AsymmetricCryptoKey::from_der(&private_key.as_bytes().into())?;
let key = PrivateKey::from_der(&private_key.as_bytes().into())?;
let key: SymmetricCryptoKey = user_key.decapsulate_key_unsigned(&key)?;
Ok(key)
}
Expand All @@ -66,7 +66,7 @@ pub(crate) fn auth_request_decrypt_master_key(
) -> Result<SymmetricCryptoKey, EncryptionSettingsError> {
use bitwarden_crypto::MasterKey;

let key = AsymmetricCryptoKey::from_der(&private_key.as_bytes().into())?;
let key = PrivateKey::from_der(&private_key.as_bytes().into())?;
let master_key: SymmetricCryptoKey = master_key.decapsulate_key_unsigned(&key)?;
let master_key = MasterKey::try_from(&master_key)?;

Expand All @@ -88,7 +88,7 @@ pub(crate) fn approve_auth_request(
client: &Client,
public_key: B64,
) -> Result<UnsignedSharedKey, ApproveAuthRequestError> {
let public_key = AsymmetricPublicCryptoKey::from_der(&SpkiPublicKeyBytes::from(&public_key))?;
let public_key = PublicKey::from_der(&SpkiPublicKeyBytes::from(&public_key))?;

let key_store = client.internal.get_key_store();
let ctx = key_store.context();
Expand Down Expand Up @@ -130,8 +130,7 @@ mod tests {
248, 43, 255, 67, 35, 61, 245, 93,
];

let private_key =
AsymmetricCryptoKey::from_der(&request.private_key.as_bytes().into()).unwrap();
let private_key = PrivateKey::from_der(&request.private_key.as_bytes().into()).unwrap();

let secret = BitwardenLegacyKeyBytes::from(secret);
let encrypted = UnsignedSharedKey::encapsulate_key_unsigned(
Expand Down
5 changes: 2 additions & 3 deletions crates/bitwarden-core/src/auth/tde.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bitwarden_crypto::{
AsymmetricPublicCryptoKey, DeviceKey, EncString, Kdf, SpkiPublicKeyBytes, SymmetricCryptoKey,
DeviceKey, EncString, Kdf, PublicKey, SpkiPublicKeyBytes, SymmetricCryptoKey,
TrustDeviceResponse, UnsignedSharedKey, UserKey,
};
use bitwarden_encoding::B64;
Expand All @@ -18,8 +18,7 @@ pub(super) fn make_register_tde_keys(
org_public_key: B64,
remember_device: bool,
) -> Result<RegisterTdeKeyResponse, EncryptionSettingsError> {
let public_key =
AsymmetricPublicCryptoKey::from_der(&SpkiPublicKeyBytes::from(&org_public_key))?;
let public_key = PublicKey::from_der(&SpkiPublicKeyBytes::from(&org_public_key))?;

let user_key = UserKey::new(SymmetricCryptoKey::make_aes256_cbc_hmac_key());
let key_pair = user_key.make_key_pair()?;
Expand Down
6 changes: 3 additions & 3 deletions crates/bitwarden-core/src/client/encryption_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl EncryptionSettings {
org_enc_keys: Vec<(OrganizationId, UnsignedSharedKey)>,
store: &KeyStore<KeyIds>,
) -> Result<(), EncryptionSettingsError> {
use crate::key_management::AsymmetricKeyId;
use crate::key_management::PrivateKeyId;

let mut ctx = store.context_mut();

Expand All @@ -70,7 +70,7 @@ impl EncryptionSettings {
return Ok(());
}

if !ctx.has_asymmetric_key(AsymmetricKeyId::UserPrivateKey) {
if !ctx.has_private_key(PrivateKeyId::UserPrivateKey) {
return Err(MissingPrivateKeyError.into());
}

Expand All @@ -81,7 +81,7 @@ impl EncryptionSettings {
// Decrypt the org keys with the private key
for (org_id, org_enc_key) in org_enc_keys {
ctx.decapsulate_key_unsigned(
AsymmetricKeyId::UserPrivateKey,
PrivateKeyId::UserPrivateKey,
SymmetricKeyId::Organization(org_id),
&org_enc_key,
)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::sync::RwLock;

use bitwarden_api_api::models::{AccountKeysRequestModel, SecurityStateModel};
use bitwarden_crypto::{
AsymmetricPublicCryptoKey, CoseSerializable, CryptoError, EncString, KeyStore, KeyStoreContext,
CoseSerializable, CryptoError, EncString, KeyStore, KeyStoreContext, PublicKey,
PublicKeyEncryptionAlgorithm, SignatureAlgorithm, SignedPublicKey, SymmetricKeyAlgorithm,
VerifyingKey,
};
Expand All @@ -27,7 +27,7 @@ use tsify::Tsify;
use crate::{
UserId,
key_management::{
AsymmetricKeyId, KeyIds, SecurityState, SignedSecurityState, SigningKeyId, SymmetricKeyId,
KeyIds, PrivateKeyId, SecurityState, SignedSecurityState, SigningKeyId, SymmetricKeyId,
},
};

Expand Down Expand Up @@ -214,7 +214,7 @@ impl WrappedAccountCryptographicState {
mut ctx: KeyStoreContext<KeyIds>,
) -> Result<(), AccountCryptographyInitializationError> {
if ctx.has_symmetric_key(SymmetricKeyId::User)
|| ctx.has_asymmetric_key(AsymmetricKeyId::UserPrivateKey)
|| ctx.has_private_key(PrivateKeyId::UserPrivateKey)
|| ctx.has_signing_key(SigningKeyId::UserSigningKey)
{
return Err(AccountCryptographyInitializationError::KeyStoreAlreadyInitialized);
Expand All @@ -232,7 +232,7 @@ impl WrappedAccountCryptographicState {
// Some users have unreadable V1 private keys. In this case, we set no keys to
// state.
if let Ok(private_key_id) = ctx.unwrap_private_key(user_key, private_key) {
ctx.persist_asymmetric_key(private_key_id, AsymmetricKeyId::UserPrivateKey)?;
ctx.persist_private_key(private_key_id, PrivateKeyId::UserPrivateKey)?;
} else {
tracing::warn!(
"V1 private key could not be unwrapped, skipping setting private key"
Expand Down Expand Up @@ -272,7 +272,7 @@ impl WrappedAccountCryptographicState {
.to_owned()
.verify_and_unwrap(&ctx.get_verifying_key(signing_key_id)?)
.map_err(|_| AccountCryptographyInitializationError::TamperedData)?;
ctx.persist_asymmetric_key(private_key_id, AsymmetricKeyId::UserPrivateKey)?;
ctx.persist_private_key(private_key_id, PrivateKeyId::UserPrivateKey)?;
ctx.persist_signing_key(signing_key_id, SigningKeyId::UserSigningKey)?;
ctx.persist_symmetric_key(user_key, SymmetricKeyId::User)?;
// Not manually dropping ctx here would lead to a deadlock, since storing the state
Expand Down Expand Up @@ -311,7 +311,7 @@ impl WrappedAccountCryptographicState {
fn public_key(
&self,
store: &KeyStore<KeyIds>,
) -> Result<Option<AsymmetricPublicCryptoKey>, AccountCryptographyInitializationError> {
) -> Result<Option<PublicKey>, AccountCryptographyInitializationError> {
match self {
WrappedAccountCryptographicState::V1 { private_key }
| WrappedAccountCryptographicState::V2 { private_key, .. } => {
Expand Down Expand Up @@ -346,7 +346,7 @@ mod tests {
use bitwarden_crypto::{KeyStore, PrimitiveEncryptable};

use super::*;
use crate::key_management::{AsymmetricKeyId, SigningKeyId, SymmetricKeyId};
use crate::key_management::{PrivateKeyId, SigningKeyId, SymmetricKeyId};

#[test]
fn test_set_to_context_v1() {
Expand Down Expand Up @@ -388,7 +388,7 @@ mod tests {
let ctx = store.context();

// Assert that the private key and user symmetric key were set in the store
assert!(ctx.has_asymmetric_key(AsymmetricKeyId::UserPrivateKey));
assert!(ctx.has_private_key(PrivateKeyId::UserPrivateKey));
assert!(ctx.has_symmetric_key(SymmetricKeyId::User));
}

Expand Down Expand Up @@ -450,7 +450,7 @@ mod tests {
assert!(
store
.context()
.has_asymmetric_key(AsymmetricKeyId::UserPrivateKey)
.has_private_key(PrivateKeyId::UserPrivateKey)
);
assert!(
store
Expand Down Expand Up @@ -495,7 +495,7 @@ mod tests {
assert_eq!(
pk_pair.public_key.unwrap(),
B64::from(
ctx.get_public_key(AsymmetricKeyId::UserPrivateKey)
ctx.get_public_key(PrivateKeyId::UserPrivateKey)
.unwrap()
.to_der()
.unwrap()
Expand Down Expand Up @@ -558,6 +558,6 @@ mod tests {
// The user symmetric key should be set
assert!(ctx.has_symmetric_key(SymmetricKeyId::User));
// But the private key should NOT be set (due to corruption)
assert!(!ctx.has_asymmetric_key(AsymmetricKeyId::UserPrivateKey));
assert!(!ctx.has_private_key(PrivateKeyId::UserPrivateKey));
}
}
31 changes: 15 additions & 16 deletions crates/bitwarden-core/src/key_management/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
use std::collections::HashMap;

use bitwarden_crypto::{
AsymmetricCryptoKey, CoseSerializable, CryptoError, EncString, Kdf, KeyDecryptable,
KeyEncryptable, MasterKey, Pkcs8PrivateKeyBytes, PrimitiveEncryptable, RotateableKeySet,
SignatureAlgorithm, SignedPublicKey, SigningKey, SpkiPublicKeyBytes, SymmetricCryptoKey,
UnsignedSharedKey, UserKey, dangerous_get_v2_rotated_account_keys,
derive_symmetric_key_from_prf,
CoseSerializable, CryptoError, EncString, Kdf, KeyDecryptable, KeyEncryptable, MasterKey,
Pkcs8PrivateKeyBytes, PrimitiveEncryptable, PrivateKey, RotateableKeySet, SignatureAlgorithm,
SignedPublicKey, SigningKey, SpkiPublicKeyBytes, SymmetricCryptoKey, UnsignedSharedKey,
UserKey, dangerous_get_v2_rotated_account_keys, derive_symmetric_key_from_prf,
safe::{PasswordProtectedKeyEnvelope, PasswordProtectedKeyEnvelopeError},
};
use bitwarden_encoding::B64;
Expand All @@ -27,7 +26,7 @@ use crate::{
client::{LoginMethod, UserLoginMethod, encryption_settings::EncryptionSettingsError},
error::StatefulCryptoError,
key_management::{
AsymmetricKeyId, SecurityState, SignedSecurityState, SigningKeyId, SymmetricKeyId,
PrivateKeyId, SecurityState, SignedSecurityState, SigningKeyId, SymmetricKeyId,
account_cryptographic_state::WrappedAccountCryptographicState,
master_password::{MasterPasswordAuthenticationData, MasterPasswordUnlockData},
},
Expand Down Expand Up @@ -543,9 +542,9 @@ pub(super) fn enroll_admin_password_reset(
client: &Client,
public_key: B64,
) -> Result<UnsignedSharedKey, EnrollAdminPasswordResetError> {
use bitwarden_crypto::AsymmetricPublicCryptoKey;
use bitwarden_crypto::PublicKey;

let public_key = AsymmetricPublicCryptoKey::from_der(&SpkiPublicKeyBytes::from(&public_key))?;
let public_key = PublicKey::from_der(&SpkiPublicKeyBytes::from(&public_key))?;
let key_store = client.internal.get_key_store();
let ctx = key_store.context();
// FIXME: [PM-18110] This should be removed once the key store can handle public key encryption
Expand Down Expand Up @@ -670,8 +669,8 @@ pub(super) fn verify_asymmetric_keys(
.map_err(VerifyError::DecryptFailed)?;

let decrypted_private_key = Pkcs8PrivateKeyBytes::from(decrypted_private_key);
let private_key = AsymmetricCryptoKey::from_der(&decrypted_private_key)
.map_err(VerifyError::ParseFailed)?;
let private_key =
PrivateKey::from_der(&decrypted_private_key).map_err(VerifyError::ParseFailed)?;

let derived_public_key_vec = private_key
.to_public_key()
Expand Down Expand Up @@ -741,7 +740,7 @@ pub(crate) fn make_v2_keys_for_v1_user(
let mut ctx = key_store.context();

// Re-use existing private key
let private_key_id = AsymmetricKeyId::UserPrivateKey;
let private_key_id = PrivateKeyId::UserPrivateKey;

// Ensure that the function is only called for a V1 user.
if client.internal.get_security_version() != 1 {
Expand All @@ -754,14 +753,14 @@ pub(crate) fn make_v2_keys_for_v1_user(
// Ensure the user has a private key.
// V1 user must have a private key to upgrade. This should be ensured by the client before
// calling the upgrade function.
if !ctx.has_asymmetric_key(AsymmetricKeyId::UserPrivateKey) {
if !ctx.has_private_key(PrivateKeyId::UserPrivateKey) {
return Err(StatefulCryptoError::Crypto(CryptoError::MissingKeyId(
"UserPrivateKey".to_string(),
)));
}

#[allow(deprecated)]
let private_key = ctx.dangerous_get_asymmetric_key(private_key_id)?.clone();
let private_key = ctx.dangerous_get_private_key(private_key_id)?.clone();

// New user key
let user_key = SymmetricCryptoKey::make_xchacha20_poly1305_key();
Expand Down Expand Up @@ -828,7 +827,7 @@ pub(crate) fn get_v2_rotated_account_keys(
.ok_or(StatefulCryptoError::MissingSecurityState)?;

let rotated_keys = dangerous_get_v2_rotated_account_keys(
AsymmetricKeyId::UserPrivateKey,
PrivateKeyId::UserPrivateKey,
SigningKeyId::UserSigningKey,
&ctx,
)?;
Expand Down Expand Up @@ -1248,7 +1247,7 @@ mod tests {
let private_key: B64 = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCzLtEUdxfcLxDj84yaGFsVF5hZ8Hjlb08NMQDy1RnBma06I3ZESshLYzVz4r/gegMn9OOltfV/Yxlyvida8oW6qdlfJ7AVz6Oa8pV7BiL40C7b76+oqraQpyYw2HChANB1AhXL9SqWngKmLZwjA7qiCrmcc0kZHeOb4KnKtp9iVvPVs+8veFvKgYO4ba2AAOHKFdR0W55/agXfAy+fWUAkC8mc9ikyJdQWaPV6OZvC2XFkOseBQm9Rynudh3BQpoWiL6w620efe7t5k+02/EyOFJL9f/XEEjM/+Yo0t3LAfkuhHGeKiRST59Xc9hTEmyJTeVXROtz+0fjqOp3xkaObAgMBAAECggEACs4xhnO0HaZhh1/iH7zORMIRXKeyxP2LQiTR8xwN5JJ9wRWmGAR9VasS7EZFTDidIGVME2u/h4s5EqXnhxfO+0gGksVvgNXJ/qw87E8K2216g6ZNo6vSGA7H1GH2vo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TA+KtEGF2dtGQ=".parse().unwrap();

let private_key = Pkcs8PrivateKeyBytes::from(private_key.as_bytes());
let private_key = AsymmetricCryptoKey::from_der(&private_key).unwrap();
let private_key = PrivateKey::from_der(&private_key).unwrap();
let decrypted: SymmetricCryptoKey =
encrypted.decapsulate_key_unsigned(&private_key).unwrap();

Expand Down Expand Up @@ -1554,7 +1553,7 @@ mod tests {
let key_store = client.internal.get_key_store();
let context = key_store.context();
assert!(context.has_symmetric_key(SymmetricKeyId::User));
assert!(context.has_asymmetric_key(AsymmetricKeyId::UserPrivateKey));
assert!(context.has_private_key(PrivateKeyId::UserPrivateKey));
let login_method = client.internal.get_login_method().unwrap();
if let LoginMethod::User(UserLoginMethod::Username {
email,
Expand Down
10 changes: 5 additions & 5 deletions crates/bitwarden-core/src/key_management/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
//! Any code that needs to interact with the [KeyStore] should use these types.
//!
//! - [SymmetricKeyId] is used to identify symmetric keys.
//! - [AsymmetricKeyId] is used to identify asymmetric keys.
//! - [KeyIds] is a helper type that combines both symmetric and asymmetric key identifiers. This is
//! - [PrivateKeyId] is used to identify private keys.
//! - [KeyIds] is a helper type that combines both symmetric and private key identifiers. This is
//! usually used in the type bounds of [KeyStore],
//! [KeyStoreContext](bitwarden_crypto::KeyStoreContext),
//! [PrimitiveEncryptable](bitwarden_crypto::PrimitiveEncryptable),
Expand Down Expand Up @@ -50,8 +50,8 @@ key_ids! {
Local(LocalId),
}

#[asymmetric]
pub enum AsymmetricKeyId {
#[private]
pub enum PrivateKeyId {
UserPrivateKey,
#[local]
Local(LocalId),
Expand All @@ -64,7 +64,7 @@ key_ids! {
Local(LocalId),
}

pub KeyIds => SymmetricKeyId, AsymmetricKeyId, SigningKeyId;
pub KeyIds => SymmetricKeyId, PrivateKeyId, SigningKeyId;
}

/// This is a helper function to create a test KeyStore with a single user key.
Expand Down
4 changes: 2 additions & 2 deletions crates/bitwarden-core/src/platform/generate_fingerprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use bitwarden_encoding::B64;
use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::{MissingPrivateKeyError, key_management::AsymmetricKeyId};
use crate::{MissingPrivateKeyError, key_management::PrivateKeyId};

/// Request to generate a fingerprint.
#[derive(Serialize, Deserialize, Debug)]
Expand Down Expand Up @@ -66,7 +66,7 @@ pub(crate) fn generate_user_fingerprint(
// FIXME: [PM-18110] This should be removed once the key store can handle public keys and
// fingerprints
#[allow(deprecated)]
let private_key = ctx.dangerous_get_asymmetric_key(AsymmetricKeyId::UserPrivateKey)?;
let private_key = ctx.dangerous_get_private_key(PrivateKeyId::UserPrivateKey)?;

let public_key = private_key.to_public_key().to_der()?;
let fingerprint = fingerprint(&fingerprint_material, &public_key)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ key_ids! {
VaultKey(LocalId)
}

#[asymmetric]
pub enum ExampleAsymmetricKey {
#[private]
pub enum ExamplePrivateKey {
Key(u8),
#[local]
Local(LocalId)
Expand All @@ -104,5 +104,5 @@ key_ids! {
Local(LocalId)
}

pub ExampleIds => ExampleSymmetricKey, ExampleAsymmetricKey, ExampleSigningKey;
pub ExampleIds => ExampleSymmetricKey, ExamplePrivateKey, ExampleSigningKey;
}
6 changes: 3 additions & 3 deletions crates/bitwarden-crypto/examples/seal_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ key_ids! {
ItemKey(LocalId)
}

#[asymmetric]
pub enum ExampleAsymmetricKey {
#[private]
pub enum ExamplePrivateKey {
Key(u8),
#[local]
Local(LocalId),
Expand All @@ -111,5 +111,5 @@ key_ids! {
#[local]
Local(LocalId),
}
pub ExampleIds => ExampleSymmetricKey, ExampleAsymmetricKey, ExampleSigningKey;
pub ExampleIds => ExampleSymmetricKey, ExamplePrivateKey, ExampleSigningKey;
}
Loading
Loading