/** * 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; }
/** * @param int $count * @return bool|string * @throws \OCA\Files_Encryption\Exception\EncryptionException */ public function stream_read($count) { $this->writeCache = ''; if ($count !== Crypt::BLOCKSIZE) { \OCP\Util::writeLog('Encryption library', 'PHP "bug" 21641 no longer holds, decryption system requires refactoring', \OCP\Util::FATAL); throw new EncryptionException('expected a block size of 8192 byte', EncryptionException::UNEXPECTED_BLOCK_SIZE); } // Get the data from the file handle $data = fread($this->handle, $count); // if this block contained the header we move on to the next block if (Crypt::isHeader($data)) { $data = fread($this->handle, $count); } $result = null; if (strlen($data)) { if (!$this->getKey()) { // Error! We don't have a key to decrypt the file with throw new \Exception('Encryption key not found for "' . $this->rawPath . '" during attempted read via stream'); } else { // Decrypt data $result = Crypt::symmetricDecryptFileContent($data, $this->plainKey, $this->cipher); } } return $result; }