- md5
- e0442c8972a866f7cc9cba9bef1bcd40
- sha1
- 8583f1ad6f3111b83f85e5f99b8d444c765cadee
- sha256
- 6f643c788390764d711ebb972c27ec0fa32fe42411ce71aa344d0620c73fc6b9
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
<?php
declare(strict_types=1);
namespace SpomkyLabs\Pki\X509\Certificate;
use ArrayIterator;
use function count;
use Countable;
use IteratorAggregate;
use SpomkyLabs\Pki\CryptoEncoding\PEM;
use SpomkyLabs\Pki\CryptoEncoding\PEMBundle;
/**
* Implements a list of certificates.
*/
final class CertificateBundle implements Countable, IteratorAggregate
{
/**
* Certificates.
*
* @var Certificate[]
*/
private array $certs;
/**
* Mapping from public key id to array of certificates.
*
* @var null|(Certificate[])[]
*/
private ?array $keyIdMap = null;
/**
* @param Certificate ...$certs Certificate objects
*/
private function __construct(Certificate ...$certs)
{
$this->certs = $certs;
}
/**
* Reset internal cached variables on clone.
*/
public function __clone()
{
$this->keyIdMap = null;
}
public static function create(Certificate ...$certs): self
{
return new self(...$certs);
}
/**
* Initialize from PEMs.
*
* @param PEM ...$pems PEM objects
*/
public static function fromPEMs(PEM ...$pems): self
{
$certs = array_map(Certificate::fromPEM(...), $pems);
return self::create(...$certs);
}
/**
* Initialize from PEM bundle.
*/
public static function fromPEMBundle(PEMBundle $pem_bundle): self
{
return self::fromPEMs(...$pem_bundle->all());
}
/**
* Get self with certificates added.
*/
public function withCertificates(Certificate ...$cert): self
{
$obj = clone $this;
$obj->certs = array_merge($obj->certs, $cert);
return $obj;
}
/**
* Get self with certificates from PEMBundle added.
*/
public function withPEMBundle(PEMBundle $pem_bundle): self
{
$certs = $this->certs;
foreach ($pem_bundle as $pem) {
$certs[] = Certificate::fromPEM($pem);
}
return self::create(...$certs);
}
/**
* Get self with single certificate from PEM added.
*/
public function withPEM(PEM $pem): self
{
$certs = $this->certs;
$certs[] = Certificate::fromPEM($pem);
return self::create(...$certs);
}
/**
* Check whether bundle contains a given certificate.
*/
public function contains(Certificate $cert): bool
{
$id = self::_getCertKeyId($cert);
$map = $this->_getKeyIdMap();
if (! isset($map[$id])) {
return false;
}
foreach ($map[$id] as $c) {
/** @var Certificate $c */
if ($cert->equals($c)) {
return true;
}
}
return false;
}
/**
* Get all certificates that have given subject key identifier.
*
* @return Certificate[]
*/
public function allBySubjectKeyIdentifier(string $id): array
{
$map = $this->_getKeyIdMap();
if (! isset($map[$id])) {
return [];
}
return $map[$id];
}
/**
* Get all certificates in a bundle.
*
* @return Certificate[]
*/
public function all(): array
{
return $this->certs;
}
/**
* @see \Countable::count()
*/
public function count(): int
{
return count($this->certs);
}
/**
* Get iterator for certificates.
*
* @see \IteratorAggregate::getIterator()
*/
public function getIterator(): ArrayIterator
{
return new ArrayIterator($this->certs);
}
/**
* Get certificate mapping by public key id.
*
* @return (Certificate[])[]
*/
private function _getKeyIdMap(): array
{
// lazily build mapping
if (! isset($this->keyIdMap)) {
$this->keyIdMap = [];
foreach ($this->certs as $cert) {
$id = self::_getCertKeyId($cert);
if (! isset($this->keyIdMap[$id])) {
$this->keyIdMap[$id] = [];
}
array_push($this->keyIdMap[$id], $cert);
}
}
return $this->keyIdMap;
}
/**
* Get public key id for the certificate.
*/
private static function _getCertKeyId(Certificate $cert): string
{
$exts = $cert->tbsCertificate()
->extensions();
if ($exts->hasSubjectKeyIdentifier()) {
return $exts->subjectKeyIdentifier()
->keyIdentifier();
}
return $cert->tbsCertificate()
->subjectPublicKeyInfo()
->keyIdentifier();
}
}
The same content is in 11 releases, Joomla 6.2.0-alpha2 to 6.3.0-alpha1. See them all, or every version of this file.