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\Primitive\OctetString; |
10
|
|
|
use Sop\ASN1\Type\UnspecifiedType; |
11
|
|
|
use Sop\X501\ASN1\AttributeValue\AttributeValue; |
12
|
|
|
use Sop\X501\MatchingRule\BinaryMatch; |
13
|
|
|
use Sop\X501\MatchingRule\MatchingRule; |
14
|
|
|
use Sop\X509\GeneralName\GeneralName; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Base class implementing <i>SvceAuthInfo</i> ASN.1 type used by |
18
|
|
|
* attribute certificate attribute values. |
19
|
|
|
* |
20
|
|
|
* @see https://tools.ietf.org/html/rfc5755#section-4.4.1 |
21
|
|
|
*/ |
22
|
|
|
abstract class SvceAuthInfo extends AttributeValue |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Service. |
26
|
|
|
* |
27
|
|
|
* @var GeneralName |
28
|
|
|
*/ |
29
|
|
|
protected $_service; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Ident. |
33
|
|
|
* |
34
|
|
|
* @var GeneralName |
35
|
|
|
*/ |
36
|
|
|
protected $_ident; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Auth info. |
40
|
|
|
* |
41
|
|
|
* @var null|string |
42
|
|
|
*/ |
43
|
|
|
protected $_authInfo; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Constructor. |
47
|
|
|
* |
48
|
|
|
* @param GeneralName $service |
49
|
|
|
* @param GeneralName $ident |
50
|
|
|
* @param null|string $auth_info |
51
|
|
|
*/ |
52
|
9 |
|
public function __construct(GeneralName $service, GeneralName $ident, |
53
|
|
|
?string $auth_info = null) |
54
|
|
|
{ |
55
|
9 |
|
$this->_service = $service; |
56
|
9 |
|
$this->_ident = $ident; |
57
|
9 |
|
$this->_authInfo = $auth_info; |
58
|
9 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
* |
63
|
|
|
* @return self |
64
|
|
|
*/ |
65
|
5 |
|
public static function fromASN1(UnspecifiedType $el): AttributeValue |
66
|
|
|
{ |
67
|
5 |
|
$seq = $el->asSequence(); |
68
|
5 |
|
$service = GeneralName::fromASN1($seq->at(0)->asTagged()); |
69
|
5 |
|
$ident = GeneralName::fromASN1($seq->at(1)->asTagged()); |
70
|
5 |
|
$auth_info = null; |
71
|
5 |
|
if ($seq->has(2, Element::TYPE_OCTET_STRING)) { |
72
|
3 |
|
$auth_info = $seq->at(2)->asString()->string(); |
73
|
|
|
} |
74
|
5 |
|
return new static($service, $ident, $auth_info); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get service name. |
79
|
|
|
* |
80
|
|
|
* @return GeneralName |
81
|
|
|
*/ |
82
|
2 |
|
public function service(): GeneralName |
83
|
|
|
{ |
84
|
2 |
|
return $this->_service; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get ident. |
89
|
|
|
* |
90
|
|
|
* @return GeneralName |
91
|
|
|
*/ |
92
|
2 |
|
public function ident(): GeneralName |
93
|
|
|
{ |
94
|
2 |
|
return $this->_ident; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Check whether authentication info is present. |
99
|
|
|
* |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
2 |
|
public function hasAuthInfo(): bool |
103
|
|
|
{ |
104
|
2 |
|
return isset($this->_authInfo); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get authentication info. |
109
|
|
|
* |
110
|
|
|
* @throws \LogicException If not set |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
2 |
|
public function authInfo(): string |
115
|
|
|
{ |
116
|
2 |
|
if (!$this->hasAuthInfo()) { |
117
|
1 |
|
throw new \LogicException('authInfo not set.'); |
118
|
|
|
} |
119
|
1 |
|
return $this->_authInfo; |
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
9 |
|
public function toASN1(): Element |
126
|
|
|
{ |
127
|
9 |
|
$elements = [$this->_service->toASN1(), $this->_ident->toASN1()]; |
128
|
9 |
|
if (isset($this->_authInfo)) { |
129
|
4 |
|
$elements[] = new OctetString($this->_authInfo); |
|
|
|
|
130
|
|
|
} |
131
|
9 |
|
return new Sequence(...$elements); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
3 |
|
public function stringValue(): string |
138
|
|
|
{ |
139
|
3 |
|
return '#' . bin2hex($this->toASN1()->toDER()); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
1 |
|
public function equalityMatchingRule(): MatchingRule |
146
|
|
|
{ |
147
|
1 |
|
return new BinaryMatch(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
1 |
|
public function rfc2253String(): string |
154
|
|
|
{ |
155
|
1 |
|
return $this->stringValue(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
1 |
|
protected function _transcodedString(): string |
162
|
|
|
{ |
163
|
1 |
|
return $this->stringValue(); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|