| Total Complexity | 37 |
| Total Lines | 283 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 0 | ||
| 1 | <?php |
||
| 20 | class Holder |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * Holder PKC's issuer and serial. |
||
| 24 | * |
||
| 25 | * @var null|IssuerSerial |
||
| 26 | */ |
||
| 27 | protected $_baseCertificateID; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Holder PKC's subject. |
||
| 31 | * |
||
| 32 | * @var null|GeneralNames |
||
| 33 | */ |
||
| 34 | protected $_entityName; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Linked object. |
||
| 38 | * |
||
| 39 | * @var null|ObjectDigestInfo |
||
| 40 | */ |
||
| 41 | protected $_objectDigestInfo; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Constructor. |
||
| 45 | * |
||
| 46 | * @param null|IssuerSerial $issuer_serial |
||
| 47 | * @param null|GeneralNames $entity_name |
||
| 48 | */ |
||
| 49 | 21 | public function __construct(?IssuerSerial $issuer_serial = null, |
|
| 50 | ?GeneralNames $entity_name = null) |
||
| 51 | { |
||
| 52 | 21 | $this->_baseCertificateID = $issuer_serial; |
|
| 53 | 21 | $this->_entityName = $entity_name; |
|
| 54 | 21 | } |
|
| 55 | |||
| 56 | /** |
||
| 57 | * Initialize from a holder's public key certificate. |
||
| 58 | * |
||
| 59 | * @param Certificate $cert |
||
| 60 | * |
||
| 61 | * @return self |
||
| 62 | */ |
||
| 63 | 1 | public static function fromPKC(Certificate $cert): self |
|
| 64 | { |
||
| 65 | 1 | return new self(IssuerSerial::fromPKC($cert)); |
|
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Initialize from ASN.1. |
||
| 70 | * |
||
| 71 | * @param Sequence $seq |
||
| 72 | */ |
||
| 73 | 7 | public static function fromASN1(Sequence $seq): self |
|
| 74 | { |
||
| 75 | 7 | $cert_id = null; |
|
| 76 | 7 | $entity_name = null; |
|
| 77 | 7 | $digest_info = null; |
|
| 78 | 7 | if ($seq->hasTagged(0)) { |
|
| 79 | 7 | $cert_id = IssuerSerial::fromASN1( |
|
| 80 | 7 | $seq->getTagged(0)->asImplicit(Element::TYPE_SEQUENCE) |
|
| 81 | 7 | ->asSequence()); |
|
| 82 | } |
||
| 83 | 7 | if ($seq->hasTagged(1)) { |
|
| 84 | 3 | $entity_name = GeneralNames::fromASN1( |
|
| 85 | 3 | $seq->getTagged(1)->asImplicit(Element::TYPE_SEQUENCE) |
|
| 86 | 3 | ->asSequence()); |
|
| 87 | } |
||
| 88 | 7 | if ($seq->hasTagged(2)) { |
|
| 89 | 1 | $digest_info = ObjectDigestInfo::fromASN1( |
|
| 90 | 1 | $seq->getTagged(2)->asImplicit(Element::TYPE_SEQUENCE) |
|
| 91 | 1 | ->asSequence()); |
|
| 92 | } |
||
| 93 | 7 | $obj = new self($cert_id, $entity_name); |
|
| 94 | 7 | $obj->_objectDigestInfo = $digest_info; |
|
| 95 | 7 | return $obj; |
|
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Get self with base certificate ID. |
||
| 100 | * |
||
| 101 | * @param IssuerSerial $issuer |
||
| 102 | * |
||
| 103 | * @return self |
||
| 104 | */ |
||
| 105 | 1 | public function withBaseCertificateID(IssuerSerial $issuer): self |
|
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Get self with entity name. |
||
| 114 | * |
||
| 115 | * @param GeneralNames $names |
||
| 116 | * |
||
| 117 | * @return self |
||
| 118 | */ |
||
| 119 | 1 | public function withEntityName(GeneralNames $names): self |
|
| 120 | { |
||
| 121 | 1 | $obj = clone $this; |
|
| 122 | 1 | $obj->_entityName = $names; |
|
| 123 | 1 | return $obj; |
|
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Get self with object digest info. |
||
| 128 | * |
||
| 129 | * @param ObjectDigestInfo $odi |
||
| 130 | * |
||
| 131 | * @return self |
||
| 132 | */ |
||
| 133 | 2 | public function withObjectDigestInfo(ObjectDigestInfo $odi): self |
|
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Check whether base certificate ID is present. |
||
| 142 | * |
||
| 143 | * @return bool |
||
| 144 | */ |
||
| 145 | 2 | public function hasBaseCertificateID(): bool |
|
| 146 | { |
||
| 147 | 2 | return isset($this->_baseCertificateID); |
|
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Get base certificate ID. |
||
| 152 | * |
||
| 153 | * @throws \LogicException If not set |
||
| 154 | * |
||
| 155 | * @return IssuerSerial |
||
| 156 | */ |
||
| 157 | 2 | public function baseCertificateID(): IssuerSerial |
|
| 158 | { |
||
| 159 | 2 | if (!$this->hasBaseCertificateID()) { |
|
| 160 | 1 | throw new \LogicException('baseCertificateID not set.'); |
|
| 161 | } |
||
| 162 | 1 | return $this->_baseCertificateID; |
|
|
1 ignored issue
–
show
|
|||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Check whether entity name is present. |
||
| 167 | * |
||
| 168 | * @return bool |
||
| 169 | */ |
||
| 170 | 2 | public function hasEntityName(): bool |
|
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Get entity name. |
||
| 177 | * |
||
| 178 | * @throws \LogicException If not set |
||
| 179 | * |
||
| 180 | * @return GeneralNames |
||
| 181 | */ |
||
| 182 | 2 | public function entityName(): GeneralNames |
|
| 183 | { |
||
| 184 | 2 | if (!$this->hasEntityName()) { |
|
| 185 | 1 | throw new \LogicException('entityName not set.'); |
|
| 186 | } |
||
| 187 | 1 | return $this->_entityName; |
|
|
1 ignored issue
–
show
|
|||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Check whether object digest info is present. |
||
| 192 | * |
||
| 193 | * @return bool |
||
| 194 | */ |
||
| 195 | 2 | public function hasObjectDigestInfo(): bool |
|
| 196 | { |
||
| 197 | 2 | return isset($this->_objectDigestInfo); |
|
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Get object digest info. |
||
| 202 | * |
||
| 203 | * @throws \LogicException If not set |
||
| 204 | * |
||
| 205 | * @return ObjectDigestInfo |
||
| 206 | */ |
||
| 207 | 2 | public function objectDigestInfo(): ObjectDigestInfo |
|
| 208 | { |
||
| 209 | 2 | if (!$this->hasObjectDigestInfo()) { |
|
| 210 | 1 | throw new \LogicException('objectDigestInfo not set.'); |
|
| 211 | } |
||
| 212 | 1 | return $this->_objectDigestInfo; |
|
|
1 ignored issue
–
show
|
|||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Generate ASN.1 structure. |
||
| 217 | * |
||
| 218 | * @return Sequence |
||
| 219 | */ |
||
| 220 | 20 | public function toASN1(): Sequence |
|
| 221 | { |
||
| 222 | 20 | $elements = []; |
|
| 223 | 20 | if (isset($this->_baseCertificateID)) { |
|
| 224 | 20 | $elements[] = new ImplicitlyTaggedType(0, |
|
| 225 | 20 | $this->_baseCertificateID->toASN1()); |
|
|
1 ignored issue
–
show
|
|||
| 226 | } |
||
| 227 | 20 | if (isset($this->_entityName)) { |
|
| 228 | 4 | $elements[] = new ImplicitlyTaggedType(1, |
|
| 229 | 4 | $this->_entityName->toASN1()); |
|
|
1 ignored issue
–
show
|
|||
| 230 | } |
||
| 231 | 20 | if (isset($this->_objectDigestInfo)) { |
|
| 232 | 1 | $elements[] = new ImplicitlyTaggedType(2, |
|
| 233 | 1 | $this->_objectDigestInfo->toASN1()); |
|
|
1 ignored issue
–
show
|
|||
| 234 | } |
||
| 235 | 20 | return new Sequence(...$elements); |
|
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Check whether Holder identifies given certificate. |
||
| 240 | * |
||
| 241 | * @param Certificate $cert |
||
| 242 | * |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | 19 | public function identifiesPKC(Certificate $cert): bool |
|
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Check whether entityName matches the given certificate. |
||
| 265 | * |
||
| 266 | * @param Certificate $cert |
||
| 267 | * |
||
| 268 | * @return bool |
||
| 269 | */ |
||
| 270 | 4 | private function _checkEntityName(Certificate $cert): bool |
|
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Check whether any of the subject alternative names match entityName. |
||
| 288 | * |
||
| 289 | * @param GeneralNames $san |
||
| 290 | * |
||
| 291 | * @return bool |
||
| 292 | */ |
||
| 293 | 2 | private function _checkEntityAlternativeNames(GeneralNames $san): bool |
|
| 303 | } |
||
| 304 | } |
||
| 305 |