summaryrefslogtreecommitdiffstats
path: root/apps/files_encryption/lib/util.php
diff options
context:
space:
mode:
Diffstat (limited to 'apps/files_encryption/lib/util.php')
-rw-r--r--apps/files_encryption/lib/util.php86
1 files changed, 71 insertions, 15 deletions
diff --git a/apps/files_encryption/lib/util.php b/apps/files_encryption/lib/util.php
index e44a8bd3dda..087dada7f1b 100644
--- a/apps/files_encryption/lib/util.php
+++ b/apps/files_encryption/lib/util.php
@@ -167,11 +167,12 @@ class Util {
\OC_FileProxy::$enabled = false;
// Encrypt private key with user pwd as passphrase
- $encryptedPrivateKey = Crypt::symmetricEncryptFileContent($keypair['privateKey'], $passphrase);
+ $encryptedPrivateKey = Crypt::symmetricEncryptFileContent($keypair['privateKey'], $passphrase, Helper::getCipher());
// Save key-pair
if ($encryptedPrivateKey) {
- $this->view->file_put_contents($this->privateKeyPath, $encryptedPrivateKey);
+ $header = crypt::generateHeader();
+ $this->view->file_put_contents($this->privateKeyPath, $header . $encryptedPrivateKey);
$this->view->file_put_contents($this->publicKeyPath, $keypair['publicKey']);
}
@@ -384,8 +385,14 @@ class Util {
&& $this->isEncryptedPath($path)
) {
- // get the size from filesystem
- $size = $this->view->filesize($path);
+ $offset = 0;
+ if ($this->containHeader($path)) {
+ $offset = Crypt::BLOCKSIZE;
+ }
+
+ // get the size from filesystem if the file contains a encryption header we
+ // we substract it
+ $size = $this->view->filesize($path) - $offset;
// fast path, else the calculation for $lastChunkNr is bogus
if ($size === 0) {
@@ -396,15 +403,15 @@ class Util {
// calculate last chunk nr
// next highest is end of chunks, one subtracted is last one
// we have to read the last chunk, we can't just calculate it (because of padding etc)
- $lastChunkNr = ceil($size/ 8192) - 1;
- $lastChunkSize = $size - ($lastChunkNr * 8192);
+ $lastChunkNr = ceil($size/ Crypt::BLOCKSIZE) - 1;
+ $lastChunkSize = $size - ($lastChunkNr * Crypt::BLOCKSIZE);
// open stream
$stream = fopen('crypt://' . $path, "r");
if (is_resource($stream)) {
// calculate last chunk position
- $lastChunckPos = ($lastChunkNr * 8192);
+ $lastChunckPos = ($lastChunkNr * Crypt::BLOCKSIZE);
// seek to end
if (@fseek($stream, $lastChunckPos) === -1) {
@@ -439,6 +446,30 @@ class Util {
}
/**
+ * check if encrypted file contain a encryption header
+ *
+ * @param string $path
+ * @return boolean
+ */
+ private function containHeader($path) {
+ // Disable encryption proxy to read the raw data
+ $proxyStatus = \OC_FileProxy::$enabled;
+ \OC_FileProxy::$enabled = false;
+
+ $isHeader = false;
+ $handle = $this->view->fopen($path, 'r');
+
+ if (is_resource($handle)) {
+ $firstBlock = fread($handle, Crypt::BLOCKSIZE);
+ $isHeader = Crypt::isHeader($firstBlock);
+ }
+
+ \OC_FileProxy::$enabled = $proxyStatus;
+
+ return $isHeader;
+ }
+
+ /**
* fix the file size of the encrypted file
* @param string $path absolute path
* @return boolean true / false if file is encrypted
@@ -993,10 +1024,10 @@ class Util {
// check if it is a group mount
if (\OCP\App::isEnabled("files_external")) {
- $mount = \OC_Mount_Config::getSystemMountPoints();
- foreach ($mount as $mountPoint => $data) {
- if ($mountPoint == substr($ownerPath, 1, strlen($mountPoint))) {
- $userIds = array_merge($userIds, $this->getUserWithAccessToMountPoint($data['applicable']['users'], $data['applicable']['groups']));
+ $mounts = \OC_Mount_Config::getSystemMountPoints();
+ foreach ($mounts as $mount) {
+ if ($mount['mountpoint'] == substr($ownerPath, 1, strlen($mount['mountpoint']))) {
+ $userIds = array_merge($userIds, $this->getUserWithAccessToMountPoint($mount['applicable']['users'], $mount['applicable']['groups']));
}
}
}
@@ -1454,10 +1485,12 @@ class Util {
public function isSystemWideMountPoint($path) {
$normalizedPath = ltrim($path, '/');
if (\OCP\App::isEnabled("files_external")) {
- $mount = \OC_Mount_Config::getSystemMountPoints();
- foreach ($mount as $mountPoint => $data) {
- if ($mountPoint == substr($normalizedPath, 0, strlen($mountPoint))) {
- return true;
+ $mounts = \OC_Mount_Config::getSystemMountPoints();
+ foreach ($mounts as $mount) {
+ if ($mount['mountpoint'] == substr($normalizedPath, 0, strlen($mount['mountpoint']))) {
+ if ($this->isMountPointApplicableToUser($mount)) {
+ return true;
+ }
}
}
}
@@ -1465,6 +1498,29 @@ class Util {
}
/**
+ * check if mount point is applicable to user
+ *
+ * @param array $mount contains $mount['applicable']['users'], $mount['applicable']['groups']
+ * @return boolean
+ */
+ protected function isMountPointApplicableToUser($mount) {
+ $uid = \OCP\User::getUser();
+ $acceptedUids = array('all', $uid);
+ // check if mount point is applicable for the user
+ $intersection = array_intersect($acceptedUids, $mount['applicable']['users']);
+ if (!empty($intersection)) {
+ return true;
+ }
+ // check if mount point is applicable for group where the user is a member
+ foreach ($mount['applicable']['groups'] as $gid) {
+ if (\OC_Group::inGroup($uid, $gid)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
* decrypt private key and add it to the current session
* @param array $params with 'uid' and 'password'
* @return mixed session or false