| 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; |
| 12 |
|
| 13 |
use WP_DEFENDER_VENDOR\FG\ASN1\Exception\ParserException; |
| 14 |
use WP_DEFENDER_VENDOR\FG\ASN1\OID; |
| 15 |
use WP_DEFENDER_VENDOR\FG\ASN1\ASNObject; |
| 16 |
use WP_DEFENDER_VENDOR\FG\ASN1\Parsable; |
| 17 |
use WP_DEFENDER_VENDOR\FG\ASN1\Identifier; |
| 18 |
use WP_DEFENDER_VENDOR\FG\ASN1\Universal\OctetString; |
| 19 |
use WP_DEFENDER_VENDOR\FG\ASN1\Universal\Set; |
| 20 |
use WP_DEFENDER_VENDOR\FG\ASN1\Universal\Sequence; |
| 21 |
use WP_DEFENDER_VENDOR\FG\ASN1\Universal\ObjectIdentifier; |
| 22 |
use WP_DEFENDER_VENDOR\FG\X509\SAN\SubjectAlternativeNames; |
| 23 |
|
| 24 |
class CertificateExtensions extends Set implements Parsable |
| 25 |
{ |
| 26 |
private $innerSequence; |
| 27 |
private $extensions = []; |
| 28 |
|
| 29 |
public function __construct() |
| 30 |
{ |
| 31 |
$this->innerSequence = new Sequence(); |
| 32 |
parent::__construct($this->innerSequence); |
| 33 |
} |
| 34 |
|
| 35 |
public function addSubjectAlternativeNames(SubjectAlternativeNames $sans) |
| 36 |
{ |
| 37 |
$this->addExtension(OID::CERT_EXT_SUBJECT_ALT_NAME, $sans); |
| 38 |
} |
| 39 |
|
| 40 |
private function addExtension($oidString, ASNObject $extension) |
| 41 |
{ |
| 42 |
$sequence = new Sequence(); |
| 43 |
$sequence->addChild(new ObjectIdentifier($oidString)); |
| 44 |
$sequence->addChild($extension); |
| 45 |
|
| 46 |
$this->innerSequence->addChild($sequence); |
| 47 |
$this->extensions[] = $extension; |
| 48 |
} |
| 49 |
|
| 50 |
public function getContent() |
| 51 |
{ |
| 52 |
return $this->extensions; |
| 53 |
} |
| 54 |
|
| 55 |
public static function fromBinary(&$binaryData, &$offsetIndex = 0) |
| 56 |
{ |
| 57 |
self::parseIdentifier($binaryData[$offsetIndex], Identifier::SET, $offsetIndex++); |
| 58 |
self::parseContentLength($binaryData, $offsetIndex); |
| 59 |
|
| 60 |
$tmpOffset = $offsetIndex; |
| 61 |
$extensions = Sequence::fromBinary($binaryData, $offsetIndex); |
| 62 |
$tmpOffset += 1 + $extensions->getNumberOfLengthOctets(); |
| 63 |
|
| 64 |
$parsedObject = new self(); |
| 65 |
foreach ($extensions as $extension) { |
| 66 |
if ($extension->getType() != Identifier::SEQUENCE) { |
| 67 |
//FIXME wrong offset index |
| 68 |
throw new ParserException('Could not parse Certificate Extensions: Expected ASN.1 Sequence but got '.$extension->getTypeName(), $offsetIndex); |
| 69 |
} |
| 70 |
|
| 71 |
$tmpOffset += 1 + $extension->getNumberOfLengthOctets(); |
| 72 |
$children = $extension->getChildren(); |
| 73 |
if (count($children) < 2) { |
| 74 |
throw new ParserException('Could not parse Certificate Extensions: Needs at least two child elements per extension sequence (object identifier and octet string)', $tmpOffset); |
| 75 |
} |
| 76 |
/** @var \WP_DEFENDER_VENDOR\FG\ASN1\ASNObject $objectIdentifier */ |
| 77 |
$objectIdentifier = $children[0]; |
| 78 |
|
| 79 |
/** @var OctetString $octetString */ |
| 80 |
$octetString = $children[1]; |
| 81 |
|
| 82 |
if ($objectIdentifier->getType() != Identifier::OBJECT_IDENTIFIER) { |
| 83 |
throw new ParserException('Could not parse Certificate Extensions: Expected ASN.1 Object Identifier but got '.$extension->getTypeName(), $tmpOffset); |
| 84 |
} |
| 85 |
|
| 86 |
$tmpOffset += $objectIdentifier->getObjectLength(); |
| 87 |
|
| 88 |
if ($objectIdentifier->getContent() == OID::CERT_EXT_SUBJECT_ALT_NAME) { |
| 89 |
$sans = SubjectAlternativeNames::fromBinary($binaryData, $tmpOffset); |
| 90 |
$parsedObject->addSubjectAlternativeNames($sans); |
| 91 |
} else { |
| 92 |
// can now only parse SANs. There might be more in the future |
| 93 |
$tmpOffset += $octetString->getObjectLength(); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
$parsedObject->getBinary(); // Determine the number of content octets and object sizes once (just to let the equality unit tests pass :/ ) |
| 98 |
return $parsedObject; |
| 99 |
} |
| 100 |
} |
| 101 |
|