Skip to content

Commit f52e521

Browse files
authored
Add bitwarden-encoding crate with b64 and b64url types (#404)
Provides a generic B64 and B64Url types we can use throughout the sdk.
1 parent ab3c7db commit f52e521

File tree

25 files changed

+714
-53
lines changed

25 files changed

+714
-53
lines changed

Cargo.lock

Lines changed: 41 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ bitwarden-cli = { path = "crates/bitwarden-cli", version = "=1.0.0" }
2626
bitwarden-collections = { path = "crates/bitwarden-collections", version = "=1.0.0" }
2727
bitwarden-core = { path = "crates/bitwarden-core", version = "=1.0.0" }
2828
bitwarden-crypto = { path = "crates/bitwarden-crypto", version = "=1.0.0" }
29+
bitwarden-encoding = { path = "crates/bitwarden-encoding", version = "=1.0.0" }
2930
bitwarden-error = { path = "crates/bitwarden-error", version = "=1.0.0" }
3031
bitwarden-error-macro = { path = "crates/bitwarden-error-macro", version = "=1.0.0" }
3132
bitwarden-exporters = { path = "crates/bitwarden-exporters", version = "=1.0.0" }
@@ -49,6 +50,7 @@ chrono = { version = ">=0.4.26, <0.5", features = [
4950
"serde",
5051
"std",
5152
], default-features = false }
53+
data-encoding = ">=2.0, <3"
5254
js-sys = { version = ">=0.3.72, <0.4" }
5355
log = ">=0.4.18, <0.5"
5456
proc-macro2 = ">=1.0.89, <2"

crates/bitwarden-core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ base64 = ">=0.22.1, <0.23"
3434
bitwarden-api-api = { workspace = true }
3535
bitwarden-api-identity = { workspace = true }
3636
bitwarden-crypto = { workspace = true }
37+
bitwarden-encoding = { workspace = true }
3738
bitwarden-error = { workspace = true }
3839
bitwarden-state = { workspace = true }
3940
bitwarden-uuid = { workspace = true }

crates/bitwarden-core/src/auth/auth_client.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
use bitwarden_crypto::{
33
CryptoError, DeviceKey, EncString, Kdf, TrustDeviceResponse, UnsignedSharedKey,
44
};
5+
#[cfg(feature = "internal")]
6+
use bitwarden_encoding::B64;
57

68
#[cfg(feature = "secrets")]
79
use crate::auth::login::{login_access_token, AccessTokenLoginRequest, AccessTokenLoginResponse};
@@ -88,7 +90,7 @@ impl AuthClient {
8890
pub fn make_register_tde_keys(
8991
&self,
9092
email: String,
91-
org_public_key: String,
93+
org_public_key: B64,
9294
remember_device: bool,
9395
) -> Result<RegisterTdeKeyResponse, EncryptionSettingsError> {
9496
make_register_tde_keys(&self.client, email, org_public_key, remember_device)

crates/bitwarden-core/src/auth/tde.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use base64::{engine::general_purpose::STANDARD, Engine};
21
use bitwarden_crypto::{
32
AsymmetricPublicCryptoKey, DeviceKey, EncString, Kdf, SpkiPublicKeyBytes, SymmetricCryptoKey,
43
TrustDeviceResponse, UnsignedSharedKey, UserKey,
54
};
5+
use bitwarden_encoding::B64;
66

77
use crate::{
88
client::{encryption_settings::EncryptionSettingsError, internal::UserKeyState},
@@ -15,12 +15,11 @@ use crate::{
1515
pub(super) fn make_register_tde_keys(
1616
client: &Client,
1717
email: String,
18-
org_public_key: String,
18+
org_public_key: B64,
1919
remember_device: bool,
2020
) -> Result<RegisterTdeKeyResponse, EncryptionSettingsError> {
21-
let public_key = AsymmetricPublicCryptoKey::from_der(&SpkiPublicKeyBytes::from(
22-
STANDARD.decode(org_public_key)?,
23-
))?;
21+
let public_key =
22+
AsymmetricPublicCryptoKey::from_der(&SpkiPublicKeyBytes::from(org_public_key.as_ref()))?;
2423

2524
let user_key = UserKey::new(SymmetricCryptoKey::make_aes256_cbc_hmac_key());
2625
let key_pair = user_key.make_key_pair()?;

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ use std::str::FromStr;
2424

2525
use base64::{engine::general_purpose::STANDARD, Engine};
2626
use bitwarden_crypto::{
27-
CoseSerializable, CoseSign1Bytes, CryptoError, EncodingError, FromStrVisitor, KeyIds,
28-
KeyStoreContext, SignedObject, SigningNamespace, VerifyingKey,
27+
CoseSerializable, CoseSign1Bytes, CryptoError, EncodingError, KeyIds, KeyStoreContext,
28+
SignedObject, SigningNamespace, VerifyingKey,
2929
};
30+
use bitwarden_encoding::FromStrVisitor;
3031
use serde::{Deserialize, Serialize};
3132

3233
use crate::UserId;

crates/bitwarden-crypto/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ argon2 = { version = ">=0.5.0, <0.6", features = [
2828
"zeroize",
2929
], default-features = false }
3030
base64 = ">=0.22.1, <0.23"
31+
bitwarden-encoding = { workspace = true }
3132
bitwarden-error = { workspace = true }
3233
cbc = { version = ">=0.1.2, <0.2", features = ["alloc", "zeroize"] }
3334
chacha20poly1305 = { version = "0.10.1" }
@@ -46,7 +47,7 @@ rayon = ">=1.8.1, <2.0"
4647
rsa = ">=0.9.2, <0.10"
4748
schemars = { workspace = true }
4849
serde = { workspace = true }
49-
serde_bytes = { workspace = true }
50+
serde_bytes = { workspace = true }
5051
serde_repr.workspace = true
5152
sha1 = ">=0.10.5, <0.11"
5253
sha2 = ">=0.10.6, <0.11"

crates/bitwarden-crypto/src/enc_string/asymmetric.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::{borrow::Cow, fmt::Display, str::FromStr};
22

33
use base64::{engine::general_purpose::STANDARD, Engine};
4+
use bitwarden_encoding::FromStrVisitor;
45
pub use internal::UnsignedSharedKey;
56
use rsa::Oaep;
67
use serde::Deserialize;
@@ -9,7 +10,6 @@ use super::{from_b64_vec, split_enc_string};
910
use crate::{
1011
error::{CryptoError, EncStringParseError, Result},
1112
rsa::encrypt_rsa2048_oaep_sha1,
12-
util::FromStrVisitor,
1313
AsymmetricCryptoKey, AsymmetricPublicCryptoKey, BitwardenLegacyKeyBytes, RawPrivateKey,
1414
RawPublicKey, SymmetricCryptoKey,
1515
};

crates/bitwarden-crypto/src/enc_string/symmetric.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use std::{borrow::Cow, str::FromStr};
22

33
use base64::{engine::general_purpose::STANDARD, Engine};
4+
use bitwarden_encoding::FromStrVisitor;
45
use coset::CborSerializable;
56
use serde::Deserialize;
67

78
use super::{check_length, from_b64, from_b64_vec, split_enc_string};
89
use crate::{
910
error::{CryptoError, EncStringParseError, Result, UnsupportedOperation},
10-
util::FromStrVisitor,
1111
Aes256CbcHmacKey, ContentFormat, KeyDecryptable, KeyEncryptable, KeyEncryptableWithContentType,
1212
SymmetricCryptoKey, Utf8Bytes, XChaCha20Poly1305Key,
1313
};

crates/bitwarden-crypto/src/keys/signed_public_key.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
use std::{borrow::Cow, str::FromStr};
66

77
use base64::{engine::general_purpose::STANDARD, Engine};
8+
use bitwarden_encoding::FromStrVisitor;
89
use serde::{Deserialize, Serialize};
910
use serde_bytes::ByteBuf;
1011
use serde_repr::{Deserialize_repr, Serialize_repr};
1112

1213
use super::AsymmetricPublicCryptoKey;
1314
use crate::{
14-
cose::CoseSerializable, error::EncodingError, util::FromStrVisitor, CoseSign1Bytes,
15-
CryptoError, PublicKeyEncryptionAlgorithm, RawPublicKey, SignedObject, SigningKey,
16-
SigningNamespace, SpkiPublicKeyBytes, VerifyingKey,
15+
cose::CoseSerializable, error::EncodingError, CoseSign1Bytes, CryptoError,
16+
PublicKeyEncryptionAlgorithm, RawPublicKey, SignedObject, SigningKey, SigningNamespace,
17+
SpkiPublicKeyBytes, VerifyingKey,
1718
};
1819

1920
#[cfg(feature = "wasm")]

0 commit comments

Comments
 (0)