Completed
Pull Request — master (#4030)
by Björn
23:35 queued 09:11
created
apps/encryption/lib/KeyManager.php 2 patches
Indentation   +672 added lines, -672 removed lines patch added patch discarded remove patch
@@ -38,676 +38,676 @@
 block discarded – undo
38 38
 
39 39
 class KeyManager {
40 40
 
41
-	/**
42
-	 * @var Session
43
-	 */
44
-	protected $session;
45
-	/**
46
-	 * @var IStorage
47
-	 */
48
-	private $keyStorage;
49
-	/**
50
-	 * @var Crypt
51
-	 */
52
-	private $crypt;
53
-	/**
54
-	 * @var string
55
-	 */
56
-	private $recoveryKeyId;
57
-	/**
58
-	 * @var string
59
-	 */
60
-	private $publicShareKeyId;
61
-	/**
62
-	 * @var string
63
-	 */
64
-	private $masterKeyId;
65
-	/**
66
-	 * @var string UserID
67
-	 */
68
-	private $keyId;
69
-	/**
70
-	 * @var string
71
-	 */
72
-	private $publicKeyId = 'publicKey';
73
-	/**
74
-	 * @var string
75
-	 */
76
-	private $privateKeyId = 'privateKey';
77
-
78
-	/**
79
-	 * @var string
80
-	 */
81
-	private $shareKeyId = 'shareKey';
82
-
83
-	/**
84
-	 * @var string
85
-	 */
86
-	private $fileKeyId = 'fileKey';
87
-	/**
88
-	 * @var IConfig
89
-	 */
90
-	private $config;
91
-	/**
92
-	 * @var ILogger
93
-	 */
94
-	private $log;
95
-	/**
96
-	 * @var Util
97
-	 */
98
-	private $util;
99
-
100
-	/**
101
-	 * @param IStorage $keyStorage
102
-	 * @param Crypt $crypt
103
-	 * @param IConfig $config
104
-	 * @param IUserSession $userSession
105
-	 * @param Session $session
106
-	 * @param ILogger $log
107
-	 * @param Util $util
108
-	 */
109
-	public function __construct(
110
-		IStorage $keyStorage,
111
-		Crypt $crypt,
112
-		IConfig $config,
113
-		IUserSession $userSession,
114
-		Session $session,
115
-		ILogger $log,
116
-		Util $util
117
-	) {
118
-
119
-		$this->util = $util;
120
-		$this->session = $session;
121
-		$this->keyStorage = $keyStorage;
122
-		$this->crypt = $crypt;
123
-		$this->config = $config;
124
-		$this->log = $log;
125
-
126
-		$this->recoveryKeyId = $this->config->getAppValue('encryption',
127
-			'recoveryKeyId');
128
-		if (empty($this->recoveryKeyId)) {
129
-			$this->recoveryKeyId = 'recoveryKey_' . substr(md5(time()), 0, 8);
130
-			$this->config->setAppValue('encryption',
131
-				'recoveryKeyId',
132
-				$this->recoveryKeyId);
133
-		}
134
-
135
-		$this->publicShareKeyId = $this->config->getAppValue('encryption',
136
-			'publicShareKeyId');
137
-		if (empty($this->publicShareKeyId)) {
138
-			$this->publicShareKeyId = 'pubShare_' . substr(md5(time()), 0, 8);
139
-			$this->config->setAppValue('encryption', 'publicShareKeyId', $this->publicShareKeyId);
140
-		}
141
-
142
-		$this->masterKeyId = $this->config->getAppValue('encryption',
143
-			'masterKeyId');
144
-		if (empty($this->masterKeyId)) {
145
-			$this->masterKeyId = 'master_' . substr(md5(time()), 0, 8);
146
-			$this->config->setAppValue('encryption', 'masterKeyId', $this->masterKeyId);
147
-		}
148
-
149
-		$this->keyId = $userSession && $userSession->isLoggedIn() ? $userSession->getUser()->getUID() : false;
150
-		$this->log = $log;
151
-	}
152
-
153
-	/**
154
-	 * check if key pair for public link shares exists, if not we create one
155
-	 */
156
-	public function validateShareKey() {
157
-		$shareKey = $this->getPublicShareKey();
158
-		if (empty($shareKey)) {
159
-			$keyPair = $this->crypt->createKeyPair();
160
-
161
-			// Save public key
162
-			$this->keyStorage->setSystemUserKey(
163
-				$this->publicShareKeyId . '.publicKey', $keyPair['publicKey'],
164
-				Encryption::ID);
165
-
166
-			// Encrypt private key empty passphrase
167
-			$encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], '');
168
-			$header = $this->crypt->generateHeader();
169
-			$this->setSystemPrivateKey($this->publicShareKeyId, $header . $encryptedKey);
170
-		}
171
-	}
172
-
173
-	/**
174
-	 * check if a key pair for the master key exists, if not we create one
175
-	 */
176
-	public function validateMasterKey() {
177
-
178
-		if ($this->util->isMasterKeyEnabled() === false) {
179
-			return;
180
-		}
181
-
182
-		$masterKey = $this->getPublicMasterKey();
183
-		if (empty($masterKey)) {
184
-			$keyPair = $this->crypt->createKeyPair();
185
-
186
-			// Save public key
187
-			$this->keyStorage->setSystemUserKey(
188
-				$this->masterKeyId . '.publicKey', $keyPair['publicKey'],
189
-				Encryption::ID);
190
-
191
-			// Encrypt private key with system password
192
-			$encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $this->getMasterKeyPassword(), $this->masterKeyId);
193
-			$header = $this->crypt->generateHeader();
194
-			$this->setSystemPrivateKey($this->masterKeyId, $header . $encryptedKey);
195
-		}
196
-	}
197
-
198
-	/**
199
-	 * @return bool
200
-	 */
201
-	public function recoveryKeyExists() {
202
-		$key = $this->getRecoveryKey();
203
-		return (!empty($key));
204
-	}
205
-
206
-	/**
207
-	 * get recovery key
208
-	 *
209
-	 * @return string
210
-	 */
211
-	public function getRecoveryKey() {
212
-		return $this->keyStorage->getSystemUserKey($this->recoveryKeyId . '.publicKey', Encryption::ID);
213
-	}
214
-
215
-	/**
216
-	 * get recovery key ID
217
-	 *
218
-	 * @return string
219
-	 */
220
-	public function getRecoveryKeyId() {
221
-		return $this->recoveryKeyId;
222
-	}
223
-
224
-	/**
225
-	 * @param string $password
226
-	 * @return bool
227
-	 */
228
-	public function checkRecoveryPassword($password) {
229
-		$recoveryKey = $this->keyStorage->getSystemUserKey($this->recoveryKeyId . '.privateKey', Encryption::ID);
230
-		$decryptedRecoveryKey = $this->crypt->decryptPrivateKey($recoveryKey, $password);
231
-
232
-		if ($decryptedRecoveryKey) {
233
-			return true;
234
-		}
235
-		return false;
236
-	}
237
-
238
-	/**
239
-	 * @param string $uid
240
-	 * @param string $password
241
-	 * @param string $keyPair
242
-	 * @return bool
243
-	 */
244
-	public function storeKeyPair($uid, $password, $keyPair) {
245
-		// Save Public Key
246
-		$this->setPublicKey($uid, $keyPair['publicKey']);
247
-
248
-		$encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $password, $uid);
249
-
250
-		$header = $this->crypt->generateHeader();
251
-
252
-		if ($encryptedKey) {
253
-			$this->setPrivateKey($uid, $header . $encryptedKey);
254
-			return true;
255
-		}
256
-		return false;
257
-	}
258
-
259
-	/**
260
-	 * @param string $password
261
-	 * @param array $keyPair
262
-	 * @return bool
263
-	 */
264
-	public function setRecoveryKey($password, $keyPair) {
265
-		// Save Public Key
266
-		$this->keyStorage->setSystemUserKey($this->getRecoveryKeyId().
267
-			'.publicKey',
268
-			$keyPair['publicKey'],
269
-			Encryption::ID);
270
-
271
-		$encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $password);
272
-		$header = $this->crypt->generateHeader();
273
-
274
-		if ($encryptedKey) {
275
-			$this->setSystemPrivateKey($this->getRecoveryKeyId(), $header . $encryptedKey);
276
-			return true;
277
-		}
278
-		return false;
279
-	}
280
-
281
-	/**
282
-	 * @param $userId
283
-	 * @param $key
284
-	 * @return bool
285
-	 */
286
-	public function setPublicKey($userId, $key) {
287
-		return $this->keyStorage->setUserKey($userId, $this->publicKeyId, $key, Encryption::ID);
288
-	}
289
-
290
-	/**
291
-	 * @param $userId
292
-	 * @param string $key
293
-	 * @return bool
294
-	 */
295
-	public function setPrivateKey($userId, $key) {
296
-		return $this->keyStorage->setUserKey($userId,
297
-			$this->privateKeyId,
298
-			$key,
299
-			Encryption::ID);
300
-	}
301
-
302
-	/**
303
-	 * write file key to key storage
304
-	 *
305
-	 * @param string $path
306
-	 * @param string $key
307
-	 * @return boolean
308
-	 */
309
-	public function setFileKey($path, $key) {
310
-		return $this->keyStorage->setFileKey($path, $this->fileKeyId, $key, Encryption::ID);
311
-	}
312
-
313
-	/**
314
-	 * set all file keys (the file key and the corresponding share keys)
315
-	 *
316
-	 * @param string $path
317
-	 * @param array $keys
318
-	 */
319
-	public function setAllFileKeys($path, $keys) {
320
-		$this->setFileKey($path, $keys['data']);
321
-		foreach ($keys['keys'] as $uid => $keyFile) {
322
-			$this->setShareKey($path, $uid, $keyFile);
323
-		}
324
-	}
325
-
326
-	/**
327
-	 * write share key to the key storage
328
-	 *
329
-	 * @param string $path
330
-	 * @param string $uid
331
-	 * @param string $key
332
-	 * @return boolean
333
-	 */
334
-	public function setShareKey($path, $uid, $key) {
335
-		$keyId = $uid . '.' . $this->shareKeyId;
336
-		return $this->keyStorage->setFileKey($path, $keyId, $key, Encryption::ID);
337
-	}
338
-
339
-	/**
340
-	 * Decrypt private key and store it
341
-	 *
342
-	 * @param string $uid user id
343
-	 * @param string $passPhrase users password
344
-	 * @return boolean
345
-	 */
346
-	public function init($uid, $passPhrase) {
347
-
348
-		$this->session->setStatus(Session::INIT_EXECUTED);
349
-
350
-		try {
351
-			if($this->util->isMasterKeyEnabled()) {
352
-				$uid = $this->getMasterKeyId();
353
-				$passPhrase = $this->getMasterKeyPassword();
354
-				$privateKey = $this->getSystemPrivateKey($uid);
355
-			} else {
356
-				$privateKey = $this->getPrivateKey($uid);
357
-			}
358
-			$privateKey = $this->crypt->decryptPrivateKey($privateKey, $passPhrase, $uid);
359
-		} catch (PrivateKeyMissingException $e) {
360
-			return false;
361
-		} catch (DecryptionFailedException $e) {
362
-			return false;
363
-		} catch (\Exception $e) {
364
-			$this->log->warning(
365
-				'Could not decrypt the private key from user "' . $uid . '"" during login. ' .
366
-				'Assume password change on the user back-end. Error message: '
367
-				. $e->getMessage()
368
-			);
369
-			return false;
370
-		}
371
-
372
-		if ($privateKey) {
373
-			$this->session->setPrivateKey($privateKey);
374
-			$this->session->setStatus(Session::INIT_SUCCESSFUL);
375
-			return true;
376
-		}
377
-
378
-		return false;
379
-	}
380
-
381
-	/**
382
-	 * @param $userId
383
-	 * @return string
384
-	 * @throws PrivateKeyMissingException
385
-	 */
386
-	public function getPrivateKey($userId) {
387
-		$privateKey = $this->keyStorage->getUserKey($userId,
388
-			$this->privateKeyId, Encryption::ID);
389
-
390
-		if (strlen($privateKey) !== 0) {
391
-			return $privateKey;
392
-		}
393
-		throw new PrivateKeyMissingException($userId);
394
-	}
395
-
396
-	/**
397
-	 * @param string $path
398
-	 * @param $uid
399
-	 * @return string
400
-	 */
401
-	public function getFileKey($path, $uid) {
402
-		if ($uid === '') {
403
-			$uid = null;
404
-		}
405
-		$publicAccess = is_null($uid);
406
-		$encryptedFileKey = $this->keyStorage->getFileKey($path, $this->fileKeyId, Encryption::ID);
407
-
408
-		if (empty($encryptedFileKey)) {
409
-			return '';
410
-		}
411
-
412
-		if ($this->util->isMasterKeyEnabled()) {
413
-			$uid = $this->getMasterKeyId();
414
-			$shareKey = $this->getShareKey($path, $uid);
415
-			if ($publicAccess) {
416
-				$privateKey = $this->getSystemPrivateKey($uid);
417
-				$privateKey = $this->crypt->decryptPrivateKey($privateKey, $this->getMasterKeyPassword(), $uid);
418
-			} else {
419
-				// when logged in, the master key is already decrypted in the session
420
-				$privateKey = $this->session->getPrivateKey();
421
-			}
422
-		} else if ($publicAccess) {
423
-			// use public share key for public links
424
-			$uid = $this->getPublicShareKeyId();
425
-			$shareKey = $this->getShareKey($path, $uid);
426
-			$privateKey = $this->keyStorage->getSystemUserKey($this->publicShareKeyId . '.privateKey', Encryption::ID);
427
-			$privateKey = $this->crypt->decryptPrivateKey($privateKey);
428
-		} else {
429
-			$shareKey = $this->getShareKey($path, $uid);
430
-			$privateKey = $this->session->getPrivateKey();
431
-		}
432
-
433
-		if ($encryptedFileKey && $shareKey && $privateKey) {
434
-			return $this->crypt->multiKeyDecrypt($encryptedFileKey,
435
-				$shareKey,
436
-				$privateKey);
437
-		}
438
-
439
-		return '';
440
-	}
441
-
442
-	/**
443
-	 * Get the current version of a file
444
-	 *
445
-	 * @param string $path
446
-	 * @param View $view
447
-	 * @return int
448
-	 */
449
-	public function getVersion($path, View $view) {
450
-		$fileInfo = $view->getFileInfo($path);
451
-		if($fileInfo === false) {
452
-			return 0;
453
-		}
454
-		return $fileInfo->getEncryptedVersion();
455
-	}
456
-
457
-	/**
458
-	 * Set the current version of a file
459
-	 *
460
-	 * @param string $path
461
-	 * @param int $version
462
-	 * @param View $view
463
-	 */
464
-	public function setVersion($path, $version, View $view) {
465
-		$fileInfo= $view->getFileInfo($path);
466
-
467
-		if($fileInfo !== false) {
468
-			$cache = $fileInfo->getStorage()->getCache();
469
-			$cache->update($fileInfo->getId(), ['encrypted' => $version, 'encryptedVersion' => $version]);
470
-		}
471
-	}
472
-
473
-	/**
474
-	 * get the encrypted file key
475
-	 *
476
-	 * @param string $path
477
-	 * @return string
478
-	 */
479
-	public function getEncryptedFileKey($path) {
480
-		$encryptedFileKey = $this->keyStorage->getFileKey($path,
481
-			$this->fileKeyId, Encryption::ID);
482
-
483
-		return $encryptedFileKey;
484
-	}
485
-
486
-	/**
487
-	 * delete share key
488
-	 *
489
-	 * @param string $path
490
-	 * @param string $keyId
491
-	 * @return boolean
492
-	 */
493
-	public function deleteShareKey($path, $keyId) {
494
-		return $this->keyStorage->deleteFileKey(
495
-			$path,
496
-			$keyId . '.' . $this->shareKeyId,
497
-			Encryption::ID);
498
-	}
499
-
500
-
501
-	/**
502
-	 * @param $path
503
-	 * @param $uid
504
-	 * @return mixed
505
-	 */
506
-	public function getShareKey($path, $uid) {
507
-		$keyId = $uid . '.' . $this->shareKeyId;
508
-		return $this->keyStorage->getFileKey($path, $keyId, Encryption::ID);
509
-	}
510
-
511
-	/**
512
-	 * check if user has a private and a public key
513
-	 *
514
-	 * @param string $userId
515
-	 * @return bool
516
-	 * @throws PrivateKeyMissingException
517
-	 * @throws PublicKeyMissingException
518
-	 */
519
-	public function userHasKeys($userId) {
520
-		$privateKey = $publicKey = true;
521
-		$exception = null;
522
-
523
-		try {
524
-			$this->getPrivateKey($userId);
525
-		} catch (PrivateKeyMissingException $e) {
526
-			$privateKey = false;
527
-			$exception = $e;
528
-		}
529
-		try {
530
-			$this->getPublicKey($userId);
531
-		} catch (PublicKeyMissingException $e) {
532
-			$publicKey = false;
533
-			$exception = $e;
534
-		}
535
-
536
-		if ($privateKey && $publicKey) {
537
-			return true;
538
-		} elseif (!$privateKey && !$publicKey) {
539
-			return false;
540
-		} else {
541
-			throw $exception;
542
-		}
543
-	}
544
-
545
-	/**
546
-	 * @param $userId
547
-	 * @return mixed
548
-	 * @throws PublicKeyMissingException
549
-	 */
550
-	public function getPublicKey($userId) {
551
-		$publicKey = $this->keyStorage->getUserKey($userId, $this->publicKeyId, Encryption::ID);
552
-
553
-		if (strlen($publicKey) !== 0) {
554
-			return $publicKey;
555
-		}
556
-		throw new PublicKeyMissingException($userId);
557
-	}
558
-
559
-	public function getPublicShareKeyId() {
560
-		return $this->publicShareKeyId;
561
-	}
562
-
563
-	/**
564
-	 * get public key for public link shares
565
-	 *
566
-	 * @return string
567
-	 */
568
-	public function getPublicShareKey() {
569
-		return $this->keyStorage->getSystemUserKey($this->publicShareKeyId . '.publicKey', Encryption::ID);
570
-	}
571
-
572
-	/**
573
-	 * @param string $purpose
574
-	 * @param string $uid
575
-	 */
576
-	public function backupUserKeys($purpose, $uid) {
577
-		$this->keyStorage->backupUserKeys(Encryption::ID, $purpose, $uid);
578
-	}
579
-
580
-	/**
581
-	 * creat a backup of the users private and public key and then  delete it
582
-	 *
583
-	 * @param string $uid
584
-	 */
585
-	public function deleteUserKeys($uid) {
586
-		$this->deletePublicKey($uid);
587
-		$this->deletePrivateKey($uid);
588
-	}
589
-
590
-	/**
591
-	 * @param $uid
592
-	 * @return bool
593
-	 */
594
-	public function deletePublicKey($uid) {
595
-		return $this->keyStorage->deleteUserKey($uid, $this->publicKeyId, Encryption::ID);
596
-	}
597
-
598
-	/**
599
-	 * @param string $uid
600
-	 * @return bool
601
-	 */
602
-	private function deletePrivateKey($uid) {
603
-		return $this->keyStorage->deleteUserKey($uid, $this->privateKeyId, Encryption::ID);
604
-	}
605
-
606
-	/**
607
-	 * @param string $path
608
-	 * @return bool
609
-	 */
610
-	public function deleteAllFileKeys($path) {
611
-		return $this->keyStorage->deleteAllFileKeys($path);
612
-	}
613
-
614
-	/**
615
-	 * @param array $userIds
616
-	 * @return array
617
-	 * @throws PublicKeyMissingException
618
-	 */
619
-	public function getPublicKeys(array $userIds) {
620
-		$keys = [];
621
-
622
-		foreach ($userIds as $userId) {
623
-			try {
624
-				$keys[$userId] = $this->getPublicKey($userId);
625
-			} catch (PublicKeyMissingException $e) {
626
-				continue;
627
-			}
628
-		}
629
-
630
-		return $keys;
631
-
632
-	}
633
-
634
-	/**
635
-	 * @param string $keyId
636
-	 * @return string returns openssl key
637
-	 */
638
-	public function getSystemPrivateKey($keyId) {
639
-		return $this->keyStorage->getSystemUserKey($keyId . '.' . $this->privateKeyId, Encryption::ID);
640
-	}
641
-
642
-	/**
643
-	 * @param string $keyId
644
-	 * @param string $key
645
-	 * @return string returns openssl key
646
-	 */
647
-	public function setSystemPrivateKey($keyId, $key) {
648
-		return $this->keyStorage->setSystemUserKey(
649
-			$keyId . '.' . $this->privateKeyId,
650
-			$key,
651
-			Encryption::ID);
652
-	}
653
-
654
-	/**
655
-	 * add system keys such as the public share key and the recovery key
656
-	 *
657
-	 * @param array $accessList
658
-	 * @param array $publicKeys
659
-	 * @param string $uid
660
-	 * @return array
661
-	 * @throws PublicKeyMissingException
662
-	 */
663
-	public function addSystemKeys(array $accessList, array $publicKeys, $uid) {
664
-		if (!empty($accessList['public'])) {
665
-			$publicShareKey = $this->getPublicShareKey();
666
-			if (empty($publicShareKey)) {
667
-				throw new PublicKeyMissingException($this->getPublicShareKeyId());
668
-			}
669
-			$publicKeys[$this->getPublicShareKeyId()] = $publicShareKey;
670
-		}
671
-
672
-		if ($this->recoveryKeyExists() &&
673
-			$this->util->isRecoveryEnabledForUser($uid)) {
674
-
675
-			$publicKeys[$this->getRecoveryKeyId()] = $this->getRecoveryKey();
676
-		}
677
-
678
-		return $publicKeys;
679
-	}
680
-
681
-	/**
682
-	 * get master key password
683
-	 *
684
-	 * @return string
685
-	 * @throws \Exception
686
-	 */
687
-	public function getMasterKeyPassword() {
688
-		$password = $this->config->getSystemValue('secret');
689
-		if (empty($password)){
690
-			throw new \Exception('Can not get secret from ownCloud instance');
691
-		}
692
-
693
-		return $password;
694
-	}
695
-
696
-	/**
697
-	 * return master key id
698
-	 *
699
-	 * @return string
700
-	 */
701
-	public function getMasterKeyId() {
702
-		return $this->masterKeyId;
703
-	}
704
-
705
-	/**
706
-	 * get public master key
707
-	 *
708
-	 * @return string
709
-	 */
710
-	public function getPublicMasterKey() {
711
-		return $this->keyStorage->getSystemUserKey($this->masterKeyId . '.publicKey', Encryption::ID);
712
-	}
41
+    /**
42
+     * @var Session
43
+     */
44
+    protected $session;
45
+    /**
46
+     * @var IStorage
47
+     */
48
+    private $keyStorage;
49
+    /**
50
+     * @var Crypt
51
+     */
52
+    private $crypt;
53
+    /**
54
+     * @var string
55
+     */
56
+    private $recoveryKeyId;
57
+    /**
58
+     * @var string
59
+     */
60
+    private $publicShareKeyId;
61
+    /**
62
+     * @var string
63
+     */
64
+    private $masterKeyId;
65
+    /**
66
+     * @var string UserID
67
+     */
68
+    private $keyId;
69
+    /**
70
+     * @var string
71
+     */
72
+    private $publicKeyId = 'publicKey';
73
+    /**
74
+     * @var string
75
+     */
76
+    private $privateKeyId = 'privateKey';
77
+
78
+    /**
79
+     * @var string
80
+     */
81
+    private $shareKeyId = 'shareKey';
82
+
83
+    /**
84
+     * @var string
85
+     */
86
+    private $fileKeyId = 'fileKey';
87
+    /**
88
+     * @var IConfig
89
+     */
90
+    private $config;
91
+    /**
92
+     * @var ILogger
93
+     */
94
+    private $log;
95
+    /**
96
+     * @var Util
97
+     */
98
+    private $util;
99
+
100
+    /**
101
+     * @param IStorage $keyStorage
102
+     * @param Crypt $crypt
103
+     * @param IConfig $config
104
+     * @param IUserSession $userSession
105
+     * @param Session $session
106
+     * @param ILogger $log
107
+     * @param Util $util
108
+     */
109
+    public function __construct(
110
+        IStorage $keyStorage,
111
+        Crypt $crypt,
112
+        IConfig $config,
113
+        IUserSession $userSession,
114
+        Session $session,
115
+        ILogger $log,
116
+        Util $util
117
+    ) {
118
+
119
+        $this->util = $util;
120
+        $this->session = $session;
121
+        $this->keyStorage = $keyStorage;
122
+        $this->crypt = $crypt;
123
+        $this->config = $config;
124
+        $this->log = $log;
125
+
126
+        $this->recoveryKeyId = $this->config->getAppValue('encryption',
127
+            'recoveryKeyId');
128
+        if (empty($this->recoveryKeyId)) {
129
+            $this->recoveryKeyId = 'recoveryKey_' . substr(md5(time()), 0, 8);
130
+            $this->config->setAppValue('encryption',
131
+                'recoveryKeyId',
132
+                $this->recoveryKeyId);
133
+        }
134
+
135
+        $this->publicShareKeyId = $this->config->getAppValue('encryption',
136
+            'publicShareKeyId');
137
+        if (empty($this->publicShareKeyId)) {
138
+            $this->publicShareKeyId = 'pubShare_' . substr(md5(time()), 0, 8);
139
+            $this->config->setAppValue('encryption', 'publicShareKeyId', $this->publicShareKeyId);
140
+        }
141
+
142
+        $this->masterKeyId = $this->config->getAppValue('encryption',
143
+            'masterKeyId');
144
+        if (empty($this->masterKeyId)) {
145
+            $this->masterKeyId = 'master_' . substr(md5(time()), 0, 8);
146
+            $this->config->setAppValue('encryption', 'masterKeyId', $this->masterKeyId);
147
+        }
148
+
149
+        $this->keyId = $userSession && $userSession->isLoggedIn() ? $userSession->getUser()->getUID() : false;
150
+        $this->log = $log;
151
+    }
152
+
153
+    /**
154
+     * check if key pair for public link shares exists, if not we create one
155
+     */
156
+    public function validateShareKey() {
157
+        $shareKey = $this->getPublicShareKey();
158
+        if (empty($shareKey)) {
159
+            $keyPair = $this->crypt->createKeyPair();
160
+
161
+            // Save public key
162
+            $this->keyStorage->setSystemUserKey(
163
+                $this->publicShareKeyId . '.publicKey', $keyPair['publicKey'],
164
+                Encryption::ID);
165
+
166
+            // Encrypt private key empty passphrase
167
+            $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], '');
168
+            $header = $this->crypt->generateHeader();
169
+            $this->setSystemPrivateKey($this->publicShareKeyId, $header . $encryptedKey);
170
+        }
171
+    }
172
+
173
+    /**
174
+     * check if a key pair for the master key exists, if not we create one
175
+     */
176
+    public function validateMasterKey() {
177
+
178
+        if ($this->util->isMasterKeyEnabled() === false) {
179
+            return;
180
+        }
181
+
182
+        $masterKey = $this->getPublicMasterKey();
183
+        if (empty($masterKey)) {
184
+            $keyPair = $this->crypt->createKeyPair();
185
+
186
+            // Save public key
187
+            $this->keyStorage->setSystemUserKey(
188
+                $this->masterKeyId . '.publicKey', $keyPair['publicKey'],
189
+                Encryption::ID);
190
+
191
+            // Encrypt private key with system password
192
+            $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $this->getMasterKeyPassword(), $this->masterKeyId);
193
+            $header = $this->crypt->generateHeader();
194
+            $this->setSystemPrivateKey($this->masterKeyId, $header . $encryptedKey);
195
+        }
196
+    }
197
+
198
+    /**
199
+     * @return bool
200
+     */
201
+    public function recoveryKeyExists() {
202
+        $key = $this->getRecoveryKey();
203
+        return (!empty($key));
204
+    }
205
+
206
+    /**
207
+     * get recovery key
208
+     *
209
+     * @return string
210
+     */
211
+    public function getRecoveryKey() {
212
+        return $this->keyStorage->getSystemUserKey($this->recoveryKeyId . '.publicKey', Encryption::ID);
213
+    }
214
+
215
+    /**
216
+     * get recovery key ID
217
+     *
218
+     * @return string
219
+     */
220
+    public function getRecoveryKeyId() {
221
+        return $this->recoveryKeyId;
222
+    }
223
+
224
+    /**
225
+     * @param string $password
226
+     * @return bool
227
+     */
228
+    public function checkRecoveryPassword($password) {
229
+        $recoveryKey = $this->keyStorage->getSystemUserKey($this->recoveryKeyId . '.privateKey', Encryption::ID);
230
+        $decryptedRecoveryKey = $this->crypt->decryptPrivateKey($recoveryKey, $password);
231
+
232
+        if ($decryptedRecoveryKey) {
233
+            return true;
234
+        }
235
+        return false;
236
+    }
237
+
238
+    /**
239
+     * @param string $uid
240
+     * @param string $password
241
+     * @param string $keyPair
242
+     * @return bool
243
+     */
244
+    public function storeKeyPair($uid, $password, $keyPair) {
245
+        // Save Public Key
246
+        $this->setPublicKey($uid, $keyPair['publicKey']);
247
+
248
+        $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $password, $uid);
249
+
250
+        $header = $this->crypt->generateHeader();
251
+
252
+        if ($encryptedKey) {
253
+            $this->setPrivateKey($uid, $header . $encryptedKey);
254
+            return true;
255
+        }
256
+        return false;
257
+    }
258
+
259
+    /**
260
+     * @param string $password
261
+     * @param array $keyPair
262
+     * @return bool
263
+     */
264
+    public function setRecoveryKey($password, $keyPair) {
265
+        // Save Public Key
266
+        $this->keyStorage->setSystemUserKey($this->getRecoveryKeyId().
267
+            '.publicKey',
268
+            $keyPair['publicKey'],
269
+            Encryption::ID);
270
+
271
+        $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $password);
272
+        $header = $this->crypt->generateHeader();
273
+
274
+        if ($encryptedKey) {
275
+            $this->setSystemPrivateKey($this->getRecoveryKeyId(), $header . $encryptedKey);
276
+            return true;
277
+        }
278
+        return false;
279
+    }
280
+
281
+    /**
282
+     * @param $userId
283
+     * @param $key
284
+     * @return bool
285
+     */
286
+    public function setPublicKey($userId, $key) {
287
+        return $this->keyStorage->setUserKey($userId, $this->publicKeyId, $key, Encryption::ID);
288
+    }
289
+
290
+    /**
291
+     * @param $userId
292
+     * @param string $key
293
+     * @return bool
294
+     */
295
+    public function setPrivateKey($userId, $key) {
296
+        return $this->keyStorage->setUserKey($userId,
297
+            $this->privateKeyId,
298
+            $key,
299
+            Encryption::ID);
300
+    }
301
+
302
+    /**
303
+     * write file key to key storage
304
+     *
305
+     * @param string $path
306
+     * @param string $key
307
+     * @return boolean
308
+     */
309
+    public function setFileKey($path, $key) {
310
+        return $this->keyStorage->setFileKey($path, $this->fileKeyId, $key, Encryption::ID);
311
+    }
312
+
313
+    /**
314
+     * set all file keys (the file key and the corresponding share keys)
315
+     *
316
+     * @param string $path
317
+     * @param array $keys
318
+     */
319
+    public function setAllFileKeys($path, $keys) {
320
+        $this->setFileKey($path, $keys['data']);
321
+        foreach ($keys['keys'] as $uid => $keyFile) {
322
+            $this->setShareKey($path, $uid, $keyFile);
323
+        }
324
+    }
325
+
326
+    /**
327
+     * write share key to the key storage
328
+     *
329
+     * @param string $path
330
+     * @param string $uid
331
+     * @param string $key
332
+     * @return boolean
333
+     */
334
+    public function setShareKey($path, $uid, $key) {
335
+        $keyId = $uid . '.' . $this->shareKeyId;
336
+        return $this->keyStorage->setFileKey($path, $keyId, $key, Encryption::ID);
337
+    }
338
+
339
+    /**
340
+     * Decrypt private key and store it
341
+     *
342
+     * @param string $uid user id
343
+     * @param string $passPhrase users password
344
+     * @return boolean
345
+     */
346
+    public function init($uid, $passPhrase) {
347
+
348
+        $this->session->setStatus(Session::INIT_EXECUTED);
349
+
350
+        try {
351
+            if($this->util->isMasterKeyEnabled()) {
352
+                $uid = $this->getMasterKeyId();
353
+                $passPhrase = $this->getMasterKeyPassword();
354
+                $privateKey = $this->getSystemPrivateKey($uid);
355
+            } else {
356
+                $privateKey = $this->getPrivateKey($uid);
357
+            }
358
+            $privateKey = $this->crypt->decryptPrivateKey($privateKey, $passPhrase, $uid);
359
+        } catch (PrivateKeyMissingException $e) {
360
+            return false;
361
+        } catch (DecryptionFailedException $e) {
362
+            return false;
363
+        } catch (\Exception $e) {
364
+            $this->log->warning(
365
+                'Could not decrypt the private key from user "' . $uid . '"" during login. ' .
366
+                'Assume password change on the user back-end. Error message: '
367
+                . $e->getMessage()
368
+            );
369
+            return false;
370
+        }
371
+
372
+        if ($privateKey) {
373
+            $this->session->setPrivateKey($privateKey);
374
+            $this->session->setStatus(Session::INIT_SUCCESSFUL);
375
+            return true;
376
+        }
377
+
378
+        return false;
379
+    }
380
+
381
+    /**
382
+     * @param $userId
383
+     * @return string
384
+     * @throws PrivateKeyMissingException
385
+     */
386
+    public function getPrivateKey($userId) {
387
+        $privateKey = $this->keyStorage->getUserKey($userId,
388
+            $this->privateKeyId, Encryption::ID);
389
+
390
+        if (strlen($privateKey) !== 0) {
391
+            return $privateKey;
392
+        }
393
+        throw new PrivateKeyMissingException($userId);
394
+    }
395
+
396
+    /**
397
+     * @param string $path
398
+     * @param $uid
399
+     * @return string
400
+     */
401
+    public function getFileKey($path, $uid) {
402
+        if ($uid === '') {
403
+            $uid = null;
404
+        }
405
+        $publicAccess = is_null($uid);
406
+        $encryptedFileKey = $this->keyStorage->getFileKey($path, $this->fileKeyId, Encryption::ID);
407
+
408
+        if (empty($encryptedFileKey)) {
409
+            return '';
410
+        }
411
+
412
+        if ($this->util->isMasterKeyEnabled()) {
413
+            $uid = $this->getMasterKeyId();
414
+            $shareKey = $this->getShareKey($path, $uid);
415
+            if ($publicAccess) {
416
+                $privateKey = $this->getSystemPrivateKey($uid);
417
+                $privateKey = $this->crypt->decryptPrivateKey($privateKey, $this->getMasterKeyPassword(), $uid);
418
+            } else {
419
+                // when logged in, the master key is already decrypted in the session
420
+                $privateKey = $this->session->getPrivateKey();
421
+            }
422
+        } else if ($publicAccess) {
423
+            // use public share key for public links
424
+            $uid = $this->getPublicShareKeyId();
425
+            $shareKey = $this->getShareKey($path, $uid);
426
+            $privateKey = $this->keyStorage->getSystemUserKey($this->publicShareKeyId . '.privateKey', Encryption::ID);
427
+            $privateKey = $this->crypt->decryptPrivateKey($privateKey);
428
+        } else {
429
+            $shareKey = $this->getShareKey($path, $uid);
430
+            $privateKey = $this->session->getPrivateKey();
431
+        }
432
+
433
+        if ($encryptedFileKey && $shareKey && $privateKey) {
434
+            return $this->crypt->multiKeyDecrypt($encryptedFileKey,
435
+                $shareKey,
436
+                $privateKey);
437
+        }
438
+
439
+        return '';
440
+    }
441
+
442
+    /**
443
+     * Get the current version of a file
444
+     *
445
+     * @param string $path
446
+     * @param View $view
447
+     * @return int
448
+     */
449
+    public function getVersion($path, View $view) {
450
+        $fileInfo = $view->getFileInfo($path);
451
+        if($fileInfo === false) {
452
+            return 0;
453
+        }
454
+        return $fileInfo->getEncryptedVersion();
455
+    }
456
+
457
+    /**
458
+     * Set the current version of a file
459
+     *
460
+     * @param string $path
461
+     * @param int $version
462
+     * @param View $view
463
+     */
464
+    public function setVersion($path, $version, View $view) {
465
+        $fileInfo= $view->getFileInfo($path);
466
+
467
+        if($fileInfo !== false) {
468
+            $cache = $fileInfo->getStorage()->getCache();
469
+            $cache->update($fileInfo->getId(), ['encrypted' => $version, 'encryptedVersion' => $version]);
470
+        }
471
+    }
472
+
473
+    /**
474
+     * get the encrypted file key
475
+     *
476
+     * @param string $path
477
+     * @return string
478
+     */
479
+    public function getEncryptedFileKey($path) {
480
+        $encryptedFileKey = $this->keyStorage->getFileKey($path,
481
+            $this->fileKeyId, Encryption::ID);
482
+
483
+        return $encryptedFileKey;
484
+    }
485
+
486
+    /**
487
+     * delete share key
488
+     *
489
+     * @param string $path
490
+     * @param string $keyId
491
+     * @return boolean
492
+     */
493
+    public function deleteShareKey($path, $keyId) {
494
+        return $this->keyStorage->deleteFileKey(
495
+            $path,
496
+            $keyId . '.' . $this->shareKeyId,
497
+            Encryption::ID);
498
+    }
499
+
500
+
501
+    /**
502
+     * @param $path
503
+     * @param $uid
504
+     * @return mixed
505
+     */
506
+    public function getShareKey($path, $uid) {
507
+        $keyId = $uid . '.' . $this->shareKeyId;
508
+        return $this->keyStorage->getFileKey($path, $keyId, Encryption::ID);
509
+    }
510
+
511
+    /**
512
+     * check if user has a private and a public key
513
+     *
514
+     * @param string $userId
515
+     * @return bool
516
+     * @throws PrivateKeyMissingException
517
+     * @throws PublicKeyMissingException
518
+     */
519
+    public function userHasKeys($userId) {
520
+        $privateKey = $publicKey = true;
521
+        $exception = null;
522
+
523
+        try {
524
+            $this->getPrivateKey($userId);
525
+        } catch (PrivateKeyMissingException $e) {
526
+            $privateKey = false;
527
+            $exception = $e;
528
+        }
529
+        try {
530
+            $this->getPublicKey($userId);
531
+        } catch (PublicKeyMissingException $e) {
532
+            $publicKey = false;
533
+            $exception = $e;
534
+        }
535
+
536
+        if ($privateKey && $publicKey) {
537
+            return true;
538
+        } elseif (!$privateKey && !$publicKey) {
539
+            return false;
540
+        } else {
541
+            throw $exception;
542
+        }
543
+    }
544
+
545
+    /**
546
+     * @param $userId
547
+     * @return mixed
548
+     * @throws PublicKeyMissingException
549
+     */
550
+    public function getPublicKey($userId) {
551
+        $publicKey = $this->keyStorage->getUserKey($userId, $this->publicKeyId, Encryption::ID);
552
+
553
+        if (strlen($publicKey) !== 0) {
554
+            return $publicKey;
555
+        }
556
+        throw new PublicKeyMissingException($userId);
557
+    }
558
+
559
+    public function getPublicShareKeyId() {
560
+        return $this->publicShareKeyId;
561
+    }
562
+
563
+    /**
564
+     * get public key for public link shares
565
+     *
566
+     * @return string
567
+     */
568
+    public function getPublicShareKey() {
569
+        return $this->keyStorage->getSystemUserKey($this->publicShareKeyId . '.publicKey', Encryption::ID);
570
+    }
571
+
572
+    /**
573
+     * @param string $purpose
574
+     * @param string $uid
575
+     */
576
+    public function backupUserKeys($purpose, $uid) {
577
+        $this->keyStorage->backupUserKeys(Encryption::ID, $purpose, $uid);
578
+    }
579
+
580
+    /**
581
+     * creat a backup of the users private and public key and then  delete it
582
+     *
583
+     * @param string $uid
584
+     */
585
+    public function deleteUserKeys($uid) {
586
+        $this->deletePublicKey($uid);
587
+        $this->deletePrivateKey($uid);
588
+    }
589
+
590
+    /**
591
+     * @param $uid
592
+     * @return bool
593
+     */
594
+    public function deletePublicKey($uid) {
595
+        return $this->keyStorage->deleteUserKey($uid, $this->publicKeyId, Encryption::ID);
596
+    }
597
+
598
+    /**
599
+     * @param string $uid
600
+     * @return bool
601
+     */
602
+    private function deletePrivateKey($uid) {
603
+        return $this->keyStorage->deleteUserKey($uid, $this->privateKeyId, Encryption::ID);
604
+    }
605
+
606
+    /**
607
+     * @param string $path
608
+     * @return bool
609
+     */
610
+    public function deleteAllFileKeys($path) {
611
+        return $this->keyStorage->deleteAllFileKeys($path);
612
+    }
613
+
614
+    /**
615
+     * @param array $userIds
616
+     * @return array
617
+     * @throws PublicKeyMissingException
618
+     */
619
+    public function getPublicKeys(array $userIds) {
620
+        $keys = [];
621
+
622
+        foreach ($userIds as $userId) {
623
+            try {
624
+                $keys[$userId] = $this->getPublicKey($userId);
625
+            } catch (PublicKeyMissingException $e) {
626
+                continue;
627
+            }
628
+        }
629
+
630
+        return $keys;
631
+
632
+    }
633
+
634
+    /**
635
+     * @param string $keyId
636
+     * @return string returns openssl key
637
+     */
638
+    public function getSystemPrivateKey($keyId) {
639
+        return $this->keyStorage->getSystemUserKey($keyId . '.' . $this->privateKeyId, Encryption::ID);
640
+    }
641
+
642
+    /**
643
+     * @param string $keyId
644
+     * @param string $key
645
+     * @return string returns openssl key
646
+     */
647
+    public function setSystemPrivateKey($keyId, $key) {
648
+        return $this->keyStorage->setSystemUserKey(
649
+            $keyId . '.' . $this->privateKeyId,
650
+            $key,
651
+            Encryption::ID);
652
+    }
653
+
654
+    /**
655
+     * add system keys such as the public share key and the recovery key
656
+     *
657
+     * @param array $accessList
658
+     * @param array $publicKeys
659
+     * @param string $uid
660
+     * @return array
661
+     * @throws PublicKeyMissingException
662
+     */
663
+    public function addSystemKeys(array $accessList, array $publicKeys, $uid) {
664
+        if (!empty($accessList['public'])) {
665
+            $publicShareKey = $this->getPublicShareKey();
666
+            if (empty($publicShareKey)) {
667
+                throw new PublicKeyMissingException($this->getPublicShareKeyId());
668
+            }
669
+            $publicKeys[$this->getPublicShareKeyId()] = $publicShareKey;
670
+        }
671
+
672
+        if ($this->recoveryKeyExists() &&
673
+            $this->util->isRecoveryEnabledForUser($uid)) {
674
+
675
+            $publicKeys[$this->getRecoveryKeyId()] = $this->getRecoveryKey();
676
+        }
677
+
678
+        return $publicKeys;
679
+    }
680
+
681
+    /**
682
+     * get master key password
683
+     *
684
+     * @return string
685
+     * @throws \Exception
686
+     */
687
+    public function getMasterKeyPassword() {
688
+        $password = $this->config->getSystemValue('secret');
689
+        if (empty($password)){
690
+            throw new \Exception('Can not get secret from ownCloud instance');
691
+        }
692
+
693
+        return $password;
694
+    }
695
+
696
+    /**
697
+     * return master key id
698
+     *
699
+     * @return string
700
+     */
701
+    public function getMasterKeyId() {
702
+        return $this->masterKeyId;
703
+    }
704
+
705
+    /**
706
+     * get public master key
707
+     *
708
+     * @return string
709
+     */
710
+    public function getPublicMasterKey() {
711
+        return $this->keyStorage->getSystemUserKey($this->masterKeyId . '.publicKey', Encryption::ID);
712
+    }
713 713
 }
Please login to merge, or discard this patch.
Spacing   +25 added lines, -25 removed lines patch added patch discarded remove patch
@@ -126,7 +126,7 @@  discard block
 block discarded – undo
126 126
 		$this->recoveryKeyId = $this->config->getAppValue('encryption',
127 127
 			'recoveryKeyId');
128 128
 		if (empty($this->recoveryKeyId)) {
129
-			$this->recoveryKeyId = 'recoveryKey_' . substr(md5(time()), 0, 8);
129
+			$this->recoveryKeyId = 'recoveryKey_'.substr(md5(time()), 0, 8);
130 130
 			$this->config->setAppValue('encryption',
131 131
 				'recoveryKeyId',
132 132
 				$this->recoveryKeyId);
@@ -135,14 +135,14 @@  discard block
 block discarded – undo
135 135
 		$this->publicShareKeyId = $this->config->getAppValue('encryption',
136 136
 			'publicShareKeyId');
137 137
 		if (empty($this->publicShareKeyId)) {
138
-			$this->publicShareKeyId = 'pubShare_' . substr(md5(time()), 0, 8);
138
+			$this->publicShareKeyId = 'pubShare_'.substr(md5(time()), 0, 8);
139 139
 			$this->config->setAppValue('encryption', 'publicShareKeyId', $this->publicShareKeyId);
140 140
 		}
141 141
 
142 142
 		$this->masterKeyId = $this->config->getAppValue('encryption',
143 143
 			'masterKeyId');
144 144
 		if (empty($this->masterKeyId)) {
145
-			$this->masterKeyId = 'master_' . substr(md5(time()), 0, 8);
145
+			$this->masterKeyId = 'master_'.substr(md5(time()), 0, 8);
146 146
 			$this->config->setAppValue('encryption', 'masterKeyId', $this->masterKeyId);
147 147
 		}
148 148
 
@@ -160,13 +160,13 @@  discard block
 block discarded – undo
160 160
 
161 161
 			// Save public key
162 162
 			$this->keyStorage->setSystemUserKey(
163
-				$this->publicShareKeyId . '.publicKey', $keyPair['publicKey'],
163
+				$this->publicShareKeyId.'.publicKey', $keyPair['publicKey'],
164 164
 				Encryption::ID);
165 165
 
166 166
 			// Encrypt private key empty passphrase
167 167
 			$encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], '');
168 168
 			$header = $this->crypt->generateHeader();
169
-			$this->setSystemPrivateKey($this->publicShareKeyId, $header . $encryptedKey);
169
+			$this->setSystemPrivateKey($this->publicShareKeyId, $header.$encryptedKey);
170 170
 		}
171 171
 	}
172 172
 
@@ -185,13 +185,13 @@  discard block
 block discarded – undo
185 185
 
186 186
 			// Save public key
187 187
 			$this->keyStorage->setSystemUserKey(
188
-				$this->masterKeyId . '.publicKey', $keyPair['publicKey'],
188
+				$this->masterKeyId.'.publicKey', $keyPair['publicKey'],
189 189
 				Encryption::ID);
190 190
 
191 191
 			// Encrypt private key with system password
192 192
 			$encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $this->getMasterKeyPassword(), $this->masterKeyId);
193 193
 			$header = $this->crypt->generateHeader();
194
-			$this->setSystemPrivateKey($this->masterKeyId, $header . $encryptedKey);
194
+			$this->setSystemPrivateKey($this->masterKeyId, $header.$encryptedKey);
195 195
 		}
196 196
 	}
197 197
 
@@ -209,7 +209,7 @@  discard block
 block discarded – undo
209 209
 	 * @return string
210 210
 	 */
211 211
 	public function getRecoveryKey() {
212
-		return $this->keyStorage->getSystemUserKey($this->recoveryKeyId . '.publicKey', Encryption::ID);
212
+		return $this->keyStorage->getSystemUserKey($this->recoveryKeyId.'.publicKey', Encryption::ID);
213 213
 	}
214 214
 
215 215
 	/**
@@ -226,7 +226,7 @@  discard block
 block discarded – undo
226 226
 	 * @return bool
227 227
 	 */
228 228
 	public function checkRecoveryPassword($password) {
229
-		$recoveryKey = $this->keyStorage->getSystemUserKey($this->recoveryKeyId . '.privateKey', Encryption::ID);
229
+		$recoveryKey = $this->keyStorage->getSystemUserKey($this->recoveryKeyId.'.privateKey', Encryption::ID);
230 230
 		$decryptedRecoveryKey = $this->crypt->decryptPrivateKey($recoveryKey, $password);
231 231
 
232 232
 		if ($decryptedRecoveryKey) {
@@ -250,7 +250,7 @@  discard block
 block discarded – undo
250 250
 		$header = $this->crypt->generateHeader();
251 251
 
252 252
 		if ($encryptedKey) {
253
-			$this->setPrivateKey($uid, $header . $encryptedKey);
253
+			$this->setPrivateKey($uid, $header.$encryptedKey);
254 254
 			return true;
255 255
 		}
256 256
 		return false;
@@ -272,7 +272,7 @@  discard block
 block discarded – undo
272 272
 		$header = $this->crypt->generateHeader();
273 273
 
274 274
 		if ($encryptedKey) {
275
-			$this->setSystemPrivateKey($this->getRecoveryKeyId(), $header . $encryptedKey);
275
+			$this->setSystemPrivateKey($this->getRecoveryKeyId(), $header.$encryptedKey);
276 276
 			return true;
277 277
 		}
278 278
 		return false;
@@ -332,7 +332,7 @@  discard block
 block discarded – undo
332 332
 	 * @return boolean
333 333
 	 */
334 334
 	public function setShareKey($path, $uid, $key) {
335
-		$keyId = $uid . '.' . $this->shareKeyId;
335
+		$keyId = $uid.'.'.$this->shareKeyId;
336 336
 		return $this->keyStorage->setFileKey($path, $keyId, $key, Encryption::ID);
337 337
 	}
338 338
 
@@ -348,7 +348,7 @@  discard block
 block discarded – undo
348 348
 		$this->session->setStatus(Session::INIT_EXECUTED);
349 349
 
350 350
 		try {
351
-			if($this->util->isMasterKeyEnabled()) {
351
+			if ($this->util->isMasterKeyEnabled()) {
352 352
 				$uid = $this->getMasterKeyId();
353 353
 				$passPhrase = $this->getMasterKeyPassword();
354 354
 				$privateKey = $this->getSystemPrivateKey($uid);
@@ -362,7 +362,7 @@  discard block
 block discarded – undo
362 362
 			return false;
363 363
 		} catch (\Exception $e) {
364 364
 			$this->log->warning(
365
-				'Could not decrypt the private key from user "' . $uid . '"" during login. ' .
365
+				'Could not decrypt the private key from user "'.$uid.'"" during login. '.
366 366
 				'Assume password change on the user back-end. Error message: '
367 367
 				. $e->getMessage()
368 368
 			);
@@ -423,7 +423,7 @@  discard block
 block discarded – undo
423 423
 			// use public share key for public links
424 424
 			$uid = $this->getPublicShareKeyId();
425 425
 			$shareKey = $this->getShareKey($path, $uid);
426
-			$privateKey = $this->keyStorage->getSystemUserKey($this->publicShareKeyId . '.privateKey', Encryption::ID);
426
+			$privateKey = $this->keyStorage->getSystemUserKey($this->publicShareKeyId.'.privateKey', Encryption::ID);
427 427
 			$privateKey = $this->crypt->decryptPrivateKey($privateKey);
428 428
 		} else {
429 429
 			$shareKey = $this->getShareKey($path, $uid);
@@ -448,7 +448,7 @@  discard block
 block discarded – undo
448 448
 	 */
449 449
 	public function getVersion($path, View $view) {
450 450
 		$fileInfo = $view->getFileInfo($path);
451
-		if($fileInfo === false) {
451
+		if ($fileInfo === false) {
452 452
 			return 0;
453 453
 		}
454 454
 		return $fileInfo->getEncryptedVersion();
@@ -462,9 +462,9 @@  discard block
 block discarded – undo
462 462
 	 * @param View $view
463 463
 	 */
464 464
 	public function setVersion($path, $version, View $view) {
465
-		$fileInfo= $view->getFileInfo($path);
465
+		$fileInfo = $view->getFileInfo($path);
466 466
 
467
-		if($fileInfo !== false) {
467
+		if ($fileInfo !== false) {
468 468
 			$cache = $fileInfo->getStorage()->getCache();
469 469
 			$cache->update($fileInfo->getId(), ['encrypted' => $version, 'encryptedVersion' => $version]);
470 470
 		}
@@ -493,7 +493,7 @@  discard block
 block discarded – undo
493 493
 	public function deleteShareKey($path, $keyId) {
494 494
 		return $this->keyStorage->deleteFileKey(
495 495
 			$path,
496
-			$keyId . '.' . $this->shareKeyId,
496
+			$keyId.'.'.$this->shareKeyId,
497 497
 			Encryption::ID);
498 498
 	}
499 499
 
@@ -504,7 +504,7 @@  discard block
 block discarded – undo
504 504
 	 * @return mixed
505 505
 	 */
506 506
 	public function getShareKey($path, $uid) {
507
-		$keyId = $uid . '.' . $this->shareKeyId;
507
+		$keyId = $uid.'.'.$this->shareKeyId;
508 508
 		return $this->keyStorage->getFileKey($path, $keyId, Encryption::ID);
509 509
 	}
510 510
 
@@ -566,7 +566,7 @@  discard block
 block discarded – undo
566 566
 	 * @return string
567 567
 	 */
568 568
 	public function getPublicShareKey() {
569
-		return $this->keyStorage->getSystemUserKey($this->publicShareKeyId . '.publicKey', Encryption::ID);
569
+		return $this->keyStorage->getSystemUserKey($this->publicShareKeyId.'.publicKey', Encryption::ID);
570 570
 	}
571 571
 
572 572
 	/**
@@ -636,7 +636,7 @@  discard block
 block discarded – undo
636 636
 	 * @return string returns openssl key
637 637
 	 */
638 638
 	public function getSystemPrivateKey($keyId) {
639
-		return $this->keyStorage->getSystemUserKey($keyId . '.' . $this->privateKeyId, Encryption::ID);
639
+		return $this->keyStorage->getSystemUserKey($keyId.'.'.$this->privateKeyId, Encryption::ID);
640 640
 	}
641 641
 
642 642
 	/**
@@ -646,7 +646,7 @@  discard block
 block discarded – undo
646 646
 	 */
647 647
 	public function setSystemPrivateKey($keyId, $key) {
648 648
 		return $this->keyStorage->setSystemUserKey(
649
-			$keyId . '.' . $this->privateKeyId,
649
+			$keyId.'.'.$this->privateKeyId,
650 650
 			$key,
651 651
 			Encryption::ID);
652 652
 	}
@@ -686,7 +686,7 @@  discard block
 block discarded – undo
686 686
 	 */
687 687
 	public function getMasterKeyPassword() {
688 688
 		$password = $this->config->getSystemValue('secret');
689
-		if (empty($password)){
689
+		if (empty($password)) {
690 690
 			throw new \Exception('Can not get secret from ownCloud instance');
691 691
 		}
692 692
 
@@ -708,6 +708,6 @@  discard block
 block discarded – undo
708 708
 	 * @return string
709 709
 	 */
710 710
 	public function getPublicMasterKey() {
711
-		return $this->keyStorage->getSystemUserKey($this->masterKeyId . '.publicKey', Encryption::ID);
711
+		return $this->keyStorage->getSystemUserKey($this->masterKeyId.'.publicKey', Encryption::ID);
712 712
 	}
713 713
 }
Please login to merge, or discard this patch.