1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Sop\X509\Certificate\Extension; |
6
|
|
|
|
7
|
|
|
use Sop\ASN1\Element; |
8
|
|
|
use Sop\ASN1\Type\Constructed\Sequence; |
9
|
|
|
use Sop\ASN1\Type\UnspecifiedType; |
10
|
|
|
use Sop\X501\ASN1\Attribute; |
11
|
|
|
use Sop\X509\Feature\AttributeContainer; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Implements 'Subject Directory Attributes' certificate extension. |
15
|
|
|
* |
16
|
|
|
* @see https://tools.ietf.org/html/rfc5280#section-4.2.1.8 |
17
|
|
|
*/ |
18
|
|
|
class SubjectDirectoryAttributesExtension extends Extension implements \Countable, \IteratorAggregate |
19
|
|
|
{ |
20
|
|
|
use AttributeContainer; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Constructor. |
24
|
|
|
* |
25
|
|
|
* @param bool $critical |
26
|
|
|
* @param Attribute ...$attribs One or more Attribute objects |
27
|
|
|
*/ |
28
|
11 |
|
public function __construct(bool $critical, Attribute ...$attribs) |
29
|
|
|
{ |
30
|
11 |
|
parent::__construct(self::OID_SUBJECT_DIRECTORY_ATTRIBUTES, $critical); |
31
|
11 |
|
$this->_attributes = $attribs; |
32
|
11 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
10 |
|
protected static function _fromDER(string $data, bool $critical): Extension |
38
|
|
|
{ |
39
|
10 |
|
$attribs = array_map( |
40
|
|
|
function (UnspecifiedType $el) { |
41
|
9 |
|
return Attribute::fromASN1($el->asSequence()); |
42
|
10 |
|
}, UnspecifiedType::fromDER($data)->asSequence()->elements()); |
43
|
10 |
|
if (!count($attribs)) { |
44
|
1 |
|
throw new \UnexpectedValueException( |
45
|
1 |
|
'SubjectDirectoryAttributes must have at least one Attribute.'); |
46
|
|
|
} |
47
|
9 |
|
return new self($critical, ...$attribs); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
16 |
|
protected function _valueASN1(): Element |
54
|
|
|
{ |
55
|
16 |
|
if (!count($this->_attributes)) { |
56
|
1 |
|
throw new \LogicException('No attributes'); |
57
|
|
|
} |
58
|
15 |
|
$elements = array_map( |
59
|
|
|
function (Attribute $attr) { |
60
|
15 |
|
return $attr->toASN1(); |
61
|
15 |
|
}, array_values($this->_attributes)); |
62
|
15 |
|
return new Sequence(...$elements); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|