updatePrivateKeyPassword.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. \OCP\JSON::success(array('data' => array('message' => $l->t('Private key password successfully updated.'))));
  36. } else {
  37. \OCP\JSON::error(array('data' => array('message' => $l->t('Could not update the private key password. Maybe the old password was not correct.'))));
  38. }