Ejemplo n.º 1
0
 /**
  * @medium
  * @brief Test that data that is written by the crypto stream wrapper
  * @note Encrypted data is manually prepared and decrypted here to avoid dependency on success of stream_read
  * @note If this test fails with truncate content, check that enough array slices are being rejoined to form $e, as the crypt.php file may have gotten longer and broken the manual
  * reassembly of its data
  */
 function testSymmetricStreamEncryptLongFileContent()
 {
     // Generate a a random filename
     $filename = 'tmp-' . uniqid() . '.test';
     $util = new Encryption\Util(new \OC_FilesystemView(), $this->userId);
     // Save long data as encrypted file using stream wrapper
     $cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/' . $filename, $this->dataLong . $this->dataLong);
     // Test that data was successfully written
     $this->assertTrue(is_int($cryptedFile));
     // Disable encryption proxy to prevent recursive calls
     $proxyStatus = \OC_FileProxy::$enabled;
     \OC_FileProxy::$enabled = false;
     // Get file contents without using any wrapper to get it's actual contents on disk
     $retreivedCryptedFile = $this->view->file_get_contents($this->userId . '/files/' . $filename);
     // Re-enable proxy - our work is done
     \OC_FileProxy::$enabled = $proxyStatus;
     // Check that the file was encrypted before being written to disk
     $this->assertNotEquals($this->dataLong . $this->dataLong, $retreivedCryptedFile);
     // Manuallly split saved file into separate IVs and encrypted chunks
     $r = preg_split('/(00iv00.{16,18})/', $retreivedCryptedFile, NULL, PREG_SPLIT_DELIM_CAPTURE);
     //print_r($r);
     // Join IVs and their respective data chunks
     $e = array();
     $i = 0;
     while ($i < count($r) - 1) {
         $e[] = $r[$i] . $r[$i + 1];
         $i = $i + 2;
     }
     //print_r($e);
     // Get the encrypted keyfile
     $encKeyfile = Encryption\Keymanager::getFileKey($this->view, $util, $filename);
     // Attempt to fetch the user's shareKey
     $shareKey = Encryption\Keymanager::getShareKey($this->view, $this->userId, $util, $filename);
     // get session
     $session = new \OCA\Encryption\Session($this->view);
     // get private key
     $privateKey = $session->getPrivateKey($this->userId);
     // Decrypt keyfile with shareKey
     $plainKeyfile = Encryption\Crypt::multiKeyDecrypt($encKeyfile, $shareKey, $privateKey);
     // Set var for reassembling decrypted content
     $decrypt = '';
     // Manually decrypt chunk
     foreach ($e as $chunk) {
         $chunkDecrypt = Encryption\Crypt::symmetricDecryptFileContent($chunk, $plainKeyfile);
         // Assemble decrypted chunks
         $decrypt .= $chunkDecrypt;
     }
     $this->assertEquals($this->dataLong . $this->dataLong, $decrypt);
     // Teardown
     $this->view->unlink($this->userId . '/files/' . $filename);
     Encryption\Keymanager::deleteFileKey($this->view, $filename);
 }
Ejemplo n.º 2
0
 /**
  * Decrypt a keyfile
  * @param string $filePath
  * @param string $privateKey
  * @return false|string
  */
 private function decryptKeyfile($filePath, $privateKey)
 {
     // Get the encrypted keyfile
     $encKeyfile = Keymanager::getFileKey($this->view, $this, $filePath);
     // The file has a shareKey and must use it for decryption
     $shareKey = Keymanager::getShareKey($this->view, $this->keyId, $this, $filePath);
     $plainKeyfile = Crypt::multiKeyDecrypt($encKeyfile, $shareKey, $privateKey);
     return $plainKeyfile;
 }