示例#1
0
 protected function setUp()
 {
     parent::setUp();
     // login user
     self::loginHelper(self::TEST_ENCRYPTION_UTIL_USER1);
     \OC_User::setUserId(self::TEST_ENCRYPTION_UTIL_USER1);
     $this->userId = self::TEST_ENCRYPTION_UTIL_USER1;
     $this->pass = self::TEST_ENCRYPTION_UTIL_USER1;
     // set content for encrypting / decrypting in tests
     $this->dataUrl = __DIR__ . '/../lib/crypt.php';
     $this->dataShort = 'hats';
     $this->dataLong = file_get_contents(__DIR__ . '/../lib/crypt.php');
     $this->legacyData = __DIR__ . '/legacy-text.txt';
     $this->legacyEncryptedData = __DIR__ . '/legacy-encrypted-text.txt';
     $this->legacyEncryptedDataKey = __DIR__ . '/encryption.key';
     $this->legacyKey = "30943623843030686906";
     $keypair = \OCA\Files_Encryption\Crypt::createKeypair();
     $this->genPublicKey = $keypair['publicKey'];
     $this->genPrivateKey = $keypair['privateKey'];
     $this->publicKeyDir = \OCA\Files_Encryption\Keymanager::getPublicKeyPath();
     $this->encryptionDir = '/' . $this->userId . '/' . 'files_encryption';
     $this->keysPath = $this->encryptionDir . '/' . 'keys';
     $this->publicKeyPath = $this->publicKeyDir . '/' . $this->userId . '.publicKey';
     // e.g. data/public-keys/admin.publicKey
     $this->privateKeyPath = $this->encryptionDir . '/' . $this->userId . '.privateKey';
     // e.g. data/admin/admin.privateKey
     $this->view = new \OC\Files\View('/');
     $this->util = new \OCA\Files_Encryption\Util($this->view, $this->userId);
     // remember files_trashbin state
     $this->stateFilesTrashbin = \OC_App::isEnabled('files_trashbin');
     // we don't want to tests with app files_trashbin enabled
     \OC_App::disable('files_trashbin');
 }
示例#2
0
 protected function setUp()
 {
     parent::setUp();
     // set content for encrypting / decrypting in tests
     $this->dataLong = file_get_contents(__DIR__ . '/../lib/crypt.php');
     $this->dataShort = 'hats';
     $this->dataUrl = __DIR__ . '/../lib/crypt.php';
     $this->legacyData = __DIR__ . '/legacy-text.txt';
     $this->legacyEncryptedData = __DIR__ . '/legacy-encrypted-text.txt';
     $this->randomKey = \OCA\Files_Encryption\Crypt::generateKey();
     $keypair = \OCA\Files_Encryption\Crypt::createKeypair();
     $this->genPublicKey = $keypair['publicKey'];
     $this->genPrivateKey = $keypair['privateKey'];
     $this->view = new \OC\Files\View('/');
     self::loginHelper(self::TEST_USER);
     $this->userId = self::TEST_USER;
     $this->pass = self::TEST_USER;
     $userHome = \OC_User::getHome($this->userId);
     $this->dataDir = str_replace('/' . $this->userId, '', $userHome);
 }
示例#3
0
 /**
  * Sets up user folders and keys for serverside encryption
  *
  * @param string $passphrase to encrypt server-stored private key with
  * @return bool
  */
 public function setupServerSide($passphrase = null)
 {
     // Set directories to check / create
     $setUpDirs = array($this->userDir, $this->publicKeyDir, $this->encryptionDir, $this->keysPath);
     // Check / create all necessary dirs
     foreach ($setUpDirs as $dirPath) {
         if (!$this->view->file_exists($dirPath)) {
             $this->view->mkdir($dirPath);
         }
     }
     // Create user keypair
     // we should never override a keyfile
     if (!$this->view->file_exists($this->publicKeyPath) && !$this->view->file_exists($this->privateKeyPath)) {
         // Generate keypair
         $keypair = Crypt::createKeypair();
         if ($keypair) {
             \OC_FileProxy::$enabled = false;
             // Encrypt private key with user pwd as passphrase
             $encryptedPrivateKey = Crypt::symmetricEncryptFileContent($keypair['privateKey'], $passphrase, Helper::getCipher());
             // Save key-pair
             if ($encryptedPrivateKey) {
                 $header = crypt::generateHeader();
                 $this->view->file_put_contents($this->privateKeyPath, $header . $encryptedPrivateKey);
                 $this->view->file_put_contents($this->publicKeyPath, $keypair['publicKey']);
             }
             \OC_FileProxy::$enabled = true;
         }
     } else {
         // check if public-key exists but private-key is missing
         if ($this->view->file_exists($this->publicKeyPath) && !$this->view->file_exists($this->privateKeyPath)) {
             \OCP\Util::writeLog('Encryption library', 'public key exists but private key is missing for "' . $this->keyId . '"', \OCP\Util::FATAL);
             return false;
         } else {
             if (!$this->view->file_exists($this->publicKeyPath) && $this->view->file_exists($this->privateKeyPath)) {
                 \OCP\Util::writeLog('Encryption library', 'private key exists but public key is missing for "' . $this->keyId . '"', \OCP\Util::FATAL);
                 return false;
             }
         }
     }
     return true;
 }
示例#4
0
文件: crypt.php 项目: samj1912/repo
 /**
  * @large
  */
 public function testMultiKeyEncrypt()
 {
     # TODO: search in keyfile for actual content as IV will ensure this test always passes
     $pair1 = \OCA\Files_Encryption\Crypt::createKeypair();
     $this->assertEquals(2, count($pair1));
     $this->assertTrue(strlen($pair1['publicKey']) > 1);
     $this->assertTrue(strlen($pair1['privateKey']) > 1);
     $crypted = \OCA\Files_Encryption\Crypt::multiKeyEncrypt($this->dataShort, array($pair1['publicKey']));
     $this->assertNotEquals($this->dataShort, $crypted['data']);
     $decrypt = \OCA\Files_Encryption\Crypt::multiKeyDecrypt($crypted['data'], $crypted['keys'][0], $pair1['privateKey']);
     $this->assertEquals($this->dataShort, $decrypt);
 }