Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[rust][contacts] tighten root key and attachment APIs

Neeraj Gupta1af565ebfece
diff --git a/mobile/packages/rust/rust/src/api/contacts.rs b/mobile/packages/rust/rust/src/api/contacts.rs
index e3978314c6..2adb9f5af0 100644
--- a/mobile/packages/rust/rust/src/api/contacts.rs
+++ b/mobile/packages/rust/rust/src/api/contacts.rs
@@ -357,14 +357,14 @@ impl ContactsCtx {
.map_err(Into::into)
}
- /// Download an attachment by type and attachment id.
- pub async fn get_attachment(
+ /// Download an encrypted attachment by type and attachment id.
+ pub async fn get_attachment_encrypted(
&self,
attachment_type: AttachmentType,
attachment_id: String,
) -> Result<Vec<u8>, ContactsError> {
self.inner
- .get_attachment(attachment_type.into(), &attachment_id)
+ .get_attachment_encrypted(attachment_type.into(), &attachment_id)
.await
.map_err(Into::into)
}
diff --git a/rust/contacts/src/client.rs b/rust/contacts/src/client.rs
index c6af6d2118..c630e9cca5 100644
--- a/rust/contacts/src/client.rs
+++ b/rust/contacts/src/client.rs
@@ -331,7 +331,7 @@ impl ContactsCtx {
self.decode_contact(response)
}
- pub async fn get_attachment(
+ pub async fn get_attachment_encrypted(
&self,
attachment_type: AttachmentType,
attachment_id: &str,
@@ -370,7 +370,7 @@ impl ContactsCtx {
contacts_crypto::unwrap_contact_key(encrypted_key, &root_contact_key)?
};
let encrypted_picture = self
- .get_attachment(
+ .get_attachment_encrypted(
AttachmentType::ProfilePicture,
current.profile_picture_attachment_id.as_deref().unwrap(),
)
@@ -491,17 +491,14 @@ async fn create_root_key(
match http.post_empty("/user-entity/key", &request).await {
Ok(()) => Ok(None),
- Err(HttpError::Http { .. }) => {
+ Err(HttpError::Http { status, message }) if status == 409 => {
if let Some(remote_root_key) = fetch_root_key(http).await? {
Ok(Some(remote_root_key))
} else {
- Err(HttpError::Http {
- status: 500,
- message: "failed to create root contact key".to_string(),
- }
- .into())
+ Err(HttpError::Http { status, message }.into())
}
}
+ Err(HttpError::Http { status, message }) => Err(HttpError::Http { status, message }.into()),
Err(err) => Err(err.into()),
}
}
diff --git a/rust/contacts/tests/contacts_mock.rs b/rust/contacts/tests/contacts_mock.rs
index e75cc1461c..2f407cecec 100644
--- a/rust/contacts/tests/contacts_mock.rs
+++ b/rust/contacts/tests/contacts_mock.rs
@@ -483,7 +483,7 @@ async fn get_attachment_uses_generic_signed_download_url() {
.ctx;
let downloaded = ctx
- .get_attachment(AttachmentType::ProfilePicture, "ua_generic1")
+ .get_attachment_encrypted(AttachmentType::ProfilePicture, "ua_generic1")
.await
.unwrap();
diff --git a/rust/core/src/crypto/impl_pure/blob.rs b/rust/core/src/crypto/impl_pure/blob.rs
index 0670a2f427..c8d2aa0538 100644
--- a/rust/core/src/crypto/impl_pure/blob.rs
+++ b/rust/core/src/crypto/impl_pure/blob.rs
@@ -162,9 +162,8 @@ pub fn encrypt_json<T: serde::Serialize>(value: &T, key: &[u8]) -> Result<Encryp
/// Encrypt a JSON value and return a combined `header || ciphertext` payload.
pub fn encrypt_json_combined<T: serde::Serialize>(value: &T, key: &[u8]) -> Result<Vec<u8>> {
- let json = serde_json::to_vec(value).map_err(|e| {
- CryptoError::InvalidKeyDerivationParams(format!("JSON serialization failed: {}", e))
- })?;
+ let json = serde_json::to_vec(value)
+ .map_err(|e| CryptoError::Json(format!("JSON serialization failed: {}", e)))?;
encrypt_combined(&json, key)
}
@@ -188,9 +187,8 @@ pub fn decrypt_json_combined<T: serde::de::DeserializeOwned>(
key: &[u8],
) -> Result<T> {
let plaintext = decrypt_combined(combined, key)?;
- serde_json::from_slice(&plaintext).map_err(|e| {
- CryptoError::InvalidKeyDerivationParams(format!("JSON deserialization failed: {}", e))
- })
+ serde_json::from_slice(&plaintext)
+ .map_err(|e| CryptoError::Json(format!("JSON deserialization failed: {}", e)))
}
#[cfg(test)]