PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.3.0
Defender Security – Malware Scanner, Login Security & Firewall v6.3.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / lib / packages / FG / X509 / CertificateExtensions.php

CertificateExtensions.php in Defender Security – Malware Scanner, Login Security & Firewall 6.3.0, at lib/packages/FG/X509/CertificateExtensions.php

101 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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