Skip to content

Commit 67dd5e9

Browse files
committed
Move content format to separate file
1 parent b9b0f6e commit 67dd5e9

File tree

5 files changed

+35
-33
lines changed

5 files changed

+35
-33
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use serde::{Deserialize, Serialize};
2+
#[cfg(feature = "wasm")]
3+
use tsify_next::Tsify;
4+
5+
/// The content format describes the format of the contained bytes. Message encryption always
6+
/// happens on the byte level, and this allows determining what format the contained data has. For
7+
/// instance, an `EncString` in most cases contains UTF-8 encoded text. In some cases it may contain
8+
/// a Pkcs8 private key, or a COSE key. Specifically, for COSE keys, this allows distinguishing
9+
/// between the old symmetric key format, represented as `ContentFormat::OctetStream`, and the new
10+
/// COSE key format, represented as `ContentFormat::CoseKey`.
11+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
12+
#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
13+
pub enum ContentFormat {
14+
/// UTF-8 encoded text
15+
Utf8,
16+
/// Pkcs8 private key DER
17+
Pkcs8,
18+
/// COSE serialized CoseKey
19+
CoseKey,
20+
/// Bitwarden Legacy Key
21+
/// There are three permissible byte values here:
22+
/// - [u8; 32] - AES-CBC (no hmac) key. This is to be removed and banned.
23+
/// - [u8; 64] - AES-CBC with HMAC key. This is the v1 userkey key type
24+
/// - [u8; >64] - COSE key. Padded to be larger than 64 bytes.
25+
BitwardenLegacyKey,
26+
/// Stream of bytes
27+
OctetStream,
28+
}

crates/bitwarden-crypto/src/cose.rs

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use typenum::U32;
1515

1616
use crate::{
1717
error::{EncStringParseError, EncodingError},
18-
xchacha20, CryptoError, SymmetricCryptoKey, XChaCha20Poly1305Key,
18+
xchacha20, ContentFormat, CryptoError, SymmetricCryptoKey, XChaCha20Poly1305Key,
1919
};
2020

2121
/// XChaCha20 <https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-xchacha-03> is used over ChaCha20
@@ -29,31 +29,6 @@ const XCHACHA20_TEXT_PAD_BLOCK_SIZE: usize = 32;
2929
const CONTENT_TYPE_PADDED_UTF8: &str = "application/x.bitwarden.utf8-padded";
3030
const CONTENT_TYPE_BITWARDEN_LEGACY_KEY: &str = "application/x.bitwarden.legacy-key";
3131

32-
/// The content format describes the format of the contained bytes. Message encryption always
33-
/// happens on the byte level, and this allows determining what format the contained data has. For
34-
/// instance, an `EncString` in most cases contains UTF-8 encoded text. In some cases it may contain
35-
/// a Pkcs8 private key, or a COSE key. Specifically, for COSE keys, this allows distinguishing
36-
/// between the old symmetric key format, represented as `ContentFormat::OctetStream`, and the new
37-
/// COSE key format, represented as `ContentFormat::CoseKey`.
38-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
39-
#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
40-
pub enum ContentFormat {
41-
/// UTF-8 encoded text
42-
Utf8,
43-
/// Pkcs8 private key DER
44-
Pkcs8,
45-
/// COSE serialized CoseKey
46-
CoseKey,
47-
/// Bitwarden Legacy Key
48-
/// There are three permissible byte values here:
49-
/// - [u8; 32] - AES-CBC (no hmac) key. This is to be removed and banned.
50-
/// - [u8; 64] - AES-CBC with HMAC key. This is the v1 userkey key type
51-
/// - [u8; >64] - COSE key. Padded to be larger than 64 bytes.
52-
BitwardenLegacyKey,
53-
/// Stream of bytes
54-
OctetStream,
55-
}
56-
5732
// Labels
5833
//
5934
/// The label used for the namespace ensuring strong domain separation when using signatures.
@@ -208,9 +183,7 @@ impl TryFrom<&coset::Header> for ContentFormat {
208183
Some(ContentType::Text(format)) if format == CONTENT_TYPE_PADDED_UTF8 => {
209184
Ok(ContentFormat::Utf8)
210185
}
211-
Some(ContentType::Text(format)) if format == CONTENT_TYPE_BITWARDEN_LEGACY_KEY => {
212-
Ok(ContentFormat::BitwardenLegacyKey)
213-
}
186+
214187
Some(ContentType::Assigned(CoapContentFormat::Pkcs8)) => Ok(ContentFormat::Pkcs8),
215188
Some(ContentType::Assigned(CoapContentFormat::CoseKey)) => Ok(ContentFormat::CoseKey),
216189
Some(ContentType::Assigned(CoapContentFormat::OctetStream)) => {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{collections::HashMap, hash::Hash, sync::Arc};
33
use rayon::prelude::*;
44
use uuid::Uuid;
55

6-
use crate::{cose::ContentFormat, error::Result, CryptoError, SymmetricCryptoKey};
6+
use crate::{error::Result, ContentFormat, CryptoError, SymmetricCryptoKey};
77

88
#[allow(missing_docs)]
99
pub trait KeyContainer: Send + Sync {

crates/bitwarden-crypto/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
static ALLOC: ZeroizingAllocator<std::alloc::System> = ZeroizingAllocator(std::alloc::System);
1414

1515
mod aes;
16+
mod content_format;
17+
pub use content_format::ContentFormat;
1618
mod enc_string;
1719
pub use enc_string::{EncString, UnsignedSharedKey};
1820
mod error;
@@ -35,7 +37,6 @@ pub use cose::CoseSerializable;
3537
mod signing;
3638
pub use signing::*;
3739
mod traits;
38-
pub use cose::ContentFormat;
3940
mod xchacha20;
4041
pub use traits::{
4142
CompositeEncryptable, Decryptable, IdentifyKey, KeyId, KeyIds, PrimitiveEncryptable,

crates/bitwarden-crypto/src/traits/encryptable.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ impl<Ids: KeyIds, Key: KeyId, T: PrimitiveEncryptable<Ids, Key, Output>, Output>
138138
&self,
139139
ctx: &mut KeyStoreContext<Ids>,
140140
key: Key,
141-
content_format: crate::cose::ContentFormat,
141+
content_format: crate::ContentFormat,
142142
) -> Result<Option<Output>, CryptoError> {
143143
self.as_ref()
144144
.map(|value| value.encrypt(ctx, key, content_format))
@@ -164,7 +164,7 @@ impl<Ids: KeyIds, Key: KeyId, T: PrimitiveEncryptable<Ids, Key, Output>, Output>
164164
#[cfg(test)]
165165
mod tests {
166166
use crate::{
167-
cose::ContentFormat, traits::tests::*, AsymmetricCryptoKey, Decryptable, KeyStore,
167+
traits::tests::*, AsymmetricCryptoKey, ContentFormat, Decryptable, KeyStore,
168168
PrimitiveEncryptable, PrimitiveEncryptableWithContentType, PublicKeyEncryptionAlgorithm,
169169
SymmetricCryptoKey,
170170
};

0 commit comments

Comments
 (0)