Skip to content

Commit b23fcbf

Browse files
committed
Merge branch 'km/data-envelope-follow-up' into km/beeep/safe-data-envelope
2 parents 0ff2877 + d704375 commit b23fcbf

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

crates/bitwarden-crypto/examples/seal_struct.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ fn main() {
5151
.into();
5252

5353
// Seal the item into an encrypted blob, and store the content-encryption-key in the context.
54-
let sealed_item = DataEnvelope::seal(my_item, ExampleSymmetricKey::ItemKey, &mut ctx)
55-
.expect("Sealing should work");
54+
let (sealed_item, cek) = DataEnvelope::seal(my_item, &mut ctx).expect("Sealing should work");
5655

5756
// Store the sealed item on disk
5857
disk.save("sealed_item", (&sealed_item).into());

crates/bitwarden-crypto/src/safe/data_envelope.rs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use thiserror::Error;
1010
use wasm_bindgen::convert::FromWasmAbi;
1111

1212
use crate::{
13-
CONTENT_TYPE_PADDED_CBOR, CoseEncrypt0Bytes, CryptoError, EncodingError, KeyIds,
13+
CONTENT_TYPE_PADDED_CBOR, CoseEncrypt0Bytes, CryptoError, EncString, EncodingError, KeyIds,
1414
SerializedMessage, SymmetricCryptoKey, XChaCha20Poly1305Key,
1515
cose::{DATA_ENVELOPE_NAMESPACE, XCHACHA20_POLY1305},
1616
ensure_equal, ensure_matches,
@@ -71,16 +71,37 @@ impl DataEnvelope {
7171
/// context.
7272
pub fn seal<Ids: KeyIds, T>(
7373
data: T,
74-
cek_keyslot: Ids::Symmetric,
7574
ctx: &mut crate::store::KeyStoreContext<Ids>,
76-
) -> Result<Self, DataEnvelopeError>
75+
) -> Result<(Self, Ids::Symmetric), DataEnvelopeError>
7776
where
7877
T: Serialize + SealableVersionedData,
7978
{
8079
let (envelope, cek) = Self::seal_ref(&data, &T::NAMESPACE)?;
81-
ctx.set_symmetric_key_internal(cek_keyslot, SymmetricCryptoKey::XChaCha20Poly1305Key(cek))
80+
let cek_id = ctx
81+
.generate_symmetric_key()
82+
.map_err(|_| DataEnvelopeError::KeyStoreError)?;
83+
ctx.set_symmetric_key_internal(cek_id, SymmetricCryptoKey::XChaCha20Poly1305Key(cek))
8284
.map_err(|_| DataEnvelopeError::KeyStoreError)?;
83-
Ok(envelope)
85+
Ok((envelope, cek_id))
86+
}
87+
88+
/// Seals a struct into an encrypted blob. The content encryption key is wrapped with the
89+
/// provided wrapping key
90+
pub fn seal_with_wrapping_key<Ids: KeyIds, T>(
91+
data: T,
92+
wrapping_key: &Ids::Symmetric,
93+
ctx: &mut crate::store::KeyStoreContext<Ids>,
94+
) -> Result<(Self, EncString), DataEnvelopeError>
95+
where
96+
T: Serialize + SealableVersionedData,
97+
{
98+
let (envelope, cek) = Self::seal(data, ctx)?;
99+
100+
let wrapped_cek = ctx
101+
.wrap_symmetric_key(*wrapping_key, cek)
102+
.map_err(|_| DataEnvelopeError::EncryptionError)?;
103+
104+
Ok((envelope, wrapped_cek))
84105
}
85106

86107
/// Seals a struct into an encrypted blob, and returns the encrypted blob and the
@@ -159,6 +180,22 @@ impl DataEnvelope {
159180
}
160181
}
161182

183+
/// Unseals the data from the encrypted blob and wrapped content-encryption-key.
184+
pub fn unseal_with_wrapping_key<Ids: KeyIds, T>(
185+
&self,
186+
wrapping_key: &Ids::Symmetric,
187+
wrapped_cek: &EncString,
188+
ctx: &mut crate::store::KeyStoreContext<Ids>,
189+
) -> Result<T, DataEnvelopeError>
190+
where
191+
T: DeserializeOwned + SealableVersionedData,
192+
{
193+
let cek = ctx
194+
.unwrap_symmetric_key(*wrapping_key, wrapped_cek)
195+
.map_err(|_| DataEnvelopeError::DecryptionError)?;
196+
self.unseal(cek, ctx)
197+
}
198+
162199
/// Unseals the data from the encrypted blob using the provided content-encryption-key.
163200
fn unseal_ref<T>(
164201
&self,

0 commit comments

Comments
 (0)