| 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\X509\SAN; |
| 12 |
|
| 13 |
use WP_DEFENDER_VENDOR\FG\ASN1\Exception\ParserException; |
| 14 |
use WP_DEFENDER_VENDOR\FG\ASN1\ASNObject; |
| 15 |
use WP_DEFENDER_VENDOR\FG\ASN1\OID; |
| 16 |
use WP_DEFENDER_VENDOR\FG\ASN1\Parsable; |
| 17 |
use WP_DEFENDER_VENDOR\FG\ASN1\Identifier; |
| 18 |
use WP_DEFENDER_VENDOR\FG\ASN1\Universal\Sequence; |
| 19 |
|
| 20 |
/** |
| 21 |
* See section 8.3.2.1 of ITU-T X.509. |
| 22 |
*/ |
| 23 |
class SubjectAlternativeNames extends ASNObject implements Parsable |
| 24 |
{ |
| 25 |
private $alternativeNamesSequence; |
| 26 |
|
| 27 |
public function __construct() |
| 28 |
{ |
| 29 |
$this->alternativeNamesSequence = new Sequence(); |
| 30 |
} |
| 31 |
|
| 32 |
protected function calculateContentLength() |
| 33 |
{ |
| 34 |
return $this->alternativeNamesSequence->getObjectLength(); |
| 35 |
} |
| 36 |
|
| 37 |
public function getType() |
| 38 |
{ |
| 39 |
return Identifier::OCTETSTRING; |
| 40 |
} |
| 41 |
|
| 42 |
public function addDomainName(DNSName $domainName) |
| 43 |
{ |
| 44 |
$this->alternativeNamesSequence->addChild($domainName); |
| 45 |
} |
| 46 |
|
| 47 |
public function addIP(IPAddress $ip) |
| 48 |
{ |
| 49 |
$this->alternativeNamesSequence->addChild($ip); |
| 50 |
} |
| 51 |
|
| 52 |
public function getContent() |
| 53 |
{ |
| 54 |
return $this->alternativeNamesSequence->getContent(); |
| 55 |
} |
| 56 |
|
| 57 |
protected function getEncodedValue() |
| 58 |
{ |
| 59 |
return $this->alternativeNamesSequence->getBinary(); |
| 60 |
} |
| 61 |
|
| 62 |
public static function fromBinary(&$binaryData, &$offsetIndex = 0) |
| 63 |
{ |
| 64 |
self::parseIdentifier($binaryData[$offsetIndex], Identifier::OCTETSTRING, $offsetIndex++); |
| 65 |
$contentLength = self::parseContentLength($binaryData, $offsetIndex); |
| 66 |
|
| 67 |
if ($contentLength < 2) { |
| 68 |
throw new ParserException('Can not parse Subject Alternative Names: The Sequence within the octet string after the Object identifier '.OID::CERT_EXT_SUBJECT_ALT_NAME." is too short ({$contentLength} octets)", $offsetIndex); |
| 69 |
} |
| 70 |
|
| 71 |
$offsetOfSequence = $offsetIndex; |
| 72 |
$sequence = Sequence::fromBinary($binaryData, $offsetIndex); |
| 73 |
$offsetOfSequence += $sequence->getNumberOfLengthOctets() + 1; |
| 74 |
|
| 75 |
if ($sequence->getObjectLength() != $contentLength) { |
| 76 |
throw new ParserException('Can not parse Subject Alternative Names: The Sequence length does not match the length of the surrounding octet string', $offsetIndex); |
| 77 |
} |
| 78 |
|
| 79 |
$parsedObject = new self(); |
| 80 |
/** @var \WP_DEFENDER_VENDOR\FG\ASN1\ASNObject $object */ |
| 81 |
foreach ($sequence as $object) { |
| 82 |
if ($object->getType() == DNSName::IDENTIFIER) { |
| 83 |
$domainName = DNSName::fromBinary($binaryData, $offsetOfSequence); |
| 84 |
$parsedObject->addDomainName($domainName); |
| 85 |
} elseif ($object->getType() == IPAddress::IDENTIFIER) { |
| 86 |
$ip = IPAddress::fromBinary($binaryData, $offsetOfSequence); |
| 87 |
$parsedObject->addIP($ip); |
| 88 |
} else { |
| 89 |
throw new ParserException('Could not parse Subject Alternative Name: Only DNSName and IP SANs are currently supported', $offsetIndex); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
$parsedObject->getBinary(); // Determine the number of content octets and object sizes once (just to let the equality unit tests pass :/ ) |
| 94 |
return $parsedObject; |
| 95 |
} |
| 96 |
} |
| 97 |
|