From 83d98e2d8b52b8ea975d39ab28bd9b1e14a2a56f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Schie=C3=9Fle?= Date: Wed, 19 Jun 2013 15:52:33 +0200 Subject: [PATCH 1/3] add some more error messages, in case something went wrong --- apps/files_encryption/lib/crypt.php | 33 +++++++++++++++-------------- apps/files_encryption/lib/util.php | 19 ++++++++++------- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/apps/files_encryption/lib/crypt.php b/apps/files_encryption/lib/crypt.php index 945b342a31..4322bbf1af 100755 --- a/apps/files_encryption/lib/crypt.php +++ b/apps/files_encryption/lib/crypt.php @@ -53,19 +53,26 @@ class Crypt { $res = openssl_pkey_new(array('private_key_bits' => 4096)); - // Get private key - openssl_pkey_export($res, $privateKey); + if ($res === false) { + \OCP\Util::writeLog('Encryption library', 'couldn\'t generate users key-pair for ' . \OCP\User::getUser(), \OCP\Util::ERROR); + $result = false; + } else { - // Get public key - $publicKey = openssl_pkey_get_details($res); + // Get private key + openssl_pkey_export($res, $privateKey); - $publicKey = $publicKey['key']; + // Get public key + $publicKey = openssl_pkey_get_details($res); - return (array( - 'publicKey' => $publicKey, - 'privateKey' => $privateKey - )); + $publicKey = $publicKey['key']; + $result = array( + 'publicKey' => $publicKey, + 'privateKey' => $privateKey + ); + } + + return $result; } /** @@ -287,28 +294,22 @@ class Crypt { public static function symmetricEncryptFileContent($plainContent, $passphrase = '') { if (!$plainContent) { - + \OCP\Util::writeLog('Encryption library', 'symmetrically encryption failed, no content given.', \OCP\Util::ERROR); return false; - } $iv = self::generateIv(); if ($encryptedContent = self::encrypt($plainContent, $iv, $passphrase)) { - // Combine content to encrypt with IV identifier and actual IV $catfile = self::concatIv($encryptedContent, $iv); - $padded = self::addPadding($catfile); return $padded; } else { - \OCP\Util::writeLog('Encryption library', 'Encryption (symmetric) of keyfile content failed', \OCP\Util::ERROR); - return false; - } } diff --git a/apps/files_encryption/lib/util.php b/apps/files_encryption/lib/util.php index b4b3923a79..e8e53859bd 100644 --- a/apps/files_encryption/lib/util.php +++ b/apps/files_encryption/lib/util.php @@ -228,18 +228,21 @@ class Util { // Generate keypair $keypair = Crypt::createKeypair(); - \OC_FileProxy::$enabled = false; + if ($keypair) { - // Save public key - $this->view->file_put_contents($this->publicKeyPath, $keypair['publicKey']); + \OC_FileProxy::$enabled = false; - // Encrypt private key with user pwd as passphrase - $encryptedPrivateKey = Crypt::symmetricEncryptFileContent($keypair['privateKey'], $passphrase); + // Encrypt private key with user pwd as passphrase + $encryptedPrivateKey = Crypt::symmetricEncryptFileContent($keypair['privateKey'], $passphrase); - // Save private key - $this->view->file_put_contents($this->privateKeyPath, $encryptedPrivateKey); + // Save key-pair + if ($encryptedPrivateKey) { + $this->view->file_put_contents($this->privateKeyPath, $encryptedPrivateKey); + $this->view->file_put_contents($this->publicKeyPath, $keypair['publicKey']); + } - \OC_FileProxy::$enabled = true; + \OC_FileProxy::$enabled = true; + } } else { // check if public-key exists but private-key is missing From e2e370f1993ebc4eeac64ce293a992be2205260f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Schie=C3=9Fle?= Date: Wed, 19 Jun 2013 15:58:34 +0200 Subject: [PATCH 2/3] some more error messages --- apps/files_encryption/lib/crypt.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/apps/files_encryption/lib/crypt.php b/apps/files_encryption/lib/crypt.php index 4322bbf1af..3e5a97464d 100755 --- a/apps/files_encryption/lib/crypt.php +++ b/apps/files_encryption/lib/crypt.php @@ -56,20 +56,18 @@ class Crypt { if ($res === false) { \OCP\Util::writeLog('Encryption library', 'couldn\'t generate users key-pair for ' . \OCP\User::getUser(), \OCP\Util::ERROR); $result = false; - } else { - - // Get private key - openssl_pkey_export($res, $privateKey); + } elseif (openssl_pkey_export($res, $privateKey)) { // Get public key $publicKey = openssl_pkey_get_details($res); - $publicKey = $publicKey['key']; $result = array( 'publicKey' => $publicKey, 'privateKey' => $privateKey ); + } else { + \OCP\Util::writeLog('Encryption library', 'couldn\'t export users private key, please check your servers openSSL configuration.' . \OCP\User::getUser(), \OCP\Util::ERROR); } return $result; From fe61230cc14b130bc553cf3270df8f3bd8b88d4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Schie=C3=9Fle?= Date: Wed, 19 Jun 2013 16:55:31 +0200 Subject: [PATCH 3/3] always have a defined return value --- apps/files_encryption/lib/crypt.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/apps/files_encryption/lib/crypt.php b/apps/files_encryption/lib/crypt.php index 3e5a97464d..927064012b 100755 --- a/apps/files_encryption/lib/crypt.php +++ b/apps/files_encryption/lib/crypt.php @@ -51,18 +51,18 @@ class Crypt { */ public static function createKeypair() { + $return = false; + $res = openssl_pkey_new(array('private_key_bits' => 4096)); if ($res === false) { \OCP\Util::writeLog('Encryption library', 'couldn\'t generate users key-pair for ' . \OCP\User::getUser(), \OCP\Util::ERROR); - $result = false; } elseif (openssl_pkey_export($res, $privateKey)) { - // Get public key $publicKey = openssl_pkey_get_details($res); $publicKey = $publicKey['key']; - $result = array( + $return = array( 'publicKey' => $publicKey, 'privateKey' => $privateKey ); @@ -70,7 +70,7 @@ class Crypt { \OCP\Util::writeLog('Encryption library', 'couldn\'t export users private key, please check your servers openSSL configuration.' . \OCP\User::getUser(), \OCP\Util::ERROR); } - return $result; + return $return; } /**