| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SpomkyLabs\Pki\X509\AttributeCertificate; |
| 6 |
|
| 7 |
use LogicException; |
| 8 |
use SpomkyLabs\Pki\ASN1\Element; |
| 9 |
use SpomkyLabs\Pki\ASN1\Type\Constructed\Sequence; |
| 10 |
use SpomkyLabs\Pki\ASN1\Type\Tagged\ImplicitlyTaggedType; |
| 11 |
use SpomkyLabs\Pki\X509\Certificate\Certificate; |
| 12 |
use SpomkyLabs\Pki\X509\GeneralName\DirectoryName; |
| 13 |
use SpomkyLabs\Pki\X509\GeneralName\GeneralName; |
| 14 |
use SpomkyLabs\Pki\X509\GeneralName\GeneralNames; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
final class Holder |
| 22 |
{ |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
private ?ObjectDigestInfo $objectDigestInfo = null; |
| 27 |
|
| 28 |
private function __construct( |
| 29 |
private ?IssuerSerial $baseCertificateID, |
| 30 |
private ?GeneralNames $entityName |
| 31 |
) { |
| 32 |
} |
| 33 |
|
| 34 |
public static function create(?IssuerSerial $baseCertificateID = null, ?GeneralNames $entityName = null): self |
| 35 |
{ |
| 36 |
return new self($baseCertificateID, $entityName); |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public static function fromPKC(Certificate $cert): self |
| 43 |
{ |
| 44 |
return self::create(IssuerSerial::fromPKC($cert)); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public static function fromASN1(Sequence $seq): self |
| 51 |
{ |
| 52 |
$cert_id = null; |
| 53 |
$entity_name = null; |
| 54 |
$digest_info = null; |
| 55 |
if ($seq->hasTagged(0)) { |
| 56 |
$cert_id = IssuerSerial::fromASN1( |
| 57 |
$seq->getTagged(0) |
| 58 |
->asImplicit(Element::TYPE_SEQUENCE) |
| 59 |
->asSequence() |
| 60 |
); |
| 61 |
} |
| 62 |
if ($seq->hasTagged(1)) { |
| 63 |
$entity_name = GeneralNames::fromASN1( |
| 64 |
$seq->getTagged(1) |
| 65 |
->asImplicit(Element::TYPE_SEQUENCE) |
| 66 |
->asSequence() |
| 67 |
); |
| 68 |
} |
| 69 |
if ($seq->hasTagged(2)) { |
| 70 |
$digest_info = ObjectDigestInfo::fromASN1( |
| 71 |
$seq->getTagged(2) |
| 72 |
->asImplicit(Element::TYPE_SEQUENCE) |
| 73 |
->asSequence() |
| 74 |
); |
| 75 |
} |
| 76 |
return self::create($cert_id, $entity_name) |
| 77 |
->withObjectDigestInfo($digest_info); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public function withBaseCertificateID(IssuerSerial $issuer): self |
| 84 |
{ |
| 85 |
$obj = clone $this; |
| 86 |
$obj->baseCertificateID = $issuer; |
| 87 |
return $obj; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public function withEntityName(GeneralNames $names): self |
| 94 |
{ |
| 95 |
$obj = clone $this; |
| 96 |
$obj->entityName = $names; |
| 97 |
return $obj; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public function withObjectDigestInfo(?ObjectDigestInfo $odi): self |
| 104 |
{ |
| 105 |
$obj = clone $this; |
| 106 |
$obj->objectDigestInfo = $odi; |
| 107 |
return $obj; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public function hasBaseCertificateID(): bool |
| 114 |
{ |
| 115 |
return isset($this->baseCertificateID); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public function baseCertificateID(): IssuerSerial |
| 122 |
{ |
| 123 |
if (! $this->hasBaseCertificateID()) { |
| 124 |
throw new LogicException('baseCertificateID not set.'); |
| 125 |
} |
| 126 |
return $this->baseCertificateID; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public function hasEntityName(): bool |
| 133 |
{ |
| 134 |
return isset($this->entityName); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
public function entityName(): GeneralNames |
| 141 |
{ |
| 142 |
if (! $this->hasEntityName()) { |
| 143 |
throw new LogicException('entityName not set.'); |
| 144 |
} |
| 145 |
return $this->entityName; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public function hasObjectDigestInfo(): bool |
| 152 |
{ |
| 153 |
return isset($this->objectDigestInfo); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public function objectDigestInfo(): ObjectDigestInfo |
| 160 |
{ |
| 161 |
if (! $this->hasObjectDigestInfo()) { |
| 162 |
throw new LogicException('objectDigestInfo not set.'); |
| 163 |
} |
| 164 |
return $this->objectDigestInfo; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public function toASN1(): Sequence |
| 171 |
{ |
| 172 |
$elements = []; |
| 173 |
if (isset($this->baseCertificateID)) { |
| 174 |
$elements[] = ImplicitlyTaggedType::create(0, $this->baseCertificateID->toASN1()); |
| 175 |
} |
| 176 |
if (isset($this->entityName)) { |
| 177 |
$elements[] = ImplicitlyTaggedType::create(1, $this->entityName->toASN1()); |
| 178 |
} |
| 179 |
if (isset($this->objectDigestInfo)) { |
| 180 |
$elements[] = ImplicitlyTaggedType::create(2, $this->objectDigestInfo->toASN1()); |
| 181 |
} |
| 182 |
return Sequence::create(...$elements); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
public function identifiesPKC(Certificate $cert): bool |
| 189 |
{ |
| 190 |
|
| 191 |
if ($this->baseCertificateID === null && $this->entityName === null) { |
| 192 |
return false; |
| 193 |
} |
| 194 |
|
| 195 |
if ($this->baseCertificateID !== null && ! $this->baseCertificateID->identifiesPKC($cert)) { |
| 196 |
return false; |
| 197 |
} |
| 198 |
|
| 199 |
if ($this->entityName !== null && ! $this->_checkEntityName($cert)) { |
| 200 |
return false; |
| 201 |
} |
| 202 |
return true; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
private function _checkEntityName(Certificate $cert): bool |
| 209 |
{ |
| 210 |
$name = $this->entityName?->firstDN(); |
| 211 |
if ($name !== null && $cert->tbsCertificate()->subject()->equals($name)) { |
| 212 |
return true; |
| 213 |
} |
| 214 |
$exts = $cert->tbsCertificate() |
| 215 |
->extensions(); |
| 216 |
if ($exts->hasSubjectAlternativeName()) { |
| 217 |
$ext = $exts->subjectAlternativeName(); |
| 218 |
if ($this->_checkEntityAlternativeNames($ext->names())) { |
| 219 |
return true; |
| 220 |
} |
| 221 |
} |
| 222 |
return false; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
private function _checkEntityAlternativeNames(GeneralNames $san): bool |
| 229 |
{ |
| 230 |
|
| 231 |
$name = $this->entityName?->firstDN(); |
| 232 |
if ($name === null) { |
| 233 |
return false; |
| 234 |
} |
| 235 |
foreach ($san->allOf(GeneralName::TAG_DIRECTORY_NAME) as $dn) { |
| 236 |
if ($dn instanceof DirectoryName && $dn->dn()->equals($name)) { |
| 237 |
return true; |
| 238 |
} |
| 239 |
} |
| 240 |
return false; |
| 241 |
} |
| 242 |
} |