1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Sop\X509\AttributeCertificate\Attribute; |
6
|
|
|
|
7
|
|
|
use Sop\ASN1\Element; |
8
|
|
|
use Sop\ASN1\Type\Constructed\Sequence; |
9
|
|
|
use Sop\ASN1\Type\Tagged\ExplicitlyTaggedType; |
10
|
|
|
use Sop\ASN1\Type\Tagged\ImplicitlyTaggedType; |
11
|
|
|
use Sop\ASN1\Type\UnspecifiedType; |
12
|
|
|
use Sop\X501\ASN1\AttributeType; |
13
|
|
|
use Sop\X501\ASN1\AttributeValue\AttributeValue; |
14
|
|
|
use Sop\X501\MatchingRule\BinaryMatch; |
15
|
|
|
use Sop\X501\MatchingRule\MatchingRule; |
16
|
|
|
use Sop\X509\GeneralName\GeneralName; |
17
|
|
|
use Sop\X509\GeneralName\GeneralNames; |
18
|
|
|
use Sop\X509\GeneralName\UniformResourceIdentifier; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Implements value for 'Role' attribute. |
22
|
|
|
* |
23
|
|
|
* @see https://tools.ietf.org/html/rfc5755#section-4.4.5 |
24
|
|
|
*/ |
25
|
|
|
class RoleAttributeValue extends AttributeValue |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Issuing authority. |
29
|
|
|
* |
30
|
|
|
* @var null|GeneralNames |
31
|
|
|
*/ |
32
|
|
|
protected $_roleAuthority; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Role name. |
36
|
|
|
* |
37
|
|
|
* @var GeneralName |
38
|
|
|
*/ |
39
|
|
|
protected $_roleName; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Constructor. |
43
|
|
|
* |
44
|
|
|
* @param GeneralName $name Role name |
45
|
|
|
* @param null|GeneralNames $authority Issuing authority |
46
|
|
|
*/ |
47
|
15 |
|
public function __construct(GeneralName $name, |
48
|
|
|
?GeneralNames $authority = null) |
49
|
|
|
{ |
50
|
15 |
|
$this->_roleAuthority = $authority; |
51
|
15 |
|
$this->_roleName = $name; |
52
|
15 |
|
$this->_oid = AttributeType::OID_ROLE; |
53
|
15 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Initialize from a role string. |
57
|
|
|
* |
58
|
|
|
* @param string $role_name Role name in URI format |
59
|
|
|
* @param null|GeneralNames $authority Issuing authority |
60
|
|
|
* |
61
|
|
|
* @return self |
62
|
|
|
*/ |
63
|
2 |
|
public static function fromString(string $role_name, |
64
|
|
|
?GeneralNames $authority = null): self |
65
|
|
|
{ |
66
|
2 |
|
return new self(new UniformResourceIdentifier($role_name), $authority); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
* |
72
|
|
|
* @return self |
73
|
|
|
*/ |
74
|
8 |
|
public static function fromASN1(UnspecifiedType $el): AttributeValue |
75
|
|
|
{ |
76
|
8 |
|
$seq = $el->asSequence(); |
77
|
8 |
|
$authority = null; |
78
|
8 |
|
if ($seq->hasTagged(0)) { |
79
|
1 |
|
$authority = GeneralNames::fromASN1( |
80
|
1 |
|
$seq->getTagged(0)->asImplicit(Element::TYPE_SEQUENCE) |
81
|
1 |
|
->asSequence()); |
82
|
|
|
} |
83
|
8 |
|
$name = GeneralName::fromASN1($seq->getTagged(1) |
84
|
8 |
|
->asExplicit()->asTagged()); |
85
|
8 |
|
return new self($name, $authority); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Check whether issuing authority is present. |
90
|
|
|
* |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
2 |
|
public function hasRoleAuthority(): bool |
94
|
|
|
{ |
95
|
2 |
|
return isset($this->_roleAuthority); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get issuing authority. |
100
|
|
|
* |
101
|
|
|
* @throws \LogicException If not set |
102
|
|
|
* |
103
|
|
|
* @return GeneralNames |
104
|
|
|
*/ |
105
|
2 |
|
public function roleAuthority(): GeneralNames |
106
|
|
|
{ |
107
|
2 |
|
if (!$this->hasRoleAuthority()) { |
108
|
1 |
|
throw new \LogicException('roleAuthority not set.'); |
109
|
|
|
} |
110
|
1 |
|
return $this->_roleAuthority; |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get role name. |
115
|
|
|
* |
116
|
|
|
* @return GeneralName |
117
|
|
|
*/ |
118
|
2 |
|
public function roleName(): GeneralName |
119
|
|
|
{ |
120
|
2 |
|
return $this->_roleName; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
17 |
|
public function toASN1(): Element |
127
|
|
|
{ |
128
|
17 |
|
$elements = []; |
129
|
17 |
|
if (isset($this->_roleAuthority)) { |
130
|
4 |
|
$elements[] = new ImplicitlyTaggedType( |
131
|
4 |
|
0, $this->_roleAuthority->toASN1()); |
|
|
|
|
132
|
|
|
} |
133
|
17 |
|
$elements[] = new ExplicitlyTaggedType( |
134
|
17 |
|
1, $this->_roleName->toASN1()); |
135
|
17 |
|
return new Sequence(...$elements); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* {@inheritdoc} |
140
|
|
|
*/ |
141
|
3 |
|
public function stringValue(): string |
142
|
|
|
{ |
143
|
3 |
|
return '#' . bin2hex($this->toASN1()->toDER()); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritdoc} |
148
|
|
|
*/ |
149
|
1 |
|
public function equalityMatchingRule(): MatchingRule |
150
|
|
|
{ |
151
|
1 |
|
return new BinaryMatch(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* {@inheritdoc} |
156
|
|
|
*/ |
157
|
1 |
|
public function rfc2253String(): string |
158
|
|
|
{ |
159
|
1 |
|
return $this->stringValue(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* {@inheritdoc} |
164
|
|
|
*/ |
165
|
1 |
|
protected function _transcodedString(): string |
166
|
|
|
{ |
167
|
1 |
|
return $this->stringValue(); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|