Changed Encryption to use LibSodium instead of OpenSSL, refactored many things and overall improved the code quality and performance with magic.

This commit is contained in:
netkas 2025-01-03 12:27:04 -05:00
parent 46ad03a54d
commit 367399f0fd
44 changed files with 2971 additions and 2016 deletions

View file

@ -2,10 +2,7 @@
namespace Socialbox\Objects;
use InvalidArgumentException;
use Socialbox\Classes\Cryptography;
use Socialbox\Classes\Utilities;
use Socialbox\Enums\SessionState;
use Socialbox\Enums\StandardHeaders;
use Socialbox\Enums\Types\RequestType;
use Socialbox\Exceptions\CryptographyException;
@ -18,7 +15,7 @@
class ClientRequest
{
private array $headers;
private RequestType $requestType;
private ?RequestType $requestType;
private ?string $requestBody;
private ?string $clientName;
@ -27,6 +24,14 @@
private ?string $sessionUuid;
private ?string $signature;
/**
* Initializes the instance with the provided request headers and optional request body.
*
* @param array $headers An associative array of request headers used to set properties such as client name, version, and others.
* @param string|null $requestBody The optional body of the request, or null if not provided.
*
* @return void
*/
public function __construct(array $headers, ?string $requestBody)
{
$this->headers = $headers;
@ -34,17 +39,28 @@
$this->clientName = $headers[StandardHeaders::CLIENT_NAME->value] ?? null;
$this->clientVersion = $headers[StandardHeaders::CLIENT_VERSION->value] ?? null;
$this->requestType = RequestType::from($headers[StandardHeaders::REQUEST_TYPE->value]);
$this->requestType = RequestType::tryFrom($headers[StandardHeaders::REQUEST_TYPE->value]);
$this->identifyAs = $headers[StandardHeaders::IDENTIFY_AS->value] ?? null;
$this->sessionUuid = $headers[StandardHeaders::SESSION_UUID->value] ?? null;
$this->signature = $headers[StandardHeaders::SIGNATURE->value] ?? null;
}
/**
* Retrieves the headers.
*
* @return array Returns an array of headers.
*/
public function getHeaders(): array
{
return $this->headers;
}
/**
* Checks if the specified header exists in the collection of headers.
*
* @param StandardHeaders|string $header The header to check, either as a StandardHeaders enum or a string.
* @return bool Returns true if the header exists, otherwise false.
*/
public function headerExists(StandardHeaders|string $header): bool
{
if(is_string($header))
@ -55,6 +71,12 @@
return isset($this->headers[$header->value]);
}
/**
* Retrieves the value of a specified header.
*
* @param StandardHeaders|string $header The header to retrieve, provided as either a StandardHeaders enum or a string key.
* @return string|null Returns the header value if it exists, or null if the header does not exist.
*/
public function getHeader(StandardHeaders|string $header): ?string
{
if(!$this->headerExists($header))
@ -70,26 +92,51 @@
return $this->headers[$header->value];
}
/**
* Retrieves the request body.
*
* @return string|null Returns the request body as a string if available, or null if not set.
*/
public function getRequestBody(): ?string
{
return $this->requestBody;
}
/**
* Retrieves the name of the client.
*
* @return string|null Returns the client's name if set, or null if not available.
*/
public function getClientName(): ?string
{
return $this->clientName;
}
/**
* Retrieves the client version.
*
* @return string|null Returns the client version if available, or null if not set.
*/
public function getClientVersion(): ?string
{
return $this->clientVersion;
}
public function getRequestType(): RequestType
/**
* Retrieves the request type associated with the current instance.
*
* @return RequestType|null Returns the associated RequestType if available, or null if not set.
*/
public function getRequestType(): ?RequestType
{
return $this->requestType;
}
/**
* Retrieves the peer address the instance identifies as.
*
* @return PeerAddress|null Returns a PeerAddress instance if the identification address is set, or null otherwise.
*/
public function getIdentifyAs(): ?PeerAddress
{
if($this->identifyAs === null)
@ -100,11 +147,21 @@
return PeerAddress::fromAddress($this->identifyAs);
}
/**
* Retrieves the UUID of the current session.
*
* @return string|null Returns the session UUID if available, or null if it is not set.
*/
public function getSessionUuid(): ?string
{
return $this->sessionUuid;
}
/**
* Retrieves the current session associated with the session UUID.
*
* @return SessionRecord|null Returns the associated SessionRecord if the session UUID exists, or null if no session UUID is set.
*/
public function getSession(): ?SessionRecord
{
if($this->sessionUuid === null)
@ -115,6 +172,11 @@
return SessionManager::getSession($this->sessionUuid);
}
/**
* Retrieves the associated peer for the current session.
*
* @return RegisteredPeerRecord|null Returns the associated RegisteredPeerRecord if available, or null if no session exists.
*/
public function getPeer(): ?RegisteredPeerRecord
{
$session = $this->getSession();
@ -127,11 +189,22 @@
return RegisteredPeerManager::getPeer($session->getPeerUuid());
}
/**
* Retrieves the signature value.
*
* @return string|null The signature value or null if not set
*/
public function getSignature(): ?string
{
return $this->signature;
}
/**
* Verifies the signature of the provided decrypted content.
*
* @param string $decryptedContent The decrypted content to verify the signature against.
* @return bool True if the signature is valid, false otherwise.
*/
private function verifySignature(string $decryptedContent): bool
{
if($this->getSignature() == null || $this->getSessionUuid() == null)
@ -141,7 +214,11 @@
try
{
return Cryptography::verifyContent($decryptedContent, $this->getSignature(), $this->getSession()->getPublicKey(), true);
return Cryptography::verifyMessage(
message: $decryptedContent,
signature: $this->getSignature(),
publicKey: $this->getSession()->getClientPublicSigningKey()
);
}
catch(CryptographyException)
{
@ -156,52 +233,12 @@
* @return RpcRequest[] The parsed RpcRequest objects
* @throws RequestException Thrown if the request is invalid
*/
public function getRpcRequests(): array
public function getRpcRequests(string $json): array
{
if($this->getSessionUuid() === null)
$body = json_decode($json, true);
if($body === false)
{
throw new RequestException("Session UUID required", 400);
}
// Get the existing session
$session = $this->getSession();
// If we're awaiting a DHE, encryption is not possible at this point
if($session->getState() === SessionState::AWAITING_DHE)
{
throw new RequestException("DHE exchange required", 400);
}
// If the session is not active, we can't serve these requests
if($session->getState() !== SessionState::ACTIVE)
{
throw new RequestException("Session is not active", 400);
}
// Attempt to decrypt the content and verify the signature of the request
try
{
$decrypted = Cryptography::decryptTransport($this->requestBody, $session->getEncryptionKey());
if(!$this->verifySignature($decrypted))
{
throw new RequestException("Invalid request signature", 401);
}
}
catch (CryptographyException $e)
{
throw new RequestException("Failed to decrypt request body", 400, $e);
}
// At this stage, all checks has passed; we can try parsing the RPC request
try
{
// Decode the request body
$body = Utilities::jsonDecode($decrypted);
}
catch(InvalidArgumentException $e)
{
throw new RequestException("Invalid JSON in request body: " . $e->getMessage(), 400, $e);
throw new RequestException('Malformed JSON', 400);
}
// If the body only contains a method, we assume it's a single request

View file

@ -1,41 +0,0 @@
<?php
namespace Socialbox\Objects\Database;
class DecryptedRecord
{
private string $key;
private string $pepper;
private string $salt;
public function __construct(array $data)
{
$this->key = $data['key'];
$this->pepper = $data['pepper'];
$this->salt = $data['salt'];
}
/**
* @return string
*/
public function getKey(): string
{
return $this->key;
}
/**
* @return string
*/
public function getPepper(): string
{
return $this->pepper;
}
/**
* @return string
*/
public function getSalt(): string
{
return $this->salt;
}
}

View file

@ -1,83 +0,0 @@
<?php
namespace Socialbox\Objects\Database;
use Socialbox\Classes\Configuration;
use Socialbox\Classes\SecuredPassword;
use Socialbox\Exceptions\CryptographyException;
class EncryptionRecord
{
private string $data;
private string $iv;
private string $tag;
/**
* Public constructor for the EncryptionRecord
*
* @param array $data
*/
public function __construct(array $data)
{
$this->data = $data['data'];
$this->iv = $data['iv'];
$this->tag = $data['tag'];
}
/**
* Retrieves the stored data.
*
* @return string The stored data.
*/
public function getData(): string
{
return $this->data;
}
/**
* Retrieves the initialization vector (IV).
*
* @return string The initialization vector.
*/
public function getIv(): string
{
return $this->iv;
}
/**
* Retrieves the tag.
*
* @return string The tag.
*/
public function getTag(): string
{
return $this->tag;
}
/**
* Decrypts the encrypted record using available encryption keys.
*
* Iterates through the configured encryption keys to attempt decryption of the data.
* If successful, returns a DecryptedRecord object with the decrypted data.
* Throws an exception if decryption fails with all available keys.
*
* @return DecryptedRecord The decrypted record containing the original data.
* @throws CryptographyException If decryption fails with all provided keys.
*/
public function decrypt(): DecryptedRecord
{
foreach(Configuration::getInstanceConfiguration()->getEncryptionKeys() as $encryptionKey)
{
$decryptedVault = openssl_decrypt(base64_decode($this->data), SecuredPassword::ENCRYPTION_ALGORITHM,
$encryptionKey, OPENSSL_RAW_DATA, base64_decode($this->iv), base64_decode($this->tag)
);
if ($decryptedVault !== false)
{
return new DecryptedRecord(json_decode($decryptedVault, true));
}
}
throw new CryptographyException("Decryption failed");
}
}

View file

@ -1,110 +1,144 @@
<?php
namespace Socialbox\Objects\Database;
namespace Socialbox\Objects\Database;
use DateTime;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Objects\ResolvedServer;
use DateTime;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Objects\DnsRecord;
class ResolvedServerRecord implements SerializableInterface
{
private string $domain;
private string $endpoint;
private string $publicKey;
private DateTime $updated;
/**
* Constructs a new instance of the class.
*
* @param array $data An associative array containing the domain, endpoint, public_key, and updated values.
* @throws \DateMalformedStringException
*/
public function __construct(array $data)
class ResolvedServerRecord implements SerializableInterface
{
$this->domain = (string)$data['domain'];
$this->endpoint = (string)$data['endpoint'];
$this->publicKey = (string)$data['public_key'];
private string $domain;
private string $endpoint;
private string $publicKey;
private DateTime $expires;
private DateTime $updated;
if(is_null($data['updated']))
/**
* Constructs a new instance of the class.
*
* @param array $data An associative array containing the domain, endpoint, public_key, and updated values.
* @throws \DateMalformedStringException
*/
public function __construct(array $data)
{
$this->updated = new DateTime();
$this->domain = (string)$data['domain'];
$this->endpoint = (string)$data['endpoint'];
$this->publicKey = (string)$data['public_key'];
if(is_null($data['expires']))
{
$this->expires = new DateTime();
}
elseif (is_int($data['expires']))
{
$this->expires = (new DateTime())->setTimestamp($data['expires']);
}
elseif (is_string($data['expires']))
{
$this->expires = new DateTime($data['expires']);
}
else
{
$this->expires = $data['expires'];
}
if(is_null($data['updated']))
{
$this->updated = new DateTime();
}
elseif (is_int($data['updated']))
{
$this->updated = (new DateTime())->setTimestamp($data['updated']);
}
elseif (is_string($data['updated']))
{
$this->updated = new DateTime($data['updated']);
}
else
{
$this->updated = $data['updated'];
}
}
elseif (is_string($data['updated']))
/**
* Retrieves the domain value.
*
* @return string The domain as a string.
*/
public function getDomain(): string
{
$this->updated = new DateTime($data['updated']);
return $this->domain;
}
else
/**
* Retrieves the configured endpoint.
*
* @return string The endpoint as a string.
*/
public function getEndpoint(): string
{
$this->updated = $data['updated'];
return $this->endpoint;
}
}
/**
*
* @return string The domain value.
*/
public function getDomain(): string
{
return $this->domain;
}
/**
* Retrieves the public key.
*
* @return string The public key as a string.
*/
public function getPublicKey(): string
{
return $this->publicKey;
}
/**
*
* @return string The endpoint value.
*/
public function getEndpoint(): string
{
return $this->endpoint;
}
/**
* Retrieves the expiration timestamp.
*
* @return DateTime The DateTime object representing the expiration time.
*/
public function getExpires(): DateTime
{
return $this->expires;
}
/**
*
* @return string The public key.
*/
public function getPublicKey(): string
{
return $this->publicKey;
}
/**
* Retrieves the timestamp of the last update.
*
* @return DateTime The DateTime object representing the last update time.
*/
public function getUpdated(): DateTime
{
return $this->updated;
}
/**
* Retrieves the timestamp of the last update.
*
* @return DateTime The DateTime object representing the last update time.
*/
public function getUpdated(): DateTime
{
return $this->updated;
}
/**
* Fetches the DNS record based on the provided endpoint, public key, and expiration time.
*
* @return DnsRecord An instance of the DnsRecord containing the endpoint, public key, and expiration timestamp.
*/
public function getDnsRecord(): DnsRecord
{
return new DnsRecord($this->endpoint, $this->publicKey, $this->expires->getTimestamp());
}
/**
* Converts the record to a ResolvedServer object.
*
* @return ResolvedServer The ResolvedServer object.
*/
public function toResolvedServer(): ResolvedServer
{
return new ResolvedServer($this->endpoint, $this->publicKey);
}
/**
* @inheritDoc
*/
public static function fromArray(array $data): object
{
return new self($data);
}
/**
* @inheritDoc
* @throws \DateMalformedStringException
*/
public static function fromArray(array $data): object
{
return new self($data);
}
/**
* @inheritDoc
*/
public function toArray(): array
{
return [
'domain' => $this->domain,
'endpoint' => $this->endpoint,
'public_key' => $this->publicKey,
'updated' => $this->updated->format('Y-m-d H:i:s')
];
}
}
/**
* @inheritDoc
*/
public function toArray(): array
{
return [
'domain' => $this->domain,
'endpoint' => $this->endpoint,
'public_key' => $this->publicKey,
'updated' => $this->updated->format('Y-m-d H:i:s')
];
}
}

View file

@ -1,108 +0,0 @@
<?php
namespace Socialbox\Objects\Database;
use DateTime;
class SecurePasswordRecord
{
private string $peerUuid;
private string $iv;
private string $encryptedPassword;
private string $encryptedTag;
private DateTime $updated;
/**
* Constructor to initialize the object with provided data.
*
* @param array $data An associative array containing keys:
* - 'peer_uuid': The UUID of the peer.
* - 'iv': The initialization vector.
* - 'encrypted_password': The encrypted password.
* - 'encrypted_tag': The encrypted tag.
*
* @throws \DateMalformedStringException
*/
public function __construct(array $data)
{
$this->peerUuid = $data['peer_uuid'];
$this->iv = $data['iv'];
$this->encryptedPassword = $data['encrypted_password'];
$this->encryptedTag = $data['encrypted_tag'];
if($data['updated'] instanceof DateTime)
{
$this->updated = $data['updated'];
}
else
{
$this->updated = new DateTime($data['updated']);
}
}
/**
* Retrieves the UUID of the peer.
*
* @return string The UUID of the peer.
*/
public function getPeerUuid(): string
{
return $this->peerUuid;
}
/**
* Retrieves the initialization vector (IV) value.
*
* @return string The initialization vector.
*/
public function getIv(): string
{
return $this->iv;
}
/**
* Retrieves the encrypted password.
*
* @return string The encrypted password.
*/
public function getEncryptedPassword(): string
{
return $this->encryptedPassword;
}
/**
* Retrieves the encrypted tag.
*
* @return string The encrypted tag.
*/
public function getEncryptedTag(): string
{
return $this->encryptedTag;
}
/**
* Retrieves the updated timestamp.
*
* @return DateTime The updated timestamp.
*/
public function getUpdated(): DateTime
{
return $this->updated;
}
public function toArray(): array
{
return [
'peer_uuid' => $this->peerUuid,
'iv' => $this->iv,
'encrypted_password' => $this->encryptedPassword,
'encrypted_tag' => $this->encryptedTag,
'updated' => $this->updated->format('Y-m-d H:i:s')
];
}
public static function fromArray(array $data): SecurePasswordRecord
{
return new SecurePasswordRecord($data);
}
}

View file

@ -15,9 +15,13 @@
private string $clientName;
private string $clientVersion;
private bool $authenticated;
private string $publicKey;
private string $clientPublicSigningKey;
public string $clientPublicEncryptionKey;
private string $serverPublicEncryptionKey;
private string $serverPrivateEncryptionKey;
private ?string $clientTransportEncryptionKey;
private ?string $serverTransportEncryptionKey;
private SessionState $state;
private ?string $encryptionKey;
/**
* @var SessionFlags[]
*/
@ -42,10 +46,14 @@
$this->clientName = $data['client_name'];
$this->clientVersion = $data['client_version'];
$this->authenticated = $data['authenticated'] ?? false;
$this->publicKey = $data['public_key'];
$this->clientPublicSigningKey = $data['client_public_signing_key'];
$this->clientPublicEncryptionKey = $data['client_public_encryption_key'];
$this->serverPublicEncryptionKey = $data['server_public_encryption_key'];
$this->serverPrivateEncryptionKey = $data['server_private_encryption_key'];
$this->clientTransportEncryptionKey = $data['client_transport_encryption_key'] ?? null;
$this->serverTransportEncryptionKey = $data['server_transport_encryption_key'] ?? null;
$this->created = $data['created'];
$this->lastRequest = $data['last_request'];
$this->encryptionKey = $data['encryption_key'] ?? null;
$this->flags = SessionFlags::fromString($data['flags']);
if(SessionState::tryFrom($data['state']) == null)
@ -99,9 +107,55 @@
*
* @return string Returns the public key as a string.
*/
public function getPublicKey(): string
public function getClientPublicSigningKey(): string
{
return $this->publicKey;
return $this->clientPublicSigningKey;
}
/**
* Retrieves the encryption key associated with the instance.
*
* @return string|null Returns the encryption key as a string, or null if not set.
*/
public function getClientPublicEncryptionKey(): ?string
{
return $this->clientPublicEncryptionKey;
}
/**
* @return string
*/
public function getServerPublicEncryptionKey(): string
{
return $this->serverPublicEncryptionKey;
}
/**
* @return string
*/
public function getServerPrivateEncryptionKey(): string
{
return $this->serverPrivateEncryptionKey;
}
/**
* Retrieves the client encryption key associated with the instance.
*
* @return string|null Returns the client encryption key as a string, or null if not set.
*/
public function getClientTransportEncryptionKey(): ?string
{
return $this->clientTransportEncryptionKey;
}
/**
* Retrieves the server encryption key associated with the instance.
*
* @return string|null Returns the server encryption key as a string, or null if not set.
*/
public function getServerTransportEncryptionKey(): ?string
{
return $this->serverTransportEncryptionKey;
}
/**
@ -114,16 +168,6 @@
return $this->state;
}
/**
* Retrieves the encryption key associated with the instance.
*
* @return string|null Returns the encryption key as a string.
*/
public function getEncryptionKey(): ?string
{
return $this->encryptionKey;
}
/**
* Retrieves the creation date and time of the object.
*
@ -194,6 +238,11 @@
return $this->clientVersion;
}
/**
* Converts the current session state into a standard session state object.
*
* @return \Socialbox\Objects\Standard\SessionState The standardized session state object.
*/
public function toStandardSessionState(): \Socialbox\Objects\Standard\SessionState
{
return new \Socialbox\Objects\Standard\SessionState([
@ -207,10 +256,7 @@
/**
* Creates a new instance of the class using the provided array data.
*
* @param array $data An associative array of data used to initialize the object properties.
* @return object Returns a newly created object instance.
* @inheritDoc
*/
public static function fromArray(array $data): object
{
@ -218,10 +264,7 @@
}
/**
* Converts the object's properties to an associative array.
*
* @return array An associative array representing the object's data, including keys 'uuid', 'peer_uuid',
* 'authenticated', 'public_key', 'state', 'flags', 'created', and 'last_request'.
* @inheritDoc
*/
public function toArray(): array
{
@ -229,7 +272,12 @@
'uuid' => $this->uuid,
'peer_uuid' => $this->peerUuid,
'authenticated' => $this->authenticated,
'public_key' => $this->publicKey,
'client_public_signing_key' => $this->clientPublicSigningKey,
'client_public_encryption_key' => $this->clientPublicEncryptionKey,
'server_public_encryption_key' => $this->serverPublicEncryptionKey,
'server_private_encryption_key' => $this->serverPrivateEncryptionKey,
'client_transport_encryption_key' => $this->clientTransportEncryptionKey,
'server_transport_encryption_key' => $this->serverTransportEncryptionKey,
'state' => $this->state->value,
'flags' => SessionFlags::toString($this->flags),
'created' => $this->created,

View file

@ -0,0 +1,67 @@
<?php
namespace Socialbox\Objects;
class DnsRecord
{
private string $rpcEndpoint;
private string $publicSigningKey;
private int $expires;
/**
* Constructor for initializing the class with required parameters.
*
* @param string $rpcEndpoint The RPC endpoint.
* @param string $publicSigningKey The public signing key.
* @param int $expires The expiration time in seconds.
* @return void
*/
public function __construct(string $rpcEndpoint, string $publicSigningKey, int $expires)
{
$this->rpcEndpoint = $rpcEndpoint;
$this->publicSigningKey = $publicSigningKey;
$this->expires = $expires;
}
/**
* Retrieves the RPC endpoint.
*
* @return string The RPC endpoint.
*/
public function getRpcEndpoint(): string
{
return $this->rpcEndpoint;
}
/**
* Retrieves the public signing key.
*
* @return string Returns the public signing key as a string.
*/
public function getPublicSigningKey(): string
{
return $this->publicSigningKey;
}
/**
* Retrieves the expiration time.
*
* @return int The expiration timestamp as an integer.
*/
public function getExpires(): int
{
return $this->expires;
}
/**
* Creates a new instance of DnsRecord from the provided array of data.
*
* @param array $data An associative array containing the keys 'rpc_endpoint', 'public_key', and 'expires'
* required to instantiate a DnsRecord object.
* @return DnsRecord Returns a new DnsRecord instance populated with the data from the array.
*/
public static function fromArray(array $data): DnsRecord
{
return new DnsRecord($data['rpc_endpoint'], $data['public_key'], $data['expires']);
}
}

View file

@ -2,49 +2,63 @@
namespace Socialbox\Objects;
use Socialbox\Interfaces\SerializableInterface;
/**
* Represents an exported session containing cryptographic keys, identifiers, and endpoints.
*/
class ExportedSession
class ExportedSession implements SerializableInterface
{
private string $peerAddress;
private string $privateKey;
private string $publicKey;
private string $encryptionKey;
private string $serverPublicKey;
private string $rpcEndpoint;
private string $sessionUuid;
private string $sessionUUID;
private string $transportEncryptionAlgorithm;
private int $serverKeypairExpires;
private string $serverPublicSigningKey;
private string $serverPublicEncryptionKey;
private string $clientPublicSigningKey;
private string $clientPrivateSigningKey;
private string $clientPublicEncryptionKey;
private string $clientPrivateEncryptionKey;
private string $privateSharedSecret;
private string $clientTransportEncryptionKey;
private string $serverTransportEncryptionKey;
/**
* Initializes a new instance of the class with the provided data.
* Constructor method to initialize class properties from the provided data array.
*
* @param array $data An associative array containing the configuration data.
* Expected keys:
* - 'peer_address': The address of the peer.
* - 'private_key': The private key for secure communication.
* - 'public_key': The public key for secure communication.
* - 'encryption_key': The encryption key used for communication.
* - 'server_public_key': The server's public key.
* - 'rpc_endpoint': The RPC endpoint for network communication.
* - 'session_uuid': The unique identifier for the session.
* @param array $data Associative array containing the required properties such as:
* 'peer_address', 'rpc_endpoint', 'session_uuid',
* 'server_public_signing_key', 'server_public_encryption_key',
* 'client_public_signing_key', 'client_private_signing_key',
* 'client_public_encryption_key', 'client_private_encryption_key',
* 'private_shared_secret', 'client_transport_encryption_key',
* 'server_transport_encryption_key'.
*
* @return void
*/
public function __construct(array $data)
{
$this->peerAddress = $data['peer_address'];
$this->privateKey = $data['private_key'];
$this->publicKey = $data['public_key'];
$this->encryptionKey = $data['encryption_key'];
$this->serverPublicKey = $data['server_public_key'];
$this->rpcEndpoint = $data['rpc_endpoint'];
$this->sessionUuid = $data['session_uuid'];
$this->sessionUUID = $data['session_uuid'];
$this->transportEncryptionAlgorithm = $data['transport_encryption_algorithm'];
$this->serverKeypairExpires = $data['server_keypair_expires'];
$this->serverPublicSigningKey = $data['server_public_signing_key'];
$this->serverPublicEncryptionKey = $data['server_public_encryption_key'];
$this->clientPublicSigningKey = $data['client_public_signing_key'];
$this->clientPrivateSigningKey = $data['client_private_signing_key'];
$this->clientPublicEncryptionKey = $data['client_public_encryption_key'];
$this->clientPrivateEncryptionKey = $data['client_private_encryption_key'];
$this->privateSharedSecret = $data['private_shared_secret'];
$this->clientTransportEncryptionKey = $data['client_transport_encryption_key'];
$this->serverTransportEncryptionKey = $data['server_transport_encryption_key'];
}
/**
* Retrieves the address of the peer.
* Retrieves the peer address associated with the current instance.
*
* @return string The peer's address as a string.
* @return string The peer address.
*/
public function getPeerAddress(): string
{
@ -52,47 +66,7 @@
}
/**
* Retrieves the private key.
*
* @return string The private key.
*/
public function getPrivateKey(): string
{
return $this->privateKey;
}
/**
* Retrieves the public key.
*
* @return string The public key.
*/
public function getPublicKey(): string
{
return $this->publicKey;
}
/**
* Retrieves the encryption key.
*
* @return string The encryption key.
*/
public function getEncryptionKey(): string
{
return $this->encryptionKey;
}
/**
* Retrieves the public key of the server.
*
* @return string The server's public key.
*/
public function getServerPublicKey(): string
{
return $this->serverPublicKey;
}
/**
* Retrieves the RPC endpoint URL.
* Retrieves the RPC endpoint.
*
* @return string The RPC endpoint.
*/
@ -102,38 +76,150 @@
}
/**
* Retrieves the unique identifier for the current session.
* Retrieves the session UUID associated with the current instance.
*
* @return string The session UUID.
*/
public function getSessionUuid(): string
public function getSessionUUID(): string
{
return $this->sessionUuid;
return $this->sessionUUID;
}
/**
* Converts the current instance into an array representation.
* Retrieves the transport encryption algorithm being used.
*
* @return array An associative array containing the instance properties and their respective values.
* @return string The transport encryption algorithm.
*/
public function getTransportEncryptionAlgorithm(): string
{
return $this->transportEncryptionAlgorithm;
}
/**
* Retrieves the expiration time of the server key pair.
*
* @return int The expiration timestamp of the server key pair.
*/
public function getServerKeypairExpires(): int
{
return $this->serverKeypairExpires;
}
/**
* Retrieves the public signing key of the server.
*
* @return string The server's public signing key.
*/
public function getServerPublicSigningKey(): string
{
return $this->serverPublicSigningKey;
}
/**
* Retrieves the server's public encryption key.
*
* @return string The server's public encryption key.
*/
public function getServerPublicEncryptionKey(): string
{
return $this->serverPublicEncryptionKey;
}
/**
* Retrieves the client's public signing key.
*
* @return string The client's public signing key.
*/
public function getClientPublicSigningKey(): string
{
return $this->clientPublicSigningKey;
}
/**
* Retrieves the client's private signing key.
*
* @return string The client's private signing key.
*/
public function getClientPrivateSigningKey(): string
{
return $this->clientPrivateSigningKey;
}
/**
* Retrieves the public encryption key of the client.
*
* @return string The client's public encryption key.
*/
public function getClientPublicEncryptionKey(): string
{
return $this->clientPublicEncryptionKey;
}
/**
* Retrieves the client's private encryption key.
*
* @return string The client's private encryption key.
*/
public function getClientPrivateEncryptionKey(): string
{
return $this->clientPrivateEncryptionKey;
}
/**
* Retrieves the private shared secret associated with the current instance.
*
* @return string The private shared secret.
*/
public function getPrivateSharedSecret(): string
{
return $this->privateSharedSecret;
}
/**
* Retrieves the client transport encryption key.
*
* @return string The client transport encryption key.
*/
public function getClientTransportEncryptionKey(): string
{
return $this->clientTransportEncryptionKey;
}
/**
* Retrieves the server transport encryption key associated with the current instance.
*
* @return string The server transport encryption key.
*/
public function getServerTransportEncryptionKey(): string
{
return $this->serverTransportEncryptionKey;
}
/**
* @inheritDoc
*/
public function toArray(): array
{
return [
'peer_address' => $this->peerAddress,
'private_key' => $this->privateKey,
'public_key' => $this->publicKey,
'encryption_key' => $this->encryptionKey,
'server_public_key' => $this->serverPublicKey,
'rpc_endpoint' => $this->rpcEndpoint,
'session_uuid' => $this->sessionUuid
'session_uuid' => $this->sessionUUID,
'transport_encryption_algorithm' => $this->transportEncryptionAlgorithm,
'server_keypair_expires' => $this->serverKeypairExpires,
'server_public_signing_key' => $this->serverPublicSigningKey,
'server_public_encryption_key' => $this->serverPublicEncryptionKey,
'client_public_signing_key' => $this->clientPublicSigningKey,
'client_private_signing_key' => $this->clientPrivateSigningKey,
'client_public_encryption_key' => $this->clientPublicEncryptionKey,
'client_private_encryption_key' => $this->clientPrivateEncryptionKey,
'private_shared_secret' => $this->privateSharedSecret,
'client_transport_encryption_key' => $this->clientTransportEncryptionKey,
'server_transport_encryption_key' => $this->serverTransportEncryptionKey,
];
}
/**
* Creates an instance of ExportedSession from the provided array.
*
* @param array $data The input data used to construct the ExportedSession instance.
* @return ExportedSession The new ExportedSession instance created from the given data.
* @inheritDoc
*/
public static function fromArray(array $data): ExportedSession
{

View file

@ -1,25 +1,11 @@
<?php
namespace Socialbox\Objects;
namespace Socialbox\Objects;
class ResolvedServer
{
private string $endpoint;
private string $publicKey;
use Socialbox\Objects\Standard\ServerInformation;
public function __construct(string $endpoint, string $publicKey)
class ResolvedServer
{
$this->endpoint = $endpoint;
$this->publicKey = $publicKey;
}
public function getEndpoint(): string
{
return $this->endpoint;
}
public function getPublicKey(): string
{
return $this->publicKey;
}
}
private DnsRecord $dnsRecord;
private ServerInformation $serverInformation;
}

View file

@ -0,0 +1,25 @@
<?php
namespace Socialbox\Objects;
class ResolvedServer
{
private string $endpoint;
private string $publicKey;
public function __construct(string $endpoint, string $publicKey)
{
$this->endpoint = $endpoint;
$this->publicKey = $publicKey;
}
public function getEndpoint(): string
{
return $this->endpoint;
}
public function getPublicKey(): string
{
return $this->publicKey;
}
}

View file

@ -0,0 +1,75 @@
<?php
namespace Socialbox\Objects\Standard;
use Socialbox\Interfaces\SerializableInterface;
class ServerInformation implements SerializableInterface
{
private string $serverName;
private int $serverKeypairExpires;
private string $transportEncryptionAlgorithm;
/**
* Constructor method to initialize the object with provided data.
*
* @param array $data The array containing initialization parameters, including 'server_name', 'server_keypair_expires', and 'transport_encryption_algorithm'.
* @return void
*/
public function __construct(array $data)
{
$this->serverName = $data['server_name'];
$this->serverKeypairExpires = $data['server_keypair_expires'];
$this->transportEncryptionAlgorithm = $data['transport_encryption_algorithm'];
}
/**
* Retrieves the name of the server.
*
* @return string The server name.
*/
public function getServerName(): string
{
return $this->serverName;
}
/**
* Retrieves the expiration time of the server key pair.
*
* @return int The expiration timestamp of the server key pair.
*/
public function getServerKeypairExpires(): int
{
return $this->serverKeypairExpires;
}
/**
* Retrieves the transport encryption algorithm being used.
*
* @return string The transport encryption algorithm.
*/
public function getTransportEncryptionAlgorithm(): string
{
return $this->transportEncryptionAlgorithm;
}
/**
* @inheritDoc
*/
public static function fromArray(array $data): ServerInformation
{
return new self($data);
}
/**
* @inheritDoc
*/
public function toArray(): array
{
return [
'server_name' => $this->serverName,
'server_keypair_expires' => $this->serverKeypairExpires,
'transport_encryption_algorithm' => $this->transportEncryptionAlgorithm,
];
}
}