Phase 0: Fundament für Inhaus
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 1m14s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 1m12s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 1m7s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 1m19s
CI / PHP 8.3 / mysql:8.0 (pull_request) Successful in 1m32s
CI / PHP 8.4 / mysql:8.0 (pull_request) Successful in 1m22s
CI / PHP 8.4 / mysql:8.4 (pull_request) Successful in 1m32s
CI / PHP 8.3 / mysql:8.4 (pull_request) Successful in 1m35s
CI / Build-Artefakte aktuell (pull_request) Failing after 1m7s
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 1m14s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 1m12s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 1m7s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 1m19s
CI / PHP 8.3 / mysql:8.0 (pull_request) Successful in 1m32s
CI / PHP 8.4 / mysql:8.0 (pull_request) Successful in 1m22s
CI / PHP 8.4 / mysql:8.4 (pull_request) Successful in 1m32s
CI / PHP 8.3 / mysql:8.4 (pull_request) Successful in 1m35s
CI / Build-Artefakte aktuell (pull_request) Failing after 1m7s
- Laravel 13 mit Filament 5 (Admin), Inertia 3 + Vue 3 + Vuetify 4 (Material 3) - Anmeldung über Fortify (Login, Passwort vergessen/ändern), keine Selbstregistrierung - Profil mit Profilbild (WebP in festen Größen), Personenseiten, Header mit Avatar-Menü - Material-3-Farbschema aus der Primärfarbe, hell/dunkel/System pro Person - Basis-Branding (Portalname, Logo, Primärfarbe) im Admin unter „Erscheinungsbild“ - Benutzerverwaltung im Admin, Zugang vorläufig über is_admin - vendor/ (ohne Dev-Pakete) und Frontend-Build mitversioniert, Dev-Pakete in vendor-dev/ - PHPUnit-Tests gegen MySQL/MariaDB, Forgejo-CI-Matrix, Docker-Entwicklungsumgebung - VERSION, CHANGELOG.md und CONTRIBUTING.md mit Release-Ablauf Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
7197eaebf2
commit
9c0f8de791
15771 changed files with 1564103 additions and 5 deletions
151
vendor/web-auth/cose-lib/src/Structure/CoseHeaders.php
vendored
Normal file
151
vendor/web-auth/cose-lib/src/Structure/CoseHeaders.php
vendored
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Cose\Structure;
|
||||
|
||||
use CBOR\ByteStringObject;
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\DecoderInterface;
|
||||
use CBOR\IndefiniteLengthByteStringObject;
|
||||
use CBOR\IndefiniteLengthMapObject;
|
||||
use CBOR\MapObject;
|
||||
use CBOR\Tag\AbstractCoseTag;
|
||||
|
||||
/**
|
||||
* The two header buckets of a COSE message, read the way RFC 9052 defines them.
|
||||
*
|
||||
* cbor-php 3.4.0 owns the shape of a COSE message and hands back the buckets as they were encoded. What it does not
|
||||
* do -- and what a CBOR library has no business doing -- is apply the header rules of RFC 9052 sections 1.5 and 3 on
|
||||
* top: a label is an integer or a text string and nothing else, the two are distinct even when they normalize to the
|
||||
* same map offset, a label appears at most once, and the protected bucket holds exactly one CBOR item.
|
||||
*
|
||||
* That is what this reads. It works on any COSE message: the upstream classes through fromMessage(), the deprecated
|
||||
* Cose\...Tag classes and the per-signer or per-recipient entries through of().
|
||||
*
|
||||
* ```php
|
||||
* $message = Decoder::create()->decode(StringStream::create($bytes)); // CBOR\Tag\CoseSign1Tag
|
||||
* $headers = CoseHeaders::fromMessage($message);
|
||||
*
|
||||
* $alg = $headers->getProtectedHeaderParameter(1); // never answered by the text string "1"
|
||||
* $kid = $headers->getHeaderParameter(4); // protected bucket first
|
||||
* ```
|
||||
*
|
||||
* The protected bucket is decoded once, on first use.
|
||||
*
|
||||
* @see https://www.rfc-editor.org/rfc/rfc9052#section-3
|
||||
* @see https://github.com/web-auth/cose-lib/issues/166
|
||||
* @see \Cose\Tests\Structure\CoseHeadersTest
|
||||
*/
|
||||
final class CoseHeaders
|
||||
{
|
||||
public const DEFAULT_PROTECTED_HEADER_MAX_DEPTH = HeaderMapHelper::DEFAULT_PROTECTED_HEADER_MAX_DEPTH;
|
||||
|
||||
private ?MapObject $decodedProtectedHeader = null;
|
||||
|
||||
private function __construct(
|
||||
private readonly ByteStringObject|IndefiniteLengthByteStringObject $protectedHeader,
|
||||
private readonly MapObject|IndefiniteLengthMapObject $unprotectedHeader,
|
||||
private readonly ?DecoderInterface $decoder,
|
||||
private readonly int $maxDepth
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* The buckets of a COSE message, given as they are carried.
|
||||
*
|
||||
* This is the form to use for a per-signer COSE_Signature or a COSE_recipient -- {@see CoseSignature} and
|
||||
* {@see CoseRecipient} build it for you -- and for the deprecated Cose\...Tag classes.
|
||||
*/
|
||||
public static function of(
|
||||
ByteStringObject|IndefiniteLengthByteStringObject $protectedHeader,
|
||||
MapObject|IndefiniteLengthMapObject $unprotectedHeader,
|
||||
?DecoderInterface $decoder = null,
|
||||
int $maxDepth = self::DEFAULT_PROTECTED_HEADER_MAX_DEPTH
|
||||
): self {
|
||||
return new self($protectedHeader, $unprotectedHeader, $decoder, $maxDepth);
|
||||
}
|
||||
|
||||
/**
|
||||
* The buckets of one of the six COSE messages of cbor-php 3.4.0 or later.
|
||||
*
|
||||
* The nesting bound only applies to the decoder built when none is given: a caller passing its own $decoder sets
|
||||
* its own bound, and $maxDepth is then ignored.
|
||||
*/
|
||||
public static function fromMessage(
|
||||
AbstractCoseTag $message,
|
||||
?DecoderInterface $decoder = null,
|
||||
int $maxDepth = self::DEFAULT_PROTECTED_HEADER_MAX_DEPTH
|
||||
): self {
|
||||
return new self($message->getProtectedHeader(), $message->getUnprotectedHeader(), $decoder, $maxDepth);
|
||||
}
|
||||
|
||||
/**
|
||||
* The protected bucket as it is carried: the byte string the cryptographic structure covers verbatim.
|
||||
*/
|
||||
public function getProtectedHeader(): ByteStringObject|IndefiniteLengthByteStringObject
|
||||
{
|
||||
return $this->protectedHeader;
|
||||
}
|
||||
|
||||
/**
|
||||
* The protected bucket, decoded and checked: the zero-length byte string of RFC 9052 section 3 is an empty
|
||||
* header, trailing bytes after the header map are rejected, and every key is a label.
|
||||
*/
|
||||
public function getProtectedHeaderAsMap(): MapObject
|
||||
{
|
||||
return $this->decodedProtectedHeader ??= HeaderMapHelper::decodeProtected(
|
||||
$this->protectedHeader,
|
||||
$this->decoder,
|
||||
$this->maxDepth
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The unprotected bucket, with its labels checked.
|
||||
*/
|
||||
public function getUnprotectedHeaderAsMap(): MapObject
|
||||
{
|
||||
return HeaderMapHelper::assertValidLabels($this->unprotectedHeader);
|
||||
}
|
||||
|
||||
/**
|
||||
* The unprotected bucket as it is carried.
|
||||
*/
|
||||
public function getUnprotectedHeader(): MapObject|IndefiniteLengthMapObject
|
||||
{
|
||||
return $this->unprotectedHeader;
|
||||
}
|
||||
|
||||
/**
|
||||
* The value of a label in the protected bucket, or null when the bucket does not carry it.
|
||||
*
|
||||
* The label is matched by type as well as by value, so the integer 1 -- the algorithm -- is never answered by
|
||||
* the text string "1" or by a byte string that happens to normalize the same way.
|
||||
*/
|
||||
public function getProtectedHeaderParameter(int|string $label): ?CBORObject
|
||||
{
|
||||
return HeaderMapHelper::findLabel($this->getProtectedHeaderAsMap(), $label);
|
||||
}
|
||||
|
||||
/**
|
||||
* The value of a label in the unprotected bucket, or null when the bucket does not carry it.
|
||||
*/
|
||||
public function getUnprotectedHeaderParameter(int|string $label): ?CBORObject
|
||||
{
|
||||
return HeaderMapHelper::findLabel($this->unprotectedHeader, $label);
|
||||
}
|
||||
|
||||
/**
|
||||
* The value of a label, looked up in the protected bucket first.
|
||||
*
|
||||
* A parameter found there is the one the signature or the MAC commits to, so it wins over an unprotected copy of
|
||||
* the same label. A message carrying a label in both buckets is malformed anyway (RFC 9052 section 3: "The same
|
||||
* label MUST NOT occur in both buckets"); preferring the protected value keeps the answer on the authenticated
|
||||
* side of that mistake.
|
||||
*/
|
||||
public function getHeaderParameter(int|string $label): ?CBORObject
|
||||
{
|
||||
return $this->getProtectedHeaderParameter($label) ?? $this->getUnprotectedHeaderParameter($label);
|
||||
}
|
||||
}
|
||||
167
vendor/web-auth/cose-lib/src/Structure/CoseRecipient.php
vendored
Normal file
167
vendor/web-auth/cose-lib/src/Structure/CoseRecipient.php
vendored
Normal file
|
|
@ -0,0 +1,167 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Cose\Structure;
|
||||
|
||||
use CBOR\ByteStringObject;
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\IndefiniteLengthByteStringObject;
|
||||
use CBOR\IndefiniteLengthListObject;
|
||||
use CBOR\IndefiniteLengthMapObject;
|
||||
use CBOR\ListObject;
|
||||
use CBOR\MapObject;
|
||||
use CBOR\OtherObject\NullObject;
|
||||
use InvalidArgumentException;
|
||||
use LogicException;
|
||||
|
||||
/**
|
||||
* One entry of the "recipients" list of a COSE_Encrypt or of a COSE_Mac (RFC 9052 sections 5.1 and 6.1).
|
||||
*
|
||||
* COSE_recipient = [ Headers, ciphertext : bstr / nil, ? recipients : [+COSE_recipient] ]
|
||||
*
|
||||
* The raw list a message carries says nothing about what is inside it, and neither cbor-php nor the deprecated
|
||||
* Cose\...Tag classes look: both accept an empty list and entries of any shape. This view is the checked form.
|
||||
*
|
||||
* A recipient may itself carry recipients, which is how RFC 9052 expresses key layering; getRecipients() walks that
|
||||
* tree one level at a time and each level is a view of the same kind.
|
||||
*
|
||||
* @see https://www.rfc-editor.org/rfc/rfc9052#section-5.1
|
||||
* @see \Cose\Tests\Structure\CoseRecipientTest
|
||||
*/
|
||||
final class CoseRecipient
|
||||
{
|
||||
private readonly ByteStringObject|IndefiniteLengthByteStringObject $protectedHeader;
|
||||
|
||||
private readonly MapObject|IndefiniteLengthMapObject $unprotectedHeader;
|
||||
|
||||
private readonly ByteStringObject|IndefiniteLengthByteStringObject|null $ciphertext;
|
||||
|
||||
private readonly ListObject|IndefiniteLengthListObject|null $recipients;
|
||||
|
||||
public function __construct(ListObject|IndefiniteLengthListObject $recipient)
|
||||
{
|
||||
HeaderMapHelper::assertRecipientList(ListObject::create([$recipient]));
|
||||
|
||||
/** @var ByteStringObject|IndefiniteLengthByteStringObject $protectedHeader */
|
||||
$protectedHeader = $recipient->get(0);
|
||||
/** @var IndefiniteLengthMapObject|MapObject $unprotectedHeader */
|
||||
$unprotectedHeader = $recipient->get(1);
|
||||
// assertRecipientList() has just ruled out everything but a byte string and nil at this position.
|
||||
/** @var ByteStringObject|IndefiniteLengthByteStringObject|null $ciphertext */
|
||||
$ciphertext = HeaderMapHelper::isNil($recipient->get(2)) ? null : $recipient->get(2);
|
||||
|
||||
$this->protectedHeader = $protectedHeader;
|
||||
$this->unprotectedHeader = $unprotectedHeader;
|
||||
$this->ciphertext = $ciphertext;
|
||||
|
||||
$nested = $recipient->count() === 4 ? $recipient->get(3) : null;
|
||||
$this->recipients = $nested instanceof ListObject || $nested instanceof IndefiniteLengthListObject
|
||||
? $nested
|
||||
: null;
|
||||
}
|
||||
|
||||
public static function create(ListObject|IndefiniteLengthListObject $recipient): self
|
||||
{
|
||||
return new self($recipient);
|
||||
}
|
||||
|
||||
/**
|
||||
* Every entry of a "recipients" list, checked and wrapped.
|
||||
*
|
||||
* @return list<self>
|
||||
*/
|
||||
public static function all(ListObject|IndefiniteLengthListObject $recipients): array
|
||||
{
|
||||
HeaderMapHelper::assertRecipientList($recipients);
|
||||
|
||||
$result = [];
|
||||
foreach ($recipients as $recipient) {
|
||||
if (! $recipient instanceof ListObject && ! $recipient instanceof IndefiniteLengthListObject) {
|
||||
throw new InvalidArgumentException('Not a valid COSE_recipient object. It shall be a List object.');
|
||||
}
|
||||
$result[] = new self($recipient);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* The protected bucket of this recipient, as it is carried.
|
||||
*/
|
||||
public function getProtectedHeader(): ByteStringObject|IndefiniteLengthByteStringObject
|
||||
{
|
||||
return $this->protectedHeader;
|
||||
}
|
||||
|
||||
public function getUnprotectedHeader(): MapObject|IndefiniteLengthMapObject
|
||||
{
|
||||
return $this->unprotectedHeader;
|
||||
}
|
||||
|
||||
/**
|
||||
* The headers of this recipient, read the way RFC 9052 defines them.
|
||||
*/
|
||||
public function headers(): CoseHeaders
|
||||
{
|
||||
return CoseHeaders::of($this->protectedHeader, $this->unprotectedHeader);
|
||||
}
|
||||
|
||||
public function getProtectedHeaderParameter(int|string $label): ?CBORObject
|
||||
{
|
||||
return $this->headers()
|
||||
->getProtectedHeaderParameter($label);
|
||||
}
|
||||
|
||||
public function getUnprotectedHeaderParameter(int|string $label): ?CBORObject
|
||||
{
|
||||
return $this->headers()
|
||||
->getUnprotectedHeaderParameter($label);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the ciphertext of this recipient travels outside the message.
|
||||
*/
|
||||
public function hasDetachedCiphertext(): bool
|
||||
{
|
||||
return $this->ciphertext === null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws LogicException when the ciphertext is detached; hasDetachedCiphertext() tells the two cases apart
|
||||
*/
|
||||
public function getCiphertext(): ByteStringObject|IndefiniteLengthByteStringObject
|
||||
{
|
||||
return $this->ciphertext ?? throw new LogicException(
|
||||
'The ciphertext of this COSE_recipient is detached (RFC 9052 section 5.1); the application supplies it separately.'
|
||||
);
|
||||
}
|
||||
|
||||
public function hasRecipients(): bool
|
||||
{
|
||||
return $this->recipients !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The recipients nested under this one, empty when it carries none.
|
||||
*
|
||||
* @return list<self>
|
||||
*/
|
||||
public function getRecipients(): array
|
||||
{
|
||||
return $this->recipients === null ? [] : self::all($this->recipients);
|
||||
}
|
||||
|
||||
/**
|
||||
* The list this view was built from, rebuilt so that nothing the caller does to it reaches the message.
|
||||
*/
|
||||
public function toListObject(): ListObject
|
||||
{
|
||||
$items = [$this->protectedHeader, $this->unprotectedHeader, $this->ciphertext ?? NullObject::create()];
|
||||
if ($this->recipients !== null) {
|
||||
$items[] = $this->recipients;
|
||||
}
|
||||
|
||||
return ListObject::create($items);
|
||||
}
|
||||
}
|
||||
51
vendor/web-auth/cose-lib/src/Structure/CoseStructure.php
vendored
Normal file
51
vendor/web-auth/cose-lib/src/Structure/CoseStructure.php
vendored
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Cose\Structure;
|
||||
|
||||
use CBOR\ByteStringObject;
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\ListObject;
|
||||
use CBOR\TextStringObject;
|
||||
use Stringable;
|
||||
|
||||
/**
|
||||
* The shape every RFC 9052 cryptographic structure has: a context string that names what is being computed,
|
||||
* followed by the fields that structure commits to.
|
||||
*
|
||||
* These are the byte strings that are signed, MACed or fed as additional authenticated data -- never the payload on
|
||||
* its own. Casting an instance to string yields the CBOR encoding to hand to the algorithm.
|
||||
*
|
||||
* @see https://www.rfc-editor.org/rfc/rfc9052#section-4.4
|
||||
* @see https://www.rfc-editor.org/rfc/rfc9052#section-5.3
|
||||
* @see https://www.rfc-editor.org/rfc/rfc9052#section-6.3
|
||||
*/
|
||||
abstract class CoseStructure implements Stringable
|
||||
{
|
||||
/**
|
||||
* RFC 9052 section 4.4 on external_aad: "If this field is not supplied, it defaults to a zero-length byte
|
||||
* string."
|
||||
*/
|
||||
public static function emptyExternalAad(): ByteStringObject
|
||||
{
|
||||
return ByteStringObject::create('');
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return (string) ListObject::create([TextStringObject::create($this->context()), ...$this->items()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* The context string of this structure, as RFC 9052 spells it.
|
||||
*/
|
||||
abstract protected function context(): string;
|
||||
|
||||
/**
|
||||
* The fields following the context, in the order the CDDL gives them.
|
||||
*
|
||||
* @return list<CBORObject>
|
||||
*/
|
||||
abstract protected function items(): array;
|
||||
}
|
||||
341
vendor/web-auth/cose-lib/src/Structure/HeaderMapHelper.php
vendored
Normal file
341
vendor/web-auth/cose-lib/src/Structure/HeaderMapHelper.php
vendored
Normal file
|
|
@ -0,0 +1,341 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Cose\Structure;
|
||||
|
||||
use CBOR\ByteStringObject;
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\Decoder;
|
||||
use CBOR\DecoderInterface;
|
||||
use CBOR\IndefiniteLengthByteStringObject;
|
||||
use CBOR\IndefiniteLengthListObject;
|
||||
use CBOR\IndefiniteLengthMapObject;
|
||||
use CBOR\IndefiniteLengthTextStringObject;
|
||||
use CBOR\ListObject;
|
||||
use CBOR\MapObject;
|
||||
use CBOR\NegativeIntegerObject;
|
||||
use CBOR\OtherObject\OtherObjectInterface;
|
||||
use CBOR\StringStream;
|
||||
use CBOR\TextStringObject;
|
||||
use CBOR\UnsignedIntegerObject;
|
||||
use function in_array;
|
||||
use InvalidArgumentException;
|
||||
use function is_int;
|
||||
use function ord;
|
||||
use function sprintf;
|
||||
use function strlen;
|
||||
|
||||
/**
|
||||
* The RFC 9052 rules that sit above the CBOR shape of a COSE message: how a protected bucket is encoded and decoded,
|
||||
* what a header label may be, which CBOR tag number a message type carries, and the shape of the signature and
|
||||
* recipient lists.
|
||||
*
|
||||
* These are deliberately static functions over plain CBOR objects rather than methods on a message class. Since
|
||||
* spomky-labs/cbor-php 3.4.0 the six COSE structures live upstream, as CBOR\Tag\CoseSign1Tag and its siblings: that
|
||||
* library owns the shape of a message, this one owns what RFC 9052 says the shape means. Everything here therefore
|
||||
* applies to an upstream message, to the deprecated Cose\...Tag classes, and to a header map a caller assembled
|
||||
* itself.
|
||||
*
|
||||
* {@see CoseHeaders} is the ergonomic form of the header half of this: it reads the two buckets of a message once
|
||||
* and answers label lookups against them.
|
||||
*
|
||||
* @see https://www.rfc-editor.org/rfc/rfc9052#section-3
|
||||
* @see https://www.rfc-editor.org/rfc/rfc9052#section-1.5
|
||||
* @see https://github.com/web-auth/cose-lib/issues/166
|
||||
* @see \Cose\Tests\Structure\HeaderMapHelperTest
|
||||
*/
|
||||
final class HeaderMapHelper
|
||||
{
|
||||
/**
|
||||
* A COSE header is a flat map of a handful of parameters whose values nest a couple of levels at most, so the
|
||||
* decoder built when none is given is bounded far below the cbor-php default of 1000: a protected header crafted
|
||||
* to nest thousands of levels is rejected instead of being walked.
|
||||
*/
|
||||
public const DEFAULT_PROTECTED_HEADER_MAX_DEPTH = 32;
|
||||
|
||||
/**
|
||||
* Decode the protected bucket, strictly.
|
||||
*
|
||||
* Three RFC 9052 rules the upstream accessor does not apply, in one place:
|
||||
*
|
||||
* - Section 3: "Recipients MUST accept both a zero-length byte string and a zero-length map encoded in a byte
|
||||
* string." The zero-length byte string is the form senders are told to prefer, and decoding it as CBOR yields
|
||||
* nothing at all, hence the guard before the decoder runs.
|
||||
* - Section 3 CDDL: "empty_or_serialized_map = bstr .cbor header_map / bstr .size 0". The ".cbor" control of
|
||||
* RFC 8610 section 3.8.4 carries exactly one data item, so trailing bytes make the bucket malformed.
|
||||
* - Section 1.5: a key that is neither an integer nor a text string is not a label at all.
|
||||
*/
|
||||
public static function decodeProtected(
|
||||
ByteStringObject|IndefiniteLengthByteStringObject $protectedHeader,
|
||||
?DecoderInterface $decoder = null,
|
||||
int $maxDepth = self::DEFAULT_PROTECTED_HEADER_MAX_DEPTH
|
||||
): MapObject {
|
||||
$raw = $protectedHeader->getValue();
|
||||
if ($raw === '') {
|
||||
return MapObject::create();
|
||||
}
|
||||
|
||||
$stream = new StringStream($raw);
|
||||
$decoder ??= Decoder::create(null, null, $maxDepth);
|
||||
$decoded = $decoder->decode($stream);
|
||||
|
||||
// cbor-php exposes no end-of-stream predicate, so the probe is a read that has to fail.
|
||||
$trailing = true;
|
||||
try {
|
||||
$stream->read(1);
|
||||
} catch (InvalidArgumentException) {
|
||||
$trailing = false;
|
||||
}
|
||||
if ($trailing) {
|
||||
throw new InvalidArgumentException(
|
||||
'Invalid protected header. The byte string carries trailing data after the header map.'
|
||||
);
|
||||
}
|
||||
|
||||
if (! $decoded instanceof MapObject && ! $decoded instanceof IndefiniteLengthMapObject) {
|
||||
throw new InvalidArgumentException('Protected header is not a valid Map object.');
|
||||
}
|
||||
|
||||
return self::assertValidLabels($decoded);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the protected bucket.
|
||||
*
|
||||
* RFC 9052 section 3: "Senders SHOULD encode a zero-length map as a zero-length byte string rather than as a
|
||||
* zero-length map (encoded as h'a0')." Upstream createFromComponents() emits h'a0'; a sender that wants the
|
||||
* preferred form passes the byte string this returns.
|
||||
*/
|
||||
public static function encodeProtected(MapObject|IndefiniteLengthMapObject $protectedHeader): ByteStringObject
|
||||
{
|
||||
$checked = self::assertValidLabels($protectedHeader);
|
||||
|
||||
return ByteStringObject::create($checked->count() === 0 ? '' : (string) $checked);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that every key of a header map is a label and that no label appears twice, and hand back an equivalent
|
||||
* definite-length map.
|
||||
*
|
||||
* RFC 9052 section 1.5: "label = int / tstr" and "the presence a label that is neither a text string nor an
|
||||
* integer is an error". Section 3: "Labels in each of the maps MUST be unique. When processing messages, if a
|
||||
* label appears multiple times, the message MUST be rejected as malformed." A byte string key is the case worth
|
||||
* naming: cbor-php normalizes h'31' to the string "1", so without this check a byte string key would answer a
|
||||
* lookup for the integer label 1, the algorithm parameter.
|
||||
*/
|
||||
public static function assertValidLabels(MapObject|IndefiniteLengthMapObject $header): MapObject
|
||||
{
|
||||
$checked = MapObject::create();
|
||||
foreach ($header as $item) {
|
||||
$key = $item->getKey();
|
||||
if (! self::isLabel($key)) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'Invalid header label. A label shall be an integer or a text string, got "%s" (RFC 9052 section 1.5).',
|
||||
$key::class
|
||||
));
|
||||
}
|
||||
$checked->add($key, $item->getValue());
|
||||
}
|
||||
|
||||
return $checked;
|
||||
}
|
||||
|
||||
/**
|
||||
* Look a label up by value *and* by type.
|
||||
*
|
||||
* The map accessors of cbor-php are keyed by the normalized key, and PHP turns the numeric string offset "1"
|
||||
* into the integer 1, so int 1 and tstr "1" -- two distinct labels for RFC 9052 -- share one offset there. This
|
||||
* lookup compares the major type as well, which is what makes a lookup for the label 1 mean the algorithm
|
||||
* parameter and nothing else.
|
||||
*/
|
||||
public static function findLabel(MapObject|IndefiniteLengthMapObject $header, int|string $label): ?CBORObject
|
||||
{
|
||||
foreach ($header as $item) {
|
||||
$key = $item->getKey();
|
||||
$matches = is_int($label)
|
||||
? ($key instanceof UnsignedIntegerObject || $key instanceof NegativeIntegerObject)
|
||||
&& $key->normalize() === (string) $label
|
||||
: ($key instanceof TextStringObject || $key instanceof IndefiniteLengthTextStringObject)
|
||||
&& $key->normalize() === $label;
|
||||
if ($matches) {
|
||||
return $item->getValue();
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that the CBOR tag a message carries is the one its structure claims.
|
||||
*
|
||||
* RFC 9052 section 2 maps each message type to one tag number, and the decoder dispatches on that number, so
|
||||
* this is for the paths that bypass it: a class constructed directly, or createFromLoadedData() called on the
|
||||
* value of a GenericTag. Neither the upstream classes nor the deprecated ones check it, so a message can
|
||||
* otherwise keep claiming to be a COSE_Sign1 while serializing as a COSE_Mac0.
|
||||
*
|
||||
* The comparison is on the decoded number, not on the raw components, so a non-minimal encoding of the right
|
||||
* number stays acceptable exactly as it is on the decoder path (RFC 8949 allows it outside deterministic
|
||||
* encoding).
|
||||
*/
|
||||
public static function assertTagNumber(int $additionalInformation, ?string $data, int $expected, string $name): void
|
||||
{
|
||||
$number = self::tagNumber($additionalInformation, $data, $name);
|
||||
if ($number !== $expected) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'Not a valid %s object. Expected the CBOR tag %d, got %d.',
|
||||
$name,
|
||||
$expected,
|
||||
$number
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check a "signatures" list against "signatures : [+ COSE_Signature]" and "COSE_Signature = [ Headers,
|
||||
* signature : bstr ]" (RFC 9052 section 4.1), Headers being the protected byte string and the unprotected map.
|
||||
*/
|
||||
public static function assertSignatureList(ListObject|IndefiniteLengthListObject $signatures): void
|
||||
{
|
||||
if ($signatures->count() === 0) {
|
||||
throw new InvalidArgumentException(
|
||||
'Not a valid CoseSign object. The signatures list shall hold at least one COSE_Signature (RFC 9052 section 4.1).'
|
||||
);
|
||||
}
|
||||
foreach ($signatures as $signature) {
|
||||
if (! self::isList($signature)
|
||||
|| $signature->count() !== 3
|
||||
|| ! self::isByteString($signature->get(0))
|
||||
|| ! self::isMap($signature->get(1))
|
||||
|| ! self::isByteString($signature->get(2))
|
||||
) {
|
||||
throw new InvalidArgumentException(
|
||||
'Not a valid CoseSign object. Each signature shall be a COSE_Signature [bstr, map, bstr].'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check a "recipients" list against "recipients : [+COSE_recipient]" and "COSE_recipient = [ Headers,
|
||||
* ciphertext : bstr / nil, ? recipients : [+COSE_recipient] ]" (RFC 9052 section 5.1).
|
||||
*
|
||||
* The nested list is walked with the same rule, which is what bounds a recipient tree to well-formed levels
|
||||
* rather than to whatever the decoder happened to accept.
|
||||
*/
|
||||
public static function assertRecipientList(
|
||||
ListObject|IndefiniteLengthListObject $recipients,
|
||||
string $name = 'COSE_recipient'
|
||||
): void {
|
||||
if ($recipients->count() === 0) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'Not a valid %s object. The recipients list shall hold at least one COSE_recipient (RFC 9052 section 5.1).',
|
||||
$name
|
||||
));
|
||||
}
|
||||
foreach ($recipients as $recipient) {
|
||||
if (! self::isList($recipient)
|
||||
|| ! in_array($recipient->count(), [3, 4], true)
|
||||
|| ! self::isByteString($recipient->get(0))
|
||||
|| ! self::isMap($recipient->get(1))
|
||||
|| ! (self::isByteString($recipient->get(2)) || self::isNil($recipient->get(2)))
|
||||
) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'Not a valid %s object. Each recipient shall be a COSE_recipient [bstr, map, bstr / nil, ? [+ COSE_recipient]].',
|
||||
$name
|
||||
));
|
||||
}
|
||||
if ($recipient->count() === 4) {
|
||||
$nested = $recipient->get(3);
|
||||
if (! self::isList($nested)) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'Not a valid %s object. The nested recipients of a COSE_recipient shall be a List object.',
|
||||
$name
|
||||
));
|
||||
}
|
||||
self::assertRecipientList($nested, $name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether an item is the CBOR "nil" simple value, which is how RFC 9052 spells detached content.
|
||||
*
|
||||
* The decoder maps simple value 22 to NullObject only when the caller's OtherObjectManager knows that class; an
|
||||
* empty manager yields a GenericObject carrying the same head. Both are nil on the wire, so both are nil here.
|
||||
*/
|
||||
public static function isNil(CBORObject $object): bool
|
||||
{
|
||||
return $object instanceof OtherObjectInterface
|
||||
&& $object->getAdditionalInformation() === CBORObject::OBJECT_NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @phpstan-assert-if-true IndefiniteLengthListObject|ListObject $object
|
||||
*/
|
||||
private static function isList(CBORObject $object): bool
|
||||
{
|
||||
return $object instanceof ListObject || $object instanceof IndefiniteLengthListObject;
|
||||
}
|
||||
|
||||
private static function isMap(CBORObject $object): bool
|
||||
{
|
||||
return $object instanceof MapObject || $object instanceof IndefiniteLengthMapObject;
|
||||
}
|
||||
|
||||
private static function isByteString(CBORObject $object): bool
|
||||
{
|
||||
return $object instanceof ByteStringObject || $object instanceof IndefiniteLengthByteStringObject;
|
||||
}
|
||||
|
||||
private static function isLabel(CBORObject $key): bool
|
||||
{
|
||||
return $key instanceof UnsignedIntegerObject
|
||||
|| $key instanceof NegativeIntegerObject
|
||||
|| $key instanceof TextStringObject
|
||||
|| $key instanceof IndefiniteLengthTextStringObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* A tag number is the argument of a major type 6 head: the additional information carries it directly below 24,
|
||||
* and announces its width above.
|
||||
*/
|
||||
private static function tagNumber(int $additionalInformation, ?string $data, string $name): int
|
||||
{
|
||||
if ($additionalInformation < 24) {
|
||||
return $additionalInformation;
|
||||
}
|
||||
|
||||
$width = match ($additionalInformation) {
|
||||
CBORObject::LENGTH_1_BYTE => 1,
|
||||
CBORObject::LENGTH_2_BYTES => 2,
|
||||
CBORObject::LENGTH_4_BYTES => 4,
|
||||
CBORObject::LENGTH_8_BYTES => 8,
|
||||
default => throw new InvalidArgumentException(sprintf(
|
||||
'Not a valid %s object. The additional information %d is not a valid CBOR tag head.',
|
||||
$name,
|
||||
$additionalInformation
|
||||
)),
|
||||
};
|
||||
if ($data === null || strlen($data) !== $width) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'Not a valid %s object. The CBOR tag head announces %d byte(s) of tag number.',
|
||||
$name,
|
||||
$width
|
||||
));
|
||||
}
|
||||
|
||||
$number = 0;
|
||||
for ($i = 0; $i < $width; ++$i) {
|
||||
$number = ($number << 8) | ord($data[$i]);
|
||||
}
|
||||
if ($number < 0) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'Not a valid %s object. The CBOR tag number exceeds the platform integer range.',
|
||||
$name
|
||||
));
|
||||
}
|
||||
|
||||
return $number;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue