2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-04-08 01:14:19 +00:00

WMBus: handle partially encrypted messages

git-svn-id: https://svn.fhem.de/fhem/trunk@18058 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
kaihs 2018-12-26 11:45:15 +00:00
parent f200d34d31
commit 32763eb99b

View File

@ -1453,12 +1453,14 @@ sub decrypt($) {
for (1..8) { for (1..8) {
$initVector .= pack('C',$self->{access_no}); $initVector .= pack('C',$self->{access_no});
} }
if (length($encrypted)%16 == 0) { if (length($encrypted)%16 == 0) {
# no padding if data length is multiple of blocksize # no padding if data length is multiple of blocksize
$padding = 0; $padding = 0;
} else { } else {
$padding = 2; $padding = 2;
} }
#printf("length encrypted %d padding %d\n", length($encrypted), $padding);
my $cipher = Crypt::Mode::CBC->new('AES', $padding); my $cipher = Crypt::Mode::CBC->new('AES', $padding);
return $cipher->decrypt($encrypted, $self->{aeskey}, $initVector); return $cipher->decrypt($encrypted, $self->{aeskey}, $initVector);
} }
@ -1827,8 +1829,18 @@ sub decodeApplicationLayer($) {
if ($self->{aeskey}) { if ($self->{aeskey}) {
if ($hasCBC) { if ($hasCBC) {
#printf("encrypted payload %s\n", unpack("H*", substr($applicationlayer,$offset))); my $encrypted_length = $self->{cw_parts}{encrypted_blocks} * 16;
$payload = $self->decrypt(substr($applicationlayer,$offset)); #printf("encrypted payload %s\n", unpack("H*", substr($applicationlayer,$offset, $encrypted_length)));
eval {
$payload = $self->decrypt(substr($applicationlayer, $offset, $encrypted_length))
. substr($applicationlayer, $offset+$encrypted_length);
};
if ($@) {
#fatal decryption error occurred
$self->{errormsg} = "fatal decryption error: $@";
$self->{errorcode} = ERR_DECRYPTION_FAILED;
return 0;
}
#printf("decrypted payload %s\n", unpack("H*", $payload)); #printf("decrypted payload %s\n", unpack("H*", $payload));
if (unpack('n', $payload) == 0x2f2f) { if (unpack('n', $payload) == 0x2f2f) {
$self->{decrypted} = 1; $self->{decrypted} = 1;