From 38a29571de70ea57a9357cf51de7598718cbf45a Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Mon, 16 Sep 2019 17:11:24 +0200 Subject: [PATCH] F #3064: Rewrite of encrypt/decrypt methods --- src/common/Attribute.cc | 149 ++++++++++++++++++++-------------------- 1 file changed, 75 insertions(+), 74 deletions(-) diff --git a/src/common/Attribute.cc b/src/common/Attribute.cc index 6c640e9c72..17ad559044 100644 --- a/src/common/Attribute.cc +++ b/src/common/Attribute.cc @@ -338,75 +338,47 @@ int VectorAttribute::vector_value(const string& name, bool& value) const /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -static void encrypt_attr(const std::string& one_key, - const std::string& in, - std::string& out) -{ - if (!one_key.empty()) - { - std::string * encrypted = one_util::aes256cbc_encrypt(in, one_key); - - out = *encrypted; - - delete encrypted; - } - else - { - out = in; - } -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -static bool decrypt_attr(const std::string& one_key, - const std::string& in, - std::string& out) -{ - if (one_key.empty()) - { - out = in; - return true; - } - - std::string * plain = one_util::aes256cbc_decrypt(in, one_key); - - if (plain == nullptr) - { - return false; - } - - out = *plain; - - delete plain; - - return true; -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - void SingleAttribute::encrypt(const string& one_key, const set& eas) { - string encrypted; - string tmp; - - // Simple attribute present, but not encrypted, crypt it - if (!value().empty() && !decrypt_attr(one_key, value(), tmp)) + if ( one_key.empty() ) { - encrypt_attr(one_key, value(), encrypted); - - replace(encrypted); + return; } + + std::string * plain = one_util::aes256cbc_decrypt(attribute_value, one_key); + + if ( plain != nullptr ) + { + delete plain; + return; + } + + std::string * encrypted = one_util::aes256cbc_encrypt(attribute_value, one_key); + + if ( encrypted == nullptr ) + { + return; + } + + attribute_value = *encrypted; + + delete encrypted; } void SingleAttribute::decrypt(const string& one_key, const set& eas) { - string plain; - - if (!value().empty() && decrypt_attr(one_key, value(), plain)) + if ( one_key.empty() ) { - replace(plain); + return; + } + + std::string * plain = one_util::aes256cbc_decrypt(attribute_value, one_key); + + if ( plain != nullptr ) + { + attribute_value = *plain; + + delete plain; } } @@ -415,36 +387,65 @@ void SingleAttribute::decrypt(const string& one_key, const set& eas) void VectorAttribute::encrypt(const string& one_key, const set& eas) { - string att; - string encrypted; - string tmp; + if ( one_key.empty() ) + { + return; + } for ( auto ea : eas ) { - att = vector_value(ea); + string att = vector_value(ea); - if (!att.empty() && !decrypt_attr(one_key, att, tmp)) + if (att.empty()) { - // Nested attribute present, but not encrypted, crypt it - encrypt_attr(one_key, att, encrypted); - - replace(ea, encrypted); + continue; } + + std::string * plain = one_util::aes256cbc_decrypt(att, one_key); + + if ( plain != nullptr ) + { + delete plain; + continue; + } + + std::string * encrypted = one_util::aes256cbc_encrypt(att, one_key); + + if ( encrypted == nullptr ) + { + continue; + } + + replace(ea, *encrypted); + + delete encrypted; } } void VectorAttribute::decrypt(const string& one_key, const set& eas) { - string att; - string plain; + if ( one_key.empty() ) + { + return; + } for ( auto ea : eas ) { - att = vector_value(ea); + string att = vector_value(ea); - if (!att.empty() && decrypt_attr(one_key, att, plain)) + if (att.empty()) { - replace(ea, plain); + continue; + } + + std::string * plain = one_util::aes256cbc_decrypt(att, one_key); + + if ( plain != nullptr ) + { + replace(ea, *plain); + + delete plain; } } } +