| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SpomkyLabs\Pki\X509\Certificate; |
| 6 |
|
| 7 |
use ArrayIterator; |
| 8 |
use function count; |
| 9 |
use Countable; |
| 10 |
use IteratorAggregate; |
| 11 |
use SpomkyLabs\Pki\CryptoEncoding\PEM; |
| 12 |
use SpomkyLabs\Pki\CryptoEncoding\PEMBundle; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
final class CertificateBundle implements Countable, IteratorAggregate |
| 18 |
{ |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
private array $certs; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
private ?array $keyIdMap = null; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
private function __construct(Certificate ...$certs) |
| 37 |
{ |
| 38 |
$this->certs = $certs; |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public function __clone() |
| 45 |
{ |
| 46 |
$this->keyIdMap = null; |
| 47 |
} |
| 48 |
|
| 49 |
public static function create(Certificate ...$certs): self |
| 50 |
{ |
| 51 |
return new self(...$certs); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public static function fromPEMs(PEM ...$pems): self |
| 60 |
{ |
| 61 |
$certs = array_map(Certificate::fromPEM(...), $pems); |
| 62 |
return self::create(...$certs); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public static function fromPEMBundle(PEMBundle $pem_bundle): self |
| 69 |
{ |
| 70 |
return self::fromPEMs(...$pem_bundle->all()); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public function withCertificates(Certificate ...$cert): self |
| 77 |
{ |
| 78 |
$obj = clone $this; |
| 79 |
$obj->certs = array_merge($obj->certs, $cert); |
| 80 |
return $obj; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public function withPEMBundle(PEMBundle $pem_bundle): self |
| 87 |
{ |
| 88 |
$certs = $this->certs; |
| 89 |
foreach ($pem_bundle as $pem) { |
| 90 |
$certs[] = Certificate::fromPEM($pem); |
| 91 |
} |
| 92 |
return self::create(...$certs); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public function withPEM(PEM $pem): self |
| 99 |
{ |
| 100 |
$certs = $this->certs; |
| 101 |
$certs[] = Certificate::fromPEM($pem); |
| 102 |
return self::create(...$certs); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public function contains(Certificate $cert): bool |
| 109 |
{ |
| 110 |
$id = self::_getCertKeyId($cert); |
| 111 |
$map = $this->_getKeyIdMap(); |
| 112 |
if (! isset($map[$id])) { |
| 113 |
return false; |
| 114 |
} |
| 115 |
foreach ($map[$id] as $c) { |
| 116 |
|
| 117 |
if ($cert->equals($c)) { |
| 118 |
return true; |
| 119 |
} |
| 120 |
} |
| 121 |
return false; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public function allBySubjectKeyIdentifier(string $id): array |
| 130 |
{ |
| 131 |
$map = $this->_getKeyIdMap(); |
| 132 |
if (! isset($map[$id])) { |
| 133 |
return []; |
| 134 |
} |
| 135 |
return $map[$id]; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public function all(): array |
| 144 |
{ |
| 145 |
return $this->certs; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public function count(): int |
| 152 |
{ |
| 153 |
return count($this->certs); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public function getIterator(): ArrayIterator |
| 162 |
{ |
| 163 |
return new ArrayIterator($this->certs); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
private function _getKeyIdMap(): array |
| 172 |
{ |
| 173 |
|
| 174 |
if (! isset($this->keyIdMap)) { |
| 175 |
$this->keyIdMap = []; |
| 176 |
foreach ($this->certs as $cert) { |
| 177 |
$id = self::_getCertKeyId($cert); |
| 178 |
if (! isset($this->keyIdMap[$id])) { |
| 179 |
$this->keyIdMap[$id] = []; |
| 180 |
} |
| 181 |
array_push($this->keyIdMap[$id], $cert); |
| 182 |
} |
| 183 |
} |
| 184 |
return $this->keyIdMap; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
private static function _getCertKeyId(Certificate $cert): string |
| 191 |
{ |
| 192 |
$exts = $cert->tbsCertificate() |
| 193 |
->extensions(); |
| 194 |
if ($exts->hasSubjectKeyIdentifier()) { |
| 195 |
return $exts->subjectKeyIdentifier() |
| 196 |
->keyIdentifier(); |
| 197 |
} |
| 198 |
return $cert->tbsCertificate() |
| 199 |
->subjectPublicKeyInfo() |
| 200 |
->keyIdentifier(); |
| 201 |
} |
| 202 |
} |