- md5
- 862f1bf606184bbe8c937157d2191ebd
- sha1
- 6147270feba67e7f38ddbaddcac4950d53938f9c
- sha256
- ee9f2425815cccbceae69a0e5fe02df403ddbe9119f48739d11981ee3f5498df
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
<?php
declare(strict_types=1);
namespace SpomkyLabs\Pki\X509\Certificate\Extension;
use ArrayIterator;
use Countable;
use IteratorAggregate;
use LogicException;
use SpomkyLabs\Pki\ASN1\Element;
use SpomkyLabs\Pki\ASN1\Type\Constructed\Sequence;
use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType;
use SpomkyLabs\Pki\X509\Certificate\Extension\CertificatePolicy\PolicyInformation;
use SpomkyLabs\Pki\X509\Certificate\Extension\PolicyMappings\PolicyMapping;
use UnexpectedValueException;
use function count;
/**
* Implements 'Policy Mappings' certificate extension.
*
* @see https://tools.ietf.org/html/rfc5280#section-4.2.1.5
*/
final class PolicyMappingsExtension extends Extension implements Countable, IteratorAggregate
{
/**
* Policy mappings.
*
* @var PolicyMapping[]
*/
private readonly array $mappings;
/**
* @param PolicyMapping ...$mappings One or more PolicyMapping objects
*/
private function __construct(bool $critical, PolicyMapping ...$mappings)
{
parent::__construct(self::OID_POLICY_MAPPINGS, $critical);
$this->mappings = $mappings;
}
public static function create(bool $critical, PolicyMapping ...$mappings): self
{
return new self($critical, ...$mappings);
}
/**
* Get all mappings.
*
* @return PolicyMapping[]
*/
public function mappings(): array
{
return $this->mappings;
}
/**
* Get mappings flattened into a single array of arrays of subject domains keyed by issuer domain.
*
* Eg. if policy mappings contains multiple mappings with the same issuer domain policy, their corresponding subject
* domain policies are placed under the same key.
*
* @return (string[])[]
*/
public function flattenedMappings(): array
{
$mappings = [];
foreach ($this->mappings as $mapping) {
$idp = $mapping->issuerDomainPolicy();
if (! isset($mappings[$idp])) {
$mappings[$idp] = [];
}
array_push($mappings[$idp], $mapping->subjectDomainPolicy());
}
return $mappings;
}
/**
* Get all subject domain policy OIDs that are mapped to given issuer domain policy OID.
*
* @param string $oid Issuer domain policy
*
* @return string[] List of OIDs in dotted format
*/
public function issuerMappings(string $oid): array
{
$oids = [];
foreach ($this->mappings as $mapping) {
if ($mapping->issuerDomainPolicy() === $oid) {
$oids[] = $mapping->subjectDomainPolicy();
}
}
return $oids;
}
/**
* Get all mapped issuer domain policy OIDs.
*
* @return string[]
*/
public function issuerDomainPolicies(): array
{
$idps = array_map(static fn (PolicyMapping $mapping) => $mapping->issuerDomainPolicy(), $this->mappings);
return array_values(array_unique($idps));
}
/**
* Check whether policy mappings have anyPolicy mapped.
*
* RFC 5280 section 4.2.1.5 states that "Policies MUST NOT be mapped either to or from the special value anyPolicy".
*/
public function hasAnyPolicyMapping(): bool
{
foreach ($this->mappings as $mapping) {
if ($mapping->issuerDomainPolicy() === PolicyInformation::OID_ANY_POLICY) {
return true;
}
if ($mapping->subjectDomainPolicy() === PolicyInformation::OID_ANY_POLICY) {
return true;
}
}
return false;
}
/**
* Get the number of mappings.
*
* @see \Countable::count()
*/
public function count(): int
{
return count($this->mappings);
}
/**
* Get iterator for policy mappings.
*
* @see \IteratorAggregate::getIterator()
*/
public function getIterator(): ArrayIterator
{
return new ArrayIterator($this->mappings);
}
protected static function fromDER(string $data, bool $critical): static
{
$mappings = array_map(
static fn (UnspecifiedType $el) => PolicyMapping::fromASN1($el->asSequence()),
UnspecifiedType::fromDER($data)->asSequence()->elements()
);
if (count($mappings) === 0) {
throw new UnexpectedValueException('PolicyMappings must have at least one mapping.');
}
return self::create($critical, ...$mappings);
}
protected function valueASN1(): Element
{
if (count($this->mappings) === 0) {
throw new LogicException('No mappings.');
}
$elements = array_map(static fn (PolicyMapping $mapping) => $mapping->toASN1(), $this->mappings);
return Sequence::create(...$elements);
}
}
The same content is in 125 releases, Joomla 5.1.0-beta1 to 6.2.0-alpha1. See them all, or every version of this file.