2015-02-24 21:05:19 +03:00
|
|
|
<?php
|
2015-04-07 18:02:49 +03:00
|
|
|
/**
|
|
|
|
* @author Björn Schießle <schiessle@owncloud.com>
|
|
|
|
* @author Clark Tomlinson <fallen013@gmail.com>
|
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
2015-02-24 21:05:19 +03:00
|
|
|
namespace OCA\Encryption;
|
|
|
|
|
2015-03-25 00:29:10 +03:00
|
|
|
use OC\Encryption\Exceptions\DecryptionFailedException;
|
2015-04-22 12:18:18 +03:00
|
|
|
use OCA\Encryption\Crypto\Encryption;
|
2015-03-31 13:01:51 +03:00
|
|
|
use OCA\Encryption\Exceptions\PrivateKeyMissingException;
|
2015-04-01 17:36:08 +03:00
|
|
|
use OCA\Encryption\Exceptions\PublicKeyMissingException;
|
2015-02-24 21:05:19 +03:00
|
|
|
use OCA\Encryption\Crypto\Crypt;
|
2015-03-25 00:29:10 +03:00
|
|
|
use OCP\Encryption\Keys\IStorage;
|
2015-02-24 21:05:19 +03:00
|
|
|
use OCP\IConfig;
|
2015-03-25 00:29:10 +03:00
|
|
|
use OCP\ILogger;
|
2015-02-24 21:05:19 +03:00
|
|
|
use OCP\IUserSession;
|
|
|
|
|
|
|
|
class KeyManager {
|
|
|
|
|
|
|
|
/**
|
2015-03-31 12:15:47 +03:00
|
|
|
* @var Session
|
2015-03-25 00:29:10 +03:00
|
|
|
*/
|
2015-03-31 12:15:47 +03:00
|
|
|
protected $session;
|
2015-03-25 00:29:10 +03:00
|
|
|
/**
|
|
|
|
* @var IStorage
|
2015-02-24 21:05:19 +03:00
|
|
|
*/
|
|
|
|
private $keyStorage;
|
|
|
|
/**
|
|
|
|
* @var Crypt
|
|
|
|
*/
|
|
|
|
private $crypt;
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $recoveryKeyId;
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $publicShareKeyId;
|
|
|
|
/**
|
|
|
|
* @var string UserID
|
|
|
|
*/
|
|
|
|
private $keyId;
|
2015-03-25 00:29:10 +03:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-03-26 14:23:36 +03:00
|
|
|
private $publicKeyId = 'publicKey';
|
2015-03-25 00:29:10 +03:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-03-26 14:23:36 +03:00
|
|
|
private $privateKeyId = 'privateKey';
|
2015-02-24 21:05:19 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-03-26 14:23:36 +03:00
|
|
|
private $shareKeyId = 'shareKey';
|
2015-03-25 00:29:10 +03:00
|
|
|
|
2015-02-24 21:05:19 +03:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-03-26 14:23:36 +03:00
|
|
|
private $fileKeyId = 'fileKey';
|
2015-02-24 21:05:19 +03:00
|
|
|
/**
|
|
|
|
* @var IConfig
|
|
|
|
*/
|
|
|
|
private $config;
|
2015-03-25 00:29:10 +03:00
|
|
|
/**
|
|
|
|
* @var ILogger
|
|
|
|
*/
|
|
|
|
private $log;
|
2015-03-31 18:13:36 +03:00
|
|
|
/**
|
|
|
|
* @var Util
|
|
|
|
*/
|
|
|
|
private $util;
|
2015-02-24 21:05:19 +03:00
|
|
|
|
|
|
|
/**
|
2015-03-25 00:29:10 +03:00
|
|
|
* @param IStorage $keyStorage
|
2015-02-24 21:05:19 +03:00
|
|
|
* @param Crypt $crypt
|
|
|
|
* @param IConfig $config
|
2015-03-27 03:35:36 +03:00
|
|
|
* @param IUserSession $userSession
|
2015-03-31 12:15:47 +03:00
|
|
|
* @param Session $session
|
2015-03-25 00:29:10 +03:00
|
|
|
* @param ILogger $log
|
2015-03-31 18:13:36 +03:00
|
|
|
* @param Util $util
|
2015-02-24 21:05:19 +03:00
|
|
|
*/
|
2015-03-27 20:10:32 +03:00
|
|
|
public function __construct(
|
|
|
|
IStorage $keyStorage,
|
|
|
|
Crypt $crypt,
|
|
|
|
IConfig $config,
|
|
|
|
IUserSession $userSession,
|
2015-03-31 12:15:47 +03:00
|
|
|
Session $session,
|
2015-03-31 18:13:36 +03:00
|
|
|
ILogger $log,
|
|
|
|
Util $util
|
2015-03-31 00:01:50 +03:00
|
|
|
) {
|
2015-02-24 21:05:19 +03:00
|
|
|
|
2015-03-31 18:13:36 +03:00
|
|
|
$this->util = $util;
|
2015-03-31 12:15:47 +03:00
|
|
|
$this->session = $session;
|
2015-02-24 21:05:19 +03:00
|
|
|
$this->keyStorage = $keyStorage;
|
|
|
|
$this->crypt = $crypt;
|
|
|
|
$this->config = $config;
|
2015-04-01 14:59:29 +03:00
|
|
|
$this->log = $log;
|
|
|
|
|
2015-03-25 00:29:10 +03:00
|
|
|
$this->recoveryKeyId = $this->config->getAppValue('encryption',
|
|
|
|
'recoveryKeyId');
|
2015-03-31 14:48:03 +03:00
|
|
|
if (empty($this->recoveryKeyId)) {
|
|
|
|
$this->recoveryKeyId = 'recoveryKey_' . substr(md5(time()), 0, 8);
|
|
|
|
$this->config->setAppValue('encryption',
|
|
|
|
'recoveryKeyId',
|
|
|
|
$this->recoveryKeyId);
|
|
|
|
}
|
|
|
|
|
2015-03-25 00:29:10 +03:00
|
|
|
$this->publicShareKeyId = $this->config->getAppValue('encryption',
|
|
|
|
'publicShareKeyId');
|
2015-03-27 20:10:32 +03:00
|
|
|
if (empty($this->publicShareKeyId)) {
|
|
|
|
$this->publicShareKeyId = 'pubShare_' . substr(md5(time()), 0, 8);
|
2015-04-01 14:59:29 +03:00
|
|
|
$this->config->setAppValue('encryption', 'publicShareKeyId', $this->publicShareKeyId);
|
|
|
|
}
|
2015-03-27 20:10:32 +03:00
|
|
|
|
2015-04-17 18:51:18 +03:00
|
|
|
$this->keyId = $userSession && $userSession->isLoggedIn() ? $userSession->getUser()->getUID() : false;
|
|
|
|
$this->log = $log;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function validateShareKey() {
|
2015-04-01 14:59:29 +03:00
|
|
|
$shareKey = $this->getPublicShareKey();
|
|
|
|
if (empty($shareKey)) {
|
2015-03-30 10:59:28 +03:00
|
|
|
$keyPair = $this->crypt->createKeyPair();
|
2015-03-27 20:10:32 +03:00
|
|
|
|
|
|
|
// Save public key
|
|
|
|
$this->keyStorage->setSystemUserKey(
|
2015-04-22 12:18:18 +03:00
|
|
|
$this->publicShareKeyId . '.publicKey', $keyPair['publicKey'],
|
|
|
|
Encryption::ID);
|
2015-03-27 20:10:32 +03:00
|
|
|
|
|
|
|
// Encrypt private key empty passphrase
|
2015-04-01 14:59:29 +03:00
|
|
|
$encryptedKey = $this->crypt->symmetricEncryptFileContent($keyPair['privateKey'], '');
|
2015-04-15 20:52:48 +03:00
|
|
|
$header = $this->crypt->generateHeader();
|
|
|
|
$this->setSystemPrivateKey($this->publicShareKeyId, $header . $encryptedKey);
|
2015-03-27 20:10:32 +03:00
|
|
|
}
|
2015-03-25 00:29:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function recoveryKeyExists() {
|
2015-04-01 14:59:29 +03:00
|
|
|
$key = $this->getRecoveryKey();
|
|
|
|
return (!empty($key));
|
2015-03-28 13:02:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get recovery key
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getRecoveryKey() {
|
2015-04-22 12:18:18 +03:00
|
|
|
return $this->keyStorage->getSystemUserKey($this->recoveryKeyId . '.publicKey', Encryption::ID);
|
2015-03-28 13:02:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get recovery key ID
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getRecoveryKeyId() {
|
|
|
|
return $this->recoveryKeyId;
|
2015-03-25 00:29:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $password
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function checkRecoveryPassword($password) {
|
2015-04-22 12:18:18 +03:00
|
|
|
$recoveryKey = $this->keyStorage->getSystemUserKey($this->recoveryKeyId . '.privateKey', Encryption::ID);
|
2015-03-25 00:29:10 +03:00
|
|
|
$decryptedRecoveryKey = $this->crypt->decryptPrivateKey($recoveryKey,
|
|
|
|
$password);
|
|
|
|
|
|
|
|
if ($decryptedRecoveryKey) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $uid
|
|
|
|
* @param string $password
|
|
|
|
* @param string $keyPair
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function storeKeyPair($uid, $password, $keyPair) {
|
|
|
|
// Save Public Key
|
|
|
|
$this->setPublicKey($uid, $keyPair['publicKey']);
|
|
|
|
|
|
|
|
$encryptedKey = $this->crypt->symmetricEncryptFileContent($keyPair['privateKey'],
|
|
|
|
$password);
|
2015-04-08 15:19:11 +03:00
|
|
|
$header = $this->crypt->generateHeader();
|
2015-03-25 00:29:10 +03:00
|
|
|
|
|
|
|
if ($encryptedKey) {
|
2015-04-08 15:19:11 +03:00
|
|
|
$this->setPrivateKey($uid, $header . $encryptedKey);
|
2015-03-25 00:29:10 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-01 16:41:31 +03:00
|
|
|
/**
|
2015-03-31 14:48:03 +03:00
|
|
|
* @param string $password
|
|
|
|
* @param array $keyPair
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function setRecoveryKey($password, $keyPair) {
|
|
|
|
// Save Public Key
|
2015-04-22 12:18:18 +03:00
|
|
|
$this->keyStorage->setSystemUserKey($this->getRecoveryKeyId().
|
|
|
|
'.publicKey',
|
|
|
|
$keyPair['publicKey'],
|
|
|
|
Encryption::ID);
|
2015-03-31 14:48:03 +03:00
|
|
|
|
|
|
|
$encryptedKey = $this->crypt->symmetricEncryptFileContent($keyPair['privateKey'],
|
|
|
|
$password);
|
2015-04-08 15:19:11 +03:00
|
|
|
$header = $this->crypt->generateHeader();
|
2015-03-31 14:48:03 +03:00
|
|
|
|
|
|
|
if ($encryptedKey) {
|
2015-04-08 15:19:11 +03:00
|
|
|
$this->setSystemPrivateKey($this->getRecoveryKeyId(), $header . $encryptedKey);
|
2015-03-31 14:48:03 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-25 00:29:10 +03:00
|
|
|
/**
|
|
|
|
* @param $userId
|
|
|
|
* @param $key
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function setPublicKey($userId, $key) {
|
2015-04-22 12:18:18 +03:00
|
|
|
return $this->keyStorage->setUserKey($userId, $this->publicKeyId, $key, Encryption::ID);
|
2015-03-25 00:29:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $userId
|
|
|
|
* @param $key
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function setPrivateKey($userId, $key) {
|
|
|
|
return $this->keyStorage->setUserKey($userId,
|
|
|
|
$this->privateKeyId,
|
2015-04-22 12:18:18 +03:00
|
|
|
$key,
|
|
|
|
Encryption::ID);
|
2015-03-25 00:29:10 +03:00
|
|
|
}
|
|
|
|
|
2015-03-26 15:37:14 +03:00
|
|
|
/**
|
|
|
|
* write file key to key storage
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param string $key
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function setFileKey($path, $key) {
|
2015-04-22 12:18:18 +03:00
|
|
|
return $this->keyStorage->setFileKey($path, $this->fileKeyId, $key, Encryption::ID);
|
2015-03-26 15:37:14 +03:00
|
|
|
}
|
|
|
|
|
2015-03-27 13:43:02 +03:00
|
|
|
/**
|
|
|
|
* set all file keys (the file key and the corresponding share keys)
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param array $keys
|
|
|
|
*/
|
|
|
|
public function setAllFileKeys($path, $keys) {
|
|
|
|
$this->setFileKey($path, $keys['data']);
|
|
|
|
foreach ($keys['keys'] as $uid => $keyFile) {
|
|
|
|
$this->setShareKey($path, $uid, $keyFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 15:37:14 +03:00
|
|
|
/**
|
|
|
|
* write share key to the key storage
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param string $uid
|
|
|
|
* @param string $key
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function setShareKey($path, $uid, $key) {
|
|
|
|
$keyId = $uid . '.' . $this->shareKeyId;
|
2015-04-22 12:18:18 +03:00
|
|
|
return $this->keyStorage->setFileKey($path, $keyId, $key, Encryption::ID);
|
2015-03-26 15:37:14 +03:00
|
|
|
}
|
|
|
|
|
2015-03-25 00:29:10 +03:00
|
|
|
/**
|
|
|
|
* Decrypt private key and store it
|
|
|
|
*
|
|
|
|
* @param string $uid userid
|
|
|
|
* @param string $passPhrase users password
|
2015-03-31 21:30:54 +03:00
|
|
|
* @return boolean
|
2015-03-25 00:29:10 +03:00
|
|
|
*/
|
|
|
|
public function init($uid, $passPhrase) {
|
2015-04-16 14:47:27 +03:00
|
|
|
|
|
|
|
$this->session->setStatus(Session::INIT_EXECUTED);
|
|
|
|
|
2015-03-25 00:29:10 +03:00
|
|
|
try {
|
|
|
|
$privateKey = $this->getPrivateKey($uid);
|
|
|
|
$privateKey = $this->crypt->decryptPrivateKey($privateKey,
|
|
|
|
$passPhrase);
|
|
|
|
} catch (PrivateKeyMissingException $e) {
|
|
|
|
return false;
|
|
|
|
} catch (DecryptionFailedException $e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-16 14:47:27 +03:00
|
|
|
if ($privateKey) {
|
|
|
|
$this->session->setPrivateKey($privateKey);
|
|
|
|
$this->session->setStatus(Session::INIT_SUCCESSFUL);
|
|
|
|
return true;
|
|
|
|
}
|
2015-03-31 21:30:54 +03:00
|
|
|
|
2015-04-16 14:47:27 +03:00
|
|
|
return false;
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $userId
|
|
|
|
* @return mixed
|
|
|
|
* @throws PrivateKeyMissingException
|
|
|
|
*/
|
|
|
|
public function getPrivateKey($userId) {
|
2015-03-25 00:29:10 +03:00
|
|
|
$privateKey = $this->keyStorage->getUserKey($userId,
|
2015-04-22 12:18:18 +03:00
|
|
|
$this->privateKeyId, Encryption::ID);
|
2015-02-24 21:05:19 +03:00
|
|
|
|
|
|
|
if (strlen($privateKey) !== 0) {
|
|
|
|
return $privateKey;
|
|
|
|
}
|
2015-04-01 17:36:08 +03:00
|
|
|
throw new PrivateKeyMissingException($userId);
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-25 00:29:10 +03:00
|
|
|
* @param $path
|
2015-03-26 16:13:39 +03:00
|
|
|
* @param $uid
|
|
|
|
* @return string
|
2015-02-24 21:05:19 +03:00
|
|
|
*/
|
2015-03-26 16:13:39 +03:00
|
|
|
public function getFileKey($path, $uid) {
|
2015-04-22 12:18:18 +03:00
|
|
|
$encryptedFileKey = $this->keyStorage->getFileKey($path, $this->fileKeyId, Encryption::ID);
|
2015-04-01 14:59:29 +03:00
|
|
|
|
|
|
|
if (is_null($uid)) {
|
|
|
|
$uid = $this->getPublicShareKeyId();
|
|
|
|
$shareKey = $this->getShareKey($path, $uid);
|
2015-04-22 12:18:18 +03:00
|
|
|
$privateKey = $this->keyStorage->getSystemUserKey($this->publicShareKeyId . '.privateKey', Encryption::ID);
|
2015-04-15 20:37:03 +03:00
|
|
|
$privateKey = $this->crypt->decryptPrivateKey($privateKey);
|
2015-04-01 14:59:29 +03:00
|
|
|
} else {
|
|
|
|
$shareKey = $this->getShareKey($path, $uid);
|
|
|
|
$privateKey = $this->session->getPrivateKey();
|
|
|
|
}
|
2015-03-26 16:13:39 +03:00
|
|
|
|
|
|
|
if ($encryptedFileKey && $shareKey && $privateKey) {
|
2015-04-01 14:59:29 +03:00
|
|
|
return $this->crypt->multiKeyDecrypt($encryptedFileKey,
|
2015-03-27 03:35:36 +03:00
|
|
|
$shareKey,
|
|
|
|
$privateKey);
|
2015-03-26 16:13:39 +03:00
|
|
|
}
|
|
|
|
|
2015-04-01 16:41:31 +03:00
|
|
|
return '';
|
2015-03-25 00:29:10 +03:00
|
|
|
}
|
2015-02-24 21:05:19 +03:00
|
|
|
|
2015-04-01 15:24:56 +03:00
|
|
|
/**
|
|
|
|
* get the encrypted file key
|
|
|
|
*
|
|
|
|
* @param $path
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getEncryptedFileKey($path) {
|
|
|
|
$encryptedFileKey = $this->keyStorage->getFileKey($path,
|
2015-04-22 12:18:18 +03:00
|
|
|
$this->fileKeyId, Encryption::ID);
|
2015-04-01 15:24:56 +03:00
|
|
|
|
|
|
|
return $encryptedFileKey;
|
|
|
|
}
|
|
|
|
|
2015-03-31 20:24:52 +03:00
|
|
|
/**
|
2015-03-31 21:30:54 +03:00
|
|
|
* delete share key
|
2015-03-31 20:24:52 +03:00
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param string $keyId
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2015-03-31 21:30:54 +03:00
|
|
|
public function deleteShareKey($path, $keyId) {
|
2015-04-22 12:18:18 +03:00
|
|
|
return $this->keyStorage->deleteFileKey(
|
|
|
|
$path,
|
|
|
|
$keyId . '.' . $this->shareKeyId,
|
|
|
|
Encryption::ID);
|
2015-03-31 20:24:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-25 00:29:10 +03:00
|
|
|
/**
|
|
|
|
* @param $path
|
2015-03-26 16:13:39 +03:00
|
|
|
* @param $uid
|
2015-03-25 00:29:10 +03:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2015-03-26 16:13:39 +03:00
|
|
|
public function getShareKey($path, $uid) {
|
|
|
|
$keyId = $uid . '.' . $this->shareKeyId;
|
2015-04-22 12:18:18 +03:00
|
|
|
return $this->keyStorage->getFileKey($path, $keyId, Encryption::ID);
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $userId
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function userHasKeys($userId) {
|
|
|
|
try {
|
|
|
|
$this->getPrivateKey($userId);
|
|
|
|
$this->getPublicKey($userId);
|
|
|
|
} catch (PrivateKeyMissingException $e) {
|
|
|
|
return false;
|
|
|
|
} catch (PublicKeyMissingException $e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-25 00:29:10 +03:00
|
|
|
* @param $userId
|
|
|
|
* @return mixed
|
|
|
|
* @throws PublicKeyMissingException
|
2015-02-24 21:05:19 +03:00
|
|
|
*/
|
2015-03-25 00:29:10 +03:00
|
|
|
public function getPublicKey($userId) {
|
2015-04-22 12:18:18 +03:00
|
|
|
$publicKey = $this->keyStorage->getUserKey($userId, $this->publicKeyId, Encryption::ID);
|
2015-02-24 21:05:19 +03:00
|
|
|
|
2015-03-25 00:29:10 +03:00
|
|
|
if (strlen($publicKey) !== 0) {
|
|
|
|
return $publicKey;
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
2015-04-01 17:36:08 +03:00
|
|
|
throw new PublicKeyMissingException($userId);
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
2015-03-27 20:10:32 +03:00
|
|
|
public function getPublicShareKeyId() {
|
|
|
|
return $this->publicShareKeyId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-01 14:59:29 +03:00
|
|
|
* get public key for public link shares
|
2015-03-27 20:10:32 +03:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getPublicShareKey() {
|
2015-04-22 12:18:18 +03:00
|
|
|
return $this->keyStorage->getSystemUserKey($this->publicShareKeyId . '.publicKey', Encryption::ID);
|
2015-03-27 20:10:32 +03:00
|
|
|
}
|
|
|
|
|
2015-02-24 21:05:19 +03:00
|
|
|
/**
|
2015-03-25 00:29:10 +03:00
|
|
|
* @param $purpose
|
|
|
|
* @param bool $timestamp
|
|
|
|
* @param bool $includeUserKeys
|
2015-02-24 21:05:19 +03:00
|
|
|
*/
|
2015-03-25 00:29:10 +03:00
|
|
|
public function backupAllKeys($purpose, $timestamp = true, $includeUserKeys = true) {
|
|
|
|
// $backupDir = $this->keyStorage->;
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-25 00:29:10 +03:00
|
|
|
* @param string $uid
|
2015-02-24 21:05:19 +03:00
|
|
|
*/
|
2015-03-25 00:29:10 +03:00
|
|
|
public function replaceUserKeys($uid) {
|
|
|
|
$this->backupAllKeys('password_reset');
|
|
|
|
$this->deletePublicKey($uid);
|
|
|
|
$this->deletePrivateKey($uid);
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-25 00:29:10 +03:00
|
|
|
* @param $uid
|
2015-02-24 21:05:19 +03:00
|
|
|
* @return bool
|
|
|
|
*/
|
2015-03-25 00:29:10 +03:00
|
|
|
public function deletePublicKey($uid) {
|
2015-04-22 12:18:18 +03:00
|
|
|
return $this->keyStorage->deleteUserKey($uid, $this->publicKeyId, Encryption::ID);
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-25 00:29:10 +03:00
|
|
|
* @param $uid
|
2015-02-24 21:05:19 +03:00
|
|
|
* @return bool
|
|
|
|
*/
|
2015-03-25 00:29:10 +03:00
|
|
|
private function deletePrivateKey($uid) {
|
2015-04-22 12:18:18 +03:00
|
|
|
return $this->keyStorage->deleteUserKey($uid, $this->privateKeyId, Encryption::ID);
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
2015-03-27 13:43:02 +03:00
|
|
|
public function deleteAllFileKeys($path) {
|
2015-04-22 12:18:18 +03:00
|
|
|
return $this->keyStorage->deleteAllFileKeys($path, Encryption::ID);
|
2015-03-27 13:43:02 +03:00
|
|
|
}
|
|
|
|
|
2015-02-24 21:05:19 +03:00
|
|
|
/**
|
2015-03-25 00:29:10 +03:00
|
|
|
* @param array $userIds
|
|
|
|
* @return array
|
|
|
|
* @throws PublicKeyMissingException
|
2015-02-24 21:05:19 +03:00
|
|
|
*/
|
2015-03-25 00:29:10 +03:00
|
|
|
public function getPublicKeys(array $userIds) {
|
|
|
|
$keys = [];
|
2015-02-24 21:05:19 +03:00
|
|
|
|
2015-03-25 00:29:10 +03:00
|
|
|
foreach ($userIds as $userId) {
|
|
|
|
try {
|
|
|
|
$keys[$userId] = $this->getPublicKey($userId);
|
|
|
|
} catch (PublicKeyMissingException $e) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2015-02-24 21:05:19 +03:00
|
|
|
|
2015-03-25 00:29:10 +03:00
|
|
|
return $keys;
|
2015-02-24 21:05:19 +03:00
|
|
|
|
|
|
|
}
|
2015-03-27 03:35:36 +03:00
|
|
|
|
|
|
|
/**
|
2015-03-31 14:48:03 +03:00
|
|
|
* @param string $keyId
|
|
|
|
* @return string returns openssl key
|
|
|
|
*/
|
|
|
|
public function getSystemPrivateKey($keyId) {
|
2015-04-22 12:18:18 +03:00
|
|
|
return $this->keyStorage->getSystemUserKey($keyId . '.' . $this->privateKeyId, Encryption::ID);
|
2015-03-31 14:48:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $keyId
|
|
|
|
* @param string $key
|
2015-03-27 03:35:36 +03:00
|
|
|
* @return string returns openssl key
|
|
|
|
*/
|
2015-03-31 14:48:03 +03:00
|
|
|
public function setSystemPrivateKey($keyId, $key) {
|
2015-04-22 12:18:18 +03:00
|
|
|
return $this->keyStorage->setSystemUserKey(
|
|
|
|
$keyId . '.' . $this->privateKeyId,
|
|
|
|
$key,
|
|
|
|
Encryption::ID);
|
2015-03-27 03:35:36 +03:00
|
|
|
}
|
2015-03-31 18:13:36 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* add system keys such as the public share key and the recovery key
|
|
|
|
*
|
|
|
|
* @param array $accessList
|
|
|
|
* @param array $publicKeys
|
|
|
|
* @return array
|
2015-04-01 16:41:31 +03:00
|
|
|
* @throws PublicKeyMissingException
|
2015-03-31 18:13:36 +03:00
|
|
|
*/
|
|
|
|
public function addSystemKeys(array $accessList, array $publicKeys) {
|
|
|
|
if (!empty($accessList['public'])) {
|
2015-04-01 14:59:29 +03:00
|
|
|
$publicShareKey = $this->getPublicShareKey();
|
|
|
|
if (empty($publicShareKey)) {
|
2015-04-01 17:36:08 +03:00
|
|
|
throw new PublicKeyMissingException($this->getPublicShareKeyId());
|
2015-04-01 14:59:29 +03:00
|
|
|
}
|
|
|
|
$publicKeys[$this->getPublicShareKeyId()] = $publicShareKey;
|
2015-03-31 18:13:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->recoveryKeyExists() &&
|
|
|
|
$this->util->isRecoveryEnabledForUser()) {
|
|
|
|
|
|
|
|
$publicKeys[$this->getRecoveryKeyId()] = $this->getRecoveryKey();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $publicKeys;
|
|
|
|
}
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|