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\UnspecifiedType; |
9
|
|
|
use Sop\X501\ASN1\Attribute; |
10
|
|
|
use Sop\X501\ASN1\Collection\SequenceOfAttributes; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Implements 'Subject Directory Attributes' certificate extension. |
14
|
|
|
* |
15
|
|
|
* @see https://tools.ietf.org/html/rfc5280#section-4.2.1.8 |
16
|
|
|
*/ |
17
|
|
|
class SubjectDirectoryAttributesExtension extends Extension implements \Countable, \IteratorAggregate |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Attributes. |
21
|
|
|
* |
22
|
|
|
* @var SequenceOfAttributes |
23
|
|
|
*/ |
24
|
|
|
private $_attributes; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Constructor. |
28
|
|
|
* |
29
|
|
|
* @param bool $critical |
30
|
|
|
* @param Attribute ...$attribs One or more Attribute objects |
31
|
|
|
*/ |
32
|
11 |
|
public function __construct(bool $critical, Attribute ...$attribs) |
33
|
|
|
{ |
34
|
11 |
|
parent::__construct(self::OID_SUBJECT_DIRECTORY_ATTRIBUTES, $critical); |
35
|
11 |
|
$this->_attributes = new SequenceOfAttributes(...$attribs); |
36
|
11 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Check whether attribute is present. |
40
|
|
|
* |
41
|
|
|
* @param string $name OID or attribute name |
42
|
|
|
* |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
2 |
|
public function has(string $name): bool |
46
|
|
|
{ |
47
|
2 |
|
return $this->_attributes->has($name); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get first attribute by OID or attribute name. |
52
|
|
|
* |
53
|
|
|
* @param string $name OID or attribute name |
54
|
|
|
* |
55
|
|
|
* @throws \UnexpectedValueException if attribute is not present |
56
|
|
|
* |
57
|
|
|
* @return Attribute |
58
|
|
|
*/ |
59
|
3 |
|
public function firstOf(string $name): Attribute |
60
|
|
|
{ |
61
|
3 |
|
return $this->_attributes->firstOf($name); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get all attributes of given name. |
66
|
|
|
* |
67
|
|
|
* @param string $name OID or attribute name |
68
|
|
|
* |
69
|
|
|
* @return Attribute[] |
70
|
|
|
*/ |
71
|
2 |
|
public function allOf(string $name): array |
72
|
|
|
{ |
73
|
2 |
|
return $this->_attributes->allOf($name); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get all attributes. |
78
|
|
|
* |
79
|
|
|
* @return Attribute[] |
80
|
|
|
*/ |
81
|
1 |
|
public function all(): array |
82
|
|
|
{ |
83
|
1 |
|
return $this->_attributes->all(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get number of attributes. |
88
|
|
|
* |
89
|
|
|
* @return int |
90
|
|
|
*/ |
91
|
1 |
|
public function count(): int |
92
|
|
|
{ |
93
|
1 |
|
return count($this->_attributes); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get iterator for attributes. |
98
|
|
|
* |
99
|
|
|
* @return \ArrayIterator|Attribute[] |
100
|
|
|
*/ |
101
|
1 |
|
public function getIterator(): \ArrayIterator |
102
|
|
|
{ |
103
|
1 |
|
return $this->_attributes->getIterator(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
10 |
|
protected static function _fromDER(string $data, bool $critical): Extension |
110
|
|
|
{ |
111
|
10 |
|
$attribs = SequenceOfAttributes::fromASN1( |
112
|
10 |
|
UnspecifiedType::fromDER($data)->asSequence()); |
113
|
10 |
|
if (!count($attribs)) { |
114
|
1 |
|
throw new \UnexpectedValueException( |
115
|
1 |
|
'SubjectDirectoryAttributes must have at least one Attribute.'); |
116
|
|
|
} |
117
|
9 |
|
return new self($critical, ...$attribs->all()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
16 |
|
protected function _valueASN1(): Element |
124
|
|
|
{ |
125
|
16 |
|
if (!count($this->_attributes)) { |
126
|
1 |
|
throw new \LogicException('No attributes'); |
127
|
|
|
} |
128
|
15 |
|
return $this->_attributes->toASN1(); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|