1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Sop\X509\AttributeCertificate; |
6
|
|
|
|
7
|
|
|
use Sop\ASN1\Type\Constructed\Sequence; |
8
|
|
|
use Sop\ASN1\Type\UnspecifiedType; |
9
|
|
|
use Sop\X501\ASN1\Attribute; |
10
|
|
|
use Sop\X501\ASN1\AttributeType; |
11
|
|
|
use Sop\X501\ASN1\AttributeValue\AttributeValue; |
12
|
|
|
use Sop\X509\AttributeCertificate\Attribute\AccessIdentityAttributeValue; |
13
|
|
|
use Sop\X509\AttributeCertificate\Attribute\AuthenticationInfoAttributeValue; |
14
|
|
|
use Sop\X509\AttributeCertificate\Attribute\ChargingIdentityAttributeValue; |
15
|
|
|
use Sop\X509\AttributeCertificate\Attribute\GroupAttributeValue; |
16
|
|
|
use Sop\X509\AttributeCertificate\Attribute\RoleAttributeValue; |
17
|
|
|
use Sop\X509\Feature\AttributeContainer; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Implements *Attributes* ASN.1 type as a *SEQUENCE OF Attribute*. |
21
|
|
|
* |
22
|
|
|
* Used in *AttributeCertificateInfo*. |
23
|
|
|
* |
24
|
|
|
* @see https://tools.ietf.org/html/rfc5755#section-4.1 |
25
|
|
|
* @see https://tools.ietf.org/html/rfc5755#section-4.2.7 |
26
|
|
|
*/ |
27
|
|
|
class Attributes implements \Countable, \IteratorAggregate |
28
|
|
|
{ |
29
|
|
|
use AttributeContainer; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Mapping from OID to attribute value class name. |
33
|
|
|
* |
34
|
|
|
* @internal |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
const MAP_OID_TO_CLASS = [ |
39
|
|
|
AccessIdentityAttributeValue::OID => AccessIdentityAttributeValue::class, |
40
|
|
|
AuthenticationInfoAttributeValue::OID => AuthenticationInfoAttributeValue::class, |
41
|
|
|
ChargingIdentityAttributeValue::OID => ChargingIdentityAttributeValue::class, |
42
|
|
|
GroupAttributeValue::OID => GroupAttributeValue::class, |
43
|
|
|
AttributeType::OID_ROLE => RoleAttributeValue::class, |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Constructor. |
48
|
|
|
* |
49
|
|
|
* @param Attribute ...$attribs |
50
|
|
|
*/ |
51
|
|
|
public function __construct(Attribute ...$attribs) |
52
|
|
|
{ |
53
|
14 |
|
$this->_attributes = $attribs; |
54
|
|
|
} |
55
|
14 |
|
|
56
|
14 |
|
/** |
57
|
|
|
* Initialize from attribute values. |
58
|
|
|
* |
59
|
|
|
* @param AttributeValue ...$values |
60
|
|
|
* |
61
|
|
|
* @return self |
62
|
|
|
*/ |
63
|
|
|
public static function fromAttributeValues(AttributeValue ...$values): self |
64
|
8 |
|
{ |
65
|
|
|
$attribs = array_map( |
66
|
8 |
|
function (AttributeValue $value) { |
67
|
8 |
|
return $value->toAttribute(); |
68
|
8 |
|
}, $values); |
69
|
8 |
|
return new self(...$attribs); |
70
|
8 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Initialize from ASN.1. |
74
|
|
|
* |
75
|
|
|
* @param Sequence $seq |
76
|
|
|
* |
77
|
|
|
* @return self |
78
|
|
|
*/ |
79
|
6 |
|
public static function fromASN1(Sequence $seq): self |
80
|
|
|
{ |
81
|
6 |
|
$attribs = array_map( |
82
|
6 |
|
function (UnspecifiedType $el) { |
83
|
6 |
|
return Attribute::fromASN1($el->asSequence()); |
84
|
6 |
|
}, $seq->elements()); |
85
|
|
|
// cast attributes |
86
|
6 |
|
$attribs = array_map( |
87
|
6 |
|
function (Attribute $attr) { |
88
|
6 |
|
$oid = $attr->oid(); |
89
|
6 |
|
if (array_key_exists($oid, self::MAP_OID_TO_CLASS)) { |
90
|
6 |
|
$cls = self::MAP_OID_TO_CLASS[$oid]; |
91
|
6 |
|
$attr = $attr->castValues($cls); |
92
|
|
|
} |
93
|
6 |
|
return $attr; |
94
|
6 |
|
}, $attribs); |
95
|
6 |
|
return new self(...$attribs); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Check whether 'Access Identity' attribute is present. |
100
|
|
|
* |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
1 |
|
public function hasAccessIdentity(): bool |
104
|
|
|
{ |
105
|
1 |
|
return $this->has(AccessIdentityAttributeValue::OID); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get the first 'Access Identity' attribute value. |
110
|
|
|
* |
111
|
|
|
* @return AccessIdentityAttributeValue |
112
|
|
|
*/ |
113
|
1 |
|
public function accessIdentity(): AccessIdentityAttributeValue |
114
|
|
|
{ |
115
|
1 |
|
return $this->firstOf(AccessIdentityAttributeValue::OID)->first(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Check whether 'Service Authentication Information' attribute is present. |
120
|
|
|
* |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
1 |
|
public function hasAuthenticationInformation(): bool |
124
|
|
|
{ |
125
|
1 |
|
return $this->has(AuthenticationInfoAttributeValue::OID); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get the first 'Service Authentication Information' attribute value. |
130
|
|
|
* |
131
|
|
|
* @return AuthenticationInfoAttributeValue |
132
|
|
|
*/ |
133
|
1 |
|
public function authenticationInformation(): AuthenticationInfoAttributeValue |
134
|
|
|
{ |
135
|
1 |
|
return $this->firstOf(AuthenticationInfoAttributeValue::OID)->first(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Check whether 'Charging Identity' attribute is present. |
140
|
|
|
* |
141
|
|
|
* @return bool |
142
|
|
|
*/ |
143
|
1 |
|
public function hasChargingIdentity(): bool |
144
|
|
|
{ |
145
|
1 |
|
return $this->has(ChargingIdentityAttributeValue::OID); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get the first 'Charging Identity' attribute value. |
150
|
|
|
* |
151
|
|
|
* @return ChargingIdentityAttributeValue |
152
|
|
|
*/ |
153
|
1 |
|
public function chargingIdentity(): ChargingIdentityAttributeValue |
154
|
|
|
{ |
155
|
1 |
|
return $this->firstOf(ChargingIdentityAttributeValue::OID)->first(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Check whether 'Group' attribute is present. |
160
|
|
|
* |
161
|
|
|
* @return bool |
162
|
|
|
*/ |
163
|
1 |
|
public function hasGroup(): bool |
164
|
|
|
{ |
165
|
1 |
|
return $this->has(GroupAttributeValue::OID); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Get the first 'Group' attribute value. |
170
|
|
|
* |
171
|
|
|
* @return GroupAttributeValue |
172
|
|
|
*/ |
173
|
1 |
|
public function group(): GroupAttributeValue |
174
|
|
|
{ |
175
|
1 |
|
return $this->firstOf(GroupAttributeValue::OID)->first(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Check whether 'Role' attribute is present. |
180
|
|
|
* |
181
|
|
|
* @return bool |
182
|
|
|
*/ |
183
|
1 |
|
public function hasRole(): bool |
184
|
|
|
{ |
185
|
1 |
|
return $this->has(AttributeType::OID_ROLE); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Get the first 'Role' attribute value. |
190
|
|
|
* |
191
|
|
|
* @return RoleAttributeValue |
192
|
|
|
*/ |
193
|
1 |
|
public function role(): RoleAttributeValue |
194
|
|
|
{ |
195
|
1 |
|
return $this->firstOf(AttributeType::OID_ROLE)->first(); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Get all 'Role' attribute values. |
200
|
|
|
* |
201
|
|
|
* @return RoleAttributeValue[] |
202
|
|
|
*/ |
203
|
2 |
|
public function roles(): array |
204
|
|
|
{ |
205
|
2 |
|
return array_merge([], |
206
|
2 |
|
...array_map( |
207
|
2 |
|
function (Attribute $attr) { |
208
|
2 |
|
return $attr->values(); |
209
|
2 |
|
}, $this->allOf(AttributeType::OID_ROLE))); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Generate ASN.1 structure. |
214
|
|
|
* |
215
|
|
|
* @return Sequence |
216
|
|
|
*/ |
217
|
20 |
|
public function toASN1(): Sequence |
218
|
|
|
{ |
219
|
20 |
|
$elements = array_map( |
220
|
20 |
|
function (Attribute $attr) { |
221
|
12 |
|
return $attr->toASN1(); |
222
|
20 |
|
}, array_values($this->_attributes)); |
223
|
20 |
|
return new Sequence(...$elements); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|