diff --git a/src/wmbus.cc b/src/wmbus.cc index 2f541ac..54e2dad 100644 --- a/src/wmbus.cc +++ b/src/wmbus.cc @@ -4145,11 +4145,11 @@ bool BusDeviceCommonImplementation::handleTelegram(AboutTelegram &about, vector< if (about.type == FrameType::WMBUS) { - size_t len = frame[0]+1; - if (frame.size() > 0 && len != frame.size()) + size_t expected_len = frame[0]+1; + if (frame.size() > 0 && expected_len != frame.size()) { warning("(wmbus) telegram length byte (the first) 0x%02x (%d) is probably wrong. Expected 0x%02x (%zu) based on the length of the telegram.\n", - frame[0], frame[0], len, len); + frame[0], frame[0], frame.size()-1, frame.size()-1); } } diff --git a/src/wmbus_utils.cc b/src/wmbus_utils.cc index 99ddc1f..f5f67a8 100644 --- a/src/wmbus_utils.cc +++ b/src/wmbus_utils.cc @@ -114,6 +114,16 @@ bool decrypt_TPL_AES_CBC_IV(Telegram *t, } *num_encrypted_bytes = num_bytes_to_decrypt; + + if (buffer.size() < num_bytes_to_decrypt) + { + warning("(TPL) warning: aes-cbc-iv decryption received less bytes than expected for decryption! " + "Got %zu bytes but expected at least %zu bytes since num encr blocks was %d.\n", + buffer.size(), num_bytes_to_decrypt, + t->tpl_num_encr_blocks); + num_bytes_to_decrypt = buffer.size(); + } + *num_not_encrypted_at_end = buffer.size()-num_bytes_to_decrypt; debug("(TPL) num encrypted blocks %zu (%d bytes and remaining unencrypted %zu bytes)\n", @@ -123,15 +133,6 @@ bool decrypt_TPL_AES_CBC_IV(Telegram *t, debugPayload("(TPL) AES CBC IV decrypting", buffer); - if (buffer.size() < num_bytes_to_decrypt) - { - warning("(TPL) warning: decryption received less bytes than expected for decryption! " - "Got %zu bytes but expected at least %zu bytes since num encr blocks was %d.\n", - buffer.size(), num_bytes_to_decrypt, - t->tpl_num_encr_blocks); - num_bytes_to_decrypt = buffer.size(); - } - // The content should be a multiple of 16 since we are using AES CBC mode. if (num_bytes_to_decrypt % 16 != 0) { @@ -208,6 +209,15 @@ bool decrypt_TPL_AES_CBC_NO_IV(Telegram *t, vector &frame, vector: } *num_encrypted_bytes = num_bytes_to_decrypt; + if (buffer.size() < num_bytes_to_decrypt) + { + warning("(TPL) warning: aes-cbc-no-iv decryption received less bytes than expected for decryption! " + "Got %zu bytes but expected at least %zu bytes since num encr blocks was %d.\n", + buffer.size(), num_bytes_to_decrypt, + t->tpl_num_encr_blocks); + num_bytes_to_decrypt = buffer.size(); + } + *num_not_encrypted_at_end = buffer.size()-num_bytes_to_decrypt; debug("(TPL) num encrypted blocks %d (%d bytes and remaining unencrypted %d bytes)\n", @@ -215,15 +225,6 @@ bool decrypt_TPL_AES_CBC_NO_IV(Telegram *t, vector &frame, vector: if (aeskey.size() == 0) return false; - if (buffer.size() < num_bytes_to_decrypt) - { - warning("(TPL) warning: decryption received less bytes than expected for decryption! " - "Got %zu bytes but expected at least %zu bytes since num encr blocks was %d.\n", - buffer.size(), num_bytes_to_decrypt, - t->tpl_num_encr_blocks); - num_bytes_to_decrypt = buffer.size(); - } - // The content should be a multiple of 16 since we are using AES CBC mode. if (num_bytes_to_decrypt % 16 != 0) { diff --git a/tests/test_listen_to_all.sh b/tests/test_listen_to_all.sh index 6152a10..f52a303 100755 --- a/tests/test_listen_to_all.sh +++ b/tests/test_listen_to_all.sh @@ -70,7 +70,7 @@ Received telegram from: 58234965 type: Heat meter (0xc3) ver: 0x27 driver: vario451 -(wmbus) telegram length byte (the first) 0x31 (49) is probably wrong. Expected 0x32 (50) based on the length of the telegram. +(wmbus) telegram length byte (the first) 0x31 (49) is probably wrong. Expected 0x34 (52) based on the length of the telegram. Received telegram from: 11776622 manufacturer: (TCH) Techem Service (0x5068) type: Heat Cost Allocator (0x80) diff --git a/tests/test_logfile.sh b/tests/test_logfile.sh index 5449535..ae84b90 100755 --- a/tests/test_logfile.sh +++ b/tests/test_logfile.sh @@ -55,7 +55,7 @@ RES=$(cat $TEST/thelog2.txt | tr '\n' ' ' | tr -d ' ' | sed 's/|+1 /|+0 /g' ) # The sed replacement reduces the risk of a failing test when the second counter happens to flip within the test run. -EXP=$(printf 'telegram=|A244EE4D785634123C067A8F000000_0C1348550000426CE1F14C130000000082046C21298C0413330000008D04931E3A3CFE3300000033000000330000003300000033000000330000003300000033000000330000003300000033000000330000004300000034180000046D0D0B5C2B03FD6C5E150082206C5C290BFD0F0200018C4079678885238310FD3100000082106C01018110FD610002FD66020002FD170000|+0 telegram=|A244EE4D111111113C077AAC000000_0C1389490000426CE1F14C130000000082046C21298C0413010000008D04931E3A3CFE0100000001000000010000000100000001000000010000000100000001000000010000000100000001000000010000001600000031130000046D0A0C5C2B03FD6C60150082206C5C290BFD0F0200018C4079629885238310FD3100000082106C01018110FD610002FD66020002FD170000|+0 telegram=|1E44AE4C9956341268077A36001000_2F2F0413181E0000023B00002F2F2F2F|+0 telegram=|1844AE4C4455223368077A55000000_041389E20100023B0000|+0(wmbus)telegramlengthbyte(thefirst)0x31(49)isprobablywrong.Expected0x32(50)basedonthelengthofthetelegram.' | tr '\n' ' ' | tr -d ' ') +EXP=$(printf 'telegram=|A244EE4D785634123C067A8F000000_0C1348550000426CE1F14C130000000082046C21298C0413330000008D04931E3A3CFE3300000033000000330000003300000033000000330000003300000033000000330000003300000033000000330000004300000034180000046D0D0B5C2B03FD6C5E150082206C5C290BFD0F0200018C4079678885238310FD3100000082106C01018110FD610002FD66020002FD170000|+0 telegram=|A244EE4D111111113C077AAC000000_0C1389490000426CE1F14C130000000082046C21298C0413010000008D04931E3A3CFE0100000001000000010000000100000001000000010000000100000001000000010000000100000001000000010000001600000031130000046D0A0C5C2B03FD6C60150082206C5C290BFD0F0200018C4079629885238310FD3100000082106C01018110FD610002FD66020002FD170000|+0 telegram=|1E44AE4C9956341268077A36001000_2F2F0413181E0000023B00002F2F2F2F|+0 telegram=|1844AE4C4455223368077A55000000_041389E20100023B0000|+0(wmbus)telegramlengthbyte(thefirst)0x31(49)isprobablywrong.Expected0x34(52)basedonthelengthofthetelegram.' | tr '\n' ' ' | tr -d ' ') if [ "$RES" != "$EXP" ] then