Skip to content

Commit 5c739f6

Browse files
committed
refactor: throw exceptions if errors are detected, instead of just logging them.
1 parent 7b41abf commit 5c739f6

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

common/source/CipherEngine.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,15 @@ namespace SDMS{
4242
std::fill_n(output.get(), paddedLength + SDMS::CipherEngine::NULL_TERMINATOR_SIZE, 0); // manually zero-initialize
4343
const int outputLength = EVP_EncodeBlock(reinterpret_cast<unsigned char*>(output.get()), input, length);
4444
if (paddedLength != outputLength)
45-
{ DL_ERROR(log_context, "Output Length (" << outputLength <<") and Predicted Padded Length ("<< paddedLength <<" ) of encoded bytes not equal!"); }
45+
{
46+
std::ostringstream oss;
47+
oss << "Output Length (" << outputLength
48+
<< ") and Predicted Padded Length (" << paddedLength
49+
<< ") of encoded bytes not equal!";
50+
51+
DL_ERROR(log_context, oss.str());
52+
EXCEPT_PARAM(1, oss.str());
53+
}
4654
return output;
4755
}
4856

@@ -54,8 +62,15 @@ namespace SDMS{
5462
auto output = std::make_unique<unsigned char[]>(paddedLength+SDMS::CipherEngine::NULL_TERMINATOR_SIZE);
5563
std::fill_n(output.get(), paddedLength+SDMS::CipherEngine::NULL_TERMINATOR_SIZE, 0);
5664
const int outputLength = EVP_DecodeBlock(output.get(), reinterpret_cast<const unsigned char*>(input), length);
57-
if (paddedLength != outputLength)
58-
{ DL_ERROR(log_context, "Output Length (" << outputLength <<") and Predicted Padded Length ("<< paddedLength <<" ) of decoded bytes not equal!"); }
65+
if (paddedLength != outputLength) {
66+
std::ostringstream oss;
67+
oss << "Output Length (" << outputLength
68+
<< ") and Predicted Padded Length (" << paddedLength
69+
<< ") of decoded bytes not equal!";
70+
71+
DL_ERROR(log_context, oss.str());
72+
EXCEPT_PARAM(1, oss.str());
73+
}
5974
return output;
6075
}
6176
void CipherEngine::generateIV(unsigned char *iv)
@@ -107,7 +122,7 @@ bool CipherEngine::tokenNeedsUpdate(const Value::Object &obj)
107122
CipherEngine::CipherBytes CipherEngine::encryptAlgorithm(unsigned char *iv, const std::string& msg, LogContext log_context)
108123
{
109124
if (msg.length() > MAX_MSG_LENGTH) {
110-
throw TraceException(__FILE__, __LINE__, 0, std::string("Message too long for encryption"));
125+
EXCEPT_PARAM(0, std::string("Message too long for encryption"));
111126
}
112127
EVP_CIPHER_CTX *ctx = nullptr;
113128
CipherBytes bytes_result = {};

common/tests/unit/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ foreach(PROG
2121
add_executable(unit_${PROG} ${${PROG}_SOURCES})
2222
if(BUILD_SHARED_LIBS)
2323
target_link_libraries(unit_${PROG} PRIVATE ${DATAFED_BOOST_LIBRARIES}
24-
common libzmq protobuf::libprotobuf Threads::Threads)
24+
common libzmq protobuf::libprotobuf Threads::Threads
25+
"${OPENSSL_SSL_LIBRARY}" "${OPENSSL_CRYPTO_LIBRARY}" )
2526
target_compile_definitions(unit_${PROG} PRIVATE BOOST_TEST_DYN_LINK)
2627
else()
2728
target_link_libraries(unit_${PROG} PRIVATE ${DATAFED_BOOST_LIBRARIES}
28-
common libzmq-static protobuf::libprotobuf Threads::Threads)
29+
common libzmq-static protobuf::libprotobuf Threads::Threads
30+
"${OPENSSL_SSL_LIBRARY}" "${OPENSSL_CRYPTO_LIBRARY}" )
2931
endif()
3032
# Only want this if using shared boost libraries
3133
if ( ENABLE_UNIT_TESTS )

common/tests/unit/test_CipherEngine.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,31 @@ BOOST_AUTO_TEST_CASE(testing_KeyGeneration)
4141
finalArray[lv] = keyArray[lv];
4242
}
4343
BOOST_CHECK(sizeof(finalArray)==SDMS::CipherEngine::KEY_LENGTH);
44+
45+
// Assert that the generated key is not all zeros
46+
bool all_zeros = true;
47+
for (int i = 0; i < SDMS::CipherEngine::KEY_LENGTH; ++i) {
48+
if (token_key[i] != 0) {
49+
all_zeros = false;
50+
break;
51+
}
52+
}
53+
BOOST_CHECK(!all_zeros);
54+
// Generate a second key and check that it is different from the first
55+
unsigned char second_key[SDMS::CipherEngine::KEY_LENGTH];
56+
CipherEngine::generateEncryptionKey(second_key);
57+
bool keys_are_different = false;
58+
for (int i = 0; i < SDMS::CipherEngine::KEY_LENGTH; ++i) {
59+
if (token_key[i] != second_key[i]) {
60+
keys_are_different = true;
61+
break;
62+
}
63+
}
64+
BOOST_CHECK(keys_are_different);
4465
}
4566

67+
68+
4669
BOOST_AUTO_TEST_CASE(test_EncryptionDecryption)
4770
{
4871
LogContext log_context;

0 commit comments

Comments
 (0)