-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
RopsFileBuilder
for aiding in the initial rops file encry…
…ption.
- Loading branch information
Showing
14 changed files
with
233 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
use crate::*; | ||
|
||
pub trait IntegrationKeyId<I: Integration> { | ||
fn append_to_builder<F: FileFormat>(self, rops_file_builder: &mut RopsFileBuilder<F>); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use crate::*; | ||
|
||
pub struct RopsFileBuilder<F: FileFormat> { | ||
plaintext_map: F::Map, | ||
partial_encryption: Option<PartialEncryptionConfig>, | ||
mac_only_encrypted: Option<bool>, | ||
#[cfg(feature = "age")] | ||
pub(crate) age_key_ids: Vec<<AgeIntegration as Integration>::KeyId>, | ||
#[cfg(feature = "aws-kms")] | ||
pub(crate) aws_kms_key_ids: Vec<<AwsKmsIntegration as Integration>::KeyId>, | ||
} | ||
|
||
impl<F: FileFormat> RopsFileBuilder<F> { | ||
pub fn new(plaintext_map: F::Map) -> Self { | ||
Self { | ||
plaintext_map, | ||
partial_encryption: None, | ||
mac_only_encrypted: None, | ||
age_key_ids: Vec::new(), | ||
aws_kms_key_ids: Vec::new(), | ||
} | ||
} | ||
|
||
pub fn with_partial_encryption(mut self, partial_encryption: PartialEncryptionConfig) -> Self { | ||
self.partial_encryption = Some(partial_encryption); | ||
self | ||
} | ||
|
||
pub fn mac_only_encrypted(mut self) -> Self { | ||
self.mac_only_encrypted = Some(true); | ||
self | ||
} | ||
|
||
pub fn add_integration_key<I: Integration>(mut self, key_id: I::KeyId) -> Self { | ||
key_id.append_to_builder(&mut self); | ||
self | ||
} | ||
|
||
pub fn encrypt<C: Cipher, H: Hasher>(self) -> Result<RopsFile<EncryptedFile<C, H>, F>, RopsFileEncryptError> { | ||
#[rustfmt::skip] | ||
let Self { plaintext_map, partial_encryption, mac_only_encrypted, age_key_ids, aws_kms_key_ids } = self; | ||
|
||
let data_key = DataKey::new(); | ||
|
||
let decrypted_map = plaintext_map.decrypted_to_internal()?; | ||
|
||
let mac = Mac::<H>::compute( | ||
MacOnlyEncryptedConfig::new(mac_only_encrypted, partial_encryption.as_ref()), | ||
&decrypted_map, | ||
); | ||
|
||
let encrypted_map_result = decrypted_map.encrypt(&data_key, partial_encryption.as_ref()); | ||
|
||
let mut integration_metadata = IntegrationMetadata::default(); | ||
#[cfg(feature = "age")] | ||
integration_metadata.add_integration_keys::<AgeIntegration>(age_key_ids, &data_key)?; | ||
#[cfg(feature = "aws-kms")] | ||
integration_metadata.add_integration_keys::<AwsKmsIntegration>(aws_kms_key_ids, &data_key)?; | ||
|
||
let encrypted_metadata_result = RopsFileMetadata { | ||
intregation: integration_metadata, | ||
last_modified: LastModifiedDateTime::now(), | ||
mac, | ||
partial_encryption, | ||
mac_only_encrypted, | ||
} | ||
.encrypt(&data_key); | ||
|
||
RopsFile::from_parts_results(encrypted_map_result, encrypted_metadata_result) | ||
} | ||
} | ||
|
||
// Redundant to test combinations of file formats, integrations, ciphers and hashers if the | ||
// respective trait implementations are well tested. | ||
#[cfg(all(test, feature = "yaml", feature = "age", feature = "aes-gcm", feature = "sha2"))] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn encrypts_with_builder() { | ||
AgeIntegration::set_mock_private_key_env_var(); | ||
|
||
let builder_rops_file = | ||
RopsFileBuilder::<YamlFileFormat>::new(RopsFileFormatMap::<DecryptedMap, YamlFileFormat>::mock().into_inner_map()) | ||
.with_partial_encryption(MockTestUtil::mock()) | ||
.mac_only_encrypted() | ||
.add_integration_key::<AgeIntegration>(AgeIntegration::mock_key_id()) | ||
.encrypt::<AES256GCM, SHA512>() | ||
.unwrap() | ||
.decrypt::<YamlFileFormat>() | ||
.unwrap(); | ||
|
||
assert_eq!(RopsFileFormatMap::mock(), builder_rops_file.map); | ||
assert_ne!(RopsFileMetadata::mock(), builder_rops_file.metadata); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.