98_gcmsend: Add AES encryption

git-svn-id: https://svn.fhem.de/fhem/trunk@12117 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
klassm
2016-09-04 15:24:20 +00:00
parent 7b41313964
commit 2d69076ad1

View File

@@ -1,24 +1,36 @@
package main; package main;
use strict;
use warnings;
use HTTP::Request; use HTTP::Request;
use LWP::UserAgent; use LWP::UserAgent;
use IO::Socket::SSL; use IO::Socket::SSL;
use utf8; use utf8;
use Crypt::CBC;
use Crypt::Cipher::AES;
my @gets = ('dummy'); sub gcmsend_Initialize($)
sub
gcmsend_Initialize($)
{ {
my ($hash) = @_; my ($hash) = @_;
$hash->{DefFn} = "gcmsend_Define"; $hash->{DefFn} = "gcmsend_Define";
$hash->{NotifyFn} = "gcmsend_notify"; $hash->{NotifyFn} = "gcmsend_notify";
$hash->{AttrFn} = "gcmsend_attr";
$hash->{SetFn} = "gcmsend_set"; $hash->{SetFn} = "gcmsend_set";
$hash->{AttrList} = "loglevel:0,1,2,3,4,5 regIds apiKey stateFilter vibrate deviceFilter playSound"; $hash->{AttrList} = "loglevel:0,1,2,3,4,5 regIds apiKey stateFilter vibrate deviceFilter cryptKey";
} }
sub sub gcmsend_attr {
gcmsend_set { my ($cmd, $name, $aName, $aVal) = @_;
if (not $aName eq "cryptKey") {
return undef;
}
$aVal = sprintf("%016s", $aVal);
$aVal = substr $aVal, length($aVal) - 16, 16;
$_[3] = $aVal;
return undef;
}
sub gcmsend_set {
my ($hash, @a) = @_; my ($hash, @a) = @_;
my $v = @a[1]; my $v = @a[1];
if ($v eq "delete_saved_states") { if ($v eq "delete_saved_states") {
@@ -38,8 +50,7 @@ gcmsend_set {
} }
} }
sub sub gcmsend_Define($$)
gcmsend_Define($$)
{ {
my ($hash, $def) = @_; my ($hash, $def) = @_;
@@ -50,7 +61,7 @@ gcmsend_Define($$)
return "gcmsend_Define: too many arguments. Usage:\n". return "gcmsend_Define: too many arguments. Usage:\n".
"define <name> gcmsend"; "define <name> gcmsend";
} }
return "Invalid arguments. Usage: \n define <name> gcmsend" if(int(@a) != 0); return "Invalid arguments. Usage: \n define <name> gcmsend" if (int(@args) != 2);
$hash->{STATE} = 'Initialized'; $hash->{STATE} = 'Initialized';
@@ -72,8 +83,13 @@ sub gcmsend_array_to_json(@) {
return "[".$ret."]"; return "[".$ret."]";
} }
sub gcmsend_sendPayload($$) { sub gcmsend_sendPayload($%) {
my ($hash, $payload) = @_; my ($hash, %payload) = @_;
my %generalPayload = gcmsend_getGeneralPayload($hash);
my %toSendPayload = (%generalPayload, %payload);
my %encryptedPayload = gcmsend_encrypt($hash, %toSendPayload);
my $jsonPayload = gcmsend_toJson(%encryptedPayload);
my $name = $hash->{NAME}; my $name = $hash->{NAME};
@@ -91,15 +107,13 @@ sub gcmsend_sendPayload($$) {
} }
return undef if (int(@registrationIds) == 0); return undef if (int(@registrationIds) == 0);
my $unixTtimestamp = time*1000;
my $data = my $data =
"{". "{".
"\"registration_ids\":".gcmsend_array_to_json(@registrationIds).",". "\"registration_ids\":".gcmsend_array_to_json(@registrationIds).",".
"\"data\": $payload". "\"data\": $jsonPayload".
"}"; "}";
Log $logLevel, "data is $payload"; Log $logLevel, "data is $jsonPayload";
my $req = HTTP::Request->new( POST => "https://android.googleapis.com/gcm/send" ); my $req = HTTP::Request->new( POST => "https://android.googleapis.com/gcm/send" );
$req->header( Authorization => 'key='.$apikey ); $req->header( Authorization => 'key='.$apikey );
@@ -115,8 +129,8 @@ sub gcmsend_sendPayload($$) {
return undef; return undef;
} }
sub gcmsend_fillGeneralPayload($$) { sub gcmsend_getGeneralPayload($) {
my ($hash, $payloadString) = @_; my ($hash) = @_;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
@@ -124,28 +138,69 @@ sub gcmsend_fillGeneralPayload($$) {
if (AttrVal($name, "vibrate", "false") eq "true") { if (AttrVal($name, "vibrate", "false") eq "true") {
$vibrate = "true"; $vibrate = "true";
} }
my $playSound = "false";
if (AttrVal($name, "playSound", "false") eq "true") {
$playSound = "true";
}
return $payloadString . "," . my $gcmName = $hash->{NAME};
"\"source\":\"gcmsend_fhem\"," .
"\"vibrate\":\"$vibrate\"," . my %generalPayload = (
"\"playSound\":\"$playSound\""; "source" => "gcmsend_fhem",
"gcmDeviceName" => $gcmName,
"vibrate" => "$vibrate"
);
return %generalPayload;
} }
sub gcmsend_sendNotify($$$) { sub gcmsend_sendNotify($$$) {
my ($hash, $deviceName, $changes) = @_; my ($hash, $deviceName, $changes) = @_;
my %payload = (
"deviceName" => $deviceName,
"changes" => $changes,
"type" => "notify"
);
gcmsend_sendPayload($hash, %payload);
}
my $payload = sub gcmsend_toJson(%) {
"\"deviceName\": \"$deviceName\"," . my (%hash) = @_;
"\"changes\":\"$changes\"," . my @entries = ();
"\"type\":\"notify\"";
$payload = "{" . gcmsend_fillGeneralPayload($hash, $payload) . "}"; while (my ($key, $value) = each %hash) {
my $entry = "\"$key\":\"$value\"";
push @entries, $entry;
}
return "{".join(", ", @entries)."}";
}
gcmsend_sendPayload($hash, $payload); my %gcmsend_encrypt_keys = ("type" => "", "notifyId" => "", "changes" => "", "deviceName" => "",
"tickerText" => "", "contentText" => "", "contentTitle" => "");
sub gcmsend_encrypt($%) {
my ($hash, %payload) = @_;
my $key = AttrVal($hash->{NAME}, "cryptKey", "");
if ($key eq "") {
return %payload;
}
my $cipher = Crypt::CBC->new(
-cipher => 'Crypt::Cipher::AES',
-key => $key,
-iv => $key,
-padding => 'standard',
-header => 'none',
-blocksize => '16',
-literal_key => 1,
-keysize => 16
);
my %newPayload = ();
while (my ($key, $value) = each %payload) {
if (exists(%gcmsend_encrypt_keys->{$key})) {
my $padded = sprintf '%16s', $value;
my $length = length($padded);
%newPayload->{$key} = $cipher->encrypt_hex( $value );
} else {
%newPayload->{$key} = $value;
}
}
return %newPayload;
} }
sub gcmsend_sendMessage($$) { sub gcmsend_sendMessage($$) {
@@ -176,19 +231,14 @@ sub gcmsend_sendMessage($$) {
return "Illegal message format. Required format is \r\n ". return "Illegal message format. Required format is \r\n ".
"tickerText|contentTitle|contentText[|NotifyID]"; "tickerText|contentTitle|contentText[|NotifyID]";
} }
my %payload = (
my $payload = "tickerText" => $tickerText,
"\"tickerText\":\"$tickerText\"," . "contentTitle" => $contentTitle,
"\"contentTitle\":\"$contentTitle\"," . "contentText" => $contentText,
"\"contentText\":\"$contentText\"," . "notifyId" => $notifyId,
"\"notifyId\":\"$notifyId\"," . "type" => "message"
"\"source\":\"gcmsend_fhem\"," . );
"\"type\":\"message\"" gcmsend_sendPayload($hash, %payload);
;
$payload = "{" . gcmsend_fillGeneralPayload($hash, $payload) . "}";
gcmsend_sendPayload($hash, $payload);
return undef; return undef;
} }
@@ -235,8 +285,6 @@ sub gcmsend_notify($$)
for (my $i = 0; $i < $nrOfFieldChanges; $i++) { for (my $i = 0; $i < $nrOfFieldChanges; $i++) {
my @keyValue = split(":", $dev->{CHANGED}[$i]); my @keyValue = split(":", $dev->{CHANGED}[$i]);
my $length = int($keyValue);
my $change = $dev->{CHANGED}[$i]; my $change = $dev->{CHANGED}[$i];
@@ -257,12 +305,14 @@ sub gcmsend_notify($$)
} }
if (!($stateFilter eq "") && !($value =~ m/$stateFilter/)) { if (!($stateFilter eq "") && !($value =~ m/$stateFilter/)) {
Log $logLevel, "$gcmName $name: ignoring $key, as value $value is blocked by stateFilter regexp."; Log $logLevel,
"$gcmName $name: ignoring $key, as value $value is blocked by stateFilter regexp.";
} elsif ($value eq "") { } elsif ($value eq "") {
Log $logLevel, "$gcmName $name: ignoring $key, as value is empty."; Log $logLevel, "$gcmName $name: ignoring $key, as value is empty.";
} elsif ($lastDeviceStates->{$key} && $lastDeviceStates->{$key} eq $value) { } elsif ($lastDeviceStates->{$key} && $lastDeviceStates->{$key} eq $value) {
my $savedValue = $lastDeviceStates->{$key}; my $savedValue = $lastDeviceStates->{$key};
Log $logLevel, "$gcmName $name: ignoring $key, save value is $savedValue, value is $value"; Log $logLevel,
"$gcmName $name: ignoring $key, save value is $savedValue, value is $value";
} else { } else {
$lastDeviceStates->{$key} = $value; $lastDeviceStates->{$key} = $value;
# Multiple field updates are separated by <|>. # Multiple field updates are separated by <|>.
@@ -314,7 +364,8 @@ sub gcmsend_notify($$)
Notes: Notes:
<ul> <ul>
<li>Module to send messages to GCM (Google Cloud Messaging).</li> <li>Module to send messages to GCM (Google Cloud Messaging).</li>
<li>Prerequisite is a GCM AcsendFieldCount with Google (see <a href="https://code.google.com/apis/console/">Google API Console</a></li> <li>Prerequisite is a GCM Account (see <a href="https://code.google.com/apis/console/">Google API Console</a></li>
<li>Futhermore <code>Crypt::CBC</code> and <code>Crypt::Cipher::AES</code> Perl modules have to be installed
</ul> </ul>
</ul> </ul>
@@ -349,8 +400,7 @@ sub gcmsend_notify($$)
<br />Make the receiving device vibrate upon receiving the message. Must be true or false.</li> <br />Make the receiving device vibrate upon receiving the message. Must be true or false.</li>
<li><a name="gcmsend_deviceFilter"><code>attr &lt;name&gt; deviceFilter &lt;regexp&gt;</a> <li><a name="gcmsend_deviceFilter"><code>attr &lt;name&gt; deviceFilter &lt;regexp&gt;</a>
<br />Send a GCM notify only is the device name matches the given filter regexp.</li> <br />Send a GCM notify only is the device name matches the given filter regexp.</li>
<li><a name="gcmsend_playSound"><code attr &lt;name&gt; playSound &lt;true|false&gt;</a> <li><a name="gcmsend_cryptKey"><code>attr &lt;name&gt; cryptKey &lt;key&gt;</a> <br/>Some key to encrypt message content. The key must have a size of 16 bytes. If the key length does not match it will be either cut or padded to the required length. As encryption algorithm AES is used.</li>
<br />Specifies that the implementation of GCM should play a sound when an event is received. Note that andFHEM does not implement this attribute yet.</li>
</ul> </ul>
</ul> </ul>