1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Sop\X509\CertificationRequest; |
6
|
|
|
|
7
|
|
|
use Sop\ASN1\Type\Constructed\Set; |
8
|
|
|
use Sop\ASN1\Type\UnspecifiedType; |
9
|
|
|
use Sop\X501\ASN1\Attribute; |
10
|
|
|
use Sop\X501\ASN1\AttributeValue\AttributeValue; |
11
|
|
|
use Sop\X509\CertificationRequest\Attribute\ExtensionRequestValue; |
12
|
|
|
use Sop\X509\Feature\AttributeContainer; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Implements <i>Attributes</i> ASN.1 type as a <i>SET OF Attribute</i>. |
16
|
|
|
* |
17
|
|
|
* Used in <i>CertificationRequestInfo</i>. |
18
|
|
|
* |
19
|
|
|
* @see https://tools.ietf.org/html/rfc2986#section-4 |
20
|
|
|
*/ |
21
|
|
|
class Attributes implements \Countable, \IteratorAggregate |
22
|
|
|
{ |
23
|
|
|
use AttributeContainer; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Mapping from OID to attribute value class name. |
27
|
|
|
* |
28
|
|
|
* @internal |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
const MAP_OID_TO_CLASS = [ |
33
|
|
|
ExtensionRequestValue::OID => ExtensionRequestValue::class, |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Constructor. |
38
|
|
|
* |
39
|
|
|
* @param Attribute ...$attribs Attribute objects |
40
|
|
|
*/ |
41
|
8 |
|
public function __construct(Attribute ...$attribs) |
42
|
|
|
{ |
43
|
8 |
|
$this->_attributes = $attribs; |
44
|
8 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Initialize from attribute values. |
48
|
|
|
* |
49
|
|
|
* @param AttributeValue ...$values |
50
|
|
|
* |
51
|
|
|
* @return self |
52
|
|
|
*/ |
53
|
1 |
|
public static function fromAttributeValues(AttributeValue ...$values): Attributes |
54
|
|
|
{ |
55
|
1 |
|
$attribs = array_map( |
56
|
|
|
function (AttributeValue $value) { |
57
|
1 |
|
return $value->toAttribute(); |
58
|
1 |
|
}, $values); |
59
|
1 |
|
return new self(...$attribs); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Initialize from ASN.1. |
64
|
|
|
* |
65
|
|
|
* @param Set $set |
66
|
|
|
* |
67
|
|
|
* @return self |
68
|
|
|
*/ |
69
|
4 |
|
public static function fromASN1(Set $set): Attributes |
70
|
|
|
{ |
71
|
4 |
|
$attribs = array_map( |
72
|
|
|
function (UnspecifiedType $el) { |
73
|
4 |
|
return Attribute::fromASN1($el->asSequence()); |
74
|
4 |
|
}, $set->elements()); |
75
|
|
|
// cast attributes |
76
|
4 |
|
$attribs = array_map( |
77
|
|
|
function (Attribute $attr) { |
78
|
4 |
|
$oid = $attr->oid(); |
79
|
4 |
|
if (array_key_exists($oid, self::MAP_OID_TO_CLASS)) { |
80
|
4 |
|
$cls = self::MAP_OID_TO_CLASS[$oid]; |
81
|
4 |
|
return $attr->castValues($cls); |
82
|
|
|
} |
83
|
1 |
|
return $attr; |
84
|
4 |
|
}, $attribs); |
85
|
4 |
|
return new self(...$attribs); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Check whether extension request attribute is present. |
90
|
|
|
* |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
6 |
|
public function hasExtensionRequest(): bool |
94
|
|
|
{ |
95
|
6 |
|
return $this->has(ExtensionRequestValue::OID); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get extension request attribute value. |
100
|
|
|
* |
101
|
|
|
* @throws \LogicException |
102
|
|
|
* |
103
|
|
|
* @return ExtensionRequestValue |
104
|
|
|
*/ |
105
|
4 |
|
public function extensionRequest(): ExtensionRequestValue |
106
|
|
|
{ |
107
|
4 |
|
if (!$this->hasExtensionRequest()) { |
108
|
1 |
|
throw new \LogicException('No extension request attribute.'); |
109
|
|
|
} |
110
|
3 |
|
return $this->firstOf(ExtensionRequestValue::OID)->first(); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Generate ASN.1 structure. |
115
|
|
|
* |
116
|
|
|
* @return Set |
117
|
|
|
*/ |
118
|
5 |
|
public function toASN1(): Set |
119
|
|
|
{ |
120
|
5 |
|
$elements = array_map( |
121
|
|
|
function (Attribute $attr) { |
122
|
5 |
|
return $attr->toASN1(); |
123
|
5 |
|
}, array_values($this->_attributes)); |
124
|
5 |
|
$set = new Set(...$elements); |
125
|
5 |
|
return $set->sortedSetOf(); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|