@@ -17,667 +17,667 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class UnspecifiedType implements ElementBase |
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * The wrapped element. |
|
| 22 | - * |
|
| 23 | - * @var Element |
|
| 24 | - */ |
|
| 25 | - private $_element; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * Constructor. |
|
| 29 | - * |
|
| 30 | - * @param Element $el |
|
| 31 | - */ |
|
| 32 | - public function __construct(Element $el) |
|
| 33 | - { |
|
| 34 | - $this->_element = $el; |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Initialize from DER data. |
|
| 39 | - * |
|
| 40 | - * @param string $data DER encoded data |
|
| 41 | - * |
|
| 42 | - * @return self |
|
| 43 | - */ |
|
| 44 | - public static function fromDER(string $data): self |
|
| 45 | - { |
|
| 46 | - return Element::fromDER($data)->asUnspecified(); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Initialize from ElementBase interface. |
|
| 51 | - * |
|
| 52 | - * @param ElementBase $el |
|
| 53 | - * |
|
| 54 | - * @return self |
|
| 55 | - */ |
|
| 56 | - public static function fromElementBase(ElementBase $el): self |
|
| 57 | - { |
|
| 58 | - // if element is already wrapped |
|
| 59 | - if ($el instanceof self) { |
|
| 60 | - return $el; |
|
| 61 | - } |
|
| 62 | - return new self($el->asElement()); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Get the wrapped element as a context specific tagged type. |
|
| 67 | - * |
|
| 68 | - * @throws \UnexpectedValueException If the element is not tagged |
|
| 69 | - * |
|
| 70 | - * @return TaggedType |
|
| 71 | - */ |
|
| 72 | - public function asTagged(): TaggedType |
|
| 73 | - { |
|
| 74 | - if (!$this->_element instanceof TaggedType) { |
|
| 75 | - throw new \UnexpectedValueException( |
|
| 76 | - 'Tagged element expected, got ' . $this->_typeDescriptorString()); |
|
| 77 | - } |
|
| 78 | - return $this->_element; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Get the wrapped element as an application specific type. |
|
| 83 | - * |
|
| 84 | - * @throws \UnexpectedValueException If element is not application specific |
|
| 85 | - * |
|
| 86 | - * @return \Sop\ASN1\Type\Tagged\ApplicationType |
|
| 87 | - */ |
|
| 88 | - public function asApplication(): Tagged\ApplicationType |
|
| 89 | - { |
|
| 90 | - if (!$this->_element instanceof Tagged\ApplicationType) { |
|
| 91 | - throw new \UnexpectedValueException( |
|
| 92 | - 'Application type expected, got ' . |
|
| 93 | - $this->_typeDescriptorString()); |
|
| 94 | - } |
|
| 95 | - return $this->_element; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Get the wrapped element as a private tagged type. |
|
| 100 | - * |
|
| 101 | - * @throws \UnexpectedValueException If element is not using private tagging |
|
| 102 | - * |
|
| 103 | - * @return \Sop\ASN1\Type\Tagged\PrivateType |
|
| 104 | - */ |
|
| 105 | - public function asPrivate(): Tagged\PrivateType |
|
| 106 | - { |
|
| 107 | - if (!$this->_element instanceof Tagged\PrivateType) { |
|
| 108 | - throw new \UnexpectedValueException( |
|
| 109 | - 'Private type expected, got ' . $this->_typeDescriptorString()); |
|
| 110 | - } |
|
| 111 | - return $this->_element; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Get the wrapped element as a boolean type. |
|
| 116 | - * |
|
| 117 | - * @throws \UnexpectedValueException If the element is not a boolean |
|
| 118 | - * |
|
| 119 | - * @return \Sop\ASN1\Type\Primitive\Boolean |
|
| 120 | - */ |
|
| 121 | - public function asBoolean(): Primitive\Boolean |
|
| 122 | - { |
|
| 123 | - if (!$this->_element instanceof Primitive\Boolean) { |
|
| 124 | - throw new \UnexpectedValueException( |
|
| 125 | - $this->_generateExceptionMessage(Element::TYPE_BOOLEAN)); |
|
| 126 | - } |
|
| 127 | - return $this->_element; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * Get the wrapped element as an integer type. |
|
| 132 | - * |
|
| 133 | - * @throws \UnexpectedValueException If the element is not an integer |
|
| 134 | - * |
|
| 135 | - * @return \Sop\ASN1\Type\Primitive\Integer |
|
| 136 | - */ |
|
| 137 | - public function asInteger(): Primitive\Integer |
|
| 138 | - { |
|
| 139 | - if (!$this->_element instanceof Primitive\Integer) { |
|
| 140 | - throw new \UnexpectedValueException( |
|
| 141 | - $this->_generateExceptionMessage(Element::TYPE_INTEGER)); |
|
| 142 | - } |
|
| 143 | - return $this->_element; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Get the wrapped element as a bit string type. |
|
| 148 | - * |
|
| 149 | - * @throws \UnexpectedValueException If the element is not a bit string |
|
| 150 | - * |
|
| 151 | - * @return \Sop\ASN1\Type\Primitive\BitString |
|
| 152 | - */ |
|
| 153 | - public function asBitString(): Primitive\BitString |
|
| 154 | - { |
|
| 155 | - if (!$this->_element instanceof Primitive\BitString) { |
|
| 156 | - throw new \UnexpectedValueException( |
|
| 157 | - $this->_generateExceptionMessage(Element::TYPE_BIT_STRING)); |
|
| 158 | - } |
|
| 159 | - return $this->_element; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Get the wrapped element as an octet string type. |
|
| 164 | - * |
|
| 165 | - * @throws \UnexpectedValueException If the element is not an octet string |
|
| 166 | - * |
|
| 167 | - * @return \Sop\ASN1\Type\Primitive\OctetString |
|
| 168 | - */ |
|
| 169 | - public function asOctetString(): Primitive\OctetString |
|
| 170 | - { |
|
| 171 | - if (!$this->_element instanceof Primitive\OctetString) { |
|
| 172 | - throw new \UnexpectedValueException( |
|
| 173 | - $this->_generateExceptionMessage(Element::TYPE_OCTET_STRING)); |
|
| 174 | - } |
|
| 175 | - return $this->_element; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Get the wrapped element as a null type. |
|
| 180 | - * |
|
| 181 | - * @throws \UnexpectedValueException If the element is not a null |
|
| 182 | - * |
|
| 183 | - * @return \Sop\ASN1\Type\Primitive\NullType |
|
| 184 | - */ |
|
| 185 | - public function asNull(): Primitive\NullType |
|
| 186 | - { |
|
| 187 | - if (!$this->_element instanceof Primitive\NullType) { |
|
| 188 | - throw new \UnexpectedValueException( |
|
| 189 | - $this->_generateExceptionMessage(Element::TYPE_NULL)); |
|
| 190 | - } |
|
| 191 | - return $this->_element; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Get the wrapped element as an object identifier type. |
|
| 196 | - * |
|
| 197 | - * @throws \UnexpectedValueException If the element is not an object |
|
| 198 | - * identifier |
|
| 199 | - * |
|
| 200 | - * @return \Sop\ASN1\Type\Primitive\ObjectIdentifier |
|
| 201 | - */ |
|
| 202 | - public function asObjectIdentifier(): Primitive\ObjectIdentifier |
|
| 203 | - { |
|
| 204 | - if (!$this->_element instanceof Primitive\ObjectIdentifier) { |
|
| 205 | - throw new \UnexpectedValueException( |
|
| 206 | - $this->_generateExceptionMessage( |
|
| 207 | - Element::TYPE_OBJECT_IDENTIFIER)); |
|
| 208 | - } |
|
| 209 | - return $this->_element; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * Get the wrapped element as an object descriptor type. |
|
| 214 | - * |
|
| 215 | - * @throws \UnexpectedValueException If the element is not an object |
|
| 216 | - * descriptor |
|
| 217 | - * |
|
| 218 | - * @return \Sop\ASN1\Type\Primitive\ObjectDescriptor |
|
| 219 | - */ |
|
| 220 | - public function asObjectDescriptor(): Primitive\ObjectDescriptor |
|
| 221 | - { |
|
| 222 | - if (!$this->_element instanceof Primitive\ObjectDescriptor) { |
|
| 223 | - throw new \UnexpectedValueException( |
|
| 224 | - $this->_generateExceptionMessage( |
|
| 225 | - Element::TYPE_OBJECT_DESCRIPTOR)); |
|
| 226 | - } |
|
| 227 | - return $this->_element; |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - /** |
|
| 231 | - * Get the wrapped element as a real type. |
|
| 232 | - * |
|
| 233 | - * @throws \UnexpectedValueException If the element is not a real |
|
| 234 | - * |
|
| 235 | - * @return \Sop\ASN1\Type\Primitive\Real |
|
| 236 | - */ |
|
| 237 | - public function asReal(): Primitive\Real |
|
| 238 | - { |
|
| 239 | - if (!$this->_element instanceof Primitive\Real) { |
|
| 240 | - throw new \UnexpectedValueException( |
|
| 241 | - $this->_generateExceptionMessage(Element::TYPE_REAL)); |
|
| 242 | - } |
|
| 243 | - return $this->_element; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * Get the wrapped element as an enumerated type. |
|
| 248 | - * |
|
| 249 | - * @throws \UnexpectedValueException If the element is not an enumerated |
|
| 250 | - * |
|
| 251 | - * @return \Sop\ASN1\Type\Primitive\Enumerated |
|
| 252 | - */ |
|
| 253 | - public function asEnumerated(): Primitive\Enumerated |
|
| 254 | - { |
|
| 255 | - if (!$this->_element instanceof Primitive\Enumerated) { |
|
| 256 | - throw new \UnexpectedValueException( |
|
| 257 | - $this->_generateExceptionMessage(Element::TYPE_ENUMERATED)); |
|
| 258 | - } |
|
| 259 | - return $this->_element; |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * Get the wrapped element as a UTF8 string type. |
|
| 264 | - * |
|
| 265 | - * @throws \UnexpectedValueException If the element is not a UTF8 string |
|
| 266 | - * |
|
| 267 | - * @return \Sop\ASN1\Type\Primitive\UTF8String |
|
| 268 | - */ |
|
| 269 | - public function asUTF8String(): Primitive\UTF8String |
|
| 270 | - { |
|
| 271 | - if (!$this->_element instanceof Primitive\UTF8String) { |
|
| 272 | - throw new \UnexpectedValueException( |
|
| 273 | - $this->_generateExceptionMessage(Element::TYPE_UTF8_STRING)); |
|
| 274 | - } |
|
| 275 | - return $this->_element; |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - /** |
|
| 279 | - * Get the wrapped element as a relative OID type. |
|
| 280 | - * |
|
| 281 | - * @throws \UnexpectedValueException If the element is not a relative OID |
|
| 282 | - * |
|
| 283 | - * @return \Sop\ASN1\Type\Primitive\RelativeOID |
|
| 284 | - */ |
|
| 285 | - public function asRelativeOID(): Primitive\RelativeOID |
|
| 286 | - { |
|
| 287 | - if (!$this->_element instanceof Primitive\RelativeOID) { |
|
| 288 | - throw new \UnexpectedValueException( |
|
| 289 | - $this->_generateExceptionMessage(Element::TYPE_RELATIVE_OID)); |
|
| 290 | - } |
|
| 291 | - return $this->_element; |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - /** |
|
| 295 | - * Get the wrapped element as a sequence type. |
|
| 296 | - * |
|
| 297 | - * @throws \UnexpectedValueException If the element is not a sequence |
|
| 298 | - * |
|
| 299 | - * @return \Sop\ASN1\Type\Constructed\Sequence |
|
| 300 | - */ |
|
| 301 | - public function asSequence(): Constructed\Sequence |
|
| 302 | - { |
|
| 303 | - if (!$this->_element instanceof Constructed\Sequence) { |
|
| 304 | - throw new \UnexpectedValueException( |
|
| 305 | - $this->_generateExceptionMessage(Element::TYPE_SEQUENCE)); |
|
| 306 | - } |
|
| 307 | - return $this->_element; |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - /** |
|
| 311 | - * Get the wrapped element as a set type. |
|
| 312 | - * |
|
| 313 | - * @throws \UnexpectedValueException If the element is not a set |
|
| 314 | - * |
|
| 315 | - * @return \Sop\ASN1\Type\Constructed\Set |
|
| 316 | - */ |
|
| 317 | - public function asSet(): Constructed\Set |
|
| 318 | - { |
|
| 319 | - if (!$this->_element instanceof Constructed\Set) { |
|
| 320 | - throw new \UnexpectedValueException( |
|
| 321 | - $this->_generateExceptionMessage(Element::TYPE_SET)); |
|
| 322 | - } |
|
| 323 | - return $this->_element; |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - /** |
|
| 327 | - * Get the wrapped element as a numeric string type. |
|
| 328 | - * |
|
| 329 | - * @throws \UnexpectedValueException If the element is not a numeric string |
|
| 330 | - * |
|
| 331 | - * @return \Sop\ASN1\Type\Primitive\NumericString |
|
| 332 | - */ |
|
| 333 | - public function asNumericString(): Primitive\NumericString |
|
| 334 | - { |
|
| 335 | - if (!$this->_element instanceof Primitive\NumericString) { |
|
| 336 | - throw new \UnexpectedValueException( |
|
| 337 | - $this->_generateExceptionMessage(Element::TYPE_NUMERIC_STRING)); |
|
| 338 | - } |
|
| 339 | - return $this->_element; |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * Get the wrapped element as a printable string type. |
|
| 344 | - * |
|
| 345 | - * @throws \UnexpectedValueException If the element is not a printable |
|
| 346 | - * string |
|
| 347 | - * |
|
| 348 | - * @return \Sop\ASN1\Type\Primitive\PrintableString |
|
| 349 | - */ |
|
| 350 | - public function asPrintableString(): Primitive\PrintableString |
|
| 351 | - { |
|
| 352 | - if (!$this->_element instanceof Primitive\PrintableString) { |
|
| 353 | - throw new \UnexpectedValueException( |
|
| 354 | - $this->_generateExceptionMessage(Element::TYPE_PRINTABLE_STRING)); |
|
| 355 | - } |
|
| 356 | - return $this->_element; |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * Get the wrapped element as a T61 string type. |
|
| 361 | - * |
|
| 362 | - * @throws \UnexpectedValueException If the element is not a T61 string |
|
| 363 | - * |
|
| 364 | - * @return \Sop\ASN1\Type\Primitive\T61String |
|
| 365 | - */ |
|
| 366 | - public function asT61String(): Primitive\T61String |
|
| 367 | - { |
|
| 368 | - if (!$this->_element instanceof Primitive\T61String) { |
|
| 369 | - throw new \UnexpectedValueException( |
|
| 370 | - $this->_generateExceptionMessage(Element::TYPE_T61_STRING)); |
|
| 371 | - } |
|
| 372 | - return $this->_element; |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * Get the wrapped element as a videotex string type. |
|
| 377 | - * |
|
| 378 | - * @throws \UnexpectedValueException If the element is not a videotex string |
|
| 379 | - * |
|
| 380 | - * @return \Sop\ASN1\Type\Primitive\VideotexString |
|
| 381 | - */ |
|
| 382 | - public function asVideotexString(): Primitive\VideotexString |
|
| 383 | - { |
|
| 384 | - if (!$this->_element instanceof Primitive\VideotexString) { |
|
| 385 | - throw new \UnexpectedValueException( |
|
| 386 | - $this->_generateExceptionMessage(Element::TYPE_VIDEOTEX_STRING)); |
|
| 387 | - } |
|
| 388 | - return $this->_element; |
|
| 389 | - } |
|
| 390 | - |
|
| 391 | - /** |
|
| 392 | - * Get the wrapped element as a IA5 string type. |
|
| 393 | - * |
|
| 394 | - * @throws \UnexpectedValueException If the element is not a IA5 string |
|
| 395 | - * |
|
| 396 | - * @return \Sop\ASN1\Type\Primitive\IA5String |
|
| 397 | - */ |
|
| 398 | - public function asIA5String(): Primitive\IA5String |
|
| 399 | - { |
|
| 400 | - if (!$this->_element instanceof Primitive\IA5String) { |
|
| 401 | - throw new \UnexpectedValueException( |
|
| 402 | - $this->_generateExceptionMessage(Element::TYPE_IA5_STRING)); |
|
| 403 | - } |
|
| 404 | - return $this->_element; |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - /** |
|
| 408 | - * Get the wrapped element as an UTC time type. |
|
| 409 | - * |
|
| 410 | - * @throws \UnexpectedValueException If the element is not a UTC time |
|
| 411 | - * |
|
| 412 | - * @return \Sop\ASN1\Type\Primitive\UTCTime |
|
| 413 | - */ |
|
| 414 | - public function asUTCTime(): Primitive\UTCTime |
|
| 415 | - { |
|
| 416 | - if (!$this->_element instanceof Primitive\UTCTime) { |
|
| 417 | - throw new \UnexpectedValueException( |
|
| 418 | - $this->_generateExceptionMessage(Element::TYPE_UTC_TIME)); |
|
| 419 | - } |
|
| 420 | - return $this->_element; |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - /** |
|
| 424 | - * Get the wrapped element as a generalized time type. |
|
| 425 | - * |
|
| 426 | - * @throws \UnexpectedValueException If the element is not a generalized |
|
| 427 | - * time |
|
| 428 | - * |
|
| 429 | - * @return \Sop\ASN1\Type\Primitive\GeneralizedTime |
|
| 430 | - */ |
|
| 431 | - public function asGeneralizedTime(): Primitive\GeneralizedTime |
|
| 432 | - { |
|
| 433 | - if (!$this->_element instanceof Primitive\GeneralizedTime) { |
|
| 434 | - throw new \UnexpectedValueException( |
|
| 435 | - $this->_generateExceptionMessage(Element::TYPE_GENERALIZED_TIME)); |
|
| 436 | - } |
|
| 437 | - return $this->_element; |
|
| 438 | - } |
|
| 439 | - |
|
| 440 | - /** |
|
| 441 | - * Get the wrapped element as a graphic string type. |
|
| 442 | - * |
|
| 443 | - * @throws \UnexpectedValueException If the element is not a graphic string |
|
| 444 | - * |
|
| 445 | - * @return \Sop\ASN1\Type\Primitive\GraphicString |
|
| 446 | - */ |
|
| 447 | - public function asGraphicString(): Primitive\GraphicString |
|
| 448 | - { |
|
| 449 | - if (!$this->_element instanceof Primitive\GraphicString) { |
|
| 450 | - throw new \UnexpectedValueException( |
|
| 451 | - $this->_generateExceptionMessage(Element::TYPE_GRAPHIC_STRING)); |
|
| 452 | - } |
|
| 453 | - return $this->_element; |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - /** |
|
| 457 | - * Get the wrapped element as a visible string type. |
|
| 458 | - * |
|
| 459 | - * @throws \UnexpectedValueException If the element is not a visible string |
|
| 460 | - * |
|
| 461 | - * @return \Sop\ASN1\Type\Primitive\VisibleString |
|
| 462 | - */ |
|
| 463 | - public function asVisibleString(): Primitive\VisibleString |
|
| 464 | - { |
|
| 465 | - if (!$this->_element instanceof Primitive\VisibleString) { |
|
| 466 | - throw new \UnexpectedValueException( |
|
| 467 | - $this->_generateExceptionMessage(Element::TYPE_VISIBLE_STRING)); |
|
| 468 | - } |
|
| 469 | - return $this->_element; |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - /** |
|
| 473 | - * Get the wrapped element as a general string type. |
|
| 474 | - * |
|
| 475 | - * @throws \UnexpectedValueException If the element is not general string |
|
| 476 | - * |
|
| 477 | - * @return \Sop\ASN1\Type\Primitive\GeneralString |
|
| 478 | - */ |
|
| 479 | - public function asGeneralString(): Primitive\GeneralString |
|
| 480 | - { |
|
| 481 | - if (!$this->_element instanceof Primitive\GeneralString) { |
|
| 482 | - throw new \UnexpectedValueException( |
|
| 483 | - $this->_generateExceptionMessage(Element::TYPE_GENERAL_STRING)); |
|
| 484 | - } |
|
| 485 | - return $this->_element; |
|
| 486 | - } |
|
| 487 | - |
|
| 488 | - /** |
|
| 489 | - * Get the wrapped element as a universal string type. |
|
| 490 | - * |
|
| 491 | - * @throws \UnexpectedValueException If the element is not a universal |
|
| 492 | - * string |
|
| 493 | - * |
|
| 494 | - * @return \Sop\ASN1\Type\Primitive\UniversalString |
|
| 495 | - */ |
|
| 496 | - public function asUniversalString(): Primitive\UniversalString |
|
| 497 | - { |
|
| 498 | - if (!$this->_element instanceof Primitive\UniversalString) { |
|
| 499 | - throw new \UnexpectedValueException( |
|
| 500 | - $this->_generateExceptionMessage(Element::TYPE_UNIVERSAL_STRING)); |
|
| 501 | - } |
|
| 502 | - return $this->_element; |
|
| 503 | - } |
|
| 504 | - |
|
| 505 | - /** |
|
| 506 | - * Get the wrapped element as a character string type. |
|
| 507 | - * |
|
| 508 | - * @throws \UnexpectedValueException If the element is not a character |
|
| 509 | - * string |
|
| 510 | - * |
|
| 511 | - * @return \Sop\ASN1\Type\Primitive\CharacterString |
|
| 512 | - */ |
|
| 513 | - public function asCharacterString(): Primitive\CharacterString |
|
| 514 | - { |
|
| 515 | - if (!$this->_element instanceof Primitive\CharacterString) { |
|
| 516 | - throw new \UnexpectedValueException( |
|
| 517 | - $this->_generateExceptionMessage(Element::TYPE_CHARACTER_STRING)); |
|
| 518 | - } |
|
| 519 | - return $this->_element; |
|
| 520 | - } |
|
| 521 | - |
|
| 522 | - /** |
|
| 523 | - * Get the wrapped element as a BMP string type. |
|
| 524 | - * |
|
| 525 | - * @throws \UnexpectedValueException If the element is not a bmp string |
|
| 526 | - * |
|
| 527 | - * @return \Sop\ASN1\Type\Primitive\BMPString |
|
| 528 | - */ |
|
| 529 | - public function asBMPString(): Primitive\BMPString |
|
| 530 | - { |
|
| 531 | - if (!$this->_element instanceof Primitive\BMPString) { |
|
| 532 | - throw new \UnexpectedValueException( |
|
| 533 | - $this->_generateExceptionMessage(Element::TYPE_BMP_STRING)); |
|
| 534 | - } |
|
| 535 | - return $this->_element; |
|
| 536 | - } |
|
| 537 | - |
|
| 538 | - /** |
|
| 539 | - * Get the wrapped element as any string type. |
|
| 540 | - * |
|
| 541 | - * @throws \UnexpectedValueException If the element is not a string |
|
| 542 | - * |
|
| 543 | - * @return StringType |
|
| 544 | - */ |
|
| 545 | - public function asString(): StringType |
|
| 546 | - { |
|
| 547 | - if (!$this->_element instanceof StringType) { |
|
| 548 | - throw new \UnexpectedValueException( |
|
| 549 | - $this->_generateExceptionMessage(Element::TYPE_STRING)); |
|
| 550 | - } |
|
| 551 | - return $this->_element; |
|
| 552 | - } |
|
| 553 | - |
|
| 554 | - /** |
|
| 555 | - * Get the wrapped element as any time type. |
|
| 556 | - * |
|
| 557 | - * @throws \UnexpectedValueException If the element is not a time |
|
| 558 | - * |
|
| 559 | - * @return TimeType |
|
| 560 | - */ |
|
| 561 | - public function asTime(): TimeType |
|
| 562 | - { |
|
| 563 | - if (!$this->_element instanceof TimeType) { |
|
| 564 | - throw new \UnexpectedValueException( |
|
| 565 | - $this->_generateExceptionMessage(Element::TYPE_TIME)); |
|
| 566 | - } |
|
| 567 | - return $this->_element; |
|
| 568 | - } |
|
| 569 | - |
|
| 570 | - /** |
|
| 571 | - * {@inheritdoc} |
|
| 572 | - */ |
|
| 573 | - public function toDER(): string |
|
| 574 | - { |
|
| 575 | - return $this->_element->toDER(); |
|
| 576 | - } |
|
| 577 | - |
|
| 578 | - /** |
|
| 579 | - * {@inheritdoc} |
|
| 580 | - */ |
|
| 581 | - public function typeClass(): int |
|
| 582 | - { |
|
| 583 | - return $this->_element->typeClass(); |
|
| 584 | - } |
|
| 585 | - |
|
| 586 | - /** |
|
| 587 | - * {@inheritdoc} |
|
| 588 | - */ |
|
| 589 | - public function isConstructed(): bool |
|
| 590 | - { |
|
| 591 | - return $this->_element->isConstructed(); |
|
| 592 | - } |
|
| 593 | - |
|
| 594 | - /** |
|
| 595 | - * {@inheritdoc} |
|
| 596 | - */ |
|
| 597 | - public function tag(): int |
|
| 598 | - { |
|
| 599 | - return $this->_element->tag(); |
|
| 600 | - } |
|
| 601 | - |
|
| 602 | - /** |
|
| 603 | - * {@inheritdoc} |
|
| 604 | - */ |
|
| 605 | - public function isType(int $tag): bool |
|
| 606 | - { |
|
| 607 | - return $this->_element->isType($tag); |
|
| 608 | - } |
|
| 609 | - |
|
| 610 | - /** |
|
| 611 | - * {@inheritdoc} |
|
| 612 | - * |
|
| 613 | - * Consider using any of the <code>as*</code> accessor methods instead. |
|
| 614 | - */ |
|
| 615 | - public function expectType(int $tag): ElementBase |
|
| 616 | - { |
|
| 617 | - return $this->_element->expectType($tag); |
|
| 618 | - } |
|
| 619 | - |
|
| 620 | - /** |
|
| 621 | - * {@inheritdoc} |
|
| 622 | - */ |
|
| 623 | - public function isTagged(): bool |
|
| 624 | - { |
|
| 625 | - return $this->_element->isTagged(); |
|
| 626 | - } |
|
| 627 | - |
|
| 628 | - /** |
|
| 629 | - * {@inheritdoc} |
|
| 630 | - * |
|
| 631 | - * Consider using <code>asTagged()</code> method instead and chaining |
|
| 632 | - * with <code>TaggedType::asExplicit()</code> or |
|
| 633 | - * <code>TaggedType::asImplicit()</code>. |
|
| 634 | - */ |
|
| 635 | - public function expectTagged(?int $tag = null): TaggedType |
|
| 636 | - { |
|
| 637 | - return $this->_element->expectTagged($tag); |
|
| 638 | - } |
|
| 639 | - |
|
| 640 | - /** |
|
| 641 | - * {@inheritdoc} |
|
| 642 | - */ |
|
| 643 | - public function asElement(): Element |
|
| 644 | - { |
|
| 645 | - return $this->_element; |
|
| 646 | - } |
|
| 647 | - |
|
| 648 | - /** |
|
| 649 | - * {@inheritdoc} |
|
| 650 | - */ |
|
| 651 | - public function asUnspecified(): UnspecifiedType |
|
| 652 | - { |
|
| 653 | - return $this; |
|
| 654 | - } |
|
| 655 | - |
|
| 656 | - /** |
|
| 657 | - * Generate message for exceptions thrown by <code>as*</code> methods. |
|
| 658 | - * |
|
| 659 | - * @param int $tag Type tag of the expected element |
|
| 660 | - * |
|
| 661 | - * @return string |
|
| 662 | - */ |
|
| 663 | - private function _generateExceptionMessage(int $tag): string |
|
| 664 | - { |
|
| 665 | - return sprintf('%s expected, got %s.', Element::tagToName($tag), |
|
| 666 | - $this->_typeDescriptorString()); |
|
| 667 | - } |
|
| 668 | - |
|
| 669 | - /** |
|
| 670 | - * Get textual description of the wrapped element for debugging purposes. |
|
| 671 | - * |
|
| 672 | - * @return string |
|
| 673 | - */ |
|
| 674 | - private function _typeDescriptorString(): string |
|
| 675 | - { |
|
| 676 | - $type_cls = $this->_element->typeClass(); |
|
| 677 | - $tag = $this->_element->tag(); |
|
| 678 | - if (Identifier::CLASS_UNIVERSAL == $type_cls) { |
|
| 679 | - return Element::tagToName($tag); |
|
| 680 | - } |
|
| 681 | - return Identifier::classToName($type_cls) . " TAG {$tag}"; |
|
| 682 | - } |
|
| 20 | + /** |
|
| 21 | + * The wrapped element. |
|
| 22 | + * |
|
| 23 | + * @var Element |
|
| 24 | + */ |
|
| 25 | + private $_element; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Constructor. |
|
| 29 | + * |
|
| 30 | + * @param Element $el |
|
| 31 | + */ |
|
| 32 | + public function __construct(Element $el) |
|
| 33 | + { |
|
| 34 | + $this->_element = $el; |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Initialize from DER data. |
|
| 39 | + * |
|
| 40 | + * @param string $data DER encoded data |
|
| 41 | + * |
|
| 42 | + * @return self |
|
| 43 | + */ |
|
| 44 | + public static function fromDER(string $data): self |
|
| 45 | + { |
|
| 46 | + return Element::fromDER($data)->asUnspecified(); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Initialize from ElementBase interface. |
|
| 51 | + * |
|
| 52 | + * @param ElementBase $el |
|
| 53 | + * |
|
| 54 | + * @return self |
|
| 55 | + */ |
|
| 56 | + public static function fromElementBase(ElementBase $el): self |
|
| 57 | + { |
|
| 58 | + // if element is already wrapped |
|
| 59 | + if ($el instanceof self) { |
|
| 60 | + return $el; |
|
| 61 | + } |
|
| 62 | + return new self($el->asElement()); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Get the wrapped element as a context specific tagged type. |
|
| 67 | + * |
|
| 68 | + * @throws \UnexpectedValueException If the element is not tagged |
|
| 69 | + * |
|
| 70 | + * @return TaggedType |
|
| 71 | + */ |
|
| 72 | + public function asTagged(): TaggedType |
|
| 73 | + { |
|
| 74 | + if (!$this->_element instanceof TaggedType) { |
|
| 75 | + throw new \UnexpectedValueException( |
|
| 76 | + 'Tagged element expected, got ' . $this->_typeDescriptorString()); |
|
| 77 | + } |
|
| 78 | + return $this->_element; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Get the wrapped element as an application specific type. |
|
| 83 | + * |
|
| 84 | + * @throws \UnexpectedValueException If element is not application specific |
|
| 85 | + * |
|
| 86 | + * @return \Sop\ASN1\Type\Tagged\ApplicationType |
|
| 87 | + */ |
|
| 88 | + public function asApplication(): Tagged\ApplicationType |
|
| 89 | + { |
|
| 90 | + if (!$this->_element instanceof Tagged\ApplicationType) { |
|
| 91 | + throw new \UnexpectedValueException( |
|
| 92 | + 'Application type expected, got ' . |
|
| 93 | + $this->_typeDescriptorString()); |
|
| 94 | + } |
|
| 95 | + return $this->_element; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Get the wrapped element as a private tagged type. |
|
| 100 | + * |
|
| 101 | + * @throws \UnexpectedValueException If element is not using private tagging |
|
| 102 | + * |
|
| 103 | + * @return \Sop\ASN1\Type\Tagged\PrivateType |
|
| 104 | + */ |
|
| 105 | + public function asPrivate(): Tagged\PrivateType |
|
| 106 | + { |
|
| 107 | + if (!$this->_element instanceof Tagged\PrivateType) { |
|
| 108 | + throw new \UnexpectedValueException( |
|
| 109 | + 'Private type expected, got ' . $this->_typeDescriptorString()); |
|
| 110 | + } |
|
| 111 | + return $this->_element; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Get the wrapped element as a boolean type. |
|
| 116 | + * |
|
| 117 | + * @throws \UnexpectedValueException If the element is not a boolean |
|
| 118 | + * |
|
| 119 | + * @return \Sop\ASN1\Type\Primitive\Boolean |
|
| 120 | + */ |
|
| 121 | + public function asBoolean(): Primitive\Boolean |
|
| 122 | + { |
|
| 123 | + if (!$this->_element instanceof Primitive\Boolean) { |
|
| 124 | + throw new \UnexpectedValueException( |
|
| 125 | + $this->_generateExceptionMessage(Element::TYPE_BOOLEAN)); |
|
| 126 | + } |
|
| 127 | + return $this->_element; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * Get the wrapped element as an integer type. |
|
| 132 | + * |
|
| 133 | + * @throws \UnexpectedValueException If the element is not an integer |
|
| 134 | + * |
|
| 135 | + * @return \Sop\ASN1\Type\Primitive\Integer |
|
| 136 | + */ |
|
| 137 | + public function asInteger(): Primitive\Integer |
|
| 138 | + { |
|
| 139 | + if (!$this->_element instanceof Primitive\Integer) { |
|
| 140 | + throw new \UnexpectedValueException( |
|
| 141 | + $this->_generateExceptionMessage(Element::TYPE_INTEGER)); |
|
| 142 | + } |
|
| 143 | + return $this->_element; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Get the wrapped element as a bit string type. |
|
| 148 | + * |
|
| 149 | + * @throws \UnexpectedValueException If the element is not a bit string |
|
| 150 | + * |
|
| 151 | + * @return \Sop\ASN1\Type\Primitive\BitString |
|
| 152 | + */ |
|
| 153 | + public function asBitString(): Primitive\BitString |
|
| 154 | + { |
|
| 155 | + if (!$this->_element instanceof Primitive\BitString) { |
|
| 156 | + throw new \UnexpectedValueException( |
|
| 157 | + $this->_generateExceptionMessage(Element::TYPE_BIT_STRING)); |
|
| 158 | + } |
|
| 159 | + return $this->_element; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Get the wrapped element as an octet string type. |
|
| 164 | + * |
|
| 165 | + * @throws \UnexpectedValueException If the element is not an octet string |
|
| 166 | + * |
|
| 167 | + * @return \Sop\ASN1\Type\Primitive\OctetString |
|
| 168 | + */ |
|
| 169 | + public function asOctetString(): Primitive\OctetString |
|
| 170 | + { |
|
| 171 | + if (!$this->_element instanceof Primitive\OctetString) { |
|
| 172 | + throw new \UnexpectedValueException( |
|
| 173 | + $this->_generateExceptionMessage(Element::TYPE_OCTET_STRING)); |
|
| 174 | + } |
|
| 175 | + return $this->_element; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Get the wrapped element as a null type. |
|
| 180 | + * |
|
| 181 | + * @throws \UnexpectedValueException If the element is not a null |
|
| 182 | + * |
|
| 183 | + * @return \Sop\ASN1\Type\Primitive\NullType |
|
| 184 | + */ |
|
| 185 | + public function asNull(): Primitive\NullType |
|
| 186 | + { |
|
| 187 | + if (!$this->_element instanceof Primitive\NullType) { |
|
| 188 | + throw new \UnexpectedValueException( |
|
| 189 | + $this->_generateExceptionMessage(Element::TYPE_NULL)); |
|
| 190 | + } |
|
| 191 | + return $this->_element; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Get the wrapped element as an object identifier type. |
|
| 196 | + * |
|
| 197 | + * @throws \UnexpectedValueException If the element is not an object |
|
| 198 | + * identifier |
|
| 199 | + * |
|
| 200 | + * @return \Sop\ASN1\Type\Primitive\ObjectIdentifier |
|
| 201 | + */ |
|
| 202 | + public function asObjectIdentifier(): Primitive\ObjectIdentifier |
|
| 203 | + { |
|
| 204 | + if (!$this->_element instanceof Primitive\ObjectIdentifier) { |
|
| 205 | + throw new \UnexpectedValueException( |
|
| 206 | + $this->_generateExceptionMessage( |
|
| 207 | + Element::TYPE_OBJECT_IDENTIFIER)); |
|
| 208 | + } |
|
| 209 | + return $this->_element; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * Get the wrapped element as an object descriptor type. |
|
| 214 | + * |
|
| 215 | + * @throws \UnexpectedValueException If the element is not an object |
|
| 216 | + * descriptor |
|
| 217 | + * |
|
| 218 | + * @return \Sop\ASN1\Type\Primitive\ObjectDescriptor |
|
| 219 | + */ |
|
| 220 | + public function asObjectDescriptor(): Primitive\ObjectDescriptor |
|
| 221 | + { |
|
| 222 | + if (!$this->_element instanceof Primitive\ObjectDescriptor) { |
|
| 223 | + throw new \UnexpectedValueException( |
|
| 224 | + $this->_generateExceptionMessage( |
|
| 225 | + Element::TYPE_OBJECT_DESCRIPTOR)); |
|
| 226 | + } |
|
| 227 | + return $this->_element; |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * Get the wrapped element as a real type. |
|
| 232 | + * |
|
| 233 | + * @throws \UnexpectedValueException If the element is not a real |
|
| 234 | + * |
|
| 235 | + * @return \Sop\ASN1\Type\Primitive\Real |
|
| 236 | + */ |
|
| 237 | + public function asReal(): Primitive\Real |
|
| 238 | + { |
|
| 239 | + if (!$this->_element instanceof Primitive\Real) { |
|
| 240 | + throw new \UnexpectedValueException( |
|
| 241 | + $this->_generateExceptionMessage(Element::TYPE_REAL)); |
|
| 242 | + } |
|
| 243 | + return $this->_element; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * Get the wrapped element as an enumerated type. |
|
| 248 | + * |
|
| 249 | + * @throws \UnexpectedValueException If the element is not an enumerated |
|
| 250 | + * |
|
| 251 | + * @return \Sop\ASN1\Type\Primitive\Enumerated |
|
| 252 | + */ |
|
| 253 | + public function asEnumerated(): Primitive\Enumerated |
|
| 254 | + { |
|
| 255 | + if (!$this->_element instanceof Primitive\Enumerated) { |
|
| 256 | + throw new \UnexpectedValueException( |
|
| 257 | + $this->_generateExceptionMessage(Element::TYPE_ENUMERATED)); |
|
| 258 | + } |
|
| 259 | + return $this->_element; |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * Get the wrapped element as a UTF8 string type. |
|
| 264 | + * |
|
| 265 | + * @throws \UnexpectedValueException If the element is not a UTF8 string |
|
| 266 | + * |
|
| 267 | + * @return \Sop\ASN1\Type\Primitive\UTF8String |
|
| 268 | + */ |
|
| 269 | + public function asUTF8String(): Primitive\UTF8String |
|
| 270 | + { |
|
| 271 | + if (!$this->_element instanceof Primitive\UTF8String) { |
|
| 272 | + throw new \UnexpectedValueException( |
|
| 273 | + $this->_generateExceptionMessage(Element::TYPE_UTF8_STRING)); |
|
| 274 | + } |
|
| 275 | + return $this->_element; |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + /** |
|
| 279 | + * Get the wrapped element as a relative OID type. |
|
| 280 | + * |
|
| 281 | + * @throws \UnexpectedValueException If the element is not a relative OID |
|
| 282 | + * |
|
| 283 | + * @return \Sop\ASN1\Type\Primitive\RelativeOID |
|
| 284 | + */ |
|
| 285 | + public function asRelativeOID(): Primitive\RelativeOID |
|
| 286 | + { |
|
| 287 | + if (!$this->_element instanceof Primitive\RelativeOID) { |
|
| 288 | + throw new \UnexpectedValueException( |
|
| 289 | + $this->_generateExceptionMessage(Element::TYPE_RELATIVE_OID)); |
|
| 290 | + } |
|
| 291 | + return $this->_element; |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + /** |
|
| 295 | + * Get the wrapped element as a sequence type. |
|
| 296 | + * |
|
| 297 | + * @throws \UnexpectedValueException If the element is not a sequence |
|
| 298 | + * |
|
| 299 | + * @return \Sop\ASN1\Type\Constructed\Sequence |
|
| 300 | + */ |
|
| 301 | + public function asSequence(): Constructed\Sequence |
|
| 302 | + { |
|
| 303 | + if (!$this->_element instanceof Constructed\Sequence) { |
|
| 304 | + throw new \UnexpectedValueException( |
|
| 305 | + $this->_generateExceptionMessage(Element::TYPE_SEQUENCE)); |
|
| 306 | + } |
|
| 307 | + return $this->_element; |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + /** |
|
| 311 | + * Get the wrapped element as a set type. |
|
| 312 | + * |
|
| 313 | + * @throws \UnexpectedValueException If the element is not a set |
|
| 314 | + * |
|
| 315 | + * @return \Sop\ASN1\Type\Constructed\Set |
|
| 316 | + */ |
|
| 317 | + public function asSet(): Constructed\Set |
|
| 318 | + { |
|
| 319 | + if (!$this->_element instanceof Constructed\Set) { |
|
| 320 | + throw new \UnexpectedValueException( |
|
| 321 | + $this->_generateExceptionMessage(Element::TYPE_SET)); |
|
| 322 | + } |
|
| 323 | + return $this->_element; |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + /** |
|
| 327 | + * Get the wrapped element as a numeric string type. |
|
| 328 | + * |
|
| 329 | + * @throws \UnexpectedValueException If the element is not a numeric string |
|
| 330 | + * |
|
| 331 | + * @return \Sop\ASN1\Type\Primitive\NumericString |
|
| 332 | + */ |
|
| 333 | + public function asNumericString(): Primitive\NumericString |
|
| 334 | + { |
|
| 335 | + if (!$this->_element instanceof Primitive\NumericString) { |
|
| 336 | + throw new \UnexpectedValueException( |
|
| 337 | + $this->_generateExceptionMessage(Element::TYPE_NUMERIC_STRING)); |
|
| 338 | + } |
|
| 339 | + return $this->_element; |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * Get the wrapped element as a printable string type. |
|
| 344 | + * |
|
| 345 | + * @throws \UnexpectedValueException If the element is not a printable |
|
| 346 | + * string |
|
| 347 | + * |
|
| 348 | + * @return \Sop\ASN1\Type\Primitive\PrintableString |
|
| 349 | + */ |
|
| 350 | + public function asPrintableString(): Primitive\PrintableString |
|
| 351 | + { |
|
| 352 | + if (!$this->_element instanceof Primitive\PrintableString) { |
|
| 353 | + throw new \UnexpectedValueException( |
|
| 354 | + $this->_generateExceptionMessage(Element::TYPE_PRINTABLE_STRING)); |
|
| 355 | + } |
|
| 356 | + return $this->_element; |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * Get the wrapped element as a T61 string type. |
|
| 361 | + * |
|
| 362 | + * @throws \UnexpectedValueException If the element is not a T61 string |
|
| 363 | + * |
|
| 364 | + * @return \Sop\ASN1\Type\Primitive\T61String |
|
| 365 | + */ |
|
| 366 | + public function asT61String(): Primitive\T61String |
|
| 367 | + { |
|
| 368 | + if (!$this->_element instanceof Primitive\T61String) { |
|
| 369 | + throw new \UnexpectedValueException( |
|
| 370 | + $this->_generateExceptionMessage(Element::TYPE_T61_STRING)); |
|
| 371 | + } |
|
| 372 | + return $this->_element; |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * Get the wrapped element as a videotex string type. |
|
| 377 | + * |
|
| 378 | + * @throws \UnexpectedValueException If the element is not a videotex string |
|
| 379 | + * |
|
| 380 | + * @return \Sop\ASN1\Type\Primitive\VideotexString |
|
| 381 | + */ |
|
| 382 | + public function asVideotexString(): Primitive\VideotexString |
|
| 383 | + { |
|
| 384 | + if (!$this->_element instanceof Primitive\VideotexString) { |
|
| 385 | + throw new \UnexpectedValueException( |
|
| 386 | + $this->_generateExceptionMessage(Element::TYPE_VIDEOTEX_STRING)); |
|
| 387 | + } |
|
| 388 | + return $this->_element; |
|
| 389 | + } |
|
| 390 | + |
|
| 391 | + /** |
|
| 392 | + * Get the wrapped element as a IA5 string type. |
|
| 393 | + * |
|
| 394 | + * @throws \UnexpectedValueException If the element is not a IA5 string |
|
| 395 | + * |
|
| 396 | + * @return \Sop\ASN1\Type\Primitive\IA5String |
|
| 397 | + */ |
|
| 398 | + public function asIA5String(): Primitive\IA5String |
|
| 399 | + { |
|
| 400 | + if (!$this->_element instanceof Primitive\IA5String) { |
|
| 401 | + throw new \UnexpectedValueException( |
|
| 402 | + $this->_generateExceptionMessage(Element::TYPE_IA5_STRING)); |
|
| 403 | + } |
|
| 404 | + return $this->_element; |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + /** |
|
| 408 | + * Get the wrapped element as an UTC time type. |
|
| 409 | + * |
|
| 410 | + * @throws \UnexpectedValueException If the element is not a UTC time |
|
| 411 | + * |
|
| 412 | + * @return \Sop\ASN1\Type\Primitive\UTCTime |
|
| 413 | + */ |
|
| 414 | + public function asUTCTime(): Primitive\UTCTime |
|
| 415 | + { |
|
| 416 | + if (!$this->_element instanceof Primitive\UTCTime) { |
|
| 417 | + throw new \UnexpectedValueException( |
|
| 418 | + $this->_generateExceptionMessage(Element::TYPE_UTC_TIME)); |
|
| 419 | + } |
|
| 420 | + return $this->_element; |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + /** |
|
| 424 | + * Get the wrapped element as a generalized time type. |
|
| 425 | + * |
|
| 426 | + * @throws \UnexpectedValueException If the element is not a generalized |
|
| 427 | + * time |
|
| 428 | + * |
|
| 429 | + * @return \Sop\ASN1\Type\Primitive\GeneralizedTime |
|
| 430 | + */ |
|
| 431 | + public function asGeneralizedTime(): Primitive\GeneralizedTime |
|
| 432 | + { |
|
| 433 | + if (!$this->_element instanceof Primitive\GeneralizedTime) { |
|
| 434 | + throw new \UnexpectedValueException( |
|
| 435 | + $this->_generateExceptionMessage(Element::TYPE_GENERALIZED_TIME)); |
|
| 436 | + } |
|
| 437 | + return $this->_element; |
|
| 438 | + } |
|
| 439 | + |
|
| 440 | + /** |
|
| 441 | + * Get the wrapped element as a graphic string type. |
|
| 442 | + * |
|
| 443 | + * @throws \UnexpectedValueException If the element is not a graphic string |
|
| 444 | + * |
|
| 445 | + * @return \Sop\ASN1\Type\Primitive\GraphicString |
|
| 446 | + */ |
|
| 447 | + public function asGraphicString(): Primitive\GraphicString |
|
| 448 | + { |
|
| 449 | + if (!$this->_element instanceof Primitive\GraphicString) { |
|
| 450 | + throw new \UnexpectedValueException( |
|
| 451 | + $this->_generateExceptionMessage(Element::TYPE_GRAPHIC_STRING)); |
|
| 452 | + } |
|
| 453 | + return $this->_element; |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + /** |
|
| 457 | + * Get the wrapped element as a visible string type. |
|
| 458 | + * |
|
| 459 | + * @throws \UnexpectedValueException If the element is not a visible string |
|
| 460 | + * |
|
| 461 | + * @return \Sop\ASN1\Type\Primitive\VisibleString |
|
| 462 | + */ |
|
| 463 | + public function asVisibleString(): Primitive\VisibleString |
|
| 464 | + { |
|
| 465 | + if (!$this->_element instanceof Primitive\VisibleString) { |
|
| 466 | + throw new \UnexpectedValueException( |
|
| 467 | + $this->_generateExceptionMessage(Element::TYPE_VISIBLE_STRING)); |
|
| 468 | + } |
|
| 469 | + return $this->_element; |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + /** |
|
| 473 | + * Get the wrapped element as a general string type. |
|
| 474 | + * |
|
| 475 | + * @throws \UnexpectedValueException If the element is not general string |
|
| 476 | + * |
|
| 477 | + * @return \Sop\ASN1\Type\Primitive\GeneralString |
|
| 478 | + */ |
|
| 479 | + public function asGeneralString(): Primitive\GeneralString |
|
| 480 | + { |
|
| 481 | + if (!$this->_element instanceof Primitive\GeneralString) { |
|
| 482 | + throw new \UnexpectedValueException( |
|
| 483 | + $this->_generateExceptionMessage(Element::TYPE_GENERAL_STRING)); |
|
| 484 | + } |
|
| 485 | + return $this->_element; |
|
| 486 | + } |
|
| 487 | + |
|
| 488 | + /** |
|
| 489 | + * Get the wrapped element as a universal string type. |
|
| 490 | + * |
|
| 491 | + * @throws \UnexpectedValueException If the element is not a universal |
|
| 492 | + * string |
|
| 493 | + * |
|
| 494 | + * @return \Sop\ASN1\Type\Primitive\UniversalString |
|
| 495 | + */ |
|
| 496 | + public function asUniversalString(): Primitive\UniversalString |
|
| 497 | + { |
|
| 498 | + if (!$this->_element instanceof Primitive\UniversalString) { |
|
| 499 | + throw new \UnexpectedValueException( |
|
| 500 | + $this->_generateExceptionMessage(Element::TYPE_UNIVERSAL_STRING)); |
|
| 501 | + } |
|
| 502 | + return $this->_element; |
|
| 503 | + } |
|
| 504 | + |
|
| 505 | + /** |
|
| 506 | + * Get the wrapped element as a character string type. |
|
| 507 | + * |
|
| 508 | + * @throws \UnexpectedValueException If the element is not a character |
|
| 509 | + * string |
|
| 510 | + * |
|
| 511 | + * @return \Sop\ASN1\Type\Primitive\CharacterString |
|
| 512 | + */ |
|
| 513 | + public function asCharacterString(): Primitive\CharacterString |
|
| 514 | + { |
|
| 515 | + if (!$this->_element instanceof Primitive\CharacterString) { |
|
| 516 | + throw new \UnexpectedValueException( |
|
| 517 | + $this->_generateExceptionMessage(Element::TYPE_CHARACTER_STRING)); |
|
| 518 | + } |
|
| 519 | + return $this->_element; |
|
| 520 | + } |
|
| 521 | + |
|
| 522 | + /** |
|
| 523 | + * Get the wrapped element as a BMP string type. |
|
| 524 | + * |
|
| 525 | + * @throws \UnexpectedValueException If the element is not a bmp string |
|
| 526 | + * |
|
| 527 | + * @return \Sop\ASN1\Type\Primitive\BMPString |
|
| 528 | + */ |
|
| 529 | + public function asBMPString(): Primitive\BMPString |
|
| 530 | + { |
|
| 531 | + if (!$this->_element instanceof Primitive\BMPString) { |
|
| 532 | + throw new \UnexpectedValueException( |
|
| 533 | + $this->_generateExceptionMessage(Element::TYPE_BMP_STRING)); |
|
| 534 | + } |
|
| 535 | + return $this->_element; |
|
| 536 | + } |
|
| 537 | + |
|
| 538 | + /** |
|
| 539 | + * Get the wrapped element as any string type. |
|
| 540 | + * |
|
| 541 | + * @throws \UnexpectedValueException If the element is not a string |
|
| 542 | + * |
|
| 543 | + * @return StringType |
|
| 544 | + */ |
|
| 545 | + public function asString(): StringType |
|
| 546 | + { |
|
| 547 | + if (!$this->_element instanceof StringType) { |
|
| 548 | + throw new \UnexpectedValueException( |
|
| 549 | + $this->_generateExceptionMessage(Element::TYPE_STRING)); |
|
| 550 | + } |
|
| 551 | + return $this->_element; |
|
| 552 | + } |
|
| 553 | + |
|
| 554 | + /** |
|
| 555 | + * Get the wrapped element as any time type. |
|
| 556 | + * |
|
| 557 | + * @throws \UnexpectedValueException If the element is not a time |
|
| 558 | + * |
|
| 559 | + * @return TimeType |
|
| 560 | + */ |
|
| 561 | + public function asTime(): TimeType |
|
| 562 | + { |
|
| 563 | + if (!$this->_element instanceof TimeType) { |
|
| 564 | + throw new \UnexpectedValueException( |
|
| 565 | + $this->_generateExceptionMessage(Element::TYPE_TIME)); |
|
| 566 | + } |
|
| 567 | + return $this->_element; |
|
| 568 | + } |
|
| 569 | + |
|
| 570 | + /** |
|
| 571 | + * {@inheritdoc} |
|
| 572 | + */ |
|
| 573 | + public function toDER(): string |
|
| 574 | + { |
|
| 575 | + return $this->_element->toDER(); |
|
| 576 | + } |
|
| 577 | + |
|
| 578 | + /** |
|
| 579 | + * {@inheritdoc} |
|
| 580 | + */ |
|
| 581 | + public function typeClass(): int |
|
| 582 | + { |
|
| 583 | + return $this->_element->typeClass(); |
|
| 584 | + } |
|
| 585 | + |
|
| 586 | + /** |
|
| 587 | + * {@inheritdoc} |
|
| 588 | + */ |
|
| 589 | + public function isConstructed(): bool |
|
| 590 | + { |
|
| 591 | + return $this->_element->isConstructed(); |
|
| 592 | + } |
|
| 593 | + |
|
| 594 | + /** |
|
| 595 | + * {@inheritdoc} |
|
| 596 | + */ |
|
| 597 | + public function tag(): int |
|
| 598 | + { |
|
| 599 | + return $this->_element->tag(); |
|
| 600 | + } |
|
| 601 | + |
|
| 602 | + /** |
|
| 603 | + * {@inheritdoc} |
|
| 604 | + */ |
|
| 605 | + public function isType(int $tag): bool |
|
| 606 | + { |
|
| 607 | + return $this->_element->isType($tag); |
|
| 608 | + } |
|
| 609 | + |
|
| 610 | + /** |
|
| 611 | + * {@inheritdoc} |
|
| 612 | + * |
|
| 613 | + * Consider using any of the <code>as*</code> accessor methods instead. |
|
| 614 | + */ |
|
| 615 | + public function expectType(int $tag): ElementBase |
|
| 616 | + { |
|
| 617 | + return $this->_element->expectType($tag); |
|
| 618 | + } |
|
| 619 | + |
|
| 620 | + /** |
|
| 621 | + * {@inheritdoc} |
|
| 622 | + */ |
|
| 623 | + public function isTagged(): bool |
|
| 624 | + { |
|
| 625 | + return $this->_element->isTagged(); |
|
| 626 | + } |
|
| 627 | + |
|
| 628 | + /** |
|
| 629 | + * {@inheritdoc} |
|
| 630 | + * |
|
| 631 | + * Consider using <code>asTagged()</code> method instead and chaining |
|
| 632 | + * with <code>TaggedType::asExplicit()</code> or |
|
| 633 | + * <code>TaggedType::asImplicit()</code>. |
|
| 634 | + */ |
|
| 635 | + public function expectTagged(?int $tag = null): TaggedType |
|
| 636 | + { |
|
| 637 | + return $this->_element->expectTagged($tag); |
|
| 638 | + } |
|
| 639 | + |
|
| 640 | + /** |
|
| 641 | + * {@inheritdoc} |
|
| 642 | + */ |
|
| 643 | + public function asElement(): Element |
|
| 644 | + { |
|
| 645 | + return $this->_element; |
|
| 646 | + } |
|
| 647 | + |
|
| 648 | + /** |
|
| 649 | + * {@inheritdoc} |
|
| 650 | + */ |
|
| 651 | + public function asUnspecified(): UnspecifiedType |
|
| 652 | + { |
|
| 653 | + return $this; |
|
| 654 | + } |
|
| 655 | + |
|
| 656 | + /** |
|
| 657 | + * Generate message for exceptions thrown by <code>as*</code> methods. |
|
| 658 | + * |
|
| 659 | + * @param int $tag Type tag of the expected element |
|
| 660 | + * |
|
| 661 | + * @return string |
|
| 662 | + */ |
|
| 663 | + private function _generateExceptionMessage(int $tag): string |
|
| 664 | + { |
|
| 665 | + return sprintf('%s expected, got %s.', Element::tagToName($tag), |
|
| 666 | + $this->_typeDescriptorString()); |
|
| 667 | + } |
|
| 668 | + |
|
| 669 | + /** |
|
| 670 | + * Get textual description of the wrapped element for debugging purposes. |
|
| 671 | + * |
|
| 672 | + * @return string |
|
| 673 | + */ |
|
| 674 | + private function _typeDescriptorString(): string |
|
| 675 | + { |
|
| 676 | + $type_cls = $this->_element->typeClass(); |
|
| 677 | + $tag = $this->_element->tag(); |
|
| 678 | + if (Identifier::CLASS_UNIVERSAL == $type_cls) { |
|
| 679 | + return Element::tagToName($tag); |
|
| 680 | + } |
|
| 681 | + return Identifier::classToName($type_cls) . " TAG {$tag}"; |
|
| 682 | + } |
|
| 683 | 683 | } |
@@ -12,56 +12,56 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class Set extends Structure |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * Constructor. |
|
| 17 | - * |
|
| 18 | - * @param Element ...$elements Any number of elements |
|
| 19 | - */ |
|
| 20 | - public function __construct(Element ...$elements) |
|
| 21 | - { |
|
| 22 | - $this->_typeTag = self::TYPE_SET; |
|
| 23 | - parent::__construct(...$elements); |
|
| 24 | - } |
|
| 15 | + /** |
|
| 16 | + * Constructor. |
|
| 17 | + * |
|
| 18 | + * @param Element ...$elements Any number of elements |
|
| 19 | + */ |
|
| 20 | + public function __construct(Element ...$elements) |
|
| 21 | + { |
|
| 22 | + $this->_typeTag = self::TYPE_SET; |
|
| 23 | + parent::__construct(...$elements); |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Sort by canonical ascending order. |
|
| 28 | - * |
|
| 29 | - * Used for DER encoding of SET type. |
|
| 30 | - * |
|
| 31 | - * @return self |
|
| 32 | - */ |
|
| 33 | - public function sortedSet(): self |
|
| 34 | - { |
|
| 35 | - $obj = clone $this; |
|
| 36 | - usort($obj->_elements, |
|
| 37 | - function (Element $a, Element $b) { |
|
| 38 | - if ($a->typeClass() != $b->typeClass()) { |
|
| 39 | - return $a->typeClass() < $b->typeClass() ? -1 : 1; |
|
| 40 | - } |
|
| 41 | - if ($a->tag() == $b->tag()) { |
|
| 42 | - return 0; |
|
| 43 | - } |
|
| 44 | - return $a->tag() < $b->tag() ? -1 : 1; |
|
| 45 | - }); |
|
| 46 | - return $obj; |
|
| 47 | - } |
|
| 26 | + /** |
|
| 27 | + * Sort by canonical ascending order. |
|
| 28 | + * |
|
| 29 | + * Used for DER encoding of SET type. |
|
| 30 | + * |
|
| 31 | + * @return self |
|
| 32 | + */ |
|
| 33 | + public function sortedSet(): self |
|
| 34 | + { |
|
| 35 | + $obj = clone $this; |
|
| 36 | + usort($obj->_elements, |
|
| 37 | + function (Element $a, Element $b) { |
|
| 38 | + if ($a->typeClass() != $b->typeClass()) { |
|
| 39 | + return $a->typeClass() < $b->typeClass() ? -1 : 1; |
|
| 40 | + } |
|
| 41 | + if ($a->tag() == $b->tag()) { |
|
| 42 | + return 0; |
|
| 43 | + } |
|
| 44 | + return $a->tag() < $b->tag() ? -1 : 1; |
|
| 45 | + }); |
|
| 46 | + return $obj; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Sort by encoding ascending order. |
|
| 51 | - * |
|
| 52 | - * Used for DER encoding of SET OF type. |
|
| 53 | - * |
|
| 54 | - * @return self |
|
| 55 | - */ |
|
| 56 | - public function sortedSetOf(): self |
|
| 57 | - { |
|
| 58 | - $obj = clone $this; |
|
| 59 | - usort($obj->_elements, |
|
| 60 | - function (Element $a, Element $b) { |
|
| 61 | - $a_der = $a->toDER(); |
|
| 62 | - $b_der = $b->toDER(); |
|
| 63 | - return strcmp($a_der, $b_der); |
|
| 64 | - }); |
|
| 65 | - return $obj; |
|
| 66 | - } |
|
| 49 | + /** |
|
| 50 | + * Sort by encoding ascending order. |
|
| 51 | + * |
|
| 52 | + * Used for DER encoding of SET OF type. |
|
| 53 | + * |
|
| 54 | + * @return self |
|
| 55 | + */ |
|
| 56 | + public function sortedSetOf(): self |
|
| 57 | + { |
|
| 58 | + $obj = clone $this; |
|
| 59 | + usort($obj->_elements, |
|
| 60 | + function (Element $a, Element $b) { |
|
| 61 | + $a_der = $a->toDER(); |
|
| 62 | + $b_der = $b->toDER(); |
|
| 63 | + return strcmp($a_der, $b_der); |
|
| 64 | + }); |
|
| 65 | + return $obj; |
|
| 66 | + } |
|
| 67 | 67 | } |
@@ -17,161 +17,161 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class Integer extends Element |
| 19 | 19 | { |
| 20 | - use UniversalClass; |
|
| 21 | - use PrimitiveType; |
|
| 20 | + use UniversalClass; |
|
| 21 | + use PrimitiveType; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * The number. |
|
| 25 | - * |
|
| 26 | - * @var BigInt |
|
| 27 | - */ |
|
| 28 | - private $_number; |
|
| 23 | + /** |
|
| 24 | + * The number. |
|
| 25 | + * |
|
| 26 | + * @var BigInt |
|
| 27 | + */ |
|
| 28 | + private $_number; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Constructor. |
|
| 32 | - * |
|
| 33 | - * @param int|string $number Base 10 integer |
|
| 34 | - */ |
|
| 35 | - public function __construct($number) |
|
| 36 | - { |
|
| 37 | - $this->_typeTag = self::TYPE_INTEGER; |
|
| 38 | - if (!self::_validateNumber($number)) { |
|
| 39 | - $var = is_scalar($number) ? strval($number) : gettype($number); |
|
| 40 | - throw new \InvalidArgumentException("'{$var}' is not a valid number."); |
|
| 41 | - } |
|
| 42 | - $this->_number = new BigInt($number); |
|
| 43 | - } |
|
| 30 | + /** |
|
| 31 | + * Constructor. |
|
| 32 | + * |
|
| 33 | + * @param int|string $number Base 10 integer |
|
| 34 | + */ |
|
| 35 | + public function __construct($number) |
|
| 36 | + { |
|
| 37 | + $this->_typeTag = self::TYPE_INTEGER; |
|
| 38 | + if (!self::_validateNumber($number)) { |
|
| 39 | + $var = is_scalar($number) ? strval($number) : gettype($number); |
|
| 40 | + throw new \InvalidArgumentException("'{$var}' is not a valid number."); |
|
| 41 | + } |
|
| 42 | + $this->_number = new BigInt($number); |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * Get the number as a base 10. |
|
| 47 | - * |
|
| 48 | - * @return string Integer as a string |
|
| 49 | - */ |
|
| 50 | - public function number(): string |
|
| 51 | - { |
|
| 52 | - return $this->_number->base10(); |
|
| 53 | - } |
|
| 45 | + /** |
|
| 46 | + * Get the number as a base 10. |
|
| 47 | + * |
|
| 48 | + * @return string Integer as a string |
|
| 49 | + */ |
|
| 50 | + public function number(): string |
|
| 51 | + { |
|
| 52 | + return $this->_number->base10(); |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Get the number as an integer type. |
|
| 57 | - * |
|
| 58 | - * @return int |
|
| 59 | - */ |
|
| 60 | - public function intNumber(): int |
|
| 61 | - { |
|
| 62 | - return $this->_number->intVal(); |
|
| 63 | - } |
|
| 55 | + /** |
|
| 56 | + * Get the number as an integer type. |
|
| 57 | + * |
|
| 58 | + * @return int |
|
| 59 | + */ |
|
| 60 | + public function intNumber(): int |
|
| 61 | + { |
|
| 62 | + return $this->_number->intVal(); |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * {@inheritdoc} |
|
| 67 | - */ |
|
| 68 | - protected function _encodedContentDER(): string |
|
| 69 | - { |
|
| 70 | - $num = $this->_number->gmpObj(); |
|
| 71 | - switch (gmp_sign($num)) { |
|
| 72 | - // positive |
|
| 73 | - case 1: |
|
| 74 | - return self::_encodePositiveInteger($num); |
|
| 75 | - // negative |
|
| 76 | - case -1: |
|
| 77 | - return self::_encodeNegativeInteger($num); |
|
| 78 | - } |
|
| 79 | - // zero |
|
| 80 | - return "\0"; |
|
| 81 | - } |
|
| 65 | + /** |
|
| 66 | + * {@inheritdoc} |
|
| 67 | + */ |
|
| 68 | + protected function _encodedContentDER(): string |
|
| 69 | + { |
|
| 70 | + $num = $this->_number->gmpObj(); |
|
| 71 | + switch (gmp_sign($num)) { |
|
| 72 | + // positive |
|
| 73 | + case 1: |
|
| 74 | + return self::_encodePositiveInteger($num); |
|
| 75 | + // negative |
|
| 76 | + case -1: |
|
| 77 | + return self::_encodeNegativeInteger($num); |
|
| 78 | + } |
|
| 79 | + // zero |
|
| 80 | + return "\0"; |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * {@inheritdoc} |
|
| 85 | - */ |
|
| 86 | - protected static function _decodeFromDER(Identifier $identifier, |
|
| 87 | - string $data, int &$offset): ElementBase |
|
| 88 | - { |
|
| 89 | - $idx = $offset; |
|
| 90 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 91 | - $bytes = substr($data, $idx, $length); |
|
| 92 | - $idx += $length; |
|
| 93 | - $neg = ord($bytes[0]) & 0x80; |
|
| 94 | - // negative, apply inversion of two's complement |
|
| 95 | - if ($neg) { |
|
| 96 | - $len = strlen($bytes); |
|
| 97 | - for ($i = 0; $i < $len; ++$i) { |
|
| 98 | - $bytes[$i] = ~$bytes[$i]; |
|
| 99 | - } |
|
| 100 | - } |
|
| 101 | - $num = gmp_init(bin2hex($bytes), 16); |
|
| 102 | - // negative, apply addition of two's complement |
|
| 103 | - // and produce negative result |
|
| 104 | - if ($neg) { |
|
| 105 | - $num = gmp_neg($num + 1); |
|
| 106 | - } |
|
| 107 | - $offset = $idx; |
|
| 108 | - // late static binding since enumerated extends integer type |
|
| 109 | - return new static(gmp_strval($num, 10)); |
|
| 110 | - } |
|
| 83 | + /** |
|
| 84 | + * {@inheritdoc} |
|
| 85 | + */ |
|
| 86 | + protected static function _decodeFromDER(Identifier $identifier, |
|
| 87 | + string $data, int &$offset): ElementBase |
|
| 88 | + { |
|
| 89 | + $idx = $offset; |
|
| 90 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 91 | + $bytes = substr($data, $idx, $length); |
|
| 92 | + $idx += $length; |
|
| 93 | + $neg = ord($bytes[0]) & 0x80; |
|
| 94 | + // negative, apply inversion of two's complement |
|
| 95 | + if ($neg) { |
|
| 96 | + $len = strlen($bytes); |
|
| 97 | + for ($i = 0; $i < $len; ++$i) { |
|
| 98 | + $bytes[$i] = ~$bytes[$i]; |
|
| 99 | + } |
|
| 100 | + } |
|
| 101 | + $num = gmp_init(bin2hex($bytes), 16); |
|
| 102 | + // negative, apply addition of two's complement |
|
| 103 | + // and produce negative result |
|
| 104 | + if ($neg) { |
|
| 105 | + $num = gmp_neg($num + 1); |
|
| 106 | + } |
|
| 107 | + $offset = $idx; |
|
| 108 | + // late static binding since enumerated extends integer type |
|
| 109 | + return new static(gmp_strval($num, 10)); |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * Encode positive integer to DER content. |
|
| 114 | - * |
|
| 115 | - * @param \GMP $num |
|
| 116 | - * |
|
| 117 | - * @return string |
|
| 118 | - */ |
|
| 119 | - private static function _encodePositiveInteger(\GMP $num): string |
|
| 120 | - { |
|
| 121 | - $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 122 | - // if first bit is 1, prepend full zero byte |
|
| 123 | - // to represent positive two's complement |
|
| 124 | - if (ord($bin[0]) & 0x80) { |
|
| 125 | - $bin = chr(0x00) . $bin; |
|
| 126 | - } |
|
| 127 | - return $bin; |
|
| 128 | - } |
|
| 112 | + /** |
|
| 113 | + * Encode positive integer to DER content. |
|
| 114 | + * |
|
| 115 | + * @param \GMP $num |
|
| 116 | + * |
|
| 117 | + * @return string |
|
| 118 | + */ |
|
| 119 | + private static function _encodePositiveInteger(\GMP $num): string |
|
| 120 | + { |
|
| 121 | + $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 122 | + // if first bit is 1, prepend full zero byte |
|
| 123 | + // to represent positive two's complement |
|
| 124 | + if (ord($bin[0]) & 0x80) { |
|
| 125 | + $bin = chr(0x00) . $bin; |
|
| 126 | + } |
|
| 127 | + return $bin; |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | - /** |
|
| 131 | - * Encode negative integer to DER content. |
|
| 132 | - * |
|
| 133 | - * @param \GMP $num |
|
| 134 | - * |
|
| 135 | - * @return string |
|
| 136 | - */ |
|
| 137 | - private static function _encodeNegativeInteger(\GMP $num): string |
|
| 138 | - { |
|
| 139 | - $num = gmp_abs($num); |
|
| 140 | - // compute number of bytes required |
|
| 141 | - $width = 1; |
|
| 142 | - if ($num > 128) { |
|
| 143 | - $tmp = $num; |
|
| 144 | - do { |
|
| 145 | - ++$width; |
|
| 146 | - $tmp >>= 8; |
|
| 147 | - } while ($tmp > 128); |
|
| 148 | - } |
|
| 149 | - // compute two's complement 2^n - x |
|
| 150 | - $num = gmp_pow('2', 8 * $width) - $num; |
|
| 151 | - $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 152 | - // if first bit is 0, prepend full inverted byte |
|
| 153 | - // to represent negative two's complement |
|
| 154 | - if (!(ord($bin[0]) & 0x80)) { |
|
| 155 | - $bin = chr(0xff) . $bin; |
|
| 156 | - } |
|
| 157 | - return $bin; |
|
| 158 | - } |
|
| 130 | + /** |
|
| 131 | + * Encode negative integer to DER content. |
|
| 132 | + * |
|
| 133 | + * @param \GMP $num |
|
| 134 | + * |
|
| 135 | + * @return string |
|
| 136 | + */ |
|
| 137 | + private static function _encodeNegativeInteger(\GMP $num): string |
|
| 138 | + { |
|
| 139 | + $num = gmp_abs($num); |
|
| 140 | + // compute number of bytes required |
|
| 141 | + $width = 1; |
|
| 142 | + if ($num > 128) { |
|
| 143 | + $tmp = $num; |
|
| 144 | + do { |
|
| 145 | + ++$width; |
|
| 146 | + $tmp >>= 8; |
|
| 147 | + } while ($tmp > 128); |
|
| 148 | + } |
|
| 149 | + // compute two's complement 2^n - x |
|
| 150 | + $num = gmp_pow('2', 8 * $width) - $num; |
|
| 151 | + $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 152 | + // if first bit is 0, prepend full inverted byte |
|
| 153 | + // to represent negative two's complement |
|
| 154 | + if (!(ord($bin[0]) & 0x80)) { |
|
| 155 | + $bin = chr(0xff) . $bin; |
|
| 156 | + } |
|
| 157 | + return $bin; |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | - /** |
|
| 161 | - * Test that number is valid for this context. |
|
| 162 | - * |
|
| 163 | - * @param mixed $num |
|
| 164 | - * |
|
| 165 | - * @return bool |
|
| 166 | - */ |
|
| 167 | - private static function _validateNumber($num): bool |
|
| 168 | - { |
|
| 169 | - if (is_int($num)) { |
|
| 170 | - return true; |
|
| 171 | - } |
|
| 172 | - if (is_string($num) && preg_match('/-?\d+/', $num)) { |
|
| 173 | - return true; |
|
| 174 | - } |
|
| 175 | - return false; |
|
| 176 | - } |
|
| 160 | + /** |
|
| 161 | + * Test that number is valid for this context. |
|
| 162 | + * |
|
| 163 | + * @param mixed $num |
|
| 164 | + * |
|
| 165 | + * @return bool |
|
| 166 | + */ |
|
| 167 | + private static function _validateNumber($num): bool |
|
| 168 | + { |
|
| 169 | + if (is_int($num)) { |
|
| 170 | + return true; |
|
| 171 | + } |
|
| 172 | + if (is_string($num) && preg_match('/-?\d+/', $num)) { |
|
| 173 | + return true; |
|
| 174 | + } |
|
| 175 | + return false; |
|
| 176 | + } |
|
| 177 | 177 | } |
@@ -17,276 +17,276 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class Real extends Element |
| 19 | 19 | { |
| 20 | - use UniversalClass; |
|
| 21 | - use PrimitiveType; |
|
| 20 | + use UniversalClass; |
|
| 21 | + use PrimitiveType; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Regex pattern to parse NR3 form number conforming to DER. |
|
| 25 | - * |
|
| 26 | - * @var string |
|
| 27 | - */ |
|
| 28 | - const NR3_REGEX = '/^(-?)(\d+)?\.E([+\-]?\d+)$/'; |
|
| 23 | + /** |
|
| 24 | + * Regex pattern to parse NR3 form number conforming to DER. |
|
| 25 | + * |
|
| 26 | + * @var string |
|
| 27 | + */ |
|
| 28 | + const NR3_REGEX = '/^(-?)(\d+)?\.E([+\-]?\d+)$/'; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Regex pattern to parse PHP exponent number format. |
|
| 32 | - * |
|
| 33 | - * @see http://php.net/manual/en/language.types.float.php |
|
| 34 | - * |
|
| 35 | - * @var string |
|
| 36 | - */ |
|
| 37 | - const PHP_EXPONENT_DNUM = '/^' . |
|
| 38 | - '([+\-]?' . // sign |
|
| 39 | - '(?:' . |
|
| 40 | - '\d+' . // LNUM |
|
| 41 | - '|' . |
|
| 42 | - '(?:\d*\.\d+|\d+\.\d*)' . // DNUM |
|
| 43 | - '))[eE]' . |
|
| 44 | - '([+\-]?\d+)' . // exponent |
|
| 45 | - '$/'; |
|
| 30 | + /** |
|
| 31 | + * Regex pattern to parse PHP exponent number format. |
|
| 32 | + * |
|
| 33 | + * @see http://php.net/manual/en/language.types.float.php |
|
| 34 | + * |
|
| 35 | + * @var string |
|
| 36 | + */ |
|
| 37 | + const PHP_EXPONENT_DNUM = '/^' . |
|
| 38 | + '([+\-]?' . // sign |
|
| 39 | + '(?:' . |
|
| 40 | + '\d+' . // LNUM |
|
| 41 | + '|' . |
|
| 42 | + '(?:\d*\.\d+|\d+\.\d*)' . // DNUM |
|
| 43 | + '))[eE]' . |
|
| 44 | + '([+\-]?\d+)' . // exponent |
|
| 45 | + '$/'; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Number zero represented in NR3 form. |
|
| 49 | - * |
|
| 50 | - * @var string |
|
| 51 | - */ |
|
| 52 | - const NR3_ZERO = '.E+0'; |
|
| 47 | + /** |
|
| 48 | + * Number zero represented in NR3 form. |
|
| 49 | + * |
|
| 50 | + * @var string |
|
| 51 | + */ |
|
| 52 | + const NR3_ZERO = '.E+0'; |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Number in NR3 form. |
|
| 56 | - * |
|
| 57 | - * @var string |
|
| 58 | - */ |
|
| 59 | - private $_number; |
|
| 54 | + /** |
|
| 55 | + * Number in NR3 form. |
|
| 56 | + * |
|
| 57 | + * @var string |
|
| 58 | + */ |
|
| 59 | + private $_number; |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Constructor. |
|
| 63 | - * |
|
| 64 | - * @param string $number number in NR3 form |
|
| 65 | - */ |
|
| 66 | - public function __construct(string $number) |
|
| 67 | - { |
|
| 68 | - $this->_typeTag = self::TYPE_REAL; |
|
| 69 | - if (!self::_validateNumber($number)) { |
|
| 70 | - throw new \InvalidArgumentException( |
|
| 71 | - "'{$number}' is not a valid NR3 form real."); |
|
| 72 | - } |
|
| 73 | - $this->_number = $number; |
|
| 74 | - } |
|
| 61 | + /** |
|
| 62 | + * Constructor. |
|
| 63 | + * |
|
| 64 | + * @param string $number number in NR3 form |
|
| 65 | + */ |
|
| 66 | + public function __construct(string $number) |
|
| 67 | + { |
|
| 68 | + $this->_typeTag = self::TYPE_REAL; |
|
| 69 | + if (!self::_validateNumber($number)) { |
|
| 70 | + throw new \InvalidArgumentException( |
|
| 71 | + "'{$number}' is not a valid NR3 form real."); |
|
| 72 | + } |
|
| 73 | + $this->_number = $number; |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * Initialize from float. |
|
| 78 | - * |
|
| 79 | - * @param float $number |
|
| 80 | - * |
|
| 81 | - * @return self |
|
| 82 | - */ |
|
| 83 | - public static function fromFloat(float $number): self |
|
| 84 | - { |
|
| 85 | - return new self(self::_decimalToNR3(strval($number))); |
|
| 86 | - } |
|
| 76 | + /** |
|
| 77 | + * Initialize from float. |
|
| 78 | + * |
|
| 79 | + * @param float $number |
|
| 80 | + * |
|
| 81 | + * @return self |
|
| 82 | + */ |
|
| 83 | + public static function fromFloat(float $number): self |
|
| 84 | + { |
|
| 85 | + return new self(self::_decimalToNR3(strval($number))); |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * Get number as a float. |
|
| 90 | - * |
|
| 91 | - * @return float |
|
| 92 | - */ |
|
| 93 | - public function float(): float |
|
| 94 | - { |
|
| 95 | - return self::_nr3ToDecimal($this->_number); |
|
| 96 | - } |
|
| 88 | + /** |
|
| 89 | + * Get number as a float. |
|
| 90 | + * |
|
| 91 | + * @return float |
|
| 92 | + */ |
|
| 93 | + public function float(): float |
|
| 94 | + { |
|
| 95 | + return self::_nr3ToDecimal($this->_number); |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - /** |
|
| 99 | - * {@inheritdoc} |
|
| 100 | - */ |
|
| 101 | - protected function _encodedContentDER(): string |
|
| 102 | - { |
|
| 103 | - /* if the real value is the value zero, there shall be no contents |
|
| 98 | + /** |
|
| 99 | + * {@inheritdoc} |
|
| 100 | + */ |
|
| 101 | + protected function _encodedContentDER(): string |
|
| 102 | + { |
|
| 103 | + /* if the real value is the value zero, there shall be no contents |
|
| 104 | 104 | octets in the encoding. (X.690 07-2002, section 8.5.2) */ |
| 105 | - if (self::NR3_ZERO == $this->_number) { |
|
| 106 | - return ''; |
|
| 107 | - } |
|
| 108 | - // encode in NR3 decimal encoding |
|
| 109 | - return chr(0x03) . $this->_number; |
|
| 110 | - } |
|
| 105 | + if (self::NR3_ZERO == $this->_number) { |
|
| 106 | + return ''; |
|
| 107 | + } |
|
| 108 | + // encode in NR3 decimal encoding |
|
| 109 | + return chr(0x03) . $this->_number; |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * {@inheritdoc} |
|
| 114 | - */ |
|
| 115 | - protected static function _decodeFromDER(Identifier $identifier, |
|
| 116 | - string $data, int &$offset): ElementBase |
|
| 117 | - { |
|
| 118 | - $idx = $offset; |
|
| 119 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 120 | - // if length is zero, value is zero (spec 8.5.2) |
|
| 121 | - if (!$length) { |
|
| 122 | - $obj = new self(self::NR3_ZERO); |
|
| 123 | - } else { |
|
| 124 | - $bytes = substr($data, $idx, $length); |
|
| 125 | - $byte = ord($bytes[0]); |
|
| 126 | - if (0x80 & $byte) { // bit 8 = 1 |
|
| 127 | - $obj = self::_decodeBinaryEncoding($bytes); |
|
| 128 | - } elseif (0x00 == $byte >> 6) { // bit 8 = 0, bit 7 = 0 |
|
| 129 | - $obj = self::_decodeDecimalEncoding($bytes); |
|
| 130 | - } else { // bit 8 = 0, bit 7 = 1 |
|
| 131 | - $obj = self::_decodeSpecialRealValue($bytes); |
|
| 132 | - } |
|
| 133 | - } |
|
| 134 | - $offset = $idx + $length; |
|
| 135 | - return $obj; |
|
| 136 | - } |
|
| 112 | + /** |
|
| 113 | + * {@inheritdoc} |
|
| 114 | + */ |
|
| 115 | + protected static function _decodeFromDER(Identifier $identifier, |
|
| 116 | + string $data, int &$offset): ElementBase |
|
| 117 | + { |
|
| 118 | + $idx = $offset; |
|
| 119 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 120 | + // if length is zero, value is zero (spec 8.5.2) |
|
| 121 | + if (!$length) { |
|
| 122 | + $obj = new self(self::NR3_ZERO); |
|
| 123 | + } else { |
|
| 124 | + $bytes = substr($data, $idx, $length); |
|
| 125 | + $byte = ord($bytes[0]); |
|
| 126 | + if (0x80 & $byte) { // bit 8 = 1 |
|
| 127 | + $obj = self::_decodeBinaryEncoding($bytes); |
|
| 128 | + } elseif (0x00 == $byte >> 6) { // bit 8 = 0, bit 7 = 0 |
|
| 129 | + $obj = self::_decodeDecimalEncoding($bytes); |
|
| 130 | + } else { // bit 8 = 0, bit 7 = 1 |
|
| 131 | + $obj = self::_decodeSpecialRealValue($bytes); |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | + $offset = $idx + $length; |
|
| 135 | + return $obj; |
|
| 136 | + } |
|
| 137 | 137 | |
| 138 | - /** |
|
| 139 | - * @todo Implement |
|
| 140 | - * |
|
| 141 | - * @param string $data |
|
| 142 | - */ |
|
| 143 | - protected static function _decodeBinaryEncoding(string $data) |
|
| 144 | - { |
|
| 145 | - throw new \RuntimeException( |
|
| 146 | - 'Binary encoding of REAL is not implemented.'); |
|
| 147 | - } |
|
| 138 | + /** |
|
| 139 | + * @todo Implement |
|
| 140 | + * |
|
| 141 | + * @param string $data |
|
| 142 | + */ |
|
| 143 | + protected static function _decodeBinaryEncoding(string $data) |
|
| 144 | + { |
|
| 145 | + throw new \RuntimeException( |
|
| 146 | + 'Binary encoding of REAL is not implemented.'); |
|
| 147 | + } |
|
| 148 | 148 | |
| 149 | - /** |
|
| 150 | - * @param string $data |
|
| 151 | - * |
|
| 152 | - * @throws \RuntimeException |
|
| 153 | - * |
|
| 154 | - * @return self |
|
| 155 | - */ |
|
| 156 | - protected static function _decodeDecimalEncoding(string $data): self |
|
| 157 | - { |
|
| 158 | - $nr = ord($data[0]) & 0x03; |
|
| 159 | - if (0x03 != $nr) { |
|
| 160 | - throw new \RuntimeException('Only NR3 form supported.'); |
|
| 161 | - } |
|
| 162 | - $str = substr($data, 1); |
|
| 163 | - return new self($str); |
|
| 164 | - } |
|
| 149 | + /** |
|
| 150 | + * @param string $data |
|
| 151 | + * |
|
| 152 | + * @throws \RuntimeException |
|
| 153 | + * |
|
| 154 | + * @return self |
|
| 155 | + */ |
|
| 156 | + protected static function _decodeDecimalEncoding(string $data): self |
|
| 157 | + { |
|
| 158 | + $nr = ord($data[0]) & 0x03; |
|
| 159 | + if (0x03 != $nr) { |
|
| 160 | + throw new \RuntimeException('Only NR3 form supported.'); |
|
| 161 | + } |
|
| 162 | + $str = substr($data, 1); |
|
| 163 | + return new self($str); |
|
| 164 | + } |
|
| 165 | 165 | |
| 166 | - /** |
|
| 167 | - * @todo Implement |
|
| 168 | - * |
|
| 169 | - * @param string $data |
|
| 170 | - */ |
|
| 171 | - protected static function _decodeSpecialRealValue(string $data) |
|
| 172 | - { |
|
| 173 | - if (1 != strlen($data)) { |
|
| 174 | - throw new DecodeException( |
|
| 175 | - 'SpecialRealValue must have one content octet.'); |
|
| 176 | - } |
|
| 177 | - $byte = ord($data[0]); |
|
| 178 | - if (0x40 == $byte) { // positive infinity |
|
| 179 | - throw new \RuntimeException('PLUS-INFINITY not supported.'); |
|
| 180 | - } |
|
| 181 | - if (0x41 == $byte) { // negative infinity |
|
| 182 | - throw new \RuntimeException('MINUS-INFINITY not supported.'); |
|
| 183 | - } |
|
| 184 | - throw new DecodeException('Invalid SpecialRealValue encoding.'); |
|
| 185 | - } |
|
| 166 | + /** |
|
| 167 | + * @todo Implement |
|
| 168 | + * |
|
| 169 | + * @param string $data |
|
| 170 | + */ |
|
| 171 | + protected static function _decodeSpecialRealValue(string $data) |
|
| 172 | + { |
|
| 173 | + if (1 != strlen($data)) { |
|
| 174 | + throw new DecodeException( |
|
| 175 | + 'SpecialRealValue must have one content octet.'); |
|
| 176 | + } |
|
| 177 | + $byte = ord($data[0]); |
|
| 178 | + if (0x40 == $byte) { // positive infinity |
|
| 179 | + throw new \RuntimeException('PLUS-INFINITY not supported.'); |
|
| 180 | + } |
|
| 181 | + if (0x41 == $byte) { // negative infinity |
|
| 182 | + throw new \RuntimeException('MINUS-INFINITY not supported.'); |
|
| 183 | + } |
|
| 184 | + throw new DecodeException('Invalid SpecialRealValue encoding.'); |
|
| 185 | + } |
|
| 186 | 186 | |
| 187 | - /** |
|
| 188 | - * Convert decimal number string to NR3 form. |
|
| 189 | - * |
|
| 190 | - * @param string $str |
|
| 191 | - * |
|
| 192 | - * @return string |
|
| 193 | - */ |
|
| 194 | - private static function _decimalToNR3(string $str): string |
|
| 195 | - { |
|
| 196 | - // if number is in exponent form |
|
| 197 | - /** @var string[] $match */ |
|
| 198 | - if (preg_match(self::PHP_EXPONENT_DNUM, $str, $match)) { |
|
| 199 | - $parts = explode('.', $match[1]); |
|
| 200 | - $m = ltrim($parts[0], '0'); |
|
| 201 | - $e = intval($match[2]); |
|
| 202 | - // if mantissa had decimals |
|
| 203 | - if (2 == count($parts)) { |
|
| 204 | - $d = rtrim($parts[1], '0'); |
|
| 205 | - $e -= strlen($d); |
|
| 206 | - $m .= $d; |
|
| 207 | - } |
|
| 208 | - } else { |
|
| 209 | - // explode from decimal |
|
| 210 | - $parts = explode('.', $str); |
|
| 211 | - $m = ltrim($parts[0], '0'); |
|
| 212 | - // if number had decimals |
|
| 213 | - if (2 == count($parts)) { |
|
| 214 | - // exponent is negative number of the decimals |
|
| 215 | - $e = -strlen($parts[1]); |
|
| 216 | - // append decimals to the mantissa |
|
| 217 | - $m .= $parts[1]; |
|
| 218 | - } else { |
|
| 219 | - $e = 0; |
|
| 220 | - } |
|
| 221 | - // shift trailing zeroes from the mantissa to the exponent |
|
| 222 | - while ('0' === substr($m, -1)) { |
|
| 223 | - ++$e; |
|
| 224 | - $m = substr($m, 0, -1); |
|
| 225 | - } |
|
| 226 | - } |
|
| 227 | - /* if exponent is zero, it must be prefixed with a "+" sign |
|
| 187 | + /** |
|
| 188 | + * Convert decimal number string to NR3 form. |
|
| 189 | + * |
|
| 190 | + * @param string $str |
|
| 191 | + * |
|
| 192 | + * @return string |
|
| 193 | + */ |
|
| 194 | + private static function _decimalToNR3(string $str): string |
|
| 195 | + { |
|
| 196 | + // if number is in exponent form |
|
| 197 | + /** @var string[] $match */ |
|
| 198 | + if (preg_match(self::PHP_EXPONENT_DNUM, $str, $match)) { |
|
| 199 | + $parts = explode('.', $match[1]); |
|
| 200 | + $m = ltrim($parts[0], '0'); |
|
| 201 | + $e = intval($match[2]); |
|
| 202 | + // if mantissa had decimals |
|
| 203 | + if (2 == count($parts)) { |
|
| 204 | + $d = rtrim($parts[1], '0'); |
|
| 205 | + $e -= strlen($d); |
|
| 206 | + $m .= $d; |
|
| 207 | + } |
|
| 208 | + } else { |
|
| 209 | + // explode from decimal |
|
| 210 | + $parts = explode('.', $str); |
|
| 211 | + $m = ltrim($parts[0], '0'); |
|
| 212 | + // if number had decimals |
|
| 213 | + if (2 == count($parts)) { |
|
| 214 | + // exponent is negative number of the decimals |
|
| 215 | + $e = -strlen($parts[1]); |
|
| 216 | + // append decimals to the mantissa |
|
| 217 | + $m .= $parts[1]; |
|
| 218 | + } else { |
|
| 219 | + $e = 0; |
|
| 220 | + } |
|
| 221 | + // shift trailing zeroes from the mantissa to the exponent |
|
| 222 | + while ('0' === substr($m, -1)) { |
|
| 223 | + ++$e; |
|
| 224 | + $m = substr($m, 0, -1); |
|
| 225 | + } |
|
| 226 | + } |
|
| 227 | + /* if exponent is zero, it must be prefixed with a "+" sign |
|
| 228 | 228 | (X.690 07-2002, section 11.3.2.6) */ |
| 229 | - if (0 == $e) { |
|
| 230 | - $es = '+'; |
|
| 231 | - } else { |
|
| 232 | - $es = $e < 0 ? '-' : ''; |
|
| 233 | - } |
|
| 234 | - return sprintf('%s.E%s%d', $m, $es, abs($e)); |
|
| 235 | - } |
|
| 229 | + if (0 == $e) { |
|
| 230 | + $es = '+'; |
|
| 231 | + } else { |
|
| 232 | + $es = $e < 0 ? '-' : ''; |
|
| 233 | + } |
|
| 234 | + return sprintf('%s.E%s%d', $m, $es, abs($e)); |
|
| 235 | + } |
|
| 236 | 236 | |
| 237 | - /** |
|
| 238 | - * Convert NR3 form number to decimal. |
|
| 239 | - * |
|
| 240 | - * @param string $str |
|
| 241 | - * |
|
| 242 | - * @throws \UnexpectedValueException |
|
| 243 | - * |
|
| 244 | - * @return float |
|
| 245 | - */ |
|
| 246 | - private static function _nr3ToDecimal(string $str): float |
|
| 247 | - { |
|
| 248 | - /** @var string[] $match */ |
|
| 249 | - if (!preg_match(self::NR3_REGEX, $str, $match)) { |
|
| 250 | - throw new \UnexpectedValueException( |
|
| 251 | - "'{$str}' is not a valid NR3 form real."); |
|
| 252 | - } |
|
| 253 | - $m = $match[2]; |
|
| 254 | - // if number started with minus sign |
|
| 255 | - $inv = '-' == $match[1]; |
|
| 256 | - $e = intval($match[3]); |
|
| 257 | - // positive exponent |
|
| 258 | - if ($e > 0) { |
|
| 259 | - // pad with trailing zeroes |
|
| 260 | - $num = $m . str_repeat('0', $e); |
|
| 261 | - } elseif ($e < 0) { |
|
| 262 | - // pad with leading zeroes |
|
| 263 | - if (strlen($m) < abs($e)) { |
|
| 264 | - $m = str_repeat('0', intval(abs($e)) - strlen($m)) . $m; |
|
| 265 | - } |
|
| 266 | - // insert decimal point |
|
| 267 | - $num = substr($m, 0, $e) . '.' . substr($m, $e); |
|
| 268 | - } else { |
|
| 269 | - $num = empty($m) ? '0' : $m; |
|
| 270 | - } |
|
| 271 | - // if number is negative |
|
| 272 | - if ($inv) { |
|
| 273 | - $num = "-{$num}"; |
|
| 274 | - } |
|
| 275 | - return floatval($num); |
|
| 276 | - } |
|
| 237 | + /** |
|
| 238 | + * Convert NR3 form number to decimal. |
|
| 239 | + * |
|
| 240 | + * @param string $str |
|
| 241 | + * |
|
| 242 | + * @throws \UnexpectedValueException |
|
| 243 | + * |
|
| 244 | + * @return float |
|
| 245 | + */ |
|
| 246 | + private static function _nr3ToDecimal(string $str): float |
|
| 247 | + { |
|
| 248 | + /** @var string[] $match */ |
|
| 249 | + if (!preg_match(self::NR3_REGEX, $str, $match)) { |
|
| 250 | + throw new \UnexpectedValueException( |
|
| 251 | + "'{$str}' is not a valid NR3 form real."); |
|
| 252 | + } |
|
| 253 | + $m = $match[2]; |
|
| 254 | + // if number started with minus sign |
|
| 255 | + $inv = '-' == $match[1]; |
|
| 256 | + $e = intval($match[3]); |
|
| 257 | + // positive exponent |
|
| 258 | + if ($e > 0) { |
|
| 259 | + // pad with trailing zeroes |
|
| 260 | + $num = $m . str_repeat('0', $e); |
|
| 261 | + } elseif ($e < 0) { |
|
| 262 | + // pad with leading zeroes |
|
| 263 | + if (strlen($m) < abs($e)) { |
|
| 264 | + $m = str_repeat('0', intval(abs($e)) - strlen($m)) . $m; |
|
| 265 | + } |
|
| 266 | + // insert decimal point |
|
| 267 | + $num = substr($m, 0, $e) . '.' . substr($m, $e); |
|
| 268 | + } else { |
|
| 269 | + $num = empty($m) ? '0' : $m; |
|
| 270 | + } |
|
| 271 | + // if number is negative |
|
| 272 | + if ($inv) { |
|
| 273 | + $num = "-{$num}"; |
|
| 274 | + } |
|
| 275 | + return floatval($num); |
|
| 276 | + } |
|
| 277 | 277 | |
| 278 | - /** |
|
| 279 | - * Test that number is valid for this context. |
|
| 280 | - * |
|
| 281 | - * @param mixed $num |
|
| 282 | - * |
|
| 283 | - * @return bool |
|
| 284 | - */ |
|
| 285 | - private static function _validateNumber($num): bool |
|
| 286 | - { |
|
| 287 | - if (!preg_match(self::NR3_REGEX, $num)) { |
|
| 288 | - return false; |
|
| 289 | - } |
|
| 290 | - return true; |
|
| 291 | - } |
|
| 278 | + /** |
|
| 279 | + * Test that number is valid for this context. |
|
| 280 | + * |
|
| 281 | + * @param mixed $num |
|
| 282 | + * |
|
| 283 | + * @return bool |
|
| 284 | + */ |
|
| 285 | + private static function _validateNumber($num): bool |
|
| 286 | + { |
|
| 287 | + if (!preg_match(self::NR3_REGEX, $num)) { |
|
| 288 | + return false; |
|
| 289 | + } |
|
| 290 | + return true; |
|
| 291 | + } |
|
| 292 | 292 | } |
@@ -17,199 +17,199 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class ObjectIdentifier extends Element |
| 19 | 19 | { |
| 20 | - use UniversalClass; |
|
| 21 | - use PrimitiveType; |
|
| 20 | + use UniversalClass; |
|
| 21 | + use PrimitiveType; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Object identifier in dotted format. |
|
| 25 | - * |
|
| 26 | - * @var string |
|
| 27 | - */ |
|
| 28 | - protected $_oid; |
|
| 23 | + /** |
|
| 24 | + * Object identifier in dotted format. |
|
| 25 | + * |
|
| 26 | + * @var string |
|
| 27 | + */ |
|
| 28 | + protected $_oid; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Object identifier split to sub ID's. |
|
| 32 | - * |
|
| 33 | - * @var \GMP[] |
|
| 34 | - */ |
|
| 35 | - protected $_subids; |
|
| 30 | + /** |
|
| 31 | + * Object identifier split to sub ID's. |
|
| 32 | + * |
|
| 33 | + * @var \GMP[] |
|
| 34 | + */ |
|
| 35 | + protected $_subids; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Constructor. |
|
| 39 | - * |
|
| 40 | - * @param string $oid OID in dotted format |
|
| 41 | - */ |
|
| 42 | - public function __construct(string $oid) |
|
| 43 | - { |
|
| 44 | - $this->_oid = $oid; |
|
| 45 | - $this->_subids = self::_explodeDottedOID($oid); |
|
| 46 | - // if OID is non-empty |
|
| 47 | - if (count($this->_subids) > 0) { |
|
| 48 | - // check that at least two nodes are set |
|
| 49 | - if (count($this->_subids) < 2) { |
|
| 50 | - throw new \UnexpectedValueException( |
|
| 51 | - 'OID must have at least two nodes.'); |
|
| 52 | - } |
|
| 53 | - // check that root arc is in 0..2 range |
|
| 54 | - if ($this->_subids[0] > 2) { |
|
| 55 | - throw new \UnexpectedValueException( |
|
| 56 | - 'Root arc must be in range of 0..2.'); |
|
| 57 | - } |
|
| 58 | - // if root arc is 0 or 1, second node must be in 0..39 range |
|
| 59 | - if ($this->_subids[0] < 2 && $this->_subids[1] >= 40) { |
|
| 60 | - throw new \UnexpectedValueException( |
|
| 61 | - 'Second node must be in 0..39 range for root arcs 0 and 1.'); |
|
| 62 | - } |
|
| 63 | - } |
|
| 64 | - $this->_typeTag = self::TYPE_OBJECT_IDENTIFIER; |
|
| 65 | - } |
|
| 37 | + /** |
|
| 38 | + * Constructor. |
|
| 39 | + * |
|
| 40 | + * @param string $oid OID in dotted format |
|
| 41 | + */ |
|
| 42 | + public function __construct(string $oid) |
|
| 43 | + { |
|
| 44 | + $this->_oid = $oid; |
|
| 45 | + $this->_subids = self::_explodeDottedOID($oid); |
|
| 46 | + // if OID is non-empty |
|
| 47 | + if (count($this->_subids) > 0) { |
|
| 48 | + // check that at least two nodes are set |
|
| 49 | + if (count($this->_subids) < 2) { |
|
| 50 | + throw new \UnexpectedValueException( |
|
| 51 | + 'OID must have at least two nodes.'); |
|
| 52 | + } |
|
| 53 | + // check that root arc is in 0..2 range |
|
| 54 | + if ($this->_subids[0] > 2) { |
|
| 55 | + throw new \UnexpectedValueException( |
|
| 56 | + 'Root arc must be in range of 0..2.'); |
|
| 57 | + } |
|
| 58 | + // if root arc is 0 or 1, second node must be in 0..39 range |
|
| 59 | + if ($this->_subids[0] < 2 && $this->_subids[1] >= 40) { |
|
| 60 | + throw new \UnexpectedValueException( |
|
| 61 | + 'Second node must be in 0..39 range for root arcs 0 and 1.'); |
|
| 62 | + } |
|
| 63 | + } |
|
| 64 | + $this->_typeTag = self::TYPE_OBJECT_IDENTIFIER; |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * Get OID in dotted format. |
|
| 69 | - * |
|
| 70 | - * @return string |
|
| 71 | - */ |
|
| 72 | - public function oid(): string |
|
| 73 | - { |
|
| 74 | - return $this->_oid; |
|
| 75 | - } |
|
| 67 | + /** |
|
| 68 | + * Get OID in dotted format. |
|
| 69 | + * |
|
| 70 | + * @return string |
|
| 71 | + */ |
|
| 72 | + public function oid(): string |
|
| 73 | + { |
|
| 74 | + return $this->_oid; |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * {@inheritdoc} |
|
| 79 | - */ |
|
| 80 | - protected function _encodedContentDER(): string |
|
| 81 | - { |
|
| 82 | - $subids = $this->_subids; |
|
| 83 | - // encode first two subids to one according to spec section 8.19.4 |
|
| 84 | - if (count($subids) >= 2) { |
|
| 85 | - $num = ($subids[0] * 40) + $subids[1]; |
|
| 86 | - array_splice($subids, 0, 2, [$num]); |
|
| 87 | - } |
|
| 88 | - return self::_encodeSubIDs(...$subids); |
|
| 89 | - } |
|
| 77 | + /** |
|
| 78 | + * {@inheritdoc} |
|
| 79 | + */ |
|
| 80 | + protected function _encodedContentDER(): string |
|
| 81 | + { |
|
| 82 | + $subids = $this->_subids; |
|
| 83 | + // encode first two subids to one according to spec section 8.19.4 |
|
| 84 | + if (count($subids) >= 2) { |
|
| 85 | + $num = ($subids[0] * 40) + $subids[1]; |
|
| 86 | + array_splice($subids, 0, 2, [$num]); |
|
| 87 | + } |
|
| 88 | + return self::_encodeSubIDs(...$subids); |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - /** |
|
| 92 | - * {@inheritdoc} |
|
| 93 | - */ |
|
| 94 | - protected static function _decodeFromDER(Identifier $identifier, |
|
| 95 | - string $data, int &$offset): ElementBase |
|
| 96 | - { |
|
| 97 | - $idx = $offset; |
|
| 98 | - $len = Length::expectFromDER($data, $idx)->intLength(); |
|
| 99 | - $subids = self::_decodeSubIDs(substr($data, $idx, $len)); |
|
| 100 | - $idx += $len; |
|
| 101 | - // decode first subidentifier according to spec section 8.19.4 |
|
| 102 | - if (isset($subids[0])) { |
|
| 103 | - if ($subids[0] < 80) { |
|
| 104 | - [$x, $y] = gmp_div_qr($subids[0], '40'); |
|
| 105 | - } else { |
|
| 106 | - $x = gmp_init(2, 10); |
|
| 107 | - $y = $subids[0] - 80; |
|
| 108 | - } |
|
| 109 | - array_splice($subids, 0, 1, [$x, $y]); |
|
| 110 | - } |
|
| 111 | - $offset = $idx; |
|
| 112 | - return new self(self::_implodeSubIDs(...$subids)); |
|
| 113 | - } |
|
| 91 | + /** |
|
| 92 | + * {@inheritdoc} |
|
| 93 | + */ |
|
| 94 | + protected static function _decodeFromDER(Identifier $identifier, |
|
| 95 | + string $data, int &$offset): ElementBase |
|
| 96 | + { |
|
| 97 | + $idx = $offset; |
|
| 98 | + $len = Length::expectFromDER($data, $idx)->intLength(); |
|
| 99 | + $subids = self::_decodeSubIDs(substr($data, $idx, $len)); |
|
| 100 | + $idx += $len; |
|
| 101 | + // decode first subidentifier according to spec section 8.19.4 |
|
| 102 | + if (isset($subids[0])) { |
|
| 103 | + if ($subids[0] < 80) { |
|
| 104 | + [$x, $y] = gmp_div_qr($subids[0], '40'); |
|
| 105 | + } else { |
|
| 106 | + $x = gmp_init(2, 10); |
|
| 107 | + $y = $subids[0] - 80; |
|
| 108 | + } |
|
| 109 | + array_splice($subids, 0, 1, [$x, $y]); |
|
| 110 | + } |
|
| 111 | + $offset = $idx; |
|
| 112 | + return new self(self::_implodeSubIDs(...$subids)); |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - /** |
|
| 116 | - * Explode dotted OID to an array of sub ID's. |
|
| 117 | - * |
|
| 118 | - * @param string $oid OID in dotted format |
|
| 119 | - * |
|
| 120 | - * @return \GMP[] Array of GMP numbers |
|
| 121 | - */ |
|
| 122 | - protected static function _explodeDottedOID(string $oid): array |
|
| 123 | - { |
|
| 124 | - $subids = []; |
|
| 125 | - if (strlen($oid)) { |
|
| 126 | - foreach (explode('.', $oid) as $subid) { |
|
| 127 | - $n = @gmp_init($subid, 10); |
|
| 128 | - if (false === $n) { |
|
| 129 | - throw new \UnexpectedValueException( |
|
| 130 | - "'{$subid}' is not a number."); |
|
| 131 | - } |
|
| 132 | - $subids[] = $n; |
|
| 133 | - } |
|
| 134 | - } |
|
| 135 | - return $subids; |
|
| 136 | - } |
|
| 115 | + /** |
|
| 116 | + * Explode dotted OID to an array of sub ID's. |
|
| 117 | + * |
|
| 118 | + * @param string $oid OID in dotted format |
|
| 119 | + * |
|
| 120 | + * @return \GMP[] Array of GMP numbers |
|
| 121 | + */ |
|
| 122 | + protected static function _explodeDottedOID(string $oid): array |
|
| 123 | + { |
|
| 124 | + $subids = []; |
|
| 125 | + if (strlen($oid)) { |
|
| 126 | + foreach (explode('.', $oid) as $subid) { |
|
| 127 | + $n = @gmp_init($subid, 10); |
|
| 128 | + if (false === $n) { |
|
| 129 | + throw new \UnexpectedValueException( |
|
| 130 | + "'{$subid}' is not a number."); |
|
| 131 | + } |
|
| 132 | + $subids[] = $n; |
|
| 133 | + } |
|
| 134 | + } |
|
| 135 | + return $subids; |
|
| 136 | + } |
|
| 137 | 137 | |
| 138 | - /** |
|
| 139 | - * Implode an array of sub IDs to dotted OID format. |
|
| 140 | - * |
|
| 141 | - * @param \GMP ...$subids |
|
| 142 | - * |
|
| 143 | - * @return string |
|
| 144 | - */ |
|
| 145 | - protected static function _implodeSubIDs(\GMP ...$subids): string |
|
| 146 | - { |
|
| 147 | - return implode('.', |
|
| 148 | - array_map(function ($num) { |
|
| 149 | - return gmp_strval($num, 10); |
|
| 150 | - }, $subids)); |
|
| 151 | - } |
|
| 138 | + /** |
|
| 139 | + * Implode an array of sub IDs to dotted OID format. |
|
| 140 | + * |
|
| 141 | + * @param \GMP ...$subids |
|
| 142 | + * |
|
| 143 | + * @return string |
|
| 144 | + */ |
|
| 145 | + protected static function _implodeSubIDs(\GMP ...$subids): string |
|
| 146 | + { |
|
| 147 | + return implode('.', |
|
| 148 | + array_map(function ($num) { |
|
| 149 | + return gmp_strval($num, 10); |
|
| 150 | + }, $subids)); |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - /** |
|
| 154 | - * Encode sub ID's to DER. |
|
| 155 | - * |
|
| 156 | - * @param \GMP ...$subids |
|
| 157 | - * |
|
| 158 | - * @return string |
|
| 159 | - */ |
|
| 160 | - protected static function _encodeSubIDs(\GMP ...$subids): string |
|
| 161 | - { |
|
| 162 | - $data = ''; |
|
| 163 | - foreach ($subids as $subid) { |
|
| 164 | - // if number fits to one base 128 byte |
|
| 165 | - if ($subid < 128) { |
|
| 166 | - $data .= chr(intval($subid)); |
|
| 167 | - } else { // encode to multiple bytes |
|
| 168 | - $bytes = []; |
|
| 169 | - do { |
|
| 170 | - array_unshift($bytes, 0x7f & gmp_intval($subid)); |
|
| 171 | - $subid >>= 7; |
|
| 172 | - } while ($subid > 0); |
|
| 173 | - // all bytes except last must have bit 8 set to one |
|
| 174 | - foreach (array_splice($bytes, 0, -1) as $byte) { |
|
| 175 | - $data .= chr(0x80 | $byte); |
|
| 176 | - } |
|
| 177 | - $data .= chr(reset($bytes)); |
|
| 178 | - } |
|
| 179 | - } |
|
| 180 | - return $data; |
|
| 181 | - } |
|
| 153 | + /** |
|
| 154 | + * Encode sub ID's to DER. |
|
| 155 | + * |
|
| 156 | + * @param \GMP ...$subids |
|
| 157 | + * |
|
| 158 | + * @return string |
|
| 159 | + */ |
|
| 160 | + protected static function _encodeSubIDs(\GMP ...$subids): string |
|
| 161 | + { |
|
| 162 | + $data = ''; |
|
| 163 | + foreach ($subids as $subid) { |
|
| 164 | + // if number fits to one base 128 byte |
|
| 165 | + if ($subid < 128) { |
|
| 166 | + $data .= chr(intval($subid)); |
|
| 167 | + } else { // encode to multiple bytes |
|
| 168 | + $bytes = []; |
|
| 169 | + do { |
|
| 170 | + array_unshift($bytes, 0x7f & gmp_intval($subid)); |
|
| 171 | + $subid >>= 7; |
|
| 172 | + } while ($subid > 0); |
|
| 173 | + // all bytes except last must have bit 8 set to one |
|
| 174 | + foreach (array_splice($bytes, 0, -1) as $byte) { |
|
| 175 | + $data .= chr(0x80 | $byte); |
|
| 176 | + } |
|
| 177 | + $data .= chr(reset($bytes)); |
|
| 178 | + } |
|
| 179 | + } |
|
| 180 | + return $data; |
|
| 181 | + } |
|
| 182 | 182 | |
| 183 | - /** |
|
| 184 | - * Decode sub ID's from DER data. |
|
| 185 | - * |
|
| 186 | - * @param string $data |
|
| 187 | - * |
|
| 188 | - * @throws DecodeException |
|
| 189 | - * |
|
| 190 | - * @return \GMP[] Array of GMP numbers |
|
| 191 | - */ |
|
| 192 | - protected static function _decodeSubIDs(string $data): array |
|
| 193 | - { |
|
| 194 | - $subids = []; |
|
| 195 | - $idx = 0; |
|
| 196 | - $end = strlen($data); |
|
| 197 | - while ($idx < $end) { |
|
| 198 | - $num = gmp_init('0', 10); |
|
| 199 | - while (true) { |
|
| 200 | - if ($idx >= $end) { |
|
| 201 | - throw new DecodeException('Unexpected end of data.'); |
|
| 202 | - } |
|
| 203 | - $byte = ord($data[$idx++]); |
|
| 204 | - $num |= $byte & 0x7f; |
|
| 205 | - // bit 8 of the last octet is zero |
|
| 206 | - if (!($byte & 0x80)) { |
|
| 207 | - break; |
|
| 208 | - } |
|
| 209 | - $num <<= 7; |
|
| 210 | - } |
|
| 211 | - $subids[] = $num; |
|
| 212 | - } |
|
| 213 | - return $subids; |
|
| 214 | - } |
|
| 183 | + /** |
|
| 184 | + * Decode sub ID's from DER data. |
|
| 185 | + * |
|
| 186 | + * @param string $data |
|
| 187 | + * |
|
| 188 | + * @throws DecodeException |
|
| 189 | + * |
|
| 190 | + * @return \GMP[] Array of GMP numbers |
|
| 191 | + */ |
|
| 192 | + protected static function _decodeSubIDs(string $data): array |
|
| 193 | + { |
|
| 194 | + $subids = []; |
|
| 195 | + $idx = 0; |
|
| 196 | + $end = strlen($data); |
|
| 197 | + while ($idx < $end) { |
|
| 198 | + $num = gmp_init('0', 10); |
|
| 199 | + while (true) { |
|
| 200 | + if ($idx >= $end) { |
|
| 201 | + throw new DecodeException('Unexpected end of data.'); |
|
| 202 | + } |
|
| 203 | + $byte = ord($data[$idx++]); |
|
| 204 | + $num |= $byte & 0x7f; |
|
| 205 | + // bit 8 of the last octet is zero |
|
| 206 | + if (!($byte & 0x80)) { |
|
| 207 | + break; |
|
| 208 | + } |
|
| 209 | + $num <<= 7; |
|
| 210 | + } |
|
| 211 | + $subids[] = $num; |
|
| 212 | + } |
|
| 213 | + return $subids; |
|
| 214 | + } |
|
| 215 | 215 | } |
@@ -17,110 +17,110 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class GeneralizedTime extends TimeType |
| 19 | 19 | { |
| 20 | - use UniversalClass; |
|
| 21 | - use PrimitiveType; |
|
| 20 | + use UniversalClass; |
|
| 21 | + use PrimitiveType; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Regular expression to parse date. |
|
| 25 | - * |
|
| 26 | - * DER restricts format to UTC timezone (Z suffix). |
|
| 27 | - * |
|
| 28 | - * @var string |
|
| 29 | - */ |
|
| 30 | - const REGEX = '#^' . |
|
| 31 | - '(\d\d\d\d)' . // YYYY |
|
| 32 | - '(\d\d)' . // MM |
|
| 33 | - '(\d\d)' . // DD |
|
| 34 | - '(\d\d)' . // hh |
|
| 35 | - '(\d\d)' . // mm |
|
| 36 | - '(\d\d)' . // ss |
|
| 37 | - '(?:\.(\d+))?' . // frac |
|
| 38 | - 'Z' . // TZ |
|
| 39 | - '$#'; |
|
| 23 | + /** |
|
| 24 | + * Regular expression to parse date. |
|
| 25 | + * |
|
| 26 | + * DER restricts format to UTC timezone (Z suffix). |
|
| 27 | + * |
|
| 28 | + * @var string |
|
| 29 | + */ |
|
| 30 | + const REGEX = '#^' . |
|
| 31 | + '(\d\d\d\d)' . // YYYY |
|
| 32 | + '(\d\d)' . // MM |
|
| 33 | + '(\d\d)' . // DD |
|
| 34 | + '(\d\d)' . // hh |
|
| 35 | + '(\d\d)' . // mm |
|
| 36 | + '(\d\d)' . // ss |
|
| 37 | + '(?:\.(\d+))?' . // frac |
|
| 38 | + 'Z' . // TZ |
|
| 39 | + '$#'; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Cached formatted date. |
|
| 43 | - * |
|
| 44 | - * @var null|string |
|
| 45 | - */ |
|
| 46 | - private $_formatted; |
|
| 41 | + /** |
|
| 42 | + * Cached formatted date. |
|
| 43 | + * |
|
| 44 | + * @var null|string |
|
| 45 | + */ |
|
| 46 | + private $_formatted; |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Constructor. |
|
| 50 | - * |
|
| 51 | - * @param \DateTimeImmutable $dt |
|
| 52 | - */ |
|
| 53 | - public function __construct(\DateTimeImmutable $dt) |
|
| 54 | - { |
|
| 55 | - $this->_typeTag = self::TYPE_GENERALIZED_TIME; |
|
| 56 | - parent::__construct($dt); |
|
| 57 | - } |
|
| 48 | + /** |
|
| 49 | + * Constructor. |
|
| 50 | + * |
|
| 51 | + * @param \DateTimeImmutable $dt |
|
| 52 | + */ |
|
| 53 | + public function __construct(\DateTimeImmutable $dt) |
|
| 54 | + { |
|
| 55 | + $this->_typeTag = self::TYPE_GENERALIZED_TIME; |
|
| 56 | + parent::__construct($dt); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Clear cached variables on clone. |
|
| 61 | - */ |
|
| 62 | - public function __clone() |
|
| 63 | - { |
|
| 64 | - $this->_formatted = null; |
|
| 65 | - } |
|
| 59 | + /** |
|
| 60 | + * Clear cached variables on clone. |
|
| 61 | + */ |
|
| 62 | + public function __clone() |
|
| 63 | + { |
|
| 64 | + $this->_formatted = null; |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * {@inheritdoc} |
|
| 69 | - */ |
|
| 70 | - protected function _encodedContentDER(): string |
|
| 71 | - { |
|
| 72 | - if (!isset($this->_formatted)) { |
|
| 73 | - $dt = $this->_dateTime->setTimezone( |
|
| 74 | - self::_createTimeZone(self::TZ_UTC)); |
|
| 75 | - $this->_formatted = $dt->format('YmdHis'); |
|
| 76 | - // if fractions were used |
|
| 77 | - $frac = $dt->format('u'); |
|
| 78 | - if (0 != $frac) { |
|
| 79 | - $frac = rtrim($frac, '0'); |
|
| 80 | - $this->_formatted .= ".{$frac}"; |
|
| 81 | - } |
|
| 82 | - // timezone |
|
| 83 | - $this->_formatted .= 'Z'; |
|
| 84 | - } |
|
| 85 | - return $this->_formatted; |
|
| 86 | - } |
|
| 67 | + /** |
|
| 68 | + * {@inheritdoc} |
|
| 69 | + */ |
|
| 70 | + protected function _encodedContentDER(): string |
|
| 71 | + { |
|
| 72 | + if (!isset($this->_formatted)) { |
|
| 73 | + $dt = $this->_dateTime->setTimezone( |
|
| 74 | + self::_createTimeZone(self::TZ_UTC)); |
|
| 75 | + $this->_formatted = $dt->format('YmdHis'); |
|
| 76 | + // if fractions were used |
|
| 77 | + $frac = $dt->format('u'); |
|
| 78 | + if (0 != $frac) { |
|
| 79 | + $frac = rtrim($frac, '0'); |
|
| 80 | + $this->_formatted .= ".{$frac}"; |
|
| 81 | + } |
|
| 82 | + // timezone |
|
| 83 | + $this->_formatted .= 'Z'; |
|
| 84 | + } |
|
| 85 | + return $this->_formatted; |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * {@inheritdoc} |
|
| 90 | - */ |
|
| 91 | - protected static function _decodeFromDER(Identifier $identifier, |
|
| 92 | - string $data, int &$offset): ElementBase |
|
| 93 | - { |
|
| 94 | - $idx = $offset; |
|
| 95 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 96 | - $str = substr($data, $idx, $length); |
|
| 97 | - $idx += $length; |
|
| 98 | - /** @var string[] $match */ |
|
| 99 | - if (!preg_match(self::REGEX, $str, $match)) { |
|
| 100 | - throw new DecodeException('Invalid GeneralizedTime format.'); |
|
| 101 | - } |
|
| 102 | - [, $year, $month, $day, $hour, $minute, $second] = $match; |
|
| 103 | - if (isset($match[7])) { |
|
| 104 | - $frac = $match[7]; |
|
| 105 | - // DER restricts trailing zeroes in fractional seconds component |
|
| 106 | - if ('0' === $frac[strlen($frac) - 1]) { |
|
| 107 | - throw new DecodeException( |
|
| 108 | - 'Fractional seconds must omit trailing zeroes.'); |
|
| 109 | - } |
|
| 110 | - $frac = (int) $frac; |
|
| 111 | - } else { |
|
| 112 | - $frac = 0; |
|
| 113 | - } |
|
| 114 | - $time = $year . $month . $day . $hour . $minute . $second . '.' . $frac . |
|
| 115 | - self::TZ_UTC; |
|
| 116 | - $dt = \DateTimeImmutable::createFromFormat('!YmdHis.uT', $time, |
|
| 117 | - self::_createTimeZone(self::TZ_UTC)); |
|
| 118 | - if (!$dt) { |
|
| 119 | - throw new DecodeException( |
|
| 120 | - 'Failed to decode GeneralizedTime: ' . |
|
| 121 | - self::_getLastDateTimeImmutableErrorsStr()); |
|
| 122 | - } |
|
| 123 | - $offset = $idx; |
|
| 124 | - return new self($dt); |
|
| 125 | - } |
|
| 88 | + /** |
|
| 89 | + * {@inheritdoc} |
|
| 90 | + */ |
|
| 91 | + protected static function _decodeFromDER(Identifier $identifier, |
|
| 92 | + string $data, int &$offset): ElementBase |
|
| 93 | + { |
|
| 94 | + $idx = $offset; |
|
| 95 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 96 | + $str = substr($data, $idx, $length); |
|
| 97 | + $idx += $length; |
|
| 98 | + /** @var string[] $match */ |
|
| 99 | + if (!preg_match(self::REGEX, $str, $match)) { |
|
| 100 | + throw new DecodeException('Invalid GeneralizedTime format.'); |
|
| 101 | + } |
|
| 102 | + [, $year, $month, $day, $hour, $minute, $second] = $match; |
|
| 103 | + if (isset($match[7])) { |
|
| 104 | + $frac = $match[7]; |
|
| 105 | + // DER restricts trailing zeroes in fractional seconds component |
|
| 106 | + if ('0' === $frac[strlen($frac) - 1]) { |
|
| 107 | + throw new DecodeException( |
|
| 108 | + 'Fractional seconds must omit trailing zeroes.'); |
|
| 109 | + } |
|
| 110 | + $frac = (int) $frac; |
|
| 111 | + } else { |
|
| 112 | + $frac = 0; |
|
| 113 | + } |
|
| 114 | + $time = $year . $month . $day . $hour . $minute . $second . '.' . $frac . |
|
| 115 | + self::TZ_UTC; |
|
| 116 | + $dt = \DateTimeImmutable::createFromFormat('!YmdHis.uT', $time, |
|
| 117 | + self::_createTimeZone(self::TZ_UTC)); |
|
| 118 | + if (!$dt) { |
|
| 119 | + throw new DecodeException( |
|
| 120 | + 'Failed to decode GeneralizedTime: ' . |
|
| 121 | + self::_getLastDateTimeImmutableErrorsStr()); |
|
| 122 | + } |
|
| 123 | + $offset = $idx; |
|
| 124 | + return new self($dt); |
|
| 125 | + } |
|
| 126 | 126 | } |
@@ -15,406 +15,406 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | abstract class Structure extends Element implements \Countable, \IteratorAggregate |
| 17 | 17 | { |
| 18 | - use UniversalClass; |
|
| 18 | + use UniversalClass; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Array of elements in the structure. |
|
| 22 | - * |
|
| 23 | - * @var Element[] |
|
| 24 | - */ |
|
| 25 | - protected $_elements; |
|
| 20 | + /** |
|
| 21 | + * Array of elements in the structure. |
|
| 22 | + * |
|
| 23 | + * @var Element[] |
|
| 24 | + */ |
|
| 25 | + protected $_elements; |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Lookup table for the tagged elements. |
|
| 29 | - * |
|
| 30 | - * @var null|TaggedType[] |
|
| 31 | - */ |
|
| 32 | - private $_taggedMap; |
|
| 27 | + /** |
|
| 28 | + * Lookup table for the tagged elements. |
|
| 29 | + * |
|
| 30 | + * @var null|TaggedType[] |
|
| 31 | + */ |
|
| 32 | + private $_taggedMap; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Cache variable of elements wrapped into UnspecifiedType objects. |
|
| 36 | - * |
|
| 37 | - * @var null|UnspecifiedType[] |
|
| 38 | - */ |
|
| 39 | - private $_unspecifiedTypes; |
|
| 34 | + /** |
|
| 35 | + * Cache variable of elements wrapped into UnspecifiedType objects. |
|
| 36 | + * |
|
| 37 | + * @var null|UnspecifiedType[] |
|
| 38 | + */ |
|
| 39 | + private $_unspecifiedTypes; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Constructor. |
|
| 43 | - * |
|
| 44 | - * @param ElementBase ...$elements Any number of elements |
|
| 45 | - */ |
|
| 46 | - public function __construct(ElementBase ...$elements) |
|
| 47 | - { |
|
| 48 | - $this->_elements = array_map( |
|
| 49 | - function (ElementBase $el) { |
|
| 50 | - return $el->asElement(); |
|
| 51 | - }, $elements); |
|
| 52 | - } |
|
| 41 | + /** |
|
| 42 | + * Constructor. |
|
| 43 | + * |
|
| 44 | + * @param ElementBase ...$elements Any number of elements |
|
| 45 | + */ |
|
| 46 | + public function __construct(ElementBase ...$elements) |
|
| 47 | + { |
|
| 48 | + $this->_elements = array_map( |
|
| 49 | + function (ElementBase $el) { |
|
| 50 | + return $el->asElement(); |
|
| 51 | + }, $elements); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Clone magic method. |
|
| 56 | - */ |
|
| 57 | - public function __clone() |
|
| 58 | - { |
|
| 59 | - // clear cache-variables |
|
| 60 | - $this->_taggedMap = null; |
|
| 61 | - $this->_unspecifiedTypes = null; |
|
| 62 | - } |
|
| 54 | + /** |
|
| 55 | + * Clone magic method. |
|
| 56 | + */ |
|
| 57 | + public function __clone() |
|
| 58 | + { |
|
| 59 | + // clear cache-variables |
|
| 60 | + $this->_taggedMap = null; |
|
| 61 | + $this->_unspecifiedTypes = null; |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * {@inheritdoc} |
|
| 66 | - */ |
|
| 67 | - public function isConstructed(): bool |
|
| 68 | - { |
|
| 69 | - return true; |
|
| 70 | - } |
|
| 64 | + /** |
|
| 65 | + * {@inheritdoc} |
|
| 66 | + */ |
|
| 67 | + public function isConstructed(): bool |
|
| 68 | + { |
|
| 69 | + return true; |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * Explode DER structure to DER encoded components that it contains. |
|
| 74 | - * |
|
| 75 | - * @param string $data |
|
| 76 | - * |
|
| 77 | - * @throws DecodeException |
|
| 78 | - * |
|
| 79 | - * @return string[] |
|
| 80 | - */ |
|
| 81 | - public static function explodeDER(string $data): array |
|
| 82 | - { |
|
| 83 | - $offset = 0; |
|
| 84 | - $identifier = Identifier::fromDER($data, $offset); |
|
| 85 | - if (!$identifier->isConstructed()) { |
|
| 86 | - throw new DecodeException('Element is not constructed.'); |
|
| 87 | - } |
|
| 88 | - $length = Length::expectFromDER($data, $offset); |
|
| 89 | - if ($length->isIndefinite()) { |
|
| 90 | - throw new DecodeException( |
|
| 91 | - 'Explode not implemented for indefinite length encoding.'); |
|
| 92 | - } |
|
| 93 | - $end = $offset + $length->intLength(); |
|
| 94 | - $parts = []; |
|
| 95 | - while ($offset < $end) { |
|
| 96 | - // start of the element |
|
| 97 | - $idx = $offset; |
|
| 98 | - // skip identifier |
|
| 99 | - Identifier::fromDER($data, $offset); |
|
| 100 | - // decode element length |
|
| 101 | - $length = Length::expectFromDER($data, $offset)->intLength(); |
|
| 102 | - // extract der encoding of the element |
|
| 103 | - $parts[] = substr($data, $idx, $offset - $idx + $length); |
|
| 104 | - // update offset over content |
|
| 105 | - $offset += $length; |
|
| 106 | - } |
|
| 107 | - return $parts; |
|
| 108 | - } |
|
| 72 | + /** |
|
| 73 | + * Explode DER structure to DER encoded components that it contains. |
|
| 74 | + * |
|
| 75 | + * @param string $data |
|
| 76 | + * |
|
| 77 | + * @throws DecodeException |
|
| 78 | + * |
|
| 79 | + * @return string[] |
|
| 80 | + */ |
|
| 81 | + public static function explodeDER(string $data): array |
|
| 82 | + { |
|
| 83 | + $offset = 0; |
|
| 84 | + $identifier = Identifier::fromDER($data, $offset); |
|
| 85 | + if (!$identifier->isConstructed()) { |
|
| 86 | + throw new DecodeException('Element is not constructed.'); |
|
| 87 | + } |
|
| 88 | + $length = Length::expectFromDER($data, $offset); |
|
| 89 | + if ($length->isIndefinite()) { |
|
| 90 | + throw new DecodeException( |
|
| 91 | + 'Explode not implemented for indefinite length encoding.'); |
|
| 92 | + } |
|
| 93 | + $end = $offset + $length->intLength(); |
|
| 94 | + $parts = []; |
|
| 95 | + while ($offset < $end) { |
|
| 96 | + // start of the element |
|
| 97 | + $idx = $offset; |
|
| 98 | + // skip identifier |
|
| 99 | + Identifier::fromDER($data, $offset); |
|
| 100 | + // decode element length |
|
| 101 | + $length = Length::expectFromDER($data, $offset)->intLength(); |
|
| 102 | + // extract der encoding of the element |
|
| 103 | + $parts[] = substr($data, $idx, $offset - $idx + $length); |
|
| 104 | + // update offset over content |
|
| 105 | + $offset += $length; |
|
| 106 | + } |
|
| 107 | + return $parts; |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | - /** |
|
| 111 | - * Get self with an element at the given index replaced by another. |
|
| 112 | - * |
|
| 113 | - * @param int $idx Element index |
|
| 114 | - * @param Element $el New element to insert into the structure |
|
| 115 | - * |
|
| 116 | - * @throws \OutOfBoundsException |
|
| 117 | - * |
|
| 118 | - * @return self |
|
| 119 | - */ |
|
| 120 | - public function withReplaced(int $idx, Element $el): self |
|
| 121 | - { |
|
| 122 | - if (!isset($this->_elements[$idx])) { |
|
| 123 | - throw new \OutOfBoundsException( |
|
| 124 | - "Structure doesn't have element at index {$idx}."); |
|
| 125 | - } |
|
| 126 | - $obj = clone $this; |
|
| 127 | - $obj->_elements[$idx] = $el; |
|
| 128 | - return $obj; |
|
| 129 | - } |
|
| 110 | + /** |
|
| 111 | + * Get self with an element at the given index replaced by another. |
|
| 112 | + * |
|
| 113 | + * @param int $idx Element index |
|
| 114 | + * @param Element $el New element to insert into the structure |
|
| 115 | + * |
|
| 116 | + * @throws \OutOfBoundsException |
|
| 117 | + * |
|
| 118 | + * @return self |
|
| 119 | + */ |
|
| 120 | + public function withReplaced(int $idx, Element $el): self |
|
| 121 | + { |
|
| 122 | + if (!isset($this->_elements[$idx])) { |
|
| 123 | + throw new \OutOfBoundsException( |
|
| 124 | + "Structure doesn't have element at index {$idx}."); |
|
| 125 | + } |
|
| 126 | + $obj = clone $this; |
|
| 127 | + $obj->_elements[$idx] = $el; |
|
| 128 | + return $obj; |
|
| 129 | + } |
|
| 130 | 130 | |
| 131 | - /** |
|
| 132 | - * Get self with an element inserted before the given index. |
|
| 133 | - * |
|
| 134 | - * @param int $idx Element index |
|
| 135 | - * @param Element $el New element to insert into the structure |
|
| 136 | - * |
|
| 137 | - * @throws \OutOfBoundsException |
|
| 138 | - * |
|
| 139 | - * @return self |
|
| 140 | - */ |
|
| 141 | - public function withInserted(int $idx, Element $el): self |
|
| 142 | - { |
|
| 143 | - if (count($this->_elements) < $idx || $idx < 0) { |
|
| 144 | - throw new \OutOfBoundsException("Index {$idx} is out of bounds."); |
|
| 145 | - } |
|
| 146 | - $obj = clone $this; |
|
| 147 | - array_splice($obj->_elements, $idx, 0, [$el]); |
|
| 148 | - return $obj; |
|
| 149 | - } |
|
| 131 | + /** |
|
| 132 | + * Get self with an element inserted before the given index. |
|
| 133 | + * |
|
| 134 | + * @param int $idx Element index |
|
| 135 | + * @param Element $el New element to insert into the structure |
|
| 136 | + * |
|
| 137 | + * @throws \OutOfBoundsException |
|
| 138 | + * |
|
| 139 | + * @return self |
|
| 140 | + */ |
|
| 141 | + public function withInserted(int $idx, Element $el): self |
|
| 142 | + { |
|
| 143 | + if (count($this->_elements) < $idx || $idx < 0) { |
|
| 144 | + throw new \OutOfBoundsException("Index {$idx} is out of bounds."); |
|
| 145 | + } |
|
| 146 | + $obj = clone $this; |
|
| 147 | + array_splice($obj->_elements, $idx, 0, [$el]); |
|
| 148 | + return $obj; |
|
| 149 | + } |
|
| 150 | 150 | |
| 151 | - /** |
|
| 152 | - * Get self with an element appended to the end. |
|
| 153 | - * |
|
| 154 | - * @param Element $el Element to insert into the structure |
|
| 155 | - * |
|
| 156 | - * @return self |
|
| 157 | - */ |
|
| 158 | - public function withAppended(Element $el): self |
|
| 159 | - { |
|
| 160 | - $obj = clone $this; |
|
| 161 | - array_push($obj->_elements, $el); |
|
| 162 | - return $obj; |
|
| 163 | - } |
|
| 151 | + /** |
|
| 152 | + * Get self with an element appended to the end. |
|
| 153 | + * |
|
| 154 | + * @param Element $el Element to insert into the structure |
|
| 155 | + * |
|
| 156 | + * @return self |
|
| 157 | + */ |
|
| 158 | + public function withAppended(Element $el): self |
|
| 159 | + { |
|
| 160 | + $obj = clone $this; |
|
| 161 | + array_push($obj->_elements, $el); |
|
| 162 | + return $obj; |
|
| 163 | + } |
|
| 164 | 164 | |
| 165 | - /** |
|
| 166 | - * Get self with an element prepended in the beginning. |
|
| 167 | - * |
|
| 168 | - * @param Element $el Element to insert into the structure |
|
| 169 | - * |
|
| 170 | - * @return self |
|
| 171 | - */ |
|
| 172 | - public function withPrepended(Element $el): self |
|
| 173 | - { |
|
| 174 | - $obj = clone $this; |
|
| 175 | - array_unshift($obj->_elements, $el); |
|
| 176 | - return $obj; |
|
| 177 | - } |
|
| 165 | + /** |
|
| 166 | + * Get self with an element prepended in the beginning. |
|
| 167 | + * |
|
| 168 | + * @param Element $el Element to insert into the structure |
|
| 169 | + * |
|
| 170 | + * @return self |
|
| 171 | + */ |
|
| 172 | + public function withPrepended(Element $el): self |
|
| 173 | + { |
|
| 174 | + $obj = clone $this; |
|
| 175 | + array_unshift($obj->_elements, $el); |
|
| 176 | + return $obj; |
|
| 177 | + } |
|
| 178 | 178 | |
| 179 | - /** |
|
| 180 | - * Get self with an element at the given index removed. |
|
| 181 | - * |
|
| 182 | - * @param int $idx Element index |
|
| 183 | - * |
|
| 184 | - * @throws \OutOfBoundsException |
|
| 185 | - * |
|
| 186 | - * @return self |
|
| 187 | - */ |
|
| 188 | - public function withoutElement(int $idx): self |
|
| 189 | - { |
|
| 190 | - if (!isset($this->_elements[$idx])) { |
|
| 191 | - throw new \OutOfBoundsException( |
|
| 192 | - "Structure doesn't have element at index {$idx}."); |
|
| 193 | - } |
|
| 194 | - $obj = clone $this; |
|
| 195 | - array_splice($obj->_elements, $idx, 1); |
|
| 196 | - return $obj; |
|
| 197 | - } |
|
| 179 | + /** |
|
| 180 | + * Get self with an element at the given index removed. |
|
| 181 | + * |
|
| 182 | + * @param int $idx Element index |
|
| 183 | + * |
|
| 184 | + * @throws \OutOfBoundsException |
|
| 185 | + * |
|
| 186 | + * @return self |
|
| 187 | + */ |
|
| 188 | + public function withoutElement(int $idx): self |
|
| 189 | + { |
|
| 190 | + if (!isset($this->_elements[$idx])) { |
|
| 191 | + throw new \OutOfBoundsException( |
|
| 192 | + "Structure doesn't have element at index {$idx}."); |
|
| 193 | + } |
|
| 194 | + $obj = clone $this; |
|
| 195 | + array_splice($obj->_elements, $idx, 1); |
|
| 196 | + return $obj; |
|
| 197 | + } |
|
| 198 | 198 | |
| 199 | - /** |
|
| 200 | - * Get elements in the structure. |
|
| 201 | - * |
|
| 202 | - * @return UnspecifiedType[] |
|
| 203 | - */ |
|
| 204 | - public function elements(): array |
|
| 205 | - { |
|
| 206 | - if (!isset($this->_unspecifiedTypes)) { |
|
| 207 | - $this->_unspecifiedTypes = array_map( |
|
| 208 | - function (Element $el) { |
|
| 209 | - return new UnspecifiedType($el); |
|
| 210 | - }, $this->_elements); |
|
| 211 | - } |
|
| 212 | - return $this->_unspecifiedTypes; |
|
| 213 | - } |
|
| 199 | + /** |
|
| 200 | + * Get elements in the structure. |
|
| 201 | + * |
|
| 202 | + * @return UnspecifiedType[] |
|
| 203 | + */ |
|
| 204 | + public function elements(): array |
|
| 205 | + { |
|
| 206 | + if (!isset($this->_unspecifiedTypes)) { |
|
| 207 | + $this->_unspecifiedTypes = array_map( |
|
| 208 | + function (Element $el) { |
|
| 209 | + return new UnspecifiedType($el); |
|
| 210 | + }, $this->_elements); |
|
| 211 | + } |
|
| 212 | + return $this->_unspecifiedTypes; |
|
| 213 | + } |
|
| 214 | 214 | |
| 215 | - /** |
|
| 216 | - * Check whether the structure has an element at the given index, optionally |
|
| 217 | - * satisfying given tag expectation. |
|
| 218 | - * |
|
| 219 | - * @param int $idx Index 0..n |
|
| 220 | - * @param null|int $expectedTag Optional type tag expectation |
|
| 221 | - * |
|
| 222 | - * @return bool |
|
| 223 | - */ |
|
| 224 | - public function has(int $idx, ?int $expectedTag = null): bool |
|
| 225 | - { |
|
| 226 | - if (!isset($this->_elements[$idx])) { |
|
| 227 | - return false; |
|
| 228 | - } |
|
| 229 | - if (isset($expectedTag)) { |
|
| 230 | - if (!$this->_elements[$idx]->isType($expectedTag)) { |
|
| 231 | - return false; |
|
| 232 | - } |
|
| 233 | - } |
|
| 234 | - return true; |
|
| 235 | - } |
|
| 215 | + /** |
|
| 216 | + * Check whether the structure has an element at the given index, optionally |
|
| 217 | + * satisfying given tag expectation. |
|
| 218 | + * |
|
| 219 | + * @param int $idx Index 0..n |
|
| 220 | + * @param null|int $expectedTag Optional type tag expectation |
|
| 221 | + * |
|
| 222 | + * @return bool |
|
| 223 | + */ |
|
| 224 | + public function has(int $idx, ?int $expectedTag = null): bool |
|
| 225 | + { |
|
| 226 | + if (!isset($this->_elements[$idx])) { |
|
| 227 | + return false; |
|
| 228 | + } |
|
| 229 | + if (isset($expectedTag)) { |
|
| 230 | + if (!$this->_elements[$idx]->isType($expectedTag)) { |
|
| 231 | + return false; |
|
| 232 | + } |
|
| 233 | + } |
|
| 234 | + return true; |
|
| 235 | + } |
|
| 236 | 236 | |
| 237 | - /** |
|
| 238 | - * Get the element at the given index, optionally checking that the element |
|
| 239 | - * has a given tag. |
|
| 240 | - * |
|
| 241 | - * @param int $idx Index 0..n |
|
| 242 | - * |
|
| 243 | - * @throws \OutOfBoundsException If element doesn't exists |
|
| 244 | - * @throws \UnexpectedValueException If expectation fails |
|
| 245 | - * |
|
| 246 | - * @return UnspecifiedType |
|
| 247 | - */ |
|
| 248 | - public function at(int $idx): UnspecifiedType |
|
| 249 | - { |
|
| 250 | - if (!isset($this->_elements[$idx])) { |
|
| 251 | - throw new \OutOfBoundsException( |
|
| 252 | - "Structure doesn't have an element at index {$idx}."); |
|
| 253 | - } |
|
| 254 | - return new UnspecifiedType($this->_elements[$idx]); |
|
| 255 | - } |
|
| 237 | + /** |
|
| 238 | + * Get the element at the given index, optionally checking that the element |
|
| 239 | + * has a given tag. |
|
| 240 | + * |
|
| 241 | + * @param int $idx Index 0..n |
|
| 242 | + * |
|
| 243 | + * @throws \OutOfBoundsException If element doesn't exists |
|
| 244 | + * @throws \UnexpectedValueException If expectation fails |
|
| 245 | + * |
|
| 246 | + * @return UnspecifiedType |
|
| 247 | + */ |
|
| 248 | + public function at(int $idx): UnspecifiedType |
|
| 249 | + { |
|
| 250 | + if (!isset($this->_elements[$idx])) { |
|
| 251 | + throw new \OutOfBoundsException( |
|
| 252 | + "Structure doesn't have an element at index {$idx}."); |
|
| 253 | + } |
|
| 254 | + return new UnspecifiedType($this->_elements[$idx]); |
|
| 255 | + } |
|
| 256 | 256 | |
| 257 | - /** |
|
| 258 | - * Check whether the structure contains a context specific element with a |
|
| 259 | - * given tag. |
|
| 260 | - * |
|
| 261 | - * @param int $tag Tag number |
|
| 262 | - * |
|
| 263 | - * @return bool |
|
| 264 | - */ |
|
| 265 | - public function hasTagged(int $tag): bool |
|
| 266 | - { |
|
| 267 | - // lazily build lookup map |
|
| 268 | - if (!isset($this->_taggedMap)) { |
|
| 269 | - $this->_taggedMap = []; |
|
| 270 | - foreach ($this->_elements as $element) { |
|
| 271 | - if ($element->isTagged()) { |
|
| 272 | - $this->_taggedMap[$element->tag()] = $element; |
|
| 273 | - } |
|
| 274 | - } |
|
| 275 | - } |
|
| 276 | - return isset($this->_taggedMap[$tag]); |
|
| 277 | - } |
|
| 257 | + /** |
|
| 258 | + * Check whether the structure contains a context specific element with a |
|
| 259 | + * given tag. |
|
| 260 | + * |
|
| 261 | + * @param int $tag Tag number |
|
| 262 | + * |
|
| 263 | + * @return bool |
|
| 264 | + */ |
|
| 265 | + public function hasTagged(int $tag): bool |
|
| 266 | + { |
|
| 267 | + // lazily build lookup map |
|
| 268 | + if (!isset($this->_taggedMap)) { |
|
| 269 | + $this->_taggedMap = []; |
|
| 270 | + foreach ($this->_elements as $element) { |
|
| 271 | + if ($element->isTagged()) { |
|
| 272 | + $this->_taggedMap[$element->tag()] = $element; |
|
| 273 | + } |
|
| 274 | + } |
|
| 275 | + } |
|
| 276 | + return isset($this->_taggedMap[$tag]); |
|
| 277 | + } |
|
| 278 | 278 | |
| 279 | - /** |
|
| 280 | - * Get a context specific element tagged with a given tag. |
|
| 281 | - * |
|
| 282 | - * @param int $tag |
|
| 283 | - * |
|
| 284 | - * @throws \LogicException If tag doesn't exists |
|
| 285 | - * |
|
| 286 | - * @return TaggedType |
|
| 287 | - */ |
|
| 288 | - public function getTagged(int $tag): TaggedType |
|
| 289 | - { |
|
| 290 | - if (!$this->hasTagged($tag)) { |
|
| 291 | - throw new \LogicException("No tagged element for tag {$tag}."); |
|
| 292 | - } |
|
| 293 | - return $this->_taggedMap[$tag]; |
|
| 294 | - } |
|
| 279 | + /** |
|
| 280 | + * Get a context specific element tagged with a given tag. |
|
| 281 | + * |
|
| 282 | + * @param int $tag |
|
| 283 | + * |
|
| 284 | + * @throws \LogicException If tag doesn't exists |
|
| 285 | + * |
|
| 286 | + * @return TaggedType |
|
| 287 | + */ |
|
| 288 | + public function getTagged(int $tag): TaggedType |
|
| 289 | + { |
|
| 290 | + if (!$this->hasTagged($tag)) { |
|
| 291 | + throw new \LogicException("No tagged element for tag {$tag}."); |
|
| 292 | + } |
|
| 293 | + return $this->_taggedMap[$tag]; |
|
| 294 | + } |
|
| 295 | 295 | |
| 296 | - /** |
|
| 297 | - * @see \Countable::count() |
|
| 298 | - * |
|
| 299 | - * @return int |
|
| 300 | - */ |
|
| 301 | - public function count(): int |
|
| 302 | - { |
|
| 303 | - return count($this->_elements); |
|
| 304 | - } |
|
| 296 | + /** |
|
| 297 | + * @see \Countable::count() |
|
| 298 | + * |
|
| 299 | + * @return int |
|
| 300 | + */ |
|
| 301 | + public function count(): int |
|
| 302 | + { |
|
| 303 | + return count($this->_elements); |
|
| 304 | + } |
|
| 305 | 305 | |
| 306 | - /** |
|
| 307 | - * Get an iterator for the UnspecifiedElement objects. |
|
| 308 | - * |
|
| 309 | - * @see \IteratorAggregate::getIterator() |
|
| 310 | - * |
|
| 311 | - * @return \ArrayIterator |
|
| 312 | - */ |
|
| 313 | - public function getIterator(): \ArrayIterator |
|
| 314 | - { |
|
| 315 | - return new \ArrayIterator($this->elements()); |
|
| 316 | - } |
|
| 306 | + /** |
|
| 307 | + * Get an iterator for the UnspecifiedElement objects. |
|
| 308 | + * |
|
| 309 | + * @see \IteratorAggregate::getIterator() |
|
| 310 | + * |
|
| 311 | + * @return \ArrayIterator |
|
| 312 | + */ |
|
| 313 | + public function getIterator(): \ArrayIterator |
|
| 314 | + { |
|
| 315 | + return new \ArrayIterator($this->elements()); |
|
| 316 | + } |
|
| 317 | 317 | |
| 318 | - /** |
|
| 319 | - * @see \Sop\ASN1\Element::_encodedContentDER() |
|
| 320 | - * |
|
| 321 | - * @return string |
|
| 322 | - */ |
|
| 323 | - protected function _encodedContentDER(): string |
|
| 324 | - { |
|
| 325 | - $data = ''; |
|
| 326 | - foreach ($this->_elements as $element) { |
|
| 327 | - $data .= $element->toDER(); |
|
| 328 | - } |
|
| 329 | - return $data; |
|
| 330 | - } |
|
| 318 | + /** |
|
| 319 | + * @see \Sop\ASN1\Element::_encodedContentDER() |
|
| 320 | + * |
|
| 321 | + * @return string |
|
| 322 | + */ |
|
| 323 | + protected function _encodedContentDER(): string |
|
| 324 | + { |
|
| 325 | + $data = ''; |
|
| 326 | + foreach ($this->_elements as $element) { |
|
| 327 | + $data .= $element->toDER(); |
|
| 328 | + } |
|
| 329 | + return $data; |
|
| 330 | + } |
|
| 331 | 331 | |
| 332 | - /** |
|
| 333 | - * {@inheritdoc} |
|
| 334 | - * |
|
| 335 | - * @see \Sop\ASN1\Element::_decodeFromDER() |
|
| 336 | - * |
|
| 337 | - * @return self |
|
| 338 | - */ |
|
| 339 | - protected static function _decodeFromDER(Identifier $identifier, |
|
| 340 | - string $data, int &$offset): ElementBase |
|
| 341 | - { |
|
| 342 | - if (!$identifier->isConstructed()) { |
|
| 343 | - throw new DecodeException( |
|
| 344 | - 'Structured element must have constructed bit set.'); |
|
| 345 | - } |
|
| 346 | - $idx = $offset; |
|
| 347 | - $length = Length::expectFromDER($data, $idx); |
|
| 348 | - if ($length->isIndefinite()) { |
|
| 349 | - $type = self::_decodeIndefiniteLength($data, $idx); |
|
| 350 | - } else { |
|
| 351 | - $type = self::_decodeDefiniteLength($data, $idx, |
|
| 352 | - $length->intLength()); |
|
| 353 | - } |
|
| 354 | - $offset = $idx; |
|
| 355 | - return $type; |
|
| 356 | - } |
|
| 332 | + /** |
|
| 333 | + * {@inheritdoc} |
|
| 334 | + * |
|
| 335 | + * @see \Sop\ASN1\Element::_decodeFromDER() |
|
| 336 | + * |
|
| 337 | + * @return self |
|
| 338 | + */ |
|
| 339 | + protected static function _decodeFromDER(Identifier $identifier, |
|
| 340 | + string $data, int &$offset): ElementBase |
|
| 341 | + { |
|
| 342 | + if (!$identifier->isConstructed()) { |
|
| 343 | + throw new DecodeException( |
|
| 344 | + 'Structured element must have constructed bit set.'); |
|
| 345 | + } |
|
| 346 | + $idx = $offset; |
|
| 347 | + $length = Length::expectFromDER($data, $idx); |
|
| 348 | + if ($length->isIndefinite()) { |
|
| 349 | + $type = self::_decodeIndefiniteLength($data, $idx); |
|
| 350 | + } else { |
|
| 351 | + $type = self::_decodeDefiniteLength($data, $idx, |
|
| 352 | + $length->intLength()); |
|
| 353 | + } |
|
| 354 | + $offset = $idx; |
|
| 355 | + return $type; |
|
| 356 | + } |
|
| 357 | 357 | |
| 358 | - /** |
|
| 359 | - * Decode elements for a definite length. |
|
| 360 | - * |
|
| 361 | - * @param string $data DER data |
|
| 362 | - * @param int $offset Offset to data |
|
| 363 | - * @param int $length Number of bytes to decode |
|
| 364 | - * |
|
| 365 | - * @throws DecodeException |
|
| 366 | - * |
|
| 367 | - * @return ElementBase |
|
| 368 | - */ |
|
| 369 | - private static function _decodeDefiniteLength(string $data, int &$offset, |
|
| 370 | - int $length): ElementBase |
|
| 371 | - { |
|
| 372 | - $idx = $offset; |
|
| 373 | - $end = $idx + $length; |
|
| 374 | - $elements = []; |
|
| 375 | - while ($idx < $end) { |
|
| 376 | - $elements[] = Element::fromDER($data, $idx); |
|
| 377 | - // check that element didn't overflow length |
|
| 378 | - if ($idx > $end) { |
|
| 379 | - throw new DecodeException( |
|
| 380 | - "Structure's content overflows length."); |
|
| 381 | - } |
|
| 382 | - } |
|
| 383 | - $offset = $idx; |
|
| 384 | - // return instance by static late binding |
|
| 385 | - return new static(...$elements); |
|
| 386 | - } |
|
| 358 | + /** |
|
| 359 | + * Decode elements for a definite length. |
|
| 360 | + * |
|
| 361 | + * @param string $data DER data |
|
| 362 | + * @param int $offset Offset to data |
|
| 363 | + * @param int $length Number of bytes to decode |
|
| 364 | + * |
|
| 365 | + * @throws DecodeException |
|
| 366 | + * |
|
| 367 | + * @return ElementBase |
|
| 368 | + */ |
|
| 369 | + private static function _decodeDefiniteLength(string $data, int &$offset, |
|
| 370 | + int $length): ElementBase |
|
| 371 | + { |
|
| 372 | + $idx = $offset; |
|
| 373 | + $end = $idx + $length; |
|
| 374 | + $elements = []; |
|
| 375 | + while ($idx < $end) { |
|
| 376 | + $elements[] = Element::fromDER($data, $idx); |
|
| 377 | + // check that element didn't overflow length |
|
| 378 | + if ($idx > $end) { |
|
| 379 | + throw new DecodeException( |
|
| 380 | + "Structure's content overflows length."); |
|
| 381 | + } |
|
| 382 | + } |
|
| 383 | + $offset = $idx; |
|
| 384 | + // return instance by static late binding |
|
| 385 | + return new static(...$elements); |
|
| 386 | + } |
|
| 387 | 387 | |
| 388 | - /** |
|
| 389 | - * Decode elements for an indefinite length. |
|
| 390 | - * |
|
| 391 | - * @param string $data DER data |
|
| 392 | - * @param int $offset Offset to data |
|
| 393 | - * |
|
| 394 | - * @throws DecodeException |
|
| 395 | - * |
|
| 396 | - * @return ElementBase |
|
| 397 | - */ |
|
| 398 | - private static function _decodeIndefiniteLength( |
|
| 399 | - string $data, int &$offset): ElementBase |
|
| 400 | - { |
|
| 401 | - $idx = $offset; |
|
| 402 | - $elements = []; |
|
| 403 | - $end = strlen($data); |
|
| 404 | - while (true) { |
|
| 405 | - if ($idx >= $end) { |
|
| 406 | - throw new DecodeException( |
|
| 407 | - 'Unexpected end of data while decoding indefinite length structure.'); |
|
| 408 | - } |
|
| 409 | - $el = Element::fromDER($data, $idx); |
|
| 410 | - if ($el->isType(self::TYPE_EOC)) { |
|
| 411 | - break; |
|
| 412 | - } |
|
| 413 | - $elements[] = $el; |
|
| 414 | - } |
|
| 415 | - $offset = $idx; |
|
| 416 | - $type = new static(...$elements); |
|
| 417 | - $type->_indefiniteLength = true; |
|
| 418 | - return $type; |
|
| 419 | - } |
|
| 388 | + /** |
|
| 389 | + * Decode elements for an indefinite length. |
|
| 390 | + * |
|
| 391 | + * @param string $data DER data |
|
| 392 | + * @param int $offset Offset to data |
|
| 393 | + * |
|
| 394 | + * @throws DecodeException |
|
| 395 | + * |
|
| 396 | + * @return ElementBase |
|
| 397 | + */ |
|
| 398 | + private static function _decodeIndefiniteLength( |
|
| 399 | + string $data, int &$offset): ElementBase |
|
| 400 | + { |
|
| 401 | + $idx = $offset; |
|
| 402 | + $elements = []; |
|
| 403 | + $end = strlen($data); |
|
| 404 | + while (true) { |
|
| 405 | + if ($idx >= $end) { |
|
| 406 | + throw new DecodeException( |
|
| 407 | + 'Unexpected end of data while decoding indefinite length structure.'); |
|
| 408 | + } |
|
| 409 | + $el = Element::fromDER($data, $idx); |
|
| 410 | + if ($el->isType(self::TYPE_EOC)) { |
|
| 411 | + break; |
|
| 412 | + } |
|
| 413 | + $elements[] = $el; |
|
| 414 | + } |
|
| 415 | + $offset = $idx; |
|
| 416 | + $type = new static(...$elements); |
|
| 417 | + $type->_indefiniteLength = true; |
|
| 418 | + return $type; |
|
| 419 | + } |
|
| 420 | 420 | } |
@@ -23,467 +23,467 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | abstract class Element implements ElementBase |
| 25 | 25 | { |
| 26 | - // Universal type tags |
|
| 27 | - const TYPE_EOC = 0x00; |
|
| 28 | - const TYPE_BOOLEAN = 0x01; |
|
| 29 | - const TYPE_INTEGER = 0x02; |
|
| 30 | - const TYPE_BIT_STRING = 0x03; |
|
| 31 | - const TYPE_OCTET_STRING = 0x04; |
|
| 32 | - const TYPE_NULL = 0x05; |
|
| 33 | - const TYPE_OBJECT_IDENTIFIER = 0x06; |
|
| 34 | - const TYPE_OBJECT_DESCRIPTOR = 0x07; |
|
| 35 | - const TYPE_EXTERNAL = 0x08; |
|
| 36 | - const TYPE_REAL = 0x09; |
|
| 37 | - const TYPE_ENUMERATED = 0x0a; |
|
| 38 | - const TYPE_EMBEDDED_PDV = 0x0b; |
|
| 39 | - const TYPE_UTF8_STRING = 0x0c; |
|
| 40 | - const TYPE_RELATIVE_OID = 0x0d; |
|
| 41 | - const TYPE_SEQUENCE = 0x10; |
|
| 42 | - const TYPE_SET = 0x11; |
|
| 43 | - const TYPE_NUMERIC_STRING = 0x12; |
|
| 44 | - const TYPE_PRINTABLE_STRING = 0x13; |
|
| 45 | - const TYPE_T61_STRING = 0x14; |
|
| 46 | - const TYPE_VIDEOTEX_STRING = 0x15; |
|
| 47 | - const TYPE_IA5_STRING = 0x16; |
|
| 48 | - const TYPE_UTC_TIME = 0x17; |
|
| 49 | - const TYPE_GENERALIZED_TIME = 0x18; |
|
| 50 | - const TYPE_GRAPHIC_STRING = 0x19; |
|
| 51 | - const TYPE_VISIBLE_STRING = 0x1a; |
|
| 52 | - const TYPE_GENERAL_STRING = 0x1b; |
|
| 53 | - const TYPE_UNIVERSAL_STRING = 0x1c; |
|
| 54 | - const TYPE_CHARACTER_STRING = 0x1d; |
|
| 55 | - const TYPE_BMP_STRING = 0x1e; |
|
| 26 | + // Universal type tags |
|
| 27 | + const TYPE_EOC = 0x00; |
|
| 28 | + const TYPE_BOOLEAN = 0x01; |
|
| 29 | + const TYPE_INTEGER = 0x02; |
|
| 30 | + const TYPE_BIT_STRING = 0x03; |
|
| 31 | + const TYPE_OCTET_STRING = 0x04; |
|
| 32 | + const TYPE_NULL = 0x05; |
|
| 33 | + const TYPE_OBJECT_IDENTIFIER = 0x06; |
|
| 34 | + const TYPE_OBJECT_DESCRIPTOR = 0x07; |
|
| 35 | + const TYPE_EXTERNAL = 0x08; |
|
| 36 | + const TYPE_REAL = 0x09; |
|
| 37 | + const TYPE_ENUMERATED = 0x0a; |
|
| 38 | + const TYPE_EMBEDDED_PDV = 0x0b; |
|
| 39 | + const TYPE_UTF8_STRING = 0x0c; |
|
| 40 | + const TYPE_RELATIVE_OID = 0x0d; |
|
| 41 | + const TYPE_SEQUENCE = 0x10; |
|
| 42 | + const TYPE_SET = 0x11; |
|
| 43 | + const TYPE_NUMERIC_STRING = 0x12; |
|
| 44 | + const TYPE_PRINTABLE_STRING = 0x13; |
|
| 45 | + const TYPE_T61_STRING = 0x14; |
|
| 46 | + const TYPE_VIDEOTEX_STRING = 0x15; |
|
| 47 | + const TYPE_IA5_STRING = 0x16; |
|
| 48 | + const TYPE_UTC_TIME = 0x17; |
|
| 49 | + const TYPE_GENERALIZED_TIME = 0x18; |
|
| 50 | + const TYPE_GRAPHIC_STRING = 0x19; |
|
| 51 | + const TYPE_VISIBLE_STRING = 0x1a; |
|
| 52 | + const TYPE_GENERAL_STRING = 0x1b; |
|
| 53 | + const TYPE_UNIVERSAL_STRING = 0x1c; |
|
| 54 | + const TYPE_CHARACTER_STRING = 0x1d; |
|
| 55 | + const TYPE_BMP_STRING = 0x1e; |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * Mapping from universal type tag to implementation class name. |
|
| 59 | - * |
|
| 60 | - * @internal |
|
| 61 | - * |
|
| 62 | - * @var array |
|
| 63 | - */ |
|
| 64 | - const MAP_TAG_TO_CLASS = [ |
|
| 65 | - self::TYPE_EOC => Primitive\EOC::class, |
|
| 66 | - self::TYPE_BOOLEAN => Primitive\Boolean::class, |
|
| 67 | - self::TYPE_INTEGER => Primitive\Integer::class, |
|
| 68 | - self::TYPE_BIT_STRING => Primitive\BitString::class, |
|
| 69 | - self::TYPE_OCTET_STRING => Primitive\OctetString::class, |
|
| 70 | - self::TYPE_NULL => Primitive\NullType::class, |
|
| 71 | - self::TYPE_OBJECT_IDENTIFIER => Primitive\ObjectIdentifier::class, |
|
| 72 | - self::TYPE_OBJECT_DESCRIPTOR => Primitive\ObjectDescriptor::class, |
|
| 73 | - self::TYPE_REAL => Primitive\Real::class, |
|
| 74 | - self::TYPE_ENUMERATED => Primitive\Enumerated::class, |
|
| 75 | - self::TYPE_UTF8_STRING => Primitive\UTF8String::class, |
|
| 76 | - self::TYPE_RELATIVE_OID => Primitive\RelativeOID::class, |
|
| 77 | - self::TYPE_SEQUENCE => Constructed\Sequence::class, |
|
| 78 | - self::TYPE_SET => Constructed\Set::class, |
|
| 79 | - self::TYPE_NUMERIC_STRING => Primitive\NumericString::class, |
|
| 80 | - self::TYPE_PRINTABLE_STRING => Primitive\PrintableString::class, |
|
| 81 | - self::TYPE_T61_STRING => Primitive\T61String::class, |
|
| 82 | - self::TYPE_VIDEOTEX_STRING => Primitive\VideotexString::class, |
|
| 83 | - self::TYPE_IA5_STRING => Primitive\IA5String::class, |
|
| 84 | - self::TYPE_UTC_TIME => Primitive\UTCTime::class, |
|
| 85 | - self::TYPE_GENERALIZED_TIME => Primitive\GeneralizedTime::class, |
|
| 86 | - self::TYPE_GRAPHIC_STRING => Primitive\GraphicString::class, |
|
| 87 | - self::TYPE_VISIBLE_STRING => Primitive\VisibleString::class, |
|
| 88 | - self::TYPE_GENERAL_STRING => Primitive\GeneralString::class, |
|
| 89 | - self::TYPE_UNIVERSAL_STRING => Primitive\UniversalString::class, |
|
| 90 | - self::TYPE_CHARACTER_STRING => Primitive\CharacterString::class, |
|
| 91 | - self::TYPE_BMP_STRING => Primitive\BMPString::class, |
|
| 92 | - ]; |
|
| 57 | + /** |
|
| 58 | + * Mapping from universal type tag to implementation class name. |
|
| 59 | + * |
|
| 60 | + * @internal |
|
| 61 | + * |
|
| 62 | + * @var array |
|
| 63 | + */ |
|
| 64 | + const MAP_TAG_TO_CLASS = [ |
|
| 65 | + self::TYPE_EOC => Primitive\EOC::class, |
|
| 66 | + self::TYPE_BOOLEAN => Primitive\Boolean::class, |
|
| 67 | + self::TYPE_INTEGER => Primitive\Integer::class, |
|
| 68 | + self::TYPE_BIT_STRING => Primitive\BitString::class, |
|
| 69 | + self::TYPE_OCTET_STRING => Primitive\OctetString::class, |
|
| 70 | + self::TYPE_NULL => Primitive\NullType::class, |
|
| 71 | + self::TYPE_OBJECT_IDENTIFIER => Primitive\ObjectIdentifier::class, |
|
| 72 | + self::TYPE_OBJECT_DESCRIPTOR => Primitive\ObjectDescriptor::class, |
|
| 73 | + self::TYPE_REAL => Primitive\Real::class, |
|
| 74 | + self::TYPE_ENUMERATED => Primitive\Enumerated::class, |
|
| 75 | + self::TYPE_UTF8_STRING => Primitive\UTF8String::class, |
|
| 76 | + self::TYPE_RELATIVE_OID => Primitive\RelativeOID::class, |
|
| 77 | + self::TYPE_SEQUENCE => Constructed\Sequence::class, |
|
| 78 | + self::TYPE_SET => Constructed\Set::class, |
|
| 79 | + self::TYPE_NUMERIC_STRING => Primitive\NumericString::class, |
|
| 80 | + self::TYPE_PRINTABLE_STRING => Primitive\PrintableString::class, |
|
| 81 | + self::TYPE_T61_STRING => Primitive\T61String::class, |
|
| 82 | + self::TYPE_VIDEOTEX_STRING => Primitive\VideotexString::class, |
|
| 83 | + self::TYPE_IA5_STRING => Primitive\IA5String::class, |
|
| 84 | + self::TYPE_UTC_TIME => Primitive\UTCTime::class, |
|
| 85 | + self::TYPE_GENERALIZED_TIME => Primitive\GeneralizedTime::class, |
|
| 86 | + self::TYPE_GRAPHIC_STRING => Primitive\GraphicString::class, |
|
| 87 | + self::TYPE_VISIBLE_STRING => Primitive\VisibleString::class, |
|
| 88 | + self::TYPE_GENERAL_STRING => Primitive\GeneralString::class, |
|
| 89 | + self::TYPE_UNIVERSAL_STRING => Primitive\UniversalString::class, |
|
| 90 | + self::TYPE_CHARACTER_STRING => Primitive\CharacterString::class, |
|
| 91 | + self::TYPE_BMP_STRING => Primitive\BMPString::class, |
|
| 92 | + ]; |
|
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * Pseudotype for all string types. |
|
| 96 | - * |
|
| 97 | - * May be used as an expectation parameter. |
|
| 98 | - * |
|
| 99 | - * @var int |
|
| 100 | - */ |
|
| 101 | - const TYPE_STRING = -1; |
|
| 94 | + /** |
|
| 95 | + * Pseudotype for all string types. |
|
| 96 | + * |
|
| 97 | + * May be used as an expectation parameter. |
|
| 98 | + * |
|
| 99 | + * @var int |
|
| 100 | + */ |
|
| 101 | + const TYPE_STRING = -1; |
|
| 102 | 102 | |
| 103 | - /** |
|
| 104 | - * Pseudotype for all time types. |
|
| 105 | - * |
|
| 106 | - * May be used as an expectation parameter. |
|
| 107 | - * |
|
| 108 | - * @var int |
|
| 109 | - */ |
|
| 110 | - const TYPE_TIME = -2; |
|
| 103 | + /** |
|
| 104 | + * Pseudotype for all time types. |
|
| 105 | + * |
|
| 106 | + * May be used as an expectation parameter. |
|
| 107 | + * |
|
| 108 | + * @var int |
|
| 109 | + */ |
|
| 110 | + const TYPE_TIME = -2; |
|
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * Mapping from universal type tag to human readable name. |
|
| 114 | - * |
|
| 115 | - * @internal |
|
| 116 | - * |
|
| 117 | - * @var array |
|
| 118 | - */ |
|
| 119 | - const MAP_TYPE_TO_NAME = [ |
|
| 120 | - self::TYPE_EOC => 'EOC', |
|
| 121 | - self::TYPE_BOOLEAN => 'BOOLEAN', |
|
| 122 | - self::TYPE_INTEGER => 'INTEGER', |
|
| 123 | - self::TYPE_BIT_STRING => 'BIT STRING', |
|
| 124 | - self::TYPE_OCTET_STRING => 'OCTET STRING', |
|
| 125 | - self::TYPE_NULL => 'NULL', |
|
| 126 | - self::TYPE_OBJECT_IDENTIFIER => 'OBJECT IDENTIFIER', |
|
| 127 | - self::TYPE_OBJECT_DESCRIPTOR => 'ObjectDescriptor', |
|
| 128 | - self::TYPE_EXTERNAL => 'EXTERNAL', |
|
| 129 | - self::TYPE_REAL => 'REAL', |
|
| 130 | - self::TYPE_ENUMERATED => 'ENUMERATED', |
|
| 131 | - self::TYPE_EMBEDDED_PDV => 'EMBEDDED PDV', |
|
| 132 | - self::TYPE_UTF8_STRING => 'UTF8String', |
|
| 133 | - self::TYPE_RELATIVE_OID => 'RELATIVE-OID', |
|
| 134 | - self::TYPE_SEQUENCE => 'SEQUENCE', |
|
| 135 | - self::TYPE_SET => 'SET', |
|
| 136 | - self::TYPE_NUMERIC_STRING => 'NumericString', |
|
| 137 | - self::TYPE_PRINTABLE_STRING => 'PrintableString', |
|
| 138 | - self::TYPE_T61_STRING => 'T61String', |
|
| 139 | - self::TYPE_VIDEOTEX_STRING => 'VideotexString', |
|
| 140 | - self::TYPE_IA5_STRING => 'IA5String', |
|
| 141 | - self::TYPE_UTC_TIME => 'UTCTime', |
|
| 142 | - self::TYPE_GENERALIZED_TIME => 'GeneralizedTime', |
|
| 143 | - self::TYPE_GRAPHIC_STRING => 'GraphicString', |
|
| 144 | - self::TYPE_VISIBLE_STRING => 'VisibleString', |
|
| 145 | - self::TYPE_GENERAL_STRING => 'GeneralString', |
|
| 146 | - self::TYPE_UNIVERSAL_STRING => 'UniversalString', |
|
| 147 | - self::TYPE_CHARACTER_STRING => 'CHARACTER STRING', |
|
| 148 | - self::TYPE_BMP_STRING => 'BMPString', |
|
| 149 | - self::TYPE_STRING => 'Any String', |
|
| 150 | - self::TYPE_TIME => 'Any Time', |
|
| 151 | - ]; |
|
| 112 | + /** |
|
| 113 | + * Mapping from universal type tag to human readable name. |
|
| 114 | + * |
|
| 115 | + * @internal |
|
| 116 | + * |
|
| 117 | + * @var array |
|
| 118 | + */ |
|
| 119 | + const MAP_TYPE_TO_NAME = [ |
|
| 120 | + self::TYPE_EOC => 'EOC', |
|
| 121 | + self::TYPE_BOOLEAN => 'BOOLEAN', |
|
| 122 | + self::TYPE_INTEGER => 'INTEGER', |
|
| 123 | + self::TYPE_BIT_STRING => 'BIT STRING', |
|
| 124 | + self::TYPE_OCTET_STRING => 'OCTET STRING', |
|
| 125 | + self::TYPE_NULL => 'NULL', |
|
| 126 | + self::TYPE_OBJECT_IDENTIFIER => 'OBJECT IDENTIFIER', |
|
| 127 | + self::TYPE_OBJECT_DESCRIPTOR => 'ObjectDescriptor', |
|
| 128 | + self::TYPE_EXTERNAL => 'EXTERNAL', |
|
| 129 | + self::TYPE_REAL => 'REAL', |
|
| 130 | + self::TYPE_ENUMERATED => 'ENUMERATED', |
|
| 131 | + self::TYPE_EMBEDDED_PDV => 'EMBEDDED PDV', |
|
| 132 | + self::TYPE_UTF8_STRING => 'UTF8String', |
|
| 133 | + self::TYPE_RELATIVE_OID => 'RELATIVE-OID', |
|
| 134 | + self::TYPE_SEQUENCE => 'SEQUENCE', |
|
| 135 | + self::TYPE_SET => 'SET', |
|
| 136 | + self::TYPE_NUMERIC_STRING => 'NumericString', |
|
| 137 | + self::TYPE_PRINTABLE_STRING => 'PrintableString', |
|
| 138 | + self::TYPE_T61_STRING => 'T61String', |
|
| 139 | + self::TYPE_VIDEOTEX_STRING => 'VideotexString', |
|
| 140 | + self::TYPE_IA5_STRING => 'IA5String', |
|
| 141 | + self::TYPE_UTC_TIME => 'UTCTime', |
|
| 142 | + self::TYPE_GENERALIZED_TIME => 'GeneralizedTime', |
|
| 143 | + self::TYPE_GRAPHIC_STRING => 'GraphicString', |
|
| 144 | + self::TYPE_VISIBLE_STRING => 'VisibleString', |
|
| 145 | + self::TYPE_GENERAL_STRING => 'GeneralString', |
|
| 146 | + self::TYPE_UNIVERSAL_STRING => 'UniversalString', |
|
| 147 | + self::TYPE_CHARACTER_STRING => 'CHARACTER STRING', |
|
| 148 | + self::TYPE_BMP_STRING => 'BMPString', |
|
| 149 | + self::TYPE_STRING => 'Any String', |
|
| 150 | + self::TYPE_TIME => 'Any Time', |
|
| 151 | + ]; |
|
| 152 | 152 | |
| 153 | - /** |
|
| 154 | - * Element's type tag. |
|
| 155 | - * |
|
| 156 | - * @var int |
|
| 157 | - */ |
|
| 158 | - protected $_typeTag; |
|
| 153 | + /** |
|
| 154 | + * Element's type tag. |
|
| 155 | + * |
|
| 156 | + * @var int |
|
| 157 | + */ |
|
| 158 | + protected $_typeTag; |
|
| 159 | 159 | |
| 160 | - /** |
|
| 161 | - * Whether type shall be encoded with indefinite length. |
|
| 162 | - * |
|
| 163 | - * @var bool |
|
| 164 | - */ |
|
| 165 | - protected $_indefiniteLength = false; |
|
| 160 | + /** |
|
| 161 | + * Whether type shall be encoded with indefinite length. |
|
| 162 | + * |
|
| 163 | + * @var bool |
|
| 164 | + */ |
|
| 165 | + protected $_indefiniteLength = false; |
|
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * {@inheritdoc} |
|
| 169 | - */ |
|
| 170 | - abstract public function typeClass(): int; |
|
| 167 | + /** |
|
| 168 | + * {@inheritdoc} |
|
| 169 | + */ |
|
| 170 | + abstract public function typeClass(): int; |
|
| 171 | 171 | |
| 172 | - /** |
|
| 173 | - * {@inheritdoc} |
|
| 174 | - */ |
|
| 175 | - abstract public function isConstructed(): bool; |
|
| 172 | + /** |
|
| 173 | + * {@inheritdoc} |
|
| 174 | + */ |
|
| 175 | + abstract public function isConstructed(): bool; |
|
| 176 | 176 | |
| 177 | - /** |
|
| 178 | - * Decode element from DER data. |
|
| 179 | - * |
|
| 180 | - * @param string $data DER encoded data |
|
| 181 | - * @param null|int $offset Reference to the variable that contains offset |
|
| 182 | - * into the data where to start parsing. |
|
| 183 | - * Variable is updated to the offset next to the |
|
| 184 | - * parsed element. If null, start from offset 0. |
|
| 185 | - * |
|
| 186 | - * @throws DecodeException If decoding fails |
|
| 187 | - * @throws \UnexpectedValueException If called in the context of an expected |
|
| 188 | - * type, but decoding yields another type |
|
| 189 | - * |
|
| 190 | - * @return ElementBase |
|
| 191 | - */ |
|
| 192 | - public static function fromDER(string $data, int &$offset = null): ElementBase |
|
| 193 | - { |
|
| 194 | - $idx = $offset ?? 0; |
|
| 195 | - // decode identifier |
|
| 196 | - $identifier = Identifier::fromDER($data, $idx); |
|
| 197 | - // determine class that implements type specific decoding |
|
| 198 | - $cls = self::_determineImplClass($identifier); |
|
| 199 | - try { |
|
| 200 | - // decode remaining element |
|
| 201 | - $element = $cls::_decodeFromDER($identifier, $data, $idx); |
|
| 202 | - } catch (\LogicException $e) { |
|
| 203 | - // rethrow as a RuntimeException for unified exception handling |
|
| 204 | - throw new DecodeException( |
|
| 205 | - sprintf('Error while decoding %s.', |
|
| 206 | - self::tagToName($identifier->intTag())), 0, $e); |
|
| 207 | - } |
|
| 208 | - // if called in the context of a concrete class, check |
|
| 209 | - // that decoded type matches the type of a calling class |
|
| 210 | - $called_class = get_called_class(); |
|
| 211 | - if (self::class != $called_class) { |
|
| 212 | - if (!$element instanceof $called_class) { |
|
| 213 | - throw new \UnexpectedValueException( |
|
| 214 | - sprintf('%s expected, got %s.', $called_class, |
|
| 215 | - get_class($element))); |
|
| 216 | - } |
|
| 217 | - } |
|
| 218 | - // update offset for the caller |
|
| 219 | - if (isset($offset)) { |
|
| 220 | - $offset = $idx; |
|
| 221 | - } |
|
| 222 | - return $element; |
|
| 223 | - } |
|
| 177 | + /** |
|
| 178 | + * Decode element from DER data. |
|
| 179 | + * |
|
| 180 | + * @param string $data DER encoded data |
|
| 181 | + * @param null|int $offset Reference to the variable that contains offset |
|
| 182 | + * into the data where to start parsing. |
|
| 183 | + * Variable is updated to the offset next to the |
|
| 184 | + * parsed element. If null, start from offset 0. |
|
| 185 | + * |
|
| 186 | + * @throws DecodeException If decoding fails |
|
| 187 | + * @throws \UnexpectedValueException If called in the context of an expected |
|
| 188 | + * type, but decoding yields another type |
|
| 189 | + * |
|
| 190 | + * @return ElementBase |
|
| 191 | + */ |
|
| 192 | + public static function fromDER(string $data, int &$offset = null): ElementBase |
|
| 193 | + { |
|
| 194 | + $idx = $offset ?? 0; |
|
| 195 | + // decode identifier |
|
| 196 | + $identifier = Identifier::fromDER($data, $idx); |
|
| 197 | + // determine class that implements type specific decoding |
|
| 198 | + $cls = self::_determineImplClass($identifier); |
|
| 199 | + try { |
|
| 200 | + // decode remaining element |
|
| 201 | + $element = $cls::_decodeFromDER($identifier, $data, $idx); |
|
| 202 | + } catch (\LogicException $e) { |
|
| 203 | + // rethrow as a RuntimeException for unified exception handling |
|
| 204 | + throw new DecodeException( |
|
| 205 | + sprintf('Error while decoding %s.', |
|
| 206 | + self::tagToName($identifier->intTag())), 0, $e); |
|
| 207 | + } |
|
| 208 | + // if called in the context of a concrete class, check |
|
| 209 | + // that decoded type matches the type of a calling class |
|
| 210 | + $called_class = get_called_class(); |
|
| 211 | + if (self::class != $called_class) { |
|
| 212 | + if (!$element instanceof $called_class) { |
|
| 213 | + throw new \UnexpectedValueException( |
|
| 214 | + sprintf('%s expected, got %s.', $called_class, |
|
| 215 | + get_class($element))); |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | + // update offset for the caller |
|
| 219 | + if (isset($offset)) { |
|
| 220 | + $offset = $idx; |
|
| 221 | + } |
|
| 222 | + return $element; |
|
| 223 | + } |
|
| 224 | 224 | |
| 225 | - /** |
|
| 226 | - * {@inheritdoc} |
|
| 227 | - */ |
|
| 228 | - public function toDER(): string |
|
| 229 | - { |
|
| 230 | - $identifier = new Identifier($this->typeClass(), |
|
| 231 | - $this->isConstructed() ? Identifier::CONSTRUCTED : Identifier::PRIMITIVE, |
|
| 232 | - $this->_typeTag); |
|
| 233 | - $content = $this->_encodedContentDER(); |
|
| 234 | - if ($this->_indefiniteLength) { |
|
| 235 | - $length = new Length(0, true); |
|
| 236 | - $eoc = new Primitive\EOC(); |
|
| 237 | - return $identifier->toDER() . $length->toDER() . $content . |
|
| 238 | - $eoc->toDER(); |
|
| 239 | - } |
|
| 240 | - $length = new Length(strlen($content)); |
|
| 241 | - return $identifier->toDER() . $length->toDER() . $content; |
|
| 242 | - } |
|
| 225 | + /** |
|
| 226 | + * {@inheritdoc} |
|
| 227 | + */ |
|
| 228 | + public function toDER(): string |
|
| 229 | + { |
|
| 230 | + $identifier = new Identifier($this->typeClass(), |
|
| 231 | + $this->isConstructed() ? Identifier::CONSTRUCTED : Identifier::PRIMITIVE, |
|
| 232 | + $this->_typeTag); |
|
| 233 | + $content = $this->_encodedContentDER(); |
|
| 234 | + if ($this->_indefiniteLength) { |
|
| 235 | + $length = new Length(0, true); |
|
| 236 | + $eoc = new Primitive\EOC(); |
|
| 237 | + return $identifier->toDER() . $length->toDER() . $content . |
|
| 238 | + $eoc->toDER(); |
|
| 239 | + } |
|
| 240 | + $length = new Length(strlen($content)); |
|
| 241 | + return $identifier->toDER() . $length->toDER() . $content; |
|
| 242 | + } |
|
| 243 | 243 | |
| 244 | - /** |
|
| 245 | - * {@inheritdoc} |
|
| 246 | - */ |
|
| 247 | - public function tag(): int |
|
| 248 | - { |
|
| 249 | - return $this->_typeTag; |
|
| 250 | - } |
|
| 244 | + /** |
|
| 245 | + * {@inheritdoc} |
|
| 246 | + */ |
|
| 247 | + public function tag(): int |
|
| 248 | + { |
|
| 249 | + return $this->_typeTag; |
|
| 250 | + } |
|
| 251 | 251 | |
| 252 | - /** |
|
| 253 | - * {@inheritdoc} |
|
| 254 | - */ |
|
| 255 | - public function isType(int $tag): bool |
|
| 256 | - { |
|
| 257 | - // if element is context specific |
|
| 258 | - if (Identifier::CLASS_CONTEXT_SPECIFIC === $this->typeClass()) { |
|
| 259 | - return false; |
|
| 260 | - } |
|
| 261 | - // negative tags identify an abstract pseudotype |
|
| 262 | - if ($tag < 0) { |
|
| 263 | - return $this->_isPseudoType($tag); |
|
| 264 | - } |
|
| 265 | - return $this->_isConcreteType($tag); |
|
| 266 | - } |
|
| 252 | + /** |
|
| 253 | + * {@inheritdoc} |
|
| 254 | + */ |
|
| 255 | + public function isType(int $tag): bool |
|
| 256 | + { |
|
| 257 | + // if element is context specific |
|
| 258 | + if (Identifier::CLASS_CONTEXT_SPECIFIC === $this->typeClass()) { |
|
| 259 | + return false; |
|
| 260 | + } |
|
| 261 | + // negative tags identify an abstract pseudotype |
|
| 262 | + if ($tag < 0) { |
|
| 263 | + return $this->_isPseudoType($tag); |
|
| 264 | + } |
|
| 265 | + return $this->_isConcreteType($tag); |
|
| 266 | + } |
|
| 267 | 267 | |
| 268 | - /** |
|
| 269 | - * {@inheritdoc} |
|
| 270 | - */ |
|
| 271 | - public function expectType(int $tag): ElementBase |
|
| 272 | - { |
|
| 273 | - if (!$this->isType($tag)) { |
|
| 274 | - throw new \UnexpectedValueException( |
|
| 275 | - sprintf('%s expected, got %s.', self::tagToName($tag), |
|
| 276 | - $this->_typeDescriptorString())); |
|
| 277 | - } |
|
| 278 | - return $this; |
|
| 279 | - } |
|
| 268 | + /** |
|
| 269 | + * {@inheritdoc} |
|
| 270 | + */ |
|
| 271 | + public function expectType(int $tag): ElementBase |
|
| 272 | + { |
|
| 273 | + if (!$this->isType($tag)) { |
|
| 274 | + throw new \UnexpectedValueException( |
|
| 275 | + sprintf('%s expected, got %s.', self::tagToName($tag), |
|
| 276 | + $this->_typeDescriptorString())); |
|
| 277 | + } |
|
| 278 | + return $this; |
|
| 279 | + } |
|
| 280 | 280 | |
| 281 | - /** |
|
| 282 | - * {@inheritdoc} |
|
| 283 | - */ |
|
| 284 | - public function isTagged(): bool |
|
| 285 | - { |
|
| 286 | - return $this instanceof TaggedType; |
|
| 287 | - } |
|
| 281 | + /** |
|
| 282 | + * {@inheritdoc} |
|
| 283 | + */ |
|
| 284 | + public function isTagged(): bool |
|
| 285 | + { |
|
| 286 | + return $this instanceof TaggedType; |
|
| 287 | + } |
|
| 288 | 288 | |
| 289 | - /** |
|
| 290 | - * {@inheritdoc} |
|
| 291 | - */ |
|
| 292 | - public function expectTagged(?int $tag = null): TaggedType |
|
| 293 | - { |
|
| 294 | - if (!$this->isTagged()) { |
|
| 295 | - throw new \UnexpectedValueException( |
|
| 296 | - sprintf('Context specific element expected, got %s.', |
|
| 297 | - Identifier::classToName($this->typeClass()))); |
|
| 298 | - } |
|
| 299 | - if (isset($tag) && $this->tag() !== $tag) { |
|
| 300 | - throw new \UnexpectedValueException( |
|
| 301 | - sprintf('Tag %d expected, got %d.', $tag, $this->tag())); |
|
| 302 | - } |
|
| 303 | - return $this; |
|
| 304 | - } |
|
| 289 | + /** |
|
| 290 | + * {@inheritdoc} |
|
| 291 | + */ |
|
| 292 | + public function expectTagged(?int $tag = null): TaggedType |
|
| 293 | + { |
|
| 294 | + if (!$this->isTagged()) { |
|
| 295 | + throw new \UnexpectedValueException( |
|
| 296 | + sprintf('Context specific element expected, got %s.', |
|
| 297 | + Identifier::classToName($this->typeClass()))); |
|
| 298 | + } |
|
| 299 | + if (isset($tag) && $this->tag() !== $tag) { |
|
| 300 | + throw new \UnexpectedValueException( |
|
| 301 | + sprintf('Tag %d expected, got %d.', $tag, $this->tag())); |
|
| 302 | + } |
|
| 303 | + return $this; |
|
| 304 | + } |
|
| 305 | 305 | |
| 306 | - /** |
|
| 307 | - * Whether element has indefinite length. |
|
| 308 | - * |
|
| 309 | - * @return bool |
|
| 310 | - */ |
|
| 311 | - public function hasIndefiniteLength(): bool |
|
| 312 | - { |
|
| 313 | - return $this->_indefiniteLength; |
|
| 314 | - } |
|
| 306 | + /** |
|
| 307 | + * Whether element has indefinite length. |
|
| 308 | + * |
|
| 309 | + * @return bool |
|
| 310 | + */ |
|
| 311 | + public function hasIndefiniteLength(): bool |
|
| 312 | + { |
|
| 313 | + return $this->_indefiniteLength; |
|
| 314 | + } |
|
| 315 | 315 | |
| 316 | - /** |
|
| 317 | - * Get self with indefinite length encoding set. |
|
| 318 | - * |
|
| 319 | - * @param bool $indefinite True for indefinite length, false for definite |
|
| 320 | - * length |
|
| 321 | - * |
|
| 322 | - * @return self |
|
| 323 | - */ |
|
| 324 | - public function withIndefiniteLength(bool $indefinite = true): self |
|
| 325 | - { |
|
| 326 | - $obj = clone $this; |
|
| 327 | - $obj->_indefiniteLength = $indefinite; |
|
| 328 | - return $obj; |
|
| 329 | - } |
|
| 316 | + /** |
|
| 317 | + * Get self with indefinite length encoding set. |
|
| 318 | + * |
|
| 319 | + * @param bool $indefinite True for indefinite length, false for definite |
|
| 320 | + * length |
|
| 321 | + * |
|
| 322 | + * @return self |
|
| 323 | + */ |
|
| 324 | + public function withIndefiniteLength(bool $indefinite = true): self |
|
| 325 | + { |
|
| 326 | + $obj = clone $this; |
|
| 327 | + $obj->_indefiniteLength = $indefinite; |
|
| 328 | + return $obj; |
|
| 329 | + } |
|
| 330 | 330 | |
| 331 | - /** |
|
| 332 | - * {@inheritdoc} |
|
| 333 | - */ |
|
| 334 | - final public function asElement(): Element |
|
| 335 | - { |
|
| 336 | - return $this; |
|
| 337 | - } |
|
| 331 | + /** |
|
| 332 | + * {@inheritdoc} |
|
| 333 | + */ |
|
| 334 | + final public function asElement(): Element |
|
| 335 | + { |
|
| 336 | + return $this; |
|
| 337 | + } |
|
| 338 | 338 | |
| 339 | - /** |
|
| 340 | - * Get element decorated with UnspecifiedType object. |
|
| 341 | - * |
|
| 342 | - * @return UnspecifiedType |
|
| 343 | - */ |
|
| 344 | - public function asUnspecified(): UnspecifiedType |
|
| 345 | - { |
|
| 346 | - return new UnspecifiedType($this); |
|
| 347 | - } |
|
| 339 | + /** |
|
| 340 | + * Get element decorated with UnspecifiedType object. |
|
| 341 | + * |
|
| 342 | + * @return UnspecifiedType |
|
| 343 | + */ |
|
| 344 | + public function asUnspecified(): UnspecifiedType |
|
| 345 | + { |
|
| 346 | + return new UnspecifiedType($this); |
|
| 347 | + } |
|
| 348 | 348 | |
| 349 | - /** |
|
| 350 | - * Get human readable name for an universal tag. |
|
| 351 | - * |
|
| 352 | - * @param int $tag |
|
| 353 | - * |
|
| 354 | - * @return string |
|
| 355 | - */ |
|
| 356 | - public static function tagToName(int $tag): string |
|
| 357 | - { |
|
| 358 | - if (!array_key_exists($tag, self::MAP_TYPE_TO_NAME)) { |
|
| 359 | - return "TAG {$tag}"; |
|
| 360 | - } |
|
| 361 | - return self::MAP_TYPE_TO_NAME[$tag]; |
|
| 362 | - } |
|
| 349 | + /** |
|
| 350 | + * Get human readable name for an universal tag. |
|
| 351 | + * |
|
| 352 | + * @param int $tag |
|
| 353 | + * |
|
| 354 | + * @return string |
|
| 355 | + */ |
|
| 356 | + public static function tagToName(int $tag): string |
|
| 357 | + { |
|
| 358 | + if (!array_key_exists($tag, self::MAP_TYPE_TO_NAME)) { |
|
| 359 | + return "TAG {$tag}"; |
|
| 360 | + } |
|
| 361 | + return self::MAP_TYPE_TO_NAME[$tag]; |
|
| 362 | + } |
|
| 363 | 363 | |
| 364 | - /** |
|
| 365 | - * Get the content encoded in DER. |
|
| 366 | - * |
|
| 367 | - * Returns the DER encoded content without identifier and length header |
|
| 368 | - * octets. |
|
| 369 | - * |
|
| 370 | - * @return string |
|
| 371 | - */ |
|
| 372 | - abstract protected function _encodedContentDER(): string; |
|
| 364 | + /** |
|
| 365 | + * Get the content encoded in DER. |
|
| 366 | + * |
|
| 367 | + * Returns the DER encoded content without identifier and length header |
|
| 368 | + * octets. |
|
| 369 | + * |
|
| 370 | + * @return string |
|
| 371 | + */ |
|
| 372 | + abstract protected function _encodedContentDER(): string; |
|
| 373 | 373 | |
| 374 | - /** |
|
| 375 | - * Decode type-specific element from DER. |
|
| 376 | - * |
|
| 377 | - * @param Identifier $identifier Pre-parsed identifier |
|
| 378 | - * @param string $data DER data |
|
| 379 | - * @param int $offset Offset in data to the next byte after identifier |
|
| 380 | - * |
|
| 381 | - * @throws DecodeException If decoding fails |
|
| 382 | - * |
|
| 383 | - * @return ElementBase |
|
| 384 | - */ |
|
| 385 | - protected static function _decodeFromDER(Identifier $identifier, |
|
| 386 | - string $data, int &$offset): ElementBase |
|
| 387 | - { |
|
| 388 | - throw new \BadMethodCallException( |
|
| 389 | - __METHOD__ . ' must be implemented in derived class.'); |
|
| 390 | - } |
|
| 374 | + /** |
|
| 375 | + * Decode type-specific element from DER. |
|
| 376 | + * |
|
| 377 | + * @param Identifier $identifier Pre-parsed identifier |
|
| 378 | + * @param string $data DER data |
|
| 379 | + * @param int $offset Offset in data to the next byte after identifier |
|
| 380 | + * |
|
| 381 | + * @throws DecodeException If decoding fails |
|
| 382 | + * |
|
| 383 | + * @return ElementBase |
|
| 384 | + */ |
|
| 385 | + protected static function _decodeFromDER(Identifier $identifier, |
|
| 386 | + string $data, int &$offset): ElementBase |
|
| 387 | + { |
|
| 388 | + throw new \BadMethodCallException( |
|
| 389 | + __METHOD__ . ' must be implemented in derived class.'); |
|
| 390 | + } |
|
| 391 | 391 | |
| 392 | - /** |
|
| 393 | - * Determine the class that implements the type. |
|
| 394 | - * |
|
| 395 | - * @param Identifier $identifier |
|
| 396 | - * |
|
| 397 | - * @return string Class name |
|
| 398 | - */ |
|
| 399 | - protected static function _determineImplClass(Identifier $identifier): string |
|
| 400 | - { |
|
| 401 | - switch ($identifier->typeClass()) { |
|
| 402 | - case Identifier::CLASS_UNIVERSAL: |
|
| 403 | - return self::_determineUniversalImplClass($identifier->intTag()); |
|
| 404 | - case Identifier::CLASS_CONTEXT_SPECIFIC: |
|
| 405 | - return ContextSpecificType::class; |
|
| 406 | - case Identifier::CLASS_APPLICATION: |
|
| 407 | - return ApplicationType::class; |
|
| 408 | - case Identifier::CLASS_PRIVATE: |
|
| 409 | - return PrivateType::class; |
|
| 410 | - } |
|
| 411 | - throw new \UnexpectedValueException( |
|
| 412 | - sprintf('%s %d not implemented.', |
|
| 413 | - Identifier::classToName($identifier->typeClass()), |
|
| 414 | - $identifier->tag())); |
|
| 415 | - } |
|
| 392 | + /** |
|
| 393 | + * Determine the class that implements the type. |
|
| 394 | + * |
|
| 395 | + * @param Identifier $identifier |
|
| 396 | + * |
|
| 397 | + * @return string Class name |
|
| 398 | + */ |
|
| 399 | + protected static function _determineImplClass(Identifier $identifier): string |
|
| 400 | + { |
|
| 401 | + switch ($identifier->typeClass()) { |
|
| 402 | + case Identifier::CLASS_UNIVERSAL: |
|
| 403 | + return self::_determineUniversalImplClass($identifier->intTag()); |
|
| 404 | + case Identifier::CLASS_CONTEXT_SPECIFIC: |
|
| 405 | + return ContextSpecificType::class; |
|
| 406 | + case Identifier::CLASS_APPLICATION: |
|
| 407 | + return ApplicationType::class; |
|
| 408 | + case Identifier::CLASS_PRIVATE: |
|
| 409 | + return PrivateType::class; |
|
| 410 | + } |
|
| 411 | + throw new \UnexpectedValueException( |
|
| 412 | + sprintf('%s %d not implemented.', |
|
| 413 | + Identifier::classToName($identifier->typeClass()), |
|
| 414 | + $identifier->tag())); |
|
| 415 | + } |
|
| 416 | 416 | |
| 417 | - /** |
|
| 418 | - * Determine the class that implements an universal type of the given tag. |
|
| 419 | - * |
|
| 420 | - * @param int $tag |
|
| 421 | - * |
|
| 422 | - * @throws \UnexpectedValueException |
|
| 423 | - * |
|
| 424 | - * @return string Class name |
|
| 425 | - */ |
|
| 426 | - protected static function _determineUniversalImplClass(int $tag): string |
|
| 427 | - { |
|
| 428 | - if (!array_key_exists($tag, self::MAP_TAG_TO_CLASS)) { |
|
| 429 | - throw new \UnexpectedValueException( |
|
| 430 | - "Universal tag {$tag} not implemented."); |
|
| 431 | - } |
|
| 432 | - return self::MAP_TAG_TO_CLASS[$tag]; |
|
| 433 | - } |
|
| 417 | + /** |
|
| 418 | + * Determine the class that implements an universal type of the given tag. |
|
| 419 | + * |
|
| 420 | + * @param int $tag |
|
| 421 | + * |
|
| 422 | + * @throws \UnexpectedValueException |
|
| 423 | + * |
|
| 424 | + * @return string Class name |
|
| 425 | + */ |
|
| 426 | + protected static function _determineUniversalImplClass(int $tag): string |
|
| 427 | + { |
|
| 428 | + if (!array_key_exists($tag, self::MAP_TAG_TO_CLASS)) { |
|
| 429 | + throw new \UnexpectedValueException( |
|
| 430 | + "Universal tag {$tag} not implemented."); |
|
| 431 | + } |
|
| 432 | + return self::MAP_TAG_TO_CLASS[$tag]; |
|
| 433 | + } |
|
| 434 | 434 | |
| 435 | - /** |
|
| 436 | - * Get textual description of the type for debugging purposes. |
|
| 437 | - * |
|
| 438 | - * @return string |
|
| 439 | - */ |
|
| 440 | - protected function _typeDescriptorString(): string |
|
| 441 | - { |
|
| 442 | - if (Identifier::CLASS_UNIVERSAL == $this->typeClass()) { |
|
| 443 | - return self::tagToName($this->_typeTag); |
|
| 444 | - } |
|
| 445 | - return sprintf('%s TAG %d', Identifier::classToName($this->typeClass()), |
|
| 446 | - $this->_typeTag); |
|
| 447 | - } |
|
| 435 | + /** |
|
| 436 | + * Get textual description of the type for debugging purposes. |
|
| 437 | + * |
|
| 438 | + * @return string |
|
| 439 | + */ |
|
| 440 | + protected function _typeDescriptorString(): string |
|
| 441 | + { |
|
| 442 | + if (Identifier::CLASS_UNIVERSAL == $this->typeClass()) { |
|
| 443 | + return self::tagToName($this->_typeTag); |
|
| 444 | + } |
|
| 445 | + return sprintf('%s TAG %d', Identifier::classToName($this->typeClass()), |
|
| 446 | + $this->_typeTag); |
|
| 447 | + } |
|
| 448 | 448 | |
| 449 | - /** |
|
| 450 | - * Check whether the element is a concrete type of a given tag. |
|
| 451 | - * |
|
| 452 | - * @param int $tag |
|
| 453 | - * |
|
| 454 | - * @return bool |
|
| 455 | - */ |
|
| 456 | - private function _isConcreteType(int $tag): bool |
|
| 457 | - { |
|
| 458 | - // if tag doesn't match |
|
| 459 | - if ($this->tag() != $tag) { |
|
| 460 | - return false; |
|
| 461 | - } |
|
| 462 | - // if type is universal check that instance is of a correct class |
|
| 463 | - if (Identifier::CLASS_UNIVERSAL == $this->typeClass()) { |
|
| 464 | - $cls = self::_determineUniversalImplClass($tag); |
|
| 465 | - if (!$this instanceof $cls) { |
|
| 466 | - return false; |
|
| 467 | - } |
|
| 468 | - } |
|
| 469 | - return true; |
|
| 470 | - } |
|
| 449 | + /** |
|
| 450 | + * Check whether the element is a concrete type of a given tag. |
|
| 451 | + * |
|
| 452 | + * @param int $tag |
|
| 453 | + * |
|
| 454 | + * @return bool |
|
| 455 | + */ |
|
| 456 | + private function _isConcreteType(int $tag): bool |
|
| 457 | + { |
|
| 458 | + // if tag doesn't match |
|
| 459 | + if ($this->tag() != $tag) { |
|
| 460 | + return false; |
|
| 461 | + } |
|
| 462 | + // if type is universal check that instance is of a correct class |
|
| 463 | + if (Identifier::CLASS_UNIVERSAL == $this->typeClass()) { |
|
| 464 | + $cls = self::_determineUniversalImplClass($tag); |
|
| 465 | + if (!$this instanceof $cls) { |
|
| 466 | + return false; |
|
| 467 | + } |
|
| 468 | + } |
|
| 469 | + return true; |
|
| 470 | + } |
|
| 471 | 471 | |
| 472 | - /** |
|
| 473 | - * Check whether the element is a pseudotype. |
|
| 474 | - * |
|
| 475 | - * @param int $tag |
|
| 476 | - * |
|
| 477 | - * @return bool |
|
| 478 | - */ |
|
| 479 | - private function _isPseudoType(int $tag): bool |
|
| 480 | - { |
|
| 481 | - switch ($tag) { |
|
| 482 | - case self::TYPE_STRING: |
|
| 483 | - return $this instanceof StringType; |
|
| 484 | - case self::TYPE_TIME: |
|
| 485 | - return $this instanceof TimeType; |
|
| 486 | - } |
|
| 487 | - return false; |
|
| 488 | - } |
|
| 472 | + /** |
|
| 473 | + * Check whether the element is a pseudotype. |
|
| 474 | + * |
|
| 475 | + * @param int $tag |
|
| 476 | + * |
|
| 477 | + * @return bool |
|
| 478 | + */ |
|
| 479 | + private function _isPseudoType(int $tag): bool |
|
| 480 | + { |
|
| 481 | + switch ($tag) { |
|
| 482 | + case self::TYPE_STRING: |
|
| 483 | + return $this instanceof StringType; |
|
| 484 | + case self::TYPE_TIME: |
|
| 485 | + return $this instanceof TimeType; |
|
| 486 | + } |
|
| 487 | + return false; |
|
| 488 | + } |
|
| 489 | 489 | } |
@@ -13,301 +13,301 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class Identifier implements Encodable |
| 15 | 15 | { |
| 16 | - // Type class enumerations |
|
| 17 | - const CLASS_UNIVERSAL = 0b00; |
|
| 18 | - const CLASS_APPLICATION = 0b01; |
|
| 19 | - const CLASS_CONTEXT_SPECIFIC = 0b10; |
|
| 20 | - const CLASS_PRIVATE = 0b11; |
|
| 16 | + // Type class enumerations |
|
| 17 | + const CLASS_UNIVERSAL = 0b00; |
|
| 18 | + const CLASS_APPLICATION = 0b01; |
|
| 19 | + const CLASS_CONTEXT_SPECIFIC = 0b10; |
|
| 20 | + const CLASS_PRIVATE = 0b11; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Mapping from type class to human readable name. |
|
| 24 | - * |
|
| 25 | - * @internal |
|
| 26 | - * |
|
| 27 | - * @var array |
|
| 28 | - */ |
|
| 29 | - const MAP_CLASS_TO_NAME = [ |
|
| 30 | - self::CLASS_UNIVERSAL => 'UNIVERSAL', |
|
| 31 | - self::CLASS_APPLICATION => 'APPLICATION', |
|
| 32 | - self::CLASS_CONTEXT_SPECIFIC => 'CONTEXT SPECIFIC', |
|
| 33 | - self::CLASS_PRIVATE => 'PRIVATE', |
|
| 34 | - ]; |
|
| 22 | + /** |
|
| 23 | + * Mapping from type class to human readable name. |
|
| 24 | + * |
|
| 25 | + * @internal |
|
| 26 | + * |
|
| 27 | + * @var array |
|
| 28 | + */ |
|
| 29 | + const MAP_CLASS_TO_NAME = [ |
|
| 30 | + self::CLASS_UNIVERSAL => 'UNIVERSAL', |
|
| 31 | + self::CLASS_APPLICATION => 'APPLICATION', |
|
| 32 | + self::CLASS_CONTEXT_SPECIFIC => 'CONTEXT SPECIFIC', |
|
| 33 | + self::CLASS_PRIVATE => 'PRIVATE', |
|
| 34 | + ]; |
|
| 35 | 35 | |
| 36 | - // P/C enumerations |
|
| 37 | - const PRIMITIVE = 0b0; |
|
| 38 | - const CONSTRUCTED = 0b1; |
|
| 36 | + // P/C enumerations |
|
| 37 | + const PRIMITIVE = 0b0; |
|
| 38 | + const CONSTRUCTED = 0b1; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Type class. |
|
| 42 | - * |
|
| 43 | - * @var int |
|
| 44 | - */ |
|
| 45 | - private $_class; |
|
| 40 | + /** |
|
| 41 | + * Type class. |
|
| 42 | + * |
|
| 43 | + * @var int |
|
| 44 | + */ |
|
| 45 | + private $_class; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Primitive or Constructed. |
|
| 49 | - * |
|
| 50 | - * @var int |
|
| 51 | - */ |
|
| 52 | - private $_pc; |
|
| 47 | + /** |
|
| 48 | + * Primitive or Constructed. |
|
| 49 | + * |
|
| 50 | + * @var int |
|
| 51 | + */ |
|
| 52 | + private $_pc; |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Content type tag. |
|
| 56 | - * |
|
| 57 | - * @var BigInt |
|
| 58 | - */ |
|
| 59 | - private $_tag; |
|
| 54 | + /** |
|
| 55 | + * Content type tag. |
|
| 56 | + * |
|
| 57 | + * @var BigInt |
|
| 58 | + */ |
|
| 59 | + private $_tag; |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Constructor. |
|
| 63 | - * |
|
| 64 | - * @param int $class Type class |
|
| 65 | - * @param int $pc Primitive / Constructed |
|
| 66 | - * @param int|string $tag Type tag number |
|
| 67 | - */ |
|
| 68 | - public function __construct(int $class, int $pc, $tag) |
|
| 69 | - { |
|
| 70 | - $this->_class = 0b11 & $class; |
|
| 71 | - $this->_pc = 0b1 & $pc; |
|
| 72 | - $this->_tag = new BigInt($tag); |
|
| 73 | - } |
|
| 61 | + /** |
|
| 62 | + * Constructor. |
|
| 63 | + * |
|
| 64 | + * @param int $class Type class |
|
| 65 | + * @param int $pc Primitive / Constructed |
|
| 66 | + * @param int|string $tag Type tag number |
|
| 67 | + */ |
|
| 68 | + public function __construct(int $class, int $pc, $tag) |
|
| 69 | + { |
|
| 70 | + $this->_class = 0b11 & $class; |
|
| 71 | + $this->_pc = 0b1 & $pc; |
|
| 72 | + $this->_tag = new BigInt($tag); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * Decode identifier component from DER data. |
|
| 77 | - * |
|
| 78 | - * @param string $data DER encoded data |
|
| 79 | - * @param null|int $offset Reference to the variable that contains offset |
|
| 80 | - * into the data where to start parsing. |
|
| 81 | - * Variable is updated to the offset next to the |
|
| 82 | - * parsed identifier. If null, start from offset 0. |
|
| 83 | - * |
|
| 84 | - * @throws DecodeException If decoding fails |
|
| 85 | - * |
|
| 86 | - * @return self |
|
| 87 | - */ |
|
| 88 | - public static function fromDER(string $data, int &$offset = null): Identifier |
|
| 89 | - { |
|
| 90 | - $idx = $offset ?? 0; |
|
| 91 | - $datalen = strlen($data); |
|
| 92 | - if ($idx >= $datalen) { |
|
| 93 | - throw new DecodeException('Invalid offset.'); |
|
| 94 | - } |
|
| 95 | - $byte = ord($data[$idx++]); |
|
| 96 | - // bits 8 and 7 (class) |
|
| 97 | - // 0 = universal, 1 = application, 2 = context-specific, 3 = private |
|
| 98 | - $class = (0b11000000 & $byte) >> 6; |
|
| 99 | - // bit 6 (0 = primitive / 1 = constructed) |
|
| 100 | - $pc = (0b00100000 & $byte) >> 5; |
|
| 101 | - // bits 5 to 1 (tag number) |
|
| 102 | - $tag = (0b00011111 & $byte); |
|
| 103 | - // long-form identifier |
|
| 104 | - if (0x1f == $tag) { |
|
| 105 | - $tag = self::_decodeLongFormTag($data, $idx); |
|
| 106 | - } |
|
| 107 | - if (isset($offset)) { |
|
| 108 | - $offset = $idx; |
|
| 109 | - } |
|
| 110 | - return new self($class, $pc, $tag); |
|
| 111 | - } |
|
| 75 | + /** |
|
| 76 | + * Decode identifier component from DER data. |
|
| 77 | + * |
|
| 78 | + * @param string $data DER encoded data |
|
| 79 | + * @param null|int $offset Reference to the variable that contains offset |
|
| 80 | + * into the data where to start parsing. |
|
| 81 | + * Variable is updated to the offset next to the |
|
| 82 | + * parsed identifier. If null, start from offset 0. |
|
| 83 | + * |
|
| 84 | + * @throws DecodeException If decoding fails |
|
| 85 | + * |
|
| 86 | + * @return self |
|
| 87 | + */ |
|
| 88 | + public static function fromDER(string $data, int &$offset = null): Identifier |
|
| 89 | + { |
|
| 90 | + $idx = $offset ?? 0; |
|
| 91 | + $datalen = strlen($data); |
|
| 92 | + if ($idx >= $datalen) { |
|
| 93 | + throw new DecodeException('Invalid offset.'); |
|
| 94 | + } |
|
| 95 | + $byte = ord($data[$idx++]); |
|
| 96 | + // bits 8 and 7 (class) |
|
| 97 | + // 0 = universal, 1 = application, 2 = context-specific, 3 = private |
|
| 98 | + $class = (0b11000000 & $byte) >> 6; |
|
| 99 | + // bit 6 (0 = primitive / 1 = constructed) |
|
| 100 | + $pc = (0b00100000 & $byte) >> 5; |
|
| 101 | + // bits 5 to 1 (tag number) |
|
| 102 | + $tag = (0b00011111 & $byte); |
|
| 103 | + // long-form identifier |
|
| 104 | + if (0x1f == $tag) { |
|
| 105 | + $tag = self::_decodeLongFormTag($data, $idx); |
|
| 106 | + } |
|
| 107 | + if (isset($offset)) { |
|
| 108 | + $offset = $idx; |
|
| 109 | + } |
|
| 110 | + return new self($class, $pc, $tag); |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - /** |
|
| 114 | - * {@inheritdoc} |
|
| 115 | - */ |
|
| 116 | - public function toDER(): string |
|
| 117 | - { |
|
| 118 | - $bytes = []; |
|
| 119 | - $byte = $this->_class << 6 | $this->_pc << 5; |
|
| 120 | - $tag = $this->_tag->gmpObj(); |
|
| 121 | - if ($tag < 0x1f) { |
|
| 122 | - $bytes[] = $byte | $tag; |
|
| 123 | - } |
|
| 124 | - // long-form identifier |
|
| 125 | - else { |
|
| 126 | - $bytes[] = $byte | 0x1f; |
|
| 127 | - $octets = []; |
|
| 128 | - for (; $tag > 0; $tag >>= 7) { |
|
| 129 | - array_push($octets, gmp_intval(0x80 | ($tag & 0x7f))); |
|
| 130 | - } |
|
| 131 | - // last octet has bit 8 set to zero |
|
| 132 | - $octets[0] &= 0x7f; |
|
| 133 | - foreach (array_reverse($octets) as $octet) { |
|
| 134 | - $bytes[] = $octet; |
|
| 135 | - } |
|
| 136 | - } |
|
| 137 | - return pack('C*', ...$bytes); |
|
| 138 | - } |
|
| 113 | + /** |
|
| 114 | + * {@inheritdoc} |
|
| 115 | + */ |
|
| 116 | + public function toDER(): string |
|
| 117 | + { |
|
| 118 | + $bytes = []; |
|
| 119 | + $byte = $this->_class << 6 | $this->_pc << 5; |
|
| 120 | + $tag = $this->_tag->gmpObj(); |
|
| 121 | + if ($tag < 0x1f) { |
|
| 122 | + $bytes[] = $byte | $tag; |
|
| 123 | + } |
|
| 124 | + // long-form identifier |
|
| 125 | + else { |
|
| 126 | + $bytes[] = $byte | 0x1f; |
|
| 127 | + $octets = []; |
|
| 128 | + for (; $tag > 0; $tag >>= 7) { |
|
| 129 | + array_push($octets, gmp_intval(0x80 | ($tag & 0x7f))); |
|
| 130 | + } |
|
| 131 | + // last octet has bit 8 set to zero |
|
| 132 | + $octets[0] &= 0x7f; |
|
| 133 | + foreach (array_reverse($octets) as $octet) { |
|
| 134 | + $bytes[] = $octet; |
|
| 135 | + } |
|
| 136 | + } |
|
| 137 | + return pack('C*', ...$bytes); |
|
| 138 | + } |
|
| 139 | 139 | |
| 140 | - /** |
|
| 141 | - * Get class of the type. |
|
| 142 | - * |
|
| 143 | - * @return int |
|
| 144 | - */ |
|
| 145 | - public function typeClass(): int |
|
| 146 | - { |
|
| 147 | - return $this->_class; |
|
| 148 | - } |
|
| 140 | + /** |
|
| 141 | + * Get class of the type. |
|
| 142 | + * |
|
| 143 | + * @return int |
|
| 144 | + */ |
|
| 145 | + public function typeClass(): int |
|
| 146 | + { |
|
| 147 | + return $this->_class; |
|
| 148 | + } |
|
| 149 | 149 | |
| 150 | - /** |
|
| 151 | - * Get P/C. |
|
| 152 | - * |
|
| 153 | - * @return int |
|
| 154 | - */ |
|
| 155 | - public function pc(): int |
|
| 156 | - { |
|
| 157 | - return $this->_pc; |
|
| 158 | - } |
|
| 150 | + /** |
|
| 151 | + * Get P/C. |
|
| 152 | + * |
|
| 153 | + * @return int |
|
| 154 | + */ |
|
| 155 | + public function pc(): int |
|
| 156 | + { |
|
| 157 | + return $this->_pc; |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | - /** |
|
| 161 | - * Get the tag number. |
|
| 162 | - * |
|
| 163 | - * @return string Base 10 integer string |
|
| 164 | - */ |
|
| 165 | - public function tag(): string |
|
| 166 | - { |
|
| 167 | - return $this->_tag->base10(); |
|
| 168 | - } |
|
| 160 | + /** |
|
| 161 | + * Get the tag number. |
|
| 162 | + * |
|
| 163 | + * @return string Base 10 integer string |
|
| 164 | + */ |
|
| 165 | + public function tag(): string |
|
| 166 | + { |
|
| 167 | + return $this->_tag->base10(); |
|
| 168 | + } |
|
| 169 | 169 | |
| 170 | - /** |
|
| 171 | - * Get the tag as an integer. |
|
| 172 | - * |
|
| 173 | - * @return int |
|
| 174 | - */ |
|
| 175 | - public function intTag(): int |
|
| 176 | - { |
|
| 177 | - return $this->_tag->intVal(); |
|
| 178 | - } |
|
| 170 | + /** |
|
| 171 | + * Get the tag as an integer. |
|
| 172 | + * |
|
| 173 | + * @return int |
|
| 174 | + */ |
|
| 175 | + public function intTag(): int |
|
| 176 | + { |
|
| 177 | + return $this->_tag->intVal(); |
|
| 178 | + } |
|
| 179 | 179 | |
| 180 | - /** |
|
| 181 | - * Check whether type is of an universal class. |
|
| 182 | - * |
|
| 183 | - * @return bool |
|
| 184 | - */ |
|
| 185 | - public function isUniversal(): bool |
|
| 186 | - { |
|
| 187 | - return self::CLASS_UNIVERSAL === $this->_class; |
|
| 188 | - } |
|
| 180 | + /** |
|
| 181 | + * Check whether type is of an universal class. |
|
| 182 | + * |
|
| 183 | + * @return bool |
|
| 184 | + */ |
|
| 185 | + public function isUniversal(): bool |
|
| 186 | + { |
|
| 187 | + return self::CLASS_UNIVERSAL === $this->_class; |
|
| 188 | + } |
|
| 189 | 189 | |
| 190 | - /** |
|
| 191 | - * Check whether type is of an application class. |
|
| 192 | - * |
|
| 193 | - * @return bool |
|
| 194 | - */ |
|
| 195 | - public function isApplication(): bool |
|
| 196 | - { |
|
| 197 | - return self::CLASS_APPLICATION === $this->_class; |
|
| 198 | - } |
|
| 190 | + /** |
|
| 191 | + * Check whether type is of an application class. |
|
| 192 | + * |
|
| 193 | + * @return bool |
|
| 194 | + */ |
|
| 195 | + public function isApplication(): bool |
|
| 196 | + { |
|
| 197 | + return self::CLASS_APPLICATION === $this->_class; |
|
| 198 | + } |
|
| 199 | 199 | |
| 200 | - /** |
|
| 201 | - * Check whether type is of a context specific class. |
|
| 202 | - * |
|
| 203 | - * @return bool |
|
| 204 | - */ |
|
| 205 | - public function isContextSpecific(): bool |
|
| 206 | - { |
|
| 207 | - return self::CLASS_CONTEXT_SPECIFIC === $this->_class; |
|
| 208 | - } |
|
| 200 | + /** |
|
| 201 | + * Check whether type is of a context specific class. |
|
| 202 | + * |
|
| 203 | + * @return bool |
|
| 204 | + */ |
|
| 205 | + public function isContextSpecific(): bool |
|
| 206 | + { |
|
| 207 | + return self::CLASS_CONTEXT_SPECIFIC === $this->_class; |
|
| 208 | + } |
|
| 209 | 209 | |
| 210 | - /** |
|
| 211 | - * Check whether type is of a private class. |
|
| 212 | - * |
|
| 213 | - * @return bool |
|
| 214 | - */ |
|
| 215 | - public function isPrivate(): bool |
|
| 216 | - { |
|
| 217 | - return self::CLASS_PRIVATE === $this->_class; |
|
| 218 | - } |
|
| 210 | + /** |
|
| 211 | + * Check whether type is of a private class. |
|
| 212 | + * |
|
| 213 | + * @return bool |
|
| 214 | + */ |
|
| 215 | + public function isPrivate(): bool |
|
| 216 | + { |
|
| 217 | + return self::CLASS_PRIVATE === $this->_class; |
|
| 218 | + } |
|
| 219 | 219 | |
| 220 | - /** |
|
| 221 | - * Check whether content is primitive type. |
|
| 222 | - * |
|
| 223 | - * @return bool |
|
| 224 | - */ |
|
| 225 | - public function isPrimitive(): bool |
|
| 226 | - { |
|
| 227 | - return self::PRIMITIVE === $this->_pc; |
|
| 228 | - } |
|
| 220 | + /** |
|
| 221 | + * Check whether content is primitive type. |
|
| 222 | + * |
|
| 223 | + * @return bool |
|
| 224 | + */ |
|
| 225 | + public function isPrimitive(): bool |
|
| 226 | + { |
|
| 227 | + return self::PRIMITIVE === $this->_pc; |
|
| 228 | + } |
|
| 229 | 229 | |
| 230 | - /** |
|
| 231 | - * Check hether content is constructed type. |
|
| 232 | - * |
|
| 233 | - * @return bool |
|
| 234 | - */ |
|
| 235 | - public function isConstructed(): bool |
|
| 236 | - { |
|
| 237 | - return self::CONSTRUCTED === $this->_pc; |
|
| 238 | - } |
|
| 230 | + /** |
|
| 231 | + * Check hether content is constructed type. |
|
| 232 | + * |
|
| 233 | + * @return bool |
|
| 234 | + */ |
|
| 235 | + public function isConstructed(): bool |
|
| 236 | + { |
|
| 237 | + return self::CONSTRUCTED === $this->_pc; |
|
| 238 | + } |
|
| 239 | 239 | |
| 240 | - /** |
|
| 241 | - * Get self with given type class. |
|
| 242 | - * |
|
| 243 | - * @param int $class One of <code>CLASS_*</code> enumerations |
|
| 244 | - * |
|
| 245 | - * @return self |
|
| 246 | - */ |
|
| 247 | - public function withClass(int $class): Identifier |
|
| 248 | - { |
|
| 249 | - $obj = clone $this; |
|
| 250 | - $obj->_class = 0b11 & $class; |
|
| 251 | - return $obj; |
|
| 252 | - } |
|
| 240 | + /** |
|
| 241 | + * Get self with given type class. |
|
| 242 | + * |
|
| 243 | + * @param int $class One of <code>CLASS_*</code> enumerations |
|
| 244 | + * |
|
| 245 | + * @return self |
|
| 246 | + */ |
|
| 247 | + public function withClass(int $class): Identifier |
|
| 248 | + { |
|
| 249 | + $obj = clone $this; |
|
| 250 | + $obj->_class = 0b11 & $class; |
|
| 251 | + return $obj; |
|
| 252 | + } |
|
| 253 | 253 | |
| 254 | - /** |
|
| 255 | - * Get self with given type tag. |
|
| 256 | - * |
|
| 257 | - * @param int|string $tag Tag number |
|
| 258 | - * |
|
| 259 | - * @return self |
|
| 260 | - */ |
|
| 261 | - public function withTag($tag): Identifier |
|
| 262 | - { |
|
| 263 | - $obj = clone $this; |
|
| 264 | - $obj->_tag = new BigInt($tag); |
|
| 265 | - return $obj; |
|
| 266 | - } |
|
| 254 | + /** |
|
| 255 | + * Get self with given type tag. |
|
| 256 | + * |
|
| 257 | + * @param int|string $tag Tag number |
|
| 258 | + * |
|
| 259 | + * @return self |
|
| 260 | + */ |
|
| 261 | + public function withTag($tag): Identifier |
|
| 262 | + { |
|
| 263 | + $obj = clone $this; |
|
| 264 | + $obj->_tag = new BigInt($tag); |
|
| 265 | + return $obj; |
|
| 266 | + } |
|
| 267 | 267 | |
| 268 | - /** |
|
| 269 | - * Get human readable name of the type class. |
|
| 270 | - * |
|
| 271 | - * @param int $class |
|
| 272 | - * |
|
| 273 | - * @return string |
|
| 274 | - */ |
|
| 275 | - public static function classToName(int $class): string |
|
| 276 | - { |
|
| 277 | - if (!array_key_exists($class, self::MAP_CLASS_TO_NAME)) { |
|
| 278 | - return "CLASS {$class}"; |
|
| 279 | - } |
|
| 280 | - return self::MAP_CLASS_TO_NAME[$class]; |
|
| 281 | - } |
|
| 268 | + /** |
|
| 269 | + * Get human readable name of the type class. |
|
| 270 | + * |
|
| 271 | + * @param int $class |
|
| 272 | + * |
|
| 273 | + * @return string |
|
| 274 | + */ |
|
| 275 | + public static function classToName(int $class): string |
|
| 276 | + { |
|
| 277 | + if (!array_key_exists($class, self::MAP_CLASS_TO_NAME)) { |
|
| 278 | + return "CLASS {$class}"; |
|
| 279 | + } |
|
| 280 | + return self::MAP_CLASS_TO_NAME[$class]; |
|
| 281 | + } |
|
| 282 | 282 | |
| 283 | - /** |
|
| 284 | - * Parse long form tag. |
|
| 285 | - * |
|
| 286 | - * @param string $data DER data |
|
| 287 | - * @param int $offset Reference to the variable containing offset to data |
|
| 288 | - * |
|
| 289 | - * @throws DecodeException If decoding fails |
|
| 290 | - * |
|
| 291 | - * @return string Tag number |
|
| 292 | - */ |
|
| 293 | - private static function _decodeLongFormTag(string $data, int &$offset): string |
|
| 294 | - { |
|
| 295 | - $datalen = strlen($data); |
|
| 296 | - $tag = gmp_init(0, 10); |
|
| 297 | - while (true) { |
|
| 298 | - if ($offset >= $datalen) { |
|
| 299 | - throw new DecodeException( |
|
| 300 | - 'Unexpected end of data while decoding' . |
|
| 301 | - ' long form identifier.'); |
|
| 302 | - } |
|
| 303 | - $byte = ord($data[$offset++]); |
|
| 304 | - $tag <<= 7; |
|
| 305 | - $tag |= 0x7f & $byte; |
|
| 306 | - // last byte has bit 8 set to zero |
|
| 307 | - if (!(0x80 & $byte)) { |
|
| 308 | - break; |
|
| 309 | - } |
|
| 310 | - } |
|
| 311 | - return gmp_strval($tag, 10); |
|
| 312 | - } |
|
| 283 | + /** |
|
| 284 | + * Parse long form tag. |
|
| 285 | + * |
|
| 286 | + * @param string $data DER data |
|
| 287 | + * @param int $offset Reference to the variable containing offset to data |
|
| 288 | + * |
|
| 289 | + * @throws DecodeException If decoding fails |
|
| 290 | + * |
|
| 291 | + * @return string Tag number |
|
| 292 | + */ |
|
| 293 | + private static function _decodeLongFormTag(string $data, int &$offset): string |
|
| 294 | + { |
|
| 295 | + $datalen = strlen($data); |
|
| 296 | + $tag = gmp_init(0, 10); |
|
| 297 | + while (true) { |
|
| 298 | + if ($offset >= $datalen) { |
|
| 299 | + throw new DecodeException( |
|
| 300 | + 'Unexpected end of data while decoding' . |
|
| 301 | + ' long form identifier.'); |
|
| 302 | + } |
|
| 303 | + $byte = ord($data[$offset++]); |
|
| 304 | + $tag <<= 7; |
|
| 305 | + $tag |= 0x7f & $byte; |
|
| 306 | + // last byte has bit 8 set to zero |
|
| 307 | + if (!(0x80 & $byte)) { |
|
| 308 | + break; |
|
| 309 | + } |
|
| 310 | + } |
|
| 311 | + return gmp_strval($tag, 10); |
|
| 312 | + } |
|
| 313 | 313 | } |