Skip to content
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

Rename AsymmEncString to AsymmetricEncString #510

Merged
merged 1 commit into from
Jan 16, 2024
Merged
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
84 changes: 42 additions & 42 deletions crates/bitwarden-crypto/src/enc_string/asymmetric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@

/// # Encrypted string primitive
///
/// [AsymmEncString] is a Bitwarden specific primitive that represents an asymmetrically encrypted string. They are
/// are used together with the KeyDecryptable and KeyEncryptable traits to encrypt and decrypt
/// data using [AsymmetricCryptoKey]s.
/// [AsymmetricEncString] is a Bitwarden specific primitive that represents an asymmetrically encrypted string.
/// They are used together with the KeyDecryptable and KeyEncryptable traits to encrypt and decrypt data using
/// [AsymmetricCryptoKey]s.
///
/// The flexibility of the [AsymmEncString] type allows for different encryption algorithms to be used
/// The flexibility of the [AsymmetricEncString] type allows for different encryption algorithms to be used
/// which is represented by the different variants of the enum.
///
/// ## Note
Expand All @@ -25,12 +25,12 @@
/// variants, but we should be opinionated in which variants are used for encrypting.
///
/// ## Variants
/// - [Rsa2048_OaepSha256_B64](AsymmEncString::Rsa2048_OaepSha256_B64)
/// - [Rsa2048_OaepSha1_B64](AsymmEncString::Rsa2048_OaepSha1_B64)
/// - [Rsa2048_OaepSha256_B64](AsymmetricEncString::Rsa2048_OaepSha256_B64)
/// - [Rsa2048_OaepSha1_B64](AsymmetricEncString::Rsa2048_OaepSha1_B64)
///
/// ## Serialization
///
/// [AsymmEncString] implements [Display] and [FromStr] to allow for easy serialization and uses a
/// [AsymmetricEncString] implements [Display] and [FromStr] to allow for easy serialization and uses a
/// custom scheme to represent the different variants.
///
/// The scheme is one of the following schemes:
Expand All @@ -41,7 +41,7 @@
/// - `[data]`: is the encrypted data.
#[derive(Clone)]
#[allow(unused, non_camel_case_types)]
pub enum AsymmEncString {
pub enum AsymmetricEncString {
/// 3
Rsa2048_OaepSha256_B64 { data: Vec<u8> },
/// 4
Expand All @@ -54,39 +54,39 @@
Rsa2048_OaepSha1_HmacSha256_B64 { data: Vec<u8>, mac: Vec<u8> },
}

/// To avoid printing sensitive information, [AsymmEncString] debug prints to `AsymmEncString`.
impl std::fmt::Debug for AsymmEncString {
/// To avoid printing sensitive information, [AsymmetricEncString] debug prints to `AsymmetricEncString`.
impl std::fmt::Debug for AsymmetricEncString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AsymmEncString").finish()
f.debug_struct("AsymmetricEncString").finish()

Check warning on line 60 in crates/bitwarden-crypto/src/enc_string/asymmetric.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L60 was not covered by tests
}
}

/// Deserializes an [AsymmEncString] from a string.
impl FromStr for AsymmEncString {
/// Deserializes an [AsymmetricEncString] from a string.
impl FromStr for AsymmetricEncString {
type Err = CryptoError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let (enc_type, parts) = split_enc_string(s);
match (enc_type, parts.len()) {
("3", 1) => {
let data = from_b64_vec(parts[0])?;
Ok(AsymmEncString::Rsa2048_OaepSha256_B64 { data })
Ok(AsymmetricEncString::Rsa2048_OaepSha256_B64 { data })
}
("4", 1) => {
let data = from_b64_vec(parts[0])?;
Ok(AsymmEncString::Rsa2048_OaepSha1_B64 { data })
Ok(AsymmetricEncString::Rsa2048_OaepSha1_B64 { data })
}
#[allow(deprecated)]
("5", 2) => {
let data = from_b64_vec(parts[0])?;
let mac: Vec<u8> = from_b64_vec(parts[1])?;
Ok(AsymmEncString::Rsa2048_OaepSha256_HmacSha256_B64 { data, mac })
Ok(AsymmetricEncString::Rsa2048_OaepSha256_HmacSha256_B64 { data, mac })

Check warning on line 83 in crates/bitwarden-crypto/src/enc_string/asymmetric.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L83 was not covered by tests
}
#[allow(deprecated)]
("6", 2) => {
let data = from_b64_vec(parts[0])?;
let mac: Vec<u8> = from_b64_vec(parts[1])?;
Ok(AsymmEncString::Rsa2048_OaepSha1_HmacSha256_B64 { data, mac })
Ok(AsymmetricEncString::Rsa2048_OaepSha1_HmacSha256_B64 { data, mac })
}

(enc_type, parts) => Err(EncStringParseError::InvalidTypeAsymm {
Expand All @@ -98,15 +98,15 @@
}
}

impl Display for AsymmEncString {
impl Display for AsymmetricEncString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let parts: Vec<&[u8]> = match self {
AsymmEncString::Rsa2048_OaepSha256_B64 { data } => vec![data],
AsymmEncString::Rsa2048_OaepSha1_B64 { data } => vec![data],
AsymmetricEncString::Rsa2048_OaepSha256_B64 { data } => vec![data],
AsymmetricEncString::Rsa2048_OaepSha1_B64 { data } => vec![data],

Check warning on line 105 in crates/bitwarden-crypto/src/enc_string/asymmetric.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-crypto/src/enc_string/asymmetric.rs#L104-L105

Added lines #L104 - L105 were not covered by tests
#[allow(deprecated)]
AsymmEncString::Rsa2048_OaepSha256_HmacSha256_B64 { data, mac } => vec![data, mac],
AsymmetricEncString::Rsa2048_OaepSha256_HmacSha256_B64 { data, mac } => vec![data, mac],

Check warning on line 107 in crates/bitwarden-crypto/src/enc_string/asymmetric.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L107 was not covered by tests
#[allow(deprecated)]
AsymmEncString::Rsa2048_OaepSha1_HmacSha256_B64 { data, mac } => vec![data, mac],
AsymmetricEncString::Rsa2048_OaepSha1_HmacSha256_B64 { data, mac } => vec![data, mac],
};

let encoded_parts: Vec<String> = parts.iter().map(|part| STANDARD.encode(part)).collect();
Expand All @@ -117,7 +117,7 @@
}
}

impl<'de> Deserialize<'de> for AsymmEncString {
impl<'de> Deserialize<'de> for AsymmetricEncString {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
Expand All @@ -126,7 +126,7 @@
}
}

impl serde::Serialize for AsymmEncString {
impl serde::Serialize for AsymmetricEncString {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
Expand All @@ -135,23 +135,23 @@
}
}

impl AsymmEncString {
/// The numerical representation of the encryption type of the [AsymmEncString].
impl AsymmetricEncString {
/// The numerical representation of the encryption type of the [AsymmetricEncString].
const fn enc_type(&self) -> u8 {
match self {
AsymmEncString::Rsa2048_OaepSha256_B64 { .. } => 3,
AsymmEncString::Rsa2048_OaepSha1_B64 { .. } => 4,
AsymmetricEncString::Rsa2048_OaepSha256_B64 { .. } => 3,
AsymmetricEncString::Rsa2048_OaepSha1_B64 { .. } => 4,
#[allow(deprecated)]
AsymmEncString::Rsa2048_OaepSha256_HmacSha256_B64 { .. } => 5,
AsymmetricEncString::Rsa2048_OaepSha256_HmacSha256_B64 { .. } => 5,

Check warning on line 145 in crates/bitwarden-crypto/src/enc_string/asymmetric.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L145 was not covered by tests
#[allow(deprecated)]
AsymmEncString::Rsa2048_OaepSha1_HmacSha256_B64 { .. } => 6,
AsymmetricEncString::Rsa2048_OaepSha1_HmacSha256_B64 { .. } => 6,
}
}
}

impl KeyDecryptable<AsymmetricCryptoKey, Vec<u8>> for AsymmEncString {
impl KeyDecryptable<AsymmetricCryptoKey, Vec<u8>> for AsymmetricEncString {
fn decrypt_with_key(&self, key: &AsymmetricCryptoKey) -> Result<Vec<u8>> {
use AsymmEncString::*;
use AsymmetricEncString::*;
match self {
Rsa2048_OaepSha256_B64 { data } => key.key.decrypt(Oaep::new::<sha2::Sha256>(), data),
Rsa2048_OaepSha1_B64 { data } => key.key.decrypt(Oaep::new::<sha1::Sha1>(), data),
Expand All @@ -168,18 +168,18 @@
}
}

impl KeyDecryptable<AsymmetricCryptoKey, String> for AsymmEncString {
impl KeyDecryptable<AsymmetricCryptoKey, String> for AsymmetricEncString {
fn decrypt_with_key(&self, key: &AsymmetricCryptoKey) -> Result<String> {
let dec: Vec<u8> = self.decrypt_with_key(key)?;
String::from_utf8(dec).map_err(|_| CryptoError::InvalidUtf8String)
}
}

/// Usually we wouldn't want to expose AsymmEncStrings in the API or the schemas.
/// But during the transition phase we will expose endpoints using the AsymmEncString type.
impl schemars::JsonSchema for AsymmEncString {
/// Usually we wouldn't want to expose AsymmetricEncStrings in the API or the schemas.
/// But during the transition phase we will expose endpoints using the AsymmetricEncString type.
impl schemars::JsonSchema for AsymmetricEncString {
fn schema_name() -> String {
"AsymmEncString".to_string()
"AsymmetricEncString".to_string()

Check warning on line 182 in crates/bitwarden-crypto/src/enc_string/asymmetric.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L182 was not covered by tests
}

fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
Expand All @@ -189,7 +189,7 @@

#[cfg(test)]
mod tests {
use super::{AsymmEncString, AsymmetricCryptoKey, KeyDecryptable};
use super::{AsymmetricCryptoKey, AsymmetricEncString, KeyDecryptable};

const RSA_PRIVATE_KEY: &str = "-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCXRVrCX+2hfOQS
Expand Down Expand Up @@ -224,7 +224,7 @@
fn test_enc_string_rsa2048_oaep_sha256_b64() {
let private_key = AsymmetricCryptoKey::from_pem(RSA_PRIVATE_KEY).unwrap();
let enc_str: &str = "3.YFqzW9LL/uLjCnl0RRLtndzGJ1FV27mcwQwGjfJPOVrgCX9nJSUYCCDd0iTIyOZ/zRxG47b6L1Z3qgkEfcxjmrSBq60gijc3E2TBMAg7OCLVcjORZ+i1sOVOudmOPWro6uA8refMrg4lqbieDlbLMzjVEwxfi5WpcL876cD0vYyRwvLO3bzFrsE7x33HHHtZeOPW79RqMn5efsB5Dj9wVheC9Ix9AYDjbo+rjg9qR6guwKmS7k2MSaIQlrDR7yu8LP+ePtiSjx+gszJV5jQGfcx60dtiLQzLS/mUD+RmU7B950Bpx0H7x56lT5yXZbWK5YkoP6qd8B8D2aKbP68Ywg==";
let enc_string: AsymmEncString = enc_str.parse().unwrap();
let enc_string: AsymmetricEncString = enc_str.parse().unwrap();

assert_eq!(enc_string.enc_type(), 3);

Expand All @@ -236,7 +236,7 @@
fn test_enc_string_rsa2048_oaep_sha1_b64() {
let private_key = AsymmetricCryptoKey::from_pem(RSA_PRIVATE_KEY).unwrap();
let enc_str: &str = "4.ZheRb3PCfAunyFdQYPfyrFqpuvmln9H9w5nDjt88i5A7ug1XE0LJdQHCIYJl0YOZ1gCOGkhFu/CRY2StiLmT3iRKrrVBbC1+qRMjNNyDvRcFi91LWsmRXhONVSPjywzrJJXglsztDqGkLO93dKXNhuKpcmtBLsvgkphk/aFvxbaOvJ/FHdK/iV0dMGNhc/9tbys8laTdwBlI5xIChpRcrfH+XpSFM88+Bu03uK67N9G6eU1UmET+pISJwJvMuIDMqH+qkT7OOzgL3t6I0H2LDj+CnsumnQmDsvQzDiNfTR0IgjpoE9YH2LvPXVP2wVUkiTwXD9cG/E7XeoiduHyHjw==";
let enc_string: AsymmEncString = enc_str.parse().unwrap();
let enc_string: AsymmetricEncString = enc_str.parse().unwrap();

assert_eq!(enc_string.enc_type(), 4);

Expand All @@ -248,7 +248,7 @@
fn test_enc_string_rsa2048_oaep_sha1_hmac_sha256_b64() {
let private_key = AsymmetricCryptoKey::from_pem(RSA_PRIVATE_KEY).unwrap();
let enc_str: &str = "6.ThnNc67nNr7GELyuhGGfsXNP2zJnNqhrIsjntEQ27r2qmn8vwdHbTbfO0cwt6YgSibDN0PjiCZ1O3Wb/IFq+vwvyRwFqF9145wBF8CQCbkhV+M0XvO99kh0daovtt120Nve/5ETI5PbPag9VdalKRQWZypJaqQHm5TAQVf4F5wtLlCLMBkzqTk+wkFe7BPMTGn07T+O3eJbTxXvyMZewQ7icJF0MZVA7VyWX9qElmZ89FCKowbf1BMr5pbcQ+0KdXcSVW3to43VkTp7k7COwsuH3M/i1AuVP5YN8ixjyRpvaeGqX/ap2nCHK2Wj5VxgCGT7XEls6ZknnAp9nB9qVjQ==|s3ntw5H/KKD/qsS0lUghTHl5Sm9j6m7YEdNHf0OeAFQ=";
let enc_string: AsymmEncString = enc_str.parse().unwrap();
let enc_string: AsymmetricEncString = enc_str.parse().unwrap();

assert_eq!(enc_string.enc_type(), 6);

Expand All @@ -260,7 +260,7 @@
fn test_enc_string_serialization() {
#[derive(serde::Serialize, serde::Deserialize)]
struct Test {
key: AsymmEncString,
key: AsymmetricEncString,
}

let cipher = "6.ThnNc67nNr7GELyuhGGfsXNP2zJnNqhrIsjntEQ27r2qmn8vwdHbTbfO0cwt6YgSibDN0PjiCZ1O3Wb/IFq+vwvyRwFqF9145wBF8CQCbkhV+M0XvO99kh0daovtt120Nve/5ETI5PbPag9VdalKRQWZypJaqQHm5TAQVf4F5wtLlCLMBkzqTk+wkFe7BPMTGn07T+O3eJbTxXvyMZewQ7icJF0MZVA7VyWX9qElmZ89FCKowbf1BMr5pbcQ+0KdXcSVW3to43VkTp7k7COwsuH3M/i1AuVP5YN8ixjyRpvaeGqX/ap2nCHK2Wj5VxgCGT7XEls6ZknnAp9nB9qVjQ==|s3ntw5H/KKD/qsS0lUghTHl5Sm9j6m7YEdNHf0OeAFQ=";
Expand Down
2 changes: 1 addition & 1 deletion crates/bitwarden-crypto/src/enc_string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod symmetric;

use std::str::FromStr;

pub use asymmetric::AsymmEncString;
pub use asymmetric::AsymmetricEncString;
use base64::{engine::general_purpose::STANDARD, Engine};
pub use symmetric::EncString;

Expand Down
4 changes: 2 additions & 2 deletions crates/bitwarden-crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! This crate contains the cryptographic primitives used throughout the SDK. The crate makes a
//! best effort to abstract away cryptographic concepts into concepts such as [`EncString`],
//! [`AsymmEncString`] and [`SymmetricCryptoKey`].
//! [`AsymmetricEncString`] and [`SymmetricCryptoKey`].
//!
//! ## Conventions:
//!
Expand All @@ -23,7 +23,7 @@

mod aes;
mod enc_string;
pub use enc_string::{AsymmEncString, EncString};
pub use enc_string::{AsymmetricEncString, EncString};
mod encryptable;
pub use encryptable::{Decryptable, Encryptable, KeyContainer, LocateKey};
mod error;
Expand Down
4 changes: 2 additions & 2 deletions crates/bitwarden/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
pub use bitwarden_crypto::Kdf;
use bitwarden_crypto::SymmetricCryptoKey;
#[cfg(feature = "internal")]
use bitwarden_crypto::{AsymmEncString, EncString};
use bitwarden_crypto::{AsymmetricEncString, EncString};
use chrono::Utc;
use reqwest::header::{self};
use uuid::Uuid;
Expand Down Expand Up @@ -280,7 +280,7 @@
#[cfg(feature = "internal")]
pub(crate) fn initialize_org_crypto(
&mut self,
org_keys: Vec<(Uuid, AsymmEncString)>,
org_keys: Vec<(Uuid, AsymmetricEncString)>,

Check warning on line 283 in crates/bitwarden/src/client/client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/client/client.rs#L283

Added line #L283 was not covered by tests
) -> Result<&EncryptionSettings> {
let enc = self
.encryption_settings
Expand Down
6 changes: 3 additions & 3 deletions crates/bitwarden/src/client/encryption_settings.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::collections::HashMap;

#[cfg(feature = "internal")]
use bitwarden_crypto::{AsymmEncString, EncString};
use bitwarden_crypto::{AsymmetricCryptoKey, KeyContainer, SymmetricCryptoKey};
#[cfg(feature = "internal")]
use bitwarden_crypto::{AsymmetricEncString, EncString};
use uuid::Uuid;

#[cfg(feature = "internal")]
Expand Down Expand Up @@ -80,7 +80,7 @@
#[cfg(feature = "internal")]
pub(crate) fn set_org_keys(
&mut self,
org_enc_keys: Vec<(Uuid, AsymmEncString)>,
org_enc_keys: Vec<(Uuid, AsymmetricEncString)>,

Check warning on line 83 in crates/bitwarden/src/client/encryption_settings.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/client/encryption_settings.rs#L83

Added line #L83 was not covered by tests
) -> Result<&mut Self> {
use bitwarden_crypto::KeyDecryptable;

Expand Down
4 changes: 2 additions & 2 deletions crates/bitwarden/src/mobile/crypto.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

use bitwarden_crypto::{AsymmEncString, EncString};
use bitwarden_crypto::{AsymmetricEncString, EncString};
#[cfg(feature = "internal")]
use bitwarden_crypto::{KeyDecryptable, KeyEncryptable, MasterKey, SymmetricCryptoKey};
use schemars::JsonSchema;
Expand Down Expand Up @@ -89,7 +89,7 @@
#[cfg_attr(feature = "mobile", derive(uniffi::Record))]
pub struct InitOrgCryptoRequest {
/// The encryption keys for all the organizations the user is a part of
pub organization_keys: HashMap<uuid::Uuid, AsymmEncString>,
pub organization_keys: HashMap<uuid::Uuid, AsymmetricEncString>,
}

#[cfg(feature = "internal")]
Expand Down Expand Up @@ -165,9 +165,9 @@
let derived_key = match login_method {
LoginMethod::User(
UserLoginMethod::Username { email, kdf, .. }
| UserLoginMethod::ApiKey { email, kdf, .. },

Check warning on line 168 in crates/bitwarden/src/mobile/crypto.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/mobile/crypto.rs#L168

Added line #L168 was not covered by tests
) => MasterKey::derive(pin.as_bytes(), email.as_bytes(), kdf)?,
_ => return Err(Error::NotAuthenticated),

Check warning on line 170 in crates/bitwarden/src/mobile/crypto.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/mobile/crypto.rs#L170

Added line #L170 was not covered by tests
};

Ok(derived_key.encrypt_user_key(user_key)?)
Expand Down Expand Up @@ -257,7 +257,7 @@
},
},
)
.await

Check warning on line 260 in crates/bitwarden/src/mobile/crypto.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/mobile/crypto.rs#L260

Added line #L260 was not covered by tests
.unwrap();

assert_eq!(
Expand Down
6 changes: 3 additions & 3 deletions crates/bitwarden/src/uniffi_support.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use std::{num::NonZeroU32, str::FromStr};

use bitwarden_crypto::{AsymmEncString, EncString};
use bitwarden_crypto::{AsymmetricEncString, EncString};
use uuid::Uuid;

use crate::UniffiCustomTypeConverter;

uniffi::ffi_converter_forward!(NonZeroU32, bitwarden_crypto::UniFfiTag, crate::UniFfiTag);
uniffi::ffi_converter_forward!(EncString, bitwarden_crypto::UniFfiTag, crate::UniFfiTag);

uniffi::custom_type!(AsymmEncString, String);
uniffi::custom_type!(AsymmetricEncString, String);

Check warning on line 11 in crates/bitwarden/src/uniffi_support.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/uniffi_support.rs#L11

Added line #L11 was not covered by tests

impl UniffiCustomTypeConverter for AsymmEncString {
impl UniffiCustomTypeConverter for AsymmetricEncString {
type Builtin = String;

fn into_custom(val: Self::Builtin) -> uniffi::Result<Self> {
Expand Down
Loading