aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files_encryption/ajax/adminrecovery.php
blob: 520c7156c89f1c1f2224305c07431a916354f4ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php

/**
 * Copyright (c) 2013, Sam Tuke <samtuke@owncloud.com>
 * This file is licensed under the Affero General Public License version 3 or later.
 * See the COPYING-README file.
 *
 * @brief Script to handle admin settings for encrypted key recovery
 */
use OCA\Encryption;

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

$return = false;

function checkPassword($view, $password, $recoveryKeyId) {
	$pathKey = '/owncloud_private_key/'. $recoveryKeyId . ".private.key";
	$pathControlData = '/control-file/controlfile.enc';

	$proxyStatus = \OC_FileProxy::$enabled;
    \OC_FileProxy::$enabled = false;

	$recoveryKey = $view->file_get_contents( $pathKey );

	$decryptedRecoveryKey = \OCA\Encryption\Crypt::symmetricDecryptFileContent($recoveryKey, $password);

	$controlData = $view->file_get_contents($pathControlData);
	$decryptedControlData = \OCA\Encryption\Crypt::keyDecrypt($controlData, $decryptedRecoveryKey);

	\OC_FileProxy::$enabled = $proxyStatus;

	if ($decryptedControlData === 'ownCloud') {
		return true;
	} else {
		return false;
	}
}


// Enable recoveryAdmin

$recoveryKeyId = OC_Appconfig::getValue('files_encryption', 'recoveryKeyId');

if (isset($_POST['adminEnableRecovery']) && $_POST['adminEnableRecovery'] == 1){

	$view = new \OC\Files\View('/');

	if ($recoveryKeyId === null) {
		$recoveryKeyId = 'recovery_' . substr(md5(time()), 0, 8);
		\OC_Appconfig::setValue('files_encryption', 'recoveryKeyId', $recoveryKeyId);
	}

	if (!$view->is_dir('/owncloud_private_key')) {
		$view->mkdir('/owncloud_private_key');
	}

	if (
		(!$view->file_exists("/public-keys/" . $recoveryKeyId . ".public.key")
		|| !$view->file_exists("/owncloud_private_key/" . $recoveryKeyId . ".private.key"))
	) {

		$keypair = \OCA\Encryption\Crypt::createKeypair();

		\OC_FileProxy::$enabled = false;

		// Save public key

		if (!$view->is_dir('/public-keys')) {
			$view->mkdir('/public-keys');
		}

		$view->file_put_contents('/public-keys/' . $recoveryKeyId . '.public.key', $keypair['publicKey']);

		// Encrypt private key empthy passphrase
		$encryptedPrivateKey = \OCA\Encryption\Crypt::symmetricEncryptFileContent($keypair['privateKey'], $_POST['recoveryPassword']);

		// Save private key
		$view->file_put_contents('/owncloud_private_key/' . $recoveryKeyId . '.private.key', $encryptedPrivateKey);

		// create control file which let us check later on if the entered password was correct.
		$encryptedControlData =  \OCA\Encryption\Crypt::keyEncrypt("ownCloud", $keypair['publicKey']);
		if (!$view->is_dir('/control-file')) {
			$view->mkdir('/control-file');
		}
		$view->file_put_contents('/control-file/controlfile.enc', $encryptedControlData);

		\OC_FileProxy::$enabled = true;

		// Set recoveryAdmin as enabled
		OC_Appconfig::setValue('files_encryption', 'recoveryAdminEnabled', 1);

		$return = true;

	} else { // get recovery key and check the password
		$return = checkPassword($view, $_POST['recoveryPassword'] ,$recoveryKeyId);
		if ($return) {
			OC_Appconfig::setValue('files_encryption', 'recoveryAdminEnabled', 1);
		} 
	}

// Disable recoveryAdmin
} elseif (
	isset($_POST['adminEnableRecovery'])
	&& 0 == $_POST['adminEnableRecovery']
) {
	$view = new \OC\Files\View('/');
	$return = checkPassword($view, $_POST['recoveryPassword'], $recoveryKeyId);

	if ($return) {
	// Set recoveryAdmin as disabled
	OC_Appconfig::setValue('files_encryption', 'recoveryAdminEnabled', 0);
	}
}

// Return success or failure
( $return ) ? \OCP\JSON::success() : \OCP\JSON::error();