nextcloud/apps/encryption/lib/KeyManager.php

698 lines
16 KiB
PHP
Raw Normal View History

2015-02-24 21:05:19 +03:00
<?php
/**
* @author Björn Schießle <schiessle@owncloud.com>
* @author Clark Tomlinson <fallen013@gmail.com>
2016-01-12 17:02:16 +03:00
* @author Lukas Reschke <lukas@owncloud.com>
* @author Thomas Müller <thomas.mueller@tmit.eu>
* @author Vincent Petry <pvince81@owncloud.com>
*
2016-01-12 17:02:16 +03:00
* @copyright Copyright (c) 2016, 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;
use OC\Encryption\Exceptions\DecryptionFailedException;
use OC\Files\View;
use OCA\Encryption\Crypto\Encryption;
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;
use OCP\Encryption\Keys\IStorage;
2015-02-24 21:05:19 +03:00
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\ILogger;
2015-02-24 21:05:19 +03:00
use OCP\IUserSession;
class KeyManager {
/**
* @var Session
*/
protected $session;
/**
* @var IStorage
2015-02-24 21:05:19 +03:00
*/
private $keyStorage;
/**
* @var Crypt
*/
private $crypt;
/**
* @var string
*/
private $recoveryKeyId;
/**
* @var string
*/
private $publicShareKeyId;
2015-09-07 12:38:44 +03:00
/**
* @var string
*/
private $masterKeyId;
2015-02-24 21:05:19 +03:00
/**
* @var string UserID
*/
private $keyId;
/**
* @var string
*/
2015-03-26 14:23:36 +03:00
private $publicKeyId = 'publicKey';
/**
* @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-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;
/**
* @var ILogger
*/
private $log;
2015-03-31 18:13:36 +03:00
/**
* @var Util
*/
private $util;
2015-02-24 21:05:19 +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
* @param Session $session
* @param ILogger $log
2015-03-31 18:13:36 +03:00
* @param Util $util
2015-02-24 21:05:19 +03:00
*/
public function __construct(
IStorage $keyStorage,
Crypt $crypt,
IConfig $config,
IUserSession $userSession,
Session $session,
2015-03-31 18:13:36 +03:00
ILogger $log,
Util $util
) {
2015-02-24 21:05:19 +03:00
2015-03-31 18:13:36 +03:00
$this->util = $util;
$this->session = $session;
2015-02-24 21:05:19 +03:00
$this->keyStorage = $keyStorage;
$this->crypt = $crypt;
$this->config = $config;
$this->log = $log;
$this->recoveryKeyId = $this->config->getAppValue('encryption',
'recoveryKeyId');
if (empty($this->recoveryKeyId)) {
$this->recoveryKeyId = 'recoveryKey_' . substr(md5(time()), 0, 8);
$this->config->setAppValue('encryption',
'recoveryKeyId',
$this->recoveryKeyId);
}
$this->publicShareKeyId = $this->config->getAppValue('encryption',
'publicShareKeyId');
if (empty($this->publicShareKeyId)) {
$this->publicShareKeyId = 'pubShare_' . substr(md5(time()), 0, 8);
$this->config->setAppValue('encryption', 'publicShareKeyId', $this->publicShareKeyId);
}
2015-09-07 12:38:44 +03:00
$this->masterKeyId = $this->config->getAppValue('encryption',
'masterKeyId');
if (empty($this->masterKeyId)) {
$this->masterKeyId = 'master_' . substr(md5(time()), 0, 8);
$this->config->setAppValue('encryption', 'masterKeyId', $this->masterKeyId);
}
$this->keyId = $userSession && $userSession->isLoggedIn() ? $userSession->getUser()->getUID() : false;
$this->log = $log;
}
2015-09-07 12:38:44 +03:00
/**
* check if key pair for public link shares exists, if not we create one
*/
public function validateShareKey() {
$shareKey = $this->getPublicShareKey();
if (empty($shareKey)) {
$keyPair = $this->crypt->createKeyPair();
// Save public key
$this->keyStorage->setSystemUserKey(
$this->publicShareKeyId . '.publicKey', $keyPair['publicKey'],
Encryption::ID);
// Encrypt private key empty passphrase
$encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], '');
$header = $this->crypt->generateHeader();
$this->setSystemPrivateKey($this->publicShareKeyId, $header . $encryptedKey);
}
}
2015-09-07 12:38:44 +03:00
/**
* check if a key pair for the master key exists, if not we create one
*/
public function validateMasterKey() {
if ($this->util->isMasterKeyEnabled() === false) {
return;
}
2015-09-07 12:38:44 +03:00
$masterKey = $this->getPublicMasterKey();
if (empty($masterKey)) {
$keyPair = $this->crypt->createKeyPair();
// Save public key
$this->keyStorage->setSystemUserKey(
$this->masterKeyId . '.publicKey', $keyPair['publicKey'],
Encryption::ID);
// Encrypt private key with system password
$encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $this->getMasterKeyPassword(), $this->masterKeyId);
$header = $this->crypt->generateHeader();
$this->setSystemPrivateKey($this->masterKeyId, $header . $encryptedKey);
}
}
/**
* @return bool
*/
public function recoveryKeyExists() {
$key = $this->getRecoveryKey();
return (!empty($key));
}
/**
* get recovery key
*
* @return string
*/
public function getRecoveryKey() {
return $this->keyStorage->getSystemUserKey($this->recoveryKeyId . '.publicKey', Encryption::ID);
}
/**
* get recovery key ID
*
* @return string
*/
public function getRecoveryKeyId() {
return $this->recoveryKeyId;
}
/**
* @param string $password
* @return bool
*/
public function checkRecoveryPassword($password) {
$recoveryKey = $this->keyStorage->getSystemUserKey($this->recoveryKeyId . '.privateKey', Encryption::ID);
$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->encryptPrivateKey($keyPair['privateKey'], $password, $uid);
$header = $this->crypt->generateHeader();
if ($encryptedKey) {
$this->setPrivateKey($uid, $header . $encryptedKey);
return true;
}
return false;
}
/**
* @param string $password
* @param array $keyPair
* @return bool
*/
public function setRecoveryKey($password, $keyPair) {
// Save Public Key
$this->keyStorage->setSystemUserKey($this->getRecoveryKeyId().
'.publicKey',
$keyPair['publicKey'],
Encryption::ID);
$encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $password);
$header = $this->crypt->generateHeader();
if ($encryptedKey) {
$this->setSystemPrivateKey($this->getRecoveryKeyId(), $header . $encryptedKey);
return true;
}
return false;
}
/**
* @param $userId
* @param $key
* @return bool
*/
public function setPublicKey($userId, $key) {
return $this->keyStorage->setUserKey($userId, $this->publicKeyId, $key, Encryption::ID);
}
/**
* @param $userId
* @param string $key
* @return bool
*/
public function setPrivateKey($userId, $key) {
return $this->keyStorage->setUserKey($userId,
$this->privateKeyId,
$key,
Encryption::ID);
}
/**
* write file key to key storage
*
* @param string $path
* @param string $key
* @return boolean
*/
public function setFileKey($path, $key) {
return $this->keyStorage->setFileKey($path, $this->fileKeyId, $key, Encryption::ID);
}
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);
}
}
/**
* 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;
return $this->keyStorage->setFileKey($path, $keyId, $key, Encryption::ID);
}
/**
* Decrypt private key and store it
*
* @param string $uid user id
* @param string $passPhrase users password
* @return boolean
*/
public function init($uid, $passPhrase) {
$this->session->setStatus(Session::INIT_EXECUTED);
try {
2015-09-07 12:38:44 +03:00
if($this->util->isMasterKeyEnabled()) {
$uid = $this->getMasterKeyId();
$passPhrase = $this->getMasterKeyPassword();
$privateKey = $this->getSystemPrivateKey($uid);
} else {
$privateKey = $this->getPrivateKey($uid);
}
$privateKey = $this->crypt->decryptPrivateKey($privateKey, $passPhrase, $uid);
} catch (PrivateKeyMissingException $e) {
return false;
} catch (DecryptionFailedException $e) {
return false;
}
if ($privateKey) {
$this->session->setPrivateKey($privateKey);
$this->session->setStatus(Session::INIT_SUCCESSFUL);
return true;
}
return false;
2015-02-24 21:05:19 +03:00
}
/**
* @param $userId
* @return string
2015-02-24 21:05:19 +03:00
* @throws PrivateKeyMissingException
*/
public function getPrivateKey($userId) {
$privateKey = $this->keyStorage->getUserKey($userId,
$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
}
/**
* @param string $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) {
$encryptedFileKey = $this->keyStorage->getFileKey($path, $this->fileKeyId, Encryption::ID);
if (empty($encryptedFileKey)) {
return '';
}
if ($this->util->isMasterKeyEnabled()) {
$uid = $this->getMasterKeyId();
}
if (is_null($uid)) {
$uid = $this->getPublicShareKeyId();
$shareKey = $this->getShareKey($path, $uid);
$privateKey = $this->keyStorage->getSystemUserKey($this->publicShareKeyId . '.privateKey', Encryption::ID);
$privateKey = $this->crypt->decryptPrivateKey($privateKey);
} else {
$shareKey = $this->getShareKey($path, $uid);
$privateKey = $this->session->getPrivateKey();
}
2015-03-26 16:13:39 +03:00
if ($encryptedFileKey && $shareKey && $privateKey) {
return $this->crypt->multiKeyDecrypt($encryptedFileKey,
2015-03-27 03:35:36 +03:00
$shareKey,
$privateKey);
2015-03-26 16:13:39 +03:00
}
return '';
}
2015-02-24 21:05:19 +03:00
/**
* Get the current version of a file
*
* @param string $path
2016-02-10 00:27:23 +03:00
* @param View $view
* @return int
*/
2016-02-10 00:27:23 +03:00
public function getVersion($path, View $view) {
$fileInfo = $view->getFileInfo($path);
if($fileInfo === false) {
return 0;
}
return $fileInfo->getEncryptedVersion();
}
/**
* Set the current version of a file
*
* @param string $path
2016-02-10 00:27:23 +03:00
* @param int $version
* @param View $view
*/
2016-02-10 00:27:23 +03:00
public function setVersion($path, $version, View $view) {
$fileInfo= $view->getFileInfo($path);
if($fileInfo !== false) {
2016-02-10 00:27:23 +03:00
$cache = $fileInfo->getStorage()->getCache();
$cache->update($fileInfo->getId(), ['encrypted' => $version, 'encryptedVersion' => $version]);
}
}
2015-04-01 15:24:56 +03:00
/**
* get the encrypted file key
*
* @param string $path
2015-04-01 15:24:56 +03:00
* @return string
*/
public function getEncryptedFileKey($path) {
$encryptedFileKey = $this->keyStorage->getFileKey($path,
$this->fileKeyId, Encryption::ID);
2015-04-01 15:24:56 +03:00
return $encryptedFileKey;
}
2015-03-31 20:24:52 +03:00
/**
* delete share key
2015-03-31 20:24:52 +03:00
*
* @param string $path
* @param string $keyId
* @return boolean
*/
public function deleteShareKey($path, $keyId) {
return $this->keyStorage->deleteFileKey(
$path,
$keyId . '.' . $this->shareKeyId,
Encryption::ID);
2015-03-31 20:24:52 +03:00
}
/**
* @param $path
2015-03-26 16:13:39 +03:00
* @param $uid
* @return mixed
*/
2015-03-26 16:13:39 +03:00
public function getShareKey($path, $uid) {
$keyId = $uid . '.' . $this->shareKeyId;
return $this->keyStorage->getFileKey($path, $keyId, Encryption::ID);
2015-02-24 21:05:19 +03:00
}
/**
* check if user has a private and a public key
*
* @param string $userId
2015-02-24 21:05:19 +03:00
* @return bool
* @throws PrivateKeyMissingException
* @throws PublicKeyMissingException
2015-02-24 21:05:19 +03:00
*/
public function userHasKeys($userId) {
$privateKey = $publicKey = true;
$exception = null;
2015-02-24 21:05:19 +03:00
try {
$this->getPrivateKey($userId);
} catch (PrivateKeyMissingException $e) {
$privateKey = false;
$exception = $e;
}
try {
$this->getPublicKey($userId);
2015-02-24 21:05:19 +03:00
} catch (PublicKeyMissingException $e) {
$publicKey = false;
$exception = $e;
}
if ($privateKey && $publicKey) {
return true;
} elseif (!$privateKey && !$publicKey) {
2015-02-24 21:05:19 +03:00
return false;
} else {
throw $exception;
2015-02-24 21:05:19 +03:00
}
}
/**
* @param $userId
* @return mixed
* @throws PublicKeyMissingException
2015-02-24 21:05:19 +03:00
*/
public function getPublicKey($userId) {
$publicKey = $this->keyStorage->getUserKey($userId, $this->publicKeyId, Encryption::ID);
2015-02-24 21:05:19 +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
}
public function getPublicShareKeyId() {
return $this->publicShareKeyId;
}
/**
* get public key for public link shares
*
* @return string
*/
public function getPublicShareKey() {
return $this->keyStorage->getSystemUserKey($this->publicShareKeyId . '.publicKey', Encryption::ID);
}
2015-02-24 21:05:19 +03:00
/**
* @param string $purpose
* @param bool $timestamp
* @param bool $includeUserKeys
2015-02-24 21:05:19 +03:00
*/
public function backupAllKeys($purpose, $timestamp = true, $includeUserKeys = true) {
// $backupDir = $this->keyStorage->;
2015-02-24 21:05:19 +03:00
}
/**
* creat a backup of the users private and public key and then delete it
*
* @param string $uid
2015-02-24 21:05:19 +03:00
*/
public function deleteUserKeys($uid) {
$this->backupAllKeys('password_reset');
$this->deletePublicKey($uid);
$this->deletePrivateKey($uid);
2015-02-24 21:05:19 +03:00
}
/**
* @param $uid
2015-02-24 21:05:19 +03:00
* @return bool
*/
public function deletePublicKey($uid) {
return $this->keyStorage->deleteUserKey($uid, $this->publicKeyId, Encryption::ID);
2015-02-24 21:05:19 +03:00
}
/**
* @param string $uid
2015-02-24 21:05:19 +03:00
* @return bool
*/
private function deletePrivateKey($uid) {
return $this->keyStorage->deleteUserKey($uid, $this->privateKeyId, Encryption::ID);
2015-02-24 21:05:19 +03:00
}
/**
* @param string $path
* @return bool
*/
2015-03-27 13:43:02 +03:00
public function deleteAllFileKeys($path) {
return $this->keyStorage->deleteAllFileKeys($path);
2015-03-27 13:43:02 +03:00
}
2015-02-24 21:05:19 +03:00
/**
* @param array $userIds
* @return array
* @throws PublicKeyMissingException
2015-02-24 21:05:19 +03:00
*/
public function getPublicKeys(array $userIds) {
$keys = [];
2015-02-24 21:05:19 +03:00
foreach ($userIds as $userId) {
try {
$keys[$userId] = $this->getPublicKey($userId);
} catch (PublicKeyMissingException $e) {
continue;
}
}
2015-02-24 21:05:19 +03:00
return $keys;
2015-02-24 21:05:19 +03:00
}
2015-03-27 03:35:36 +03:00
/**
* @param string $keyId
* @return string returns openssl key
*/
public function getSystemPrivateKey($keyId) {
return $this->keyStorage->getSystemUserKey($keyId . '.' . $this->privateKeyId, Encryption::ID);
}
/**
* @param string $keyId
* @param string $key
2015-03-27 03:35:36 +03:00
* @return string returns openssl key
*/
public function setSystemPrivateKey($keyId, $key) {
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
* @param string $uid
2015-03-31 18:13:36 +03:00
* @return array
* @throws PublicKeyMissingException
2015-03-31 18:13:36 +03:00
*/
public function addSystemKeys(array $accessList, array $publicKeys, $uid) {
2015-03-31 18:13:36 +03:00
if (!empty($accessList['public'])) {
$publicShareKey = $this->getPublicShareKey();
if (empty($publicShareKey)) {
2015-04-01 17:36:08 +03:00
throw new PublicKeyMissingException($this->getPublicShareKeyId());
}
$publicKeys[$this->getPublicShareKeyId()] = $publicShareKey;
2015-03-31 18:13:36 +03:00
}
if ($this->recoveryKeyExists() &&
$this->util->isRecoveryEnabledForUser($uid)) {
2015-03-31 18:13:36 +03:00
$publicKeys[$this->getRecoveryKeyId()] = $this->getRecoveryKey();
}
return $publicKeys;
}
2015-09-07 12:38:44 +03:00
/**
* get master key password
*
* @return string
* @throws \Exception
*/
public function getMasterKeyPassword() {
2015-09-07 12:38:44 +03:00
$password = $this->config->getSystemValue('secret');
if (empty($password)){
throw new \Exception('Can not get secret from ownCloud instance');
}
return $password;
}
/**
* return master key id
*
* @return string
*/
public function getMasterKeyId() {
return $this->masterKeyId;
}
/**
* get public master key
*
* @return string
*/
public function getPublicMasterKey() {
return $this->keyStorage->getSystemUserKey($this->masterKeyId . '.publicKey', Encryption::ID);
}
2015-02-24 21:05:19 +03:00
}