primo commit

This commit is contained in:
2024-12-17 17:34:10 +01:00
commit e650f8df99
16435 changed files with 2451012 additions and 0 deletions

View File

@ -0,0 +1,111 @@
<?php
declare(strict_types=1);
namespace Jose\Component\Encryption\Serializer;
use InvalidArgumentException;
use Jose\Component\Core\Util\Base64UrlSafe;
use Jose\Component\Core\Util\JsonConverter;
use Jose\Component\Encryption\JWE;
use Jose\Component\Encryption\Recipient;
use LogicException;
use Throwable;
use function count;
use function is_array;
final class CompactSerializer implements JWESerializer
{
public const NAME = 'jwe_compact';
public function displayName(): string
{
return 'JWE Compact';
}
public function name(): string
{
return self::NAME;
}
public function serialize(JWE $jwe, ?int $recipientIndex = null): string
{
if ($recipientIndex === null) {
$recipientIndex = 0;
}
$recipient = $jwe->getRecipient($recipientIndex);
$this->checkHasNoAAD($jwe);
$this->checkHasSharedProtectedHeader($jwe);
$this->checkRecipientHasNoHeader($jwe, $recipientIndex);
return sprintf(
'%s.%s.%s.%s.%s',
$jwe->getEncodedSharedProtectedHeader(),
Base64UrlSafe::encodeUnpadded($recipient->getEncryptedKey() ?? ''),
Base64UrlSafe::encodeUnpadded($jwe->getIV() ?? ''),
Base64UrlSafe::encodeUnpadded($jwe->getCiphertext() ?? ''),
Base64UrlSafe::encodeUnpadded($jwe->getTag() ?? '')
);
}
public function unserialize(string $input): JWE
{
$parts = explode('.', $input);
if (count($parts) !== 5) {
throw new InvalidArgumentException('Unsupported input');
}
try {
$encodedSharedProtectedHeader = $parts[0];
$sharedProtectedHeader = JsonConverter::decode(
Base64UrlSafe::decodeNoPadding($encodedSharedProtectedHeader)
);
if (! is_array($sharedProtectedHeader)) {
throw new InvalidArgumentException('Unsupported input.');
}
$encryptedKey = $parts[1] === '' ? null : Base64UrlSafe::decodeNoPadding($parts[1]);
$iv = Base64UrlSafe::decodeNoPadding($parts[2]);
$ciphertext = Base64UrlSafe::decodeNoPadding($parts[3]);
$tag = Base64UrlSafe::decodeNoPadding($parts[4]);
return new JWE(
$ciphertext,
$iv,
$tag,
null,
[],
$sharedProtectedHeader,
$encodedSharedProtectedHeader,
[new Recipient([], $encryptedKey)]
);
} catch (Throwable $throwable) {
throw new InvalidArgumentException('Unsupported input', $throwable->getCode(), $throwable);
}
}
private function checkHasNoAAD(JWE $jwe): void
{
if ($jwe->getAAD() !== null) {
throw new LogicException('This JWE has AAD and cannot be converted into Compact JSON.');
}
}
private function checkRecipientHasNoHeader(JWE $jwe, int $id): void
{
if (count($jwe->getSharedHeader()) !== 0 || count($jwe->getRecipient($id)->getHeader()) !== 0) {
throw new LogicException(
'This JWE has shared header parameters or recipient header parameters and cannot be converted into Compact JSON.'
);
}
}
private function checkHasSharedProtectedHeader(JWE $jwe): void
{
if (count($jwe->getSharedProtectedHeader()) === 0) {
throw new LogicException(
'This JWE does not have shared protected header parameters and cannot be converted into Compact JSON.'
);
}
}
}

View File

@ -0,0 +1,107 @@
<?php
declare(strict_types=1);
namespace Jose\Component\Encryption\Serializer;
use InvalidArgumentException;
use Jose\Component\Core\Util\Base64UrlSafe;
use Jose\Component\Core\Util\JsonConverter;
use Jose\Component\Encryption\JWE;
use Jose\Component\Encryption\Recipient;
use function array_key_exists;
use function count;
use function is_array;
final class JSONFlattenedSerializer implements JWESerializer
{
public const NAME = 'jwe_json_flattened';
public function displayName(): string
{
return 'JWE JSON Flattened';
}
public function name(): string
{
return self::NAME;
}
public function serialize(JWE $jwe, ?int $recipientIndex = null): string
{
if ($recipientIndex === null) {
$recipientIndex = 0;
}
$recipient = $jwe->getRecipient($recipientIndex);
$data = [
'ciphertext' => Base64UrlSafe::encodeUnpadded($jwe->getCiphertext() ?? ''),
'iv' => Base64UrlSafe::encodeUnpadded($jwe->getIV() ?? ''),
'tag' => Base64UrlSafe::encodeUnpadded($jwe->getTag() ?? ''),
];
if ($jwe->getAAD() !== null) {
$data['aad'] = Base64UrlSafe::encodeUnpadded($jwe->getAAD());
}
if (count($jwe->getSharedProtectedHeader()) !== 0) {
$data['protected'] = $jwe->getEncodedSharedProtectedHeader();
}
if (count($jwe->getSharedHeader()) !== 0) {
$data['unprotected'] = $jwe->getSharedHeader();
}
if (count($recipient->getHeader()) !== 0) {
$data['header'] = $recipient->getHeader();
}
if ($recipient->getEncryptedKey() !== null) {
$data['encrypted_key'] = Base64UrlSafe::encodeUnpadded($recipient->getEncryptedKey());
}
return JsonConverter::encode($data);
}
public function unserialize(string $input): JWE
{
$data = JsonConverter::decode($input);
if (! is_array($data)) {
throw new InvalidArgumentException('Unsupported input.');
}
$this->checkData($data);
$ciphertext = Base64UrlSafe::decodeNoPadding($data['ciphertext']);
$iv = Base64UrlSafe::decodeNoPadding($data['iv']);
$tag = Base64UrlSafe::decodeNoPadding($data['tag']);
$aad = array_key_exists('aad', $data) ? Base64UrlSafe::decodeNoPadding($data['aad']) : null;
[$encodedSharedProtectedHeader, $sharedProtectedHeader, $sharedHeader] = $this->processHeaders($data);
$encryptedKey = array_key_exists('encrypted_key', $data) ? Base64UrlSafe::decodeNoPadding(
$data['encrypted_key']
) : null;
$header = array_key_exists('header', $data) ? $data['header'] : [];
return new JWE(
$ciphertext,
$iv,
$tag,
$aad,
$sharedHeader,
$sharedProtectedHeader,
$encodedSharedProtectedHeader,
[new Recipient($header, $encryptedKey)]
);
}
private function checkData(?array $data): void
{
if ($data === null || ! isset($data['ciphertext']) || isset($data['recipients'])) {
throw new InvalidArgumentException('Unsupported input.');
}
}
private function processHeaders(array $data): array
{
$encodedSharedProtectedHeader = array_key_exists('protected', $data) ? $data['protected'] : null;
$sharedProtectedHeader = $encodedSharedProtectedHeader ? JsonConverter::decode(
Base64UrlSafe::decodeNoPadding($encodedSharedProtectedHeader)
) : [];
$sharedHeader = $data['unprotected'] ?? [];
return [$encodedSharedProtectedHeader, $sharedProtectedHeader, $sharedHeader];
}
}

View File

@ -0,0 +1,124 @@
<?php
declare(strict_types=1);
namespace Jose\Component\Encryption\Serializer;
use InvalidArgumentException;
use Jose\Component\Core\Util\Base64UrlSafe;
use Jose\Component\Core\Util\JsonConverter;
use Jose\Component\Encryption\JWE;
use Jose\Component\Encryption\Recipient;
use LogicException;
use function array_key_exists;
use function count;
use function is_array;
final class JSONGeneralSerializer implements JWESerializer
{
public const NAME = 'jwe_json_general';
public function displayName(): string
{
return 'JWE JSON General';
}
public function name(): string
{
return self::NAME;
}
public function serialize(JWE $jwe, ?int $recipientIndex = null): string
{
if ($jwe->countRecipients() === 0) {
throw new LogicException('No recipient.');
}
$data = [
'ciphertext' => Base64UrlSafe::encodeUnpadded($jwe->getCiphertext() ?? ''),
'iv' => Base64UrlSafe::encodeUnpadded($jwe->getIV() ?? ''),
'tag' => Base64UrlSafe::encodeUnpadded($jwe->getTag() ?? ''),
];
if ($jwe->getAAD() !== null) {
$data['aad'] = Base64UrlSafe::encodeUnpadded($jwe->getAAD());
}
if (count($jwe->getSharedProtectedHeader()) !== 0) {
$data['protected'] = $jwe->getEncodedSharedProtectedHeader();
}
if (count($jwe->getSharedHeader()) !== 0) {
$data['unprotected'] = $jwe->getSharedHeader();
}
$data['recipients'] = [];
foreach ($jwe->getRecipients() as $recipient) {
$temp = [];
if (count($recipient->getHeader()) !== 0) {
$temp['header'] = $recipient->getHeader();
}
if ($recipient->getEncryptedKey() !== null) {
$temp['encrypted_key'] = Base64UrlSafe::encodeUnpadded($recipient->getEncryptedKey());
}
$data['recipients'][] = $temp;
}
return JsonConverter::encode($data);
}
public function unserialize(string $input): JWE
{
$data = JsonConverter::decode($input);
if (! is_array($data)) {
throw new InvalidArgumentException('Unsupported input.');
}
$this->checkData($data);
$ciphertext = Base64UrlSafe::decodeNoPadding($data['ciphertext']);
$iv = Base64UrlSafe::decodeNoPadding($data['iv']);
$tag = Base64UrlSafe::decodeNoPadding($data['tag']);
$aad = array_key_exists('aad', $data) ? Base64UrlSafe::decodeNoPadding($data['aad']) : null;
[$encodedSharedProtectedHeader, $sharedProtectedHeader, $sharedHeader] = $this->processHeaders($data);
$recipients = [];
foreach ($data['recipients'] as $recipient) {
[$encryptedKey, $header] = $this->processRecipient($recipient);
$recipients[] = new Recipient($header, $encryptedKey);
}
return new JWE(
$ciphertext,
$iv,
$tag,
$aad,
$sharedHeader,
$sharedProtectedHeader,
$encodedSharedProtectedHeader,
$recipients
);
}
private function checkData(?array $data): void
{
if ($data === null || ! isset($data['ciphertext']) || ! isset($data['recipients'])) {
throw new InvalidArgumentException('Unsupported input.');
}
}
private function processRecipient(array $recipient): array
{
$encryptedKey = array_key_exists('encrypted_key', $recipient) ? Base64UrlSafe::decodeNoPadding(
$recipient['encrypted_key']
) : null;
$header = array_key_exists('header', $recipient) ? $recipient['header'] : [];
return [$encryptedKey, $header];
}
private function processHeaders(array $data): array
{
$encodedSharedProtectedHeader = array_key_exists('protected', $data) ? $data['protected'] : null;
$sharedProtectedHeader = $encodedSharedProtectedHeader ? JsonConverter::decode(
Base64UrlSafe::decodeNoPadding($encodedSharedProtectedHeader)
) : [];
$sharedHeader = array_key_exists('unprotected', $data) ? $data['unprotected'] : [];
return [$encodedSharedProtectedHeader, $sharedProtectedHeader, $sharedHeader];
}
}

View File

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Jose\Component\Encryption\Serializer;
use Jose\Component\Encryption\JWE;
interface JWESerializer
{
/**
* The name of the serialization method.
*/
public function name(): string;
/**
* Display name of the serialization method.
*/
public function displayName(): string;
/**
* Converts a JWE into a string. If the JWE is designed for multiple recipients and the serializer only supports one
* recipient, the recipient index has to be set.
*/
public function serialize(JWE $jws, ?int $recipientIndex = null): string;
/**
* Loads data and return a JWE object. Throws an exception in case of failure.
*
* @param string $input A string that represents a JWE
*/
public function unserialize(string $input): JWE;
}

View File

@ -0,0 +1,78 @@
<?php
declare(strict_types=1);
namespace Jose\Component\Encryption\Serializer;
use InvalidArgumentException;
use Jose\Component\Encryption\JWE;
class JWESerializerManager
{
/**
* @var JWESerializer[]
*/
private array $serializers = [];
/**
* @param JWESerializer[] $serializers
*/
public function __construct(iterable $serializers)
{
foreach ($serializers as $serializer) {
$this->add($serializer);
}
}
/**
* Return the serializer names supported by the manager.
*
* @return string[]
*/
public function names(): array
{
return array_keys($this->serializers);
}
/**
* Converts a JWE into a string. Throws an exception if none of the serializer was able to convert the input.
*/
public function serialize(string $name, JWE $jws, ?int $recipientIndex = null): string
{
if (! isset($this->serializers[$name])) {
throw new InvalidArgumentException(sprintf('Unsupported serializer "%s".', $name));
}
return $this->serializers[$name]->serialize($jws, $recipientIndex);
}
/**
* Loads data and return a JWE object. Throws an exception if none of the serializer was able to convert the input.
*
* @param string $input A string that represents a JWE
* @param string|null $name the name of the serializer if the input is unserialized
*/
public function unserialize(string $input, ?string &$name = null): JWE
{
foreach ($this->serializers as $serializer) {
try {
$jws = $serializer->unserialize($input);
$name = $serializer->name();
return $jws;
} catch (InvalidArgumentException) {
continue;
}
}
throw new InvalidArgumentException('Unsupported input.');
}
/**
* Adds a serializer to the manager.
*/
private function add(JWESerializer $serializer): void
{
$this->serializers[$serializer->name()] = $serializer;
}
}

View File

@ -0,0 +1,61 @@
<?php
declare(strict_types=1);
namespace Jose\Component\Encryption\Serializer;
use InvalidArgumentException;
class JWESerializerManagerFactory
{
/**
* @var JWESerializer[]
*/
private array $serializers = [];
/**
* Creates a serializer manager factory using the given serializers.
*
* @param string[] $names
*/
public function create(array $names): JWESerializerManager
{
$serializers = [];
foreach ($names as $name) {
if (! isset($this->serializers[$name])) {
throw new InvalidArgumentException(sprintf('Unsupported serializer "%s".', $name));
}
$serializers[] = $this->serializers[$name];
}
return new JWESerializerManager($serializers);
}
/**
* Return the serializer names supported by the manager.
*
* @return string[]
*/
public function names(): array
{
return array_keys($this->serializers);
}
/**
* Returns all serializers supported by this factory.
*
* @return JWESerializer[]
*/
public function all(): array
{
return $this->serializers;
}
/**
* Adds a serializer to the manager.
*/
public function add(JWESerializer $serializer): void
{
$this->serializers[$serializer->name()] = $serializer;
}
}