1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\Security\Signature\Model;
use JsonSerializable;
use NCU\Security\Signature\Enum\DigestAlgorithm;
use NCU\Security\Signature\Enum\SignatureAlgorithm;
use NCU\Security\Signature\Exceptions\SignatoryException;
use NCU\Security\Signature\Exceptions\SignatoryNotFoundException;
use NCU\Security\Signature\IOutgoingSignedRequest;
use NCU\Security\Signature\ISignatoryManager;
use NCU\Security\Signature\ISignatureManager;
use OC\Security\Signature\SignatureManager;
/**
* extends ISignedRequest to add info requested at the generation of the signature
*
* @see ISignatureManager for details on signature
* @since 31.0.0
*/
class OutgoingSignedRequest extends SignedRequest implements
IOutgoingSignedRequest,
JsonSerializable {
private string $host = '';
private array $headers = [];
/** @var list<string> $headerList */
private array $headerList = [];
private SignatureAlgorithm $algorithm;
public function __construct(
string $body,
ISignatoryManager $signatoryManager,
private readonly string $identity,
private readonly string $method,
private readonly string $path,
) {
parent::__construct($body);
$options = $signatoryManager->getOptions();
$this->setHost($identity)
->setAlgorithm($options['algorithm'] ?? SignatureAlgorithm::RSA_SHA256)
->setSignatory($signatoryManager->getLocalSignatory())
->setDigestAlgorithm($options['digestAlgorithm'] ?? DigestAlgorithm::SHA256);
$headers = array_merge([
'(request-target)' => strtolower($method) . ' ' . $path,
'content-length' => strlen($this->getBody()),
'date' => gmdate($options['dateHeader'] ?? SignatureManager::DATE_HEADER),
'digest' => $this->getDigest(),
'host' => $this->getHost()
], $options['extraSignatureHeaders'] ?? []);
$signing = $headerList = [];
foreach ($headers as $element => $value) {
$signing[] = $element . ': ' . $value;
$headerList[] = $element;
if ($element !== '(request-target)') {
$this->addHeader($element, $value);
}
}
$this->setHeaderList($headerList)
->setSignatureData($signing);
}
/**
* @inheritDoc
*
* @param string $host
* @return $this
* @since 31.0.0
*/
public function setHost(string $host): self {
$this->host = $host;
return $this;
}
/**
* @inheritDoc
*
* @return string
* @since 31.0.0
*/
public function getHost(): string {
return $this->host;
}
/**
* @inheritDoc
*
* @param string $key
* @param string|int|float $value
*
* @return self
* @since 31.0.0
*/
public function addHeader(string $key, string|int|float $value): self {
$this->headers[$key] = $value;
return $this;
}
/**
* @inheritDoc
*
* @return array
* @since 31.0.0
*/
public function getHeaders(): array {
return $this->headers;
}
/**
* set the ordered list of used headers in the Signature
*
* @param list<string> $list
*
* @return self
* @since 31.0.0
*/
public function setHeaderList(array $list): self {
$this->headerList = $list;
return $this;
}
/**
* returns ordered list of used headers in the Signature
*
* @return list<string>
* @since 31.0.0
*/
public function getHeaderList(): array {
return $this->headerList;
}
/**
* @inheritDoc
*
* @param SignatureAlgorithm $algorithm
*
* @return self
* @since 31.0.0
*/
public function setAlgorithm(SignatureAlgorithm $algorithm): self {
$this->algorithm = $algorithm;
return $this;
}
/**
* @inheritDoc
*
* @return SignatureAlgorithm
* @since 31.0.0
*/
public function getAlgorithm(): SignatureAlgorithm {
return $this->algorithm;
}
/**
* @inheritDoc
*
* @return self
* @throws SignatoryException
* @throws SignatoryNotFoundException
* @since 31.0.0
*/
public function sign(): self {
$privateKey = $this->getSignatory()->getPrivateKey();
if ($privateKey === '') {
throw new SignatoryException('empty private key');
}
openssl_sign(
implode("\n", $this->getSignatureData()),
$signed,
$privateKey,
$this->getAlgorithm()->value
);
$this->setSignature(base64_encode($signed));
$this->setSigningElements(
[
'keyId="' . $this->getSignatory()->getKeyId() . '"',
'algorithm="' . $this->getAlgorithm()->value . '"',
'headers="' . implode(' ', $this->getHeaderList()) . '"',
'signature="' . $this->getSignature() . '"'
]
);
$this->addHeader('Signature', implode(',', $this->getSigningElements()));
return $this;
}
/**
* @param string $clear
* @param string $privateKey
* @param SignatureAlgorithm $algorithm
*
* @return string
* @throws SignatoryException
*/
private function signString(string $clear, string $privateKey, SignatureAlgorithm $algorithm): string {
if ($privateKey === '') {
throw new SignatoryException('empty private key');
}
openssl_sign($clear, $signed, $privateKey, $algorithm->value);
return base64_encode($signed);
}
public function jsonSerialize(): array {
return array_merge(
parent::jsonSerialize(),
[
'host' => $this->host,
'headers' => $this->headers,
'algorithm' => $this->algorithm->value,
'method' => $this->method,
'identity' => $this->identity,
'path' => $this->path,
]
);
}
}
|