Skip to content

Commit dcd4862

Browse files
committed
Cleanup
1 parent 015d8d7 commit dcd4862

File tree

4 files changed

+51
-19
lines changed

4 files changed

+51
-19
lines changed

crates/bitwarden-core/src/client/encryption_settings.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use bitwarden_crypto::{
2-
security_state::SignedSecurityState, CoseKeyBytes, CryptoError, Pkcs8PrivateKeyBytes,
3-
};
1+
use bitwarden_crypto::{security_state::SignedSecurityState, CryptoError, Pkcs8PrivateKeyBytes};
42
#[cfg(feature = "internal")]
53
use bitwarden_crypto::{EncString, UnsignedSharedKey};
64
#[cfg(any(feature = "internal", feature = "secrets"))]
@@ -73,7 +71,9 @@ impl EncryptionSettings {
7371
// For v2 users, we mandate the signing key and security state to be present
7472
// The private key must also be valid.
7573

76-
use bitwarden_crypto::{security_state::SecurityState, CoseSerializable, SigningKey};
74+
use bitwarden_crypto::{
75+
security_state::SecurityState, CoseKeyBytes, CoseSerializable, SigningKey,
76+
};
7777

7878
// Both of these are required for v2 users
7979
let signing_key = signing_key.ok_or(EncryptionSettingsError::Crypto(
@@ -87,11 +87,12 @@ impl EncryptionSettings {
8787

8888
// Everything MUST decrypt.
8989
let signing_key: Vec<u8> = signing_key.decrypt_with_key(&user_key)?;
90-
let signing_key = SigningKey::from_cose(&signing_key)
90+
let signing_key = SigningKey::from_cose(&CoseKeyBytes::from(signing_key))
9191
.map_err(|_| EncryptionSettingsError::InvalidSigningKey)?;
9292
let private_key: Vec<u8> = private_key.decrypt_with_key(&user_key)?;
93-
let private_key = AsymmetricCryptoKey::from_der(&private_key)
94-
.map_err(|_| EncryptionSettingsError::InvalidPrivateKey)?;
93+
let private_key =
94+
AsymmetricCryptoKey::from_der(&Pkcs8PrivateKeyBytes::from(private_key))
95+
.map_err(|_| EncryptionSettingsError::InvalidPrivateKey)?;
9596
let _security_state: SecurityState = security_state
9697
.verify_and_unwrap(&signing_key.to_verifying_key())
9798
.map_err(|_| EncryptionSettingsError::InvalidSecurityState)?;
@@ -111,7 +112,7 @@ impl EncryptionSettings {
111112

112113
// FIXME: [PM-11690] - Temporarily ignore invalid private keys until we have a
113114
// recovery process in place.
114-
AsymmetricCryptoKey::from_der(&dec)
115+
AsymmetricCryptoKey::from_der(&Pkcs8PrivateKeyBytes::from(dec))
115116
.map_err(|_| {
116117
warn!("Invalid private key");
117118
})

crates/bitwarden-core/src/key_management/crypto.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ use std::collections::HashMap;
99
use base64::{engine::general_purpose::STANDARD, Engine};
1010
use bitwarden_crypto::{
1111
security_state::SignedSecurityState, AsymmetricCryptoKey, CoseSerializable, CryptoError,
12-
EncString, Encryptable, Kdf, KeyDecryptable, KeyEncryptable, MasterKey, RotateUserKeysResponse,
13-
SignatureAlgorithm, SignedPublicKey, SigningKey, SymmetricCryptoKey, UnsignedSharedKey,
14-
UserKey,
12+
EncString, Kdf, KeyDecryptable, KeyEncryptable, MasterKey, Pkcs8PrivateKeyBytes,
13+
PrimitiveEncryptable, RotatedUserKeys, SignatureAlgorithm, SignedPublicKey, SigningKey,
14+
SymmetricCryptoKey, UnsignedSharedKey, UserKey,
1515
};
1616
use bitwarden_error::bitwarden_error;
1717
use schemars::JsonSchema;
@@ -544,9 +544,9 @@ pub(super) fn verify_asymmetric_keys(
544544
.decrypt_with_key(user_key)
545545
.map_err(VerifyError::DecryptFailed)?;
546546

547-
let decrypted_private_key = Pkcs8PrivateKeyBytes::from(decrypted_private_key);
548-
let private_key = AsymmetricCryptoKey::from_der(&decrypted_private_key)
549-
.map_err(VerifyError::ParseFailed)?;
547+
let private_key =
548+
AsymmetricCryptoKey::from_der(&Pkcs8PrivateKeyBytes::from(decrypted_private_key))
549+
.map_err(VerifyError::ParseFailed)?;
550550

551551
let derived_public_key_vec = private_key
552552
.to_public_key()
@@ -698,10 +698,10 @@ pub struct RotateUserKeysResponse {
698698
impl From<RotatedUserKeys> for RotateUserKeysResponse {
699699
fn from(rotated: RotatedUserKeys) -> Self {
700700
RotateUserKeysResponse {
701-
verifying_key: Base64String::from(rotated.verifying_key.to_vec()).into(),
701+
verifying_key: Base64String::from(rotated.verifying_key).into(),
702702
signing_key: rotated.signing_key,
703-
signed_public_key: Base64String::from(rotated.signed_public_key.to_vec()).into(),
704-
public_key: Base64String::from(rotated.public_key.to_vec()).into(),
703+
signed_public_key: Base64String::from(rotated.signed_public_key).into(),
704+
public_key: Base64String::from(rotated.public_key).into(),
705705
private_key: rotated.private_key,
706706
}
707707
}

crates/bitwarden-core/src/key_management/crypto_client.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ use crate::key_management::crypto::{
1818
use crate::{
1919
client::encryption_settings::EncryptionSettingsError,
2020
key_management::crypto::{
21-
make_keys_for_user_crypto_v2, CryptoClientError, RotateUserKeysResponse,
21+
get_v2_rotated_account_keys, make_keys_for_user_crypto_v2, CryptoClientError,
22+
RotateUserKeysResponse,
2223
},
2324
Client,
2425
};

crates/bitwarden-core/src/types.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
use base64::{engine::general_purpose::STANDARD, Engine};
2-
use bitwarden_crypto::{Pkcs8PrivateKeyBytes, SpkiPublicKeyBytes};
2+
use bitwarden_crypto::{CoseKeyBytes, CoseSign1Bytes, Pkcs8PrivateKeyBytes, SpkiPublicKeyBytes};
33

44
/// A wrapper around a Base64-encoded string that can be used to decode it into a byte vector.
55
/// This is useful for handling Base64-encoded strings in a type-safe manner,
66
/// ensuring that the string is always treated as Base64 data.
77
pub struct Base64String(String);
88

9+
impl Base64String {
10+
fn from_vec(val: Vec<u8>) -> Self {
11+
Base64String(STANDARD.encode(val))
12+
}
13+
}
14+
915
impl From<String> for Base64String {
1016
fn from(val: String) -> Self {
1117
Base64String(val)
@@ -49,3 +55,27 @@ impl TryInto<Pkcs8PrivateKeyBytes> for Base64String {
4955
Ok(Pkcs8PrivateKeyBytes::from(bytes))
5056
}
5157
}
58+
59+
impl From<Pkcs8PrivateKeyBytes> for Base64String {
60+
fn from(val: Pkcs8PrivateKeyBytes) -> Self {
61+
Self::from_vec(val.to_vec())
62+
}
63+
}
64+
65+
impl From<CoseKeyBytes> for Base64String {
66+
fn from(val: CoseKeyBytes) -> Self {
67+
Self::from_vec(val.to_vec())
68+
}
69+
}
70+
71+
impl From<SpkiPublicKeyBytes> for Base64String {
72+
fn from(val: SpkiPublicKeyBytes) -> Self {
73+
Self::from_vec(val.to_vec())
74+
}
75+
}
76+
77+
impl From<CoseSign1Bytes> for Base64String {
78+
fn from(val: CoseSign1Bytes) -> Self {
79+
Self::from_vec(val.to_vec())
80+
}
81+
}

0 commit comments

Comments
 (0)