use super::headers::{AMZ_ENCRYPTION_KMS, SSEC_ALGORITHM_HEADER, SSEC_KEY_HEADER, SSEC_KEY_MD5_HEADER};
use std::collections::HashMap;
pub const INTERNAL_ENCRYPTION_KEY_ID_HEADER: &str = "x-rustfs-encryption-key-id";
pub const INTERNAL_ENCRYPTION_KEY_HEADER: &str = "x-rustfs-encryption-key";
pub const INTERNAL_ENCRYPTION_IV_HEADER: &str = "x-rustfs-encryption-iv";
pub const INTERNAL_ENCRYPTION_ALGORITHM_HEADER: &str = "x-rustfs-encryption-algorithm";
pub const INTERNAL_ENCRYPTION_ORIGINAL_SIZE_HEADER: &str = "x-rustfs-encryption-original-size";
pub const INTERNAL_ENCRYPTION_CONTEXT_HEADER: &str = "x-rustfs-encryption-context";
pub const INTERNAL_ENCRYPTION_TAG_HEADER: &str = "x-rustfs-encryption-tag";
pub const SSEC_ORIGINAL_SIZE_HEADER: &str = "x-amz-server-side-encryption-customer-original-size";
pub const MINIO_INTERNAL_ENCRYPTION_MULTIPART_HEADER: &str = "X-Minio-Internal-Encrypted-Multipart";
pub const MINIO_INTERNAL_ENCRYPTION_IV_HEADER: &str = "X-Minio-Internal-Server-Side-Encryption-Iv";
pub const MINIO_INTERNAL_ENCRYPTION_ALGORITHM_HEADER: &str = "X-Minio-Internal-Server-Side-Encryption-Seal-Algorithm";
pub const MINIO_INTERNAL_ENCRYPTION_SSEC_SEALED_KEY_HEADER: &str = "X-Minio-Internal-Server-Side-Encryption-Sealed-Key";
pub const MINIO_INTERNAL_ENCRYPTION_S3_SEALED_KEY_HEADER: &str = "X-Minio-Internal-Server-Side-Encryption-S3-Sealed-Key";
pub const MINIO_INTERNAL_ENCRYPTION_KMS_SEALED_KEY_HEADER: &str = "X-Minio-Internal-Server-Side-Encryption-Kms-Sealed-Key";
pub const MINIO_INTERNAL_ENCRYPTION_KMS_KEY_ID_HEADER: &str = "X-Minio-Internal-Server-Side-Encryption-S3-Kms-Key-Id";
pub const MINIO_INTERNAL_ENCRYPTION_KMS_DATA_KEY_HEADER: &str = "X-Minio-Internal-Server-Side-Encryption-S3-Kms-Sealed-Key";
pub const MINIO_INTERNAL_ENCRYPTION_KMS_CONTEXT_HEADER: &str = "X-Minio-Internal-Server-Side-Encryption-Context";
pub const RUSTFS_INTERNAL_ENCRYPTION_PREFIX: &str = "x-rustfs-internal-server-side-encryption-";
pub const REPLICATION_SSEC_ALGORITHM_HEADER: &str = "X-Rustfs-Replication-Ssec-Algorithm";
pub const REPLICATION_SSEC_KEY_MD5_HEADER: &str = "X-Rustfs-Replication-Ssec-Key-Md5";
pub const REPLICATION_SSEC_ORIGINAL_SIZE_HEADER: &str = "X-Rustfs-Replication-Ssec-Original-Size";
pub const REPLICATION_ENCRYPTION_IV_HEADER: &str = "X-Rustfs-Replication-Encryption-Iv";
pub const REPLICATION_SSE_IV_HEADER: &str = "X-Rustfs-Replication-Server-Side-Encryption-Iv";
pub const REPLICATION_SSE_SEAL_ALGORITHM_HEADER: &str = "X-Rustfs-Replication-Server-Side-Encryption-Seal-Algorithm";
pub const REPLICATION_SSE_SEALED_KEY_HEADER: &str = "X-Rustfs-Replication-Server-Side-Encryption-Sealed-Key";
pub const REPLICATION_ENCRYPTED_MULTIPART_HEADER: &str = "X-Rustfs-Replication-Encrypted-Multipart";
pub const SSEC_REPLICATION_TRANSPORT_HEADERS: &[(&str, &str)] = &[
(SSEC_ALGORITHM_HEADER, REPLICATION_SSEC_ALGORITHM_HEADER),
(SSEC_KEY_MD5_HEADER, REPLICATION_SSEC_KEY_MD5_HEADER),
(SSEC_ORIGINAL_SIZE_HEADER, REPLICATION_SSEC_ORIGINAL_SIZE_HEADER),
(INTERNAL_ENCRYPTION_IV_HEADER, REPLICATION_ENCRYPTION_IV_HEADER),
(MINIO_INTERNAL_ENCRYPTION_IV_HEADER, REPLICATION_SSE_IV_HEADER),
(MINIO_INTERNAL_ENCRYPTION_ALGORITHM_HEADER, REPLICATION_SSE_SEAL_ALGORITHM_HEADER),
(MINIO_INTERNAL_ENCRYPTION_SSEC_SEALED_KEY_HEADER, REPLICATION_SSE_SEALED_KEY_HEADER),
(MINIO_INTERNAL_ENCRYPTION_MULTIPART_HEADER, REPLICATION_ENCRYPTED_MULTIPART_HEADER),
];
pub fn project_ssec_transport_headers(headers: &http::HeaderMap) -> http::HeaderMap {
let mut projected = http::HeaderMap::new();
for name in [SSEC_ALGORITHM_HEADER, SSEC_KEY_HEADER, SSEC_KEY_MD5_HEADER] {
if let Some(value) = headers.get(name) {
let mut value = value.clone();
value.set_sensitive(true);
projected.insert(name, value);
}
}
projected
}
pub const REPLICATION_SSE_TRANSPORT_PREFIXES: &[&str] = &[
"x-rustfs-replication-server-side-encryption-",
"x-rustfs-replication-encryption-",
"x-rustfs-replication-ssec-",
];
pub fn has_ssec_transport_headers(headers: &http::HeaderMap) -> bool {
headers.keys().any(|name| {
let name = name.as_str();
REPLICATION_SSE_TRANSPORT_PREFIXES
.iter()
.any(|prefix| super::starts_with_ignore_ascii_case(name, prefix))
|| name.eq_ignore_ascii_case(REPLICATION_ENCRYPTED_MULTIPART_HEADER)
})
}
pub fn ssec_transport_to_stored_metadata(headers: &http::HeaderMap) -> Option<std::collections::HashMap<String, String>> {
let mut restored = std::collections::HashMap::new();
for (stored, transport) in SSEC_REPLICATION_TRANSPORT_HEADERS {
if let Some(value) = headers.get(*transport).and_then(|value| value.to_str().ok()) {
restored.insert((*stored).to_string(), value.to_string());
}
}
if restored.is_empty() {
return None;
}
if restored.contains_key(SSEC_ALGORITHM_HEADER) {
restored.insert("x-amz-server-side-encryption".to_string(), "AES256".to_string());
}
Some(restored)
}
pub fn ssec_replication_transport_header(stored_key: &str) -> Option<&'static str> {
SSEC_REPLICATION_TRANSPORT_HEADERS
.iter()
.find(|(stored, _)| stored.eq_ignore_ascii_case(stored_key))
.map(|(_, transport)| *transport)
}
pub fn is_replication_stripped_encryption_key(key: &str) -> bool {
super::is_encryption_metadata_key(key)
|| super::is_sse_header(key)
|| key.eq_ignore_ascii_case(SSEC_ORIGINAL_SIZE_HEADER)
|| super::starts_with_ignore_ascii_case(key, RUSTFS_INTERNAL_ENCRYPTION_PREFIX)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SSEType {
SseS3,
SseKms,
SseC,
}
impl SSEType {
pub fn audit_label(self) -> &'static str {
match self {
SSEType::SseS3 => "SSE-S3",
SSEType::SseKms => "SSE-KMS",
SSEType::SseC => "SSE-C",
}
}
}
pub type KmsContextRecoder = fn(&str) -> Option<String>;
pub fn contains_managed_encryption_metadata(metadata: &HashMap<String, String>) -> bool {
metadata.contains_key(INTERNAL_ENCRYPTION_KEY_HEADER)
|| metadata.contains_key(MINIO_INTERNAL_ENCRYPTION_S3_SEALED_KEY_HEADER)
|| metadata.contains_key(MINIO_INTERNAL_ENCRYPTION_KMS_SEALED_KEY_HEADER)
|| metadata.contains_key(MINIO_INTERNAL_ENCRYPTION_KMS_DATA_KEY_HEADER)
|| metadata.contains_key(MINIO_INTERNAL_ENCRYPTION_KMS_CONTEXT_HEADER)
}
pub fn normalize_managed_metadata(
metadata: &HashMap<String, String>,
recode_kms_context: Option<KmsContextRecoder>,
) -> HashMap<String, String> {
let mut normalized = metadata.clone();
if !normalized.contains_key(INTERNAL_ENCRYPTION_KEY_HEADER)
&& let Some(value) = metadata
.get(MINIO_INTERNAL_ENCRYPTION_KMS_DATA_KEY_HEADER)
.or_else(|| metadata.get(MINIO_INTERNAL_ENCRYPTION_KMS_SEALED_KEY_HEADER))
.or_else(|| metadata.get(MINIO_INTERNAL_ENCRYPTION_S3_SEALED_KEY_HEADER))
.or_else(|| metadata.get(MINIO_INTERNAL_ENCRYPTION_SSEC_SEALED_KEY_HEADER))
{
normalized.insert(INTERNAL_ENCRYPTION_KEY_HEADER.to_string(), value.clone());
}
if !normalized.contains_key(INTERNAL_ENCRYPTION_IV_HEADER)
&& let Some(value) = metadata.get(MINIO_INTERNAL_ENCRYPTION_IV_HEADER)
{
normalized.insert(INTERNAL_ENCRYPTION_IV_HEADER.to_string(), value.clone());
}
if !normalized.contains_key(INTERNAL_ENCRYPTION_ALGORITHM_HEADER)
&& let Some(value) = metadata.get(MINIO_INTERNAL_ENCRYPTION_ALGORITHM_HEADER)
{
normalized.insert(INTERNAL_ENCRYPTION_ALGORITHM_HEADER.to_string(), value.clone());
}
if !normalized.contains_key(INTERNAL_ENCRYPTION_KEY_ID_HEADER)
&& let Some(value) = metadata.get(MINIO_INTERNAL_ENCRYPTION_KMS_KEY_ID_HEADER)
{
normalized.insert(INTERNAL_ENCRYPTION_KEY_ID_HEADER.to_string(), value.clone());
}
if !normalized.contains_key(INTERNAL_ENCRYPTION_CONTEXT_HEADER)
&& let Some(value) = metadata.get(MINIO_INTERNAL_ENCRYPTION_KMS_CONTEXT_HEADER)
&& let Some(recode) = recode_kms_context
&& let Some(encoded) = recode(value)
{
normalized.insert(INTERNAL_ENCRYPTION_CONTEXT_HEADER.to_string(), encoded);
}
normalized
}
pub fn stored_managed_encryption_key(metadata: &HashMap<String, String>) -> Option<(SSEType, String)> {
if !contains_managed_encryption_metadata(metadata) {
return None;
}
let sse_type = match metadata.get("x-amz-server-side-encryption")?.as_str() {
AMZ_ENCRYPTION_KMS => SSEType::SseKms,
_ => SSEType::SseS3,
};
let key_id = normalize_managed_metadata(metadata, None)
.get(INTERNAL_ENCRYPTION_KEY_ID_HEADER)
.or_else(|| metadata.get("x-amz-server-side-encryption-aws-kms-key-id"))
.cloned()
.unwrap_or_else(|| "default".to_string());
Some((sse_type, key_id))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ssec_transport_projection_retains_only_redacted_reader_headers() {
let mut headers = http::HeaderMap::new();
headers.insert(SSEC_ALGORITHM_HEADER, http::HeaderValue::from_static("AES256"));
headers.insert(SSEC_KEY_HEADER, http::HeaderValue::from_static("secret-key"));
headers.insert(SSEC_KEY_MD5_HEADER, http::HeaderValue::from_static("key-md5"));
headers.insert(http::header::AUTHORIZATION, http::HeaderValue::from_static("credential"));
let projected = project_ssec_transport_headers(&headers);
assert_eq!(projected.len(), 3);
assert!(projected.values().all(http::HeaderValue::is_sensitive));
assert!(projected.get(http::header::AUTHORIZATION).is_none());
assert!(!format!("{projected:?}").contains("secret-key"));
}
#[test]
fn transport_metadata_roundtrip_restores_stored_keys() {
let mut headers = http::HeaderMap::new();
headers.insert(
http::HeaderName::from_static("x-rustfs-replication-ssec-algorithm"),
http::HeaderValue::from_static("AES256"),
);
headers.insert(
http::HeaderName::from_static("x-rustfs-replication-encryption-iv"),
http::HeaderValue::from_static("iv-direct"),
);
headers.insert(
http::HeaderName::from_static("x-rustfs-replication-server-side-encryption-iv"),
http::HeaderValue::from_static("iv-minio"),
);
assert!(has_ssec_transport_headers(&headers));
let restored = ssec_transport_to_stored_metadata(&headers).expect("transport headers must restore");
assert_eq!(
restored
.get("x-amz-server-side-encryption-customer-algorithm")
.map(String::as_str),
Some("AES256")
);
assert!(!restored.keys().any(|k| k != "x-amz-server-side-encryption-customer-algorithm"
&& k.eq_ignore_ascii_case("x-amz-server-side-encryption-customer-algorithm")));
assert_eq!(restored.get(INTERNAL_ENCRYPTION_IV_HEADER).map(String::as_str), Some("iv-direct"));
assert_eq!(restored.get(MINIO_INTERNAL_ENCRYPTION_IV_HEADER).map(String::as_str), Some("iv-minio"));
assert_eq!(restored.get("x-amz-server-side-encryption").map(String::as_str), Some("AES256"));
let plain = http::HeaderMap::new();
assert!(!has_ssec_transport_headers(&plain));
assert!(ssec_transport_to_stored_metadata(&plain).is_none());
}
#[test]
fn transport_lookup_is_case_insensitive() {
assert_eq!(
ssec_replication_transport_header("X-AMZ-SERVER-SIDE-ENCRYPTION-CUSTOMER-ALGORITHM"),
Some(REPLICATION_SSEC_ALGORITHM_HEADER)
);
assert_eq!(
ssec_replication_transport_header("x-minio-internal-server-side-encryption-sealed-key"),
Some(REPLICATION_SSE_SEALED_KEY_HEADER)
);
assert_eq!(ssec_replication_transport_header("x-rustfs-encryption-key"), None);
}
#[test]
fn stripped_predicate_covers_envelopes_intents_and_ssec_material() {
assert!(is_replication_stripped_encryption_key(INTERNAL_ENCRYPTION_KEY_HEADER));
assert!(is_replication_stripped_encryption_key(INTERNAL_ENCRYPTION_KEY_ID_HEADER));
assert!(is_replication_stripped_encryption_key(INTERNAL_ENCRYPTION_CONTEXT_HEADER));
assert!(is_replication_stripped_encryption_key(MINIO_INTERNAL_ENCRYPTION_SSEC_SEALED_KEY_HEADER));
assert!(is_replication_stripped_encryption_key(MINIO_INTERNAL_ENCRYPTION_S3_SEALED_KEY_HEADER));
assert!(is_replication_stripped_encryption_key(MINIO_INTERNAL_ENCRYPTION_KMS_SEALED_KEY_HEADER));
assert!(is_replication_stripped_encryption_key(MINIO_INTERNAL_ENCRYPTION_KMS_KEY_ID_HEADER));
assert!(is_replication_stripped_encryption_key(MINIO_INTERNAL_ENCRYPTION_KMS_DATA_KEY_HEADER));
assert!(is_replication_stripped_encryption_key(MINIO_INTERNAL_ENCRYPTION_KMS_CONTEXT_HEADER));
assert!(is_replication_stripped_encryption_key(MINIO_INTERNAL_ENCRYPTION_MULTIPART_HEADER));
assert!(is_replication_stripped_encryption_key(
"x-rustfs-internal-server-side-encryption-sealed-key"
));
assert!(is_replication_stripped_encryption_key("x-amz-server-side-encryption"));
assert!(is_replication_stripped_encryption_key("x-amz-server-side-encryption-aws-kms-key-id"));
assert!(is_replication_stripped_encryption_key(SSEC_ALGORITHM_HEADER));
assert!(is_replication_stripped_encryption_key(SSEC_ORIGINAL_SIZE_HEADER));
assert!(is_replication_stripped_encryption_key(
"X-Amz-Server-Side-Encryption-Customer-Original-Size"
));
assert!(!is_replication_stripped_encryption_key("x-amz-meta-app"));
assert!(!is_replication_stripped_encryption_key("content-type"));
}
#[test]
fn managed_envelope_predicate_matches_both_key_families() {
assert!(!contains_managed_encryption_metadata(&HashMap::new()));
for key in [
INTERNAL_ENCRYPTION_KEY_HEADER,
MINIO_INTERNAL_ENCRYPTION_S3_SEALED_KEY_HEADER,
MINIO_INTERNAL_ENCRYPTION_KMS_SEALED_KEY_HEADER,
MINIO_INTERNAL_ENCRYPTION_KMS_DATA_KEY_HEADER,
MINIO_INTERNAL_ENCRYPTION_KMS_CONTEXT_HEADER,
] {
let single = HashMap::from([(key.to_string(), "value".to_string())]);
assert!(contains_managed_encryption_metadata(&single), "{key} must classify as managed SSE");
}
let ssec_only = HashMap::from([(SSEC_ALGORITHM_HEADER.to_string(), "AES256".to_string())]);
assert!(!contains_managed_encryption_metadata(&ssec_only));
}
#[test]
fn normalize_maps_minio_keys_onto_missing_rustfs_keys_only() {
let metadata = HashMap::from([
(MINIO_INTERNAL_ENCRYPTION_KMS_DATA_KEY_HEADER.to_string(), "minio-dek".to_string()),
(MINIO_INTERNAL_ENCRYPTION_IV_HEADER.to_string(), "minio-iv".to_string()),
(MINIO_INTERNAL_ENCRYPTION_ALGORITHM_HEADER.to_string(), "DAREv2-HMAC-SHA256".to_string()),
(MINIO_INTERNAL_ENCRYPTION_KMS_KEY_ID_HEADER.to_string(), "minio-key".to_string()),
]);
let normalized = normalize_managed_metadata(&metadata, None);
assert_eq!(normalized.get(INTERNAL_ENCRYPTION_KEY_HEADER).map(String::as_str), Some("minio-dek"));
assert_eq!(normalized.get(INTERNAL_ENCRYPTION_IV_HEADER).map(String::as_str), Some("minio-iv"));
assert_eq!(
normalized.get(INTERNAL_ENCRYPTION_ALGORITHM_HEADER).map(String::as_str),
Some("DAREv2-HMAC-SHA256")
);
assert_eq!(normalized.get(INTERNAL_ENCRYPTION_KEY_ID_HEADER).map(String::as_str), Some("minio-key"));
let mut both = metadata;
both.insert(INTERNAL_ENCRYPTION_KEY_ID_HEADER.to_string(), "rustfs-key".to_string());
assert_eq!(
normalize_managed_metadata(&both, None)
.get(INTERNAL_ENCRYPTION_KEY_ID_HEADER)
.map(String::as_str),
Some("rustfs-key")
);
let lowercased = HashMap::from([(MINIO_INTERNAL_ENCRYPTION_KMS_KEY_ID_HEADER.to_lowercase(), "minio-key".to_string())]);
assert!(!normalize_managed_metadata(&lowercased, None).contains_key(INTERNAL_ENCRYPTION_KEY_ID_HEADER));
}
#[test]
fn normalize_recodes_kms_context_only_through_the_injected_codec() {
let metadata = HashMap::from([(MINIO_INTERNAL_ENCRYPTION_KMS_CONTEXT_HEADER.to_string(), "encoded-context".to_string())]);
assert!(!normalize_managed_metadata(&metadata, None).contains_key(INTERNAL_ENCRYPTION_CONTEXT_HEADER));
fn reject(_value: &str) -> Option<String> {
None
}
assert!(!normalize_managed_metadata(&metadata, Some(reject)).contains_key(INTERNAL_ENCRYPTION_CONTEXT_HEADER));
fn recode(value: &str) -> Option<String> {
Some(format!("recoded:{value}"))
}
assert_eq!(
normalize_managed_metadata(&metadata, Some(recode))
.get(INTERNAL_ENCRYPTION_CONTEXT_HEADER)
.map(String::as_str),
Some("recoded:encoded-context")
);
let mut both = metadata;
both.insert(INTERNAL_ENCRYPTION_CONTEXT_HEADER.to_string(), "stored-context".to_string());
assert_eq!(
normalize_managed_metadata(&both, Some(recode))
.get(INTERNAL_ENCRYPTION_CONTEXT_HEADER)
.map(String::as_str),
Some("stored-context")
);
}
#[test]
fn stored_managed_encryption_key_attributes_scheme_and_key() {
assert!(stored_managed_encryption_key(&HashMap::new()).is_none());
let envelope_only = HashMap::from([(INTERNAL_ENCRYPTION_KEY_HEADER.to_string(), "dek".to_string())]);
assert!(stored_managed_encryption_key(&envelope_only).is_none());
let mut titlecase = envelope_only.clone();
titlecase.insert("X-Amz-Server-Side-Encryption".to_string(), "aws:kms".to_string());
assert!(stored_managed_encryption_key(&titlecase).is_none());
let mut sse_s3 = envelope_only.clone();
sse_s3.insert("x-amz-server-side-encryption".to_string(), "AES256".to_string());
assert_eq!(stored_managed_encryption_key(&sse_s3), Some((SSEType::SseS3, "default".to_string())));
let mut sse_kms = envelope_only;
sse_kms.insert("x-amz-server-side-encryption".to_string(), "aws:kms".to_string());
assert_eq!(stored_managed_encryption_key(&sse_kms), Some((SSEType::SseKms, "default".to_string())));
sse_kms.insert("x-amz-server-side-encryption-aws-kms-key-id".to_string(), "amz-key".to_string());
assert_eq!(stored_managed_encryption_key(&sse_kms), Some((SSEType::SseKms, "amz-key".to_string())));
sse_kms.insert(MINIO_INTERNAL_ENCRYPTION_KMS_KEY_ID_HEADER.to_string(), "minio-key".to_string());
assert_eq!(stored_managed_encryption_key(&sse_kms), Some((SSEType::SseKms, "minio-key".to_string())));
sse_kms.insert(INTERNAL_ENCRYPTION_KEY_ID_HEADER.to_string(), "rustfs-key".to_string());
assert_eq!(stored_managed_encryption_key(&sse_kms), Some((SSEType::SseKms, "rustfs-key".to_string())));
}
#[test]
fn sse_type_audit_labels_are_stable() {
assert_eq!(SSEType::SseS3.audit_label(), "SSE-S3");
assert_eq!(SSEType::SseKms.audit_label(), "SSE-KMS");
assert_eq!(SSEType::SseC.audit_label(), "SSE-C");
}
#[test]
fn transport_prefixes_cover_every_transport_value_key() {
for (_, transport) in SSEC_REPLICATION_TRANSPORT_HEADERS {
if transport.eq_ignore_ascii_case(REPLICATION_ENCRYPTED_MULTIPART_HEADER) {
continue;
}
let lower = transport.to_lowercase();
assert!(
REPLICATION_SSE_TRANSPORT_PREFIXES
.iter()
.any(|prefix| lower.starts_with(prefix)),
"transport key {transport} is not covered by a redaction prefix"
);
}
}
}