|
|
|
|
@ -3,10 +3,12 @@ |
|
|
|
|
/** |
|
|
|
|
* ownCloud |
|
|
|
|
* |
|
|
|
|
* @author Sam Tuke, Frank Karlitschek, Robin Appelman |
|
|
|
|
* @copyright 2012 Sam Tuke samtuke@owncloud.com, |
|
|
|
|
* Robin Appelman icewind@owncloud.com, Frank Karlitschek |
|
|
|
|
* frank@owncloud.org |
|
|
|
|
* @copyright (C) 2014 ownCloud, Inc. |
|
|
|
|
* |
|
|
|
|
* @author Bjoern Schiessle <schiessle@owncloud.com> |
|
|
|
|
* @author Sam Tuke <samtuke@owncloud.com> |
|
|
|
|
* @author Frank Karlitschek <frank@owncloud.com> |
|
|
|
|
* @author Robin Appelman <icewind@owncloud.com> |
|
|
|
|
* |
|
|
|
|
* This library is free software; you can redistribute it and/or |
|
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE |
|
|
|
|
@ -24,7 +26,6 @@ |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
namespace OCA\Encryption; |
|
|
|
|
use OCA\Encryption\Exceptions\EncryptionException; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Class for common cryptography functionality |
|
|
|
|
@ -189,7 +190,7 @@ class Crypt { |
|
|
|
|
* @param string $passphrase |
|
|
|
|
* @param string $cypher used for encryption, currently we support AES-128-CFB and AES-256-CFB |
|
|
|
|
* @return string encrypted file content |
|
|
|
|
* @throws \OCA\Encryption\Exceptions\EncryptionException |
|
|
|
|
* @throws \OCA\Encryption\Exception\EncryptionException |
|
|
|
|
*/ |
|
|
|
|
private static function encrypt($plainContent, $iv, $passphrase = '', $cipher = Crypt::DEFAULT_CIPHER) { |
|
|
|
|
|
|
|
|
|
@ -198,7 +199,7 @@ class Crypt { |
|
|
|
|
if (!$encryptedContent) { |
|
|
|
|
$error = "Encryption (symmetric) of content failed: " . openssl_error_string(); |
|
|
|
|
\OCP\Util::writeLog('Encryption library', $error, \OCP\Util::ERROR); |
|
|
|
|
throw new Exceptions\EncryptionException($error, 50); |
|
|
|
|
throw new Exception\EncryptionException($error, Exception\EncryptionException::ENCRYPTION_FAILED); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return $encryptedContent; |
|
|
|
|
@ -290,7 +291,7 @@ class Crypt { |
|
|
|
|
$padded = self::addPadding($catfile); |
|
|
|
|
|
|
|
|
|
return $padded; |
|
|
|
|
} catch (EncryptionException $e) { |
|
|
|
|
} catch (Exception\EncryptionException $e) { |
|
|
|
|
$message = 'Could not encrypt file content (code: ' . $e->getCode() . '): '; |
|
|
|
|
\OCP\Util::writeLog('files_encryption', $message . $e->getMessage(), \OCP\Util::ERROR); |
|
|
|
|
return false; |
|
|
|
|
@ -378,7 +379,7 @@ class Crypt { |
|
|
|
|
* @param string $plainContent content to be encrypted |
|
|
|
|
* @param array $publicKeys array keys must be the userId of corresponding user |
|
|
|
|
* @return array keys: keys (array, key = userId), data |
|
|
|
|
* @throws \OCA\Encryption\Exceptions\\MultiKeyEncryptException if encryption failed |
|
|
|
|
* @throws \OCA\Encryption\Exception\MultiKeyEncryptException if encryption failed |
|
|
|
|
* @note symmetricDecryptFileContent() can decrypt files created using this method |
|
|
|
|
*/ |
|
|
|
|
public static function multiKeyEncrypt($plainContent, array $publicKeys) { |
|
|
|
|
@ -386,7 +387,7 @@ class Crypt { |
|
|
|
|
// openssl_seal returns false without errors if $plainContent |
|
|
|
|
// is empty, so trigger our own error |
|
|
|
|
if (empty($plainContent)) { |
|
|
|
|
throw new Exceptions\MultiKeyEncryptException('Cannot multiKeyEncrypt empty plain content', 10); |
|
|
|
|
throw new Exception\MultiKeyEncryptException('Cannot multiKeyEncrypt empty plain content', Exception\MultiKeyEncryptException::EMPTY_DATA); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Set empty vars to be set by openssl by reference |
|
|
|
|
@ -413,7 +414,8 @@ class Crypt { |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
throw new Exceptions\MultiKeyEncryptException('multi key encryption failed: ' . openssl_error_string(), 20); |
|
|
|
|
throw new Exception\MultiKeyEncryptException('multi key encryption failed: ' . openssl_error_string(), |
|
|
|
|
Exception\MultiKeyEncryptException::OPENSSL_SEAL_FAILED); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
@ -423,7 +425,7 @@ class Crypt { |
|
|
|
|
* @param string $encryptedContent |
|
|
|
|
* @param string $shareKey |
|
|
|
|
* @param mixed $privateKey |
|
|
|
|
* @throws \OCA\Encryption\Exceptions\\MultiKeyDecryptException if decryption failed |
|
|
|
|
* @throws \OCA\Encryption\Exception\MultiKeyDecryptException if decryption failed |
|
|
|
|
* @internal param string $plainContent contains decrypted content |
|
|
|
|
* @return string $plainContent decrypted string |
|
|
|
|
* @note symmetricDecryptFileContent() can be used to decrypt files created using this method |
|
|
|
|
@ -433,7 +435,8 @@ class Crypt { |
|
|
|
|
public static function multiKeyDecrypt($encryptedContent, $shareKey, $privateKey) { |
|
|
|
|
|
|
|
|
|
if (!$encryptedContent) { |
|
|
|
|
throw new Exceptions\MultiKeyDecryptException('Cannot mutliKeyDecrypt empty plain content', 10); |
|
|
|
|
throw new Exception\MultiKeyDecryptException('Cannot mutliKeyDecrypt empty plain content', |
|
|
|
|
Exception\MultiKeyDecryptException::EMPTY_DATA); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (openssl_open($encryptedContent, $plainContent, $shareKey, $privateKey)) { |
|
|
|
|
@ -441,7 +444,8 @@ class Crypt { |
|
|
|
|
return $plainContent; |
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
throw new Exceptions\MultiKeyDecryptException('multiKeyDecrypt with share-key' . $shareKey . 'failed: ' . openssl_error_string(), 20); |
|
|
|
|
throw new Exception\MultiKeyDecryptException('multiKeyDecrypt with share-key' . $shareKey . 'failed: ' . openssl_error_string(), |
|
|
|
|
Exception\MultiKeyDecryptException::OPENSSL_OPEN_FAILED); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
@ -550,14 +554,15 @@ class Crypt { |
|
|
|
|
* get chiper from header |
|
|
|
|
* |
|
|
|
|
* @param array $header |
|
|
|
|
* @throws \OCA\Encryption\Exceptions\EncryptionException |
|
|
|
|
* @throws \OCA\Encryption\Exception\EncryptionException |
|
|
|
|
*/ |
|
|
|
|
public static function getCipher($header) { |
|
|
|
|
$cipher = isset($header['cipher']) ? $header['cipher'] : 'AES-128-CFB'; |
|
|
|
|
|
|
|
|
|
if ($cipher !== 'AES-256-CFB' && $cipher !== 'AES-128-CFB') { |
|
|
|
|
|
|
|
|
|
throw new \OCA\Encryption\Exceptions\EncryptionException('file header broken, no supported cipher defined', 40); |
|
|
|
|
throw new Exception\EncryptionException('file header broken, no supported cipher defined', |
|
|
|
|
Exception\EncryptionException::UNKNOWN_CIPHER); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return $cipher; |
|
|
|
|
|