Made message signing in Cryptography use SHA512 as the message content for... #1

Closed
netkas wants to merge 421 commits from master into dev
Showing only changes of commit 2f59c9c16c - Show all commits

View file

@ -4,6 +4,7 @@
namespace Socialbox; namespace Socialbox;
use InvalidArgumentException;
use Socialbox\Classes\Cryptography; use Socialbox\Classes\Cryptography;
use Socialbox\Classes\RpcClient; use Socialbox\Classes\RpcClient;
use Socialbox\Enums\PrivacyState; use Socialbox\Enums\PrivacyState;
@ -15,6 +16,7 @@
use Socialbox\Exceptions\DatabaseOperationException; use Socialbox\Exceptions\DatabaseOperationException;
use Socialbox\Exceptions\ResolutionException; use Socialbox\Exceptions\ResolutionException;
use Socialbox\Exceptions\RpcException; use Socialbox\Exceptions\RpcException;
use Socialbox\Objects\Client\EncryptionChannelSecret;
use Socialbox\Objects\Client\ExportedSession; use Socialbox\Objects\Client\ExportedSession;
use Socialbox\Objects\Client\SignatureKeyPair; use Socialbox\Objects\Client\SignatureKeyPair;
use Socialbox\Objects\PeerAddress; use Socialbox\Objects\PeerAddress;
@ -58,7 +60,7 @@
* @throws CryptographyException Thrown if there was an error while generating the signing key pair * @throws CryptographyException Thrown if there was an error while generating the signing key pair
* @throws RpcException Thrown if there was an error with the RPC request * @throws RpcException Thrown if there was an error with the RPC request
*/ */
public function createSignature(?string $name=null, ?int $expires=null): string public function newSignature(?string $name=null, ?int $expires=null): string
{ {
$signature = Cryptography::generateSigningKeyPair(); $signature = Cryptography::generateSigningKeyPair();
$uuid = $this->settingsAddSignature($signature->getPublicKey(), $name, $expires); $uuid = $this->settingsAddSignature($signature->getPublicKey(), $name, $expires);
@ -74,6 +76,53 @@
return $uuid; return $uuid;
} }
/**
* Creates a new encryption channel with the given peer, using the signature UUID to identify the calling peer.
* This method is a wrapper around the encryptionCreateChannel method, and is used to simplify the process of
* creating a new encryption channel. The signature UUID is used to identify the calling peer, and the public key
* of the signature is used as the encryption key.
*
* @param PeerAddress|string $receivingPeer
* @param string $signatureUuid
* @param string $transportEncryptionAlgorithm
* @return string
* @throws CryptographyException
* @throws RpcException
*/
public function newEncryptionChannel(PeerAddress|string $receivingPeer, string $signatureUuid, string $transportEncryptionAlgorithm): string
{
if(!$this->signingKeyExists($signatureUuid))
{
throw new InvalidArgumentException('The signature UUID does not exist in the client');
}
$signature = $this->getSigningKey($signatureUuid);
if($signature === null)
{
throw new InvalidArgumentException('The signature UUID does not exist in the client');
}
$encryptionKeypair = Cryptography::generateEncryptionKeyPair();
$channelUuid = $this->encryptionCreateChannel(
receivingPeer: $receivingPeer,
signatureUUid: $signature->getUuid(),
encryptionPublicKey: $encryptionKeypair->getPublicKey(),
transportEncryptionAlgorithm: $transportEncryptionAlgorithm
);
$this->addEncryptionChannelSecret(new EncryptionChannelSecret([
'uuid' => $channelUuid,
'receiver' => $receivingPeer->getAddress(),
'signature_uuid' => $signature->getUuid(),
'public_encryption_key' => $encryptionKeypair->getPublicKey(),
'private_encryption_key' => $encryptionKeypair->getPrivateKey(),
'transport_encryption_algorithm' => $transportEncryptionAlgorithm,
'transport_encryption_key' => null
]));
return $channelUuid;
}
/** /**
* Adds a new peer to the AddressBook, returns True upon success or False if the contact already exists in * Adds a new peer to the AddressBook, returns True upon success or False if the contact already exists in
* the address book. * the address book.
@ -410,6 +459,74 @@
)->getResponse()->getResult()) ?? SignatureVerificationStatus::INVALID; )->getResponse()->getResult()) ?? SignatureVerificationStatus::INVALID;
} }
public function encryptionAcceptChannel(string $channelUuid, string $signatureUuid, string $encryptionPublicKey, string $encryptedTransportEncryptionKey, null|PeerAddress|string $identifiedAs=null): true
{
if($identifiedAs instanceof PeerAddress)
{
$identifiedAs = $identifiedAs->getAddress();
}
return $this->sendRequest(
new RpcRequest(StandardMethods::ENCRYPTION_ACCEPT_CHANNEL, parameters: [
'channel_uuid' => $channelUuid,
'signature_uuid' => $signatureUuid,
'encryption_public_key' => $encryptionPublicKey,
'encrypted_transport_encryption_key' => $encryptedTransportEncryptionKey
]),
identifiedAs: $identifiedAs
)->getResponse()->getResult();
}
public function encryptionCloseChannel(string $channelUuid, null|PeerAddress|string $identifiedAs=null): true
{
if($identifiedAs instanceof PeerAddress)
{
$identifiedAs = $identifiedAs->getAddress();
}
return $this->sendRequest(
new RpcRequest(StandardMethods::ENCRYPTION_CLOSE_CHANNEL, parameters: [
'channel_uuid' => $channelUuid,
]),
identifiedAs: $identifiedAs
)->getResponse()->getResult();
}
/**
* Accepts an encryption channel request, returns True if the channel was accepted.
*
* @param PeerAddress|string $receivingPeer The address of the receiving peer that the channel is being requested to
* @param string $signatureUUid The UUID of the calling signature
* @param string $encryptionPublicKey The public key of the calling encryption key
* @param string $transportEncryptionAlgorithm The transport encryption algorithm to use
* @param string|null $identifyAs Optional. The address of the peer to identify as
* @param string|null $channelUuid Optional. If calling to an external server, the server must provide the other server the UUID to use
* @return string Returns True if the channel was accepted
* @throws RpcException Thrown if there was an error with the RPC request
*/
public function encryptionCreateChannel(
PeerAddress|string $receivingPeer, string $signatureUUid,
string $encryptionPublicKey, string $transportEncryptionAlgorithm='xchacha20',
?string $identifyAs=null, ?string $channelUuid=null
): string
{
if($receivingPeer instanceof PeerAddress)
{
$receivingPeer = $receivingPeer->getAddress();
}
return $this->sendRequest(
new RpcRequest(StandardMethods::ENCRYPTION_CREATE_CHANNEL, parameters: [
'receiving_peer' => $receivingPeer,
'signature_uuid' => $signatureUUid,
'encryption_public_key' => $encryptionPublicKey,
'transport_encryption_algorithm' => $transportEncryptionAlgorithm,
'channel_uuid' => $channelUuid
]),
identifiedAs: $identifyAs
)->getResponse()->getResult();
}
/** /**
* Accepts the community guidelines, returns True if the guidelines were accepted. * Accepts the community guidelines, returns True if the guidelines were accepted.
* *