-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathKeyController.php
More file actions
316 lines (289 loc) · 10.7 KB
/
KeyController.php
File metadata and controls
316 lines (289 loc) · 10.7 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\EndToEndEncryption\Controller;
use BadMethodCallException;
use Exception;
use OCA\EndToEndEncryption\Exceptions\KeyExistsException;
use OCA\EndToEndEncryption\IKeyStorage;
use OCA\EndToEndEncryption\SignatureHandler;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\AnonRateLimit;
use OCP\AppFramework\Http\Attribute\BruteForceProtection;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCS\OCSBadRequestException;
use OCP\AppFramework\OCS\OCSForbiddenException;
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\AppFramework\OCSController;
use OCP\Files\ForbiddenException;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IL10N;
use OCP\IRequest;
use Psr\Log\LoggerInterface;
class KeyController extends OCSController {
use ThrottleRequestTrait;
private ?string $userId;
private IKeyStorage $keyStorage;
private SignatureHandler $signatureHandler;
private LoggerInterface $logger;
private IL10N $l10n;
public function __construct(string $AppName,
IRequest $request,
?string $userId,
IKeyStorage $keyStorage,
SignatureHandler $signatureHandler,
LoggerInterface $logger,
IL10N $l10n,
) {
parent::__construct($AppName, $request);
$this->userId = $userId;
$this->keyStorage = $keyStorage;
$this->signatureHandler = $signatureHandler;
$this->logger = $logger;
$this->l10n = $l10n;
}
/**
* Get private key
*
* @NoAdminRequired
* @E2ERestrictUserAgent
*
* @return DataResponse<Http::STATUS_OK, array{private-key: string}, array{}>|DataResponse<Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND, array{message: string}, array{}>
* @throws OCSBadRequestException Internal error
* @throws OCSForbiddenException Not allowed to get private key
* @throws OCSNotFoundException Private key not found
*
* 200: Private key returned
*/
#[BruteForceProtection('e2ee')]
public function getPrivateKey(): DataResponse {
try {
$privateKey = $this->keyStorage->getPrivateKey($this->userId);
return new DataResponse(['private-key' => $privateKey]);
} catch (ForbiddenException $e) {
$this->logger->error('Tried to access private key without permission', ['exception' => $e]);
return $this->throttleRequest(Http::STATUS_FORBIDDEN, 'Not allowed to get private key');
} catch (NotFoundException $e) {
$this->logger->warning('Could not find the private key of the user: ' . $this->userId, ['exception' => $e]);
return $this->throttleRequest(Http::STATUS_NOT_FOUND, 'Could not find the private key');
} catch (Exception $e) {
$this->logger->critical($e->getMessage(), ['exception' => $e, 'app' => $this->appName]);
throw new OCSBadRequestException($this->l10n->t('Internal error'));
}
}
/**
* Delete the users private key
*
* @NoAdminRequired
*
* @return DataResponse<Http::STATUS_OK, list<empty>, array{}>
* @throws OCSBadRequestException Internal error
* @throws OCSForbiddenException Not allowed to delete public key
* @throws OCSNotFoundException Private key not found
*
* 200: Private key deleted successfully
*/
public function deletePrivateKey(): DataResponse {
try {
$this->keyStorage->deletePrivateKey($this->userId);
return new DataResponse();
} catch (NotPermittedException $e) {
throw new OCSForbiddenException($this->l10n->t('You are not allowed to delete this private key'));
} catch (NotFoundException $e) {
throw new OCSNotFoundException($this->l10n->t('Could not find the private key belonging to the user %s', [$this->userId]));
} catch (Exception $e) {
$this->logger->critical($e->getMessage(), ['exception' => $e, 'app' => $this->appName]);
throw new OCSBadRequestException($this->l10n->t('Internal error'));
}
}
/**
* Set private key
*
* @NoAdminRequired
* @E2ERestrictUserAgent
*
* @param string $privateKey The new private key
* @return DataResponse<Http::STATUS_OK, array{private-key: string}, array{}>|DataResponse<Http::STATUS_CONFLICT, list<empty>, array{}>
* @throws OCSBadRequestException Internal error
*
* 200: Private key set successfully
* 409: Private key already exists
*/
public function setPrivateKey(string $privateKey): DataResponse {
try {
$this->keyStorage->setPrivateKey($privateKey, $this->userId);
} catch (KeyExistsException $e) {
return new DataResponse([], Http::STATUS_CONFLICT);
} catch (Exception $e) {
$this->logger->critical($e->getMessage(), ['exception' => $e, 'app' => $this->appName]);
throw new OCSBadRequestException($this->l10n->t('Internal error'));
}
return new DataResponse(['private-key' => $privateKey]);
}
/**
* Get public key
*
* @NoAdminRequired
* @E2ERestrictUserAgent
* @param string $users a json encoded list of users
* @return DataResponse<Http::STATUS_OK, array{public-keys: array<string, string>}, array{}>|DataResponse<Http::STATUS_NOT_FOUND, array{message: string}, array{}>
* @throws OCSBadRequestException Internal error
*
* 200: Public keys returned
* 404: Public key for at least one user not found
*/
public function getPublicKeys(string $users = ''): DataResponse {
$usersArray = $this->jsonDecode($users);
$result = ['public-keys' => []];
foreach ($usersArray as $uid) {
try {
$publicKey = $this->keyStorage->getPublicKey($uid);
$result['public-keys'][$uid] = $publicKey;
} catch (NotFoundException $e) {
$this->logger->debug('Could not find the public key of the user: ' . $uid, ['exception' => $e]);
return $this->throttleRequest(Http::STATUS_NOT_FOUND, 'Could not find the public key belonging to the user ' . $uid);
} catch (Exception $e) {
$this->logger->critical($e->getMessage(), ['exception' => $e, 'app' => $this->appName]);
throw new OCSBadRequestException($this->l10n->t('Internal error'));
}
}
return new DataResponse($result);
}
/**
* Create public key, store it on the server and return it to the user
*
* If no public key exists and the request contains a valid certificate
* from the currently logged in user we will create one
*
* @NoAdminRequired
* @E2ERestrictUserAgent
*
* @param string $csr request to create a valid public key
*
* @return DataResponse<Http::STATUS_OK, array{public-key: string}, array{}>|DataResponse<Http::STATUS_CONFLICT, list<empty>, array{}>
* @throws OCSForbiddenException Common name (CN) does not match the current user
* @throws OCSBadRequestException Internal error
*
* 200: Public key created successfully
* 409: Public key already exists
*/
public function createPublicKey(string $csr): DataResponse {
if ($this->keyStorage->publicKeyExists($this->userId)) {
return new DataResponse([], Http::STATUS_CONFLICT);
}
try {
$subject = openssl_csr_get_subject($csr);
$publicKey = $this->signatureHandler->sign($csr);
} catch (BadMethodCallException $e) {
$this->logger->critical($e->getMessage(), ['exception' => $e, 'app' => $this->appName]);
throw new OCSBadRequestException($e->getMessage());
} catch (Exception $e) {
$this->logger->critical($e->getMessage(), ['exception' => $e, 'app' => $this->appName]);
throw new OCSBadRequestException($this->l10n->t('Internal error'));
}
$cn = isset($subject['CN']) ? $subject['CN'] : '';
if ($cn !== $this->userId) {
throw new OCSForbiddenException($this->l10n->t('Common name (CN) does not match the current user'));
}
$this->keyStorage->setPublicKey($publicKey, $this->userId);
return new DataResponse(['public-key' => $publicKey]);
}
/**
* Set public key
*
* @NoAdminRequired
* @E2ERestrictUserAgent
*
* @param string $publicKey The new public key
* @return DataResponse<Http::STATUS_OK, array{public-key: string}, array{}>|DataResponse<Http::STATUS_CONFLICT, list<empty>, array{}>
* @throws OCSBadRequestException Internal error
*
* 200: Public key set successfully
* 409: Public key already exists
*/
public function setPublicKey(string $publicKey): DataResponse {
try {
$this->keyStorage->setPublicKey($publicKey, $this->userId);
} catch (KeyExistsException $e) {
return new DataResponse([], Http::STATUS_CONFLICT);
} catch (Exception $e) {
$this->logger->error('Fail to set user public key', ['exception' => $e, 'app' => $this->appName]);
throw new OCSBadRequestException($this->l10n->t('Internal error'));
}
return new DataResponse(['public-key' => $publicKey]);
}
/**
* Delete the users public key
*
* @NoAdminRequired
*
* @return DataResponse<Http::STATUS_OK, list<empty>, array{}>
*
* @throws OCSForbiddenException Not allowed to delete public key
* @throws OCSBadRequestException Internal error
* @throws OCSNotFoundException Public key not found
*
* 200: Public key deleted successfully
*/
public function deletePublicKey(): ?DataResponse {
try {
$this->keyStorage->deletePublicKey($this->userId);
return new DataResponse();
} catch (NotFoundException $e) {
throw new OCSNotFoundException($this->l10n->t('Could not find the public key belonging to %s', [$this->userId]));
} catch (NotPermittedException $e) {
throw new OCSForbiddenException($this->l10n->t('This is not your public key to delete'));
} catch (Exception $e) {
$this->logger->critical($e->getMessage(), ['exception' => $e, 'app' => $this->appName]);
throw new OCSBadRequestException($this->l10n->t('Internal error'));
}
}
/**
* @NoAdminRequired
* @E2ERestrictUserAgent
*
* Get the public server key so that the clients can verify the
* signature of the users public keys
*
* @return DataResponse<Http::STATUS_OK, array{public-key: string}, array{}>
*
* @throws OCSBadRequestException Internal error
*
* 200: Server public key returned
*/
public function getPublicServerKey(): DataResponse {
try {
$publicKey = $this->signatureHandler->getPublicServerKey();
} catch (Exception $e) {
$this->logger->critical($e->getMessage(), ['exception' => $e, 'app' => $this->appName]);
throw new OCSBadRequestException($this->l10n->t('Internal error'));
}
return new DataResponse(['public-key' => $publicKey]);
}
/**
* Decode JSON-encoded userlist and return an array
* add the currently logged in user if the user isn't part of the list
*
* @param string $users JSON-encoded userlist
* @return array
* @throws OCSBadRequestException
*/
private function jsonDecode(string $users): array {
$usersArray = [];
if (!empty($users)) {
// TODO - use JSON_THROW_ON_ERROR once we require PHP 7.3
$usersArray = \json_decode($users, true);
if ($usersArray === null) {
throw new OCSBadRequestException($this->l10n->t('Cannot decode userlist'));
}
}
if (!in_array($this->userId, $usersArray, true)) {
$usersArray[] = $this->userId;
}
return $usersArray;
}
}