Skip to content

Commit

Permalink
Split calculateMac out of connection class to remove duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
pavarnos committed Aug 15, 2018
1 parent 91483fb commit e448be0
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 88 deletions.
14 changes: 7 additions & 7 deletions src/Threema/MsgApi/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

namespace Threema\MsgApi;

use Threema\MsgApi\Encryptor\AbstractEncryptor;
use Threema\MsgApi\Helpers\E2EHelper;
use Threema\MsgApi\Helpers\ReceiveMessageResult;
use Threema\MsgApi\HttpDriver\HttpDriverInterface;
use Threema\MsgApi\Request\CapabilityRequest;
use Threema\MsgApi\Request\CreditsRequest;
use Threema\MsgApi\Request\DownloadFileRequest;
Expand All @@ -18,10 +22,6 @@
use Threema\MsgApi\Request\SendE2ERequest;
use Threema\MsgApi\Request\SendSimpleRequest;
use Threema\MsgApi\Request\UploadFileRequest;
use Threema\MsgApi\Encryptor\AbstractEncryptor;
use Threema\MsgApi\Helpers\E2EHelper;
use Threema\MsgApi\Helpers\ReceiveMessageResult;
use Threema\MsgApi\HttpDriver\HttpDriverInterface;
use Threema\MsgApi\Response\CapabilityResponse;
use Threema\MsgApi\Response\CreditsResponse;
use Threema\MsgApi\Response\DownloadFileResponse;
Expand Down Expand Up @@ -229,7 +229,7 @@ public final function sendFileMessage(string $myPrivateKeyHex, string $toThreema
}

/**
* Check the HMAC of an ingoing Threema request. Always do this before decrypting the message.
* Check the HMAC of an incoming Threema request. Always do this before decrypting the message.
*
* @param string $threemaId
* @param string $gatewayId
Expand All @@ -244,8 +244,8 @@ public final function sendFileMessage(string $myPrivateKeyHex, string $toThreema
public final function macIsValid(string $threemaId, string $gatewayId, string $messageId, string $date,
string $nonce, string $box, string $mac, string $secret): bool
{
$calculatedMac = hash_hmac('sha256', $threemaId . $gatewayId . $messageId . $date . $nonce . $box, $secret);
return hash_equals($calculatedMac, $mac);
return hash_equals($this->encryptor->calculateMac($threemaId, $gatewayId, $messageId, $date, $nonce, $box,
$secret), $mac);
}

/**
Expand Down
60 changes: 0 additions & 60 deletions src/Threema/MsgApi/Helpers/AssocArray.php

This file was deleted.

21 changes: 0 additions & 21 deletions src/Threema/MsgApi/Helpers/E2EHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public function __construct(string $privateKeyBinary, Connection $connection, Ab
* @param string $receiverPublicKey binary format
* @param string $text
* @return \Threema\MsgApi\Response\SendE2EResponse
* @throws \Threema\MsgApi\Exceptions\Exception
*/
public final function sendTextMessage(string $threemaId, string $receiverPublicKey, string $text): SendE2EResponse
{
Expand Down Expand Up @@ -290,26 +289,6 @@ public final function receiveMessage(
return $receiveResult;
}

/**
* Check the HMAC of an ingoing Threema request. Always do this before decrypting the message.
*
* @param string $threemaId
* @param string $gatewayId
* @param string $messageId
* @param string $date
* @param string $nonce nonce as hex encoded string
* @param string $box box as hex encoded string
* @param string $mac the original one send by the server
* @param string $secret hex
* @return bool true if check was successful, false if not
*/
public final function checkMac(string $threemaId, string $gatewayId, string $messageId, string $date, string $nonce,
string $box, string $mac, string $secret): bool
{
$calculatedMac = hash_hmac('sha256', $threemaId . $gatewayId . $messageId . $date . $nonce . $box, $secret);
return hash_equals($calculatedMac, $mac);
}

private function assertIsCapable(string $threemaId, string $wantedCapability)
{
$capability = $this->connection->keyCapability($threemaId);
Expand Down

0 comments on commit e448be0

Please sign in to comment.