-
Notifications
You must be signed in to change notification settings - Fork 13
/
RecreateMasterKey.php
263 lines (218 loc) · 7.79 KB
/
RecreateMasterKey.php
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
/**
* @author Sujith Haridasan <[email protected]>
*
* @copyright Copyright (c) 2017, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Encryption\Command;
use OC\Encryption\DecryptAll;
use OC\Encryption\Exceptions\DecryptionFailedException;
use OC\Encryption\Manager;
use OC\Files\Filesystem;
use OC\Files\View;
use OC\Memcache\ArrayCache;
use OCA\Encryption\Crypto\EncryptAll;
use OCA\Encryption\KeyManager;
use OCA\Encryption\Users\Setup;
use OCA\Encryption\Util;
use OCP\App\IAppManager;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IL10N;
use OCP\ILogger;
use OCP\ISession;
use OCP\IUserManager;
use OCP\Mail\IMailer;
use OCP\Security\ISecureRandom;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
class RecreateMasterKey extends Command {
/** @var Manager */
protected $encryptionManager;
/** @var IUserManager */
protected $userManager;
/** @var View */
protected $rootView;
/** @var KeyManager */
protected $keyManager;
/** @var Util */
protected $util;
/** @var \OC\Encryption\Util */
protected $encUtil;
/** @var IAppManager */
protected $appManager;
/** @var IAppConfig */
protected $appConfig;
/** @var IConfig */
protected $config;
/** @var ISession */
protected $session;
/** @var QuestionHelper */
protected $questionHelper;
/** @var Setup */
protected $userSetup;
/** @var IMailer */
protected $mailer;
/** @var ISecureRandom */
protected $secureRandom;
/** @var IL10N */
protected $l;
/** @var ILogger */
protected $logger;
/** @var */
protected $encryptAll;
protected $decryptAll;
/** @var array files which couldn't be decrypted */
protected $failed;
/**
* RecreateMasterKey constructor.
*
* @param IUserManager $userManager
* @param View $rootView
* @param KeyManager $keyManager
* @param Util $util
* @param IAppManager $appManager
* @param IAppConfig $appConfig
* @param IConfig $config
* @param ISession $session
* @param QuestionHelper $questionHelper
* @param Setup $userSetup
* @param IMailer $mailer
* @param ISecureRandom $secureRandom
* @param IL10N $l
* @param ILogger $logger
*/
public function __construct(IUserManager $userManager, View $rootView, KeyManager $keyManager, Util $util, \OC\Encryption\Util $encUtil,
IAppManager $appManager, IAppConfig $appConfig, IConfig $config, ISession $session,
Manager $encryptionManager, QuestionHelper $questionHelper, Setup $userSetup, IMailer $mailer,
ISecureRandom $secureRandom, IL10N $l, ILogger $logger) {
parent::__construct();
$this->userManager = $userManager;
$this->rootView = $rootView;
$this->keyManager = $keyManager;
$this->util = $util;
$this->encUtil = $encUtil;
$this->appManager = $appManager;
$this->appConfig = $appConfig;
$this->config = $config;
$this->session = $session;
$this->encryptionManager = $encryptionManager;
$this->questionHelper = $questionHelper;
$this->userSetup = $userSetup;
$this->mailer = $mailer;
$this->secureRandom = $secureRandom;
$this->l = $l;
$this->logger = $logger;
}
protected function configure() {
parent::configure();
$this
->setName('encryption:recreate-master-key')
->setDescription('Replace existing master key with new one. Encrypt the file system with newly created master key')
;
$this->addOption(
'yes',
'y',
InputOption::VALUE_NONE,
'Answer yes to all questions'
);
}
protected function execute(InputInterface $input, OutputInterface $output) {
$yes = $input->getOption('yes');
if ($this->util->isMasterKeyEnabled()) {
$question = new ConfirmationQuestion(
'Warning: In order to re-create master key, the entire ownCloud filesystem will be decrypted and then encrypted using new master key.'
. ' Do you want to continue? (y/n)', false);
if ($yes || $this->questionHelper->ask($input, $output, $question)) {
$output->writeln("Decryption started\n");
$progress = new ProgressBar($output);
$progress->start();
$progress->setMessage("Decryption in progress...");
$progress->advance();
$this->decryptAllUsers($input, $output);
$progress->finish();
if (empty($this->failed)) {
$this->appManager->disableApp('encryption');
//Delete the files_encryption dir
$filesEncryptionDir = $this->encUtil->getKeyStorageRoot();
if ($filesEncryptionDir === '') {
$this->rootView->deleteAll('files_encryption');
} else {
$this->rootView->deleteAll($filesEncryptionDir . '/files_encryption');
}
$this->appConfig->setValue('core', 'encryption_enabled', 'no');
$this->appConfig->deleteKey('encryption', 'useMasterKey');
$this->appConfig->deleteKey('encryption', 'masterKeyId');
$this->appConfig->deleteKey('encryption', 'recoveryKeyId');
$this->appConfig->deleteKey('encryption', 'publicShareKeyId');
$this->appConfig->deleteKey('files_encryption', 'installed_version');
}
$output->writeln("\nDecryption completed\n");
//Reencrypt again
$this->appManager->enableApp('encryption');
$this->appConfig->setValue('core', 'encryption_enabled', 'yes');
$this->appConfig->setValue('encryption', 'enabled', 'yes');
$output->writeln("Encryption started\n");
$output->writeln("Waiting for creating new masterkey\n");
$this->keyManager->setPublicShareKeyIDAndMasterKeyId();
$output->writeln("New masterkey created successfully\n");
$this->appConfig->setValue('encryption', 'enabled', 'yes');
$this->appConfig->setValue('encryption', 'useMasterKey', '1');
/**
* Call validateShareKey method, to check if public share exists,
* else create one.
*/
$this->keyManager->validateShareKey();
/**
* Same here, check if public masterkey exists else
* create one.
*/
$this->keyManager->validateMasterKey();
$this->encryptAllUsers($input, $output);
$output->writeln("\nEncryption completed successfully\n");
} else {
$output->writeln("The process is abandoned");
}
} else {
$output->writeln("Master key is not enabled.\n");
}
}
protected function decryptAllUsers(InputInterface $input, OutputInterface $output) {
$this->decryptAll = new DecryptAll($this->encryptionManager, $this->userManager, $this->rootView, $this->logger);
$this->decryptAll->decryptAll($input,$output);
}
protected function encryptAllUsers(InputInterface $input, OutputInterface $output) {
/*
* We are reusing the encryptAll code but not the decryptAll. The reason being
* decryptAll finishes by encrypting. Which is not what we want. This will make
* things out of scope for this command. We want first the entire oC FS to be
* decrypt. Then re-encrypt the entire oC FS with the new master key generated.
*
*/
$this->encryptAll = new EncryptAll(
$this->userSetup, $this->userManager, $this->rootView,
$this->keyManager, $this->util, $this->config,
$this->mailer, $this->l, $this->questionHelper,
$this->secureRandom);
$this->encryptAll->encryptAll($input, $output);
}
}