updatePrivateKeyPassword.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /**
  3. * Copyright (c) 2013, Bjoern Schiessle <schiessle@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or later.
  5. * See the COPYING-README file.
  6. *
  7. * @brief Script to change recovery key password
  8. *
  9. */
  10. use OCA\Encryption;
  11. \OCP\JSON::checkLoggedIn();
  12. \OCP\JSON::checkAppEnabled('files_encryption');
  13. \OCP\JSON::callCheck();
  14. $l = OC_L10N::get('core');
  15. $return = false;
  16. $oldPassword = $_POST['oldPassword'];
  17. $newPassword = $_POST['newPassword'];
  18. $view = new \OC\Files\View('/');
  19. $session = new \OCA\Encryption\Session($view);
  20. $user = \OCP\User::getUser();
  21. $proxyStatus = \OC_FileProxy::$enabled;
  22. \OC_FileProxy::$enabled = false;
  23. $keyPath = '/' . $user . '/files_encryption/' . $user . '.private.key';
  24. $encryptedKey = $view->file_get_contents($keyPath);
  25. $decryptedKey = \OCA\Encryption\Crypt::decryptPrivateKey($encryptedKey, $oldPassword);
  26. if ($decryptedKey) {
  27. $encryptedKey = \OCA\Encryption\Crypt::symmetricEncryptFileContent($decryptedKey, $newPassword);
  28. $view->file_put_contents($keyPath, $encryptedKey);
  29. $session->setPrivateKey($decryptedKey);
  30. $return = true;
  31. }
  32. \OC_FileProxy::$enabled = $proxyStatus;
  33. // success or failure
  34. if ($return) {
  35. $session->setInitialized(\OCA\Encryption\Session::INIT_SUCCESSFUL);
  36. \OCP\JSON::success(array('data' => array('message' => $l->t('Private key password successfully updated.'))));
  37. } else {
  38. \OCP\JSON::error(array('data' => array('message' => $l->t('Could not update the private key password. Maybe the old password was not correct.'))));
  39. }