| 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 LogicException; |
| 12 |
use SpomkyLabs\Pki\ASN1\Type\Constructed\Sequence; |
| 13 |
use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType; |
| 14 |
use SpomkyLabs\Pki\X509\Certificate\Extension\AuthorityKeyIdentifierExtension; |
| 15 |
use SpomkyLabs\Pki\X509\Certificate\Extension\BasicConstraintsExtension; |
| 16 |
use SpomkyLabs\Pki\X509\Certificate\Extension\CertificatePoliciesExtension; |
| 17 |
use SpomkyLabs\Pki\X509\Certificate\Extension\CRLDistributionPointsExtension; |
| 18 |
use SpomkyLabs\Pki\X509\Certificate\Extension\ExtendedKeyUsageExtension; |
| 19 |
use SpomkyLabs\Pki\X509\Certificate\Extension\Extension; |
| 20 |
use SpomkyLabs\Pki\X509\Certificate\Extension\InhibitAnyPolicyExtension; |
| 21 |
use SpomkyLabs\Pki\X509\Certificate\Extension\IssuerAlternativeNameExtension; |
| 22 |
use SpomkyLabs\Pki\X509\Certificate\Extension\KeyUsageExtension; |
| 23 |
use SpomkyLabs\Pki\X509\Certificate\Extension\NameConstraintsExtension; |
| 24 |
use SpomkyLabs\Pki\X509\Certificate\Extension\PolicyConstraintsExtension; |
| 25 |
use SpomkyLabs\Pki\X509\Certificate\Extension\PolicyMappingsExtension; |
| 26 |
use SpomkyLabs\Pki\X509\Certificate\Extension\SubjectAlternativeNameExtension; |
| 27 |
use SpomkyLabs\Pki\X509\Certificate\Extension\SubjectKeyIdentifierExtension; |
| 28 |
use Traversable; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
final class Extensions implements Countable, IteratorAggregate |
| 39 |
{ |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
private array $extensions; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
private function __construct(Extension ...$extensions) |
| 51 |
{ |
| 52 |
$this->extensions = []; |
| 53 |
foreach ($extensions as $ext) { |
| 54 |
$this->extensions[$ext->oid()] = $ext; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
public static function create(Extension ...$extensions): self |
| 59 |
{ |
| 60 |
return new self(...$extensions); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public static function fromASN1(Sequence $seq): self |
| 67 |
{ |
| 68 |
$extensions = array_map( |
| 69 |
static fn (UnspecifiedType $el) => Extension::fromASN1($el->asSequence()), |
| 70 |
$seq->elements() |
| 71 |
); |
| 72 |
return self::create(...$extensions); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public function toASN1(): Sequence |
| 79 |
{ |
| 80 |
$elements = array_values(array_map(static fn ($ext) => $ext->toASN1(), $this->extensions)); |
| 81 |
return Sequence::create(...$elements); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public function withExtensions(Extension ...$exts): self |
| 90 |
{ |
| 91 |
$obj = clone $this; |
| 92 |
foreach ($exts as $ext) { |
| 93 |
$obj->extensions[$ext->oid()] = $ext; |
| 94 |
} |
| 95 |
return $obj; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public function has(string $oid): bool |
| 104 |
{ |
| 105 |
return isset($this->extensions[$oid]); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public function get(string $oid): Extension |
| 112 |
{ |
| 113 |
if (! $this->has($oid)) { |
| 114 |
throw new LogicException("No extension by OID {$oid}."); |
| 115 |
} |
| 116 |
return $this->extensions[$oid]; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public function hasAuthorityKeyIdentifier(): bool |
| 123 |
{ |
| 124 |
return $this->has(Extension::OID_AUTHORITY_KEY_IDENTIFIER); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public function authorityKeyIdentifier(): AuthorityKeyIdentifierExtension |
| 131 |
{ |
| 132 |
return $this->get(Extension::OID_AUTHORITY_KEY_IDENTIFIER); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public function hasSubjectKeyIdentifier(): bool |
| 139 |
{ |
| 140 |
return $this->has(Extension::OID_SUBJECT_KEY_IDENTIFIER); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public function subjectKeyIdentifier(): SubjectKeyIdentifierExtension |
| 147 |
{ |
| 148 |
return $this->get(Extension::OID_SUBJECT_KEY_IDENTIFIER); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public function hasKeyUsage(): bool |
| 155 |
{ |
| 156 |
return $this->has(Extension::OID_KEY_USAGE); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public function keyUsage(): KeyUsageExtension |
| 163 |
{ |
| 164 |
return $this->get(Extension::OID_KEY_USAGE); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public function hasCertificatePolicies(): bool |
| 171 |
{ |
| 172 |
return $this->has(Extension::OID_CERTIFICATE_POLICIES); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
public function certificatePolicies(): CertificatePoliciesExtension |
| 179 |
{ |
| 180 |
return $this->get(Extension::OID_CERTIFICATE_POLICIES); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public function hasPolicyMappings(): bool |
| 187 |
{ |
| 188 |
return $this->has(Extension::OID_POLICY_MAPPINGS); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
public function policyMappings(): PolicyMappingsExtension |
| 195 |
{ |
| 196 |
return $this->get(Extension::OID_POLICY_MAPPINGS); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public function hasSubjectAlternativeName(): bool |
| 203 |
{ |
| 204 |
return $this->has(Extension::OID_SUBJECT_ALT_NAME); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public function subjectAlternativeName(): SubjectAlternativeNameExtension |
| 211 |
{ |
| 212 |
return $this->get(Extension::OID_SUBJECT_ALT_NAME); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public function hasIssuerAlternativeName(): bool |
| 219 |
{ |
| 220 |
return $this->has(Extension::OID_ISSUER_ALT_NAME); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
public function issuerAlternativeName(): IssuerAlternativeNameExtension |
| 227 |
{ |
| 228 |
return $this->get(Extension::OID_ISSUER_ALT_NAME); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
public function hasBasicConstraints(): bool |
| 235 |
{ |
| 236 |
return $this->has(Extension::OID_BASIC_CONSTRAINTS); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
public function basicConstraints(): BasicConstraintsExtension |
| 243 |
{ |
| 244 |
return $this->get(Extension::OID_BASIC_CONSTRAINTS); |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
public function hasNameConstraints(): bool |
| 251 |
{ |
| 252 |
return $this->has(Extension::OID_NAME_CONSTRAINTS); |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
public function nameConstraints(): NameConstraintsExtension |
| 259 |
{ |
| 260 |
return $this->get(Extension::OID_NAME_CONSTRAINTS); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
public function hasPolicyConstraints(): bool |
| 267 |
{ |
| 268 |
return $this->has(Extension::OID_POLICY_CONSTRAINTS); |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
public function policyConstraints(): PolicyConstraintsExtension |
| 275 |
{ |
| 276 |
return $this->get(Extension::OID_POLICY_CONSTRAINTS); |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
public function hasExtendedKeyUsage(): bool |
| 283 |
{ |
| 284 |
return $this->has(Extension::OID_EXT_KEY_USAGE); |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
public function extendedKeyUsage(): ExtendedKeyUsageExtension |
| 291 |
{ |
| 292 |
return $this->get(Extension::OID_EXT_KEY_USAGE); |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
public function hasCRLDistributionPoints(): bool |
| 299 |
{ |
| 300 |
return $this->has(Extension::OID_CRL_DISTRIBUTION_POINTS); |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
public function crlDistributionPoints(): CRLDistributionPointsExtension |
| 307 |
{ |
| 308 |
return $this->get(Extension::OID_CRL_DISTRIBUTION_POINTS); |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
public function hasInhibitAnyPolicy(): bool |
| 315 |
{ |
| 316 |
return $this->has(Extension::OID_INHIBIT_ANY_POLICY); |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
public function inhibitAnyPolicy(): InhibitAnyPolicyExtension |
| 323 |
{ |
| 324 |
return $this->get(Extension::OID_INHIBIT_ANY_POLICY); |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
public function count(): int |
| 331 |
{ |
| 332 |
return count($this->extensions); |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
public function getIterator(): Traversable |
| 341 |
{ |
| 342 |
return new ArrayIterator($this->extensions); |
| 343 |
} |
| 344 |
} |