]> source.dussan.org Git - nextcloud-server.git/commitdiff
check if recovery key exists and encrypt the file with the recovery key if needed
authorBjoern Schiessle <schiessle@owncloud.com>
Sat, 28 Mar 2015 10:02:26 +0000 (11:02 +0100)
committerThomas Müller <thomas.mueller@tmit.eu>
Tue, 7 Apr 2015 11:30:28 +0000 (13:30 +0200)
apps/encryption/appinfo/encryption.php
apps/encryption/lib/crypto/encryption.php
apps/encryption/lib/keymanager.php
lib/private/encryption/util.php

index d97aa07738ce58e6602d49e33bed894d9950a11d..dd8004a48803f68b36fd00a05b5dad7784cedc2a 100644 (file)
@@ -102,7 +102,10 @@ class Encryption extends \OCP\AppFramework\App {
        public function registerEncryptionModule() {
                $container = $this->getContainer();
                $container->registerService('EncryptionModule', function (IAppContainer $c) {
-                       return new \OCA\Encryption\Crypto\Encryption($c->query('Crypt'), $c->query('KeyManager'));
+                       return new \OCA\Encryption\Crypto\Encryption(
+                               $c->query('Crypt'),
+                               $c->query('KeyManager'),
+                               $c->query('Util'));
                });
                $module = $container->query('EncryptionModule');
                $this->encryptionManager->registerEncryptionModule($module);
index da805892eafd383a83e91d9e38c0bc18a02bb12b..8c00077729e9efa3b4e61e9677cf6872e43177ed 100644 (file)
@@ -46,9 +46,19 @@ class Encryption implements IEncryptionModule {
        /** @var boolean */
        private $isWriteOperation;
 
-       public function __construct(Crypt $crypt, KeyManager $keymanager) {
+       /** @var \OC\Encryption\Util */
+       private $util;
+
+       /**
+        *
+        * @param \OCA\Encryption\Crypto\Crypt $crypt
+        * @param KeyManager $keymanager
+        * @param \OC\Encryption\Util $util
+        */
+       public function __construct(Crypt $crypt, KeyManager $keymanager, \OC\Encryption\Util $util) {
                $this->crypt = $crypt;
                $this->keymanager = $keymanager;
+               $this->util = $util;
        }
 
        /**
@@ -225,9 +235,7 @@ class Encryption implements IEncryptionModule {
                        $publicKeys[$user] = $this->keymanager->getPublicKey($user);
                }
 
-               if (!empty($accessList['public'])) {
-                       $publicKeys[$this->keymanager->getPublicShareKeyId()] = $this->keymanager->getPublicShareKey();
-               }
+               $publicKeys = $this->addSystemKeys($accessList, $publicKeys);
 
                $encryptedFileKey = $this->crypt->multiKeyEncrypt($fileKey, $publicKeys);
 
@@ -238,6 +246,29 @@ class Encryption implements IEncryptionModule {
                return true;
        }
 
+       /**
+        * add system keys such as the public share key and the recovery key
+        *
+        * @param array $accessList
+        * @param array $publicKeys
+        * @return array
+        */
+       public function addSystemKeys(array $accessList, array $publicKeys) {
+               if (!empty($accessList['public'])) {
+                       $publicKeys[$this->keymanager->getPublicShareKeyId()] = $this->keymanager->getPublicShareKey();
+               }
+
+               if ($this->keymanager->recoveryKeyExists() &&
+                       $this->util->recoveryEnabled($this->user)) {
+
+                       $publicKeys[$this->keymanager->getRecoveryKeyId()] = $this->keymanager->getRecoveryKey();
+               }
+
+
+               return $publicKeys;
+       }
+
+
        /**
         * should the file be encrypted or not
         *
index 44a46458692cf004bf1c837e796c8b60d9350809..ea338f88ea7062c4a56877ca16af9fb82c1b31d8 100644 (file)
@@ -141,7 +141,25 @@ class KeyManager {
         * @return bool
         */
        public function recoveryKeyExists() {
-               return (strlen($this->keyStorage->getSystemUserKey($this->recoveryKeyId)) !== 0);
+               return (!empty($this->keyStorage->getSystemUserKey($this->recoveryKeyId)));
+       }
+
+       /**
+        * get recovery key
+        *
+        * @return string
+        */
+       public function getRecoveryKey() {
+               return $this->keyStorage->getSystemUserKey($this->recoveryKeyId . '.publicKey');
+       }
+
+       /**
+        * get recovery key ID
+        *
+        * @return string
+        */
+       public function getRecoveryKeyId() {
+               return $this->recoveryKeyId;
        }
 
        /**
index 85e852ec2c9ee89ee99e8622d17091b99772ab0b..e3390f155d4095916c2dd88e6c72d96998c10ae7 100644 (file)
@@ -26,6 +26,7 @@ namespace OC\Encryption;
 use OC\Encryption\Exceptions\EncryptionHeaderToLargeException;
 use OC\Encryption\Exceptions\EncryptionHeaderKeyExistsException;
 use OCP\Encryption\IEncryptionModule;
+use OCP\IConfig;
 
 class Util {
 
@@ -58,19 +59,27 @@ class Util {
        /** @var \OC\User\Manager */
        protected $userManager;
 
+       /** @var IConfig */
+       protected $config;
+
        /** @var array paths excluded from encryption */
        protected $excludedPaths;
 
        /**
         * @param \OC\Files\View $view root view
         */
-       public function __construct(\OC\Files\View $view, \OC\User\Manager $userManager) {
+       public function __construct(
+               \OC\Files\View $view,
+               \OC\User\Manager $userManager,
+               IConfig $config) {
+
                $this->ocHeaderKeys = [
                        self::HEADER_ENCRYPTION_MODULE_KEY
                ];
 
                $this->view = $view;
                $this->userManager = $userManager;
+               $this->config = $config;
 
                $this->excludedPaths[] = 'files_encryption';
        }
@@ -411,4 +420,16 @@ class Util {
                return false;
        }
 
+       /**
+        * check if recovery key is enabled for user
+        *
+        * @param string $uid
+        * @return boolean
+        */
+       public function recoveryEnabled($uid) {
+               $enabled = $this->config->getUserValue($uid, 'encryption', 'recovery_enabled', '0');
+
+               return ($enabled === '1') ? true : false;
+       }
+
 }