updatePrivateKeyPassword.php 1.94 KB
Newer Older
1
2
3
4
5
6
7
<?php

/**
 * Copyright (c) 2013, Bjoern Schiessle <schiessle@owncloud.com>
 * This file is licensed under the Affero General Public License version 3 or later.
 * See the COPYING-README file.
 *
8
 * Script to change recovery key password
9
10
11
12
13
14
15
16
17
 *
 */

use OCA\Encryption;

\OCP\JSON::checkLoggedIn();
\OCP\JSON::checkAppEnabled('files_encryption');
\OCP\JSON::callCheck();

18
$l = \OC::$server->getL10N('core');
19
20

$return = false;
21
$errorMessage = $l->t('Could not update the private key password.');
22
23
24
25
26
27
28
29

$oldPassword = $_POST['oldPassword'];
$newPassword = $_POST['newPassword'];

$view = new \OC\Files\View('/');
$session = new \OCA\Encryption\Session($view);
$user = \OCP\User::getUser();

30
31
32
33
34
// check new password
$passwordCorrect = \OCP\User::checkPassword($user, $newPassword);

if ($passwordCorrect !== false) {

35
36
37
$proxyStatus = \OC_FileProxy::$enabled;
\OC_FileProxy::$enabled = false;

Florin Peter's avatar
Florin Peter committed
38
$keyPath = '/' . $user . '/files_encryption/' . $user . '.private.key';
39
40
41
42
43

$encryptedKey = $view->file_get_contents($keyPath);
$decryptedKey = \OCA\Encryption\Crypt::decryptPrivateKey($encryptedKey, $oldPassword);

if ($decryptedKey) {
Bjoern Schiessle's avatar
Bjoern Schiessle committed
44
45
46
47
48
49
50
	$cipher = \OCA\Encryption\Helper::getCipher();
	$encryptedKey = \OCA\Encryption\Crypt::symmetricEncryptFileContent($decryptedKey, $newPassword, $cipher);
	if ($encryptedKey) {
		\OCA\Encryption\Keymanager::setPrivateKey($encryptedKey, $user);
		$session->setPrivateKey($decryptedKey);
		$return = true;
	}
51
52
53
} else {
	$result = false;
	$errorMessage = $l->t('The old password was not correct, please try again.');
54
55
56
57
}

\OC_FileProxy::$enabled = $proxyStatus;

58
59
60
61
62
} else {
	$result = false;
	$errorMessage = $l->t('The current log-in password was not correct, please try again.');
}

63
64
// success or failure
if ($return) {
65
	$session->setInitialized(\OCA\Encryption\Session::INIT_SUCCESSFUL);
66
67
	\OCP\JSON::success(array('data' => array('message' => $l->t('Private key password successfully updated.'))));
} else {
68
	\OCP\JSON::error(array('data' => array('message' => $errorMessage)));
69
}