Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change to re-create masterkeys #28145

Closed
wants to merge 1 commit into from
Closed

Conversation

sharidas
Copy link
Contributor

@sharidas sharidas commented Jun 16, 2017

This change will help users to recreate masterkey
for encryption.

Signed-off-by: Sujith H sharidasan@owncloud.com

Description

This change will help users to regenerate masterkeys for encrypting data

Related Issue

https://github.com/owncloud/enterprise/issues/2033
#21434

Motivation and Context

This change will help users to regenerate masterkey. In current state masterkey once enabled cannot be changed. This change addresses the issue.

How Has This Been Tested?

Using occ command created users admin and user1. Enabled encryption app and then tried to enable masterkey. Now open the UI and try to login to both admin and user1. Verify the welcome.txt file is encrypted using cat command. As admin user try to upload/create new file, let's say hello.txt. Then update the file multiple times. And then run the command ./occ encryption:recreate-master-key from the command line. Relogin as admin user to view the files. Re-run the encryption:recreate-master-key command multiple times. And then relogin as admin user. The admin user was able to view the files.
cat the welcome.txt, hello.txt ( or any other files uploaded or created) of both admin and user1. The files are encrypted.

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@sharidas sharidas changed the title Change to re-create masterkeys [WIP] Change to re-create masterkeys Jun 16, 2017
@@ -80,6 +82,20 @@ $(document).ready(function () {
}
});

$("#newMasterKey").click(function () {
console.log("I am clicked");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

debug

} else {
debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

debug

@sharidas
Copy link
Contributor Author

@tomneedham Thanks for pointing it out. Removing them :)

@sharidas sharidas force-pushed the recreate-masterkey-task branch 2 times, most recently from 4136fd9 to a022e0b Compare June 20, 2017 04:59
@PVince81 PVince81 mentioned this pull request Jun 21, 2017
Copy link
Contributor

@PVince81 PVince81 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is running within a single ajax request then this is a no go. Decrypting and reencrypting all files within a PHP request is very likely to run into PHP timeouts.

Also, if I understand correctly, this would decrypt all files on disk first then reencrypt. It would be better (but also much more complex to implement here) to decrypt with old key and reencrypt on the fly with new key, to avoid having the files exposed for a short period of time.

}

public function createNewMasterKey() {
\OC::$server->getLogger()->warning(__METHOD__." so time to create new key!!!", ['app' => __CLASS__]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use a better message or remove ?

if (empty($this->failed)) {
//Now recreate new encryption
//Delete the encryption app
\OC::$server->getAppConfig()->deleteApp('encryption');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks a bit hacky. Can we just delete the relevant keys instead of destroying everything and reenabling ?

//Delete the files_encryption dir
$this->rootView->deleteAll('files_encryption');
\OC::$server->getConfig()->deleteAppValue('files_encryption','installed_version');
\OC::$server->getConfig()->deleteAppValues('encryption');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the difference between deleteApp above and deleteAppValues ? Doesn't this do the same ?

\OC::$server->getConfig()->deleteAppValues('encryption');

//Re-enable the encryption app
\OC_App::enable('encryption');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use app manager if possible

\OC::$server->getAppConfig()->deleteApp('encryption');
//Delete the files_encryption dir
$this->rootView->deleteAll('files_encryption');
\OC::$server->getConfig()->deleteAppValue('files_encryption','installed_version');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

receive IConfig injected in the constructor and use it instead of getConfig().

do {
$users = $backend->getUsers('', $limit, $offset);
foreach ($users as $user) {
echo "\n Entering user $user\n";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

echo ???


$("#reEncryptFS").click(function () {
$.post(
OC.generateUrl('/apps/encryption/ajax/reencryptFiles')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will very likely run into PHP timeouts... would recommend only providing OCC command for this

@@ -99,6 +99,18 @@ public function decryptAll(InputInterface $input, OutputInterface $output, $user

if (empty($this->failed)) {
$this->output->writeln('all files could be decrypted successfully!');
//Now recreate new encryption
//Delete the encryption app
\OC::$server->getAppConfig()->deleteApp('encryption');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dejavu ? I've seen this code before somewhere above

@PVince81 PVince81 added this to the development milestone Jul 4, 2017
@sharidas sharidas force-pushed the recreate-masterkey-task branch from a022e0b to 04c958a Compare July 4, 2017 11:15
@@ -672,6 +685,9 @@ private function updateEncryptedVersion(Storage $sourceStorage, $sourceInternalP
// in case of a rename we need to manipulate the source cache because
// this information will be kept for the new target
if ($isRename) {
$encryptedVersion = 1;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change comes from #28107. The rename operation is happening at

$this->rootView->rename($target, $source);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will remove this change now.

$progress = new ProgressBar($output);
$progress->start();
while(!$this->appConfig->hasKey('encryption', 'publicShareKeyId')) {
sleep(3);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this. Any better suggestion would be nice. It takes some time to regenerate new master keys.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how is that even possible ? PHP is sequential, what is it waiting for ?

AFAIK all of the openssl commands are synchronous

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's waiting for the new master keys. The code execution happens sequentially.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case sleep is useless because it will block the PHP process.

If adding it solves the problem then it means there is some background / async process running somewhere, and that's not good because it means potential race conditions in other parts of the code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and reading the code above I don't see any code that would spawn a separate process ? which line is starting the process that generates the new master key ?

@sharidas sharidas force-pushed the recreate-masterkey-task branch from 04c958a to cc8ec6e Compare July 5, 2017 05:57
@sharidas
Copy link
Contributor Author

sharidas commented Jul 5, 2017

Removed the ugly while loop which waited to create the masterkey. Instead in the keymanager I created the function which creates the masterkey and publicsharekey. And reused the same in the constructor.

@sharidas sharidas force-pushed the recreate-masterkey-task branch 3 times, most recently from 0790c1d to 258c10e Compare July 7, 2017 07:48
@sharidas sharidas changed the title [WIP] Change to re-create masterkeys Change to re-create masterkeys Jul 7, 2017
@sharidas sharidas force-pushed the recreate-masterkey-task branch from 258c10e to aa2fadd Compare July 9, 2017 14:50
@PVince81
Copy link
Contributor

@sharidas failling unit tests, please fix

@sharidas sharidas force-pushed the recreate-masterkey-task branch 4 times, most recently from eacf990 to b2e0389 Compare July 19, 2017 12:03
@sharidas sharidas force-pushed the recreate-masterkey-task branch 2 times, most recently from acf7f57 to 8d5a5d2 Compare July 31, 2017 12:05
@sharidas
Copy link
Contributor Author

Need review for this change too... This is the followup of owncloud/encryption#6

Copy link
Contributor

@PVince81 PVince81 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

too hacky

@@ -201,16 +201,19 @@ public function getMetaData($path) {
*/
public function file_get_contents($path) {

$encryptionModule = $this->getEncryptionModule($path);
//if ( \OC::$server->getAppConfig()->getValue('encryption', 'enabled', false) !== false) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove ?

@@ -421,6 +425,9 @@ public function fopen($path, $mode, $sourceFileOfRename = null) {
$encryptionModule = $this->encryptionManager->getEncryptionModule($encryptionModuleId);
$shouldEncrypt = $encryptionModule->shouldEncrypt($fullPath);
$signed = true;
if (\OC::$server->getSession()->get('decryptAllCmd') === true) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arghh, no, that's too hacky

@sharidas sharidas force-pushed the recreate-masterkey-task branch 2 times, most recently from 6919f9a to d40404a Compare July 31, 2017 17:13
@CLAassistant
Copy link

CLAassistant commented Jul 31, 2017

CLA assistant check
All committers have signed the CLA.

@sharidas
Copy link
Contributor Author

@PVince81 , Modified changes to some of the core API's. Removed the session hack from the code.

@sharidas sharidas force-pushed the recreate-masterkey-task branch from d40404a to f3c0144 Compare August 1, 2017 03:12
This change supports a new command to re-create
masterkey

Signed-off-by: Sujith H <sharidasan@owncloud.com>
@sharidas sharidas force-pushed the recreate-masterkey-task branch from f3c0144 to 616e6dd Compare August 1, 2017 03:28
* @return resource|bool
* @throws GenericEncryptionException
* @throws ModuleDoesNotExistsException
*/
public function fopen($path, $mode, $sourceFileOfRename = null) {
public function fopen($path, $mode, $sourceFileOfRename = null, $getDecryptedFile = false) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels wrong and unintuitive.

Also I don't understand: the encryption wrapper always gives access to decrypted data. fread/fwrite operations are using decrypted data. Maybe what you meant here is that you want to access the encrypted binary data directly by skipping this wrapper.

So the key here is likely to find a way to skip/disable the encryption wrapper to get access to the file. Not sure how complicated this could be.

@PVince81 PVince81 modified the milestones: planned, development Aug 7, 2017
@sharidas
Copy link
Contributor Author

Obsolete PR. The final PR: #28774

@sharidas sharidas closed this Aug 28, 2017
@lock
Copy link

lock bot commented Aug 2, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Aug 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants