| 1 |
<?php |
| 2 |
/* |
| 3 |
* This file is part of the PHPASN1 library. |
| 4 |
* |
| 5 |
* Copyright © Friedrich Große <[email protected]> |
| 6 |
* |
| 7 |
* For the full copyright and license information, please view the LICENSE |
| 8 |
* file that was distributed with this source code. |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace WP_DEFENDER_VENDOR\FG\ASN1\Universal; |
| 12 |
|
| 13 |
use Exception; |
| 14 |
use WP_DEFENDER_VENDOR\FG\ASN1\Base128; |
| 15 |
use WP_DEFENDER_VENDOR\FG\ASN1\OID; |
| 16 |
use WP_DEFENDER_VENDOR\FG\ASN1\ASNObject; |
| 17 |
use WP_DEFENDER_VENDOR\FG\ASN1\Parsable; |
| 18 |
use WP_DEFENDER_VENDOR\FG\ASN1\Identifier; |
| 19 |
use WP_DEFENDER_VENDOR\FG\ASN1\Exception\ParserException; |
| 20 |
|
| 21 |
class ObjectIdentifier extends ASNObject implements Parsable |
| 22 |
{ |
| 23 |
protected $subIdentifiers; |
| 24 |
protected $value; |
| 25 |
|
| 26 |
public function __construct($value) |
| 27 |
{ |
| 28 |
$this->subIdentifiers = explode('.', $value); |
| 29 |
$nrOfSubIdentifiers = count($this->subIdentifiers); |
| 30 |
|
| 31 |
for ($i = 0; $i < $nrOfSubIdentifiers; $i++) { |
| 32 |
if (is_numeric($this->subIdentifiers[$i])) { |
| 33 |
// enforce the integer type |
| 34 |
$this->subIdentifiers[$i] = intval($this->subIdentifiers[$i]); |
| 35 |
} else { |
| 36 |
throw new Exception("[{$value}] is no valid object identifier (sub identifier ".($i + 1).' is not numeric)!'); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
// Merge the first to arcs of the OID registration tree (per ASN definition!) |
| 41 |
if ($nrOfSubIdentifiers >= 2) { |
| 42 |
$this->subIdentifiers[1] = ($this->subIdentifiers[0] * 40) + $this->subIdentifiers[1]; |
| 43 |
unset($this->subIdentifiers[0]); |
| 44 |
} |
| 45 |
|
| 46 |
$this->value = $value; |
| 47 |
} |
| 48 |
|
| 49 |
public function getContent() |
| 50 |
{ |
| 51 |
return $this->value; |
| 52 |
} |
| 53 |
|
| 54 |
public function getType() |
| 55 |
{ |
| 56 |
return Identifier::OBJECT_IDENTIFIER; |
| 57 |
} |
| 58 |
|
| 59 |
protected function calculateContentLength() |
| 60 |
{ |
| 61 |
$length = 0; |
| 62 |
foreach ($this->subIdentifiers as $subIdentifier) { |
| 63 |
do { |
| 64 |
$subIdentifier = $subIdentifier >> 7; |
| 65 |
$length++; |
| 66 |
} while ($subIdentifier > 0); |
| 67 |
} |
| 68 |
|
| 69 |
return $length; |
| 70 |
} |
| 71 |
|
| 72 |
protected function getEncodedValue() |
| 73 |
{ |
| 74 |
$encodedValue = ''; |
| 75 |
foreach ($this->subIdentifiers as $subIdentifier) { |
| 76 |
$encodedValue .= Base128::encode($subIdentifier); |
| 77 |
} |
| 78 |
|
| 79 |
return $encodedValue; |
| 80 |
} |
| 81 |
|
| 82 |
public function __toString() |
| 83 |
{ |
| 84 |
return OID::getName($this->value); |
| 85 |
} |
| 86 |
|
| 87 |
public static function fromBinary(&$binaryData, &$offsetIndex = 0) |
| 88 |
{ |
| 89 |
self::parseIdentifier($binaryData[$offsetIndex], Identifier::OBJECT_IDENTIFIER, $offsetIndex++); |
| 90 |
$contentLength = self::parseContentLength($binaryData, $offsetIndex, 1); |
| 91 |
|
| 92 |
$firstOctet = ord($binaryData[$offsetIndex++]); |
| 93 |
$oidString = floor($firstOctet / 40).'.'.($firstOctet % 40); |
| 94 |
$oidString .= '.'.self::parseOid($binaryData, $offsetIndex, $contentLength - 1); |
| 95 |
|
| 96 |
$parsedObject = new self($oidString); |
| 97 |
$parsedObject->setContentLength($contentLength); |
| 98 |
|
| 99 |
return $parsedObject; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Parses an object identifier except for the first octet, which is parsed |
| 104 |
* differently. This way relative object identifiers can also be parsed |
| 105 |
* using this. |
| 106 |
* |
| 107 |
* @param $binaryData |
| 108 |
* @param $offsetIndex |
| 109 |
* @param $octetsToRead |
| 110 |
* |
| 111 |
* @throws ParserException |
| 112 |
* |
| 113 |
* @return string |
| 114 |
*/ |
| 115 |
protected static function parseOid(&$binaryData, &$offsetIndex, $octetsToRead) |
| 116 |
{ |
| 117 |
$oid = ''; |
| 118 |
|
| 119 |
while ($octetsToRead > 0) { |
| 120 |
$octets = ''; |
| 121 |
|
| 122 |
do { |
| 123 |
if (0 === $octetsToRead) { |
| 124 |
throw new ParserException('Malformed ASN.1 Object Identifier', $offsetIndex - 1); |
| 125 |
} |
| 126 |
|
| 127 |
$octetsToRead--; |
| 128 |
$octet = $binaryData[$offsetIndex++]; |
| 129 |
$octets .= $octet; |
| 130 |
} while (ord($octet) & 0x80); |
| 131 |
|
| 132 |
$oid .= sprintf('%d.', Base128::decode($octets)); |
| 133 |
} |
| 134 |
|
| 135 |
// Remove trailing '.' |
| 136 |
return substr($oid, 0, -1) ?: ''; |
| 137 |
} |
| 138 |
} |
| 139 |
|